Commit 24f4924
## Description
`compute_shape_offset` in `monai/data/utils.py` passes `spatial_shape`
directly to `np.array()`. When `spatial_shape` is a `torch.Tensor`, this
relies on the non-tuple sequence indexing protocol, which PyTorch
removed in version 2.9. The call raises a hard error on PyTorch ≥ 2.9.
The fix is to wrap `spatial_shape` in `tuple()` before passing it to
`np.array()`. This routes through `__iter__`, which is stable across all
PyTorch versions, and produces 0-d scalar tensors that NumPy consumes
correctly.
## Root cause
The direct caller in `monai/transforms/spatial/functional.py` (line 115)
constructs an `in_spatial_size` as a `torch.Tensor` and passes it
straight to `compute_shape_offset`. This path has been broken since
PyTorch 2.9.
## Changes
- `monai/data/utils.py` — one-character change: `np.array(spatial_shape,
...)` → `np.array(tuple(spatial_shape), ...)`
- `tests/data/utils/test_compute_shape_offset.py`: new unit tests
covering `torch.Tensor`, `np.ndarray`, and plain list inputs
Fixes #8775
---------
Signed-off-by: UGBOMEH OGOCHUKWU WILLIAMS <williamsugbomeh@gmail.com>
Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent cc92126 commit 24f4924
1 file changed
+35
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
23 | | - | |
| 22 | + | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
45 | 78 | | |
46 | 79 | | |
47 | 80 | | |
0 commit comments