> ## Documentation Index
> Fetch the complete documentation index at: https://miles.radixark.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Low Precision RL

> Unified low-precision pipelines for RL — block-wise FP8, MXFP8, and NVFP4 across rollout and training.

A common failure mode in MoE RL is precision drift between training and
inference. Pipelines that train in BF16 and serve in FP8 accumulate per-layer
numerical disagreement, which compounds into divergent log-probabilities and
gradients pointing in unintended directions.

Miles supports a unified low-precision path where rollout and training share
the same quantization logic on the forward pass. The same path is wired up for
three formats today — **block-wise FP8**, **MXFP8**, and **NVFP4** — plus the
lower-friction "BF16 train + FP8 inference" mode that's useful when standing
up a new model architecture.

## Choose a precision

| Format | Block layout | Hardware | Models tested | Maturity |
| - | - | - | - | - |
| **BF16** | — | All NVIDIA + AMD MI300X / MI325X / MI350X / MI355X | All | Baseline |
| **FP8 block-wise** (DeepSeek-style) | 128×128, FP32 scales | Hopper (H100 / H200), Blackwell (B200+), AMD MI350X / MI355X | Qwen3-4B, Qwen3-30B-A3B, DeepSeek-V4-Flash | Generally available |
| **MXFP8** | 1×32, UE8M0 scales | Blackwell only (B200, B300, GB200, GB300) | Qwen3-30B-A3B, DeepSeek-V3.2 | Beta |
| **NVFP4** (E2M1) | 1×16, two-level (FP8 + FP32) scales | Blackwell only (B200, B300, GB200, GB300) | Qwen3-30B-A3B | Beta |
| **MXFP4** (E2M1) | 1×32, UE8M0 scales | Blackwell only (B200, B300, GB200, GB300) | DeepSeek-V4-Flash (routed experts, inference only) | Experimental |

MXFP4 is not a standalone choice: rollout uses it only for the routed experts of
releases that ship them in MXFP4 ([mode 5](#5-mxfp4-routed-experts-blackwell)).

## Rollout × training compatibility

Each row is a rollout (inference) precision; each column is the trainer's
forward precision. ✅ = supported; ✗ = not supported.

| Rollout \ Train | BF16 | FP8 block-wise | MXFP8 | NVFP4 |
| - | - | - | - | - |
| **BF16** | ✅ baseline | ✗ | ✗ | ✗ |
| **FP8 block-wise** | ✅ | ✅ Hopper + Blackwell + MI350X / MI355X | ✗ | ✗ |
| **MXFP8** | ✅ | ✗ | ✅ Blackwell | ✗ |
| **NVFP4** | ✗ | ✗ | ✗ | ✅ Blackwell |
| **MXFP4** | ✗ | ✗ | ✅ Blackwell, optional MXFP4 QAT | ✗ |

The reference script (`scripts/run_qwen3_30b_a3b.py`) allows only one rollout
precision and one training precision at a time. Use paired rollout and training
flags for the end-to-end MXFP8 and NVFP4 recipes below. MXFP4 rollout keeps the
weights other than the routed experts in block-wise FP8.

## Unified training recipe

| Stage | Typical pipeline | Miles unified low-precision |
| - | - | - |
| Rollout (forward) | FP8 / MXFP8 / NVFP4 GEMM | Matching low-precision GEMM |
| Trainer (forward) | BF16 GEMM | Matching low-precision GEMM |
| Trainer (backward) | BF16 grads | Recipe-specific precision |
| Optimizer | BF16 master | Higher-precision master weights |

The precision contract covers checkpoint conversion, the trainer forward pass,
SGLang rollout, and live weight export. High-precision tensor exceptions must
match across all four stages.

## Modes

### 1. BF16 train + FP8 inference

The lowest-friction path. SGLang loads FP8 weights while the trainer keeps a
BF16 `torch_dist` checkpoint. There is precision drift between the two paths;
on MoE workloads, pair this with R3 (and optionally TIS).

```bash theme={null}
hf download Qwen/Qwen3-30B-A3B-FP8 --local-dir /root/Qwen3-30B-A3B-FP8

CKPT_ARGS=(
   --hf-checkpoint /root/Qwen3-30B-A3B-FP8        # FP8 weights for SGLang
   --ref-load      /root/Qwen3-30B-A3B_torch_dist  # BF16 torch_dist for trainer
)
```

Reference recipe:
[`examples/infra_features/low_precision/run-qwen3-4b-fp8.sh`](https://github.com/radixark/miles/blob/main/examples/infra_features/low_precision/run-qwen3-4b-fp8.sh)
— single-node Qwen3-4B. It serves an FP8 checkpoint to SGLang and trains from a
BF16 `torch_dist` checkpoint; it sets no `--fp8-recipe`, so the trainer forward
stays BF16.

### 2. Unified block-wise FP8 (DeepSeek-style)

Rollout and training share the same block-wise FP8 quantization. This is the
recipe to use on Hopper, and the layout DeepSeek ships its FP8 checkpoints in.
Block layout is 128×128 with FP32 scales.

```bash theme={null}
--transformer-impl transformer_engine
--bf16
--fp8-format e4m3
--fp8-recipe blockwise

# Optional, for MoE numerical stability
--use-tis
```

| Flag | Effect |
| - | - |
| `--transformer-impl transformer_engine` | Routes Megatron's forward through TransformerEngine so FP8 GEMM is engaged. |
| `--fp8-format e4m3` | Forward FP8 format used by TransformerEngine. |
| `--fp8-recipe blockwise` | 128×128 block-wise quantization; sglang must serve weights in the matching layout. |
| `--use-tis` | Truncated Importance Sampling for residual precision drift. |

`NVTE_FP8_BLOCK_SCALING_FP32_SCALES` is set for you in the actor env
(`miles/ray/train/actor_factory.py`), defaulting by hardware: `1` on Hopper, and
`0` on Blackwell, where TransformerEngine emulates the block-wise recipe with
MXFP8 and needs power-of-two scales. Override it only if you know you want the
non-default for your GPU.

For models that already ship 128×128 block-wise FP8 weights (DeepSeek-V3.2,
`Qwen/Qwen3-30B-A3B-FP8`), point `--hf-checkpoint` at the
block-wise FP8 directory and let SGLang autodetect. Otherwise convert with
`tools/convert_hf_to_fp8.py`.

For MoE workloads, also consider `--use-rollout-routing-replay` (R3). The
canonical recipe leaves it commented out by default but the flag is available.

Reference recipe:
[`examples/infra_features/low_precision/run-qwen3-30b-a3b-fp8-two-nodes.sh`](https://github.com/radixark/miles/blob/main/examples/infra_features/low_precision/run-qwen3-30b-a3b-fp8-two-nodes.sh)
— two-node Qwen3-30B-A3B.

### 3. Unified MXFP8 (Blackwell)

MXFP8 uses one-dimensional microscaling blocks: 32 consecutive E4M3 values
share one UE8M0 scale. The end-to-end recipe uses MXFP8 for rollout, forward
propagation, weight-gradient GEMMs, and data-gradient GEMMs while preserving
configured tensors in BF16.

![End-to-end MXFP8 RL recipe](https://mintcdn.com/radixark/Cy4ImdPUZ2E-qOic/assets/images/low-precision/mxfp8-e2e.png?fit=max\&auto=format\&n=Cy4ImdPUZ2E-qOic\&q=85\&s=d60de262ad9dec1d60d192d2e719ff3a)

**Hardware:** B200, B300, GB200, or GB300.

The Qwen3-30B-A3B launcher prepares the MXFP8 Hugging Face checkpoint and
configures both sides of the RL loop:

```bash theme={null}
python scripts/run_qwen3_30b_a3b.py prepare \
  --hardware B200 \
  --rollout-mxfp8 \
  --train-mxfp8

python scripts/run_qwen3_30b_a3b.py execute \
  --hardware B200 \
  --rollout-mxfp8 \
  --train-mxfp8
```

The training path uses:

```bash theme={null}
--transformer-impl transformer_engine
--bf16
--fp8-format e4m3
--fp8-recipe mxfp8
```

SGLang selects the MoE and dense linear GEMM backends independently. Common
MXFP8 choices are:

| Workload | Miles flag | Backends |
| - | - | - |
| MoE | `--sglang-moe-runner-backend` | `flashinfer_trtllm_routed`, `flashinfer_trtllm`, `cutlass` |
| Dense linear GEMM | `--sglang-fp8-gemm-backend` | `flashinfer_trtllm`, `flashinfer_cutlass`, `triton` |

Choose the pair that matches the model, parallel layout, and installed SGLang
stack. Convert a checkpoint outside the launcher with:

```bash theme={null}
python tools/convert_hf_to_mxfp8.py \
  --model-dir /root/models/Qwen3-30B-A3B \
  --save-dir /root/models/Qwen3-30B-A3B-MXFP8
```

The converter records a 1x32 weight block and UE8M0 scale layout. It excludes
norms, embeddings, routers, and configured high-precision tensors.

Low-precision parameter gather is not enabled in the reference recipe, so a
higher-precision master weight copy remains present during training.

### 4. NVFP4 (Blackwell)

NVFP4 stores E2M1 values in 16-value blocks with an E4M3 scale for each block
and an outer FP32 scale. The Miles reference recipe combines NVFP4 and BF16
through tensor-level precision configuration.

Activation scaling is computed per token. Gate and up projections are
quantized together so the fused rollout GEMM uses the same outer weight scale.
The trainer, checkpoint converter, and rollout kernels must use the same
scaling contract.

**Hardware:** B200, B300, GB200, or GB300.

Run the Qwen3-30B-A3B reference recipe with paired rollout and training flags:

```bash theme={null}
python scripts/run_qwen3_30b_a3b.py prepare \
  --hardware B200 \
  --rollout-nvfp4 \
  --train-nvfp4

python scripts/run_qwen3_30b_a3b.py execute \
  --hardware B200 \
  --rollout-nvfp4 \
  --train-nvfp4
```

The launcher selects per-token activation scaling, disables the incompatible
2D quantization, RHT, and stochastic-rounding paths, and loads the matching
tensor-level precision configuration. NVFP4 applies only to the routed-expert
FC1 and FC2 GEMMs; shared experts and all other unmatched tensors stay in BF16
across checkpoint conversion, training, and live weight export. Rollout uses a
BF16 KV cache.

The training path uses:

```bash theme={null}
--transformer-impl transformer_engine
--bf16
--fp4-format e2m1
--fp4-recipe nvfp4
```

Note the `--fp4-` prefix: NVFP4 has its own format and recipe flags rather than
reusing the `--fp8-` pair. The launcher also adds `--optimizer-cpu-offload --overlap-cpu-optimizer-d2h-h2d --use-precision-aware-optimizer`.

The base NVFP4 recipe uses **high-precision backward**: the forward pass uses
NVFP4 while the BF16 backward GEMMs consume the original BF16 operands.

![NVFP4 with high-precision backward](https://mintcdn.com/radixark/Cy4ImdPUZ2E-qOic/assets/images/low-precision/nvfp4-high-precision-backward.png?fit=max\&auto=format\&n=Cy4ImdPUZ2E-qOic\&q=85\&s=4dee9789d03b2efe660b29d114490cc3)

The base recipe settings used by the launcher are:

```bash theme={null}
NVTE_NVFP4_DISABLE_2D_QUANTIZATION=1
NVTE_NVFP4_DISABLE_RHT=1
NVTE_NVFP4_DISABLE_STOCHASTIC_ROUNDING=1
NVTE_NVFP4_ROW_SCALED_ACTIVATION=1
NVTE_BACKWARD_OVERRIDE=high_precision
NVTE_USE_FAST_MATH=0
SGLANG_FLASHINFER_NVFP4_PER_TOKEN_ACTIVATION=1
TRTLLM_DISABLE_FP4_QUANT_FAST_MATH=1
FLASHINFER_DISABLE_FP4_QUANT_FAST_MATH=1
```

Convert a checkpoint outside the launcher with the same `NVTE_*` recipe
environment:

```bash theme={null}
python tools/convert_hf_to_nvfp4.py \
  --model-dir /root/models/Qwen3-30B-A3B \
  --save-dir /root/models/Qwen3-30B-A3B-NVFP4
```

Checkpoint conversion and live weight export quantize only routed experts in
the main language decoder. MTP weights (including appended decoder layers),
vision/audio components, shared experts, and dense layers keep their source
precision. The HF converter uses the language `num_hidden_layers` from
`text_config` when present, otherwise the top-level config, and recognizes
`model.layers`, `language_model.model.layers`, `model.language_model.layers`,
and `language_model.layers`. First/last-layer BF16 controls apply to those
language layers. Unsupported namespaces remain unquantized. Training-time
precision is still controlled separately by `--te-precision-config-file`.

#### Advanced: dequantized backward

Dequantized backward keeps the backward GEMMs in BF16 but uses BF16
dequantizations of the NVFP4 operands produced during the forward pass.
See the humans& discussion of
[gradient stability](https://humansand.ai/blog/nvfp4-rl#improving-gradient-stability)
for the motivation and ablations.

![NVFP4 with dequantized backward](https://mintcdn.com/radixark/Cy4ImdPUZ2E-qOic/assets/images/low-precision/nvfp4-dequantized-backward.png?fit=max\&auto=format\&n=Cy4ImdPUZ2E-qOic\&q=85\&s=5a03566ad1d59884b672c745faf712b7)

Select this mode in the environment used to launch the job:

```bash theme={null}
export NVTE_BACKWARD_OVERRIDE=dequantized
```

#### Advanced: four-over-six

[Four Over Six: More Accurate NVFP4 Quantization with Adaptive Block Scaling](https://arxiv.org/abs/2512.02010)
optionally chooses, for each NVFP4 block, whether mapping the largest FP4
magnitude to 4 or 6 produces less quantization error.
See the humans&
[four-over-six analysis](https://humansand.ai/blog/nvfp4-rl#four-over-six-for-rl-weights-and-activations)
for the RL recipe discussion.

An example setting is:

```bash theme={null}
export NVTE_NVFP4_4OVER6=all
export FLASHINFER_NVFP4_4OVER6=1
export NVTE_NVFP4_4OVER6_E4M3_USE_256=all
export FLASHINFER_NVFP4_4OVER6_E4M3_USE_256=1
export NVTE_NVFP4_4OVER6_ERR_MODE=MSE
export FLASHINFER_NVFP4_4OVER6_ERR_MODE=MSE
export NVTE_NVFP4_4OVER6_ERR_USE_FAST_MATH=0
export FLASHINFER_NVFP4_4OVER6_ERR_USE_FAST_MATH=0
```

### 5. MXFP4 routed experts (Blackwell)

DeepSeek-V4 and Kimi K3 release their routed experts in MXFP4 — E2M1 values in
32-value blocks with one UE8M0 scale per block — and keep their other weights in
higher precision. Both reach these weights through MXFP4 quantization-aware
training (QAT) in post-training ([DeepSeek-V4](https://arxiv.org/abs/2606.19348),
[Kimi K3](https://arxiv.org/abs/2607.24653)).

Miles implements this for DeepSeek-V4 by changing only the routed experts on top
of FP8 training and rollout. In rollout, the routed experts run MXFP4 weights ×
MXFP8 activations (W4A8), as in the release. Three DeepSeek-V4 configurations
change one thing at a time:

| `--train-precision` | `--rollout-precision` | Routed experts in training | Routed experts in rollout |
| - | - | - | - |
| `mxfp8` | `mxfp8` | MXFP8 | MXFP8 |
| `mxfp8` | `mxfp8-mxfp4-routed-expert` | MXFP8 | MXFP4 × MXFP8 |
| `mxfp8-mxfp4-routed-expert` | `mxfp8-mxfp4-routed-expert` | MXFP4 grid × MXFP8 | MXFP4 × MXFP8 |

The first and last align the routed experts between training and rollout. The
second serves the MXFP4 experts without QAT, so the routed experts carry a
train–rollout mismatch, as in mode 1. In all three, the other layers use the
MXFP8 recipe in training and block-wise FP8 in rollout.

**MXFP4 QAT.** Training has no MXFP4 GEMM. On the first microbatch of each
forward-backward pass, the trainer rounds the routed-expert BF16 weights onto the
MXFP4 grid in place, with the block layout and rounding of the rollout encoder
(round to nearest, ties to even). MXFP4 and MXFP8 share the 1×32 block, so the
MXFP8 GEMMs receive the rounded weights unchanged, which are the weights rollout
serves. The optimizer updates the fp32 master weights and overwrites the BF16
weights after every step, so the weight gradient reaches the master weights
unchanged, as with a straight-through estimator, and no rounded copy is kept.
Master weights and optimizer state keep their precision, and nothing outside the
routed-expert weights is rounded to MXFP4.

![Routed-expert MXFP4 QAT on the MXFP8 recipe](https://mintcdn.com/radixark/EG8O8_hdInS4AAbd/assets/images/low-precision/mxfp4-qat.png?fit=max\&auto=format\&n=EG8O8_hdInS4AAbd\&q=85\&s=e9e13a036489ca9d0274fcda8d86a319)

The MXFP8 recipe also matches the 1×32 activation scaling of the W4A8 rollout
kernel, so both GEMM operands agree with rollout.

**Hardware:** Blackwell. QAT also requires initializing the trainer from the
Hugging Face checkpoint (`--init-model-source hf`).

```bash theme={null}
python scripts/run_deepseek_v4.py full-train \
   --model-name DeepSeek-V4-Flash --hardware GB300 \
   --num-nodes 8 --num-gpus-per-node 4 \
   --init-model-source hf --rollout-weight-source trainer \
   --train-precision mxfp8-mxfp4-routed-expert \
   --rollout-precision mxfp8-mxfp4-routed-expert
```

On top of the MXFP8 training path of mode 3, this passes:

```bash theme={null}
--megatron-to-hf-mode bridge
--routed-expert-qat mxfp4
--rollout-routed-expert-format mxfp4
```

On the rollout side, the launcher sets `SGLANG_DSV4_FP4_EXPERTS=1` and the
`flashinfer_mxfp4` MoE runner. Without `--colocate`, MXFP4 routed experts also
need `--update-weight-transfer-mode broadcast_packed`, which sends their UE8M0
scales as bytes.

`--routed-expert-qat` supports DeepSeek-V4 through the Megatron-Bridge provider.
It requires `--bf16` or `--fp16` with fp32 master weights and a Megatron-LM whose
`TransformerConfig` has the `moe_expert_fake_quant` field. It does not support
Megatron FSDP, the Transformer Engine op fuser, or CUDA graphs.

See [Training and rollout precision](https://miles.radixark.com/docs/models/deepseek/deepseek-v4-flash.md#35-training-and-rollout-precision)
for the option values and their constraints.

## Fine-grained BF16 exceptions

Miles supports per-layer precision configuration across checkpoint
conversion, Megatron training, SGLang rollout, and live weight export.

| Control | Purpose |
| - | - |
| `--num-layers-at-start-in-bf16` | Keep an initial layer range in BF16. |
| `--num-layers-at-end-in-bf16` | Keep a final layer range in BF16. |
| `--first-last-layers-bf16` | Apply the corresponding Megatron training rule. |
| `--extra-high-precision-layers-hf` | Exclude matching Hugging Face tensor names during conversion and rollout export. |
| `--extra-high-precision-layers-megatron` | Exclude matching Megatron tensor names during training and live export. |
| `--te-precision-config-file` | Select Transformer Engine recipes by Megatron tensor name. |

Use equivalent Hugging Face and Megatron name matchers for exceptions beyond a
recipe's default scope. NVFP4 checkpoint conversion and live export exclude
shared experts, MTP, and non-language components automatically. Common
additional exceptions include final transformer layers and MLA projections
whose contraction axis does not match a one-dimensional scaling layout.

## Stochastic rounding of BF16 weights

At RL learning rates, many updates to the FP32 master weights are smaller than
half a BF16 spacing, so rounding the masters to nearest leaves those BF16 weights
unchanged ([arXiv:2611.13545](https://arxiv.org/abs/2611.13545)). Setting
`OPEN_TRAINING_BF16_STOCHASTIC_ROUNDING_FLAG=1` in the trainer environment (for
example `--train-env-vars '{"OPEN_TRAINING_BF16_STOCHASTIC_ROUNDING_FLAG": "1"}'`)
rounds the masters into the BF16 weights stochastically after every optimizer
step; rollout and training both use the rounded weights. It applies to `--bf16`
training with the distributed optimizer, on the GPU or with
`--optimizer-cpu-offload`. Miles refuses `--use-precision-aware-optimizer`
without `--optimizer-cpu-offload` at startup, since TE FusedAdam then keeps the
master weights and casts them itself.

The random bits are Transformer Engine's NVFP4 stochastic-rounding generator
(Philox4x32-10) and depend only on the seed, the optimizer step, and the
parameter shard, so `--rematerialize-param-from-master-weight` and resumed runs
rebuild the same weights. See
[`miles/kernels/quant/stochastic_round.py`](https://github.com/radixark/miles/blob/main/miles/kernels/quant/stochastic_round.py)
and
[`miles/utils/bf16_stochastic_rounding.py`](https://github.com/radixark/miles/blob/main/miles/utils/bf16_stochastic_rounding.py).

## Hardware support

| GPU | BF16 | FP8 block-wise | MXFP8 | NVFP4 |
| - | - | - | - | - |
| NVIDIA H100 / H200 | ✅ | ✅ | ✗ | ✗ |
| NVIDIA B200 / B300 / GB200 / GB300 | ✅ | ✅ | ✅ | ✅ |
| NVIDIA A100 | ✅ | ✗ | ✗ | ✗ |
| AMD MI350X / MI355X | ✅ | ✅ | ✗ | ✗ |
| AMD MI300X / MI325X | ✅ | ✗ | ✗ | ✗ |

## When BF16 is enough

* Dense models below \~30 B.
* A100 hardware (no FP8 GEMM).
* AMD MI300X / MI325X (no FP8 block-wise path).
* Bring-up of a new model architecture, where clean BF16 numerics simplify
  debugging.

## Further reading

* [Blackwell MXFP8 and NVFP4 RL roadmap](https://github.com/radixark/miles/issues/615)
* [Towards Blackwell-Native 8-bit and 4-bit RL: End-to-End MXFP8 and NVFP4 RL in Miles](https://www.lmsys.org/blog/2026-07-29-mxfp8-nvfp4-rl)
* [The 4-bitter Lesson](https://humansand.ai/blog/nvfp4-rl)
* [Faster-than-light NVFP4 decode—with precision](https://humansand.ai/blog/faster-than-light-nvfp4-decode)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.