Skip to content
zuoyanzhangPublic

About

Shockwave: memory-safe hierarchical planning for hybrid-parallel Transformer training

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

7,084 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Shockwave

Shockwave is a memory-safe hierarchical planner for hybrid-parallel Transformer training. It is implemented on top of Megatron-LM and combines three stages:

  1. Memory-feasible parallel search over data, tensor, pipeline, context, and micro-batch dimensions (plus expert parallelism for MoE models and optional ZeRO-1/2/3 recipes).
  2. Sensitivity-guided configuration selection, which profiles a balanced baseline and a small set of single-dimension anchors, ranks the useful parallel dimensions, and validates the selected configuration with runtime measurements.
  3. Pipeline stage layer partitioning, which profiles stage time and memory and solves a mixed-integer program to assign Transformer layers to pipeline stages.

The implementation accompanies the paper:

Zuoyan Zhang, Zhiqiang Zhang, Nanqin Wang, Jinyi Wang, and Jie Zhao. Memory-Safe Hierarchical Planning for Hybrid-Parallel Transformer Training. ACM Transactions on Architecture and Code Optimization, 2026. DOI: 10.1145/3842760

Repository layout

  • megatron/core/auto_parallel/: memory admission, search-space generation, sensitivity-guided selection, and configuration profiling.
  • megatron/core/auto_pipeline/: pipeline profiling, time/memory model fitting, and MIP layer partitioning.
  • examples/shockwave/run_llama2_7b.sh: example python/torchrun workflow for Llama-2 7B.
  • examples/llama/: additional model configurations used by this implementation.

Requirements

Component Version
Python >=3.10
PyTorch 2.8.0
NumPy 1.26.4
NVIDIA Transformer Engine 2.7.0
FlashAttention >=2.7.3,<3.0
PuLP 3.2.2

The CUDA and NCCL versions must be compatible with the installed PyTorch build. torchrun is included with PyTorch, and PuLP includes the CBC solver used for pipeline partitioning.

The current branch targets NVIDIA A100 GPUs. Other NVIDIA GPU generations may work, but the memory limit and performance-sensitive settings should be adjusted for the target system.

Installation

1. Clone the repository

git clone https://github.com/zuoyanzhang/Shockwave.git
cd Shockwave

2. Create an environment

Using a recent NVIDIA PyTorch container is the easiest way to obtain compatible CUDA, NCCL, and PyTorch versions. A native virtual environment also works:

python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip setuptools wheel

Install the dependencies and Shockwave:

pip install "torch==2.8.0" "numpy==1.26.4" "pulp==3.2.2"
pip install -e ".[mlm]"
pip install --no-build-isolation \
  "transformer-engine[pytorch]==2.7.0" \
  "flash-attn>=2.7.3,<3.0"

PuLP is declared as a project dependency and is installed by the editable install. Verify the important runtime components with:

python -c "import torch, pulp; print('PyTorch:', torch.__version__); print('GPUs:', torch.cuda.device_count()); print('PuLP:', pulp.__version__)"

Quick start

The commands below use generated mock data and a null tokenizer, so a dataset and tokenizer are not required for a functional run. All commands are launched directly from a shell.

The example script uses eight local GPUs by default. Override GPUS_PER_NODE when necessary.

Stage 1 and 2: search and select the parallel configuration

GPUS_PER_NODE=8 \
GPU_MEMORY_GB=40 \
bash examples/shockwave/run_llama2_7b.sh auto-parallel \
  2>&1 | tee auto-parallel.log

The final line contains one or two JSON configurations:

Results: {"config_0": {"pipeline-model-parallel-size": 2, "tensor-model-parallel-size": 4, "data-parallel-size": 1, "micro-batch-size": 2, "context-parallel-size": 1, "zero-stage": 1, ...}}

config_0 is the best configuration according to runtime profiling. If present, config_1 is the sensitivity-ranking result when it differs from config_0. Use config_0 for the next stage.

To include ZeRO-1/2/3 in the search space:

AUTO_ZERO=1 \
GPUS_PER_NODE=8 \
GPU_MEMORY_GB=40 \
bash examples/shockwave/run_llama2_7b.sh auto-parallel \
  2>&1 | tee auto-parallel-zero.log

Stage 3: search the pipeline layer partition

Copy TP, PP, CP, MBS, and the ZeRO stage from config_0. DP is derived from the total number of GPUs:

TP=4 \
PP=2 \
CP=1 \
MBS=2 \
ZERO_STAGE=1 \
GPUS_PER_NODE=8 \
GPU_MEMORY_GB=40 \
bash examples/shockwave/run_llama2_7b.sh auto-pipeline \
  2>&1 | tee auto-pipeline.log

The result is printed in Megatron's pipeline-layout format:

layers distribution: [14, 18]
--pipeline-model-parallel-layout E,t*14|t*18,L

The exact layer counts depend on the hardware, parallel configuration, batch size, and measured memory usage.

Train with the generated plan

Pass the selected TP/PP/CP/MBS values and the generated layout to the same script:

TP=4 \
PP=2 \
CP=1 \
MBS=2 \
ZERO_STAGE=1 \
PIPELINE_LAYOUT='E,t*14|t*18,L' \
GPUS_PER_NODE=8 \
TRAIN_ITERS=1000 \
bash examples/shockwave/run_llama2_7b.sh train

The three modes are intentionally run sequentially. Do not pass --enable-auto-parallel and --enable-auto-pipeline in the same process: the auto-parallel branch exits after producing its result.

The current search admits ZeRO-2/3 configurations only when PP is 1. If config_0 selects ZeRO-2 or ZeRO-3 with PP=1, there is no multi-stage pipeline to partition; skip auto-pipeline and run train with the selected ZERO_STAGE.

Using a real dataset

Shockwave uses Megatron-LM indexed datasets. Given a JSON or JSONL file whose documents contain a text field, preprocess it with:

python tools/preprocess_data.py \
  --input /path/to/corpus.jsonl \
  --output-prefix /path/to/processed/my_corpus \
  --tokenizer-type Llama2Tokenizer \
  --tokenizer-model /path/to/tokenizer.model \
  --append-eod \
  --workers 16

Set DATA_PATH to the resulting dataset prefix and provide the tokenizer model before running any of the three modes:

export DATA_PATH=/path/to/processed/my_corpus_text_document
export TOKENIZER_MODEL=/path/to/tokenizer.model

GPUS_PER_NODE=8 GPU_MEMORY_GB=40 \
  bash examples/shockwave/run_llama2_7b.sh auto-parallel

When DATA_PATH is unset, the script automatically uses mock data.

Multi-node launch

Run the same command on every node, using the same MASTER_ADDR, MASTER_PORT, and NNODES, and a different zero-based NODE_RANK.

For example, on node 0:

NNODES=2 NODE_RANK=0 GPUS_PER_NODE=8 \
MASTER_ADDR=10.0.0.1 MASTER_PORT=6000 \
bash examples/shockwave/run_llama2_7b.sh auto-parallel

On node 1:

NNODES=2 NODE_RANK=1 GPUS_PER_NODE=8 \
MASTER_ADDR=10.0.0.1 MASTER_PORT=6000 \
bash examples/shockwave/run_llama2_7b.sh auto-parallel

Repeat the same per-node launch pattern for auto-pipeline and train. The auto-parallel coordinator uses MASTER_PORT, MASTER_PORT + 1, and MASTER_PORT + 2; ensure that all three ports are reachable between nodes.

Script configuration

The example script accepts configuration through environment variables:

Variable Default Description
GPUS_PER_NODE 8 GPUs used on each node
NNODES 1 Number of nodes
NODE_RANK 0 Zero-based node rank
MASTER_ADDR 127.0.0.1 Address of node 0
MASTER_PORT 6000 Base communication port
GPU_MEMORY_GB 40 Per-GPU memory budget used by Shockwave
TP, PP, CP 1, 1, 1 Tensor, pipeline, and context parallel sizes
MBS, GBS 1, 256 Micro- and global-batch sizes
SEQ_LENGTH 2048 Training sequence length
AUTO_ZERO 0 Set to 1 to search ZeRO-1/2/3
ZERO_STAGE 1 ZeRO recipe applied by auto-pipeline and train
PIPELINE_LAYOUT empty Required by train mode
TRAIN_ITERS 1000 Iterations used by train mode
DATA_PATH empty Megatron indexed-dataset prefix; empty selects mock data
TOKENIZER_MODEL empty Required when DATA_PATH is set

For another architecture, copy the script and replace the model dimensions and tokenizer settings. --model-type must match one of the model types accepted by Shockwave's auto-parallel argument parser.

Generated files

Auto-parallel profiling results are cached in autoparallel_temp_cache/. Reusing the same model and configuration allows later searches to reuse valid measurements. Move or remove that directory when a completely fresh profiling run is required.

Citation

@article{10.1145/3842760,
  author = {Zhang, Zuoyan and Zhang, Zhiqiang and Wang, Nanqin and Wang, Jinyi and Zhao, Jie},
  title = {Memory-Safe Hierarchical Planning for Hybrid-Parallel Transformer Training},
  year = {2026},
  publisher = {Association for Computing Machinery},
  doi = {10.1145/3842760},
  journal = {ACM Transactions on Architecture and Code Optimization}
}

Acknowledgements and license

Shockwave is built on NVIDIA Megatron-LM. The repository retains the upstream Apache 2.0 license.

About

Shockwave: memory-safe hierarchical planning for hybrid-parallel Transformer training

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages