RL-Kernel x verl: Zero mismatch RL on DeepSeek-V4.1-Flash¶
By RL-Kernel Team
TL;DR¶
We achieved zero-mismatch RL training on the full 40-layer DeepSeek-V4.1-Flash. Across 100 GRPO steps on 8 × H200, every response token's log-prob from the training engine is bitwise identical to the one from the rollout engine. verl's native recipe cannot run this model on the same hardware: only an 8-layer cut fits in memory, and that cut cannot generate a valid response.
Figure 1: 100 GRPO steps on the full DeepSeek-V4.1-Flash.
RL-Kernel is exactly 0 at every step.
- What is new in DeepSeek-V4.1? MXFP8 dense weights, a single-pass mHC, compressed KV and top-k indices shared across layers, the Engram n-gram memory, and a sliding-window cache that the release rebuilds with an approximate replay.
- How we did it? We built on the batch-invariant kernel set from our DeepSeek-V4 post. Most kernels carried over unchanged; DeepSeek-V4.1 needed one new CUDA kernel file and changes to four existing ones. (See What Is New)
- How to start? The same launcher as DeepSeek-V4; it picks the model family from the checkpoint (see Quick Start).
What Is New in DeepSeek-V4.1-Flash¶
This post only covers what DeepSeek-V4.1 adds. The five design rules, the test method and everything about MoE, expert parallelism and the sparse attention core are in the DeepSeek-V4 post and apply here unchanged.
DeepSeek-V4.1-Flash is the same family of operators with a different wiring: 40 layers instead of 43, a hidden size of 5,120 instead of 4,096, and 384 routed experts instead of 256. What that meant for the kernels:
| Modules | |
|---|---|
| Reused as is | Sparse attention core, KV record and insert, indexer query and logits, stable top-k, router, route plan, routed experts, MoE combine, EP exchange, LoRA, log-prob head |
| Small change | mHC (single-pass, wider hidden), compressor (ratios 1 and 2), q side |
| New | MXFP8 dense linear path, cross-layer KV and index sharing, two-level candidate selection, Engram, exact sliding-window attention |
Below we go through the new parts, in the same format as before.
MXFP8 dense weights¶
- Why it breaks batch invariance. DeepSeek-V4.1's dense weights carry one scale per 32 × 32 block instead of one per 128 × 128, so the FP8 GEMM path we validated for DeepSeek-V4 cannot represent them. A new GEMM kernel would mean a new reduction order to prove batch-invariant at every shape.
- How we solve it. We avoid the new kernel. The weights are dequantized once at load to BF16, which is exact (an FP8 value times a power of two is representable), and run on the BF16 GEMM that was already probed batch-invariant, re-probed at every DeepSeek-V4.1 dense shape. Every input of such a layer is first rounded to MXFP8, byte-equal to DeepSeek's reference; the rounding is exact element-wise arithmetic, so it cannot depend on the batch.
- Kernels. Reused: DeepGEMM
bf16_gemm_nt.
Single-pass mHC¶
- Why it breaks batch invariance. In DeepSeek-V4.1 a sublayer collapses the four residual streams with the mix computed by the previous sublayer, not its own, and the model ends without a learned head. The hidden size also grows past what our mHC kernel's fixed partition covered (K = 4H is now 20,480 in the controller GEMM). Changing a reduction to fit the new width risks changing the bytes DeepSeek-V4 already depends on.
- How we solve it. The same kernel is instantiated for the wider hidden size: each slice runs one chain over its chunks in ascending order and then the same fixed tree, so a DeepSeek-V4 input never enters the new chunk and keeps its bytes. The mix source (own, carried, or stream 0 at the entry) is a mode of the kernel, and the final collapse is one more kernel under the same contract. The controller GEMM's 16-way split was re-probed at the new K.
- Kernels. Ours:
mhc/dsv4_mhc.cu(extended). Reused: DeepGEMMtf32_hc_prenorm_gemm.
Cross-layer KV and index sharing¶
In DeepSeek-V4 every compressed layer owns its KV cache and its indexer. In DeepSeek-V4.1 only four layers write a compressed KV cache and eight publish top-k indices; every other layer reuses the nearest source below it. One layer also preselects candidates for the layers above: it takes the max score over blocks of 8 positions and publishes the top 2,048 blocks, and the layers that re-index score only those positions.
- Why it breaks batch invariance. Sharing couples layers in time. A consumer's bytes now depend on its source, which may have run in the same step, in an earlier prefill chunk, or in a decode step. Indices published once are read by many layers, so one order difference spreads to all of them. Scoring a gathered subset must give the same bytes as scoring everything, and the reference path takes a shortcut for short contexts, which is a second code path.
- How we solve it. Each KV group owns one shared slab of fixed size from the config, plus per-layer window rows. The canonical index order is applied once, when the source publishes. The block max is order-independent and therefore exact, the block top-k runs on the same stable top-k as before, and ties are broken by the original position, not the position in the gathered list. There is one selector for every context length and no short-context shortcut. The compressor handles the new ratios through its existing window code (ratio 2) or a direct row store (ratio 1).
- Kernels. Ours:
compressor/compress_fp8.cu(ratios 1 and 2, indexer key store),attention/dsv4_qo.cu(optional q-norm). Reused:topk/stable_topk.cu, FlashMLA and the KV record, unchanged.
Engram¶
Engram adds a gated n-gram lookup to the residual streams before two layers: the preceding tokens are hashed, the hash selects rows from a table of 196B parameters, and a gate decides how much of them to add.
- Why it breaks batch invariance. The hash of a token needs the tokens before it, which may sit in an earlier chunk or an earlier decode step. The gate has three hidden-size reductions and a sigmoid. And the table is too large for the GPU: if rollout and actor ever read different rows, or empty ones, the log-probs diverge with no error.
- How we solve it. The hash is integer arithmetic, so it is exact; the previous token ids are carried in the request's state across chunk and decode boundaries. The gate's reductions reuse the mHC kernel's fixed partition, and its sigmoid uses a correctly rounded exponential. The tables are frozen, live in host memory shared by the GPUs of a node, and are never synced. A negative control checks that empty tables do change the bytes.
- Kernels. Ours:
engram/dsv41_engram.cu.
Exact sliding-window attention¶
- Why it breaks batch invariance. DeepSeek-V4.1 does not persist each layer's sliding-window KV. The release rebuilds it with a bounded replay: it reruns only the last 128 tokens and truncates each replayed query's window at the point where the replay started. A token's bytes then depend on where the replay started, which depends on the prefill boundary and the cache state. Here the model's own inference shortcut is not batch-invariant by design.
- How we solve it. We never truncate a window. In this version every row is computed and the rollout does not reuse a prefix cache, which is exact by construction and costs prefill time. This changes which rows are computed and no kernel's arithmetic.
- Kernels. None new.
With these in place, the same end-to-end gate as before (continuous-batching rollout, the actor's packed prefill, and the training forward) passes on the release checkpoint: across all 40 layers, 0 of 2,108 tokens differ at any layer.
The full design is in the DeepSeek-V4.1 design document.
Results¶
Setup¶
We train the full DeepSeek-V4.1-Flash (40 layers) on 8 × H200 for 100 GRPO steps: DAPO-Math-17k with verl's DAPO math reward, 32 prompts × 8 responses per step, responses of up to 2,048 tokens, LoRA rank 16. The actor is RL-Kernel's model as a verl engine backend (EP 8); the rollout is vLLM with RL-Kernel's model plugin (DP 8 / EP 8) and CUDA graphs for decode.
Mismatch per step¶
| RL-Kernel + verl | verl native (NeMo Automodel) | |
|---|---|---|
| Model that fits on 8 × H200 | all 40 layers | first 8 layers only |
| Mismatched response tokens per step | 0 at all 100 steps | 523k on average, on the 8-layer cut |
| Time per step | 175 s | not comparable |
- Is \(\log p_{\text{rollout}} - \log p_{\text{train}} = 0\) ? Yes, for every response token at all 100 steps, by verl's own metric: an exact FP32 comparison per response token, with no tolerance.
- Why is there no baseline curve? verl's native DeepSeek-V4.1 path dequantizes the routed experts to BF16, which needs about 1,087 GB for 40 layers; an 8-layer cut already peaks at 100 GB per GPU. That cut cannot generate: every response runs to the 2,048-token limit and the reward stays at -2. So the only number we take from it is its mismatch count, which is never 0.
- Does RL-Kernel keep its kernels efficient? A step takes 175 s on average (133 s of generation, 28 s of actor update), and the 100 steps finished in 4.9 hours. Decode CUDA graphs made the difference: generation is 3.1× faster and the step 2.5× faster than our eager rollout.
Quick Start¶
The requirements are those of the DeepSeek-V4 guide, plus about 196 GB of host memory per node for the Engram tables.
git clone https://github.com/RL-Align/RL-Kernel && cd RL-Kernel
cd scripts/verl_e2e_lora
MODEL_FAMILY=dsv41 bash setup.sh rlkernel # environment for DeepSeek-V4.1 (vLLM main)
MODEL_FAMILY=dsv41 bash setup.sh data # checkpoint and DAPO-Math-17k
bash experiments.sh D41G # 8 GPUs: the 100-step run from this post
Limitations and Next Steps¶
Everything in the DeepSeek-V4 post's limitations still applies. DeepSeek-V4.1 adds four:
- Exact sliding-window attention by full recompute. Every prefill computes every row, and the rollout does not reuse a prefix cache. Next: a layer-staged replay that computes only the rows the output needs, about 24k rows whatever the prompt length (3.7% of a full pass at 32k tokens). The rule is derived and not yet validated.
- No DSpark, no vision. The drafter is not loaded, so the rollout does not get its decode speedup, and the model is text only.
- FP8 caches on Hopper. The policy is trained and sampled with an FP8 compressed KV and indexer key, not the FP4 formats the release was trained for, which exist only for newer GPUs. Rollout and actor still agree bitwise; the policy is not byte-for-byte the released one.
- A smaller workload and no evaluation yet. This run used 32 × 8 responses of up to 2,048 tokens and no validation set, so we report training reward only.
Acknowledgments¶
Open-Source Ecosystem and Framework Collaboration¶
This work stands on open-source projects that we use unmodified: verl for the RL loop, vLLM for the rollout, DeepGEMM and FlashMLA for the GEMM and sparse attention kernels, and DeepSeek's released reference code, which we used to check the model's wiring.
Core Contributors - DeepSeek-V4.1¶
KJLdefeated (Kai-Jie Lin): * Design, kernels, vLLM plugin, verl integration and validation.