This guide demonstrates how to perform pretraining using Megatron/torchtitan within the Primus framework. It supports both single-node and multi-node training, and includes optional HipBLASLt auto-tuning for optimal AMD GPU performance.
- 🧠 Pretraining with Primus
Primus supports multiple backends.
| Backend | Description |
|---|---|
| Megatron | Open-source framework for large-scale transformer training |
| TorchTitan | PyTorch-compatible framework developed for training at scale |
We recommend using the official rocm/megatron-lm Docker image to ensure a stable and compatible training environment. Use the following commands to pull and launch the container:
# Pull the latest Docker image
docker pull docker.io/rocm/primus:v26.3
Clone the repository and install dependencies:
# Clone with submodules
cd /workspace
git clone --recurse-submodules git@github.com:AMD-AGI/Primus.git
# Or initialize submodules if already cloned
git submodule update --init --recursive
cd Primus
# Install Python dependencies
pip install -r requirements.txt
# Set up pre-commit hooks
pre-commit installUse the run_pretrain.sh script to start training.
Use this mode for rapid iteration or validation of a model config. You do not need to enter the Docker container. Just set the config and run.
# Example for megatron llama3.1_8B
EXP=examples/megatron/configs/MI300X/llama3.1_8B-BF16-pretrain.yaml bash ./examples/run_local_pretrain.sh
# examples for torchtitan llama3.1_8B
EXP=examples/torchtitan/configs/MI300X/llama3.1_8B-pretrain.yaml bash ./examples/run_local_pretrain.shThis mode is recommended for development, debugging, or running custom workflows. You will manually enter the container and execute training inside.
# Launch the container
bash tools/docker/start_container.sh
# Access the container
docker exec -it dev_primus bash
# install required packages
cd Primus && pip install -r requirements.txt
# Example for megatron llama3.1_8B
EXP=examples/megatron/configs/MI300X/llama3.1_8B-BF16-pretrain.yaml bash ./examples/run_pretrain.sh
# examples for torchtitan llama3.1_8B
EXP=examples/torchtitan/configs/MI300X/llama3.1_8B-pretrain.yaml bash ./examples/run_pretrain.sh
Multi-node training is launched via SLURM. Specify the number of nodes and the model config:
export DOCKER_IMAGE="docker.io/rocm/primus:v26.3"
export NNODES=8
# Example for megatron llama3.1_8B
EXP=examples/megatron/configs/MI300X/llama3.1_8B-BF16-pretrain.yaml bash ./examples/run_slurm_pretrain.sh
# examples for torchtitan llama3.1_8b
EXP=examples/torchtitan/configs/MI300X/llama3.1_8B-pretrain.yaml bash ./examples/run_slurm_pretrain.shHipblasLT tuning is divided into three stages and controlled via the environment variable PRIMUS_HIPBLASLT_TUNING_STAGE:
# default 0 means no tuning
export PRIMUS_HIPBLASLT_TUNING_STAGE=${PRIMUS_HIPBLASLT_TUNING_STAGE:-0}In this stage, GEMM shapes used during training are collected.
It is recommended to reduce train_iters for faster shape generation.
# Output will be stored to:
# ./output/tune_hipblaslt/${PRIMUS_MODEL}/gemm_shape
export PRIMUS_HIPBLASLT_TUNING_STAGE=1
export EXP=examples/megatron/configs/MI300X/llama2_7B-BF16-pretrain.yaml
NNODES=1 bash ./examples/run_slurm_pretrain.shThis stage performs kernel tuning based on the dumped GEMM shapes using the offline_tune tool. It typically takes 10–30 minutes depending on model size and shape complexity.
# Output will be stored to:
# ./output/tune_hipblaslt/${PRIMUS_MODEL}/gemm_tune/tune_hipblas_gemm_results.txt
export PRIMUS_HIPBLASLT_TUNING_STAGE=2
export EXP=examples/megatron/configs/MI300X/llama2_7B-BF16-pretrain.yaml
NNODES=1 bash ./examples/run_slurm_pretrain.shIn this final stage, the tuned kernel is loaded for efficient training:
export PRIMUS_HIPBLASLT_TUNING_STAGE=3
export EXP=examples/megatron/configs/MI300X/llama2_7B-BF16-pretrain.yaml
NNODES=1 bash ./examples/run_slurm_pretrain.shThe following models are supported out of the box via provided configuration files:
Use the following command pattern to start training with a selected model configuration:
EXP=examples/megatron/configs/MI300X/<model_config> bash ./examples/run_local_pretrain.shFor example, to run the llama3.1_8B model quickly:
EXP=examples/megatron/configs/MI300X/llama3.1_8B-BF16-pretrain.yaml bash ./examples/run_local_pretrain.sh
EXP=examples/torchtitan/configs/MI300X/llama3.1_8B-pretrain.yaml bash ./examples/run_local_pretrain.shFor multi-node training via SLURM, use:
export NNODES=8
#run megatron
EXP=examples/megatron/configs/MI300X/llama3.1_8B-BF16-pretrain.yaml bash ./examples/run_slurm_pretrain.sh
# run torchtitan
EXP=examples/torchtitan/configs/MI300X/llama3.1_8B-pretrain.yaml bash ./examples/run_slurm_pretrain.shThe run_k8s_pretrain.sh script provides convenient CLI commands to manage training workloads on a Kubernetes cluster via a REST API. It supports creating, querying, deleting training jobs, and listing cluster nodes, facilitating flexible workload control for distributed training with Primus or similar frameworks.
jqinstalled (for JSON processing)- Access to Kubernetes API endpoint URL
./run_k8s_pretrain.sh --url <api_base_url> <command> [options]
Primus provides several command-line interfaces to manage training workloads and cluster resources. Below are the commonly used commands:
| Command | Description |
|---|---|
| create | Create a new training workload |
| get | Retrieve workload details |
| delete | Delete an existing workload |
| list | List all current workloads |
| nodes | List all nodes in the cluster |
Use these commands to interact with Primus for workload scheduling and resource management.
When using the create command to start a new training workload, the following options are supported:
| Option | Description | Default |
|---|---|---|
--replica |
Number of replicas (instances) | 1 |
--cpu |
Number of CPUs | 96 |
--gpu |
Number of GPUs | 8 |
--exp |
Path to experiment (training config) file (required) | — |
--data_path |
Path to training data | — |
--image |
Docker image to use | docker.io/rocm/primus:v26.3 |
--hf_token |
HuggingFace token | Read from env var HF_TOKEN |
--workspace |
Workspace name | primus-safe-pretrain |
--nodelist |
Comma-separated list of node hostnames to run on | — |
Create a training workload with 2 replicas and custom config:
bash examples/run_k8s_pretrain.sh --url http://api.example.com create --replica 2 --cpu 96 --gpu 4 \
--exp examples/megatron/configs/MI300X/llama2_7B-BF16-pretrain.yaml --data_path /mnt/data/train \
--image docker.io/custom/image:latest --hf_token myhf_token --workspace team-dev
#result:
{
"workloadId": "abc123"
}
Get workload details:
bash examples/run_k8s_pretrain.sh --url http://api.example.com get --workload-id abc123
Delete a workload:
bash examples/run_k8s_pretrain.sh --url http://api.example.com delete --workload-id abc123
List all workloads:
bash examples/run_k8s_pretrain.sh --url http://api.example.com list
List all cluster nodes:
bash examples/run_k8s_pretrain.sh --url http://api.example.com nodes