pytorch - 💡(How to fix) Fix [MPS] torch.isfinite() on non-contiguous 2D slice corrupts adjacent memory

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Fix Action

Fix / Workaround

Workaround:

torch.isfinite(tensor[:, 4:50].contiguous())  # OK
torch.isfinite(tensor[:, 4:50])               # corrupts adjacent memory

Code Example

torch.isfinite(tensor[:, 4:50].contiguous())  # OK
torch.isfinite(tensor[:, 4:50])               # corrupts adjacent memory

---

PyTorch version: 2.11.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 15.7.4 (arm64)
GCC version: Could not collect
Clang version: 17.0.0 (clang-1700.6.4.2)
CMake version: version 4.2.3
Libc version: N/A

Python version: 3.14.0 (main, Oct 10 2025, 12:54:13) [Clang 20.1.4 ] (64-bit runtime)
Python platform: macOS-15.7.4-arm64-arm-64bit-Mach-O
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
Caching allocator config: N/A

CPU:
Apple M4 Max

Versions of relevant libraries:
[pip3] Could not collect
[conda] Could not collect
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

torch.isfinite() on a non-contiguous column slice of a large 2D MPS tensor writes beyond its output buffer, silently corrupting adjacent MPS allocations. torch.isinf() is also affected.

Reproduction scriptmps_isfinite_bug.py ~ 100 lines (it's self-contained, only depends on torch).

Key observations:

  • Requires ~1.5M+ rows × 464 columns, non-contiguous column slice of 46 columns
  • The bug depends on MPS heap layout — the script retries up to 5 times and reproduces ~90% of runs
  • Calling .contiguous() before isfinite() prevents corruption
  • Affects both torch.isfinite() and torch.isinf()

Workaround:

torch.isfinite(tensor[:, 4:50].contiguous())  # OK
torch.isfinite(tensor[:, 4:50])               # corrupts adjacent memory

Versions

PyTorch version: 2.11.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 15.7.4 (arm64)
GCC version: Could not collect
Clang version: 17.0.0 (clang-1700.6.4.2)
CMake version: version 4.2.3
Libc version: N/A

Python version: 3.14.0 (main, Oct 10 2025, 12:54:13) [Clang 20.1.4 ] (64-bit runtime)
Python platform: macOS-15.7.4-arm64-arm-64bit-Mach-O
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
Caching allocator config: N/A

CPU:
Apple M4 Max

Versions of relevant libraries:
[pip3] Could not collect
[conda] Could not collect

cc @kulinseth @malfet @DenisVieriu97 @jhavukainen @aditvenk

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - 💡(How to fix) Fix [MPS] torch.isfinite() on non-contiguous 2D slice corrupts adjacent memory