Skip to content

Add PyTorch tensor serialization and assertion helpers - #63

Merged
eliotwrobson merged 2 commits into
mainfrom
torch-tensor-support
Nov 26, 2026
Merged

eliotwrobson merged 2 commits into
mainfrom
torch-tensor-support

Conversation

@eliotwrobson

Copy link
Copy Markdown
Collaborator

Summary

Adds first-class PyTorch support to the grader core, independent of any image:

  • Tensor serialization in json_utils.py: a torch.Tensor returned by student code now round-trips across the sandbox (detached, moved to CPU, dtype preserved). Lazy import torch, so the lite/full images are unaffected.
  • Assertion helpers assert_tensor_allclose and assert_tensor_equal with 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 :torch image (stacked on top of this PR) but is useful on its own to anyone extending the grader with torch today.

Testing

  • New 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.py still green (no regression in the shared to_json/from_json). mypy clean on the changed modules.
  • torch is added to the dev group from the CPU index on Linux to keep CI light.

🤖 Generated with Claude Code

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>
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>
@eliotwrobson
eliotwrobson marked this pull request as ready for review November 26, 2026 04:14
@eliotwrobson
eliotwrobson merged commit 6aab360 into main Nov 26, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant