[PR #1866] [MERGED] fix(conv2d): Conv2dBackward return tuple must match saved_tensors length #27327

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

📋 Pull Request Information

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

Base: mainHead: fix/conv2d-backward-tuple-mismatch


📝 Commits (1)

  • 12e5280 fix Conv2dBackward return tuple to match saved_tensors length

📊 Changes

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

View changed files

📝 tinytorch/src/09_convolutions/09_convolutions.py (+3 -2)

📄 Description

What breaks

Conv2dBackward.__init__ registers either 2 or 3 tensors in saved_tensors depending on whether bias exists:

if bias is not None:
    super().__init__(x, weight, bias)   # 3 saved tensors
else:
    super().__init__(x, weight)         # 2 saved tensors

But apply() always returned a 3-tuple (grad_input, grad_weight, grad_bias).

The backward walk zips saved_tensors with the return tuple. When bias=None, zip silently truncates at 2 pairs, so grad_bias is produced but never consumed. This is fragile -- any reordering of saved_tensors or apply() output would silently corrupt weight updates.

Fix

Return only (grad_input, grad_weight) when bias is None, so tuple length always equals len(saved_tensors).

Test plan

  • Run pytest tinytorch/tests/09_convolutions/ -- all conv tests pass
  • Manually verify bias=False Conv2d still trains (weight grads non-zero)
  • Verify bias=True Conv2d unchanged behavior

🔄 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/1866 **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/conv2d-backward-tuple-mismatch` --- ### 📝 Commits (1) - [`12e5280`](https://github.com/harvard-edge/cs249r_book/commit/12e5280f479c559dcbd1710811c787bbe64b8095) fix Conv2dBackward return tuple to match saved_tensors length ### 📊 Changes **1 file changed** (+3 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `tinytorch/src/09_convolutions/09_convolutions.py` (+3 -2) </details> ### 📄 Description ## What breaks `Conv2dBackward.__init__` registers either 2 or 3 tensors in `saved_tensors` depending on whether bias exists: ```python if bias is not None: super().__init__(x, weight, bias) # 3 saved tensors else: super().__init__(x, weight) # 2 saved tensors ``` But `apply()` always returned a 3-tuple `(grad_input, grad_weight, grad_bias)`. The backward walk zips `saved_tensors` with the return tuple. When `bias=None`, zip silently truncates at 2 pairs, so `grad_bias` is produced but never consumed. This is fragile -- any reordering of `saved_tensors` or `apply()` output would silently corrupt weight updates. ## Fix Return only `(grad_input, grad_weight)` when `bias is None`, so tuple length always equals `len(saved_tensors)`. ## Test plan - [ ] Run `pytest tinytorch/tests/09_convolutions/` -- all conv tests pass - [ ] Manually verify bias=False Conv2d still trains (weight grads non-zero) - [ ] Verify bias=True Conv2d unchanged behavior --- <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:12 -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#27327