[PR #1872] [MERGED] fix(tensor): reshape -1 silently truncates when size is not divisible #27333

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

📋 Pull Request Information

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

Base: mainHead: fix/reshape-neg1-divisibility-check


📝 Commits (1)

  • 74b2118 fix reshape -1 to check divisibility before inferring the dimension

📊 Changes

1 file changed (+7 additions, -0 deletions)

View changed files

📝 tinytorch/src/01_tensor/01_tensor.py (+7 -0)

📄 Description

What breaks

Tensor.reshape(-1, n) infers the unknown dimension with self.size // known_size (integer division). When self.size is not divisible by known_size, floor division silently produces a wrong truncated value.

Example:

Tensor([1,2,3,4,5,6,7]).reshape(-1, 3)
# known_size = 3, self.size = 7
# unknown_dim = 7 // 3 = 2  (silently truncated!)
# new_shape = (2, 3)
# np.prod((2,3)) = 6 != 7 → raises generic "element count mismatch"

The element-count guard does eventually catch it, but with a confusing message that doesn't explain why the -1 inference failed. PyTorch raises immediately:

RuntimeError: shape '[-1, 3]' is invalid for input of size 7

Fix

Check self.size % known_size != 0 before the division and raise a clear ValueError explaining that the total size is not divisible by the known dimensions product.

Test plan

  • pytest tinytorch/tests/01_tensor/ passes
  • Tensor(np.ones(7)).reshape(-1, 3) raises ValueError with clear message
  • Tensor(np.ones(6)).reshape(-1, 3) still works, returns shape (2, 3)

🔄 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/1872 **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/reshape-neg1-divisibility-check` --- ### 📝 Commits (1) - [`74b2118`](https://github.com/harvard-edge/cs249r_book/commit/74b2118dab7e5d41b21c8b00e2a6a50bc36b6f82) fix reshape -1 to check divisibility before inferring the dimension ### 📊 Changes **1 file changed** (+7 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `tinytorch/src/01_tensor/01_tensor.py` (+7 -0) </details> ### 📄 Description ## What breaks `Tensor.reshape(-1, n)` infers the unknown dimension with `self.size // known_size` (integer division). When `self.size` is not divisible by `known_size`, floor division silently produces a wrong truncated value. Example: ```python Tensor([1,2,3,4,5,6,7]).reshape(-1, 3) # known_size = 3, self.size = 7 # unknown_dim = 7 // 3 = 2 (silently truncated!) # new_shape = (2, 3) # np.prod((2,3)) = 6 != 7 → raises generic "element count mismatch" ``` The element-count guard does eventually catch it, but with a confusing message that doesn't explain *why* the -1 inference failed. PyTorch raises immediately: ``` RuntimeError: shape '[-1, 3]' is invalid for input of size 7 ``` ## Fix Check `self.size % known_size != 0` before the division and raise a clear `ValueError` explaining that the total size is not divisible by the known dimensions product. ## Test plan - [ ] `pytest tinytorch/tests/01_tensor/` passes - [ ] `Tensor(np.ones(7)).reshape(-1, 3)` raises `ValueError` with clear message - [ ] `Tensor(np.ones(6)).reshape(-1, 3)` still works, returns shape `(2, 3)` --- <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:26 -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#27333