Skip to Content
MS-SWIFT × TWINKLE
💻 Hardware Platforms
DOCUMENTATION5.0.0.dev

🖥️ AMD GPU Support

🧰 1. Environment setup

🧰 1.1 Base environment

Pull the ms-swift image adapted for the AMD ROCm ecosystem and start the container with the following commands.

To run a newer version of ms-swift, upgrade with pip or install from source. Add --no-deps when installing to avoid problems caused by automatically upgrading other dependencies.

IMAGE_NAME=amdagi/modelscope:ubuntu22.04-rocm7.2.0-py312-torch2.10.0-vllm0.18.1-modelscope1.35.1-swift4.1.0
docker pull ${IMAGE_NAME}

CONTAINER_NAME=swift_test
docker run -it --network=host --ipc=host --privileged --group-add video \
    --device=/dev/dri --device=/dev/kfd \
    --shm-size 512G --ulimit memlock=-1 \
    --security-opt seccomp=unconfined --cap-add SYS_PTRACE \
    --name ${CONTAINER_NAME} \
    ${IMAGE_NAME} \
    /bin/bash

🔍 1.2 Environment check

  • Confirm that pytorch in the container recognizes the AMD GPU.
python -c "import torch;print(torch.cuda.is_available())"  # output: True
  • Check the GPU topology and NUMA: rocm-smi --showtopo
============================ ROCm System Management Interface ============================
WARNING: AMD GPU device(s) is/are in a low-power state. Check power control/runtime_status

================================ Weight between two GPUs =================================
       GPU0         GPU1         GPU2         GPU3         GPU4         GPU5         GPU6         GPU7
GPU0   0            15           15           15           15           15           15           15
GPU1   15           0            15           15           15           15           15           15
GPU2   15           15           0            15           15           15           15           15
GPU3   15           15           15           0            15           15           15           15
GPU4   15           15           15           15           0            15           15           15
GPU5   15           15           15           15           15           0            15           15
GPU6   15           15           15           15           15           15           0            15
GPU7   15           15           15           15           15           15           15           0

================================= Hops between two GPUs ==================================
       GPU0         GPU1         GPU2         GPU3         GPU4         GPU5         GPU6         GPU7
GPU0   0            1            1            1            1            1            1            1
GPU1   1            0            1            1            1            1            1            1
GPU2   1            1            0            1            1            1            1            1
GPU3   1            1            1            0            1            1            1            1
GPU4   1            1            1            1            0            1            1            1
GPU5   1            1            1            1            1            0            1            1
GPU6   1            1            1            1            1            1            0            1
GPU7   1            1            1            1            1            1            1            0

=============================== Link Type between two GPUs ===============================
       GPU0         GPU1         GPU2         GPU3         GPU4         GPU5         GPU6         GPU7
GPU0   0            XGMI         XGMI         XGMI         XGMI         XGMI         XGMI         XGMI
GPU1   XGMI         0            XGMI         XGMI         XGMI         XGMI         XGMI         XGMI
GPU2   XGMI         XGMI         0            XGMI         XGMI         XGMI         XGMI         XGMI
GPU3   XGMI         XGMI         XGMI         0            XGMI         XGMI         XGMI         XGMI
GPU4   XGMI         XGMI         XGMI         XGMI         0            XGMI         XGMI         XGMI
GPU5   XGMI         XGMI         XGMI         XGMI         XGMI         0            XGMI         XGMI
GPU6   XGMI         XGMI         XGMI         XGMI         XGMI         XGMI         0            XGMI
GPU7   XGMI         XGMI         XGMI         XGMI         XGMI         XGMI         XGMI         0

======================================= Numa Nodes =======================================
GPU[0]          : (Topology) Numa Node: 0
GPU[0]          : (Topology) Numa Affinity: 0
GPU[1]          : (Topology) Numa Node: 0
GPU[1]          : (Topology) Numa Affinity: 0
GPU[2]          : (Topology) Numa Node: 0
GPU[2]          : (Topology) Numa Affinity: 0
GPU[3]          : (Topology) Numa Node: 0
GPU[3]          : (Topology) Numa Affinity: 0
GPU[4]          : (Topology) Numa Node: 1
GPU[4]          : (Topology) Numa Affinity: 1
GPU[5]          : (Topology) Numa Node: 1
GPU[5]          : (Topology) Numa Affinity: 1
GPU[6]          : (Topology) Numa Node: 1
GPU[6]          : (Topology) Numa Affinity: 1
GPU[7]          : (Topology) Numa Node: 1
GPU[7]          : (Topology) Numa Affinity: 1
================================== End of ROCm SMI Log ===================================
  • Check GPU utilization, memory usage and similar information (rocm-smi or rocm-smi -u --showmeminfo vram)
# output of 'rocm-smi'
============================================ ROCm System Management Interface ============================================
====================================================== Concise Info ======================================================
Device  Node  IDs              Temp        Power     Partitions          SCLK    MCLK    Fan  Perf  PwrCap  VRAM%  GPU%
              (DID,     GUID)  (Junction)  (Socket)  (Mem, Compute, ID)
==========================================================================================================================
0       2     0x74a2,   1017   43.0°C      155.0W    NPS1, SPX, 0        94Mhz   900Mhz  0%   auto  650.0W  0%     0%
1       3     0x74a2,   47713  41.0°C      155.0W    NPS1, SPX, 0        91Mhz   900Mhz  0%   auto  650.0W  0%     0%
2       4     0x74a2,   37449  45.0°C      159.0W    NPS1, SPX, 0        95Mhz   900Mhz  0%   auto  650.0W  0%     0%
3       5     0x74a2,   11217  41.0°C      155.0W    NPS1, SPX, 0        95Mhz   900Mhz  0%   auto  650.0W  0%     0%
4       6     0x74a2,   41880  44.0°C      160.0W    NPS1, SPX, 0        91Mhz   900Mhz  0%   auto  650.0W  0%     0%
5       7     0x74a2,   6656   42.0°C      157.0W    NPS1, SPX, 0        95Mhz   900Mhz  0%   auto  650.0W  0%     0%
6       8     0x74a2,   12840  45.0°C      160.0W    NPS1, SPX, 0        96Mhz   900Mhz  0%   auto  650.0W  0%     0%
7       9     0x74a2,   35760  43.0°C      158.0W    NPS1, SPX, 0        107Mhz  900Mhz  0%   auto  650.0W  0%     0%
==========================================================================================================================
================================================== End of ROCm SMI Log ===================================================

# output of 'rocm-smi -u --showmeminfo vram'
============================ ROCm System Management Interface ============================
=================================== % time GPU is busy ===================================
GPU[0]          : GPU use (%): 0
GPU[0]          : GFX Activity: 3862538534
GPU[1]          : GPU use (%): 0
GPU[1]          : GFX Activity: 4053246251
GPU[2]          : GPU use (%): 0
GPU[2]          : GFX Activity: 3114103535
GPU[3]          : GPU use (%): 0
GPU[3]          : GFX Activity: 4026776444
GPU[4]          : GPU use (%): 0
GPU[4]          : GFX Activity: 1224255679
GPU[5]          : GPU use (%): 0
GPU[5]          : GFX Activity: 1191191242
GPU[6]          : GPU use (%): 0
GPU[6]          : GFX Activity: 1184652679
GPU[7]          : GPU use (%): 0
GPU[7]          : GFX Activity: 2145209382
==========================================================================================
================================== Memory Usage (Bytes) ==================================
GPU[0]          : VRAM Total Memory (B): 206141652992
GPU[0]          : VRAM Total Used Memory (B): 297611264
GPU[1]          : VRAM Total Memory (B): 206141652992
GPU[1]          : VRAM Total Used Memory (B): 297623552
GPU[2]          : VRAM Total Memory (B): 206141652992
GPU[2]          : VRAM Total Used Memory (B): 297623552
GPU[3]          : VRAM Total Memory (B): 206141652992
GPU[3]          : VRAM Total Used Memory (B): 297623552
GPU[4]          : VRAM Total Memory (B): 206141652992
GPU[4]          : VRAM Total Used Memory (B): 297623552
GPU[5]          : VRAM Total Memory (B): 206141652992
GPU[5]          : VRAM Total Used Memory (B): 297623552
GPU[6]          : VRAM Total Memory (B): 206141652992
GPU[6]          : VRAM Total Used Memory (B): 297623552
GPU[7]          : VRAM Total Memory (B): 206141652992
GPU[7]          : VRAM Total Used Memory (B): 297623552
==========================================================================================
================================== End of ROCm SMI Log ===================================

💡 2. Run examples

📄 2.1 Full-parameter fine-tuning of Qwen3.5 with ms-swift Megatron

AMD GPUs have relatively large memory, so the following arguments can be tuned together to raise training throughput.

  • Parallelism tuning (TP/PP/EP, etc.): the large per-GPU memory lets users reduce the communication overhead of parallel splitting as much as possible (priority PP/EP > TP)
  • Disable optimizer cpu offload when memory allows: set --optimizer_cpu_offload false
  • Adjust activation/gradient checkpointing when memory allows: set --recompute_granularity none, or --recompute_granularity selective with --recompute_modules for fine-grained control
  • For MoE models, set export NVTE_USE_GROUPED_GEMM_TRITON=1 to use the triton implementation of the grouped gemm kernel
  • For models with a GatedDeltaNet structure, set USE_MCORE_GDN=1 to use the mcore implementation
  • To avoid issues that may appear on some AMD GPUs and keep performance stable, set export HSA_NO_SCRATCH_RECLAIM=1

Single-node training:

export HSA_NO_SCRATCH_RECLAIM=1
export NVTE_USE_GROUPED_GEMM_TRITON=1

output_dir=${PWD}/megatron_output/Qwen3.5-35B-A3B
mkdir -p ${output_dir}
current_time=$(date "+%Y.%m.%d-%H.%M.%S")
log_file=${output_dir}/"1node_full_megatron_Qwen3.5-35B-A3B_${current_time}.log"

PYTORCH_CUDA_ALLOC_CONF='expandable_segments:True' \
NPROC_PER_NODE=8 \
MAX_PIXELS=1003520 \
VIDEO_MAX_PIXELS=50176 \
FPS_MAX_FRAMES=12 \
SKIP_MULTIMODAL_MTP_VALIDATION=1 \
USE_MCORE_GDN=1 \
swift sft \
    --backend megatron \
    --model Qwen/Qwen3.5-35B-A3B \
    --dataset 'AI-ModelScope/LongAlpaca-12k' \
    --save_safetensors true \
    --load_from_cache_file true \
    --tuner full \
    --add_non_thinking_prefix true \
    --split_dataset_ratio 0.01 \
    --tensor_model_parallel_size 1 \
    --pipeline_model_parallel_size 1 \
    --expert_model_parallel_size 8 \
    --sequence_parallel true \
    --moe_permute_fusion true \
    --moe_grouped_gemm true \
    --moe_shared_expert_overlap true \
    --moe_aux_loss_coeff 1e-6 \
    --moe_expert_capacity_factor 2 \
    --per_device_train_batch_size 1 \
    --global_batch_size 8 \
    --recompute_granularity selective \
    --recompute_modules core_attn mlp moe \
    --gradient_accumulation_fusion false \
    --num_train_epochs 500 \
    --group_by_length true \
    --freeze_llm false \
    --freeze_vit false \
    --freeze_aligner false \
    --cross_entropy_loss_fusion true \
    --learning_rate 1e-5 \
    --warmup_ratio 0.05 \
    --min_lr 1e-6 \
    --weight_decay 0.1 \
    --adam_beta2 0.95 \
    --eval_steps 500 \
    --save_steps 500 \
    --save_total_limit 10 \
    --logging_steps 1 \
    --max_length 16384 \
    --dataloader_num_workers 8 \
    --dataset_num_proc 8 \
    --no_save_optim true \
    --no_save_rng true \
    --optimizer_cpu_offload false \
    --attn_impl flash \
    --padding_free false \
    --output_dir ${output_dir} \
    2>&1 | tee ${log_file}

Multi-node training:

export NNODES=2  # 2 nodes here as an example
export NODE_RANK=0  # set 0 for the master node and 1 for worker nodes
export MASTER_ADDR=<MASTER_NODE_IP>  # set according to the master node ip
export MASTER_PORT=29500  # set the communication port
export NCCL_SOCKET_IFNAME=ens50f1np1  # set according to the machine's actual network interface name; check with ifconfig
export GLOO_SOCKET_IFNAME=ens50f1np1  # set according to the machine's actual network interface name; check with ifconfig
export NCCL_IB_HCA=mlx5_0,mlx5_1,mlx5_2,mlx5_3  # set according to the actual IB adapter names; check with ibv_devices
export NCCL_IB_GID_INDEX=3

# main body of the training script: follow the single-node training script
...

🎯 2.2 Reinforcement learning training of Qwen3.5 with ms-swift Megatron

# single-node training example
export HSA_NO_SCRATCH_RECLAIM=1
export NVTE_USE_GROUPED_GEMM_TRITON=1

SYSTEM_PROMPT="""You are a helpful math assistant. Solve the problem step by step and put your final answer within \\boxed{}."""

CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 \
PYTORCH_CUDA_ALLOC_CONF='expandable_segments:True' \
swift rl \
    --backend megatron \
    --rl_type grpo \
    --model Qwen/Qwen3.5-35B-A3B \
    --nproc_per_node 8 \
    --save_safetensors true \
    --enable_thinking false \
    --context_parallel_size 1 \
    --tensor_model_parallel_size 1 \
    --expert_model_parallel_size 8 \
    --pipeline_model_parallel_size 1 \
    --moe_permute_fusion true \
    --dataset open-r1/DAPO-Math-17k-Processed \
    --system "$SYSTEM_PROMPT" \
    --num_train_epochs 1 \
    --global_batch_size 64 \
    --per_device_train_batch_size 1 \
    --num_iterations 2 \
    --num_generations 8 \
    --orm accuracy \
    --rollout_sampler vllm \
    --sampler_mode colocate \
    --vllm_gpu_memory_utilization 0.5 \
    --vllm_tensor_parallel_size 2 \
    --vllm_max_model_len 9192 \
    --max_length 1000 \
    --max_completion_length 8192 \
    --tuner lora \
    --target_modules all-linear \
    --learning_rate 5e-5 \
    --torch_dtype bfloat16 \
    --beta 0.00 \
    --epsilon 0.2 \
    --epsilon_high 0.28 \
    --dynamic_sample false \
    --overlong_filter true \
    --loss_type grpo \
    --sleep_level 1 \
    --offload_model true \
    --offload_optimizer true \
    --logging_steps 1 \
    --recompute_granularity none \
    --gradient_accumulation_fusion false \
    --resume_only_model true \
    --dataloader_num_workers 8 \
    --dataset_num_proc 8 \
    --no_save_optim true \
    --no_save_rng true \
    --save_steps 20 \
    --attn_impl flash \
    --moe_expert_capacity_factor 2 \
    --temperature 1.0 \
    --padding_free false \
    --sequence_parallel false \
    --log_completions true \
    --report_to tensorboard

⚠️ Known issues

  • Reinforcement learning training:
    • When vLLM is used as the inference engine in reinforcement learning training, vLLM>=0.11.0 is required. Use ROCm7.0 or the image we provide to avoid the sleep mode memory leak problem.
    • When training on multiple GPUs/Nodes with Ray instead of torchrun, do not set CUDA_VISIBLE_DEVICES/HIP_VISIBLE_DEVICES and similar variables, to avoid conflicts.
  • MoE model training:
    • For MoE models, add the environment variable NVTE_USE_GROUPED_GEMM_TRITON=1 and --gradient_accumulation_fusion false to avoid occasional GPU hangs.