Skip to content

Commit ec46ab3

Browse files
authored
feat(modelopt): support real NVFP4 QAT rollout for MoE and Mamba (#2983)
Signed-off-by: Hollow Man <hollowman@opensuse.org>
1 parent bbc5808 commit ec46ab3

47 files changed

Lines changed: 5092 additions & 1243 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Submodule Megatron-Bridge updated 1314 files
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# ModelOpt Real-Quant Refit Architecture
2+
3+
NeMo RL supports deployment-style NVFP4 rollout generation while a Megatron
4+
policy is trained with ModelOpt quantization-aware training. During each
5+
policy refit, Megatron-Bridge exports packed quantized tensors and NeMo RL
6+
loads them into a vLLM generation model without rebuilding the model or its
7+
CUDA graphs.
8+
9+
## Relationship to the overall NeMo RL design
10+
11+
Real-quant refit extends the standard NeMo RL policy-generation workflow. The
12+
algorithm controller continues to coordinate independent policy and generation
13+
workers through the existing interfaces. ModelOpt changes the representation
14+
used by the policy, while this design changes only how a policy update is
15+
exported and installed in the vLLM generation worker.
16+
17+
This design builds on:
18+
19+
- [Design and Philosophy](design-and-philosophy.md), which defines NeMo RL's
20+
controller, worker, isolation, and communication model;
21+
- [Generation Interface](generation.md), which defines generation backends and
22+
their weight-update lifecycle; and
23+
- [Quantization-Aware RL](../guides/quantization-aware-rl.md), which documents
24+
the user workflow, configuration, and supported recipes.
25+
26+
The real-quant path preserves those abstractions: algorithms still call the
27+
same policy and generation interfaces, and non-quantized and fake-quantized
28+
weight updates continue to use their existing paths.
29+
30+
## Component responsibilities
31+
32+
| Component | Responsibility |
33+
|---|---|
34+
| ModelOpt | Quantization configuration, calibration, QAT state, NVFP4 packing, and scale derivation |
35+
| Megatron-Bridge | Megatron-to-Hugging-Face conversion, distributed TP/PP/EP handling, and NVFP4 export |
36+
| NeMo RL | Mode validation, refit scheduling, named-tensor transport, and vLLM reload orchestration |
37+
| vLLM | Checkpoint loading, runtime-layout conversion, kernel selection, stable tensor placement, and KV-cache scale processing |
38+
39+
NeMo RL delegates quantization math and runtime-kernel conversion to ModelOpt,
40+
Megatron-Bridge, and vLLM. Its vLLM integration is limited to the
41+
format-specific adapters required to connect their public interfaces.
42+
43+
```mermaid
44+
flowchart LR
45+
A[RL algorithm controller] --> B[ModelOpt QAT policy worker]
46+
B --> C[Megatron-Bridge NVFP4 export]
47+
C --> D[NeMo RL named-tensor transport]
48+
D --> E[vLLM generation worker]
49+
E --> F[Rollouts]
50+
F --> A
51+
```
52+
53+
## Quantization modes
54+
55+
Both modes use block-16 E2M1 NVFP4 weights with E4M3 block scales.
56+
57+
| Mode | Deployment algorithm | Weights | Activations |
58+
|---|---|---|---|
59+
| W4A4 | `NVFP4` | NVFP4 | NVFP4 with per-projection input scales |
60+
| W4A16 | `W4A16_NVFP4` | NVFP4 | Native model dtype |
61+
62+
The policy and generation workers must use quantization recipes that resolve
63+
to the same mode. Unsupported or mismatched formats fail during setup.
64+
65+
Example routed-expert recipes are provided at:
66+
67+
- [`examples/modelopt/quant_configs/nvfp4_experts.yaml`](../../examples/modelopt/quant_configs/nvfp4_experts.yaml) for W4A4;
68+
- [`examples/modelopt/quant_configs/nvfp4_experts_weightonly.yaml`](../../examples/modelopt/quant_configs/nvfp4_experts_weightonly.yaml) for W4A16.
69+
70+
## Refit lifecycle
71+
72+
Every real-quant refit follows the same lifecycle:
73+
74+
1. NeMo RL validates the policy and generation quantization modes.
75+
2. Megatron-Bridge exports named NVFP4 weights and scale tensors.
76+
3. NeMo RL starts vLLM's layerwise reload lifecycle for the affected
77+
quantized modules.
78+
4. The normal vLLM model loader consumes the exported tensors.
79+
5. vLLM performs post-load conversion and copies the converted tensors into
80+
the existing runtime storage.
81+
6. NeMo RL finalizes the reload and synchronizes the device before the
82+
transport buffer can be reused.
83+
84+
This lifecycle is shared by collective and CUDA IPC transports. vLLM retains
85+
ownership of runtime tensor layouts and preserves tensor addresses referenced
86+
by CUDA graphs.
87+
88+
## vLLM compatibility adapters
89+
90+
NeMo RL registers ModelOpt NVFP4 extensions through vLLM's quantization
91+
registry. The adapters cover:
92+
93+
- W4A16 dense and fused-MoE execution;
94+
- fused-MoE input-scale loading for W4A4;
95+
- rank-local padding required by W4A16 MoE kernels; and
96+
- preservation of runtime kernel references during repeated refits.
97+
98+
Native vLLM processing remains responsible for checkpoint-layout restoration,
99+
post-load conversion, kernel construction, and stable-storage copy-back. The
100+
adapters do not define a separate reload implementation.
101+
102+
## Fused-MoE transport
103+
104+
Megatron-Bridge exports fused expert projections as expert-batched W13 and W2
105+
tensors. Each family contains the packed weight, block scale, and global scale.
106+
W4A4 additionally includes one input scale for each projection.
107+
108+
The receiving adapter validates that each tensor family is complete and maps
109+
it to the checkpoint names accepted by the vLLM model loader. Gated experts
110+
use two W13 shards; non-gated experts use one.
111+
112+
Fused-MoE refit currently requires every vLLM rank to own the full expert set.
113+
Megatron expert parallelism remains supported because Megatron-Bridge gathers
114+
the exported payload before the vLLM refit.
115+
116+
## CUDA IPC buffer lifetime
117+
118+
Layerwise loading may temporarily retain a view of an incoming tensor until a
119+
layer has received its complete payload. NeMo RL ensures that no retained view
120+
aliases a reusable CUDA IPC staging buffer before acknowledging that buffer.
121+
The final acknowledgment is sent only after vLLM finalization and device
122+
synchronization.
123+
124+
## KV-cache behavior
125+
126+
W4A4 and W4A16 weight refit do not enable KV-cache quantization. KV-cache
127+
precision remains controlled by vLLM's `kv_cache_dtype` configuration.
128+
129+
When FP8 KV cache is selected, vLLM owns its scale creation, loading, and
130+
post-load processing. The ModelOpt real-quant adapter does not replace the
131+
vLLM KV-cache processing method or process an attention layer a second time.
132+
133+
## Configuration
134+
135+
Set the same quantization recipe for the policy and generation worker, and
136+
enable real-quant generation:
137+
138+
```yaml
139+
policy:
140+
quant_cfg: examples/modelopt/quant_configs/nvfp4_experts.yaml
141+
142+
generation:
143+
quant_cfg: examples/modelopt/quant_configs/nvfp4_experts.yaml
144+
real_quant: true
145+
```
146+
147+
Layer selection belongs in a purpose-specific quantization recipe. Existing
148+
shared configs should not be changed to carry experiment-specific exclusions.
149+
Accuracy-driven exclusions should be supported by reproducible sensitivity or
150+
AutoQuant results; exclusions required by an unsupported tensor or operator
151+
contract should be documented in the corresponding recipe.
152+
153+
## Current limitations
154+
155+
- Real-quant rollout generation requires vLLM.
156+
- Policy export currently uses the Megatron policy path and Megatron-Bridge.
157+
- Supported real-quant formats are dynamic block-16 NVFP4 W4A4 and W4A16.
158+
- Fused-MoE vLLM expert parallelism is not supported during refit.
159+
- Model support is recipe-specific and requires end-to-end validation.

0 commit comments

Comments
 (0)