[PR #1525] [CLOSED] prefill: swiglu + the down product's q8_1 rows, and the MoE combine + hyper-connection write, each in one kernel (the same bits; NVFP4 packs: 8K prompt -5.1%) #3509

Closed
opened 2026-11-07 14:41:59 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/1525
Author: @sergqwer
Created: 10/8/2026
Status: ❌ Closed

Base: main ← Head: pr/prefill-fused


📝 Commits (1)

  • 759c8b9 prefill: swiglu + the down product's q8_1 rows, and the MoE combine + hyper-connection write, each in one kernel (the same bits; NVFP4 packs included)

📊 Changes

8 files changed (+474 additions, -8 deletions)

View changed files

📝 CMakeLists.txt (+12 -0)
📝 include/strata/prefill/kernels.hpp (+7 -0)
📝 include/strata/prefill/moe_mmq.hpp (+7 -0)
📝 src/prefill/kernels.cu (+81 -0)
📝 src/prefill/moe_mmq.cu (+79 -0)
📝 src/prefill/prefill.cpp (+49 -8)
➕ tests/cuda/prefill_combine_write_test.cpp (+103 -0)
➕ tests/cuda/prefill_swiglu_quant_test.cpp (+136 -0)

📄 Description

Rebased on 477ac13c (0.1.45), with a narrower scope.

  • The dense dequant part (Q8_0 / IQ4_XS to FP16 with 16-byte stores) is dropped. Upstream's dequant_q8_0_kernel and STRATA_DEQUANT_WIDE cover it now.
  • The two fusions stay. Both now also cover the NVFP4 packs: the experts' scales (swiglu_scaled, row_sd) and the FP16 xn16 image.

Two places in the prompt path write a value to memory and read it straight back:

  • swiglu + quantize: each MMQ group's swiglu (swiglu_scaled for NVFP4 experts) writes H in FP32, then quantize() reads it to q8_1 for the down product. That is two launches per group of 16 experts, ~31 groups per layer and chunk.
  • combine + write: the MoE half's combine writes bo (T x 2560 floats), then gr_write_norm_rs reads it back for each of the 4 hyper-connection rows.

Change (CUDA, the same bits):

  • mmq::swiglu_quant (STRATA_PREFILL_SWIGLU_QUANT=0 keeps the two kernels): swiglu and quantize_mmq_q8_1<D4>'s rows of H in one pass. H is never stored.
    • It is taken where the down type reads D4 rows: Q2_0, IQ4_NL, Q8_0, NVFP4 in its default W4A8 mode, and the i-quants.
    • Gate/up may be split (GGUF), interleaved (Strata pack), or split with each expert's {s_gate, s_up} applied as read. The last is NVFP4: swiglu_scaled's values, with the row's expert taken from the group's bounds.
    • Not with NVFP4's w4a4 activations (fp4_activations).
  • moe_combine_write_norm_rs (STRATA_PREFILL_COMBINE_WRITE=0 keeps the two kernels): one block per token.
    • It computes moe_combine4's sum in registers: the same thread-to-element mapping, the same k order, and the same row_sd (NVFP4's per-row s_down).
    • Then it does gr_write_norm_rs's four rows: R, rs, the BF16 or FP16 image (xf16), and xn16_lo. bo is never stored.
    • It is used exactly where the write is gr_write_norm_rs: not steered, not the last layer, not before layer 1's PLE block, not STRATA_GR_UNFUSED, Dm and shared 16-byte aligned, and not with the peer's sums.
  • Both fusions are off under HIP (not compiled or checked there) and under STRATA_DBG_NAN, which reads H and bo.
  • Tests (exit 77 without a GPU), both bit for bit:
    • prefill_combine_write_test: R, rs, xn16 and xn16_lo against the two kernels. T 1 / 333 / 1000 / 517, a padded token stride, row_sd, and the FP16 image.
    • prefill_swiglu_quant_test: the q8_1 bytes against swiglu (or swiglu_scaled) + quantize. Q2_0 / IQ4_NL / Q8_0 split and interleaved, NVFP4 groups of 1 / 3 / 16 experts that start past row 0, 1 / 37 / 4113 rows, a zero row and a 3e4 outlier.
    • Both pass on an RTX 5090 (sm_120, CUDA 13.3).

Identity: first-token logits (sha256) and greedy tokens are identical to 477ac13c, with --pcie-frac 0 --adapt-every 1000000 --expert-cache 3000: p2k 1e9c612c, 32K f5b2872c.

A/B. Setup:

  • RTX 5090, Ryzen 9 9950X3D, 128 GB, Windows 11, portable release build.
  • The GPTQ NVFP4 pack through serve: --expert-cache auto --vram-reserve-mib 1500 --prefill auto --spec 6 --spec-min-p 0.7 --kv int8 --max-context 262144.
  • The other arm is the same build with both switches at 0, which is upstream's path. Interleaved pairs over three sessions.
  • Each run reads fresh prompts several times; one value per run, its median.
prompt pairs both switches 0 this PR paired diff pairs faster
2K (2,099 tokens) 13 1079.9 ms 1076.4 ms -3.6 ms (-0.3%) 12 of 13
8K (8,035 tokens, one chunk) 10 1454.3 ms 1380.4 ms -74.0 ms (-5.1%), median -37 10 of 10
32K (4 chunks) 7 5528.9 ms 5455.5 ms -73.4 ms (-1.3%) 5 of 7

At 32K the run-to-run spread (sd of the differences 237 ms) is larger than the difference. Upstream's own build read 2K in 1088.0 ms and 32K in 5198.5 ms in the first session (3 runs), the same as the switched-off arm within that spread.

STRATA_PREFILL_TIMING=1 at 2K shows the fused path runs: "gemm down" 88 / 90 ms against 100 / 102 with the switches at 0, because the separate quantize is gone. At 2K the prompt path also waits 265-287 ms for expert copies, so most of the kernel time saved there turns into waiting.

Earlier, with the dequant part included (6674a006, IQ2_XS, --expert-cache 12000 --pcie-frac 0.25): 32K -4.0%, 8K -2.8%. Most of that was the dequant, which upstream covers now (dequant_q8_0_kernel; IQ4_XS through STRATA_DEQUANT_WIDE). That model is no longer on this PC.

How to reproduce: install with upstream setup --family nvfp4 --model NVFP4. Add --vram-reserve-mib 1500 to the engine arguments. Read fresh 2K / 8K / 32K prompts with and without STRATA_PREFILL_SWIGLU_QUANT=0 STRATA_PREFILL_COMBINE_WRITE=0. We ran --spec 6 --spec-min-p 0.7; setup writes 4 / 0.5, which the prompt path does not use.

Not measured: HIP, GPUs other than sm_120, the Q2_0 / GGUF packs on 0.1.45 (they are covered by the tests only), --peer-device, --prefill auto:32768, and NVFP4's w4a4 mode (the fusion is off there). Decode does not call these kernels.

🤖 Generated with Claude Code

https://claude.ai/code/session_01VZy1yKaDDiA8a7svdwaHio


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/1525 **Author:** [@sergqwer](https://github.com/sergqwer) **Created:** 10/8/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `pr/prefill-fused` --- ### 📝 Commits (1) - [`759c8b9`](https://github.com/Niko1221/Strata/commit/759c8b90f959066c41b0a6a7a825cff6887a4592) prefill: swiglu + the down product's q8_1 rows, and the MoE combine + hyper-connection write, each in one kernel (the same bits; NVFP4 packs included) ### 📊 Changes **8 files changed** (+474 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+12 -0) 📝 `include/strata/prefill/kernels.hpp` (+7 -0) 📝 `include/strata/prefill/moe_mmq.hpp` (+7 -0) 📝 `src/prefill/kernels.cu` (+81 -0) 📝 `src/prefill/moe_mmq.cu` (+79 -0) 📝 `src/prefill/prefill.cpp` (+49 -8) ➕ `tests/cuda/prefill_combine_write_test.cpp` (+103 -0) ➕ `tests/cuda/prefill_swiglu_quant_test.cpp` (+136 -0) </details> ### 📄 Description **Rebased on 477ac13c (0.1.45), with a narrower scope.** - The dense dequant part (Q8_0 / IQ4_XS to FP16 with 16-byte stores) is dropped. Upstream's `dequant_q8_0_kernel` and `STRATA_DEQUANT_WIDE` cover it now. - The two fusions stay. Both now also cover the NVFP4 packs: the experts' scales (`swiglu_scaled`, `row_sd`) and the FP16 `xn16` image. Two places in the prompt path write a value to memory and read it straight back: - **swiglu + quantize:** each MMQ group's swiglu (`swiglu_scaled` for NVFP4 experts) writes H in FP32, then `quantize()` reads it to q8_1 for the down product. That is two launches per group of 16 experts, ~31 groups per layer and chunk. - **combine + write:** the MoE half's combine writes `bo` (T x 2560 floats), then `gr_write_norm_rs` reads it back for each of the 4 hyper-connection rows. **Change** (CUDA, the same bits): - `mmq::swiglu_quant` (`STRATA_PREFILL_SWIGLU_QUANT=0` keeps the two kernels): swiglu and `quantize_mmq_q8_1<D4>`'s rows of H in one pass. H is never stored. - It is taken where the down type reads D4 rows: Q2_0, IQ4_NL, Q8_0, NVFP4 in its default W4A8 mode, and the i-quants. - Gate/up may be split (GGUF), interleaved (Strata pack), or split with each expert's `{s_gate, s_up}` applied as read. The last is NVFP4: `swiglu_scaled`'s values, with the row's expert taken from the group's bounds. - Not with NVFP4's w4a4 activations (`fp4_activations`). - `moe_combine_write_norm_rs` (`STRATA_PREFILL_COMBINE_WRITE=0` keeps the two kernels): one block per token. - It computes `moe_combine4`'s sum in registers: the same thread-to-element mapping, the same k order, and the same `row_sd` (NVFP4's per-row `s_down`). - Then it does `gr_write_norm_rs`'s four rows: R, rs, the BF16 or FP16 image (`xf16`), and `xn16_lo`. `bo` is never stored. - It is used exactly where the write is `gr_write_norm_rs`: not steered, not the last layer, not before layer 1's PLE block, not `STRATA_GR_UNFUSED`, Dm and shared 16-byte aligned, and not with the peer's sums. - Both fusions are off under HIP (not compiled or checked there) and under `STRATA_DBG_NAN`, which reads H and `bo`. - Tests (exit 77 without a GPU), both bit for bit: - `prefill_combine_write_test`: R, rs, `xn16` and `xn16_lo` against the two kernels. T 1 / 333 / 1000 / 517, a padded token stride, `row_sd`, and the FP16 image. - `prefill_swiglu_quant_test`: the q8_1 bytes against swiglu (or `swiglu_scaled`) + quantize. Q2_0 / IQ4_NL / Q8_0 split and interleaved, NVFP4 groups of 1 / 3 / 16 experts that start past row 0, 1 / 37 / 4113 rows, a zero row and a 3e4 outlier. - Both pass on an RTX 5090 (sm_120, CUDA 13.3). **Identity:** first-token logits (sha256) and greedy tokens are identical to 477ac13c, with `--pcie-frac 0 --adapt-every 1000000 --expert-cache 3000`: p2k `1e9c612c`, 32K `f5b2872c`. **A/B.** Setup: - RTX 5090, Ryzen 9 9950X3D, 128 GB, Windows 11, portable release build. - The GPTQ NVFP4 pack through serve: `--expert-cache auto --vram-reserve-mib 1500 --prefill auto --spec 6 --spec-min-p 0.7 --kv int8 --max-context 262144`. - The other arm is the same build with both switches at 0, which is upstream's path. Interleaved pairs over three sessions. - Each run reads fresh prompts several times; one value per run, its median. | prompt | pairs | both switches 0 | this PR | paired diff | pairs faster | |---|---|---|---|---|---| | 2K (2,099 tokens) | 13 | 1079.9 ms | 1076.4 ms | -3.6 ms (-0.3%) | 12 of 13 | | 8K (8,035 tokens, one chunk) | 10 | 1454.3 ms | 1380.4 ms | **-74.0 ms (-5.1%)**, median -37 | 10 of 10 | | 32K (4 chunks) | 7 | 5528.9 ms | 5455.5 ms | -73.4 ms (-1.3%) | 5 of 7 | At 32K the run-to-run spread (sd of the differences 237 ms) is larger than the difference. Upstream's own build read 2K in 1088.0 ms and 32K in 5198.5 ms in the first session (3 runs), the same as the switched-off arm within that spread. `STRATA_PREFILL_TIMING=1` at 2K shows the fused path runs: "gemm down" 88 / 90 ms against 100 / 102 with the switches at 0, because the separate quantize is gone. At 2K the prompt path also waits 265-287 ms for expert copies, so most of the kernel time saved there turns into waiting. Earlier, with the dequant part included (6674a006, IQ2_XS, `--expert-cache 12000 --pcie-frac 0.25`): 32K -4.0%, 8K -2.8%. Most of that was the dequant, which upstream covers now (`dequant_q8_0_kernel`; IQ4_XS through `STRATA_DEQUANT_WIDE`). That model is no longer on this PC. **How to reproduce:** install with upstream setup `--family nvfp4 --model NVFP4`. Add `--vram-reserve-mib 1500` to the engine arguments. Read fresh 2K / 8K / 32K prompts with and without `STRATA_PREFILL_SWIGLU_QUANT=0 STRATA_PREFILL_COMBINE_WRITE=0`. We ran `--spec 6 --spec-min-p 0.7`; setup writes 4 / 0.5, which the prompt path does not use. **Not measured:** HIP, GPUs other than sm_120, the Q2_0 / GGUF packs on 0.1.45 (they are covered by the tests only), `--peer-device`, `--prefill auto:32768`, and NVFP4's w4a4 mode (the fusion is off there). Decode does not call these kernels. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01VZy1yKaDDiA8a7svdwaHio --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 14:41:59 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#3509
No description provided.