Skip to content

Run Newton-Schulz in float32 on CPU - #68

Open
shaneraphel wants to merge 5 commits into
KellerJordan:masterfrom
shaneraphel:cpu-float32-newtonschulz
Open

shaneraphel wants to merge 5 commits into
KellerJordan:masterfrom
shaneraphel:cpu-float32-newtonschulz

Conversation

@shaneraphel

@shaneraphel shaneraphel commented Sep 22, 2026 •

Copy link
Copy Markdown

Summary

  • zeropower_via_newtonschulz5 keeps the same quintic coefficients.
  • Tensors on CPU use float32. CUDA and MPS stay on bfloat16.
  • SingleDeviceMuon.step picks this up without an API change.
  • Tests pin both dtypes: float32 on CPU, bfloat16 on CUDA/MPS when present (skipped otherwise).

Evidence

Apple M4, PyTorch 2.11.0, 10 CPU threads, 5 Newton-Schulz steps, median of 5 calls. MPS timed with Metal debug wrappers off.

device n bfloat16 float32 float32 vs bfloat16
CPU 256 0.0816 s 0.00216 s 38x faster
CPU 1024 10.50 s 0.0459 s 229x faster
MPS 256 0.00193 s 0.00184 s 1.05x
MPS 1024 0.0161 s 0.0196 s 0.82x

Max absolute difference, CPU n=256, same seed: 0.008578. MPS n=1024 max abs difference: 0.00412. python test_cpu_zeropower.py prints branch ok mps then ok 0.00857803225517273.

Float32 is the CPU path only. On MPS, bfloat16 is the faster of the two at n=1024, so that device is unchanged.

Test plan

  • python test_cpu_zeropower.py on CPU (dtype, finite, error bound, one optimizer step)
  • MPS branch pinned to bfloat16 in the same test
  • CUDA branch runs in CI or on a CUDA machine (skipped here, no CUDA hardware)

Prepared with an AI assistant. The table was measured on this machine before the pull request was updated. No integer kernel is included.

The quintic is unchanged. bfloat16 stays the path for every other device. On an Apple M4, a 256x256 matrix was about 32x faster in float32, with max absolute difference 0.0086 against the bfloat16 quintic.
@shaneraphel
shaneraphel force-pushed the cpu-float32-newtonschulz branch from 751ae66 to 1cb9958 Compare September 22, 2026 15:53
The quintic comparison already bounds the absolute error. This pins the dtype of the CPU path.
Apple M4, PyTorch 2.11, 10 threads. At n=1024 the MPS quintic is 0.0161 s in bfloat16 and 0.0196 s in float32. The CPU path stays float32, where the same size is 0.046 s against 10.5 s in bfloat16.
Apple M4, PyTorch 2.11, 10 threads. At n=1024 the MPS quintic is 0.0161 s in bfloat16 and 0.0196 s in float32. The CPU path stays float32, where the same size is 0.046 s against 10.5 s in bfloat16.
The CPU test pins float32. This runs the same function on CUDA or MPS when present and pins bfloat16 there, so the GPU path cannot silently change dtype. Each device is skipped when unavailable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant