Skip to content

[BUG] compute_shape_offset triggers PyTorch ≥2.9 breaking change for non-tuple sequence indexing #8775

Description

@AryanMoradi

Describe the bug

compute_shape_offset in monai/data/utils.py (line 883) passes spatial_shape directly to np.array(). When spatial_shape is a PyTorch tensor, this triggers the non-tuple sequence multidimensional indexing deprecation that became a breaking change in PyTorch 2.9+.

To reproduce

Run any pipeline that goes through SaveImagedNiftiWriter.set_metadataresample_if_neededspatial_resamplecompute_shape_offset with PyTorch ≥ 2.9.

Expected behavior

No error. spatial_shape should be converted to a plain Python tuple before being passed to np.array.

Actual behavior

On PyTorch ≥ 2.9, this either raises an error or produces silently wrong results.
On PyTorch 2.8 it was a deprecation warning.

Environment

  • MONAI: 1.5.2
  • PyTorch: 2.10.0+cu129
  • Python: 3.11
  • OS: Windows 11

Suggested fix

# monai/data/utils.py, line 883
# Before:
shape = np.array(spatial_shape, copy=True, dtype=float)
# After:
shape = np.array(tuple(spatial_shape), copy=True, dtype=float)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions