Skip to content

bf16: optional stochastic rounding of parameters from fp32 master weights (bf16.stochastic_round_params) - #9136

Draft
seanbell wants to merge 7 commits into
deepspeedai:masterfrom
seanbell:bf16-stochastic-round-params
Draft

seanbell wants to merge 7 commits into
deepspeedai:masterfrom
seanbell:bf16-stochastic-round-params

Conversation

@seanbell

@seanbell seanbell commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What it changes

Adds bf16.stochastic_round_params (default false). With bf16 parameters and fp32 master weights, ZeRO stages 1 and
2 (DeepSpeedZeroOptimizer, with or without CPU optimizer offload) and BF16_Optimizer copy each rank's updated fp32
master partition into its bf16 parameters with stochastic rounding instead of round-to-nearest, at their existing copy
and before their existing data-parallel all-gather. Under round-to-nearest an update smaller than half a bf16 spacing
leaves the bf16 parameter unchanged until the accumulated master crosses a rounding midpoint; with stochastic rounding
the forward pass, and anything that reads the module parameters, sees the master in expectation.

"bf16": {"enabled": true, "stochastic_round_params": true}

Contract

  • For a finite master within the bf16 range, the result is one of its two adjacent bf16 values, the upper with
    probability equal to the master's fractional position between them, so its expectation is the master. bf16 values
    stay exact. NaNs take the device cast; infinities stay infinite; finite values beyond the bf16 range can round to
    infinity. Parameters kept in fp32 are copied exactly.
  • It applies after every optimizer step and at the universal-checkpoint refresh (update_lp_params). A universal
    module-only warm start then initializes the masters from the rounded bf16 weights, as it does today from the
    nearest-rounded ones.
  • Each rank rounds only the partition it owns, then the existing all-gather distributes it: one draw per element, no
    extra communication. Pipeline stages broadcast their tied weights after rounding (each stage holds its copy at a
    different offset of its rounding stream).
  • Each rank draws from a dedicated per-device generator seeded from torch.initial_seed() and its data-parallel rank.
    The generator state is not checkpointed: a restarted process with the same seed replays its stream from the start.
  • With CPU optimizer offload, each fp32 master chunk is copied to the parameter's device and rounded there (4 bytes per
    bf16 parameter instead of 2; the host bf16 staging buffer is not allocated). Temporaries are bounded by rounding
    2^24 elements at a time.
  • Refused at initialization: tensor parallelism (replicated weights sit at different partition offsets on each
    tensor-parallel rank, and AutoTP also replicates KV heads inside sharded QKV tensors, so independent draws would make
    replicas differ), ZeRO-3, ZenFlow, and the stage-0 FP16/bf16 wrappers. Refused at config load: fp16 or bf16 master
    weights. Ulysses sequence parallelism, whose MPU aliases the sequence group as the model-parallel group, is supported.
  • The default keeps round-to-nearest and the existing copy path unchanged.

Tests

tests/unit/v1/half_precision/test_bf16.py, all through deepspeed.initialize:

  • the master-to-bf16 refresh of BF16_Optimizer on controlled masters: unbiased for both signs, across a binade edge
    and for subnormals (5 binomial standard errors at 2^16 draws each), bf16 values exact, infinities and carrying NaN
    payloads equal to the device cast, the same seed repeats the draw and another seed does not;
  • 8 Adam steps on two ranks for BF16_Optimizer, ZeRO-2 and ZeRO-2 with CPU offload, key off and on: with the key off
    no bf16 parameter moves (every update is below half a spacing); with it on, every parameter is one of its master's two
    neighbours, the summed error is within 5 standard errors of 0, and both ranks hold identical parameters;
  • a two-stage pipeline train_batch with a tied layer, then a native save, universal conversion and module-only warm
    start: the tied copies stay equal and the masters equal the bf16 weights;
  • tensor parallelism refused at initialize; Ulysses (two ranks) trains; ZeRO-3 refused; fp16 and bf16 masters
    refused by DeepSpeedConfig.

CPU (DS_ACCELERATOR=cpu, Gloo), Python 3.12, PyTorch 2.11: test_bf16.py and unit/v1/zero/test_zero2_gradient_safety.py
on this branch 96 passed, 8 skipped (40/6 and 56/2; the 13 feature tests all pass). With the same test file, master
(6c8e3f1f7) fails 10 of the 13 feature tests (the key is rejected as unknown; the 3 passing are the key-off
controls), and the previous head of this PR fails 4 (fp16 masters accepted, tensor parallelism accepted, tied pipeline
copies unequal, and the bf16-master refusal's wording).

Not covered

ZeRO-3, ZenFlow, tensor parallelism and the FP16 wrappers (refused, above). Accelerator generators, the H2D chunk
copy under offload, throughput and peak memory, and training quality are not measured in these CPU tests; planned: a
full training run with nearest and stochastic rounding, then step time and peak memory with and without optimizer
offload, key off and on. Draft #9108 reconstructs masters from a nearest-rounded parameter and a signed remainder; if
both land, the combination must be refused. #9131 rounds quantized GEMM operands, not stored parameters.

Once merged, a job on a DeepSpeed release containing it enables the behaviour with the one key above; nothing else is
needed.

AI-assisted. The submitter still adds the DCO sign-off; maintainer review of the proposal and of the code, and an
accelerator run, are pending, so this stays a draft.

🤖 Generated with Claude Code

…ters

ZeRO-1/2 and BF16_Optimizer copy each rank's fp32 master partition into
its bf16 parameters with round-to-nearest after every optimizer step. A
master update smaller than half a bf16 spacing does not change the bf16
parameter, so at small learning rates the parameters used by the forward
pass and saved in weights-only exports trail the masters by many steps.

Add bf16.stochastic_round_params (default false). When set, that copy
rounds each element to the bf16 neighbour away from zero with probability
equal to its fractional position between its two bf16 neighbours, so the
bf16 parameters equal the masters in expectation. The rounding happens
before the existing all-gather, so it adds no communication. Draws are
seeded from the torch seed and the data-parallel rank, so tensor-parallel
replicas of a parameter round identically. The engine refuses the key for
optimizers that do not apply it (ZeRO-3, ZenFlow, FP16_Optimizer), and
the config refuses it with bf16_master_weights_and_grads.

Test: two CPU ranks, eight Adam steps on a constant gradient that move
every master less than half a spacing. With the key off, the bf16
parameters stay at round-to-nearest of the masters (unchanged); with it
on, they are unbiased within 5 standard errors, for BF16_Optimizer and
ZeRO-2 with and without CPU offload.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added the skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. label Nov 20, 2026
seanbell and others added 4 commits November 22, 2026 04:05
… test the kernel

With CPU optimizer offload, the ZeRO-1/2 step rounded the fp32 master into
the host bf16 staging buffer, so the random draw ran on the host, where
torch draws serially (about 7 ns per element on 64 threads, against
0.04 ns for the round-to-nearest copy). The rounding now runs on the bf16
parameter's device: each master chunk is copied there in fp32 and rounded
in place, and the round-to-nearest default keeps its staging buffer.

Add a kernel test: unbiased for either sign and for subnormals (tolerance
5 binomial standard errors at 2^16 draws), exact on bf16 values, nearest
on non-finite values including NaNs whose payload would carry into the
sign bit, and repeatable under a seeded generator.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…hten tests and docs

- With CPU offload and the key on, the host bf16 staging buffer (2 bytes
  per element, pinned with pin_memory) was allocated but never used; it is
  no longer allocated, and its presence selects the default two-stage
  copy, so optimizers built without __init__ in unit tests keep working.
- Keep the new DeepSpeedZeroOptimizer argument last, so positional
  callers are unaffected.
- Write the finite-value selection into the truncated buffer instead of a
  new 64 MB result per chunk.
- Kernel test: add a value below a binade boundary. Engine test: the off
  case omits the key; the master tolerance is one fp32 epsilon per step.
- Test the bf16-master refusal on a configuration DeepSpeed otherwise
  accepts (ZeRO-2 with CPU offload), at config level; the ZeRO-3 refusal
  test fails with an assertion instead of hanging when the refusal is
  missing.
- Docs: unbiased within the finite bf16 range; replicas round alike only
  with matching seeds, partition layouts and copy order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot removed the skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. label Nov 22, 2026
seanbell and others added 2 commits November 23, 2026 03:24
…nsor parallelism and low-precision masters

Each pipeline stage rounds its own copy of a tied weight at a different offset of its rounding stream, so tied copies
diverged after a step and after a universal-checkpoint warm start; broadcast the tied weights after rounding at both
places, as initialization already does.

Tensor-parallel ranks hold replicated weights at different partition offsets (and AutoTP replicates KV heads inside
sharded QKV tensors), so independent draws make replicas differ; refuse tensor parallelism at initialization. Ulysses
sequence parallelism, which aliases its sequence group as the model-parallel group, stays supported. The optimizer
check moves ahead of optimizer construction, and fp16 masters are refused with bf16 masters at config load.

The rounding helper rounds one chunk per call and keeps NaNs on the device cast (infinities have no low bits to carry).
Tests enter through deepspeed.initialize: the master-to-bf16 refresh on controlled masters, a two-stage pipeline
train_batch plus universal warm start, tensor-parallel refusal and Ulysses training.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@seanbell seanbell changed the title bf16: opt-in stochastic rounding of the bf16 parameters from fp32 masters bf16: optional stochastic rounding of parameters from fp32 master weights (bf16.stochastic_round_params) Nov 23, 2026

This branch has not been deployed

No deployments
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