Skip to content

⚡ Bolt: [performance improvement] Option::take() for Tensor ownership in backward pass#110

Open
teerthsharma wants to merge 1 commit into
masterfrom
jules-5931644591477573448-d29da489
Open

⚡ Bolt: [performance improvement] Option::take() for Tensor ownership in backward pass#110
teerthsharma wants to merge 1 commit into
masterfrom
jules-5931644591477573448-d29da489

Conversation

@teerthsharma
Copy link
Copy Markdown
Owner

💡 What: Modified accumulate_grad to take ownership (grad: Tensor) instead of a reference. In the backward method, Option::take() is now used to pull the gradient out of the grads vector so that it can be passed by value, and then re-inserted afterwards. For operations like Op::Mul, Op::MatMul, and Op::ReLU which dynamically create partial derivative tensors, this completely avoids cloning internal Tensor metadata.

🎯 Why: During reverse-mode automatic differentiation, the backward pass operates in a tight loop. Cloning a Tensor to satisfy borrow checker constraints triggers new vector allocations for its shape and strides fields. Removing this drastically reduces GC pressure and speeds up training.

📊 Impact: Reduces memory overhead and allocation time inside the backward pass by avoiding metadata cloning for Op::Add, Op::Mul, Op::MatMul, and Op::ReLU.

🔬 Measurement: Verifiable by checking that cargo test -p aether-core continues to pass correctly. Performance gains can be benchmarked on larger MLP models like test_mlp_large_scale.


PR created automatically by Jules for task 5931644591477573448 started by @teerthsharma

… in backward pass

What: Use `Option::take()` in `autograd::Context::backward` to extract `Tensor` ownership from `grads` and pass it to `accumulate_grad`.
Why: Bypasses borrow checker conflicts that forced expensive `.clone()` operations on `Option<Tensor>` and its heap-allocated metadata (shape and strides) arrays inside the hot reverse-mode propagation loop.
Impact: Eliminates O(N) heap allocations for every operation on the gradient tape (`Add`, `Mul`, `MatMul`, `ReLU`) when propagating derivatives backward.
Measurement: Compare backpropagation time in deep network training loops before and after this optimization. Tests passed successfully with no functional changes.

Co-authored-by: teerthsharma <[email protected]>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

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.

1 participant