Training with Quantized Rollouts
Quantized rollouts are supported with the fsdp and megatron backends.
In this example, we walk through how to train a model with quantized (FP8) rollouts using SkyRL. Quantizing the rollout weights speeds up generation for large models while keeping the trainer in full precision.
Overview
During RL training, rollouts (generation) are typically the throughput bottleneck. Running the inference engine with quantized weights (e.g. FP8) can significantly speed up generation for larger models. SkyRL supports this directly through vLLM — there is no separate patched engine or extra to install.
There are two pieces to make quantized rollouts work well:
- Quantized generation — we ask vLLM to load and serve the policy weights in a quantized format. This is a pass-through option to the vLLM engine, so no SkyRL-specific integration is required.
- Off-policy correction — quantizing the rollout weights widens the gap between the rollout (inference) distribution and the training distribution. We correct for this mismatch with Truncated Importance Sampling (TIS), which applies an importance-sampling correction to the policy loss.
How does it work?
We sample generations from the inference engine with quantized weights. We then compute advantages and the policy loss, applying the TIS correction factor to account for the difference between the rollout and training probability distributions. On each weight update, weights are synced to the inference engine layer by layer in half precision (bfloat16); vLLM then quantizes them to the target format before serving.
Enabling quantized generation
Quantization is enabled by passing it through to the vLLM engine via engine_init_kwargs:
generator.inference_engine.engine_init_kwargs.quantization=fp8This uses vLLM's online dynamic FP8 quantization, so no calibration data or pre-quantized checkpoint is required.
Expert NVFP4: training on the rollouts' grid
On Blackwell, the Megatron trainer can compute its routed MoE experts on the same NVFP4 grid vLLM serves, and sync the NVFP4 expert weights to vLLM as-is:
trainer.policy.megatron_config.expert_nvfp4=true
# what the trainer rounds: w4a16 (default, weights only) or w4a4
trainer.policy.megatron_config.expert_nvfp4_mode=w4a16
# block scales of weights and activations: four_over_six (default) or absmax
trainer.policy.megatron_config.expert_nvfp4_block_scale=four_over_sixvLLM boots as a ModelOpt NVFP4 expert-only engine with per-token NVFP4 activations on the FlashInfer TRT-LLM MoE backend (W4A4) and loads the trainer-quantized expert weights over NCCL or CUDA IPC without re-quantizing them. num_layers_at_{start,end}_in_bf16 keep the first / last layers' experts in BF16 on both sides.
With w4a16, each routed-expert fprop and dgrad GEMM uses the weight rounded to NVFP4 and decoded back to BF16 with the rollouts' own quantizer: FlashInfer's fp4_quantize with vLLM's per-expert global scale and expert_nvfp4_block_scale. four_over_six picks amax/6 or amax/4 for each 16-element block, whichever has the lower squared error; absmax is vLLM's own quantizer. One FlashInfer call quantizes all experts of a grouped GEMM. The rollouts stay W4A4: w4a16 names what the trainer rounds, and it gave the best rollout-trainer logprob match in our runs. Training stays in BF16: wgrad is straight-through and the optimizer updates the unrounded weights. Shared experts and dense layers are not rounded, as in vLLM.
w4a4 also rounds the inputs of both expert GEMMs as the W4A4 rollouts quantize them, with one scale per token and FlashInfer's own per-token quantizer; dgrad passes through the rounding unchanged and wgrad reads the rounded input. The expert fprops run on NVFP4 tensor cores (FlashInfer's CuTe DSL grouped GEMM). It works on any token dispatcher, and needs expert parallelism on the engines (generator.inference_engine.expert_parallel_size) so that vLLM quantizes whole fc2-input rows as the trainer does.
The trainer's own rounding has separate knobs, which leave what the engines serve alone:
# block scales (default: expert_nvfp4_block_scale, the rollouts') and value rounding (default: round-to-nearest)
trainer.policy.megatron_config.expert_nvfp4_weight_rounding.four_over_six=true
trainer.policy.megatron_config.expert_nvfp4_weight_rounding.stochastic_rounding=true
# w4a4 only
trainer.policy.megatron_config.expert_nvfp4_activation_rounding.four_over_six=true
trainer.policy.megatron_config.expert_nvfp4_activation_rounding.stochastic_rounding=trueA block scale that differs from the rollouts' makes the trainer round to another grid than the one it emulates (logged at startup). Stochastic rounding keeps the round-to-nearest grid's block scales (one E4M3 step larger where a block's largest value would saturate) and rounds each value to one of its two E2M1 neighbours, up with probability equal to its distance from the lower one, so it is unbiased. Weights draw once per optimizer step and expert, so every fprop, dgrad and recompute of a step, and every rank holding the expert, round alike; inputs draw afresh in each forward, and activation recompute redraws the same.
The trainer's round trips and packs run on fused CUDA kernels: one kernel reads each BF16 element once and writes its round trip once, with FlashInfer's arithmetic, so the round-to-nearest bytes are FlashInfer's. The kernels are compiled with nvcc on first use (a few seconds, once per node) and cached under SKYRL_QAT_NVFP4_KERNEL_DIR (default $TMPDIR/skyrl_nvfp4_kernels_<uid>; the directory must belong to the user and not be writable by others). If nvcc is missing or the build fails, the trainer logs a warning and uses FlashInfer with a Triton decode instead; SKYRL_QAT_NVFP4_FUSED=0 selects that path. The expert_nvfp4 (...) line in the startup log names the path in use.
The trainer needs whole experts per rank (expert_tensor_parallel_size: 1), moe_grouped_gemm and the NCCL or CUDA-IPC weight sync. LoRA, MTP and fake_int4_qat are not supported. See examples/train/megatron/run_megatron_dapo_qwen3_30b_a3b_nvfp4.sh.
KV cache QAT: attending over the rollouts' KV cache
With a quantized KV cache on DSA models (DeepSeek-V3.2, GLM-5.x on FlashMLA sparse), every key and value the rollouts attend over is a dequantized cache row. The Megatron trainer can attend over the same rows:
generator.inference_engine.engine_init_kwargs.kv_cache_dtype=nvfp4_ds_mla # or fp8_ds_mla
generator.inference_engine.engine_init_kwargs.attention_config.sparse_mla_force_mqa=true
trainer.policy.megatron_config.kv_cache_qat=trueEach absorbed-MLA layer's key, [kv_layernorm(latent) | rope(k_pe)], which also supplies its value, is replaced by the row vLLM writes for that token, decoded back to BF16. One Triton pass computes it with vLLM's own RMSNorm and RoPE code and its cache writer's rounding, and it is bit-identical to vLLM's writer followed by its reader. The format is copied from kv_cache_dtype. sparse_mla_force_mqa is required: without it vLLM runs short prefills on the fresh BF16 keys instead of the cache. The rounding is straight-through: the backward runs on the rounded keys and values, and the gradient reaches the unrounded key. Prefill-decode disaggregation (enable_pd) is not supported. It works with packed sequences, context parallelism and activation recompute, and costs about 0.3 ms per layer forward at 256k tokens on B200.
On a 7-layer slice of GLM-5.3 (3 dense and 4 MoE layers, real weights, routing replay, rollouts with W4A4 experts and nvfp4_ds_mla), it roughly halves the trainer-rollout logprob mismatch at any expert setting. Mean |Δlogp| on sampled tokens (KL in parentheses):
| trainer experts | kv_cache_qat=false | kv_cache_qat=true |
|---|---|---|
| BF16 | 0.062 (3.1e-3) | 0.041 (1.4e-3) |
expert_nvfp4_mode=w4a16 | 0.054 (2.4e-3) | 0.029 (7.6e-4) |
expert_nvfp4_mode=w4a4 | 0.056 (2.5e-3) | 0.028 (6.7e-4) |
For reference, a BF16 trainer against BF16 rollouts with a BF16 cache gives 0.0095 (1.0e-4). The slice has relatively more attention than MoE layers than the full model, which overstates the cache's share.
kv_cache_qat_rounding gives the cache rows the same trainer-side knobs as the experts: four_over_six (nvfp4_ds_mla only; default off, as vLLM's cache writer is absmax) and stochastic_rounding (default off; the E2M1 latent and E4M3 RoPE, or the fp8_ds_mla E4M3 latent, on the round-to-nearest scales). Either makes the trainer's rows differ from the cache the rollouts attend over; the default reproduces it exactly. On Blackwell the hardware's cvt.rs rounds.
Enabling off-policy correction (TIS)
To apply TIS, we need the inference engine to return the rollout logprobs for the generated tokens, and we configure the correction on the policy loss:
# return rollout logprobs for the generated tokens (required for TIS)
generator.sampling_params.logprobs=0 \
# apply sequence-level TIS with an importance-ratio clip
trainer.algorithm.off_policy_correction.tis_ratio_type=sequence \
trainer.algorithm.off_policy_correction.sequence_tis_ratio_clip_high=4.0TIS can be applied at the token or sequence level. See the Off-Policy Correction guide for a full discussion of the available corrections and recommended settings.
Example
We provide a complete example that trains Qwen2.5-Coder-7B-Instruct on the SkyRL-SQL dataset with FP8 rollouts and a full-precision trainer at examples/train/text_to_sql/run_skyrl_sql_fp8.sh.
The key parameters are:
# TIS parameters
TIS_IMP_RATIO_CAP=4.0
TIS_TYPE=sequence
# returns rollout logprobs for the generated tokens; required for TIS
LOGPROBS=0
uv run --isolated --extra fsdp -m skyrl.train.entrypoints.main_base \
trainer.algorithm.off_policy_correction.tis_ratio_type=$TIS_TYPE \
trainer.algorithm.off_policy_correction.sequence_tis_ratio_clip_high=$TIS_IMP_RATIO_CAP \
generator.sampling_params.logprobs=$LOGPROBS \
generator.inference_engine.backend=vllm \
generator.inference_engine.engine_init_kwargs.quantization=fp8 \
...To run it (from the SkyRL root directory):
hf download NovaSky-AI/SkyRL-SQL-653-data-newfmt --local-dir $HOME/data/sql --repo-type dataset
export WANDB_API_KEY=<your_key_here>
bash examples/train/text_to_sql/run_skyrl_sql_fp8.shQuantized rollouts are most beneficial for larger models, where generation dominates step time. For smaller models the overhead of quantizing weights during each weight sync can outweigh the generation speedup. We recommend benchmarking on your own model and hardware.