Skip to content

fix(autograd): stop ops::reshape from doubling gradients - #4

Merged
BESTTOOLBOX merged 1 commit into
codex/parallel-demo-dashboardfrom
codex/fix-reshape-double-grad
Oct 15, 2026
Merged

BESTTOOLBOX merged 1 commit into
codex/parallel-demo-dashboardfrom
codex/fix-reshape-double-grad

Conversation

@Johnnybyzhang

@Johnnybyzhang Johnnybyzhang commented Oct 13, 2026 •

Copy link
Copy Markdown

Summary

Fixes #5.

With the default autograd engine (USE_NEW_AUTOGRAD_ENGINE=ON), ops::reshape registered a ReshapeBackward node that Tensor::reshape had already registered for the same result. Engine::register_node appends edges, so every input passed through ops::reshape received its gradient twice.

LoRALinear reshapes the LoRA output, so both LoRA gradients (A and B) were exactly 2×. This is the LoRAStepAlignment failure that every operator CI run has shown since June:

LoRA grad_A mismatch: max_abs_diff=0.30073 tolerance=1e-06

The fix removes the duplicate registration under the new engine. The legacy-engine path is unchanged, since it overwrites grad_fn instead of adding an edge.

Impact

Correction: an earlier version of this description said Adam-trained runs were "probably only slightly affected". That was wrong.

  • The error compounds: gradients are scaled by 2^k for a path through k ops::reshape calls.
  • It changes direction, not just size: in an attention block, the residual path (no reshape) and the attention path (several reshapes) are weighted unequally.
  • Measured on a 2-layer Qwen model with LoRA:
    • LoRA gradient norms were 2–22× too large.
    • Overall cosine to the correct gradient was 0.73, and 11% of entries had the wrong sign.
    • With this fix, the gradients match finite differences.

Full analysis and reproduction: #5.

Testing

On macOS:

cmake -S operator -B operator/build -DCMAKE_BUILD_TYPE=Release -DOPERATORS_WARNINGS_AS_ERRORS=ON
cmake --build operator/build
ctest --test-dir operator/build --output-on-failure

Result: 100% tests passed out of 29, including LoRAStepAlignment. On the base branch, 28/29 pass, with the failure above.

PR #3 (codex/kto-bounded-memory) fails CI for the same pre-existing reason. It will pass once this merges and #3 is rebased.

🤖 Generated with Claude Code

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 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T10:50:48.703415Z 00c8059 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants