DeepSpeed
73ca2a23 - Support Triton training ops on ROCm, and JSD Triton kernels on CUDA (#9177)

Commit
3 days ago
Support Triton training ops on ROCm, and JSD Triton kernels on CUDA (#9177) ## Changes ### 1. Triton training ops on ROCm - **Gates removed:** fused RMSNorm, fused RoPE, the AutoEP fused token ops, chunked cross-entropy and the vocab-parallel ops no longer gate on `torch.version.hip`. - `combine_impl="fused_weighted_sum"` and `row_weighting_impl="fused"` no longer raise on ROCm. - The vocab-parallel ops follow Triton availability on ROCm instead of `deepspeed.HAS_TRITON`. That flag is False on ROCm only to keep the Triton inference kernels off. - **Subchannel quantization formats:** `CUDA_Accelerator.supported_subchannel_quantization_formats()` on ROCm returns per architecture: - `gfx942` (MI300X): `int8`; OCP FP8 is only emulated there. - `gfx950` (MI350X): `int8`, `e4m3` and `e5m2`. - Other architectures: nothing, so they fall back to PyTorch. - **Grouped GEMM:** `torch._grouped_mm` stays the default on AMD. The Triton grouped GEMM is correct there but not uniformly faster on MI300X; the comment now says so. - **Tests:** the ROCm skip in `test_fused_rms_norm.py` is removed. - **Docs:** `autoep.rst` no longer says that fused RMSNorm and RoPE run eager on ROCm, or that fused row weighting is rejected there. ### 2. JSD and the other vocab-parallel Triton kernels on every CUDA GPU - The vocab-parallel Triton kernels (JSD, log-probability, entropy and KL) run whenever Triton is available and the logits are on a CUDA or ROCm GPU. JSD also uses them on Intel XPU. - Before, they also required `accelerator.is_triton_supported()`, which on CUDA means sm80+, and `deepspeed.HAS_TRITON`. JSD additionally ran on XPU when the Intel Triton backend was active. - So nothing changes on A100 or H100. GPUs older than sm80 now get the Triton kernels instead of eager row chunks; validated on sm75 below. ### 3. Fused RMSNorm on wider rows - The widest supported normalized dimension goes from 2,048 to 32,768. That covers, for example, Qwen3-235B-A22B (4,096) and DeepSeek-V3 (7,168). `replace_rms_norm` leaves wider norms on eager. - Rows wider than 2,048 launch 8 warps instead of 4. More warps would not help: 8 warps can already use nearly all of an SM's registers, and 16 warps were no faster at 32,768 wide. ## Cleanup ### 4. Module moves - `deepspeed/runtime/chunked_cross_entropy.py` becomes `deepspeed/ops/triton_ops/chunked_cross_entropy.py`. - `deepspeed/sequence/vocab_parallel_ops.py` becomes `deepspeed/ops/triton_ops/vocab_parallel_ops.py`. - Imports in the docs (`autoep.rst`, `autotp-training.md`) and the tests are updated. ### 5. Input checks and API cleanup - **Fused RMSNorm:** `assert_supported(hidden, weight)` no longer takes `eps`. Removed checks: - a duplicated Triton-availability check - weight dtype and device checks, already implied by hidden's checks plus the same-dtype and same-device checks - checks for degenerate shapes (zero width, a scalar input) - the 1-D weight and float `eps` checks, which `replace_rms_norm` already enforces at install - **Fused RoPE:** removed the tensor-type checks, the repeated per-tensor CUDA check, `head_dim > 0` and the q/k batch-size match. The last two cases now run and match eager exactly. - **AutoEP fused row weighting:** non-contiguous rows are copied instead of rejected. Strided weights are accepted, since the kernels read them by stride. The 2-D rows check is gone, because the layer always passes 2-D rows. The docs list is updated. - **Chunked cross-entropy:** the backend is chosen automatically: Triton for CUDA logits when Triton is available, PyTorch otherwise. - The `backend` parameter is gone from `chunked_cross_entropy`, `ChunkedCausalLMLoss` and `install_chunked_causal_lm_loss`. - `triton_backend_available()` is removed; it duplicated `is_triton_available()`. ### 6. Tests - `test_fused_rms_norm.py` goes from 122 to 65 tests by removing repeated coverage: - shapes that take the same kernel path - a second dtype for tests that don't depend on dtype - CPU copies of CUDA tests - tests of development-only details - The fused RMSNorm accuracy tolerances now hold on other GPUs. The kernels round exactly as eager does, but each row's FP32 scale can differ in its last bits, which occasionally rounds a value to its neighbor. Near-zero results then sit many ULPs apart. At every width: - weight gradients may differ by up to two rounding steps of the column's largest product; - input gradients may differ by 1e-7 absolute, a floor that applied only to rows up to 128 wide before. - The chunked cross-entropy tests select a backend by the logits' device. The named-backend test is removed. - The token-ops guard test no longer asserts the removed contiguity check. ## Validation ### ROCm: MI300X and MI350X - **Hardware:** 1x AMD Instinct MI300X (gfx942) and, in a separate run, 1x MI350X (gfx950). - **Software:** torch 2.13.0+rocm10.0.0, Triton 3.8.0, transformers 5.24.1, Python 3.12, Ubuntu 24.04 container. | Op (test files) | Master, MI300X | With the patch, MI300X and MI350X | |---|---|---| | Fused RMSNorm (118 tests at the time) | 61 passed, 57 skipped | 117 passed, 1 skipped (two GPUs) | | Fused RoPE | 0 passed, 34 skipped | 32 passed, 2 skipped (two GPUs) | | AutoEP fused token ops, with `test_autoep_fused_parity.py` | 0 passed, 86 skipped | 64 passed, 22 skipped (two to four GPUs) | | Chunked cross-entropy | 31 passed, 18 skipped, 1 failed | 49 passed, 1 failed (pre-existing, below) | | Vocab-parallel ops | 67 passed, 46 skipped | 100 passed, 13 skipped (two GPUs); all 33 Triton tests pass, JSD's 11 included | | Subchannel quantization | 32 passed, 29 skipped | 56 passed, 5 skipped (two GPUs) with all formats offered | | Fused SwiGLU, with `test_autoep_expert_activation.py` | 288 passed, 16 skipped | unchanged | | AutoEP `permute_rows` | 27 passed | unchanged | | Triton grouped GEMM | 77 passed, 1 skipped | unchanged | - **Pre-existing failure:** `test_collection_leaves_cuda_initialization_fork_safe` fails on ROCm on master too. On ROCm PyTorch the module-level `torch.cuda.device_count()` initializes the GPU runtime, so a forked child cannot use the GPU. It is unrelated to Triton. - **Subchannel formats:** - gfx942 lists only `int8`. Triton emulates OCP FP8 in FP16 there, with no FP8 matrix cores, making the FP8 GEMMs 1.4-6.9x slower than BF16. The row-scaled FP8 tile also needs 96 KB of shared memory against MI300X's 64 KB, so it fails with `OutOfResources` at realistic shapes. - gfx950 runs `e4m3` and `e5m2` on native FP8 matrix cores, so it lists all three formats. - Subchannel quantization is opt-in and slower than plain bf16 end to end on both GPUs. Its fused kernels are 3.9-6.4x faster than the PyTorch reference fallback that an unlisted format gets. - The subchannel test file runs its GPU tests only when `e4m3` is offered. So the INT8-only setting on MI300X is covered by the run with all formats offered, not by a default run. **JSD Triton kernels on CUDA:** a standalone script, run on the T1000. - **Accuracy:** - Inputs: 33 rows; vocab sizes 7, 4,097 and 151,936; beta 0.1, 0.5 and 0.9; fp32, bf16 and fp16 logits. - In every case but one, Triton's output and gradient errors are within 2× of eager's. The exception is bf16 with near-identical distributions, vocab 7, beta 0.1: Triton's output error is 2.06× eager's. - That case is limited by fp32 cancellation in both paths. The absolute error is 6.5e-8 on a JSD of 1.35e-4, and the gradient errors are identical. In other near-identical cases Triton is slightly more accurate than eager. - **Exact answers** (vocab 151,936, fp32 and bf16), all pass: - disjoint supports give H(beta) with zero gradient - identical inputs give 0 - an all `-inf` row gives NaN with zero gradient - **Non-contiguous input:** bf16 `logits[:, :-1]` with vocab 151,936 is read in place, with output error 6.5e-7. - **Speed and memory:** forward plus backward, bf16, 1,024 tokens × vocab 151,936, beta 0.5, median of 5 runs. | Path | Time | Peak memory | |---|---|---| | Triton | 20.7 ms | 594 MiB (the gradients themselves) | | Eager | 486.4 ms | 1,232 MiB | Triton is 23.5× faster and uses about half the peak memory. **Fused RMSNorm with 8 warps**, measured on the T1000 in bf16: - Results match eager in bf16 and fp16 from 2,049 to 32,768 wide. - Rows up to 16,384 wide run at 132-143 GB/s without register spills. - At 32,768 the forward runs at 115 GB/s and the input gradient, which spills a few registers, at 112 GB/s. 16 warps gave 121 and 103 GB/s, so that tier was dropped. **Fused RMSNorm tolerances**, checked on the T1000 with the real kernels and with an exact PyTorch copy of the weight-gradient arithmetic: - Over 200 seeds for each accuracy shape and dtype, the old rules failed 1-3 seeds in 6 of 16 cases. The new rules pass all 3,200 runs. - With each row's FP32 scale shifted by up to ±4 ULPs, as another GPU's summation order would, the old weight-gradient rule failed 10-71% of the time in 5 of 16 cases. The new rule never failed. - Both rules catch the same injected bugs: a zeroed or scaled column, a dropped row, and a 0.8% error everywhere. --------- Signed-off-by: Hongwei Chen <hongweichenmail@gmail.com> Co-authored-by: Zhipeng Wang <zhipeng.rainbowserie@gmail.com>
Author
Parents
Loading