.. meta::
   :description: Deep dive into the MXFP8 weight all-gather in TorchTitan Neuron. Each rank prequantizes its own weight shard into two MXFP8 encodings, one for the forward matmul and one for the backward, so the FSDP all-gather moves MXFP8 instead of bf16.
   :keywords: MXFP8, weight prequantization, all-gather, SimpleFSDP, TorchTitan, TorchNeuron, Trainium, FSDP, microscaling, dynamic FSDP
   :date-modified: 2026-11-18
   :content-type-template: _content-types/conceptual-deep-dive.rst

.. _torchtitan-neuron-mxfp8-weight-all-gather:

Deep dive: MXFP8 weight all-gather with prequantized weight shards
===================================================================

MXFP8 training normally all-gathers a bf16 weight and then quantizes it inside the GEMM, every
step, on every rank. Weight prequantization reverses that order: each rank quantizes only its own
shard, so the all-gather moves MXFP8 and the quantization work is divided across the data-parallel
group.

.. figure:: images/mxfp8-weight-all-gather/mxfp8-weight-all-gather-reordering.svg
   :alt: Two lanes. Baseline MXFP8 all-gathers bf16 weight shards at 2 bytes per element and then quantizes the whole weight inside the GEMM. Weight prequantization quantizes each local shard first and all-gathers MXFP8 at about 1.03 bytes per element, so the matmul needs no quantize step.
   :width: 100%

   The reordering. Both paths feed the same matmul the same bytes; they differ in whether the
   quantize happens before or after the all-gather, and therefore in what travels on the wire.

Before you start
----------------

You must be familiar with the following:

* **Microscaling (MX) quantization.** MXFP8 stores fp8 data plus one 8-bit scale per 32 elements
  along the matmul contraction axis. See the MX sections of the
  :ref:`Trainium 3 architecture guide <trainium3_arch>` and
  :doc:`MXFP matrix multiplication with NKI </nki/deep-dives/mxfp-matmul>`.
* **FSDP weight all-gather.** Under FSDP each rank holds one slice of a weight and the slices are
  all-gathered into the whole matrix before the matmul, then discarded. See
  :doc:`SimpleFSDP training </training/torchtitan/tutorials/tutorial-simple-fsdp-training>`.

.. note::

   Throughout this topic, **baseline MXFP8** means the non-prequantized path: with
   ``prequantize_weights`` unset, ``MXFP8LinearConverter`` takes its fused path and each linear
   becomes a ``NeuronMXFP8FusedLinear`` whose forward runs the Neuron fused op. That is the path
   the measurements below compare against. Weight prequantization, which swaps in
   ``NeuronMXFP8Linear`` instead, is an opt-in alternative to it, not a replacement.

Performance with and without weight prequantization
---------------------------------------------------

The table below pairs each dense MXFP8 recipe against itself: identical model, parallelism, batch
and activation-checkpointing settings, MXFP8 NKI kernels on both sides, differing only in whether
the weight shards are prequantized before the all-gather.

.. list-table:: Model FLOPs utilization, baseline MXFP8 vs weight prequantization (Trn3)
   :header-rows: 1
   :widths: 32 18 17 17 16

   * - Model
     - Parallelism
     - Baseline MXFP8
     - Prequantized
     - Change
   * - Qwen3-32B, full AC
     - TP4 x FSDP16
     - 14.32%
     - 16.42%
     - +14.7%
   * - Qwen3-14B, full AC
     - TP4 x FSDP16
     - 12.42%
     - 13.90%
     - +11.9%
   * - Llama3-8B, selective AC
     - TP4 x FSDP16
     - 17.62%
     - 19.40%
     - +10.1%
   * - Qwen3-4B, selective AC
     - TP4 x FSDP16
     - 12.35%
     - 12.78%
     - +3.5%
   * - Qwen3-1.7B, full AC
     - TP1 x FSDP64
     - 11.69%
     - 11.64%
     - -0.4%

Across the run's ten paired dense recipes, the median change is **+5.9%** relative MFU, with a
range of -0.6% to +14.7%. Prequantization saves two things. First, each weight gather moves one
byte of fp8 plus one 8-bit scale per 32 elements, so 1 + 1/32 = about 1.03 bytes per element
instead of 2. Second, each rank quantizes only its own shard rather than the whole gathered weight.
Neither saving touches the matmuls, so on Qwen3-1.7B, where the weight gather is a small share of
the step, there is no gain to recover.

How the weight is encoded
-------------------------

MX block scaling groups elements along the contraction axis, and transposing the weight moves that
axis. The forward pass (``fprop``) reads the weight transposed; the pass that computes the gradient
with respect to the input (``dgrad``) reads it as stored::

    fprop     out    = x @ W.T      contracts in_features    -> blocks along "in"
    dgrad     grad_x = grad_out @ W contracts out_features   -> blocks along "out"

One packing cannot serve both, so each weight is encoded twice.

.. figure:: images/mxfp8-weight-all-gather/mxfp8-weight-two-encodings.svg
   :alt: One bf16 weight shard of O rows by I columns feeds two encodings. The fprop encoding contracts in_features and packs to qdata I/4 by O*4 with scale I/32 by O. The dgrad encoding contracts out_features and packs to qdata O/4 by I*4 with scale O/32 by I. Neither is the transpose of the other.
   :width: 100%

   A local shard ``[O, I]`` feeds two encodings, each with its contraction dimension leading.

The ``/4`` and ``*4`` are the hardware 4x interleave along the contraction axis, unrelated to the
MX block size of 32.

.. warning::

   The two encodings are **not transposes of each other**. The tempting optimization is to quantize
   once and transpose, but no transpose converts ``[I/4, O*4]`` into ``[O/4, I*4]``. There are two
   genuine quantize calls per linear per step.

A row's position in the packed buffer depends only on its offset along the contraction axis. So
when every rank's shard is 32-aligned along that axis, the per-rank encodings concatenate into
exactly the encoding of the whole gathered tensor, and the prequantized forward is bit-identical to
baseline MXFP8 rather than an approximation of it.

Under full AC the backward needs the weight for both the recomputed forward and dgrad. The baseline
gathers one bf16 copy, 2 bytes per element, that both read, while prequantization gathers both
encodings at about 1.03 bytes each. The backward weight all-gather therefore gets no data
reduction, and the saving is forward-only.

When the weight is quantized
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Both encodings must exist when their matmul runs. ``prequantize_weights`` enables the path and
selects when they are produced:

.. list-table::
   :header-rows: 1
   :widths: 24 76

   * - Value
     - When each encoding is produced
   * - ``after_optimizer``
     - Once per optimizer step, from the local shard, then stored on the module.
   * - ``before_gather``
     - Immediately before the all-gather that consumes it.

.. figure:: images/mxfp8-weight-all-gather/mxfp8-weight-prequant-schedules.svg
   :alt: after_optimizer quantizes the local shard once per optimizer step and keeps the shard encodings resident across the forward and the backward, freeing only the gathered copy. before_gather quantizes the shard at each use and frees both the encoding and the gathered copy immediately after, repeating the quantize work within a step.
   :width: 100%

   What each option keeps between uses. ``after_optimizer`` frees only the gathered copy;
   ``before_gather`` frees the shard encoding too.

Both produce the same encodings and move the same bytes through the same collectives, so the
collective saving is the same. ``after_optimizer`` keeps both encodings resident for as long as the
model is loaded, ``2 * (1 + 1/32)`` = 2.0625 bytes per weight element per rank. ``before_gather``
frees each encoding with the matmul that read it and rebuilds it on the next use, so under full
activation checkpointing the recomputed forward adds a third quantize call per linear per step.

.. _mxfp8-prequant-dynamic-fsdp:

1D dynamic FSDP: choosing each weight's shard axis
--------------------------------------------------

Each weight is read once transposed and once as stored, so its shard must be a multiple of 32
along both dimensions. Dimension 0 does not always qualify. Qwen3-32B at TP4 x FSDP16, hidden
size 5120, sharded on dimension 0:

.. list-table:: Qwen3-32B weight shards at TP4 x FSDP16, sharded on dimension 0
   :header-rows: 1
   :widths: 10 8 20 18 18 10 10 8 8

   * - Weight
     - TP
     - Original (out, in)
     - After TP4
     - After FSDP16
     - fwd K
     - bwd K
     - fwd %32
     - bwd %32
   * - ``wqkv``
     - col
     - (10240, 5120)
     - (2560, 5120)
     - (160, 5120)
     - 5120
     - 160
     - ok
     - ok
   * - ``wo``
     - row
     - (5120, 8192)
     - (5120, 2048)
     - (320, 2048)
     - 2048
     - 320
     - ok
     - ok
   * - ``w1``
     - col
     - (25600, 5120)
     - (6400, 5120)
     - (400, 5120)
     - 5120
     - 400
     - ok
     - **fails**
   * - ``w3``
     - col
     - (25600, 5120)
     - (6400, 5120)
     - (400, 5120)
     - 5120
     - 400
     - ok
     - **fails**
   * - ``w2``
     - row
     - (5120, 25600)
     - (5120, 6400)
     - (320, 6400)
     - 6400
     - 320
     - ok
     - ok

``fwd K`` is the forward contraction length (the shard's ``in``) and ``bwd K`` the dgrad
contraction length (its ``out``). The MLP weights ``w1`` and ``w3`` fail on the backward axis:
``400 % 32 = 16``. Dimension 0 is unusable for them.

**1D dynamic FSDP** makes the shard axis a per-parameter choice over a single data-parallel axis.
``NeuronMXFP8Linear`` declares

.. code-block:: python

   dp_shard_dim_multiple_of: int = MIN_K   # MIN_K = 32

which opts the module out of dim-0 sharding and lets SimpleFSDP choose the dimension.

A dimension qualifies when it is divisible by the FSDP degree times 32, here 16 x 32 = 512. Among
qualifying dimensions SimpleFSDP prefers the one tensor parallel did not split. ``w1`` and ``w3``
therefore shard on dimension 1, giving a local shard of ``(6400, 320)`` with contraction lengths
320 and 6400. ``wqkv`` moves to dimension 1 for the same reason; ``wo`` and ``w2`` keep dimension 0.

.. note::

   A prequantized run and a non-prequantized run of the same model shard differently, so seeded
   initialization gives them different weights. Compare their loss curves only from a shared
   checkpoint.

Configuration limits
--------------------

* **SimpleFSDP only.** Other data-parallel configurations are future work.
* **Sharded tensors must be 32-aligned in both contraction dimensions.**
  :ref:`1D dynamic FSDP <mxfp8-prequant-dynamic-fsdp>` is what holds this on real shapes. Requiring
  the alignment only on the gathered weight, rather than on every rank's shard, is future work.
* **The NKI lowering only, at 1x32, with DMA transpose.** Any other ``backend_options`` raises. Of
  the NKI options only ``fused_backward`` and ``logical_layout`` apply. The MLIR lowering is not
  supported.
* **Dense attention and feed-forward linears, and the routed experts of Qwen3 and DeepSeek-V3.**
  Linear subclasses that set ``quantizable_matmul`` are rejected. GPT-OSS is not supported: its
  routed experts have no prequantized path and its attention output projection is one of those
  rejected subclasses. Expert-parallel (``neuron_ep``) experts are converted to MXFP8 but not
  prequantized.

Related concepts
----------------

* :doc:`SimpleFSDP training </training/torchtitan/tutorials/tutorial-simple-fsdp-training>` - the
  sharding strategy this feature requires.
* :doc:`MXFP matmul with NKI </nki/deep-dives/mxfp-matmul>` - the kernel-level view of MX
  quantization and the 1x32 packed layout.
* :ref:`MXFP8 training tutorial <torchtitan-neuron-tutorial-mxfp8>` - step-by-step enablement.
* :ref:`MXFP8 converter reference <torchtitan-neuron-tutorial-mxfp8-converter>` - the
  ``prequantize_weights`` field in context.
* :ref:`Common issues <torchtitan-neuron-tutorial-mxfp8-common-issues>` - troubleshooting.
