What problem does it solve?
This Skill prevents flaky or misleading unit tests for PyTorch by enforcing correct tensor comparisons, tolerance handling, dtype/shape/device checks, deterministic RNG usage, and gradient-flow validation.
Core Features & Use Cases
- Robust Tensor Assertions: Use
torch.testing.assert_close with explicit rtol and atol instead of unsafe == checks for tensors, avoiding incorrect boolean semantics and brittle equality failures.
- Invariant Validation: Assert shapes, dtypes, and device placement so silent upcasts, reshaping errors, or CPU/GPU mismatches are caught immediately.
- Autograd & Determinism Checks: Verify gradients after
backward() and ensure deterministic behavior via controlled seeding for reproducible results.
- Use Cases: Testing tensor math correctness, validating custom layers and loss functions, checking training-loop gradients, and ensuring model outputs are stable across refactors.
Quick Start
Use the 323-pytorch-unittest skill when writing or reviewing any pytest/unittest that imports torch so your assertions verify values, shapes/dtypes, device placement, gradients, and determinism with appropriate tolerances.