Skip to content

gemm: retain checked integer conversion for native activation launches - #984

Merged
juliankang4 merged 9 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-sa-q5-fp4-activation
Oct 29, 2026
Merged

juliankang4 merged 9 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-sa-q5-fp4-activation

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

FP4 입력 quantizer를 launch 1개로 합치고, 쓰이지 않는 gate/up scratch를 할당하지 않도록 했습니다.

What and why

When every routed expert of a Qwen load runs the native NVFP4 path, the input is quantized in two launches (Q8, then FP4) through a global Q8 plane that only the FP4 step reads. The fused gate/up path also keeps g and u scratch buffers that it never writes. After the routed gate/up, the FP4 conversion also walked every slot, including ones that are not listed.

This change:

  • Quantizes the input to FP4 in one launch. The Q8 rounding is shared (quant32_lane), and a native-only input skips the global Q8 stores. Inputs that other consumers share keep them.
  • Converts only the listed slots after the routed gate/up, so unlisted and refused slots are neither read nor overwritten.
  • Allocates g and u with zero cells when the load only uses the native fused gate/up. Mixed-format loads keep the existing scratch.
  • Separates operand validation from output-buffer validation in Gemm32Launch::check, so the fused path can validate without a y buffer.

The saving at 4096 routed slots per run is 2 * run * 10 * 640 * 4 bytes, 209715200 bytes. It is derived from the allocation formula, not measured on a full model. The A/B run below shows no prefill or decode change.

Release notes: Under the hood: native NVFP4 routed layers quantize their input in one launch and no longer allocate unused gate/up scratch.

Code shape

  • No second copy. The identity input and SwiGLU launchers share one kernel entry and one device core (fused_swiglu); only their input checks differ.
  • Model code holds only architecture facts. wide38.rs only decides whether the load is native-only; the common owners gained no per-model branch.
  • Dead code is gone. Nothing was left unused.
  • No silent failure. A Q8 consumer on activations that skipped the Q8 planes is a named shape error.
  • Comments state what is true now.
  • No relaxed gate. No band, tolerance or lint was moved. The gate gained Varied cases that force 1, 2, 4, 6 and 8 live fragments.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up.

Duplication found or removed (path:line), or "none": none.

What I ran

Base 4c549a477835d471929aa3c04718062488864d3b for the A/B. Gate runs used the head named in each result.

Gate commands (DGX Spark, BLOOMERY_ARCH=sm_121a):

BLOOMERY_BOX_LOCAL=1 just gate-gpu-gemm --case nvfp4_gate_up
BLOOMERY_BOX_LOCAL=1 ./tools/box.sh 'bash tools/gate.sh --gpu --oxide --release -p bloomery-gpu --lib -- --ignored --exact gemm::nvfp4mma::tests::hw_native_activation_planes --nocapture'
gemm32 case=nvfp4_gate_up rows=640 k=2560 slots=262 varied=false scratch_saved_bytes=1341440 refused=false poison=None q8_off=0 y_eq=true eager_graph_eq=true fault=None replay_fault_eq=true native=true PASS
native_activation k=2560 cols=7 only_native=false fp4_codes_scales_equal=true quantizer_nodes=1 PASS
native_activation k=2560 cols=7 only_native=true fp4_codes_scales_equal=true quantizer_nodes=1 PASS
native_activation k=2560 routed_listed=4 host=2 refused=1 inactive_planes_untouched=true PASS
test gemm::nvfp4mma::tests::hw_native_activation_planes ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 459 filtered out; finished in 18.10s

To check that the Varied cases are live, I changed $live <= 4 to $live <= 6 in gate_up_nvfp4_mma. The gate failed on the live-6 case, and passed again after restoring the original:

gemm32 case=nvfp4_gate_up rows=128 k=128 slots=96 varied=true scratch_saved_bytes=98304 refused=false poison=None q8_off=48 y_eq=false eager_graph_eq=true fault=None replay_fault_eq=true native=true FAIL
MUTANT live-6 Varied numerical FAIL confirmed
gemm32 case=nvfp4_gate_up rows=128 k=128 slots=96 varied=true scratch_saved_bytes=98304 refused=false poison=None q8_off=0 y_eq=true eager_graph_eq=true fault=None replay_fault_eq=true native=true PASS

The same nvfp4_gate_up gate also ran on an RTX 5090 with BLOOMERY_ARCH=sm_120a (partly resident, host and refused routes, Varied live arms, empty g/u, eager and graph equal).

Prefill and decode A/B on the real model, nvidia/Qwen3.8-Flash-Next-FP4-Local revision b9ce18a6887e18c089fdea8509a33e63429a3300, one DGX Spark, prompt 4096 tokens, 128 generated, median of three rounds per revision, order base/head/head/base/base/head:

cargo oxide build --arch "$(bash tools/arch.sh)" -- -p bloomery-gpu-gates --features glm5next,clef,vision,mimo2,solar2,minimaxm3,flint,gemma-embedding2,hf,checkpoint --release --bin bloomery-serve
env BLOOMERY_DRAFT=off BLOOMERY_HOST_ROOM=96G ./target/release/bloomery-serve -m "${MODEL_FP4L}" --host 127.0.0.1 --port 18731 --parallel 1 --ctx-size 16896

Each revision was built and served this way in turn. A client sent code prompts of 4096 tokens, greedy, 128 tokens generated, one at a time.

Metric Base Head Difference Ranges overlap
prefill tok/s 2555.46 2562.14 +0.261% yes
decode tok/s 32.0926 32.0656 -0.084% yes
TTFT p95 ms 1608.61 1605.12 +0.217% yes

The differences are below the 0.500% resolution, so I claim no speed change. Greedy output was equal.

Hardware (if the change is for a machine)

Machine Card Driver CUDA
DGX Spark NVIDIA GB10 595.84 13.3
RTX 5090 machine NVIDIA GeForce RTX 5090 595.99.02

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

@github-actions github-actions Bot added gpu The card engine: crates/gpu*, crates/app needs-gpu Changes code a card must check, and carries no gate run on a card tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo labels Oct 29, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: CLEAN: No behavior defect or proven quality defect remains.

Non-blocking

⚠ Complete the launch-fold evidence: crates/gpu/src/gemm/nvfp4mma.rs:2072.
At K=2560, input quantization changes from 20 Q8 CUDA blocks plus one FP4 block per column to one 64-thread block using 2,888 shared bytes [derived]. This touches decode and prefill.
Attach the registers/occupancy calculation and timed A/B commands with output for both tested hashes, as required for a launch fold with changed geometry. This establishes the cost of reduced parallelism before relying on the timing table.

Checked

Duplicates: 5 new functions checked; none duplicated. dryer reported 0 pairs. Changed gpu-gates code was checked manually.
Complexity: operands (44) moves existing validation and adds the Q8 guard; CardRoute38::new (27) adds load-time allocation choices; nvfp4_gate_up_case (72) adds case selection; fused_swiglu (18) adds conditional stores inside the device core.
Silent failure: 4 sites followed: skipped Q8 stores, zero-tile return, listed-prefix return, and discarded test fault. Q8 consumers are refused by name; route faults remain observable; the test propagates readback errors.
Tests, static mutation: forcing Q8 stores at nvfp4mma.rs:212 would fail the native-only zero assertions; changing >= to > at nvfp4mma.rs:1132 would fail the inactive-slot sentinel assertions. The PR supplies a numerical FAIL-first run for the wrong live-6 arm. Removing package-scoped cfg emission matching at recipes.py:9059 would fail the declared-only fixture assertion.
Bounds: the new hardware test covers K=64, 128, 192, 448, 2560 and Q8ACT_MAX_K, non-finite values, unaligned identity inputs and inactive-slot sentinels.
Gates: no tolerance or pin moved; existing cases remain. The new hardware test is ignored and hw_-prefixed. Python syntax passed.
Escape catalog: C1–C4 do not match the changed code.

Notes

GPU gates were not run during this review. The PR lacks the nvidia-smi line and the tested head hash required by CONTRIBUTING.md; GPU evidence is incomplete.

Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_gemm.rs, crates/gpu/src/arch/qwen3moe/wide38.rs, crates/gpu/src/gemm/act32.rs … and carries no gate run on a card (CONTRIBUTING's evidence list); a maintainer adds gpu-verified after one.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

@juliankang4
juliankang4 merged commit d80901a into midagedev:main Oct 29, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gpu The card engine: crates/gpu*, crates/app needs-gpu Changes code a card must check, and carries no gate run on a card tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant