[PR #1392] [MERGED] test(tinytorch): PyTorch-compat test coverage for Tensor API additions #8160

Closed
opened 2026-04-27 17:28:06 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/harvard-edge/cs249r_book/pull/1392
Author: @Shashank-Tripathi-07
Created: 4/17/2026
Status: Merged
Merged: 4/20/2026
Merged by: @profvjreddi

Base: devHead: feat/tensor-pytorch-compat-api


📝 Commits (2)

  • 3b0fd02 test(tinytorch): add PyTorch-compat coverage for ndim, numel, view, contiguous, masked_fill
  • 4de0f71 feat(tinytorch): add view(), masked_fill(), and Tensor stacking to src

📊 Changes

2 files changed (+109 additions, -1 deletions)

View changed files

📝 tinytorch/src/01_tensor/01_tensor.py (+24 -1)
📝 tinytorch/tests/01_tensor/test_tensor_core.py (+85 -0)

📄 Description

Summary

Adds TestTensorPyTorchCompat, 10 focused tests covering the PyTorch-compatible Tensor methods that landed in #1391 and the subsequent merge window (view, masked_fill, Tensor stacking).

Also syncs src/01_tensor/01_tensor.py with the exported package: view(), masked_fill(), and Tensor stacking were present in core/tensor.py but missing from the source file, causing CI failures.

Closes #1298.

Why these tests matter: students porting PyTorch training loops to TinyTorch will call these methods constantly. Without explicit coverage, a refactor could silently break the behavioral contracts. The -inf attention-mask pattern in masked_fill is especially tricky: it produces no exception, just wrong softmax output.

Each test targets one specific contract:

Test What it pins
test_ndim ndim matches len(shape) for rank 0-3
test_numel numel() == .size
test_view_same_as_reshape view() is a reshape alias
test_view_with_minus_one -1 inference passes through
test_contiguous_returns_correct_data values preserved
test_contiguous_is_c_contiguous C-order layout guaranteed
test_masked_fill_basic True positions replaced
test_masked_fill_attention_pattern -inf pattern for transformers
test_masked_fill_does_not_mutate_original no in-place side effects
test_tensor_from_tensor_list Tensor([t1, t2]) stacking

Test plan

  • pytest tinytorch/tests/01_tensor/test_tensor_core.py::TestTensorPyTorchCompat -v all 10 pass
  • Full test file runs clean with no regressions in existing test classes

🔄 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/1392 **Author:** [@Shashank-Tripathi-07](https://github.com/Shashank-Tripathi-07) **Created:** 4/17/2026 **Status:** ✅ Merged **Merged:** 4/20/2026 **Merged by:** [@profvjreddi](https://github.com/profvjreddi) **Base:** `dev` ← **Head:** `feat/tensor-pytorch-compat-api` --- ### 📝 Commits (2) - [`3b0fd02`](https://github.com/harvard-edge/cs249r_book/commit/3b0fd02b0fd50b4ddb445418a7d6abf4d49e1a4f) test(tinytorch): add PyTorch-compat coverage for ndim, numel, view, contiguous, masked_fill - [`4de0f71`](https://github.com/harvard-edge/cs249r_book/commit/4de0f7164c181ecfb39841b8eae41e16047910c7) feat(tinytorch): add view(), masked_fill(), and Tensor stacking to src ### 📊 Changes **2 files changed** (+109 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `tinytorch/src/01_tensor/01_tensor.py` (+24 -1) 📝 `tinytorch/tests/01_tensor/test_tensor_core.py` (+85 -0) </details> ### 📄 Description ## Summary Adds `TestTensorPyTorchCompat`, 10 focused tests covering the PyTorch-compatible Tensor methods that landed in #1391 and the subsequent merge window (view, masked_fill, Tensor stacking). Also syncs `src/01_tensor/01_tensor.py` with the exported package: `view()`, `masked_fill()`, and Tensor stacking were present in `core/tensor.py` but missing from the source file, causing CI failures. Closes #1298. **Why these tests matter:** students porting PyTorch training loops to TinyTorch will call these methods constantly. Without explicit coverage, a refactor could silently break the behavioral contracts. The -inf attention-mask pattern in `masked_fill` is especially tricky: it produces no exception, just wrong softmax output. Each test targets one specific contract: | Test | What it pins | |---|---| | `test_ndim` | ndim matches len(shape) for rank 0-3 | | `test_numel` | numel() == .size | | `test_view_same_as_reshape` | view() is a reshape alias | | `test_view_with_minus_one` | -1 inference passes through | | `test_contiguous_returns_correct_data` | values preserved | | `test_contiguous_is_c_contiguous` | C-order layout guaranteed | | `test_masked_fill_basic` | True positions replaced | | `test_masked_fill_attention_pattern` | -inf pattern for transformers | | `test_masked_fill_does_not_mutate_original` | no in-place side effects | | `test_tensor_from_tensor_list` | Tensor([t1, t2]) stacking | ## Test plan - [x] `pytest tinytorch/tests/01_tensor/test_tensor_core.py::TestTensorPyTorchCompat -v` all 10 pass - [x] Full test file runs clean with no regressions in existing test classes --- <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-04-27 17:28:06 -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#8160