Repository navigation
fix(autograd): accumulate gradients for leaf tensor backward - #275
Open
superme1one wants to merge 1 commit into
Open
superme1one wants to merge 1 commit into
superme1one wants to merge 1 commit into
Conversation
This was referenced Nov 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Calling
Backward()directly on a leaf tensor withrequires_grad=truecurrently leaves its gradient null. Route the leaf throughAccumulateGrad, retaining the existing implicit/explicit gradient validation and accumulation hooks. The fresh accumulator keeps this backward call independent of forward consumers that have already been created from the leaf.Fixes #272.
Regression coverage includes implicit scalar gradients, repeated explicit vector-gradient accumulation, backward on a leaf with a live forward consumer, and invalid implicit/explicit gradient shapes. The new cases fail against the unmodified
masterimplementation.Validation on Linux x86_64, CPU Release, Clang 18.1.8/GCC 13.4.0 libstdc++, CMake 3.31.6, with CUDA/NCCL/OpenMP disabled:
test_tensor_cpu,test_autograd_cpu,test_hook_cpu,test_module_cpu,test_optimizer_cpu, andtest_checkpoint_cpu: all suites successful (six existing CPU skips for accelerator/autocast cases).The new tests use the existing device-parameterized tensor suite. Runtime validation in this submission was CPU-only.
Upstream CI currently requires maintainer approval before jobs can run; the three successful fork workflow results above use the same workflow definitions.