From c4e372e699f90311ad500be892ca85ba97caf383 Mon Sep 17 00:00:00 2001 From: keptn42 Date: Sat, 24 Oct 2026 10:58:14 +0200 Subject: [PATCH 1/8] gemma4_bf16: i8a8 linears (int8 weight and activation, a scale per 128) From arch/gemma3's G3Linear: int8 codes with a bf16 scale per [1 x 128], gemm_nt_q at i8a8 over quant_act_i8g (an activation scale per row and 128 columns). Gemma 4 E4B's activations carry channels 40-200 times the row median; one int8 scale a row costs most of w8a8's KL (wikitext-2: w8a8 0.076, i8a8 0.010). q|k|v stay one GEMM; the fused junctions stay off. --- arch/gemma4_bf16/gemma4_bf16.cpp | 66 ++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/arch/gemma4_bf16/gemma4_bf16.cpp b/arch/gemma4_bf16/gemma4_bf16.cpp index d0bff9b7..aea34b37 100644 --- a/arch/gemma4_bf16/gemma4_bf16.cpp +++ b/arch/gemma4_bf16/gemma4_bf16.cpp @@ -104,7 +104,13 @@ struct G4Linear { rad_op op = 0, op_q = 0; /* the quantised GEMM and, for w4a8, its activation quantiser */ rad_buf q8 = 0, s8 = 0; /* the int8 pair (i8 codes + f32 row scale) the quantiser writes */ int64_t N = 0, K = 0; - bool quant = false; /* true: w4a8 */ + bool quant = false; /* true: w4a8 / w8a8 over a per-row int8 activation */ + /* true: i8a8 as arch/gemma3's G3Linear: int8 codes with a bf16 scale per [1 x 128] (libquant + * `codes=i8 group=128 scale=bf16`) over quant_act_i8g, an activation scale per (row, 128). + * Gemma 4 E4B's activations carry a few channels 40-200 times the row's median (the down + * projection's input most), and one int8 scale a row spends its codes on them. There is no + * fused norm/quantiser at this grid, so the junctions stay off for a model that uses it. */ + bool i8g = false; /* Reads another linear's int8 pair. q, k and v quantise the same norm output on the same * per-row grid, so with `in_from` set at declare this linear reads that linear's pair and * declares no quantiser of its own: one dispatch instead of three. */ @@ -175,6 +181,41 @@ inline int G4Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char * at w8a8 reads the same int8 activation and per-row scale as w4a8, so the quantiser is * shared. A recipe uses it for linears int4 hurts (attention, down_proj). */ const uint32_t cdt = known && enc.n_planes > 0 ? enc.plane[0].dtype : 0; + i8g = known && linear_is_w8(enc); + if (i8g) { + if (N % 16 || K % RAD_FP8_BLOCK) { + fprintf(stderr, "radiance: '%s' is %lldx%lld per rank; it takes rows in 16s and K in " + "whole %d-column activation scale groups\n", base, (long long)N, + (long long)K, RAD_FP8_BLOCK); + return RAD_E_INVAL; + } + w = decl_view(b, wn, nullptr, "codes", RAD_I8, { N, K }, RAD_ACCESS_PER_TOKEN, shard, grp, parts, np); + ws = decl_view(b, nm.f("%s.scale", base), wn, "scale", RAD_BF16, { N, K / RAD_FP8_BLOCK }, + RAD_ACCESS_PER_TOKEN, shard, grp, parts, np); + if (!w || !ws) return RAD_E_INVAL; + if (in_from && in_from->i8g && in_from->q8 && in_from->K == K) { + q8 = in_from->q8; + s8 = in_from->s8; + shared_in = true; + } else { + q8 = decl_b(b, nm.f("%s.q8", base), RAD_I8, { g.max_tok, K }); + s8 = decl_b(b, nm.f("%s.s8", base), RAD_F32, { g.max_tok, K / RAD_FP8_BLOCK }); + if (!q8 || !s8) return RAD_E_INVAL; + op_q = rw(b, RAD_OP(b, "quant_act_i8g", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", K), + RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", "bf16")), + RAD_NOWEIGHTS), + { a }, { q8, s8 }); + if (!op_q) return RAD_E_INVAL; + } + op = rw(b, RAD_OP(b, "gemm_nt_q", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("N", N), RAD_INT("K", K), + RAD_INT("group", RAD_FP8_BLOCK), + RAD_STR("dtype", "i8a8")), + RAD_WEIGHTS(w, ws)), + { q8, s8 }, { y }); + return op ? RAD_OK : RAD_E_INVAL; + } if (known && g4_grid_w4(enc)) { if (N % 16 || K % 128) { fprintf(stderr, "radiance: '%s' is %lldx%lld per rank; a w4 weight takes rows in " @@ -216,8 +257,8 @@ inline int G4Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char char en[256]; rad_enc_format(&enc, en, sizeof en); - fprintf(stderr, "radiance: '%s' is %s, and this model's linears are plain bf16, w4a8 or w8a8 -- " - "neither matches\n", wn, en); + fprintf(stderr, "radiance: '%s' is %s, and this model's linears are plain bf16, w4a8, w8a8 " + "or int8 a 128 (i8a8) -- none matches\n", wn, en); return RAD_E_DTYPE; } @@ -229,6 +270,11 @@ inline void G4Linear::step(RadCtx* c, rad_buf a, rad_buf y, int64_t T) const { if (!shared_in) RAD_ISSUE_N(c, op_q, T, brows(a, T), brows(q8, T), brows(s8, T), RAD_NONE); gemm(c, y, T); + } else if (i8g) { + /* quant_act_i8g takes x, q, scale: no optional `asum`. */ + if (!shared_in) + RAD_ISSUE_N(c, op_q, T, brows(a, T), brows(q8, T), brows(s8, T)); + gemm(c, y, T); } else RAD_ISSUE(c, op, brows(a, T), RAD_W(w), brows(y, T)); } @@ -239,18 +285,23 @@ inline void G4Linear::gemm(RadCtx* c, rad_buf y, int64_t T) const { } inline void G4Linear::step_rows(RadCtx* c, rad_buf a, rad_buf y, int64_t r0, int64_t n) const { - if (!quant) { + if (!quant && !i8g) { RAD_ISSUE_N(c, op, n, brow_slice(a, r0, n, K), RAD_W(w), brow_slice(y, r0, n, N)); return; } - if (!shared_in) + const int64_t sw = i8g ? K / RAD_FP8_BLOCK : 1; /* the scale plane's row width */ + if (!shared_in && i8g) + RAD_ISSUE_N(c, op_q, n, brow_slice(a, r0, n, K), brow_slice(q8, r0, n, K), + brow_slice(s8, r0, n, sw)); + else if (!shared_in) RAD_ISSUE_N(c, op_q, n, brow_slice(a, r0, n, K), brow_slice(q8, r0, n, K), brow_slice(s8, r0, n, 1), RAD_NONE); gemm_rows(c, y, r0, n); } inline void G4Linear::gemm_rows(RadCtx* c, rad_buf y, int64_t r0, int64_t n) const { - RAD_ISSUE_N(c, op, n, brow_slice(q8, r0, n, K), brow_slice(s8, r0, n, 1), RAD_W(w), RAD_W(ws), + const int64_t sw = i8g ? K / RAD_FP8_BLOCK : 1; + RAD_ISSUE_N(c, op, n, brow_slice(q8, r0, n, K), brow_slice(s8, r0, n, sw), RAD_W(w), RAD_W(ws), brow_slice(y, r0, n, N), RAD_NONE, RAD_NONE); } @@ -376,8 +427,9 @@ int Gemma4Attn::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const if (cls.has_v) RAD_ARCH_TRY(map_concat(b, qkv_wn, { q_src, k_src, v_src })); else RAD_ARCH_TRY(map_concat(b, qkv_wn, { q_src, k_src })); { + /* one q|k|v GEMM wherever the weight is on a quantised grid: w4a8 / w8a8 or i8a8 */ RadEncoding e{}; - fused = weight_enc(b, qkv_wn, &e) && g4_grid_w4(e); + fused = weight_enc(b, qkv_wn, &e) && (g4_grid_w4(e) || linear_is_w8(e)); } if (fused) { if (!qkv_slot) return RAD_E_INVAL; From e1ddb61087c64481d71698fbba308b85a2851aee Mon Sep 17 00:00:00 2001 From: keptn42 Date: Sat, 24 Oct 2026 10:58:32 +0200 Subject: [PATCH 2/8] recipes: gemma4-e4b-bf16-i8g-df (int8 trunk at i8a8, DFlash drafter on block FP8, bf16 vision tower) --- data/recipes/gemma4-e4b-bf16-i8g-df.recipe | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 data/recipes/gemma4-e4b-bf16-i8g-df.recipe diff --git a/data/recipes/gemma4-e4b-bf16-i8g-df.recipe b/data/recipes/gemma4-e4b-bf16-i8g-df.recipe new file mode 100644 index 00000000..6df7a5b3 --- /dev/null +++ b/data/recipes/gemma4-e4b-bf16-i8g-df.recipe @@ -0,0 +1,24 @@ +# gemma4-e4b-bf16-i8g-df.recipe -- Gemma 4 E4B (text, vision, NVIDIA's DFlash drafter) from a bf16 checkpoint: +# every trunk linear int8 with a bf16 scale per 128 columns, served at i8a8 (the activation quantised with a +# scale per row and 128 columns), an int8 lm_head, the drafter on block FP8, the vision tower bf16. +# Wikitext-2 KL against the bf16 original 0.010 (w8a8 recipe, one activation scale a row: 0.076). +# +# rad-convert --recipe data/recipes/gemma4-e4b-bf16-i8g-df.recipe --draft-model \ +# --set quantization_config.quant_method=compressed-tensors -o gemma4-e4b-i8g-df.rad + +blk.*.attn_qkv.weight rtn codes=i8 group=128 scale=bf16 +blk.*.attn_q.weight rtn codes=i8 group=128 scale=bf16 +blk.*.attn_o.weight rtn codes=i8 group=128 scale=bf16 +blk.*.ffn_down.weight rtn codes=i8 group=128 scale=bf16 +blk.*.ffn_gate_up.weight rtn codes=i8 group=128 scale=bf16 + +output.weight rtn codes=i8 group=128 scale=bf16 clamp=sym + +dflash.fc.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dflash.blk.*.attn_q.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dflash.blk.*.attn_k.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dflash.blk.*.attn_v.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dflash.blk.*.attn_output.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dflash.blk.*.ffn_gate_up.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dflash.blk.*.ffn_down.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dflash.draft_head.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 From 8f6f12e28823f2e4821643ef00f84c16a5ea9e6e Mon Sep 17 00:00:00 2001 From: keptn42 Date: Sat, 24 Oct 2026 11:04:36 +0200 Subject: [PATCH 3/8] README, compose: NVIDIA-Nemotron-3.5-Bolt-E4B int8 (i8a8) with DFlash and vision, one card --- README.md | 1 + deploy/compose/nemotron-3.5-bolt-e4b-i8g.yaml | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 deploy/compose/nemotron-3.5-bolt-e4b-i8g.yaml diff --git a/README.md b/README.md index ad8f3592..1a40b6a9 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ To support the development of radiance: [Patreon](https://patreon.com/StillDeadC | Gemma 4 31B Q8: int8 trunk and lm_head (Q8_0's grid at group 128), int8 DFlash drafter, vision | `gemma4-31b-dense-q8-df.rad` (33 GiB) | [convert](#convert-a-checkpoint) | `gemma4-31b-q8-df.yaml` | | Gemma 4 31B on four cards: int4 trunk, int8 activations and lm_head, RedHatAI's DFlash2 drafter, vision | `gemma4-31b-dense-w4a8-df2-qkv.rad` (23 GiB) | [download](https://huggingface.co/Ashlyn4202025/gemma4-31b-w4a8-dflash2) | `gemma4-31b-w4a8-df2-tp4.yaml` | | NVIDIA-Nemotron-3.5-Bolt-E4B bf16 (Gemma 4 E4B), NVIDIA's DFlash drafter, vision, one card | `nemotron-3.5-bolt-e4b-bf16.rad` (16 GiB) | [download](https://huggingface.co/keptn/nemotron-3.5-bolt-e4b-bf16) | `nemotron-3.5-bolt-e4b-bf16.yaml` | +| NVIDIA-Nemotron-3.5-Bolt-E4B int8 (i8a8, Gemma 4 E4B), NVIDIA's DFlash drafter, vision, one card | `nemotron-3.5-bolt-e4b-i8g.rad` (12 GiB) | [download](https://huggingface.co/keptn/nemotron-3.5-bolt-e4b-i8g) | `nemotron-3.5-bolt-e4b-i8g.yaml` | | Qwen-Image 2.1 FP8: text to image and image edits | `qwen-image-2.1-fp8.rad` (16 GiB) | [download](https://huggingface.co/StillDeadcode/radiance-qwen-image-2.1-fp8) | `qwen-image-2.1.yaml` | | Qwen-Image 2.1 Turbo FP8: the same in 8 steps | `qwen-image-2.1-turbo-fp8.rad` (16 GiB) | [download](https://huggingface.co/StillDeadcode/radiance-qwen-image-2.1-turbo-fp8) | `qwen-image-2.1-turbo.yaml` | | Krea 2 Turbo FP8: text to image, one card | `krea-2-turbo-fp8.rad` (16 GiB) | [download](https://huggingface.co/StillDeadcode/krea-2-turbo-radiance-fp8) | `krea-2-turbo.yaml` | diff --git a/deploy/compose/nemotron-3.5-bolt-e4b-i8g.yaml b/deploy/compose/nemotron-3.5-bolt-e4b-i8g.yaml new file mode 100644 index 00000000..8282c7e6 --- /dev/null +++ b/deploy/compose/nemotron-3.5-bolt-e4b-i8g.yaml @@ -0,0 +1,16 @@ +# NVIDIA-Nemotron-3.5-Bolt-E4B (Gemma 4 E4B, post-trained by NVIDIA) at int8 on one card: every trunk +# linear int8 at i8a8 (activation scale per row and 128 columns), NVIDIA's DFlash drafter (7 tokens a +# pass, the block it was trained at) and the bf16 vision tower in pinned host memory. bf16 KV. Thinking +# is off, as in NVIDIA's template; switch it per request (chat_template_kwargs). Sampling as NVIDIA +# states it (temperature 1.0, top_p 0.95) and the checkpoint's top_k 64. +name: radiance-nemotron-3.5-bolt-e4b-i8g +services: + radiance: + extends: { file: common.yaml, service: radiance } + container_name: radiance-nemotron-3.5-bolt-e4b-i8g + command: > + --model /models/nemotron-3.5-bolt-e4b-i8g.rad + --host :: --port ${PORT:-8000} --api-key ${RADIANCE_API_KEY:?set RADIANCE_API_KEY in .env} + --tp 1 --max-num-seqs 8 --max-model-len 65536 --kv-cache-dtype bf16 + --num-speculative-tokens 7 --mm-max-patches 2520 --vision-tower-placement ram + --temp 1.0 --top-p 0.95 --top-k 64 --chat-template-kwargs '{"enable_thinking":false}' From 6a6c50e718b58bf925f592a08da6bacd2526efa7 Mon Sep 17 00:00:00 2001 From: keptn42 Date: Sat, 24 Oct 2026 03:00:58 +0200 Subject: [PATCH 4/8] arch: RADIANCE_PREFETCH_BESIDE=0 turns prefetch_beside off Beside another server on the same card the lane-1 read costs more than it gains: the card has fewer hardware queue slots than the two servers have queues, and the join puts a queue switch into every decode step. --- arch/common/rad_arch.h | 12 +++++++++++- docs/GUIDE.md | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/arch/common/rad_arch.h b/arch/common/rad_arch.h index 6cd85d27..aa20f665 100644 --- a/arch/common/rad_arch.h +++ b/arch/common/rad_arch.h @@ -608,8 +608,18 @@ constexpr int64_t kPrefetchRows = 16; * would read in series on lane 0, so elsewhere the read goes on lane 1, behind a join from lane * 0. */ inline bool prefetch_lane0() { return rad_chain_on() != 0; } +/* RADIANCE_PREFETCH_BESIDE=0 issues none of these reads. Beside another server on the same card + * the lane-1 form costs more than it gains: the card has fewer hardware queue slots than the two + * servers have queues, and the join puts a queue switch into every decode step. */ +inline bool prefetch_beside_on() { + static const bool on = [] { + const char* e = std::getenv("RADIANCE_PREFETCH_BESIDE"); + return !(e && e[0] == '0' && e[1] == 0); + }(); + return on; +} inline void prefetch_beside(RadCtx* c, rad_op op, rad_weight w, int64_t T) { - if (!op || T > kPrefetchRows) return; + if (!op || T > kPrefetchRows || !prefetch_beside_on()) return; if (prefetch_lane0()) { RAD_ISSUE(c, op, RAD_W(w)); return; diff --git a/docs/GUIDE.md b/docs/GUIDE.md index 82d10fde..1bb8efed 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -3460,6 +3460,7 @@ rad-kbench --home $RADIANCE_HOME --max-cases 1 # are the kernels correct? | `HF_TOKEN` | for `scripts/hfget.sh` and `--model ` on a gated or private repo | | `XDG_CACHE_HOME` | where `--model ` fetches to without `--download-dir` (`$XDG_CACHE_HOME/radiance/hf`, else `~/.cache/radiance/hf`) | | `R4D_AR_MAX_BLOCKS` | a ceiling on libr4d's collective block counts, for two tensor-parallel engines on one set of cards; set it in both (below). The MoE models' hyper-connection gathers keep the minimum their LDS table needs. Unset, nothing changes | +| `RADIANCE_PREFETCH_BESIDE` | `0` issues no weight prefetch beside the decode collectives. For an engine that shares its cards with another server (below); alone it costs a few percent of decode. Unset, nothing changes | **Two engines on one set of cards.** Every block of a libr4d collective spins until its twin on the peer card has pushed, and a prefill-sized fused all-reduce launches more blocks than a card holds at @@ -3472,7 +3473,7 @@ low enough that their spinners together leave part of a card free, the cycle can Qwen3.8-27B (its decode at 16 sequences x 4 rows stays uncapped) and 16 for the Gemma ran 15 minutes of concurrent long prompts on both without a stall. The ceiling costs prefill time, the reductions running on fewer blocks: Qwen3.8-27B's 41k-token prefill 9.5 s uncapped, 9.8 s at 64, -10.3 s at 32. Give the engine started first its `--gpu-headroom-mib` for the second. +10.3 s at 32. Give the engine started first its `--gpu-headroom-mib` for the second. A model whose decode reads weights ahead on a second queue (Gemma 4 without chained launches) slows sharply beside a second server: an R9700 has four compute queue slots and the two servers hold eight, so every decode step waits for a queue switch. Gemma 4 31B at TP4 beside an idle Qwen3.8-Flash-Next went from 13 ms to 41 ms a step; with `RADIANCE_PREFETCH_BESIDE=0` in the Gemma engine it took 13.8 ms. **Tooling** From 7054e505d987ed12456dd6c71c514aa2d1fc36d6 Mon Sep 17 00:00:00 2001 From: keptn42 Date: Sat, 24 Oct 2026 12:39:49 +0200 Subject: [PATCH 5/8] gemma4_bf16: asymmetric int4 linears (w4a8i8_asym over a scale per 128) As gemma3's G3Linear: u4 codes, a u8 zero point and an f16 scale per [1 x 128], read against the same per-(row, 128) int8 activation as i8a8. --- arch/gemma4_bf16/gemma4_bf16.cpp | 51 ++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/arch/gemma4_bf16/gemma4_bf16.cpp b/arch/gemma4_bf16/gemma4_bf16.cpp index aea34b37..7f32d40b 100644 --- a/arch/gemma4_bf16/gemma4_bf16.cpp +++ b/arch/gemma4_bf16/gemma4_bf16.cpp @@ -111,6 +111,11 @@ struct G4Linear { * projection's input most), and one int8 scale a row spends its codes on them. There is no * fused norm/quantiser at this grid, so the junctions stay off for a model that uses it. */ bool i8g = false; + /* true: asymmetric int4 as arch/gemma3's G3Linear: u4 codes, a u8 zero point and an f16 scale + * per [1 x 128] (libquant `codes=u4 zero=u8 group=128 scale=f16`), gemm_nt_q at w4a8i8_asym + * over the same per-(row, 128) int8 activation as i8g. */ + bool a4 = false; + bool grouped() const { return i8g || a4; } /* Reads another linear's int8 pair. q, k and v quantise the same norm output on the same * per-row grid, so with `in_from` set at declare this linear reads that linear's pair and * declares no quantiser of its own: one dispatch instead of three. */ @@ -182,18 +187,32 @@ inline int G4Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char * shared. A recipe uses it for linears int4 hurts (attention, down_proj). */ const uint32_t cdt = known && enc.n_planes > 0 ? enc.plane[0].dtype : 0; i8g = known && linear_is_w8(enc); - if (i8g) { + { + const RadEncPlane* sp = rad_enc_plane(&enc, "scale"); + const RadEncPlane* zp = rad_enc_plane(&enc, "zero"); + a4 = known && rad_enc_is(&enc, "affine") && enc.n_planes == 3 && !enc.transform[0] && + cdt == RAD_U4 && enc.plane[0].block[1] == 1 && sp && sp->dtype == RAD_F16 && + sp->block[0] == 1 && sp->block[1] == RAD_FP8_BLOCK && zp && zp->dtype == RAD_U8 && + zp->block[0] == 1 && zp->block[1] == RAD_FP8_BLOCK; + } + if (i8g || a4) { if (N % 16 || K % RAD_FP8_BLOCK) { fprintf(stderr, "radiance: '%s' is %lldx%lld per rank; it takes rows in 16s and K in " "whole %d-column activation scale groups\n", base, (long long)N, (long long)K, RAD_FP8_BLOCK); return RAD_E_INVAL; } - w = decl_view(b, wn, nullptr, "codes", RAD_I8, { N, K }, RAD_ACCESS_PER_TOKEN, shard, grp, parts, np); - ws = decl_view(b, nm.f("%s.scale", base), wn, "scale", RAD_BF16, { N, K / RAD_FP8_BLOCK }, - RAD_ACCESS_PER_TOKEN, shard, grp, parts, np); + if (a4) { + w = decl_view(b, wn, nullptr, "codes", RAD_U4, { N, K }, RAD_ACCESS_PER_TOKEN, shard, grp, parts, np); + ws = decl_view(b, nm.f("%s.scale", base), wn, "scale,zero", RAD_F16, { N, K }, + RAD_ACCESS_PER_TOKEN, shard, grp, parts, np); + } else { + w = decl_view(b, wn, nullptr, "codes", RAD_I8, { N, K }, RAD_ACCESS_PER_TOKEN, shard, grp, parts, np); + ws = decl_view(b, nm.f("%s.scale", base), wn, "scale", RAD_BF16, { N, K / RAD_FP8_BLOCK }, + RAD_ACCESS_PER_TOKEN, shard, grp, parts, np); + } if (!w || !ws) return RAD_E_INVAL; - if (in_from && in_from->i8g && in_from->q8 && in_from->K == K) { + if (in_from && in_from->grouped() && in_from->q8 && in_from->K == K) { q8 = in_from->q8; s8 = in_from->s8; shared_in = true; @@ -211,7 +230,7 @@ inline int G4Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char op = rw(b, RAD_OP(b, "gemm_nt_q", RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("N", N), RAD_INT("K", K), RAD_INT("group", RAD_FP8_BLOCK), - RAD_STR("dtype", "i8a8")), + RAD_STR("dtype", a4 ? "w4a8i8_asym" : "i8a8")), RAD_WEIGHTS(w, ws)), { q8, s8 }, { y }); return op ? RAD_OK : RAD_E_INVAL; @@ -257,8 +276,8 @@ inline int G4Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char char en[256]; rad_enc_format(&enc, en, sizeof en); - fprintf(stderr, "radiance: '%s' is %s, and this model's linears are plain bf16, w4a8, w8a8 " - "or int8 a 128 (i8a8) -- none matches\n", wn, en); + fprintf(stderr, "radiance: '%s' is %s, and this model's linears are plain bf16, w4a8, w8a8, " + "int8 a 128 (i8a8) or asymmetric int4 a 128 -- none matches\n", wn, en); return RAD_E_DTYPE; } @@ -270,7 +289,7 @@ inline void G4Linear::step(RadCtx* c, rad_buf a, rad_buf y, int64_t T) const { if (!shared_in) RAD_ISSUE_N(c, op_q, T, brows(a, T), brows(q8, T), brows(s8, T), RAD_NONE); gemm(c, y, T); - } else if (i8g) { + } else if (grouped()) { /* quant_act_i8g takes x, q, scale: no optional `asum`. */ if (!shared_in) RAD_ISSUE_N(c, op_q, T, brows(a, T), brows(q8, T), brows(s8, T)); @@ -285,12 +304,12 @@ inline void G4Linear::gemm(RadCtx* c, rad_buf y, int64_t T) const { } inline void G4Linear::step_rows(RadCtx* c, rad_buf a, rad_buf y, int64_t r0, int64_t n) const { - if (!quant && !i8g) { + if (!quant && !grouped()) { RAD_ISSUE_N(c, op, n, brow_slice(a, r0, n, K), RAD_W(w), brow_slice(y, r0, n, N)); return; } - const int64_t sw = i8g ? K / RAD_FP8_BLOCK : 1; /* the scale plane's row width */ - if (!shared_in && i8g) + const int64_t sw = grouped() ? K / RAD_FP8_BLOCK : 1; /* the scale plane's row width */ + if (!shared_in && grouped()) RAD_ISSUE_N(c, op_q, n, brow_slice(a, r0, n, K), brow_slice(q8, r0, n, K), brow_slice(s8, r0, n, sw)); else if (!shared_in) @@ -300,7 +319,7 @@ inline void G4Linear::step_rows(RadCtx* c, rad_buf a, rad_buf y, int64_t r0, int } inline void G4Linear::gemm_rows(RadCtx* c, rad_buf y, int64_t r0, int64_t n) const { - const int64_t sw = i8g ? K / RAD_FP8_BLOCK : 1; + const int64_t sw = grouped() ? K / RAD_FP8_BLOCK : 1; RAD_ISSUE_N(c, op, n, brow_slice(q8, r0, n, K), brow_slice(s8, r0, n, sw), RAD_W(w), RAD_W(ws), brow_slice(y, r0, n, N), RAD_NONE, RAD_NONE); } @@ -427,9 +446,11 @@ int Gemma4Attn::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const if (cls.has_v) RAD_ARCH_TRY(map_concat(b, qkv_wn, { q_src, k_src, v_src })); else RAD_ARCH_TRY(map_concat(b, qkv_wn, { q_src, k_src })); { - /* one q|k|v GEMM wherever the weight is on a quantised grid: w4a8 / w8a8 or i8a8 */ + /* one q|k|v GEMM wherever the weight is on a quantised grid: w4a8 / w8a8, i8a8 or a4 */ RadEncoding e{}; - fused = weight_enc(b, qkv_wn, &e) && (g4_grid_w4(e) || linear_is_w8(e)); + fused = weight_enc(b, qkv_wn, &e) && (g4_grid_w4(e) || linear_is_w8(e) || + (rad_enc_is(&e, "affine") && e.n_planes == 3 && + e.plane[0].dtype == RAD_U4)); } if (fused) { if (!qkv_slot) return RAD_E_INVAL; From 7e42d394a51b2bdc3f4f1386b3a9eb99d5dab950 Mon Sep 17 00:00:00 2001 From: keptn42 Date: Sat, 24 Oct 2026 12:39:50 +0200 Subject: [PATCH 6/8] gemma4_bf16: a4 at decode heights through w4a16_asym w4a8i8_asym has only a prefill kernel. Up to 64 rows the input is cast to f16 for w4a16_asym's skinny kernel, as LinearFP8's w4 does. --- arch/gemma4_bf16/gemma4_bf16.cpp | 40 ++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/arch/gemma4_bf16/gemma4_bf16.cpp b/arch/gemma4_bf16/gemma4_bf16.cpp index 7f32d40b..382e99f6 100644 --- a/arch/gemma4_bf16/gemma4_bf16.cpp +++ b/arch/gemma4_bf16/gemma4_bf16.cpp @@ -116,6 +116,13 @@ struct G4Linear { * over the same per-(row, 128) int8 activation as i8g. */ bool a4 = false; bool grouped() const { return i8g || a4; } + /* a4 at decode heights, as LinearFP8's w4 (rad_fp8.h): up to kA4Split rows the bf16 input is + * cast to f16 (`a16`) for w4a16_asym's skinny kernel; w4a8i8_asym has only a prefill kernel, + * which at one row does 16 times the matrix work and reads its weight a quarter as fast. */ + static constexpr int64_t kA4Split = 64; + int64_t a4_split = 0; /* rows up to which op_dec serves; 0: no decode band */ + rad_buf a16 = 0; + rad_op op_a16 = 0, op_dec = 0; /* Reads another linear's int8 pair. q, k and v quantise the same norm output on the same * per-row grid, so with `in_from` set at declare this linear reads that linear's pair and * declares no quantiser of its own: one dispatch instead of three. */ @@ -212,7 +219,8 @@ inline int G4Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char RAD_ACCESS_PER_TOKEN, shard, grp, parts, np); } if (!w || !ws) return RAD_E_INVAL; - if (in_from && in_from->grouped() && in_from->q8 && in_from->K == K) { + /* not an a4 linear's pair: below its decode band it writes none */ + if (in_from && in_from->grouped() && !in_from->a4 && in_from->q8 && in_from->K == K) { q8 = in_from->q8; s8 = in_from->s8; shared_in = true; @@ -233,7 +241,24 @@ inline int G4Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char RAD_STR("dtype", a4 ? "w4a8i8_asym" : "i8a8")), RAD_WEIGHTS(w, ws)), { q8, s8 }, { y }); - return op ? RAD_OK : RAD_E_INVAL; + if (!op) return RAD_E_INVAL; + if (a4) { + a4_split = std::min(kA4Split, g.max_tok); + a16 = decl_b(b, nm.f("%s.a16", base), RAD_F16, { a4_split, K }); + if (!a16) return RAD_E_INVAL; + op_a16 = rw(b, RAD_OP(b, "cast", + RAD_PARAMS(RAD_RANGE("M", 1, a4_split), RAD_INT("n", K), + RAD_STR("from", g.dtype), RAD_STR("to", "f16")), + RAD_NOWEIGHTS), + { a }, { a16 }); + op_dec = rw(b, RAD_OP(b, "gemm_nt_q", + RAD_PARAMS(RAD_RANGE("M", 1, a4_split), RAD_INT("N", N), RAD_INT("K", K), + RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", "w4a16_asym")), + RAD_WEIGHTS(w, ws)), + { a16 }, { y }); + if (!op_a16 || !op_dec) return RAD_E_INVAL; + } + return RAD_OK; } if (known && g4_grid_w4(enc)) { if (N % 16 || K % 128) { @@ -289,6 +314,11 @@ inline void G4Linear::step(RadCtx* c, rad_buf a, rad_buf y, int64_t T) const { if (!shared_in) RAD_ISSUE_N(c, op_q, T, brows(a, T), brows(q8, T), brows(s8, T), RAD_NONE); gemm(c, y, T); + } else if (a4 && T <= a4_split) { + /* the decode band: f16 copy of the input, the skinny kernel; operands as LinearFP8's w4 */ + RAD_ISSUE_N(c, op_a16, T, brows(a, T), brows(a16, T)); + RAD_ISSUE_N(c, op_dec, T, brows(a16, T), RAD_NONE, RAD_W(w), RAD_W(ws), brows(y, T), + RAD_NONE, RAD_NONE); } else if (grouped()) { /* quant_act_i8g takes x, q, scale: no optional `asum`. */ if (!shared_in) @@ -304,6 +334,12 @@ inline void G4Linear::gemm(RadCtx* c, rad_buf y, int64_t T) const { } inline void G4Linear::step_rows(RadCtx* c, rad_buf a, rad_buf y, int64_t r0, int64_t n) const { + if (a4 && n <= a4_split) { + RAD_ISSUE_N(c, op_a16, n, brow_slice(a, r0, n, K), brows(a16, n)); + RAD_ISSUE_N(c, op_dec, n, brows(a16, n), RAD_NONE, RAD_W(w), RAD_W(ws), + brow_slice(y, r0, n, N), RAD_NONE, RAD_NONE); + return; + } if (!quant && !grouped()) { RAD_ISSUE_N(c, op, n, brow_slice(a, r0, n, K), RAD_W(w), brow_slice(y, r0, n, N)); return; From 60f6eab298d92c4c6b9ad73c312b4f5fba4a3dd2 Mon Sep 17 00:00:00 2001 From: keptn42 Date: Sat, 24 Oct 2026 12:39:50 +0200 Subject: [PATCH 7/8] gemma3: a4 at decode heights through w4a16_asym The same split as gemma4_bf16 and LinearFP8's w4: up to 64 rows an f16 copy of the input and w4a16_asym, above that quant_act_i8g and w4a8i8_asym. --- arch/gemma3/gemma3.cpp | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/arch/gemma3/gemma3.cpp b/arch/gemma3/gemma3.cpp index af01a6d8..f9efb790 100644 --- a/arch/gemma3/gemma3.cpp +++ b/arch/gemma3/gemma3.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -94,6 +95,13 @@ struct G3Linear { bool i8g = false; bool mx = false; bool a4 = false; /* asymmetric int4 over the per-(row, 128) int8 activation */ + /* a4 at decode heights, as LinearFP8's w4 (rad_fp8.h): up to kA4Split rows the bf16 input is + * cast to f16 (`a16`) for w4a16_asym's skinny kernel; w4a8i8_asym has only a prefill kernel, + * which at one row does 16 times the matrix work. */ + static constexpr int64_t kA4Split = 64; + int64_t a4_split = 0; + rad_buf a16 = 0; + rad_op op_a16 = 0, op_dec = 0; bool shared_in = false; /* Set before declare: an E4M3 (MXFP4) linear's input pair given by the caller (the pipelined * prefill's one pair for every block) instead of one of its own. It keeps its own quantiser @@ -205,7 +213,8 @@ inline int G3Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char ws = decl_view(b, nm.f("%s.scale", base), wn, "scale,zero", RAD_F16, { N, K }, RAD_ACCESS_PER_TOKEN, shard, grp, parts, np); if (!w || !ws) return RAD_E_INVAL; - if (in_from && (in_from->i8g || in_from->a4) && in_from->q8 && in_from->K == K) { + /* not an a4 linear's pair: below its decode band it writes none */ + if (in_from && in_from->i8g && in_from->q8 && in_from->K == K) { q8 = in_from->q8; s8 = in_from->s8; shared_in = true; @@ -225,7 +234,21 @@ inline int G3Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", "w4a8i8_asym")), RAD_WEIGHTS(w, ws)), { q8, s8 }, { y }); - return op ? RAD_OK : RAD_E_INVAL; + if (!op) return RAD_E_INVAL; + a4_split = std::min(kA4Split, g.max_tok); + a16 = decl_b(b, nm.f("%s.a16", base), RAD_F16, { a4_split, K }); + if (!a16) return RAD_E_INVAL; + op_a16 = rw(b, RAD_OP(b, "cast", + RAD_PARAMS(RAD_RANGE("M", 1, a4_split), RAD_INT("n", K), + RAD_STR("from", g.dtype), RAD_STR("to", "f16")), + RAD_NOWEIGHTS), + { a }, { a16 }); + op_dec = rw(b, RAD_OP(b, "gemm_nt_q", + RAD_PARAMS(RAD_RANGE("M", 1, a4_split), RAD_INT("N", N), RAD_INT("K", K), + RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", "w4a16_asym")), + RAD_WEIGHTS(w, ws)), + { a16 }, { y }); + return (op_a16 && op_dec) ? RAD_OK : RAD_E_INVAL; } if (i8g || mx) { if (N % 16 || K % RAD_FP8_BLOCK) { @@ -246,7 +269,7 @@ inline int G3Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char ws = decl_view(b, sn, wn, "scale", RAD_BF16, { N, K / RAD_FP8_BLOCK }, RAD_ACCESS_PER_TOKEN, shard, grp, parts, np); if (!w || !ws) return RAD_E_INVAL; } - if (in_from && in_from->mx == mx && (in_from->i8g || in_from->a4) == i8g && in_from->q8 && in_from->K == K) { + if (in_from && in_from->mx == mx && in_from->i8g == i8g && !in_from->a4 && in_from->q8 && in_from->K == K) { q8 = in_from->q8; s8 = in_from->s8; shared_in = true; @@ -288,6 +311,10 @@ inline void G3Linear::step(RadCtx* c, rad_buf a, rad_buf y, int64_t T) const { if (!shared_in) RAD_ISSUE_N(c, op_q, T, brows(a, T), brows(q8, T), brows(s8, T), RAD_NONE); gemm(c, y, T); + } else if (a4 && T <= a4_split) { + RAD_ISSUE_N(c, op_a16, T, brows(a, T), brows(a16, T)); + RAD_ISSUE_N(c, op_dec, T, brows(a16, T), RAD_NONE, RAD_W(w), RAD_W(ws), brows(y, T), + RAD_NONE, RAD_NONE); } else if (i8g || mx || a4) { /* quant_act_i8g / quant_act_fp8 take x, q, scale: no optional `asum`. */ if (!shared_in) @@ -308,6 +335,12 @@ inline void G3Linear::gemm_rows(RadCtx* c, rad_buf y, int64_t r0, int64_t n) con } inline void G3Linear::step_rows(RadCtx* c, rad_buf a, rad_buf y, int64_t r0, int64_t n) const { + if (a4 && n <= a4_split) { + RAD_ISSUE_N(c, op_a16, n, brow_slice(a, r0, n, K), brows(a16, n)); + RAD_ISSUE_N(c, op_dec, n, brows(a16, n), RAD_NONE, RAD_W(w), RAD_W(ws), + brow_slice(y, r0, n, N), RAD_NONE, RAD_NONE); + return; + } if (!shared_in) RAD_ISSUE_N(c, op_q, n, brow_slice(a, r0, n, K), brow_slice(q8, r0, n, K), brow_slice(s8, r0, n, K / RAD_FP8_BLOCK)); From 248ecf5858dddb57ad23201eab3465da3c43d586 Mon Sep 17 00:00:00 2001 From: keptn42 Date: Sat, 24 Oct 2026 12:39:51 +0200 Subject: [PATCH 8/8] recipes: gemma4-e4b-bf16-a4-df (int4 feed-forward, int8 attention and layers 19-23, DFlash, vision) --- data/recipes/gemma4-e4b-bf16-a4-df.recipe | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 data/recipes/gemma4-e4b-bf16-a4-df.recipe diff --git a/data/recipes/gemma4-e4b-bf16-a4-df.recipe b/data/recipes/gemma4-e4b-bf16-a4-df.recipe new file mode 100644 index 00000000..7b8e4b78 --- /dev/null +++ b/data/recipes/gemma4-e4b-bf16-a4-df.recipe @@ -0,0 +1,49 @@ +# gemma4-e4b-bf16-a4-df.recipe -- Gemma 4 E4B (text, vision, NVIDIA's DFlash drafter) from a bf16 checkpoint, +# 4-bit: the feed-forward as asymmetric int4 (u4 codes, a u8 zero point and an f16 scale per [1 x 128]), +# attention and layers 19-23 as int8 with a bf16 scale per [1 x 128] (i8a8). An int8 lm_head, the drafter on +# block FP8, the vision tower bf16. +# +# rad-convert --recipe data/recipes/gemma4-e4b-bf16-a4-df.recipe --draft-model \ +# --set quantization_config.quant_method=compressed-tensors -o gemma4-e4b-a4-df.rad +# +# The int4 linears read the activation quantised to int8 with a scale per (row, 128) above 64 rows +# (w4a8i8_asym) and an f16 copy of it at decode heights (w4a16_asym). Attention and layers 19-23, which +# amplify rounding at single tokens, stay at 8 bits. + +blk.19.attn_qkv.weight rtn codes=i8 group=128 scale=bf16 +blk.19.attn_o.weight rtn codes=i8 group=128 scale=bf16 +blk.19.ffn_gate_up.weight rtn codes=i8 group=128 scale=bf16 +blk.19.ffn_down.weight rtn codes=i8 group=128 scale=bf16 +blk.20.attn_qkv.weight rtn codes=i8 group=128 scale=bf16 +blk.20.attn_o.weight rtn codes=i8 group=128 scale=bf16 +blk.20.ffn_gate_up.weight rtn codes=i8 group=128 scale=bf16 +blk.20.ffn_down.weight rtn codes=i8 group=128 scale=bf16 +blk.21.attn_qkv.weight rtn codes=i8 group=128 scale=bf16 +blk.21.attn_o.weight rtn codes=i8 group=128 scale=bf16 +blk.21.ffn_gate_up.weight rtn codes=i8 group=128 scale=bf16 +blk.21.ffn_down.weight rtn codes=i8 group=128 scale=bf16 +blk.22.attn_qkv.weight rtn codes=i8 group=128 scale=bf16 +blk.22.attn_o.weight rtn codes=i8 group=128 scale=bf16 +blk.22.ffn_gate_up.weight rtn codes=i8 group=128 scale=bf16 +blk.22.ffn_down.weight rtn codes=i8 group=128 scale=bf16 +blk.23.attn_qkv.weight rtn codes=i8 group=128 scale=bf16 +blk.23.attn_o.weight rtn codes=i8 group=128 scale=bf16 +blk.23.ffn_gate_up.weight rtn codes=i8 group=128 scale=bf16 +blk.23.ffn_down.weight rtn codes=i8 group=128 scale=bf16 + +blk.*.attn_qkv.weight rtn codes=i8 group=128 scale=bf16 +blk.*.attn_q.weight rtn codes=i8 group=128 scale=bf16 +blk.*.attn_o.weight rtn codes=i8 group=128 scale=bf16 +blk.*.ffn_down.weight rtn codes=u4 zero=u8 group=128 scale=f16 rule=search +blk.*.ffn_gate_up.weight rtn codes=u4 zero=u8 group=128 scale=f16 rule=search + +output.weight rtn codes=i8 group=128 scale=bf16 clamp=sym + +dflash.fc.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dflash.blk.*.attn_q.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dflash.blk.*.attn_k.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dflash.blk.*.attn_v.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dflash.blk.*.attn_output.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dflash.blk.*.ffn_gate_up.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dflash.blk.*.ffn_down.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dflash.draft_head.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16