pytorch - 💡(How to fix) Fix `torch.empty((1,1), device="cpu", pin_memory=True).device` gives back MPS device on mac [1 participants]

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…
GitHub stats
pytorch/pytorch#181374Fetched 2026-04-25 06:02:47
View on GitHub
Comments
0
Participants
1
Timeline
15
Reactions
0
Author
Participants
Timeline (top)
mentioned ×5subscribed ×5labeled ×4renamed ×1

Error Message

But with MPS, I have no idea. If the answer is "there is no concept of pinned memory transfers" in MPS", I would expect the above case with device="cpu" to just not pin the memory (or at least error). If there is such a concept, this seems like a bug.

Code Example

import torch
assert torch.empty((1,1), device="cpu", pin_memory=True).device == torch.device("cpu") # errors
assert torch.empty((1,1), device="cpu", pin_memory=True).device == torch.device(type='mps', index=0) # doesn't

---

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.1 (arm64)
GCC version: Could not collect
Clang version: 16.0.0 (clang-1600.0.26.4)
CMake version: version 4.2.3
Libc version: N/A

Python version: 3.12.2 | packaged by conda-forge | (main, Feb 16 2024, 20:54:21) [Clang 16.0.6 ] (64-bit runtime)
Python platform: macOS-15.1-arm64-arm-64bit
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 M1 Pro
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

I admittedly have no idea how memory transfers on mac work, but I would sort of expect something else than the below

import torch
assert torch.empty((1,1), device="cpu", pin_memory=True).device == torch.device("cpu") # errors
assert torch.empty((1,1), device="cpu", pin_memory=True).device == torch.device(type='mps', index=0) # doesn't

I have a data loader that reads data off disk and puts it into a numpy.ndarray for context, which then will get put on the GPU after the data has been loaded off disk.

So, I'd like to get the most efficient numpy "view" possible of a torch Tensor so that I can write into it before transferring to the GPU (in the data loader).

AFAICT, with cuda, this means asking for a pinned memory buffer, which is simple with cupy and doable with pure torch if you're willing to track the numpy view after my_tensor.numpy() and the "underlying" torch.Tensor from which it was created simultaneously since updating the numpy view will update the torch tensor's data.

But with MPS, I have no idea. If the answer is "there is no concept of pinned memory transfers" in MPS", I would expect the above case with device="cpu" to just not pin the memory (or at least error). If there is such a concept, this seems like a bug.

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.1 (arm64)
GCC version: Could not collect
Clang version: 16.0.0 (clang-1600.0.26.4)
CMake version: version 4.2.3
Libc version: N/A

Python version: 3.12.2 | packaged by conda-forge | (main, Feb 16 2024, 20:54:21) [Clang 16.0.6 ] (64-bit runtime)
Python platform: macOS-15.1-arm64-arm-64bit
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 M1 Pro

cc @malfet @aditvenk @kulinseth @DenisVieriu97 @jhavukainen

extent analysis

TL;DR

The issue is likely due to the behavior of pin_memory=True on macOS with MPS devices, and a potential workaround is to check the documentation for any specific requirements or limitations for MPS.

Guidance

  • Investigate the PyTorch documentation for MPS-specific behavior regarding pin_memory=True to understand if there are any known limitations or workarounds.
  • Verify if the pin_memory=True argument has any effect on the device type, as the issue suggests that it might not work as expected with MPS devices.
  • Consider checking the PyTorch version and macOS version for any known issues or bugs related to memory management and MPS devices.
  • Look into alternative methods for efficient data transfer between CPU and GPU, such as using torch.Tensor methods or other libraries that support MPS devices.

Notes

The issue seems to be specific to the combination of PyTorch, macOS, and MPS devices, and more information might be needed to provide a definitive solution.

Recommendation

Apply workaround: The recommended approach is to investigate the PyTorch documentation and explore alternative methods for efficient data transfer, as the current behavior of pin_memory=True with MPS devices is unclear.

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