Repository navigation
Conversation
…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>
… 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>
…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>
bf16.stochastic_round_params)
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What it changes
Adds
bf16.stochastic_round_params(defaultfalse). With bf16 parameters and fp32 master weights, ZeRO stages 1 and2 (
DeepSpeedZeroOptimizer, with or without CPU optimizer offload) andBF16_Optimizercopy each rank's updated fp32master 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.
Contract
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.
update_lp_params). A universalmodule-only warm start then initializes the masters from the rounded bf16 weights, as it does today from the
nearest-rounded ones.
extra communication. Pipeline stages broadcast their tied weights after rounding (each stage holds its copy at a
different offset of its rounding stream).
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.
bf16 parameter instead of 2; the host bf16 staging buffer is not allocated). Temporaries are bounded by rounding
2^24 elements at a time.
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.
Tests
tests/unit/v1/half_precision/test_bf16.py, all throughdeepspeed.initialize:BF16_Optimizeron controlled masters: unbiased for both signs, across a binade edgeand 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;
BF16_Optimizer, ZeRO-2 and ZeRO-2 with CPU offload, key off and on: with the key offno 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;
train_batchwith a tied layer, then a native save, universal conversion and module-only warmstart: the tied copies stay equal and the masters equal the bf16 weights;
initialize; Ulysses (two ranks) trains; ZeRO-3 refused; fp16 and bf16 mastersrefused by
DeepSpeedConfig.CPU (
DS_ACCELERATOR=cpu, Gloo), Python 3.12, PyTorch 2.11:test_bf16.pyandunit/v1/zero/test_zero2_gradient_safety.pyon 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-offcontrols), 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