You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add Apple M-series (MPS) device support to Python scripts #977
Most standalone Python scripts (.py) only detect CUDA or fall back to CPU, ignoring Apple Silicon's MPS backend. This means users with M1/M2/M3/M4 Macs always fall back to CPU, missing significant GPU acceleration.
MPS is stuck in no-man's land, too slow for large models, overkill overhead for small ones. i feel its much slower.
you may use runpod and any other cloud gpu to accelerate training. one of the reason could be Apple unified memory is shared between CPU/GPU.
The three-tier pattern is exactly right, and it covers all 29 files cleanly. Two nuances worth handling while you're there:
1. pin_memory in DataLoaders must be CUDA-conditional pin_memory=True is silently ignored (or throws on some builds) when the device isn't CUDA. Safe pattern:
As of torch 2.3 MPS compile support is still partial — the compiled path silently falls back to eager mode on many ops, so you only lose startup time, but it's cleaner to skip it.
3. bfloat16 is not supported on MPS (use float16 instead)
Any place that does model.to(torch.bfloat16) will crash on Metal. A dtype helper keeps this tidy:
Just as a heads-up I plan on this in Nov/Dec and reevaluate all the code carefully on MPS with the latest PyTorch then and see if MPS can now be safely recommended. In the meantime, please don't worry about it, I'll take care of it then!
Reacted by Kushaan koul, Rene Leonhardt and Michal Vácha
Activity
rasbt commentedon Mar 9, 2026
If someone reads this, this omission of mps was on purpose (see #625 #644 #442 #846)
Perhaps worthwhile revisiting some time in the future though.
amit-chaubey commentedon Mar 19, 2026
If you use mps
if you use cpu
MPS is stuck in no-man's land, too slow for large models, overkill overhead for small ones. i feel its much slower.
you may use runpod and any other cloud gpu to accelerate training. one of the reason could be Apple unified memory is shared between CPU/GPU.
Thanks for amazing book @rasbt
kushdab commentedon Jun 26, 2026
The three-tier pattern is exactly right, and it covers all 29 files cleanly. Two nuances worth handling while you're there:
1.
pin_memoryin DataLoaders must be CUDA-conditionalpin_memory=Trueis silently ignored (or throws on some builds) when the device isn't CUDA. Safe pattern:2.
torch.compilefalls back or errors on MPSGuard any
torch.compile(model)call:As of torch 2.3 MPS compile support is still partial — the compiled path silently falls back to eager mode on many ops, so you only lose startup time, but it's cleaner to skip it.
3.
bfloat16is not supported on MPS (usefloat16instead)Any place that does
model.to(torch.bfloat16)will crash on Metal. A dtype helper keeps this tidy:Happy to help review the diff once a PR is up.
rasbt commentedon Aug 26, 2026
Just stumbled upon a great investigative write-up on the topic: "Finding a Bug in Closed-Source Shader Kernels", https://scottkirila.studio.site/blog/kernel-bug
mikemikimike commentedon Aug 27, 2026
Implemented in PR #1073.
PYTHONPATH=pkg python -m pytest pkg/llms_from_scratch/tests/test_device.py(3 passed).git diff --checkpassed.rasbt commentedon Sep 30, 2026
Just as a heads-up I plan on this in Nov/Dec and reevaluate all the code carefully on MPS with the latest PyTorch then and see if MPS can now be safely recommended. In the meantime, please don't worry about it, I'll take care of it then!