Repository navigation
Add PyTorch tensor serialization and assertion helpers - #63
Merged
Merged
Conversation
Serialize torch.Tensor values across the sandbox (detached, on CPU) in json_utils, and add assert_tensor_allclose / assert_tensor_equal with student-friendly failure messages. torch is a lazy, optional import so the lite and full images are unaffected. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This was referenced Nov 25, 2026
Gate the torch dev dependency below Python 3.14 (no wheels yet) so the lock resolves and the 3.14 job skips the torch tests, and guard the test's torch typing behind TYPE_CHECKING so mypy resolves torch.Tensor. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds first-class PyTorch support to the grader core, independent of any image:
json_utils.py: atorch.Tensorreturned by student code now round-trips across the sandbox (detached, moved to CPU, dtype preserved). Lazyimport torch, so the lite/full images are unaffected.assert_tensor_allcloseandassert_tensor_equalwith student-friendly failure messages (shape/dtype mismatch, number of differing elements, max absolute difference). Exported from the package root.This is the foundation for the
:torchimage (stacked on top of this PR) but is useful on its own to anyone extending the grader with torch today.Testing
tests/test_torch.py(skipped when torch isn't installed): serialization round-trips for float/int/bool/complex/scalar/empty tensors, grad/device detachment, and the two assertion helpers' pass/fail paths including NaN and dtype handling. 19 passing on Python 3.13.test_serialize.pystill green (no regression in the sharedto_json/from_json). mypy clean on the changed modules.🤖 Generated with Claude Code