From 00c8059001eea3bed37695374ccbc67e748c562d Mon Sep 17 00:00:00 2001 From: Johnnybyzhang Date: Tue, 13 Oct 2026 06:49:02 -0400 Subject: [PATCH] fix(autograd): stop ops::reshape from doubling gradients Tensor::reshape already registers ReshapeBackward for its result with the new autograd engine. ops::reshape then registered it a second time, and Engine::register_node appends edges, so the input received the gradient twice. LoRALinear reshapes the LoRA output, so both LoRA gradients were doubled. This is the LoRAStepAlignment failure that every operator CI run has shown since June (grad_A max_abs_diff=0.30073). Co-Authored-By: Claude Opus 5.5 --- operator/finetune_ops/core/ops.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/operator/finetune_ops/core/ops.cpp b/operator/finetune_ops/core/ops.cpp index e7e0057..b35b25a 100644 --- a/operator/finetune_ops/core/ops.cpp +++ b/operator/finetune_ops/core/ops.cpp @@ -1988,10 +1988,9 @@ TensorPtr reshape(const TensorPtr& tensor, const std::vector& shape) { if (tensor->requires_grad()) { result->set_requires_grad(true); - auto backward_fn = std::make_shared(tensor->shape()); - #ifdef USE_NEW_AUTOGRAD_ENGINE - register_backward(result, {tensor}, backward_fn); + // Tensor::reshape already registered ReshapeBackward for `result`. + // Registering it again adds a second edge and doubles the gradient. #else result->set_grad_fn([tensor, original_shape = tensor->shape()](const TensorPtr& grad_output) -> std::vector { // [Translated comment removed - see documentation]