Skip to content

[DistMuon] Send redistributed inputs and directions in BF16 - #5279

Merged
tianyu-l merged 5 commits into
pytorch:mainfrom
JavaZeroo:feat/distmuon-bf16-wire
Oct 13, 2026
Merged

tianyu-l merged 5 commits into
pytorch:mainfrom
JavaZeroo:feat/distmuon-bf16-wire

Conversation

@JavaZeroo

@JavaZeroo JavaZeroo commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Part of #5278.

Newton-Schulz casts its input to BF16 before it normalizes, so the prepared input and the direction can be sent in BF16 and the update keeps the same bits.

  • The bucket plan carries a transport_dtype. The exchange buffers and the scratch buffers are reserved in that dtype (keyed by device and dtype), and the plan hash includes it.
  • BlockShard parameters: each block is its own Muon matrix and can have its own lr ratio (match_rms_adamw uses sqrt(max(rows, cols))). main multiplies a block's direction by ratio / reference right after Newton-Schulz, in the FP32 compute scratch. In BF16 that multiply would round again, so this PR sends the raw direction and applies ratio / reference after it arrives, in the storage dtype, as a per-row scale (_direction_row_scale, rows from _block_row_spans). Blocks with the reference ratio get 1.0; when no block differs the multiply is skipped.

Numerics: parameter and momentum digests of every Muon parameter and loss / grad_norm over 40 steps are the same as main (Kimi K2.5 debugmodel on 8 GPUs, Moonlight 27 layers, Kimi K3 debugmodel).

Perf: 8x A100, K2.5 3 layers EP8, slowest rank 251.8 -> 245.9 ms. On a PCIe box (8x 3090) where the exchange is not hidden, 2689 -> 1195 ms. Exchange buffers are half the size.
Test plan: pytest tests/unit_tests/cpu/distributed/flex_shard; GPU and CPU flex_shard tests pass on 8x A100 on this PR; K2.5 debugmodel 40 steps on 8x A100 gives the same parameter and momentum digests and loss as the base.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 11, 2026
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 11, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@JavaZeroo
JavaZeroo force-pushed the feat/distmuon-bf16-wire branch 2 times, most recently from 5302124 to ea035ec Compare October 11, 2026 07:03
@weifengpy

Copy link
Copy Markdown
Contributor

aka compute_dtype? is this bitwise equal ?

@JavaZeroo

Copy link
Copy Markdown
Contributor Author

aka compute_dtype? is this bitwise equal ?

Yes, verified bitwise equal. Newton-Schulz already runs in BF16; the one FP32 op that used to run before the send, the per-block _adjust_muon_learning_rate scale, now runs after the direction comes back. And yes, it is aka compute dtype.

Newton-Schulz casts its input to BF16 before it normalizes, so the prepared
input and the direction can travel in BF16 and the update keeps the same
bits. The bucket plan carries a transport dtype; exchange and compute
scratch buffers are reserved in it. A BlockShard direction stays unscaled on
the wire and its per-block learning-rate ratio is applied on the storage
side as a per-row scale. The expert redistribution test expects the BF16
compute tensor.
@JavaZeroo
JavaZeroo force-pushed the feat/distmuon-bf16-wire branch from f9c14c2 to 94be3fc Compare October 12, 2026 02:44

@weifengpy weifengpy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a good finding! I am fully supportative. just make it per-fqn and we should be good to go

The dtype redistributed parameters are sent and computed in is now declared
per parameter on ComputeLayout; None keeps the storage dtype, and DistMuon
accepts the storage dtype or torch.bfloat16. Every redistributed parameter
of a bucket must declare the same compute dtype. The Kimi recipes declare
torch.bfloat16 on every layout, which keeps their numerics as before.
@tianyu-l
tianyu-l requested a review from weifengpy October 13, 2026 01:43
@weifengpy

Copy link
Copy Markdown
Contributor

a few things after ai review

  • update README
  • with FP32 storage, Newton-Schulz silently runs in BF16. we should raise unimplemented error in DistMuon init for fp32 comptue dtype
  • TestDistMuon::test_matches_plain_muon_across_flat_checkpoint fails for both parameter sets with ValueError: bucket 'layers.0' mixes compute dtypes. Only the redistributed layout got BF16. Despite its name, local_blocks is redistributed too (a block straddles the shard boundary), and it defaults to FP32.
  • TestDistMuonInitialExpertStorageContract hangs. Three ranks get an FP32 compute tensor where the test expects BF16 and fail. The fourth rank holds no experts, skips that check, and blocks in full_tensor() waiting for the others.
  • TestDistMuonNativeMatrixBatch passes (4 tests), and the CPU flex_shard tests pass (8).

@weifengpy weifengpy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the quick follow up. see above

@JavaZeroo

Copy link
Copy Markdown
Contributor Author

a few things after ai review

  • update README
  • with FP32 storage, Newton-Schulz silently runs in BF16. we should raise unimplemented error in DistMuon init for fp32 comptue dtype
  • TestDistMuon::test_matches_plain_muon_across_flat_checkpoint fails for both parameter sets with ValueError: bucket 'layers.0' mixes compute dtypes. Only the redistributed layout got BF16. Despite its name, local_blocks is redistributed too (a block straddles the shard boundary), and it defaults to FP32.
  • TestDistMuonInitialExpertStorageContract hangs. Three ranks get an FP32 compute tensor where the test expects BF16 and fail. The fourth rank holds no experts, skips that check, and blocks in full_tensor() waiting for the others.
  • TestDistMuonNativeMatrixBatch passes (4 tests), and the CPU flex_shard tests pass (8).

thanks for the reivew
Done:

  • README now describes compute_dtype.
  • DistMuon raises NotImplementedError at init unless the compute dtype is bf16.
  • The GPU tests set compute_dtype=torch.bfloat16 on every layout, which should fix the mixed-dtype error and the hang. The CPU schedule test sets it too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests CLA Signed This label is managed by the Meta Open Source bot.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants