Repository navigation
Conversation
Companion to deepspeedai/DeepSpeed#9131: continues training a causal LM (dense or AutoEP MoE) on WikiText-103 under BF16 and several quantization recipes, and reports validation loss, step time and peak memory. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Signed-off-by: Zhipeng Wang <7927074+PKUWZP@users.noreply.github.com>
…example from_scratch.py trains a Llama-architecture model from random initialization on all of WikiText-103 with a warmup + cosine schedule and reports final validation and test loss, so FP8 recipes can be compared with BF16 at convergence. The README gains the launch commands and two-seed reference results. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Signed-off-by: Zhipeng Wang <7927074+PKUWZP@users.noreply.github.com>
--moe builds a Qwen2-MoE-architecture model and trains it with AutoEP, so quantized grouped experts can be compared with BF16 at convergence. The README gains the launch command and two-seed MoE reference results. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Signed-off-by: Zhipeng Wang <7927074+PKUWZP@users.noreply.github.com>
from_scratch.py gains the int8 and int8_h128 recipes, and the README gains their two-seed MoE reference results. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Signed-off-by: Zhipeng Wang <7927074+PKUWZP@users.noreply.github.com>
PKUWZP
marked this pull request as ready for review
November 22, 2026 16:49
opensource-fork-sync Bot
pushed a commit
to cla7aye15I4nd/opensource-deepspeedai__DeepSpeed
that referenced
this pull request
Nov 24, 2026
…xperts (deepspeedai#9131) Adds an opt-in `quantization.subchannel` config section that runs the three GEMMs of `nn.Linear` layers and AutoEP grouped experts (forward, `dx`, `dw`) in FP8 or INT8, each configured independently. With one scale per operand row (`block_size: 0`) the FP8 path trains Qwen2.5-32B 1.21x faster per step than BF16; with block scales it is a numerics feature and slower than BF16. Proposal: deepspeedai#9132 ([accepted](deepspeedai#9132 (comment)) on 2026-11-22). Related: RFC deepspeedai#8777 and its draft PR deepspeedai#8784 (low-precision training via TorchAO HiFloat8), roadmap deepspeedai#8672 ("FP8 GEMM for MoE"). ## What it does ```json "quantization": { "subchannel": { "enabled": true, "forward": {"format": "e4m3", "block_size": 128, "hadamard_size": 0, "stochastic_rounding": false}, "dx": {"format": "e4m3", "block_size": 128, "hadamard_size": 0, "stochastic_rounding": true}, "dw": {"format": "e4m3", "block_size": 128, "hadamard_size": 0, "stochastic_rounding": true} } } ``` - **Formats**: `e4m3`, `e5m2`, `int8`, or `none` per GEMM. - **Subchannel scaling**: both operands are quantized along the GEMM's reduction dimension in blocks of 32/64/128/256 elements with one FP32 scale each. In `dw` the reduction runs over tokens and blocks restart at every expert. - **Row scaling** (`block_size: 0`, FP8 only): one scale per operand row, per expert in `dw`. Scales are applied once per output element, so the reduction is a plain FP8 tensor-core loop. - **Stochastic rounding**: unbiased, default on for `dx` and `dw`. - **Fused butterfly Hadamard** (`hadamard_size`): rotates each chunk of a block in registers before quantizing; the two operands' rotations cancel in the product. - Parameters, gradients, optimizer state and module outputs keep their usual precision. Only GEMM operands are quantized, on the fly, so there is **no memory saving**. - Default excludes keep `lm_head` and MoE routers in full precision. ## Implementation - `deepspeed/ops/triton_ops/subchannel_quant.py`: Triton quantization and GEMM kernels (block-scaled and row-scaled, rows-grouped and reduction-grouped). - `deepspeed/runtime/quantization/`: config models, a PyTorch reference implementation (the test oracle, and the fallback on devices without the kernels), one autograd function shared by dense and grouped layers, and module conversion. - Core-file wiring is small: `engine.py` (+14, one call at init), `runtime/config.py` (+6), `ep_experts.py` (+31, a `grouped_mm_fn` hook), `abstract_accelerator.py` (+6, a concrete method `supported_subchannel_quantization_formats()` returning `()` by default), `cuda_accelerator.py` (+14). - About 1,130 added lines under `deepspeed/` and `accelerator/`, most of it the standalone kernel module. ## Results Hardware: 4x NVIDIA H200 (143 GiB), driver 595.112.07, torch 2.8.0+cu129, Triton 3.4.0, transformers 5.23.0. The full-training runs below use two seeds; every other table is a single run with a single seed. ### Full training to convergence, two seeds A 1.82B-parameter Llama-architecture model (8 layers, hidden 4096, MLP 11008, GPT-2 tokenizer) trained from random initialization on all of WikiText-103 (118M tokens) for 4 epochs: 28,788 steps, sequence length 2048, batch 4 per GPU on 2 GPUs, ZeRO-2, AdamW (peak lr 2e-4, 1000 warmup steps, cosine decay to zero, weight decay 0.1), gradient clipping 1.0, no gradient checkpointing. Both FP8 recipes use nearest rounding forward and stochastic rounding for dx and dw. Within a seed, every recipe starts from the same weights and sees the same data order. Two runs shared the machine at a time, 2 GPUs each. | Recipe | Seed | Final val loss | Test loss | Test ppl | Step p50 | Peak memory | |---|---|---|---|---|---|---| | bf16 | 0 | 2.9597 | 2.9761 | 19.61 | 323 ms | 29.9 GiB | | e4m3 row-scaled | 0 | 2.9557 | 2.9747 | 19.58 | 307 ms | 29.9 GiB | | e4m3 block 128 | 0 | 2.9518 | 2.9738 | 19.57 | 386 ms | 29.9 GiB | | bf16 | 1 | 2.9650 | 2.9860 | 19.81 | 326 ms | 29.9 GiB | | e4m3 row-scaled | 1 | 2.9631 | 2.9828 | 19.74 | 305 ms | 29.9 GiB | | e4m3 block 128 | 1 | 2.9545 | 2.9792 | 19.67 | 386 ms | 29.9 GiB | - The two BF16 seeds differ by 0.010 in test loss; each FP8 run is within 0.007 of BF16 at the same seed. FP8 is marginally lower in all four comparisons, which with two seeds shows it is not worse, not that it is better. - The validation curves track each other for the whole run, with no divergence or instability. Validation loss was still falling at the end, so best and final validation loss coincide. - Row-scaled FP8 is about 5% faster than BF16 on this model, block-scaled about 19% slower. Peak memory is identical. - Not covered by this run: INT8, the Hadamard rotation, a second dataset. MoE with AutoEP, including INT8, is the next section. ### Full MoE training to convergence with AutoEP, two seeds A Qwen2-MoE-architecture model trained from random initialization with AutoEP (`autoep_size=2`, so each GPU holds 8 of the 16 experts): 8 layers, hidden 2048, 16 experts per layer with 4 active per token, expert width 1408, shared expert width 5632, about 1.6B parameters. Data, schedule, optimizer, ZeRO stage and GPU count are the same as in the dense run; the INT8 recipes round to nearest forward and stochastically for dx and dw, like the FP8 ones. The grouped experts, the shared expert and the attention projections are quantized; the router is not. | Recipe | Seed | Best val loss (step 21,500) | Final val loss | Test loss | Test ppl | Step p50 | Peak memory | |---|---|---|---|---|---|---|---| | bf16 | 0 | 2.8972 | 2.9354 | 2.9528 | 19.16 | 217 ms | 26.4 GiB | | e4m3 row-scaled | 0 | 2.8949 | 2.9331 | 2.9519 | 19.14 | 304 ms | 26.4 GiB | | e4m3 block 128 | 0 | 2.8973 | 2.9321 | 2.9493 | 19.09 | 268 ms | 26.4 GiB | | int8 block 128 | 0 | 2.8951 | 2.9352 | 2.9554 | 19.21 | 266 ms | 26.4 GiB | | int8 block 128 + Hadamard 128 | 0 | 2.8926 | 2.9308 | 2.9481 | 19.07 | 329 ms | 26.4 GiB | | bf16 | 1 | 2.9048 | 2.9361 | 2.9573 | 19.25 | 218 ms | 26.8 GiB | | e4m3 row-scaled | 1 | 2.9009 | 2.9297 | 2.9530 | 19.16 | 305 ms | 26.7 GiB | | e4m3 block 128 | 1 | 2.9026 | 2.9307 | 2.9542 | 19.19 | 268 ms | 26.8 GiB | | int8 block 128 | 1 | 2.9011 | 2.9296 | 2.9501 | 19.11 | 267 ms | 26.9 GiB | | int8 block 128 + Hadamard 128 | 1 | 2.9016 | 2.9311 | 2.9504 | 19.11 | 326 ms | 27.0 GiB | - The two BF16 seeds differ by 0.0045 in test loss; each FP8 run is within 0.0043 of BF16 at the same seed. As in the dense run, FP8 is marginally lower in all four comparisons, which shows it is not worse, not that it is better. - This model overfits in the last epoch: every run, BF16 included, has its best validation loss at step 21,500 and ends 0.03 to 0.04 higher. The recipes agree at both points, and the curves track each other for the whole run. - Both FP8 modes are slower than BF16 on this model: block-scaled by about 23%, row-scaled by about 40%. Peak memory differs by at most 0.6 GiB and follows the seed, not the recipe. - INT8 with 128-element blocks, with and without a Hadamard rotation of 128, also ends at or below BF16: test loss is between 0.003 above and 0.007 below BF16 at the same seed. The rotation makes no consistent difference to loss (0.007 lower on seed 0, equal on seed 1) and costs about 23% more step time than INT8 without it. INT8 is 23% slower than BF16 without the rotation and 51% slower with it. INT8 was trained to convergence on the MoE model only. - Not covered: `autoep_size` other than 2, a second MoE shape or dataset, INT8 on the dense model. ### Dense models: Qwen2.5-14B and Qwen2.5-32B 100 steps of continued pretraining on WikiText-103, 8192 tokens per GPU per step (batch 4 x sequence 2048), gradient checkpointing, BF16 master weights and optimizer state, AdamW lr 1e-5. 14B under ZeRO-2, 32B under ZeRO-3. | Model | Recipe | Val loss @0 | Val loss @100 | Step p50 | vs BF16 | Peak memory | |---|---|---|---|---|---|---| | Qwen2.5-14B | bf16 | 1.7576 | 1.6783 | 2145 ms | 1.00x | 66.2 GiB | | | e4m3, row-scaled | 1.7622 | 1.6839 | 1965 ms | 1.09x | 66.2 GiB | | | e4m3, block 128 | 1.7611 | 1.6816 | 2896 ms | 0.74x | 66.2 GiB | | Qwen2.5-32B | bf16 | 1.7340 | 1.6561 | 4594 ms | 1.00x | 82.2 GiB | | | e4m3, row-scaled | 1.7436 | 1.6604 | 3801 ms | 1.21x | 82.2 GiB | | | e4m3, block 128 | 1.7356 | 1.6584 | 6175 ms | 0.74x | 82.2 GiB | ### MoE models: Qwen1.5-MoE-A2.7B and Qwen3-30B-A3B 300 steps, AutoEP with `autoep_size=4`, ZeRO-2, 2048 tokens per GPU per step. | Model | Recipe | Val loss @300 | Step p50 | Peak memory | |---|---|---|---|---| | Qwen1.5-MoE-A2.7B | bf16 | 1.8900 | 341 ms | 64.5 GiB | | | e4m3, block 128 | 1.8923 | 500 ms | 64.5 GiB | | | e4m3, row-scaled | 1.8941 | 518 ms | 64.5 GiB | | | int8, block 128 | 1.8903 | 522 ms | 64.6 GiB | | | int8, block 128, Hadamard 128 | 1.8902 | 502 ms | 64.6 GiB | | Qwen3-30B-A3B | bf16 | 1.9084 | 622 ms | 91.8 GiB | | | e4m3, block 128 | 1.9118 | 789 ms | 91.8 GiB | | | e4m3, row-scaled | 1.9116 | 947 ms | 91.8 GiB | | | int8, block 128 | 1.9090 | 841 ms | 91.8 GiB | | | int8, block 128, Hadamard 128 | 1.9084 | 927 ms | 91.8 GiB | The INT8 rows were measured before the stochastic-rounding kernel was made cheaper and were not rerun. ### Single layer step (forward + dx + dw), one H200 | Shape (tokens x in -> out) | bf16 | e4m3 block 128 | e4m3 row-scaled | Row vs BF16 | |---|---|---|---|---| | 32768 x 8192 -> 8192 | 21.2 ms | 29.6 ms | 15.3 ms | 1.39x | | 16384 x 8192 -> 8192 | 9.7 ms | 14.8 ms | 7.3 ms | 1.34x | | 8192 x 4096 -> 14336 | 3.9 ms | 6.6 ms | 3.4 ms | 1.16x | | 8192 x 4096 -> 4096 | 1.09 ms | 2.01 ms | 1.39 ms | 0.78x | | 8192 x 2048 -> 2048 | 0.30 ms | 1.04 ms | 1.28 ms | 0.23x | | 16 experts, 32768 x 2048, inter 768 | 2.4 ms | 4.6 ms | 4.8 ms | 0.51x | ### What the results show, and do not - Row-scaled FP8 is faster than BF16 only on wide layers with many tokens. It is slower on both MoE models and on layers 4096 wide or narrower, where the per-call quantization passes and kernel-launch overhead outweigh the faster GEMM. - Block-scaled GEMMs are slower than BF16 everywhere: scales have to be applied between tensor-core dot products. - In full training from scratch, both FP8 modes reach the same validation and test loss as BF16, within seed-to-seed variation, on a dense model and on an MoE model with AutoEP. - In the short continued-pretraining runs, validation loss stays within 0.006 of BF16. Row scaling costs about twice the loss gap of block scaling on the dense models. - The Hadamard rotation made no consistent difference to final loss in full INT8 training on the MoE model, and stochastic rounding and the rotation made no measurable difference in the short runs. On synthetic data with outlier channels the rotation cuts INT8 GEMM error about 3.5x; it does not help FP8. - The pretrained-model runs are short continued pretraining at one learning rate: a consistency check only. The evidence for on-par final quality is the two full-training comparisons above (one dense and one MoE shape, one dataset, two seeds each). INT8 and the Hadamard rotation were trained to convergence on the MoE model only, where they also match BF16. - The Qwen2.5 dense runs use gradient checkpointing, which runs each forward twice and so weights the forward GEMM more heavily than a run without it. ## Tests ``` cd tests pytest --forked unit/v1/runtime/test_subchannel_quantization.py # 61 passed (2 H200s) CUDA_VISIBLE_DEVICES="" pytest --forked unit/v1/runtime/test_subchannel_quantization.py # 32 passed, 29 skipped pytest unit/v1/moe/test_autoep_unit.py unit/v1/moe/test_autoep_expert_activation.py \ unit/runtime/test_ds_config_model.py unit/runtime/test_ds_config_dict.py \ -k "not TestFusedGateUp and not TestDeferredExpertWeightGrad" # 355 passed, 7 failed ``` - The new tests compare forward, `dx` and `dw` against FP64 matmuls on both the kernels and the PyTorch reference, check stochastic rounding is unbiased, check module conversion, and run distributed training (`world_size=2`): dense under ZeRO 1/2/3 and AutoEP experts under ZeRO-2. One test checks that a `torch.compile`d model gives the same outputs and weight gradients as eager execution, for block and row scaling. - Two tests were added for the review fixes in 162ebd4. One runs the expert MLP with a single local expert on a buffer that ends in alignment padding rows and checks the output, `dx` and the `w1`, `w2`, `w3` gradients against the unpadded run, for `dw` format `none`, row-scaled and block-scaled; it needs the FP8 kernels. The other checks that a NaN operand comes out as NaN in the output, `dx` and `dw`, for E4M3 block-scaled, E4M3 row-scaled and INT8, on the kernels and on the reference. - The first two commands were rerun after those fixes; the third was not, and its counts are from before them. The results tables above were also measured before them. A single layer step on 16384 x 8192 -> 8192 was timed before and after on one H200 and is unchanged (6.4 ms row-scaled, 14.1 ms block-scaled). - The 7 failures are all in `test_ds_config_dict.py` and are `CUDAMismatchException` from the FusedAdam JIT build on this machine (system CUDA 13.2 vs torch built for 12.9). They are environmental; I did not confirm them on an unmodified checkout. - `TestFusedGateUp` and `TestDeferredExpertWeightGrad` were excluded because they hang on this machine on unmodified `master` as well: `torch._grouped_mm` in its 2D x 2D form does not return on torch 2.8 when an expert has no tokens. That is a separate pre-existing problem and is not addressed here. - `ci/tests_fetcher.py --base origin/master` selects the full `tests/unit/v1` suite for this diff. ## Compatibility | Axis | Validated | Not validated | |---|---|---| | ZeRO stage | 0, 1, 2, 3 for `nn.Linear` (2 and 3 also on real models); 0, 1, 2, 3 for AutoEP | | | Precision | bf16; fp32, fp16 and `torch_autocast` under ZeRO-2 and ZeRO-3 | | | Offload | none; CPU offload under ZeRO-2 and ZeRO-3 | NVMe offload: ZeRO-3 with NVMe offload did not finish 5 steps in 7 minutes on this machine even with quantization off, so it could not be tested here | | Parallelism | data parallel, AutoEP | AutoTP and PipelineModule are rejected at init; sequence parallelism untested | | Other | gradient checkpointing, gradient clipping; gradient accumulation (dense under ZeRO 1/2/3, AutoEP under ZeRO 0/1/3); `torch.compile` (dense under ZeRO-2 and ZeRO-3; AutoEP under ZeRO-0 and ZeRO-2 with whole-model compile and under ZeRO 0/1/2 with `compile.autoep_non_moe`); checkpoint save/load round-trip (dense under ZeRO 0-3 for block and row scaling, including loading a quantized checkpoint into an unquantized model and the reverse; AutoEP under ZeRO-2 and ZeRO-3) | `torch.compile` with AutoEP under ZeRO-3: whole-model compile fails inside dynamo on the test model with quantization off as well, and `compile.autoep_non_moe` rejects ZeRO-3 | | Accelerators | CUDA sm90 (H200) | every other device. Non-CUDA accelerators and CUDA below sm89 use the PyTorch reference, which simulates the quantization in FP32; this was run on CPU only | The cells beyond the unit tests listed above (ZeRO-0, AutoEP under ZeRO 0/1/3, fp32/fp16/`torch_autocast`, CPU offload, gradient accumulation, checkpoint round-trip, `torch.compile` under ZeRO and with AutoEP) were run once as short two-GPU training loops on the H200s with a throwaway test file that is not part of this PR; each checked that the loss fell and, where there is a reference, that results matched the unquantized or pre-save model. Only the `torch.compile` case is kept as a permanent test. `torch.compile` initially gave silently wrong results: when the compiler traced the host code around the Triton kernels, the quantized GEMMs were about 80% off with row scaling and about 2% off with block scaling. `subchannel_grouped_matmul` is now excluded from tracing and always runs eagerly. The root cause inside the traced path was not identified. For `torch.compile` with AutoEP, a compiled engine was compared with an eager one from the same initialization, with nearest rounding everywhere, on a mock MoE model with quantized dense layers and quantized experts. In fp32 the first-step output agrees to 4e-7 relative and every gradient to 1.4e-4 or better (unquantized: 4e-7 and 9e-7). In bf16 the output differs by 5% and the worst gradient by 19%, against 1% and 2% unquantized: the compiler's small bf16 differences flip rounding decisions, which the fp32 result shows is not an error in the quantized path. AutoEP's `gate_up_impl="fused"`, `overlap_weight_grad` and `deferred_dw_flush_period` above 1 are rejected for quantized experts. No existing config key, checkpoint format or public API changes; the feature is off by default. - **Companion examples PR**: deepspeedai/DeepSpeedExamples#1024 (draft). Docs in this PR: the config reference in `docs/_pages/config-json.md` and a tutorial, `docs/_tutorials/subchannel-quantized-training.md`. The docs site was not built locally. - **Duplicate check**: no open PR implements FP8/INT8 GEMM training with Triton kernels. deepspeedai#8784 covers the same module kinds through TorchAO and HiFloat8 on Ascend; the two would need to agree on the shape of the `quantization` config section. ## AI assistance This change was written with AI assistance (Claude Code), including the kernels, tests, benchmarks and this description. I reviewed the PR line-by-line, wrote some part of the code by myself and approved the changes. --------- Signed-off-by: Zhipeng Wang <7927074+PKUWZP@users.noreply.github.com> Signed-off-by: Zhipeng Wang <zhipeng.rainbowserie@gmail.com> Co-authored-by: Zhipeng Wang <7927074+PKUWZP@users.noreply.github.com> Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
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.
Adds
training/subchannel_quantization/, the companion example for deepspeedai/DeepSpeed#9131 (proposal: deepspeedai/DeepSpeed#9132). It should not merge before that PR does.What it adds
train.py: continues training a pretrained causal LM on WikiText-103 with DeepSpeed, either as a dense model (data parallel) or as an MoE model with AutoEP.--recipeselects thequantization.subchannelsettings:bf16(baseline),e4m3_row,e4m3,int8,int8_h128. Every recipe sees the same data in the same order, and the script reports validation loss, median step time and peak memory.from_scratch.py: trains a Llama-architecture model (default 1.82B parameters) from random initialization on all of WikiText-103 with a warmup + cosine schedule, under any of the five recipes, and reports final validation and test loss. With--moeit trains a Qwen2-MoE-architecture model with AutoEP instead. This is the script behind the full-training results in Add subchannel FP8/INT8 quantized training for nn.Linear and AutoEP experts DeepSpeed#9131.README.md: prerequisites, the recipes, launch commands for both scripts, and reference results.requirements.txt: installs DeepSpeed from the tip of Add subchannel FP8/INT8 quantized training for nn.Linear and AutoEP experts DeepSpeed#9131.Validation
Hardware: 4x NVIDIA H200, driver 595.112.07, torch 2.8.0+cu129, Triton 3.4.0, transformers 5.23.0.
e4m3_rowon Qwen1.5-MoE-A2.7B (AutoEP) and on Qwen2.5-14B (--dense --grad_checkpointing --bf16_states); both completed and wrote their result files.--denserather than a separate flag, and memory statistics came fromtorch.cudainstead of the accelerator interface.from_scratch.pybefore it was moved here. The committed version differs in a license header, a--data_diroption replacing a fixed cache path, creating the output directory, and an argument check. It was rerun after those changes for 100 steps withbf16ande4m3_rowon 2 GPUs with a small model (--layers 2 --hidden 1024 --intermediate 2048); both completed. The full runs were not repeated, and--prepwas not rerun with the new--data_diroption.autoep_size=2) were produced withfrom_scratch.pyas committed apart from line wrapping and the docstring, launched with the--moecommand in the README plus--data_dir. Before those runs, a 2-layer model was inspected to confirm that AutoEP had replaced the MoE blocks and that the grouped experts carried the quantized GEMM hook.int8andint8_h128, two seeds, 28,788 steps each) were produced withfrom_scratch.pyexactly as committed. INT8 was not trained to convergence on the dense model: 150-step runs of both recipes completed there, and the full runs were skipped.requirements.txtwas not installed into a clean environment.AI assistance
This example was written with AI assistance (Claude Code) and needs human review of every line before it merges.