Skip to content

fix(autograd): register reshape backward exactly once - #6

Merged
BESTTOOLBOX merged 2 commits into
mainfrom
codex/fix-reshape-gradient-registration
Nov 12, 2026
Merged

BESTTOOLBOX merged 2 commits into
mainfrom
codex/fix-reshape-gradient-registration

Conversation

@AndyLu666

@AndyLu666 AndyLu666 commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Make Tensor::reshape the single owner of reshape's backward registration in the new autograd engine, and add explicit reshape/LoRA and Qwen finite-difference regressions.

This is adapted to this repository, not a blind cherry-pick of the MobileRLHF fix. Here, Tensor::set_grad_fn only stores a legacy callback: current ops::reshape has one correct engine edge, while direct member reshape/view lacks one. Moving the registration into the member fixes that missing path; removing the wrapper registration prevents the doubled-gradient regression when both paths are active.

Thanks to Johnny for identifying the duplicate-registration failure mode in MobileRLHF issue #5 and proposing MobileRLHF PR #4. The related tests are in MobileRLHF PR #6.

Changes

  • Register ReshapeBackward once in Tensor::reshape; preserve the existing legacy callback/non-leaf classification.
  • Keep the wrapper's legacy branch unchanged, but do not register a second new-engine edge.
  • Add 19 analytic cases: member/view/chained/mixed reshapes, wrapper chains, residuals, valid repeated-input add/mul, transpose-then-reshape, frozen input, and LoRA A/B gradients.
  • Add a deterministic two-layer actual Qwen LoRA graph checking all 448 gradient elements against two central-difference step sizes, evaluated from the forward objective independently of autodiff. Both A and B are nonzero. Reject nonfinite AD/FD values explicitly. Use explicit failure returns, not assertions, so the regressions remain active in Release builds.
  • Register both tests under BUILD_TESTS / USE_NEW_AUTOGRAD_ENGINE in CMake; CSV output stays in each build directory.

Validation

Local macOS results on upstream 086b1a027fe708d5eaca89b1ec2bf1a876fb80de:

Check Result
Pure Release, warnings-as-errors ON 22/22 CTest
Accelerate BLAS, assertions ON, warnings-as-errors OFF 22/22 CTest
Explicit analytic cases 19/19
Qwen finite differences, pure and BLAS 448/448 for each
Actual unmodified runtime control 5 member/view/mixed failures; wrapper chains and Qwen FD still correct
Constructed double-registration control 10 analytic failures; chained wrapper gradients 2/4/8; 446/448 Qwen FD failures
Pure forward preservation All 160 logits identical across upstream/fixed/double control
Existing pure Qwen gradient preservation All 448 gradients/FD entries identical upstream vs fixed
Legacy-engine library compile Passed
Five official entrypoint scripts, two-step synthetic smoke runs Passed

TokenizerHFGolden reports SKIP without an external fixture; its inclusion in the CTest passing count is not tokenizer alignment evidence. The constructed double-registration control is not the current upstream implementation. No task accuracy/convergence, full-pretrained training, or phone-runtime improvement is claimed.

BLAS + warnings-as-errors ON still fails on the existing unused matmul_fallback_nn / matmul_fallback_nt helpers. Those unrelated kernels are unchanged; the default pure warnings-as-errors CI configuration passes locally.

Reproduction:

cmake -S operator -B /tmp/mft-reshape-release -DCMAKE_BUILD_TYPE=Release -DUSE_BLAS=OFF -DOPERATORS_WARNINGS_AS_ERRORS=ON
cmake --build /tmp/mft-reshape-release -j4
ctest --test-dir /tmp/mft-reshape-release --output-on-failure

cmake -S operator -B /tmp/mft-reshape-blas -DCMAKE_BUILD_TYPE=Release -DUSE_BLAS=ON -DOPERATORS_WARNINGS_AS_ERRORS=OFF -DCMAKE_CXX_FLAGS=-DACCELERATE_NEW_LAPACK '-DCMAKE_CXX_FLAGS_RELEASE=-O3 -UNDEBUG'
cmake --build /tmp/mft-reshape-blas -j4
ctest --test-dir /tmp/mft-reshape-blas --output-on-failure

Only five source/build files are changed. No weights, datasets, logs, unrelated energy code, or MobileRLHF submodule changes are included.

GitHub operator-ci completed successfully for head eaf0cfbfd0dafddb043626a7348070813b7808fd, including C++ tests/install/consumer checks, Android visualizer build, five synthetic entrypoint scripts, and energy scheduler integration. Android build success is not a physical-phone runtime result.

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