Repository navigation
Conversation
GPTBigCode's fused c_attn stores the query rows followed by the whole shared K/V block. transformers 5.x exposes num_key_value_heads=1 for multi-query models, so AutoTP split the fused attention by one head and left every other rank with an empty query shard. Use the query-head count for replicated-KV layouts, keep the K/V head attributes whole, recompute num_key_value_groups from the local shard, and reject TP widths larger than the query-head count from the shared AutoTP path. Reject an explicit partition_config for MQA until the heuristic attributes can follow an arbitrary shard. Make the K/V gradient reduction backend-safe: stage a contiguous copy when the view is non-contiguous, then write the reduction back. Register the hook through register_grad_hook so it runs in ZeRO's post-accumulate phase on torch_npu, and accept the two-argument AccumulateGrad signature used before torch 2.1. Signed-off-by: Venus-Tzz <1601677496@qq.com>
Add TP=2/TP=4 parity with ZeRO 0/1/2/3, shard-shape and checkpoint metadata checks, inference logits, TP-width rejection, unit coverage for the replicated-KV attributes and explicit-config guard, and the legacy AccumulateGrad hook signature. Signed-off-by: Venus-Tzz <1601677496@qq.com>
Route the shard unit, K/V row span, and group count through fusedqkv_utils helpers so the engine warning, init path, width validation, and update_mp_params share one derivation. Split the config, module, and sharded K/V attribute lists, and assert the subset relations so later edits cannot silently diverge. Signed-off-by: Venus-Tzz <1601677496@qq.com>
Keep the pre-accumulate tensor hook for the normal path and add a post-accumulate fallback for backends that skip it. The fallback reduces only the K/V rows added since its previous call, so gradient accumulation does not repeatedly scale earlier micro-batches. Add a regression test covering both hook paths. Signed-off-by: Venus-Tzz <1601677496@qq.com>
ZeRO 0/1 keep the full gradient local until the accumulation boundary, so GPTBigCode's replicated K/V rows can be all-reduced once per accumulation window instead of once per micro-batch. ZeRO 2/3 consume or partition the gradient during each micro-batch and keep the existing path. Deferral is allowed only when the whole window stays in param.grad: ZeRO-1 with a separate gradient accumulation buffer, BF16_Optimizer, CPU/NVMe offload, and zenflow keep the per-micro-batch path. The accumulation state is tracked per reducer so two engines in one process do not overwrite each other, and a reentrant boundary reduces only the rows added since the previous boundary. Add reducer unit tests and a GAS=2 parity test covering ZeRO 0/1/2/3, including fp16/bf16 with fp32 gradient accumulation on ZeRO-1. Signed-off-by: Venus-Tzz <1601677496@qq.com>
A per-backward decision let one window mix the deferred and per-micro-batch paths when no_sync covered only the non-boundary backwards. The boundary's whole-window reduce then counted the micro-batches that had already been reduced before accumulation again. Latch the decision for the whole window and keep the boundary on the same path. Add the canonical DDP no_sync pattern to the GAS=2 parity test for ZeRO 0/1. Signed-off-by: Venus-Tzz <1601677496@qq.com>
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.
Stacked on #8962 (GPTBigCode AutoTP correctness fixes), which should be merged first. Only the last two commits are new. Child-only diff: VenusTZZ/DeepSpeed@1a19b1a...218cbf2. This PR stays Draft until #8962 merges; it will then be rebased onto
masterand marked ready.Summary
AutoTP multi-query GPTBigCode keeps a full copy of the key/value (K/V) block on every tensor-parallel (TP) rank, so its gradient needs a TP all-reduce on every micro-batch. With gradient accumulation this repeats the same small collective for every micro-batch,
gradient_accumulation_stepstimes per optimizer step. For ZeRO 0/1 the full window stays inparam.grad, so the K/V rows can be all-reduced once at the accumulation boundary instead. This is a performance optimization follow-up to #8962, split out at the reviewer's request; #8962 keeps the correctness fixes only.Motivation
The per-micro-batch reduce was introduced as a correctness fix in #8962. It is necessary for ZeRO 2/3 and for configurations that move earlier micro-batches out of
param.grad, but for ZeRO 0/1 with gradient accumulation it adds2 × layersall-reduces per micro-batch for a few hundred rows. Measured on a GPTBigCode multi-query attention (MQA) model (24 layers, hidden=2048, 268.8M parameters), TP=4, bf16, ZeRO-1, seq=1024, micro-batch=1. In the table, "accumulation steps" is the number of micro-batches per optimizer step (gradient_accumulation_steps):K/V all-reduce calls per optimizer step drop from 1,536 to 48 at 32 accumulation steps, and from 384 to 48 at 8. The gain is workload-dependent: 4 layers/hidden=4096 was within noise on NPU, and a single accumulation step has no gain by construction.
Design
_ReplicatedKvGradReducerkeeps its existing tensor hook and post-accumulate fallback. When the engine allows deferral, both hooks leave the local K/V partial in place; at the boundary the post-accumulate hook all-reduces the rows accumulated during the window.DeepSpeedEngine._backward_prologue()publishes the accumulation state to the reducers. Deferral is allowed only when the whole window stays inparam.grad: ZeRO 0 (non-BF16 optimizer, no CPU offload) or ZeRO-1 withDeepSpeedZeroOptimizerand no separate gradient-accumulation buffer, more than one accumulation step (gradient_accumulation_steps() > 1), managed gradient accumulation, not insideno_sync/coalesced reduction, and not pipeline parallel. Every other configuration keeps the per-micro-batch path.no_synccovers only the non-boundary micro-batches, and the boundary's whole-window reduce would count already-reduced micro-batches again.The fallback and the deferral share reducer state, so this branch depends on the reducer added in #8962. It is stacked and should be retargeted after #8962 lands.
Files changed
deepspeed/module_inject/layers.pydeepspeed/runtime/engine.pytests/unit/v1/autotp/test_autotp_training.pyno_sync.Testing
Environment
NPU
Result: 15 passed in 358.76 s. Coverage:
TestGPTBigCodeMultiQueryAccumulation: ZeRO 0/1/2/3 with and without the canonical distributed data parallel (DDP)no_syncpattern, plus fp16/bf16 with fp32 gradient accumulation on ZeRO-1. The test asserts both loss parity against the unsharded reference and the per-step K/V all-reduce count (2/2 per step for deferred windows, 4/4 for fallback windows), so it fails if the feature silently falls back or if the two paths mix.TestGPTBigCodeMultiQueryTraining: single-micro-batch regression (no gradient accumulation), 5 passed.The tree is unchanged from the implementation before the split (
218cbf242^{tree}equals the pre-splite02f9f3c0^{tree}), so the earlier NPU and A100 performance validation still applies.CUDA
Result: 15 passed, 1 warning in 186.35 s (0:03:06) at split tip
218cbf242. Same 15-test set as NPU, including the loss-parity and per-step all-reduce-count assertions. This closes the previous CUDA test gap; the split only rewrote history, so the A100 performance numbers measured pre-split apply to this branch.Not tested
torch.compile, pipeline parallelism, and windows that usecoalesce_grad_reduction: these take the fallback path and are not benchmarked.Dependencies
git rebase --onto upstream/master 1a19b1aab, force-push, mark ready.Notes for review
Checklist
pre-commit(implementation before the split; the split only rewrote history)