docs/advance/deepseek_v4_integration.rst
Last updated: 07/12/2026.
This guide describes the DeepSeek-V4-Flash integration for a Megatron actor and a vLLM rollout model. Most of the model-specific work is at two boundaries:
Weight synchronization is required for online training. Router replay is a separate feature and is required only when R2 or R3 is enabled. DeepSeek V4 also needs a few independent compatibility fixes for model construction and fused kernels.
DeepSeek-V4-Flash does not use a single FP8 representation for all weights:
w13, w2, and
scale tensors into an MXFP4 or MegaMoE layout. This conversion can replace
or remove the original parameters.Megatron exports subsequent updates in the raw checkpoint layout. Those tensors therefore cannot be copied directly into the post-processed vLLM parameters, even if their names match.
The update path restores the representation expected by vLLM's
load_weights method before accepting an update:
weight_loader metadata.w1 and
w3 into w13.Running post-processing after each bucket is incorrect because later buckets would be loaded into parameters that an earlier post-processing pass already transformed. The bucketed transfer path must prepare the model before the first bucket and finalize it only after the last bucket.
verl/utils/vllm/vllm_quant_utils.py is the entry point: it quantizes the
outgoing weight stream and dispatches the prepare/finalize pair to the scheme
that owns each layer. verl/utils/vllm/vllm_fp8_utils.py handles the block
FP8 linear and MoE methods, verl/utils/vllm/vllm_fp4_utils.py handles the
MXFP4 routed experts, and the bucket-level orchestration is in
verl/workers/rollout/vllm_rollout/utils.py.
Incorrect synchronization can fail immediately with an expert shape mismatch
such as target 1024 vs loaded 2048. It can also complete load_weights
while leaving the expert layout or scales incorrect. In that case, the visible
symptom is a severe regression in rollout/actor log-probability correlation.
A successful load is therefore not sufficient evidence that synchronization
is correct.
R2 records routes during actor log-probability computation and replays them during the actor update. R3 records routes in the rollout backend and replays them in Megatron. Neither mode is needed merely to run DeepSeek V4; they are alignment features enabled by configuration.
The first three routed layers in DeepSeek-V4-Flash use a hash router. Their
expert IDs come from input_ids and a token-to-expert table rather than from
learned router logits. As a result, the existing learned top-k interception
does not observe these layers.
Supporting replay requires the Megatron path to:
input_ids into the decoder so the hash router has the same input as
the rollout model.Without the hash-router entries, vLLM reports routes for every MoE layer while Megatron omits the first three. Every subsequent route is then replayed against the wrong layer, even when the route tensors have compatible shapes.
R3 also needs a causal replay mask. The mask must include rows that influence
response-token logits, not only rows marked as response tokens. When a replayed
top-k result contains duplicate expert indices, dispatcher token counts must be
derived from the resulting routing map rather than assumed to equal
num_tokens * topk.
The decoder input and hash-router interception are implemented in
verl/models/mcore/model_forward_fused.py and
verl/utils/megatron/router_replay_patch.py. R3 mask construction and route
distribution are implemented in
verl/utils/megatron/router_replay_utils.py.
The following requirements are independent of weight synchronization and router replay:
deepseek_v4 model type.
Verl uses the vLLM configuration only for that exact missing-model-type case;
unrelated configuration errors continue to propagate.Verify the integration in this order: