This document is relevant for: Trn3

How to use EP to train a model with TorchTitan on Neuron#

Task overview#

This topic shows how to use expert parallelism (EP) to train a mixture-of-experts (MoE) model with TorchTitan on AWS Trainium3 (Trn3) using the AWS Neuron SDK. It covers the EP degree, how EP combines with FSDP and tensor parallelism, and how to confirm that the experts are sharded as configured.

An MoE layer holds many expert feed-forward networks, and each token uses only a few of them. Expert parallelism splits the experts of every MoE layer across a group of ranks, the EP group. Each rank keeps only its own experts. Before the expert computation, each rank sends every token to the ranks that own the experts the token was routed to, using an all-to-all collective. After the computation, the results return to the token’s rank the same way. EP lowers the expert memory on each rank and spreads the expert compute across the group.

For a complete run that uses the settings on this page, see Tutorial: Train a model with expert parallelism on Neuron.

Prerequisites#

  • TorchTitan set up on each node: Install it as described in How to set up TorchTitan Neuron.

  • Trn3 instances with LNC 2: The Neuron EP backend (neuron_ep) runs on Trn3 with the logical NeuronCore setting 2, the default. It does not run on Trn2. EP groups are built from complete groups of four ranks on Trn3 devices.

  • An MoE recipe that uses the Neuron EP backend: The Neuron EP recipes are the Qwen3-235B-A22B recipes. GPT-OSS does not use EP on Neuron. This page uses the Qwen3-235B-A22B recipe qwen3_235b_94layers_ep128_tp4_fsdp64_pp1_4k_neuron_ep_compile in torchtitan_neuron.models.qwen3.config_registry.

Instructions#

1: Select the Neuron EP backend in the recipe

A Neuron EP recipe makes two choices. moe_comm_backend="neuron_ep" selects the token dispatcher that moves the tokens between the EP ranks with Neuron all-to-all collectives. The recipe then replaces each MoE layer’s experts with BWMMGroupedExperts, which computes the experts with Neuron kernels. The recipe makes both choices; you do not pass them on the command line. In torchtitan_neuron/models/qwen3/config_registry.py, the Qwen3-235B recipes build the model like this, with moe_comm_backend="neuron_ep":

model_spec = model_registry(
    "235B-A22B",
    attn_backend="sdpa",
    moe_comm_backend=moe_comm_backend,
)
# ...
if moe_comm_backend == "neuron_ep":
    for layer in model_spec.model.layers:
        layer.moe.experts = derive(
            layer.moe.experts,
            BWMMGroupedExperts.Config,
        )

2: Set the EP degree

The EP degree is parallelism.expert_parallel_degree. The recipe sets it in its ParallelismConfig, and you can override it on the command line with --parallelism.expert_parallel_degree. Each rank holds num_experts / EP experts of each MoE layer, the local experts.

For example, the Qwen3-235B-A22B recipe has 128 experts per MoE layer and sets EP to 128, so each rank holds one local expert of each layer. A lower EP degree puts more experts on each rank and raises the memory on each rank. The recipe is validated at EP 128.

With EP 1, there is no expert parallelism, and each rank holds all experts. For EP greater than 1, the Neuron EP backend checks the EP degree when the model starts:

  • num_experts must be divisible by the EP degree.

  • The EP degree must be at least 8 and a multiple of 4, so that each EP group is made of complete groups of four ranks on Trn3 devices.

3: Combine EP with FSDP and tensor parallelism

EP does not add a factor to the number of ranks. The product of the other degrees must still equal the number of ranks:

dp_replicate × dp_shard × cp × tp × pp = number of ranks

EP reuses the ranks of dp_shard × cp × tp. Choose an EP degree that divides this product. The parameters are then sharded as follows:

  • Dense parameters (attention, embeddings, norms) use FSDP over dp_shard and tensor parallelism over tp.

  • Expert parameters are split across the EP group, and FSDP shards each expert’s weights over the remaining ranks. TorchTitan calls this axis efsdp, and its size is dp_shard × cp × tp / EP.

The Qwen3-235B-A22B recipe runs 256 ranks on four Trn3 nodes with dp_shard=64 and tp=4 (64 × 4 = 256). With EP 128, efsdp is 256 / 128 = 2: the 128 experts are spread over 128 positions, and FSDP splits each expert’s weights across two ranks.

On a Trn3 Gen1 UltraServer, the node-rank assignment keeps each EP group inside one rack. The four servers sit in two racks; see Amazon EC2 Trn3 Architecture. With 64 ranks per node, an EP group of 128 ranks spans two nodes. The UltraServer instance indices 0 and 1 share one rack, and 2 and 3 share the other. Set each node’s torchrun node rank to its ultraserver-instance-index from neuron-ls --topology, so that the EP groups are nodes 0 + 1 and 2 + 3. Tutorial: Train a model with expert parallelism on Neuron shows these commands.

Confirm your work#

To confirm that EP is active and the experts are sharded as configured, check the startup output, which every rank prints. For the Qwen3-235B-A22B recipe, it shows:

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']
  • ep=128 is the EP degree.

  • ep and efsdp in the active dimensions mean that the experts are split across the EP group and their weights are sharded with FSDP. Without EP, neither dimension is present.

Then confirm that training runs: the per-step loss lines appear, and the loss values are finite.

Common issues#

Did you encounter an error while working through this task? Here are some commonly encountered issues and how to address them.

neuron_ep requires num_experts (...) divisible by ep_size (...)

  • Possible solution: Choose an EP degree that divides the number of experts per MoE layer.

packed A2AV requires EP >= 8 in complete four-rank Trn3 device groups

  • Possible solution: Use an EP degree of at least 8 that is a multiple of 4, and run on Trn3.

packed A2AV requires NEURON_LOGICAL_NC_CONFIG=2

  • Possible solution: Run with the logical NeuronCore setting 2, the default. Unset NEURON_LOGICAL_NC_CONFIG, or set it to 2.

Invalid parallel dims: ... != WORLD_SIZE(...)

  • Possible solution: The product dp_replicate × dp_shard × cp × tp × pp must equal the number of ranks. EP is not part of this product. Change dp_shard or tp, not EP.

Config function '...' not found

  • Possible solution: Select the recipe module by its fully qualified path, such as torchtitan_neuron.models.qwen3.config_registry, not the short name qwen3. The short name selects the upstream registry, which has no Neuron EP recipes.