Skip to content

The best ChatGPT that $100 can buy.

Python

docs: note that MPS handles bf16 fine on recent macOS (opt in via NANOCHAT_DTYPE=bfloat16, ~25% memory savings). Thank you @eaglstun #793 and @ifsheldon #685

Andrej Karpathy5e43f1eb81c6
diff --git a/README.md b/README.md
index 87a9381..527efe5 100644
--- a/README.md
+++ b/README.md
@@ -108,7 +108,7 @@ nanochat does not use `torch.amp.autocast`. Instead, precision is managed explic
|----------|--------------|-----|
| CUDA SM 80+ (A100, H100, ...) | `bfloat16` | Native bf16 tensor cores |
| CUDA SM < 80 (V100, T4, ...) | `float32` | No bf16; fp16 available via `NANOCHAT_DTYPE=float16` (uses GradScaler) |
-| CPU / MPS | `float32` | No reduced-precision tensor cores |
+| CPU / MPS | `float32` | Safe default. On recent macOS, MPS also runs `NANOCHAT_DTYPE=bfloat16` fine (~25% less memory, similar speed) |
You can override the default with the `NANOCHAT_DTYPE` environment variable:
diff --git a/nanochat/common.py b/nanochat/common.py
index bd14fd2..c1ad442 100644
--- a/nanochat/common.py
+++ b/nanochat/common.py
@@ -27,6 +27,7 @@ def _detect_compute_dtype():
# fp16 training requires GradScaler (not yet implemented), so fall back to fp32.
# Users can still force fp16 via NANOCHAT_DTYPE=float16 if they know what they're doing.
return torch.float32, f"auto-detected: CUDA SM {capability[0]}{capability[1]} (pre-Ampere, bf16 not supported, using fp32)"
+ # Note: MPS on recent macOS also handles bf16 fine, opt in via NANOCHAT_DTYPE=bfloat16
return torch.float32, "auto-detected: no CUDA (CPU/MPS)"
COMPUTE_DTYPE, COMPUTE_DTYPE_REASON = _detect_compute_dtype()