Skip to content

qwen4_exp: W4A4 routed-expert prefill at FP4-Local's input scales (ATLAS_QWEN4EXP_PREFILL_W4A4, opt-in) - #266

Merged
AzeezIsh merged 7 commits into
Atlas-Inf:mainfrom
Enntity:sq-pr/w4a4
Oct 30, 2026
Merged

AzeezIsh merged 7 commits into
Atlas-Inf:mainfrom
Enntity:sq-pr/w4a4

Conversation

@data-angel

Copy link
Copy Markdown
Contributor

Part of #220. Stacked on #265: GitHub cannot base a cross-fork PR on another PR's branch, so review only the top three commits (spark-runtime: defer the skipped activation scales, kernels: moe_prefill_w4a4.cu, qwen4_exp: ATLAS_QWEN4EXP_PREFILL_W4A4=1 ...).

What

ATLAS_QWEN4EXP_PREFILL_W4A4=1 runs FP4-Local's routed-expert prefill on native W4A4: the activations are quantized to NVFP4 at the checkpoint's own input_scales, and the grouped GEMMs run as W4A4 (moe_prefill_w4a4.cu, common/nvfp4_block_quant.cuh). This replaces the q38 E4M3-chain GEMMs. Ported from SparkQwen's engine (Enntity/atlas 63221c5 and baa5d56).

  • Loading:
    • *.input_scale tensors are deferred (file and offset recorded, never uploaded) instead of dropped, for every expert.
    • With the switch on, w4a4_scales.rs builds the scale tables.
    • The startup K-major routed copy is skipped (an expected ~31.6 GiB per rank at TP=EP=2, not measured here).
  • Dispatch: try_checkpoint_routed_prefill picks W4A4 when the switch is on and the TC kernels when it is off, exactly as main does today. Layers without scales (MTP) fall back to TC.
  • Compatibility:
    • Byte for byte, the switch off leaves main's q38/nodup paths unchanged.
    • SP, _SP_ROUTE, _SP_RS_PIPE, ROWINV and the shared-expert blend in the reduce-scatter pipe all run unchanged under the switch.
  • In startup parity.

Why

This is where SparkQwen 0.4.0's routed prefill time goes. Production's commit reports 8.36 ms per layer for 16K rows (scripts/dev/qwen4exp_moe_w4a4_bench.sh time, copied here byte for byte). Prefill bytes equal 0.4.0's only with this arm on.

Benchmarks

Fingerprint (every number below). ennspark03/04, two GB10s at TP=EP=2 over RoCE, 2026-10-29. Checkpoint nvidia/Qwen3.8-Flash-Next-FP4-Local. Served by the SparkQwen launcher, profile 8x32k (--speculative --num-drafts=7 --max-num-seqs=8 --max-seq-len=32768 --max-prefill-tokens=16384 --kv-cache-dtype=bf16 --gpu-memory-utilization=0.88 --enable-prefix-caching), Strict and Spark modes.

  • Baseline: SparkQwen 0.4.0, engine Enntity/atlas 9b25f57, image atlas-sparkqwen:3e0d9e7e4b29.

  • Candidate: Enntity/atlas enntity-stack/sq-integ cf4200e = Atlas-Inf main a12bffa plus this PR series, image atlas-sparkqwen:cc103bd6cc92.

  • Harness: SparkQwen bench/sq_bench.py: streamed, temperature 0, reasoning_effort low; decode = 384-token prose/code/JSON, 3 reps; cold TTFT at 2K/8K/16K/28K, 2 reps; aggregate decode at C1–C8, 256 tokens per stream, 2 reps; warm 8K/20K conversations. Medians per cell. Plus greedy_eq.py (8 prompts, 320 tokens, temp 0) and lp_dump.py (echo prompt logprobs at 1K/6K/20K tokens).

  • Baseline and candidate arms interleaved on the same pair in one session.

  • One box and one harness, so these are observations under this fingerprint, not cross-validated claims.

  • Without this arm (image 6f3b619ca2fe):

    • Spark cold TTFT was +7.8% / +12.9% / +13.6% at 2K / 8K / 16K against 0.4.0.
    • Prompt logprobs differed from token 0: mean |Δ| 0.03 at 6K and 0.04 at 20K.
  • With it (and the SP fixes in qwen4_exp: SparkQwen's MXFP8 decode tiers, W8A8 prefill and EP-split MXFP8 shared expert on FP4-Local (opt-in) #265): cold TTFT is within ±1%, and logprobs are identical (table below). The two changes were measured together, so this arm's own share is not isolated.

Whole series vs SparkQwen 0.4.0 (arms q-040-* / q-fix-*):

Strict 0.4.0 Strict main+series Spark 0.4.0 Spark main+series
greedy text, 8 prompts 8/8 identical 8/8 identical
prompt logprobs (1K/6K/20K) max |Δ| 0.0000 max |Δ| 0.0000
decode prose / code tok/s 54.1 / 77.7 54.2 / 77.8 61.0 / 86.8 61.6 / 92.9
cold TTFT 2K / 16K / 28K s 0.681 / 3.94 / 6.92 0.677 / 3.95 / 6.99 0.673 / 3.90 / 6.88 0.673 / 3.94 / 6.94
C1 / C2 / C4 / C8 tok/s 51.3 / 83.5 / 131 / 192 51.3 / 83.4 / 130 / 189 58.1 / 88.5 / 139 / 192 58.8 / 89.2 / 139 / 202

For reference, Atlas-Inf main c998a49 with the same launcher measured −9..−12% at C1, −15% at C2, −21..−25% at C4 and +21% cold TTFT against 0.4.0 (2026-10-29, same pair and harness).

Default. Opt-in; it changes routed prefill bytes against q38. The flip measurement is the same as #265's.

Tests

  • Host tests (written first):
    • the deferred-scale record, in spark-runtime;
    • the two scale-table builders;
    • the switch parse;
    • scale-plane offsets;
    • startup parity names.
  • fmt, clippy, doc and the spark-model and spark-runtime tests pass.
  • The image build's workspace tests passed.
  • Not yet run: the GPU kernel check scripts/dev/qwen4exp_moe_w4a4_bench.sh check on this tree, and the pull-request gates.

Authorship

AI-authored (Claude Code), ported from SparkQwen's AI-authored engine. No human-written code.

🤖 Generated with Claude Code

data-angel and others added 7 commits October 29, 2026 21:23
…ensor-core tiers

SparkQwen 0.4.0 decodes FP4-Local's MXFP8 projections (GDN qkvz/out,
attention, indexer, shared expert) on `mxfp8_gemv_tc{8,16,32}`; main serves
them on the CUDA-core `mxfp8_proj` GEMV tiers. With the switch at 1, every
1..=32-row `mxfp8_proj` launch (decode, verify, `mxfp8_proj_exact_rows`
chunks) takes the tensor-core tier for its width instead. Both families are
row-invariant; they sum in different orders, so the switch is in startup
parity. Unset keeps today's tiers byte for byte.

Kernels: SparkQwen's `glm-5.3-flash/nvfp4/mxfp8_gemv.cu` (a superset of
main's: tiled, multi, SwiGLU and B128 forms over the same tc core), its PDL
touch helpers, `common/mxfp8_act_quant.cuh`, and the qwen3.8-flash-next
`mxfp8_gemv.cu` that includes them plus the SwiGLU twins.

Tests: dispatch per row count (32-row launches, tail tiers), the launch ABI,
the switch parse, the GEMV fallback, and an ignored GPU test that every row
of an m-row tc launch (m = 1..=65) equals its 1-row launch at the four
FP4-Local TP2 shapes on realistic and hostile bytes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…Lt W8A8 GEMMs

SparkQwen 0.4.0 prefills FP4-Local's MXFP8 projections (GDN qkvz/out,
attention q/k/v/o) as MXFP8 x MXFP8 GEMMs on cuBLASLt's block-scaled tensor
cores; main expands each weight to BF16 per call and runs a BF16 GEMM. With
the switch at 1 each layer carries an `MxPrefill` (the checkpoint's E4M3
bytes, scales relaid once at load into cuBLASLt's 128x4 tiles, 3% of the
weight) and every prefill pass takes it; decode and verify are unchanged.
The prefill mHC collapse writes the qkvz / q-k-v input's MXFP8 image in its
epilogue (`hc_up_mix_bf16_nt_mx`) so those rows skip the quant pass.

Ported from SparkQwen's engine (Enntity/atlas spark-shared 9b25f57):
- spark-runtime: `fp8_gemm_act_weight_t_mx` and its tuned per-row-bucket
  in-order kernel choice (`mx_algo`, the invariance sweep `mx_sweep`, the
  pinned kernel split into `mx_pinned` for the file cap), `fixed_algo`'s
  MXFP8 / block-FP8 operands, and `kchain_pin::in_order`, which also stops
  the BF16 k-chain pin from taking a negative split-K kernel
- the arena's FP8 scale scratch holds a tiled MXFP8 image of any row count
- `ops::mxfp8_act` (activation quant, scale relayout), `ops::MxW8a8`,
  `ops::qwen4exp_w8a8` (the switch, the collapse producer slots) and
  `common/mxfp8_act_quant.cu`
- the PDL list check reads `atlas_pdl_enter_touch_tri`, and the GLM list
  names the tiled, block-FP8 and multi MXFP8 tiers the shared kernel file
  now defines (inert unless ATLAS_PDL=1)

Unset keeps main's prefill byte for byte. Opt-in (`1`), where SparkQwen's
engine reads it as default-on; SparkQwen sets it. In startup parity.

Tests: switch parse, the per-layer operand lookup, the producer slots,
activation quant against a host reference (ModelOpt rounding), cuBLASLt
tile offsets, bucket/sweep tables, and ignored GPU tests that the quant and
relayout kernels write the host reference's bytes and that W8A8 rows do not
depend on the pass height and stay within 5e-2 (rel L2) of the BF16
expansion at the FP4-Local TP2 shapes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…pert

SparkQwen 0.4.0 serves FP4-Local's MXFP8 shared expert three ways main
does not: decode and verify rows on the tensor-core SwiGLU tiers
(`mxfp8_gemv_tc{8,16,32}_swiglu`, gate/up and SiLU * up in one pass, then
down on the plain tiers), prefill as W8A8 GEMMs under
ATLAS_QWEN4EXP_PREFILL_W8A8=1, and under EP each rank holding half the
intermediate (column-parallel gate/up, row-parallel down: the reference's
TP2 layout) with its partial joining the routed partial before the EP
all-reduce or reduce-scatter. On EP decode steps it runs on a side stream
forked at the MoE's entry, overlapping the router and routed experts.

With the switch at 1 the layer carries `SeparateSharedExpert::Mxfp8Native`
(`layers/moe/shared_mxfp8.rs`, loaded by `weight_loader/qwen4_exp/
shared_mx8.rs`). `MoeLayer::shared_joins_reduce` keeps the partial in every
EP blend (single row, row pair, T rows, batched, MOE_FAST) and stops
`forward_ep_reduce` adding a shared expert after the reduce; prefill blends
it before the reduce, or per row range inside the q38 reduce-scatter pipe.
The atomic C4 arm, whose finalize adds a whole shared expert, steps aside.
A call that finds the lane forked joins it and checks it writes the lane's
rows. Unset keeps main's shared expert byte for byte. In startup parity.

Ported from SparkQwen's engine (Enntity/atlas spark-shared 9b25f57:
f7ee11e, 789285e, 802eddd, 9507c06, 89ffeed), with
`ops::mxfp8_swiglu_rows` for main's MXFP8 weight type.

Tests: SwiGLU row launches (tiers, 32-row chunks, ABI, k refusal), the
decode and W8A8-less prefill launch order, and twin shape refusals.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… paths

Two SP prefill overlaps that SparkQwen 0.4.0 runs and main skipped on
FP4-Local (one profiled 16K cold prefill on 03/04: +60 ms routing, +21 ms
GDN out_proj against 0.4.0):

- ATLAS_QWEN4EXP_PREFILL_SP_ROUTE routed a rank's own rows only through the
  NVFP4 router copy, which FP4-Local does not build (its router stays the
  checkpoint's BF16), so every rank routed all rows after the gather. It now
  routes them on the BF16 router's tile GEMM, the kernel the whole-batch
  prefill router already runs (`MoeLayer::try_bf16_router_gemm`, one
  function for both), as SparkQwen does (Enntity/atlas 28611c0).
- ATLAS_QWEN4EXP_PREFILL_SP_RS_PIPE piped out_proj by row ranges only on the
  dense BF16 arm; an MXFP8 layer has no `out_proj_dense`, so its out_proj
  and reduce-scatter ran back to back. Native MXFP8 on W8A8 pipes too: its
  rows are independent (per-row quant, in-order kernels), so every byte is
  the unsplit pass's (`out_proj_rows_independent`). The ranges compute on
  one stream, so they share the activation scratch in order.

Tests: the BF16 router GEMM launches only on a flagged layer, one 128x128
tile grid; out_proj pipes on dense BF16 and W8A8 MXFP8, not on MXFP8's GEMV
tiers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nstead of dropping them

skip_activation_scales (qwen4_exp only) kept the ~74k W4A4 *.input_scale
scalars off the device (each a full allocation granule) and recorded
nothing. They are now deferred: not uploaded, but their shard and offset
kept in the store for every expert, remote ones included (the EP filter does
not apply to them), so a model loader can read them on the host --
qwen4_exp's W4A4 routed prefill takes a layer's gate/up input scale as the
max over all 512 experts, as the reference engine does. Nothing reads them
unless that prefill is selected; device memory is unchanged.

Ported from SparkQwen's engine (Enntity/atlas 9b25f57: baa5d56).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…4.cu)

FP4-Local's routed experts are NVFP4 with checkpoint input_scales. NVIDIA's
engine for that card (eugr/spark-vllm-b12x: B12X_W4A16_A4_PREFILL=1,
VLLM_B12X_MOE_FP4_LAYER_MAX_INPUT_SCALE=w13) runs prompt rows W4A4 on the
weights as stored. moe_prefill_w4a4.cu does that, on mma.sync
kind::mxf4nvf4 m16n8k64 straight from the checkpoint's N-major planes:

- moe_w4a4_quant_input: the input to NVFP4 once under g1 = 1 / max over all
  512 experts of the gate/up input_scale;
- moe_w4a4_gate_up_silu: gate/up = bf16(acc * weight_scale_2 / g1),
  act = bf16(bf16(g * sigmoid(g)) * u) with the reference's ex2 / rcp
  approximations and no clamp, act to NVFP4 under the expert's
  g2 = 1 / w2 input_scale;
- moe_w4a4_down: bf16(acc * weight_scale_2 / g2) into expert_down_out.

The quantizer (common/nvfp4_block_quant.cuh) is b12x quantize_block_fp4 bit
for bit: sf = e4m3(min(amax * g / 6, 448)), codes from |x| against the
rounded thresholds decode(sf) / g * {0.25, 0.75, ...}, ties to even. Rows are
quantized per row and each output is one in-order FP32 accumulator, so a
row's bytes do not depend on the rows beside it.

Nothing dispatches them yet (the next commit adds
ATLAS_QWEN4EXP_PREFILL_W4A4). scripts/dev/qwen4exp_moe_w4a4_bench.sh check
compares them to a CPU b12x reference; `time` against the q38 and BF16 TC
chains (production: GB10, 16000 tokens, 81K local rows: W4A4 8.36 ms a layer
vs the E4M3 chain 14.71 and the BF16 TC chain 20.25).

Ported byte for byte from SparkQwen's engine (Enntity/atlas 9b25f57:
63221c5).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… at the checkpoint's input scales

SparkQwen 0.4.0 prefills FP4-Local's routed experts the way NVIDIA's b12x
engine does: the activations quantized to NVFP4 at the checkpoint's input
scales and contracted with the NVFP4 weights as stored on native FP4 tensor
cores (moe_prefill_w4a4.cu), where main runs q38's E4M3 chain (weights
rounded to E4M3, activations cast unscaled). Under the switch (with
ATLAS_QWEN4EXP_PREFILL_MOE=1):

- each layer reads its 1536 deferred input scales (w4a4_scales.rs: g1 = 1 /
  max over all 512 experts of the gate/up scale, g2[e] = 1 / down scale,
  alpha = weight_scale_2 / g) into one small table at load;
- the q38 routed chain's GEMMs become moe_w4a4_quant_input,
  moe_w4a4_gate_up_silu and moe_w4a4_down on the same buffers and row
  order, so the router, the local unpermute, the EP zeroing skip, the SP
  route and the reduce-scatter pipe (with the MXFP8 shared-expert blend)
  run as before (q38_routed_serves keeps gating them);
- a layer without usable scales (MTP), and every layer under
  _PREFILL_MOE_BF16 / _PREFILL_BF16_PROJ (ROWINV decode precision), runs the
  BF16 TC kernels (W4A16), which must then serve;
- the startup transpose builds no routed K-major copy (both arms read the
  checkpoint planes), as under _MOE_NODUP.

Unset keeps main's q38 prefill byte for byte (_MOE_NODUP, _MOE_CHECK and
the q38 routed kernels unchanged). In startup parity: it changes the rows
both ranks reduce and the memory left for the KV pool.

Ported from SparkQwen's engine (Enntity/atlas 9b25f57: 63221c5), which
made W4A4 replace q38 outright; here it is switch-gated and the serve
predicate under the switch is production's (checkpoint planes, hidden 2560
/ inter 640).

Tests: the scale tables (g1 over remote experts too, alpha, unusable
scales), the switch's parse (on only at 1), scale-plane offsets, parity.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@data-angel
data-angel requested a review from AzeezIsh as a code owner October 30, 2026 04:53
AzeezIsh added a commit that referenced this pull request Oct 30, 2026
…nd EP-split MXFP8 shared expert on FP4-Local (opt-in)

Pair-gated on main + #265 + #266 (wave1a, #220): switches unset -> C1/C8 == #198 refs 8/8, tools 21/21; all four switches on -> decode-floor 89.3 vs 84.1 tok/s (n=3), cold TTFT -13..-15%, tools 21/21. CI green except the PR benchmark gate.
@AzeezIsh
AzeezIsh merged commit f1c8e55 into Atlas-Inf:main Oct 30, 2026
1 check passed
@AzeezIsh

Copy link
Copy Markdown
Contributor

Merged with #265. Pair results are on #265.

data-angel added a commit to Enntity/atlas that referenced this pull request Oct 30, 2026
Co-Authored-By: Claude Opus 5.5 <[email protected]>

# Conflicts:
#	crates/spark-model/src/weight_loader/qwen4_exp/mode.rs
data-angel added a commit to Enntity/atlas that referenced this pull request Oct 30, 2026
Co-Authored-By: Claude Opus 5.5 <[email protected]>

# Conflicts:
#	crates/spark-model/src/weight_loader/qwen4_exp/mode.rs
AzeezIsh added a commit that referenced this pull request Oct 30, 2026
…ft quantizer, BF16 KV-only prompt rows

Pair-gated (#220, wave1b/1c): switches unset -> no change to C1/C8 vs refs; with MTP_BF16_ATTN/ROUTER on top of the #265/#266 set, decode-floor 89.97-90.00 vs 89.25-89.36 without (observed, one boot each); Spark mode (NVFP4 draft attn) gives C1 identical to the BF16-draft run. CI green except the PR benchmark gate.
@data-angel
data-angel deleted the sq-pr/w4a4 branch November 3, 2026 05:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants