radixark/miles#1046.
1. Model Introduction
DeepSeek-V4-Flash is a 13 B-active / 284 B-total MoE model with a substantially different attention stack from V3/R1. It ships in theradixark/miles:latest image. The larger DeepSeek-V4-Pro shares the same V4 architecture family at Pro scale.
Key highlights:
- Hybrid Attention (CSA + HCA): combines Compressed Sparse Attention (light compression) and Heavily Compressed Attention (heavy compression) layers — DeepSeek’s official V4 name (see HF model card §Introduction). Implementation uses low-rank Q (
q_lora_rank=1024), single-head latent KV (head_dim=512), grouped output projection (8 groups, LoRA rank 1024). A learned topk indexer (index_topk=512, 64 heads × 128 dim) picks 512 KV per query at runtime, inheriting V3.2’s DSA-style design. - KV compressors: 44-element compression schedule
compress_ratios = [0, 0, 4, 128, 4, 128, …, 4, 0]— first / last few layers are uncompressed (ratio 0), middle layers alternate 4× (CSA) and 128× (HCA). The compressor RoPE has its own base (compress_rope_theta=160000), separate from the main attention RoPE. - Hyper-connection (HC) routing: each layer expands hidden state into
hc_mult=4parallel streams and recombines via sinkhorn-normalized mixing. Pipeline-parallel buffers are 4-D[s, b, hc_mult, d]instead of 3-D. - YaRN RoPE on main attention:
rope_theta=10000, YaRNfactor=16,original_max_position_embeddings=65536→ effective context length 1,048,576 tokens (1 M). Per-head learnable attention sinks (one scalar per head, added to softmax denominator). - FP8 weights with simulated FP8 QAT on indexer and compressor activations.
2. Supported Variants
DeepSeek-V4-Flash and DeepSeek-V4-Flash-0731 are official releases with MXFP4 routed experts;
DeepSeek-V4-Flash-FP8 repackages DeepSeek-V4-Flash with blockwise-FP8 experts. On the torch_dist
path, the launcher’s prepare-fp8 stage (tools/convert_mxfp4_to_fp8.py) casts MXFP4 experts
losslessly to blockwise FP8, after which the pipeline is identical to DeepSeek-V4-Flash-FP8.
The direct-HF path (§3.4) trains from the official
checkpoint without this cast.
3. Quick start
3.1 One-line launch
One command runs the full pipeline — dataset download, FP8 → BF16 cast, distributedtorch_dist conversion, and the training loop:
full-train subcommand chains prepare-download → [prepare-fp8 →] prepare-single → prepare-spmd → train (prepare-fp8 only for the MXFP4 releases DeepSeek-V4-Flash and DeepSeek-V4-Flash-0731). Each stage has a sentinel-based skip so you can re-run safely after the first invocation. With --init-model-source hf (§3.4), it skips prepare-spmd and runs the casts only when a converted rollout checkpoint needs them.
3.2 Launcher path defaults
The Python launcher (scripts/run_deepseek_v4.py) takes its path arguments from CLI flags. The defaults are:
You can override these via the CLI flags above or equivalently via env vars — every launcher option binds to
MILES_SCRIPT_<FIELD_NAME_UPPER> (e.g. MILES_SCRIPT_MODEL_DIR), with precedence CLI flag > env var > built-in default; run train --help to see each option’s [env var: …] name.
3.3 Colocated vs. disaggregated rollout
By default the launcher runs colocated: training and SGLang rollout share all--num-nodes × --num-gpus-per-node GPUs. Pass --rollout-num-nodes N (0 < N < --num-nodes) to run disaggregated: N nodes serve rollout, the rest train. The verified parallelism recipes are keyed on the training nodes, so the 8-node Flash recipe in disaggregated form is --num-nodes 16 --rollout-num-nodes 8 (8 train + 8 rollout — the validated layout).
3.4 Initialization and rollout weights
--init-model-source selects where the trainer’s initial weights come from:
torch_dist(default):full-traincasts the HF checkpoint to BF16 and converts it to a Megatrontorch_distcheckpoint (§4.2), which the trainer loads.hf: Megatron-Bridge builds the model and loads the HF checkpoint directly (--megatron-to-hf-mode bridge), so notorch_distconversion runs. This requires--dsv4-impl megatron, the default.
--rollout-weight-source selects where SGLang’s weights come from:
checkpoint(default): SGLang loads a checkpoint in the rollout precision (§3.5), whichfull-trainconverts first if needed.trainer: SGLang reads only the model layout and starts with dummy weights; the trainer sends the actual weights in the weight update that precedes the first rollout. The layout comes from the source HF checkpoint, with the routed-expert override of--rollout-precisionapplied. BF16 rollout requirescheckpoint.
--init-model-source hf --rollout-weight-source trainer starts RL from
the official DeepSeek-V4-Flash checkpoint with no offline weight conversion:
--save-dir, and resuming loads them.
3.5 Training and rollout precision
--train-precision and --rollout-precision accept the same values. Each value
names the routed-expert format, so equal values align the routed experts between
training and rollout. Rollout keeps the other quantized weights in block-wise FP8,
the layout of the official checkpoint: mxfp8 and mxfp8-mxfp4-routed-expert
override only the routed experts.
mxfp8 and mxfp8-mxfp4-routed-expert require Blackwell, and
mxfp8-mxfp4-routed-expert requires an MXFP4 release (DeepSeek-V4-Flash or
DeepSeek-V4-Flash-0731). BF16 rollout loads a BF16 checkpoint and requires
--rollout-weight-source checkpoint; no checkpoint has the mxfp8 rollout
layout, so it requires --rollout-weight-source trainer. For example, --train-precision mxfp8 --rollout-precision mxfp8-mxfp4-routed-expert serves the official MXFP4 experts
while training the routed experts in MXFP8, so their numerics differ between
training and rollout.
The training option selects the GEMM recipe, not the optimizer master-weight dtype.
Model parameters remain BF16, except parameters explicitly kept in FP32, and the
FP8 recipes leave the DSA indexer’s weights projection in BF16.
mxfp8-mxfp4-routed-expert follows the routed-expert setting of the official
release and the DeepSeek-V4 technical report:
the release stores routed experts in MXFP4, and the report applies MXFP4
quantization-aware training to the MoE expert weights during post-training,
with a straight-through estimator, and samples RL rollouts from the native FP4
weights.
Training has no MXFP4 GEMM. With --train-precision mxfp8-mxfp4-routed-expert,
the routed-expert BF16 weights are rounded in place to the MXFP4 grid, with the block
layout and rounding of the rollout encoder, once per forward-backward pass before
their MXFP8 GEMMs, and the weight gradient passes straight through to the fp32
master weights. The forward pass of the routed experts thus matches their MXFP4 ×
MXFP8 rollout, without saving memory or time. Master weights and optimizer state
keep their precision; attention, shared experts and activations are not rounded to
MXFP4. This value requires --init-model-source hf and the same
--rollout-precision. The report computes on the rounded weights in block-wise
FP8; this value uses MXFP8, whose 1×32 activation scaling matches the MXFP4 ×
MXFP8 rollout kernel. Low Precision RL
shows how the formats line up between rollout and training.
The launcher picks SGLang’s GEMM and MoE backends from --rollout-precision.
--sglang-fp8-gemm-backend and --sglang-moe-runner-backend override that choice;
auto lets SGLang choose.
4. Script breakdown
In this section, we explain whatfull-train does under the hood, and how to drive each stage manually if you need to debug or run outside the one-line launcher.
4.1 Download model + datasets
prepare-download subcommand does the dataset fetch automatically; pass --hf-checkpoint <path> to skip the model download when the FP8 weights are already on a shared filesystem.
4.2 HF → Megatron torch_dist conversion
The direct-HF path (§3.4) skips this step. Otherwise, the conversion happens in two stages — a single-rank FP8 → BF16 cast, followed by a distributed torch_dist shard:
prepare-spmd subcommand drives the same conversion.
4.3 Multi-node fan-out
The Python launcher manages Ray internally — start each pod with theradixark/miles:latest image and a working shared filesystem mounted at the same path on every node, then on the head node:
MILES_SCRIPT_EXTERNAL_RAY=1 and RAY_ADDRESS=… to point the launcher at an existing Ray cluster (for example, one that an orchestration layer has already brought up). When RAY_ADDRESS is unset, the launcher boots a local Ray head.
4.4 Notable quirks
model_typerewrite for pruned checkpoints. The older layer-pruning flow wrote"model_type": "deepseek_ref"into the localconfig.json. The launcher rewrites it back todeepseek_v4in place (scripts/run_deepseek_v4.py), so HF’sAutoConfig.from_pretrainedresolves the checkpoint without a patchedtransformers.
5. Example Recipe Configuration
5.1 Megatron Parallelism
These are the validated layouts shipped with the launcher; All parallelisms are supported, you can supply any other TP / EP / PP / CP combination that fits your compute.
The Nodes × GPUs column counts actor (training) nodes — in disaggregated mode (see §3.3) rollout nodes come on top of these.
5.2 Algorithm
Using GRPO as an example, you can configure the algorithm with the following flags:--moe-router-freeze-gate and --freeze-e-score-correction-bias are required and asserted on the mcore side — bias-update during RL is forbidden.
The launcher’s --enable-mis adds importance sampling to correct the train/rollout mismatch.
5.3 Rollout & SGLang
SGLANG_SKIP_CHECKPOINT_LOAD_CHECK=1, SGLANG_DSV4_FP4_EXPERTS (1 for packed MXFP4 rollout experts, otherwise 0), SGLANG_HEALTH_CHECK_TIMEOUT=120, SGLANG_DG_CACHE_DIR_PER_PROCESS=1, and SGLANG_OPT_FP8_WO_A_GEMM=0, plus MILES_SGLANG_DUMMY_LOAD=1 for trainer-owned rollout. Because --train-deterministic defaults to on, a stock run also gets --deterministic-mode plus NCCL_ALGO=Ring, NVTE_ALLOW_NONDETERMINISTIC_ALGO=0 and CUBLAS_WORKSPACE_CONFIG=:4096:8; pass --no-train-deterministic to drop those four.
On the Megatron side, V4 needs --qkv-format bshd with CP-aware data slicing. The DSA indexer additionally supports replay via --use-rollout-indexer-replay (off by default).
5.4 Optimizer
--low-memory-resume flag (off by default) puts optimizer states on CPU during ckpt resume to avoid OOM on the very first iteration.
6. Pairs Well With
- Low Precision RL
- Architecture Support — the V4 plugin lives under
miles_plugins/models/deepseek_v4/.

