Storage contract
Two tensors can have the same shape and the same logical values, yet require different address calculations. Checking one packed representation does not establish that a reader handles the other. This original CPU worksheet makes that distinction visible without running PyTorch or CUDA.[7][4]
Both example inputs represent [[1, 2, 3], [4, 5, 6]], with shape 2×3. The operation doubles each logical element into a fresh packed output. The intended result is [[2, 4, 6], [8, 10, 12]]. One storage list is [1, 2, 3, 4, 5, 6], with element strides [3, 1]. A second is [1, 4, 2, 5, 3, 6], with strides [1, 2]: the layout obtained by transposing a packed 3×2 base holding those values.[7][1]
The same logical position now has a different storage index. From the underlying storage base, the index is offset + row×row_stride + column×column_stride. A reader that substitutes row×columns + column assumes packed row-major storage. On the transpose-derived case, it produces [[2, 8, 4], [10, 6, 12]]. Every read remains in bounds. The program returns a complete answer, but several elements are wrong.[7][1]
Even a sum check misses this particular permutation: both outputs sum to 42. That does not make summation an invalid operation. It means a sum alone is insufficient to validate the specified elementwise result. The proof compares every position against the independently declared logical values.[6]
The full fixture retains five layouts: packed, transpose-derived, a stepped slice, packed with an offset of two elements, and a stepped slice with an offset of one. All encode the same six logical integers. The stride-and-offset-aware reader matches all five. The packed reader starts at the correct first element and matches the two packed cases; it fails the three strided cases. A separate deliberately faulty reader uses the right strides but starts at the allocation base, so it fails the two offset cases.[6]
Pointer origin is a separate part of the contract. PyTorch documents storage_offset in elements, while Tensor.data_ptr addresses the tensor’s first element. If a kernel receives that first-element pointer, the offset is already included: do not add it again. A view can be contiguous with a nonzero storage offset. The offset-only positive control is included specifically to avoid treating offset and contiguity as the same property.[2][3][4]
Units matter too. PyTorch documents strides as jumps along tensor dimensions; this worksheet uses element counts. NVIDIA’s cudaMallocPitch interface describes a row pitch in bytes that can include padding. A byte pitch cannot be substituted directly for an element stride. These official references explain address contracts; none of their implementations is under test here.[1][5]
A custom operator can support the declared layouts, require a particular layout and reject unsupported inputs, or make an explicit packed copy before its restricted path. The worksheet includes that copy as a control and it preserves all five logical outputs. It does not measure the copy’s cost. PyTorch’s contiguous() may return the existing tensor when it is already contiguous, so it should not be treated as a promise to reset the storage offset.[4][7]
One original standard-library invocation retained all five layouts, all fifteen reader results and fifteen focused controls. The MIT package contains every storage list, address, output and mismatch position. The model uses positive-stride, nonoverlapping rank-two integer reads. It does not cover broadcast or negative strides, channels-last layouts, aliasing writes, alignment, vectorized accesses or real memory safety. The authored failures establish a narrow contract counterexample, not a bug in a real kernel or grader, a failure rate, or a GPU performance or training result.[7][6]
- Name the pointer origin: Storage-base indexing includes the storage offset. A tensor first-element pointer already includes it.
- Keep the full output: The transpose-derived counterexample preserves the sum 42 while permuting elementwise results.
- Specify the supported layouts and pointer origin before using a correctness score to evaluate an operator.
All five authored layouts encode the same 2×3 logical matrix. Strides and offsets count elements, not bytes.
| Layout | Strides | Offset | Aware reader | Assume packed | Wrong storage base |
|---|---|---|---|---|---|
| Packed | [3, 1] | 0 | Match | Match | Match |
| Transpose-derived | [1, 2] | 0 | Match | Wrong values | Match |
| Stepped slice | [6, 2] | 0 | Match | Wrong values | Match |
| Packed, offset 2 | [3, 1] | 2 | Match | Match | Wrong values |
| Stepped, offset 1 | [6, 2] | 1 | Match | Wrong values | Wrong values |
- Assume packed honors the first-element offset but ignores strides. Wrong storage base honors strides but omits offset.
- All deliberate faulty-reader accesses in the five fixtures are in bounds. These are designed cases, not failure-frequency estimates.
Sources
- PyTorch2.15 Tensor.stride docs.pytorch.org
- PyTorch2.15 Tensor.storage_offset docs.pytorch.org
- PyTorch2.15 Tensor.data_ptr docs.pytorch.org
- PyTorch2.15 Tensor Views docs.pytorch.org
- CUDA Runtime API13.5, Memory Management, cudaMallocPitch docs.nvidia.com
- Complete authored layout result data.nodus-compute.ai
- Original MIT source, fixtures and proof data.nodus-compute.ai
![Authored CPU integer model, no PyTorch or CUDA execution. Two logical 2x3 inputs both equal [[1,2,3],[4,5,6]]. Packed storage [1,2,3,4,5,6] has strides [3,1]; transpose-derived storage [1,4,2,5,3,6] has strides [1,2]. Both offsets are 0. A reader assuming packed addresses doubles the first to [[2,4,6],[8,10,12]], but the second to [[2,8,4],[10,6,12]]. Both output sums are 42, but elementwise results differ. Storage-base index uses offset plus row and column strides. A first-element pointer already includes offset. Five layouts, 15 reader results and 15 controls retained; no actual kernel bug or performance claim.](/assets/lab-research-stride-contract-card.png)