.. meta::
   :description: MXFP8 training on TorchTitan Neuron. Train dense Qwen3-32B with TorchAO Neuron's MXFP8 linear on Trainium3, first with torch.compile and FSDP2, then with SimpleFSDP and prequantized weights, then the Qwen3-30B-A3B MoE model in MXFP8 with and without prequantized weights.
   :keywords: TorchTitan, TorchNeuron, TorchAO, torchao-neuron, MXFP8, low-precision training, weight prequantization, SimpleFSDP, FSDP2, MoE, Qwen3, AWS Neuron, Trainium3, tutorial
   :date-modified: 2026-11-21
   :content-type-template: _content-types/procedural-tutorial.rst

.. _torchtitan-neuron-tutorial-mxfp8:


=========================================================================
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
--------

:doc:`/training/torchtitan/tutorials/tutorial-simple-fsdp-training` 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:

- Completed :doc:`/training/torchtitan/tutorials/tutorial-simple-fsdp-training`,
  or read its overview. This tutorial reuses the ``run_train.sh`` launch pattern
  and the JIT compile mode it explains.

Prerequisites
-------------

- A Trn3 instance.
- Neuron SDK 2.34+
- PyTorch 2.14
- TorchTitan Neuron Setup: 
  :doc:`/training/torchtitan/tutorials/tutorial-basic-training`
- TorchAO Neuron Setup: 
  :doc:`/training/torchao-neuron/get-started/setup-torchao-neuron`

----

Prepare your environment
------------------------

Work from the ``torchtitan-neuron`` directory you installed in
:doc:`/training/torchtitan/get-started/setup-torchtitan-neuron`.



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

Train a BF16 baseline for Qwen3-32B.

.. code-block:: bash

   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.

.. code-block:: bash

   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
:doc:`/training/torchtitan/concepts/mxfp8-weight-all-gather`.

.. code-block:: bash

   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:

.. code-block:: bash

   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:

.. code-block:: bash

   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:

.. code-block:: bash

   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
:ref:`torchtitan-neuron-tutorial-mxfp8-common-issues`.

----

General tips
------------

.. _torchtitan-neuron-tutorial-mxfp8-default:

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.

.. _torchtitan-neuron-tutorial-mxfp8-verify-dims:

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
:doc:`/training/torchtitan/concepts/mxfp8-weight-all-gather`.

.. _torchtitan-neuron-tutorial-mxfp8-fqns:

Selecting layers for quantization with ``fqns``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

.. code-block:: python

   # 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",
   ]


.. _torchtitan-neuron-tutorial-mxfp8-common-issues:

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
  :ref:`torchtitan-neuron-tutorial-mxfp8-verify-dims`.
- **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.

----

.. _torchtitan-neuron-tutorial-mxfp8-reference:

Reference: MXFP8 recipe configuration
-------------------------------------

What each recipe in Steps 2 to 6 configures.

.. _torchtitan-neuron-tutorial-mxfp8-converter:

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``):

.. code-block:: python

   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 :ref:`torchtitan-neuron-tutorial-mxfp8-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
  :ref:`torchtitan-neuron-tutorial-mxfp8-verify-dims`).
- ``backend_options``: a ``NeuronMXFP8BackendOptions`` that selects and tunes
  the kernel; ``None`` uses the TorchAO Neuron defaults. See
  :doc:`/training/torchao-neuron/about-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:

.. code-block:: python

   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:

.. code-block:: python

   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
  :doc:`/training/torchtitan/tutorials/tutorial-mxfp8-autotuning`.
- ``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
----------

- :doc:`/training/torchtitan/tutorials/tutorial-mxfp8-autotuning` to tune the
  MXFP8 matmul and MoE kernels for your recipe. For the dense recipes in Steps
  2 to 4, follow its *Tune a dense model* section; for the MoE recipes in Steps
  5 and 6, follow *Tune a MoE model*.
- :doc:`/training/torchtitan/best-practices` for tuning beyond the defaults.

Further reading
---------------

- :doc:`/training/torchtitan/tutorials/tutorial-simple-fsdp-training`
- :doc:`/training/torchtitan/concepts/mxfp8-weight-all-gather` for how
  prequantized weight shards change the FSDP all-gather.
- :doc:`/training/torchtitan/about-torchtitan-neuron`
- :doc:`/training/torchao-neuron/about-torchao-neuron`
- :doc:`/torch/guides/torch-distributed` for how SimpleFSDP differs from FSDP on
  Neuron.