[PR #1871] [MERGED] fix(autograd): tracked_mul passes raw scalar to MulBackward, causing .data crash on backward #27332

Closed
opened 2026-06-18 16:55:24 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/harvard-edge/cs249r_book/pull/1871
Author: @Shashank-Tripathi-07
Created: 6/16/2026
Status: Merged
Merged: 6/16/2026
Merged by: @profvjreddi

Base: mainHead: fix/tracked-mul-scalar-tensor


📝 Commits (1)

  • 47baaea fix tracked_mul passing raw scalar to MulBackward instead of a Tensor

📊 Changes

1 file changed (+2 additions, -4 deletions)

View changed files

📝 tinytorch/src/06_autograd/06_autograd.py (+2 -4)

📄 Description

What breaks

tracked_mul() wraps a scalar operand into other_tensor = Tensor(other) correctly, but then builds the grad function with the original scalar:

result._grad_fn = MulBackward(self, other)   # BUG: raw scalar, not other_tensor

MulBackward.apply() does a, b = self.saved_tensors and then b.data to compute the gradient. Calling .data on a Python int or float raises AttributeError. Any scalar multiplication inside a requires_grad=True computation graph crashes during backward().

Fix

result._grad_fn = MulBackward(self, other_tensor)  # always a Tensor

other_tensor is already constructed and initialized with _ensure_grad_attrs. The forward value is unaffected -- _original_mul still receives the original other.

Test plan

  • pytest tinytorch/tests/06_autograd/ passes
  • Scalar mul backward: x = Tensor([2.0], requires_grad=True); (x * 3.0).backward() -- x.grad should be [3.0]

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/harvard-edge/cs249r_book/pull/1871 **Author:** [@Shashank-Tripathi-07](https://github.com/Shashank-Tripathi-07) **Created:** 6/16/2026 **Status:** ✅ Merged **Merged:** 6/16/2026 **Merged by:** [@profvjreddi](https://github.com/profvjreddi) **Base:** `main` ← **Head:** `fix/tracked-mul-scalar-tensor` --- ### 📝 Commits (1) - [`47baaea`](https://github.com/harvard-edge/cs249r_book/commit/47baaeabade6d33f0874f6e8a014d8d7ada7cf52) fix tracked_mul passing raw scalar to MulBackward instead of a Tensor ### 📊 Changes **1 file changed** (+2 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `tinytorch/src/06_autograd/06_autograd.py` (+2 -4) </details> ### 📄 Description ## What breaks `tracked_mul()` wraps a scalar operand into `other_tensor = Tensor(other)` correctly, but then builds the grad function with the original scalar: ```python result._grad_fn = MulBackward(self, other) # BUG: raw scalar, not other_tensor ``` `MulBackward.apply()` does `a, b = self.saved_tensors` and then `b.data` to compute the gradient. Calling `.data` on a Python `int` or `float` raises `AttributeError`. Any scalar multiplication inside a `requires_grad=True` computation graph crashes during `backward()`. ## Fix ```python result._grad_fn = MulBackward(self, other_tensor) # always a Tensor ``` `other_tensor` is already constructed and initialized with `_ensure_grad_attrs`. The forward value is unaffected -- `_original_mul` still receives the original `other`. ## Test plan - [ ] `pytest tinytorch/tests/06_autograd/` passes - [ ] Scalar mul backward: `x = Tensor([2.0], requires_grad=True); (x * 3.0).backward()` -- `x.grad` should be `[3.0]` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-06-18 16:55:24 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/cs249r_book#27332