This document is relevant for: Trn3

Tutorial: Train a model with expert parallelism on Neuron#

This tutorial walks you through training a mixture-of-experts (MoE) model with expert parallelism (EP) using TorchTitan on AWS Trainium3 (Trn3). When you have completed it, you will have trained the full 94-layer Qwen3-235B-A22B model for 50 steps on a four-node Trn3 UltraServer, with its experts split across 128 ranks.

Overview#

Qwen3-235B-A22B has 128 experts in each MoE layer, and each token uses 8 of them. With expert parallelism, every rank keeps only some of the experts, and the tokens travel to the ranks that own their experts. This tutorial runs the Neuron recipe that splits the experts over 128 ranks (EP 128) and shards the rest of the model with FSDP and tensor parallelism.

For what EP shards and how the EP degree combines with FSDP, see How to use EP to train a model with TorchTitan on Neuron.

Before you start#

This tutorial assumes that you have experience in the following areas:

Model details#

This tutorial uses the recipe qwen3_235b_94layers_ep128_tp4_fsdp64_pp1_4k_neuron_ep_compile in torchtitan_neuron.models.qwen3.config_registry with the following settings:

Setting

Value

Model

Qwen3-235B-A22B, all 94 layers, randomly initialized

Parallelism

EP 128, tensor parallelism 4, FSDP 64

Ranks

256: four nodes with 64 ranks each

Sequence length

4,096 tokens

Batch size

1 per data-parallel rank, 64 global

Activation checkpointing

Full (FullAC)

Compilation

torch.compile with the Neuron backend, for each transformer block and the loss

Training steps

50

Limitations:

  • Validated with Neuron SDK 2.34 on a Trn3 Gen1 UltraServer (four servers) in Amazon EKS, with the nodes set up by the Neuron UltraServer init container. See How to schedule MPI jobs to run on Neuron UltraServer on EKS.

  • The run starts from random weights and trains on c4_test, a small sample bundled with TorchTitan. It confirms that the EP path works. It is not a production training run.

Prerequisites#

  • A Trn3 Gen1 UltraServer: four Trn3 servers in two racks. All four nodes must be in the same UltraServer. See Amazon EC2 Trn3 Architecture.

  • The four nodes set up for UltraServer mode: the Neuron runtime on each node needs NEURON_RT_ULTRASERVER_MODE, NEURON_RT_ULTRASERVER_SERVER_ID, and NEURON_RT_ULTRASERVER_NODE_ID. This tutorial covers Amazon EKS, where the Neuron UltraServer init container sets them. See How to schedule MPI jobs to run on Neuron UltraServer on EKS.

  • Neuron SDK 2.34 with PyTorch 2.14 and torch-neuronx verified on each node. See Quickstart: Install and verify torch-neuronx.

  • TorchTitan installed on each node at the same path, as described in How to set up TorchTitan Neuron.

  • Network access between the nodes, with EFA on each node. On each node, /opt/amazon/efa/bin/fi_info -p efa -t FI_EP_RDM must list at least one provider: efa entry.

  • A shell on each of the four nodes. You run each step in all four shells, and you keep the same shells open through the launch, so that the exported variables are still set.


Step 1: Set the tokenizer and dataset paths#

The recipe uses the test tokenizer and the c4_test sample that come with TorchTitan, so the run needs no download. On each node, work from the torchtitan-neuron directory you installed and set the paths:

cd /path/to/torchtitan-neuron
export ASSET_ROOT="$PWD/tests/assets"
export TOKENIZER_PATH="$ASSET_ROOT/tokenizer"
mkdir -p outputs

The data loader tokenizes the sample and packs it into training sequences. It repeats the sample as needed.

Step 2: Find each node’s position in the UltraServer#

On each node, print the UltraServer topology:

neuron-ls --topology

Check the output of all four nodes:

  • The ultraserver-id is the same on all four nodes.

  • ultraserver-size is 4.

  • The ultraserver-instance-index values are 0, 1, 2, and 3, one on each node.

On each node, set NODE_RANK to that node’s ultraserver-instance-index:

export NODE_RANK=0  # This node's ultraserver-instance-index: 0, 1, 2, or 3

The instance indices 0 and 1 share one rack, and 2 and 3 share the other. With 64 ranks on each node, the recipe forms the EP groups from nodes 0 + 1 and 2 + 3, so each EP group stays inside one rack. FSDP communication can still cross the racks.

Important

Use the instance index from neuron-ls --topology, not the host name or the order of your nodes. torchrun does not choose this placement for you.

Step 3: Set the launch environment#

On each node, set MASTER_ADDR and MASTER_PORT to an address and a free port of node 0, the node whose ultraserver-instance-index is 0. Then set the Neuron settings:

export MASTER_ADDR=<address of node 0>
export MASTER_PORT=29500

export NEURON_CC_FLAGS="--enable-mixed-precision-accumulation --enable-saturate-infinity --auto-cast=none"
export CCOM_SOCKET_IFNAME=eth0
  • MASTER_ADDR must be an address of node 0 that all four nodes can reach. MASTER_PORT must be a free port on that node.

  • NEURON_CC_FLAGS passes the compiler flags of this recipe. --auto-cast=none leaves the precision to the training configuration.

  • CCOM_SOCKET_IFNAME sets the network interface for the Neuron collectives bootstrap. Set it to the interface of your nodes if it is not eth0.

Keep the NODE_RANK from Step 2 in each shell.

Step 4: Launch the run on each node#

In each of the four shells, run the same command. Start all four launches: torchrun waits until all four nodes have joined. This tutorial calls torchrun directly instead of run_train.sh, because the EP placement needs --node-rank from Step 2.

torchrun \
  --nnodes=4 \
  --nproc-per-node=64 \
  --node-rank="$NODE_RANK" \
  --master-addr="$MASTER_ADDR" \
  --master-port="$MASTER_PORT" \
  --max-restarts=0 \
  -m torchtitan.train \
  --module torchtitan_neuron.models.qwen3.config_registry \
  --config qwen3_235b_94layers_ep128_tp4_fsdp64_pp1_4k_neuron_ep_compile \
  --training.local_batch_size=1 \
  --training.global_batch_size=64 \
  --training.seq_len=4096 \
  --training.dtype=float32 \
  --training.mixed_precision_param=bfloat16 \
  --parallelism.enable_sequence_parallel \
  --debug.seed=42 \
  --training.steps=50 \
  --lr_scheduler.warmup_steps=2 \
  --metrics.log_freq=1 \
  --dataloader.dataset=c4_test \
  --dataloader.dataset_path="$ASSET_ROOT/c4_test" \
  --hf_assets_path="$TOKENIZER_PATH" \
  --dump_folder=./outputs/qwen3-235b-ep128
  • -m torchtitan.train runs the TorchTitan trainer, and --module and --config select the Neuron recipe. Use a fully qualified module path.

  • --node-rank comes from Step 2. It decides which ranks form the EP groups.

  • The recipe sets the EP degree, full activation checkpointing, and the compilation. The command sets the batch size, the sequence length, the precision, the seed, and 50 steps.

  • The run does not save checkpoints.

Note

Expect the first steps to take far longer than the later ones. The recipe compiles each transformer block and the loss with the Neuron backend on the first steps. The Neuron stack caches the results on disk, so later runs of the same recipe start faster. See TorchNeuron environment variables for the cache directories.

Step 5: Confirm success#

Every rank prints the startup output, so the shell of each node shows the same lines many times. Check them for the configuration and for EP:

Building device mesh with parallelism: pp=1, dp_replicate=1, dp_shard=64, cp=1, tp=4, ep=128
Successfully created meshes with active dimensions: ['batch', 'loss', 'tp', 'ep', 'efsdp', 'fsdp']
Model qwen3 235B-A22B size: 235,093,634,560 total parameters
Applied FullAC activation checkpointing to the model
Trainer is initialized with local batch size 1, global batch size 64, gradient accumulation steps 1, sequence length 4096, total steps 50 (warmup 2)

ep=128 with the ep and efsdp dimensions means that EP is active; see How to use EP to train a model with TorchTitan on Neuron for what each one shards.

Then watch the per-step loss. In the first steps the loss swings hard: it can rise well above its starting value between steps 2 and 6. After that it falls steadily, from about 12 at the first step to about 3 by step 50, and the gradient norm falls from tens to about 1. Read the trend, not single steps.

Confirmation#

The run succeeded if all of the following hold:

  • The startup output shows ep=128 and the ep and efsdp dimensions.

  • Training runs, and per-step loss lines appear for all 50 steps.

  • The loss values are finite, never nan, and the loss at step 50 is far lower than at step 1.

  • torchrun exits without an error on all four nodes.

Congratulations! You have now trained an MoE model with expert parallelism on a Trn3 UltraServer. If you encountered any issues, see the Common issues section below.


Common issues#

  • The run waits at startup and does not train: Start the command on all four nodes. Check that all four use the same MASTER_ADDR and MASTER_PORT, and that each node has a different NODE_RANK.

  • “Config function not found”: --module needs a fully qualified module path, not the short name qwen3. The short name selects the upstream registry, which has no Neuron EP recipes.

  • Errors about the EP degree: If you changed the parallelism, see the EP degree rules in How to use EP to train a model with TorchTitan on Neuron.

  • The process group fails to initialize with NET/OFI errors: Check EFA on each node with the fi_info command in Prerequisites.

  • The collectives bootstrap cannot find the network interface: Set CCOM_SOCKET_IFNAME to the interface of your nodes.

Clean up#

Stop the training processes with Ctrl-C in each of the four shells. Remove the outputs/qwen3-235b-ep128 directory if you no longer need it, and stop or terminate the instances, since they bill while running.

Next steps#

Further reading#

This document is relevant for: Trn3