This document is relevant for: Trn3

Tutorial: Train Qwen3 in MXFP8 on Neuron#

This tutorial walks you through MXFP8 training on AWS Trainium3 using TorchTitan with TorchNeuron and TorchAO Neuron, for a dense model (Qwen3-32B) and a mixture-of-experts (MoE) model (Qwen3-30B-A3B). When you have completed it, you will have trained both models in MXFP8 and know how to pick MXFP8 settings for your own model.

Overview#

Tutorial: Train Llama and DeepSeek with SimpleFSDP on Neuron runs SimpleFSDP in bf16. This tutorial keeps the same launch pattern and changes the precision of the matrix multiplies.

MXFP8 is a block-scaled 8-bit floating-point format: every block of 32 elements shares one scale. TorchAO Neuron provides a fused MXFP8 linear for Trainium3 that quantizes its inputs and runs the GEMM in one NKI kernel. TorchTitan Neuron swaps Linear modules for the MXFP8 module through a model converter, so the model code does not change. Attention and FFN linears are converted by default. For MoE models, a second converter moves the experts onto the NKI Library MXFP8 MoE kernels.

For dense linears there are two MXFP8 paths:

  • Default (torch.compile and FSDP2). Each rank all-gathers a bf16 weight and quantizes it inside the GEMM.

  • Prequantized weights (SimpleFSDP). Each rank quantizes only its own weight shard, and the FSDP all-gathers collect fp8 data and scale tensors instead of the bf16 data tensor. This cuts weight all-gather traffic and the redundant quantization work, which improves throughput.

Run these in order:

  1. Train the Qwen3-32B bf16 baseline.

  2. Train Qwen3-32B with MXFP8 quantization.

  3. Train with MXFP8 prequantized weights.

  4. Maximize throughput for Qwen3-32B.

  5. Train Qwen3-30B-A3B MoE with MXFP8 quantization.

  6. Train Qwen3-30B-A3B MoE with MXFP8 prequantized weights.

Kernel tuning is covered in the auto-tuning tutorial; see Next steps.

Before you start#

Make sure you have:

Prerequisites#


Prepare your environment#

Work from the torchtitan-neuron directory you installed in How to set up TorchTitan Neuron.

Step 1: Train the Qwen3-32B bf16 baseline#

Train a BF16 baseline for Qwen3-32B.

NGPU=64 \
MODULE=torchtitan_neuron.models.qwen3 \
CONFIG=qwen3_32b_tp4_fsdp16_fullac_compile \
./run_train.sh --training.steps 10 --dataloader.dataset c4_test \
  --hf_assets_path=./tests/assets/tokenizer

Step 2: Train Qwen3-32B with MXFP8 quantization#

Train Qwen3-32B with MXFP8 quantization.

NGPU=64 \
MODULE=torchtitan_neuron.models.qwen3 \
CONFIG=qwen3_32b_tp4_fsdp16_fullac_compile_mxfp8 \
./run_train.sh --training.steps 10 --dataloader.dataset c4_test \
  --hf_assets_path=./tests/assets/tokenizer

Step 3: Train with MXFP8 prequantized weights#

Train Qwen3-32B with weight quantization out of the GEMM: each rank quantizes its own shard before the FSDP all-gather. For details, see Deep dive: MXFP8 weight all-gather with prequantized weight shards.

NGPU=64 \
MODULE=torchtitan_neuron.experiments.graph_trainer.qwen3 \
CONFIG=simple_fsdp_qwen3_32b_tp4_fsdp16_fullac_mxfp8_wprequant \
./run_train.sh --training.steps 10 --dataloader.dataset c4_test \
  --hf_assets_path=./tests/assets/tokenizer

Step 4: Maximize throughput for Qwen3-32B#

For the highest performance, run the selective-AC recipe, which also raises the local batch size to 2:

NGPU=64 \
MODULE=torchtitan_neuron.experiments.graph_trainer.qwen3 \
CONFIG=simple_fsdp_qwen3_32b_tp4_fsdp16_sac_mxfp8_wprequant \
./run_train.sh --training.steps 10 --dataloader.dataset c4_test \
  --hf_assets_path=./tests/assets/tokenizer

Step 5: Train Qwen3-30B MoE with MXFP8 quantization#

Train Qwen3-30B-A3B in MXFP8 with tensor parallelism and FSDP:

NGPU=64 \
MODULE=torchtitan_neuron.models.qwen3 \
CONFIG=qwen3_30b_a3b_nki_fullac_compile_mxfp8 \
./run_train.sh --training.steps 10 --dataloader.dataset c4_test \
  --hf_assets_path=./tests/assets/tokenizer

Step 6: Train Qwen3-30B MoE with MXFP8 prequantized weights#

Apply weight prequantization for Qwen3-30B-A3B:

NGPU=64 \
MODULE=torchtitan_neuron.experiments.graph_trainer.qwen3 \
CONFIG=qwen3_30b_a3b_nki_fullac_simplefsdp_mxfp8_wprequant \
./run_train.sh --training.steps 10 --dataloader.dataset c4_test \
  --hf_assets_path=./tests/assets/tokenizer

All complete!

Important

TorchTitan Neuron logs MFU against the peak fp8 FLOPs of Trainium3, which is 4x the bf16 peak, so an MXFP8 run reports a much lower MFU than a bf16 run even when its step time is shorter.

Congratulations! You have now trained a dense model (Qwen3-32B) and a MoE model (Qwen3-30B-A3B) in MXFP8 on AWS Trainium3. To adapt the recipes to your own model, see General tips. If you encountered any issues, see Common issues.


General tips#

Choosing default settings#

For best performance, start from the prequantized SimpleFSDP recipe (Step 3). If your weight shapes do not support it (see the next subsection), use the default FSDP2 recipe with torch.compile (Step 2), which has no shape restriction beyond the kernel’s own.

Verifying dimensions for prequantization#

The prequantized path quantizes each rank’s weight shard, so a shard must hold whole 32-element blocks. For how the shard dimension is chosen and how to check your model’s weights, see Deep dive: MXFP8 weight all-gather with prequantized weight shards.

Selecting layers for quantization with fqns#

The NeuronTorchTitan recipes define these shared fqns lists in torchtitan_neuron/models/common/recipes.py:

# Dense models: every in-layer linear (attention and FFN).
MXFP8_LAYER_LINEAR_FQNS = ["layers"]

# MoE models: the attention linears only.
MXFP8_ATTENTION_FQNS = ["attention"]

# DeepSeek-V3 at TP4: attention wq, wkv_b, wo and the shared experts.
MXFP8_DEEPSEEK_V3_TP4_FQNS = [
    "attention.wq",
    "attention.wkv_b",
    "attention.wo",
    "shared_experts",
]

Common issues#

  • “prequantize_weights supports only NKI / 1x32 / DMA transpose”: your backend_options asked for wrapX, the MLIR lowering, or disable_dma_transpose together with prequantization. Drop that option or use the default recipe without prequantization.

  • “Neuron MXFP8 is only supported on trn3 or later”: you are not using a Trainium3 instance.

  • A ValueError about an encoding that “contracts over” a dimension that “must be a multiple of 32”: the shard check failed. See Verifying dimensions for prequantization.

  • MFU looks four times lower than expected: MFU is logged against the fp8 peak. See Confirmation.

  • Out of memory: the bf16 remedies apply unchanged; see the SimpleFSDP tutorial’s Common issues.


Reference: MXFP8 recipe configuration#

What each recipe in Steps 2 to 6 configures.

MXFP8 linear converter#

Every MXFP8 recipe configures MXFP8 through one MXFP8LinearConverter.Config entry. The Step 2 recipe builds it in with_mxfp8 (torchtitan_neuron/models/common/recipes.py):

MXFP8LinearConverter.Config(
    fqns=fqns,  # MXFP8_LAYER_LINEAR_FQNS = ["layers"]
    model_compile_enabled=config.compile.enable
    and "model" in config.compile.components,
    backend_options=linear_backend_options("nki"),  # wrapX layout
)

To change a recipe, edit these arguments in its config_registry.py or build your own recipe with with_mxfp8:

  • fqns: substring filters on module names. A Linear is converted when any entry appears in its fully qualified name; an empty list converts every Linear. Use it to keep sensitive layers in bf16 or to skip shapes the kernel does not support. See Selecting layers for quantization with fqns.

  • prequantize_weights: False (default) quantizes inside the GEMM. True or "before_gather" quantizes each rank’s shard before the forward all-gather, so the gather moves fp8; "after_optimizer" quantizes after the optimizer step and keeps the encodings resident. Both need SimpleFSDP and shard-compatible weight shapes (see Verifying dimensions for prequantization).

  • backend_options: a NeuronMXFP8BackendOptions that selects and tunes the kernel; None uses the TorchAO Neuron defaults. See What is TorchAO Neuron?.

On the prequantized path only nki.fused_backward and nki.logical_layout apply; any other backend_options value raises at startup.

The Step 3 recipe turns prequantization on and opts in to the fused backward:

MXFP8LinearConverter.Config(
    fqns=["layers"],
    model_compile_enabled=True,
    prequantize_weights=True,
    backend_options=_prequant_backend_options(),
)

Here _prequant_backend_options() returns NeuronMXFP8BackendOptions(nki=NkiKernelOptions(fused_backward=True)). fused_backward=True runs the two backward matmuls (dX and dW) as one fused kernel call.

MoE experts converter#

The Step 5 recipe builds on the bf16 qwen3_30b_a3b_nki_fullac_compile recipe. moe_experts=True appends the experts converter to the linear converter, and moe_tuning resolves to a tuning file that ships with TorchTitan Neuron:

with_mxfp8(
    qwen3_30b_a3b_nki_fullac_compile(),
    MXFP8_ATTENTION_FQNS,  # ["attention"]
    moe_experts=True,
    moe_tuning="qwen3_30b_a3b_tp4_b512_1x32",
)

# expands to
NeuronMXFP8GroupedExpertsConverter.Config(
    quant_scheme="1x32",
    tuning_path=mxfp8_moe_tuning("qwen3_30b_a3b_tp4_b512_1x32"),
)
  • quant_scheme: the MXFP8 layout of every GEMM operand in the expert kernels, "1x32" (recipe default) or "wrapX".

  • tuning_path: a JSON file of tuned expert kernel configurations. Recipes set it through moe_tuning; None uses the kernel defaults. To produce one, see Tune a MoE model in Tutorial: Auto-tune MXFP8 kernel configs for TorchTitan Neuron training.

  • prequantize_weights: as for the linear converter, and tensor-parallel experts only. The Step 6 recipe calls apply_moe_mxfp8(config, MXFP8_ATTENTION_FQNS, prequantize_weights=True, moe_tuning="..."), which sets it on both converters and selects the matching tuning file. Pass prequantize_experts=False to prequantize only the linears; expert-parallel (neuron_ep) experts have no prequantized path and need this setting.

The converter replaces the bf16 NKI experts with their MXFP8 counterparts and raises on any other expert type. aten::scaled_grouped_mm is not yet supported on Neuron.

Expert parallelism#

The 30B-A3B recipes use tensor-parallel experts. For expert parallelism, the Qwen3-235B recipes in torchtitan_neuron.models.qwen3 apply the same two converters to neuron_ep experts: qwen3_235b_94layers_ep128_tp4_fsdp64_pp1_4k_neuron_ep_compile_mxfp8, and 6-layer variants such as qwen3_235b_6layers_ep64_nle1_tp4_neuron_ep_compile_mxfp8 for debugging.

Clean up#

Stop the training processes (Ctrl-C on the torchrun launcher). Remove the output TorchTitan wrote under ./outputs if you no longer need it, and stop or terminate the instance, since it bills while running. Next steps ———-

Further reading#

This document is relevant for: Trn3