From 3259b4a8a2899ea42b8d904853be870985213c93 Mon Sep 17 00:00:00 2001 From: sfjes Date: Fri, 9 Oct 2026 21:28:35 -0700 Subject: [PATCH] arch: gemma4_bf16 plugin + w4a8 recipe + graph cases Gemma 4 dense as two attention classes (sliding-window + proportional-RoPE global, k_eq_v) over one GemmaRMSNorm stream with GELU ffn, two KV groups, softcapped logits, tied scaled embedding. arch_test builds the full graph. Env-gated residual-trace instrument in the arch (RADIANCE_TRACE_RESID, default off). 21.7 GiB w4a16 QAT checkpoint serves coherent sampled output on the box. --- arch/gemma4_bf16/CMakeLists.txt | 11 + arch/gemma4_bf16/gemma4_bf16.cpp | 883 ++++++++++++++++++++++ data/recipes/gemma4-31b-dense-w4a8.recipe | 19 + 3 files changed, 913 insertions(+) create mode 100644 arch/gemma4_bf16/CMakeLists.txt create mode 100644 arch/gemma4_bf16/gemma4_bf16.cpp create mode 100644 data/recipes/gemma4-31b-dense-w4a8.recipe diff --git a/arch/gemma4_bf16/CMakeLists.txt b/arch/gemma4_bf16/CMakeLists.txt new file mode 100644 index 00000000..1a4f2317 --- /dev/null +++ b/arch/gemma4_bf16/CMakeLists.txt @@ -0,0 +1,11 @@ +# A plugin like any other: built here inside radiance's tree, or on its own against an installed +# radiance (`cmake -S arch/gemma4_bf16 -B build -DCMAKE_PREFIX_PATH=`) -- the same +# rad_add_plugin either way (cmake/RadAddPlugin.cmake). +if(NOT COMMAND rad_add_plugin) + cmake_minimum_required(VERSION 3.21) + project(gemma4_bf16 LANGUAGES CXX) + find_package(radiance REQUIRED) + rad_plugin_defaults() +endif() + +rad_add_plugin(gemma4_bf16 architectures SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/gemma4_bf16.cpp) diff --git a/arch/gemma4_bf16/gemma4_bf16.cpp b/arch/gemma4_bf16/gemma4_bf16.cpp new file mode 100644 index 00000000..91438c7e --- /dev/null +++ b/arch/gemma4_bf16/gemma4_bf16.cpp @@ -0,0 +1,883 @@ +/* gemma4_bf16.cpp -- Gemma 4 dense, text-only, bf16. + * + * Two attention classes over ONE residual stream, alternating by layer. A "sliding" layer is + * sliding-window attention over head_dim 256 with a v projection and standard RoPE; a "global" + * layer is windowless attention over head_dim 512 with no v projection (k == v) and proportional + * RoPE (the "head" ladder) over the partial rotary width. Every layer is GemmaRMSNorm -- x_hat * + * (1 + w) -- with a SECOND norm on each projection's output before its residual, a weightless RMS + * on the value, and a GELU (not SwiGLU) feed-forward. The final logits are soft-capped to + * cap * tanh(x / cap). + */ +#include + +#include +#include +#include +#include +#include +#include + +namespace gemma4_bf16 { + +using namespace rad::arch; + +/* ---- container geometry RadModelMeta does not name. Both spellings are accepted: a .rad + * converted from the HF safetensors carries the flat HuggingFace keys and one converted from a + * GGUF carries llama.cpp's. */ +static long long g4_i(const RadModelMeta* m, const char* bare, long long dflt) { + long long v = rad_meta_geti(m, bare, dflt); + if (v == dflt) v = rad_meta_geti(m, (std::string("text_config.") + bare).c_str(), dflt); + return v; +} +static double g4_f(const RadModelMeta* m, const char* bare, double dflt) { + double v = rad_meta_getf(m, bare, dflt); + if (v == dflt) v = rad_meta_getf(m, (std::string("text_config.") + bare).c_str(), dflt); + return v; +} + +static constexpr int TRC_SITES = 5; /* x0, h_attn, h_mlp, x_l, h_fin */ +static inline bool trc_on() { static const bool on = std::getenv("RADIANCE_TRACE_RESID") != nullptr; return on; } +struct Model; /* forward; defined below -- the trace ring lives in Model */ +static uint16_t* trc_ring(int rank); /* forward: defined after the Model global below */ +static const char* const TRC_N[TRC_SITES] = {"x0", "h_attn", "h_mlp", "x_l", "h_fin"}; +/* Async copy of the LAST token row (T-1) of a bf16 [max_tok,n_embd] buffer into the pinned + * ring at (ring-row, site). The ring is indexed by (row, site), NOT by step: each step OVERWRITES + * the previous step's rows, so the scan at the top of step N+1 reads step N's data -- and must + * drain the stream first (step() does, trace-gated), because the engine's step_end sync does not + * wait for these out-of-band copies on the host build (the h_fin amax=0 the first traced run + * showed = a not-yet-landed copy read as zero). Pinned dst is mandatory (host.cpp:1860). Works on + * both backends: the host backend's rad_memcpy_async and rad_dev_alloc serve the same calls. */ +static inline void trc_copy(RadCtx* c, uint16_t* ring, int64_t n_embd, int row, int site, + rad_buf b, int64_t T) { +#if !defined(RAD_ARCH_NO_EXPORTS) + /* The arch_test harness (RAD_ARCH_NO_EXPORTS) compiles this file in without the runtime: it + * supplies the declare-phase ABI and nothing of the step-phase one, so the trace's runtime + * calls (rad_buf_ptr / rad_memcpy_async / rad_stream) exist only in a real build. */ + if (!trc_on() || !ring || T <= 0) return; + void* p = rad_buf_ptr(c, b); + if (!p) return; + rad_memcpy_async(ring + ((size_t)row * TRC_SITES + (size_t)site) * (size_t)n_embd, + (const uint16_t*)p + (T - 1) * (size_t)n_embd, n_embd * 2, rad_stream(c)); + /* Synchronous for the trace: the source buffer (w.h / b_x / b_h) is REUSED by a later op in + * the same step, so an async copy that lands after the reuse would read the wrong data (the + * host backend's h_attn site read as 0 / stale for this reason). Draining here -- right after + * the copy is issued, before the next op overwrites the buffer -- makes the ring hold the + * value at this point in the step. The cost is a sync per traced site (acceptable: the trace + * is a debug path, off by default). */ + rad_stream_sync(rad_stream(c)); +#endif +} + +/* layer_types is optional. When present it must name EVERY layer "full_attention" or + * "sliding_attention"; when absent the (l + 1) % interval == 0 fallback is the model card. */ +static int g4_schedule(const RadModelMeta* m, int64_t n, int64_t interval, + std::vector& is_global) { + const char* lt = rad_meta_gets(m, "layer_types", rad_meta_gets(m, "text_config.layer_types", nullptr)); + is_global.assign((size_t)n, 0); + if (lt && *lt) { + is_global.assign((size_t)n, -1); + size_t got = 0; + const char* p = lt; + while (*p) { + if (!std::strncmp(p, "full_attention", 14)) { + if (got >= (size_t)n) return RAD_E_INVAL; + is_global[got++] = 1; p += 14; + } else if (!std::strncmp(p, "sliding_attention", 17)) { + if (got >= (size_t)n) return RAD_E_INVAL; + is_global[got++] = 0; p += 17; + } else { + ++p; /* separators: whitespace, quotes, [ ] , and / */ + } + } + if (got != (size_t)n) return RAD_E_INVAL; + for (size_t i = 0; i < is_global.size(); ++i) if (is_global[i] < 0) return RAD_E_INVAL; + } else { + if (interval <= 0) { + fprintf(stderr, "radiance: gemma4: no layer_types and invalid full_attention_interval %lld\n", + (long long)interval); + return RAD_E_INVAL; + } + for (int64_t l = 0; l < n; ++l) is_global[(size_t)l] = ((l + 1) % interval == 0) ? 1 : 0; + } + return RAD_OK; +} + +/* The two attention classes share the residual stream and the FFN but differ in head geometry, + * the value path and the RoPE. The main Geom (from geom_from) is the slider; the global class is + * read from the container head and folded in here. */ +struct ClassGeom { + int64_t n_head = 0, n_head_kv = 0, head_dim = 0, n_rot = 0; + int64_t window = 0; + double theta = 1.0; + float scale = 1.0f; + bool head_ladder = false; /* true: proportional over head_dim (global) */ + bool has_v = true; /* false: k == v (global) */ + int64_t q_dim() const { return n_head * head_dim; } + int64_t kv_dim() const { return n_head_kv * head_dim; } + int64_t gqa() const { return n_head / n_head_kv; } +}; + +/* ---- one linear, plain bf16 or w4a8. The recipe re-quantises the GEMM weights to int4 codes + * with an f16 scale per [1 x 128] (group 128); a weight no rule matches stays bf16. This probes + * the weight's encoding and chooses the op by it: `gemm_nt` for a plain bf16 weight, `gemm_nt_q` + * at dtype w4a8 for the codes + their scale. w4a8 is the int8-activation grid: it reads an int8 + * activation + an f32 per-row scale, which the model's bf16 activation has to be quantised into. + * The quantiser is declared here (quant_act_i8) over the activation's bf16 plane and writes the + * int8 pair (q8, s8) the GEMM reads -- in the GEMM's fragment byte order, with one scale per row. + * quant_act_i8g (row-major, a per-(row,group) scale) is i8a8's producer, not this one's; pairing + * it with w4a8 is the compounding-into-inf seed of the all-NaN logits (see the declare comment). + * w4a16 was tried and is the WRONG grid: its row declares an f16 activation and + * its launcher never checks the operand dtype, so feeding it the model's bf16 bits reinterprets + * them as f16 -- a magnitude-compressed garbage stream (bf16 0.1 -> 1.45, 1.0 -> 1.875), finite + * nonsense that propagates through the residual. The name map is made here, before the weight is + * asked what it is (Builder::finish() needs the map entry before the declaration). */ +struct G4Linear { + rad_weight w = 0, ws = 0; /* the codes + scale (w4a8), or the single bf16 weight */ + 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 */ + + int declare(RadBuilder* b, Names& nm, const Geom& g, const char* base, + std::initializer_list src, + int64_t n_out, int64_t n_in, int shard, RadWeightGroup grp, + rad_buf a, rad_buf y, + std::initializer_list row_parts = {}); + void step(RadCtx* c, rad_buf a, rad_buf y, int64_t T) const; +}; + +inline int G4Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char* base, + std::initializer_list src, + int64_t n_out, int64_t n_in, int shard, RadWeightGroup grp, + rad_buf a, rad_buf y, + std::initializer_list row_parts) { + N = n_out; K = n_in; + if (N <= 0 || K <= 0 || src.size() == 0) return RAD_E_INVAL; + const char* wn = nm.f("%s.weight", base); + if (src.size() == 1) RAD_ARCH_TRY(map_copy(b, wn, *src.begin())); + else RAD_ARCH_TRY(map_concat(b, wn, src, 0)); + RadEncoding enc{}; + const bool known = weight_enc(b, wn, &enc); + /* a builder with no model behind it (a test, a synthetic tool) answers RAD_E_NOTFOUND, and a + * weight declared there is read as plain in its declared dtype -- the bf16 path. */ + const bool plain = !known || (rad_enc_is(&enc, "plain") && enc.n_planes == 1 && + enc.plane[0].dtype == RAD_BF16); + int64_t parts[RAD_MAX_ROW_PARTS] = {0}; + int np = 0; + for (int64_t p : row_parts) if (np < RAD_MAX_ROW_PARTS) parts[np++] = p; + + if (plain) { + w = decl_w(b, wn, RAD_BF16, { N, K }, RAD_ACCESS_PER_TOKEN, shard, grp, 0, row_parts); + if (!w) return RAD_E_INVAL; + op = rw(b, RAD_OP(b, "gemm_nt", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("N", N), RAD_INT("K", K), RAD_STR("dtype", g.dtype)), + RAD_WEIGHTS(w)), + { a }, { y }); + return op ? RAD_OK : RAD_E_INVAL; + } + + /* a w4 weight: i4 codes + an f16 scale per [1 x 128]. The GEMM (gemm_nt_q at w4a8) reads an + * int8 activation + an f32 per-row scale, which the model's bf16 activation has to be quantised + * into, so the quantiser (quant_act_i8g) is declared over the activation's bf16 plane `a` and + * writes the int8 pair (q8, s8) the GEMM reads. The codes + scale layout is byte-for-byte the + * w4a16 one -- the container serves unchanged; only the op dtype and the activation producer + * differ. */ + const RadEncPlane* sc = rad_enc_plane(&enc, "scale"); + const bool w4 = known && rad_enc_is(&enc, "affine") && enc.n_planes == 2 && !enc.transform[0] && + enc.plane[0].dtype == RAD_I4 && enc.plane[0].block[1] == 1 && + sc && sc->dtype == RAD_F16 && sc->block[0] == 1 && sc->block[1] == 128; + if (w4) { + if (N % 16 || K % 128) { + fprintf(stderr, "radiance: '%s' is %lldx%lld per rank; a w4 weight takes rows in " + "16s and K in whole 128-column scale groups.\n", base, + (long long)N, (long long)K); + return RAD_E_INVAL; + } + const char* sn = nm.f("%s.scale", base); + w = decl_view(b, wn, nullptr, "codes", RAD_I4, { N, K }, RAD_ACCESS_PER_TOKEN, shard, grp, parts, np); + ws = decl_view(b, sn, wn, "scale", RAD_F16, { N, K / 128 }, RAD_ACCESS_PER_TOKEN, shard, grp, parts, np); + if (!w || !ws) return RAD_E_INVAL; + 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, 1 }); + if (!q8 || !s8) return RAD_E_INVAL; + /* quant_act_i8, NOT quant_act_i8g: the w4a8 GEMM's A fragment reads a byte-shuffled plane + * (r4d_gemm_w4a8_nt_m64.hip:46-52, "the quantiser writes it") and applies an f32 per-ROW + * scale at the store (:61-63), so the producer must be the fragment-order per-row quantiser + * (r4d_quant_act_i8.hip:1-2). quant_act_i8g writes PLAIN ROW-MAJOR codes with a + * per-(row,group) scale (r4d_rows.cpp:2043-2046, "what the int8 trunk GEMMs (i8a8) read") + * -- pairing it with this GEMM shuffles the dot and broadcasts the wrong scale grid, the + * compounding-into-inf seed of the all-NaN logits. */ + /* group=K, NOT 128: the host quantisers (libref/libavx) are per-`group` -- one f32 scale + * per group of `group` elements -- while the device one (r4d_quant_act_i8) is per-ROW and + * ignores `group` in this 3-operand form (it only reads it in the asum form). group=K makes + * the host ngrp=1 (one scale per row, the [M,1] buffer the arch declares) and is a no-op on + * the device, so the same declaration is self-consistent on both backends. group=128 left + * the host quantiser refusing the [M,1] scale (it wants [M, K/128]). */ + op_q = rw(b, RAD_OP(b, "quant_act_i8", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", K), + RAD_INT("group", K), RAD_STR("dtype", "bf16")), + RAD_NOWEIGHTS), + { a }, { q8, s8 }); + 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", 128), RAD_STR("dtype", "w4a8")), + RAD_WEIGHTS(w, ws)), + { q8, s8 }, { y }); + quant = true; + return (op_q && op) ? RAD_OK : RAD_E_INVAL; + } + + char en[256]; + rad_enc_format(&enc, en, sizeof en); + fprintf(stderr, "radiance: '%s' is %s, and this model's linears are plain bf16 or w4a8 -- " + "neither matches\n", wn, en); + return RAD_E_DTYPE; +} + +inline void G4Linear::step(RadCtx* c, rad_buf a, rad_buf y, int64_t T) const { + if (quant) { + /* quantise the bf16 activation into the int8 pair (fragment order + a per-row scale), then + * read the pair. The scale plane is one f32 per row, browsed at T rows like the codes. + * quant_act_i8's schema is 4 operands -- x, q, scale, asum -- and `asum` (the per-group row + * sums the asymmetric grids subtract) is absent on this symmetric grid, so it is passed as + * a null tensor, not dropped: operands are positional (issue.cpp). */ + RAD_ISSUE_N(c, op_q, T, brows(a, T), brows(q8, T), brows(s8, T), RAD_NONE); + RAD_ISSUE_N(c, op, T, brows(q8, T), brows(s8, T), RAD_W(w), RAD_W(ws), + brows(y, T), RAD_NONE, RAD_NONE); + } else + RAD_ISSUE(c, op, brows(a, T), RAD_W(w), brows(y, T)); +} + +/* ---- one attention block, parameterised by class. */ +struct Gemma4Attn { + struct Wire { rad_buf x = 0, h = 0, q = 0, k = 0, v = 0, attn = 0; }; + + Wire w{}; + Geom g{}; + ClassGeom cls{}; + int layer = 0; + rad_kvgroup kv = 0; + + rad_weight w_norm = 0, w_qn = 0, w_kn = 0, w_post = 0; /* the norms (always bf16) */ + G4Linear ln_q{}, ln_k{}, ln_v{}, ln_o{}; /* the GEMM weights (bf16 or w4a8) */ + rad_op op_norm = 0, op_vn = 0, op_qn = 0, op_kn = 0, + op_rq = 0, op_rk = 0, op_attn = 0, op_kv = 0, op_post = 0, op_ar = 0, op_add = 0; + + int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const ClassGeom& cgeo, + rad_kvgroup group, const Wire& wire, const char* pre); + void step(RadCtx* c, const RadBatch* batch) const; +}; + +int Gemma4Attn::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const ClassGeom& cgeo, + rad_kvgroup group, const Wire& wire, const char* pre) { + g = geom; cls = cgeo; layer = l; kv = group; w = wire; + const int64_t qd = cls.q_dim(), kvd = cls.kv_dim(); + + /* the norms: always bf16 (no rule matches them), name map first. */ + RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.attn_norm.weight", l), nm.ckpt("%s.%lld.input_layernorm.weight", pre, (long long)l))); + RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.attn_q_norm.weight", l), nm.ckpt("%s.%lld.self_attn.q_norm.weight", pre, (long long)l))); + RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.attn_k_norm.weight", l), nm.ckpt("%s.%lld.self_attn.k_norm.weight", pre, (long long)l))); + RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.attn_post_norm.weight", l), nm.ckpt("%s.%lld.post_attention_layernorm.weight", pre, (long long)l))); + /* declared bf16, not f32: the checkpoint holds them bf16 and no rule re-quantises them, so an + * f32 declaration would widen them at load -- and a widened weight cannot be served from the + * mapped tier, which the whole layer group is pinned to because the w4a16 GEMM has no device + * kernel for the M > 64 prefill band. Reading the gain in its own (bf16) dtype is identical: + * the widening was a lossless upcast of the same bf16 value. */ + w_norm = decl_w(b, nm.f("blk.%d.attn_norm.weight", l), RAD_BF16, {g.n_embd}, + RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l)); + w_qn = decl_w(b, nm.f("blk.%d.attn_q_norm.weight", l), RAD_BF16, {cls.head_dim}, + RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l)); + w_kn = decl_w(b, nm.f("blk.%d.attn_k_norm.weight", l), RAD_BF16, {cls.head_dim}, + RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l)); + w_post = decl_w(b, nm.f("blk.%d.attn_post_norm.weight", l), RAD_BF16, {g.n_embd}, + RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l)); + + /* the GEMM weights: bf16 or w4a8, probed by G4Linear. */ + RAD_ARCH_TRY(ln_q.declare(b, nm, g, nm.f("blk.%d.attn_q", l), + { nm.ckpt("%s.%lld.self_attn.q_proj.weight_packed", pre, (long long)l) }, + qd, g.n_embd, RAD_SHARD_ROW, grp_layer(l), w.h, w.q)); + RAD_ARCH_TRY(ln_k.declare(b, nm, g, nm.f("blk.%d.attn_k", l), + { nm.ckpt("%s.%lld.self_attn.k_proj.weight_packed", pre, (long long)l) }, + kvd, g.n_embd, RAD_SHARD_ROW, grp_layer(l), w.h, w.k)); + if (cls.has_v) + RAD_ARCH_TRY(ln_v.declare(b, nm, g, nm.f("blk.%d.attn_v", l), + { nm.ckpt("%s.%lld.self_attn.v_proj.weight_packed", pre, (long long)l) }, + kvd, g.n_embd, RAD_SHARD_ROW, grp_layer(l), w.h, w.v)); + RAD_ARCH_TRY(ln_o.declare(b, nm, g, nm.f("blk.%d.attn_o", l), + { nm.ckpt("%s.%lld.self_attn.o_proj.weight_packed", pre, (long long)l) }, + g.n_embd, qd, RAD_SHARD_COL, grp_layer(l), w.attn, w.h)); + + /* input norm: x_hat * w over the residual stream. */ + op_norm = rw(b, RAD_OP(b, "rmsnorm", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd), + RAD_F64("eps", g.eps), RAD_F64("wadd", g.wadd), RAD_STR("dtype", g.dtype)), + RAD_WEIGHTS(w_norm)), + {w.x}, {w.h}); + + /* Gemma normalises the value with a WEIGHTLESS rms (the checkpoint has no v_norm gain). A + * sliding class normalises v in place; a k_eq_v class derives v from the RAW k into its own + * buffer, which must happen before k_norm rewrites k. */ + op_vn = rw(b, RAD_OP(b, "rmsnorm_p", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok * cls.n_head_kv), RAD_INT("n", cls.head_dim), + RAD_F64("eps", g.eps), RAD_STR("dtype", g.dtype)), + RAD_NOWEIGHTS), + {cls.has_v ? w.v : w.k}, {w.v}); + + op_qn = rw(b, RAD_OP(b, "rmsnorm", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok * cls.n_head), RAD_INT("n", cls.head_dim), + RAD_F64("eps", g.eps), RAD_F64("wadd", g.wadd_qk), RAD_STR("dtype", g.dtype)), + RAD_WEIGHTS(w_qn)), + {w.q}, {w.q}); + op_kn = rw(b, RAD_OP(b, "rmsnorm", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok * cls.n_head_kv), RAD_INT("n", cls.head_dim), + RAD_F64("eps", g.eps), RAD_F64("wadd", g.wadd_qk), RAD_STR("dtype", g.dtype)), + RAD_WEIGHTS(w_kn)), + {w.k}, {w.k}); + + /* sliding: standard RoPE over the full width, theta_s. global: proportional over the partial + * width (the "head" ladder), theta_g. q names its query heads; k its kv heads. */ + op_rq = rw(b, RAD_OP(b, "rope", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("head_dim", cls.head_dim), + RAD_INT("n_head", cls.n_head), RAD_INT("n_head_kv", 0), + RAD_F64("theta", cls.theta), RAD_F64("scale", cls.scale), + RAD_STR("mode", "neox"), RAD_INT("rotary_dim", cls.n_rot), + RAD_STR("ladder", cls.head_ladder ? "head" : "rotary")), + RAD_NOWEIGHTS), + {w.q}, {w.q}); + /* The rope kernel serves the fused QKV row and requires n_head > 0 (it rotates n_head + + * n_head_kv heads over a row of (n_head + 2*n_head_kv)*head_dim). The K heads are a separate + * buffer, so the K-only form is expressed as a Q-only form over the K heads: n_head is the + * count of heads to rotate (here n_head_kv) and n_head_kv is 0, so the row is + * n_head_kv*head_dim -- exactly the K buffer's width. RoPE is identical for Q and K (same + * theta, same positions), so this is the reference's separate K rope (llama.cpp ggml_rope_ext + * over the K buffer). */ + op_rk = rw(b, RAD_OP(b, "rope", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("head_dim", cls.head_dim), + RAD_INT("n_head", cls.n_head_kv), RAD_INT("n_head_kv", 0), + RAD_F64("theta", cls.theta), RAD_F64("scale", cls.scale), + RAD_STR("mode", "neox"), RAD_INT("rotary_dim", cls.n_rot), + RAD_STR("ladder", cls.head_ladder ? "head" : "rotary")), + RAD_NOWEIGHTS), + {w.k}, {w.k}); + + /* Gemma 4 scores Q.K WITHOUT the 1/sqrt(head_dim) factor: both the HF reference + * (Gemma4TextAttention: self.scaling = 1.0) and the vLLM oracle use scaling = 1.0 -- + * the learnable q/k RMSNorm weights absorb the scaling, so no extra 1/sqrt(d) is applied. + * attn_paged's default is 1/sqrt(head_dim), so pass 1.0 explicitly to match the reference. */ + op_attn = rw(b, RAD_OP(b, "attn_paged", + RAD_PARAMS(RAD_RANGE("q_len", 1, g.max_tok), RAD_INT("head_dim", cls.head_dim), + RAD_INT("gqa", cls.gqa()), RAD_INT("causal", 1), RAD_INT("window", cls.window), + RAD_INT("n_head", cls.n_head), RAD_INT("block_size", RAD_KV_BLOCK), + RAD_INT("max_seqs", g.max_seqs), RAD_INT("max_ctx", g.max_ctx), + RAD_F64("scale", 1.0), + RAD_STR("q_dtype", g.dtype), RAD_STR("kv_dtype", g.kv_dtype)), + RAD_NOWEIGHTS), + {w.q}, {w.attn}); + + op_kv = rw(b, RAD_OP(b, "kv_store", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("head_dim", cls.head_dim), + RAD_INT("n_head_kv", cls.n_head_kv), + RAD_INT("block_size", rad_kv_block_size(b, kv)), + RAD_STR("kv_dtype", g.kv_dtype)), + RAD_NOWEIGHTS), + {w.k, w.v}, {}); + + /* the SECOND residual: the attention output is itself normed (1 + w) before the add. */ + op_post = rw(b, RAD_OP(b, "rmsnorm", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd), + RAD_F64("eps", g.eps), RAD_F64("wadd", g.wadd), RAD_STR("dtype", g.dtype)), + RAD_WEIGHTS(w_post)), + {w.h}, {w.h}); + + if (g.world > 1) + op_ar = rw(b, RAD_OP(b, "all_reduce", + RAD_PARAMS(RAD_INT("world_size", g.world), RAD_RANGE("numel", g.n_embd, g.max_tok * g.n_embd), + RAD_STR("dtype", g.dtype), RAD_INT("exact", g.wire_exact), + RAD_INT("min_bytes", g.wire_min_bytes)), + RAD_NOWEIGHTS), + {w.h}, {w.h}); + + op_add = rw(b, RAD_OP(b, "add", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd), RAD_STR("dtype", g.dtype)), + RAD_NOWEIGHTS), + {w.x, w.h}, {w.x}); + return RAD_OK; +} + +void Gemma4Attn::step(RadCtx* c, const RadBatch* batch) const { + const RadKVGroupBatch* kvb = kv_batch(batch, kv); + const int32_t* slot = kvb ? kvb->slot_mapping : nullptr; + const int32_t* table = kvb ? kvb->block_table : nullptr; + const int32_t* used = kvb ? kvb->seqused : nullptr; + const int64_t T = batch->n_tok; + + RAD_ISSUE(c, op_norm, brows(w.x, T), RAD_W(w_norm), brows(w.h, T)); + ln_q.step(c, w.h, w.q, T); + ln_k.step(c, w.h, w.k, T); + if (cls.has_v) ln_v.step(c, w.h, w.v, T); + + /* normalise v FIRST: on a k_eq_v class v is derived from the raw k, before k_norm rewrites it. */ + RAD_ISSUE_N(c, op_vn, T * cls.n_head_kv, brows(cls.has_v ? w.v : w.k, T), brows(w.v, T)); + RAD_ISSUE_N(c, op_qn, T * cls.n_head, brows(w.q, T), RAD_W(w_qn), brows(w.q, T)); + RAD_ISSUE_N(c, op_kn, T * cls.n_head_kv, brows(w.k, T), RAD_W(w_kn), brows(w.k, T)); + + RAD_ISSUE(c, op_rq, brows(w.q, T), rope_posmc(g, batch, T)); + RAD_ISSUE(c, op_rk, brows(w.k, T), rope_posmc(g, batch, T)); + + RAD_ISSUE(c, op_kv, brows(w.k, T), brows(w.v, T), praw(slot, RAD_I32, T), kv_cache(kv, layer)); + + RAD_ISSUE_N(c, op_attn, batch->max_q_len, + brows(w.q, T), kv_cache(kv, layer), + praw2(table, RAD_I32, batch->n_seq, kvb ? kvb->block_table_pitch : 0), + praw(used, RAD_I32, batch->n_seq), + RAD_NONE, RAD_NONE, + praw(batch->cu_seqlens, RAD_I32, batch->n_seq + 1), + brows(w.attn, T)); + + ln_o.step(c, w.attn, w.h, T); + trc_copy(c, trc_ring(rad_rank(c)), g.n_embd, layer, 1, w.h, T); /* h_attn: pre-post-norm o-proj output */ + RAD_ISSUE_N(c, op_post, T, brows(w.h, T), RAD_W(w_post), brows(w.h, T)); + if (op_ar) RAD_ISSUE_N(c, op_ar, T * g.n_embd, brows(w.h, T), RAD_NONE); + RAD_ISSUE(c, op_add, brows(w.x, T), brows(w.h, T), brows(w.x, T)); +} + +/* ---- the feed-forward: GELU (not SwiGLU), with a second norm on the down-projection output. */ +struct Gemma4Mlp { + struct Wire { rad_buf x = 0, h = 0, gu = 0, ffn = 0; }; + + Wire w{}; + Geom g{}; + int layer = 0; + rad_weight w_norm = 0, w_post = 0; /* the norms (always bf16) */ + G4Linear ln_gu{}, ln_down{}; /* the GEMM weights (bf16 or w4a8) */ + rad_op op_norm = 0, op_act = 0, op_post = 0, op_ar = 0, op_add = 0; + + int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const Wire& wire, const char* pre); + void step(RadCtx* c, const RadBatch* batch) const; +}; + +int Gemma4Mlp::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const Wire& wire, const char* pre) { + g = geom; layer = l; w = wire; + + /* the norms: always bf16 (no rule matches them), name map first. */ + RAD_ARCH_TRY(map_copy (b, nm.f("blk.%d.ffn_norm.weight", l), nm.ckpt("%s.%lld.pre_feedforward_layernorm.weight", pre, (long long)l))); + RAD_ARCH_TRY(map_copy (b, nm.f("blk.%d.ffn_post_norm.weight", l), nm.ckpt("%s.%lld.post_feedforward_layernorm.weight", pre, (long long)l))); + /* bf16, not f32: same reason as the attention norms -- an f32 declaration widens them and a + * widened weight cannot be served from the mapped tier the whole layer group is pinned to. */ + w_norm = decl_w(b, nm.f("blk.%d.ffn_norm.weight", l), RAD_BF16, {g.n_embd}, + RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l)); + w_post = decl_w(b, nm.f("blk.%d.ffn_post_norm.weight", l), RAD_BF16, {g.n_embd}, + RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l)); + + /* the GEMM weights: bf16 or w4a8, probed by G4Linear. gate_up is the gate|up fusion. */ + RAD_ARCH_TRY(ln_gu.declare(b, nm, g, nm.f("blk.%d.ffn_gate_up", l), + { nm.ckpt("%s.%lld.mlp.gate_proj.weight_packed", pre, (long long)l), + nm.ckpt("%s.%lld.mlp.up_proj.weight_packed", pre, (long long)l) }, + 2 * g.n_ff, g.n_embd, RAD_SHARD_ROW, grp_layer(l), w.h, w.gu, + { g.n_ff, g.n_ff })); + RAD_ARCH_TRY(ln_down.declare(b, nm, g, nm.f("blk.%d.ffn_down", l), + { nm.ckpt("%s.%lld.mlp.down_proj.weight_packed", pre, (long long)l) }, + g.n_embd, g.n_ff, RAD_SHARD_COL, grp_layer(l), w.ffn, w.h)); + + op_norm = rw(b, RAD_OP(b, "rmsnorm", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd), + RAD_F64("eps", g.eps), RAD_F64("wadd", g.wadd), RAD_STR("dtype", g.dtype)), + RAD_WEIGHTS(w_norm)), + {w.x}, {w.h}); + op_act = rw(b, RAD_OP(b, "gelu_mul", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_ff), RAD_STR("dtype", g.dtype)), + RAD_NOWEIGHTS), + {w.gu}, {w.ffn}); + op_post = rw(b, RAD_OP(b, "rmsnorm", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd), + RAD_F64("eps", g.eps), RAD_F64("wadd", g.wadd), RAD_STR("dtype", g.dtype)), + RAD_WEIGHTS(w_post)), + {w.h}, {w.h}); + if (g.world > 1) + op_ar = rw(b, RAD_OP(b, "all_reduce", + RAD_PARAMS(RAD_INT("world_size", g.world), RAD_RANGE("numel", g.n_embd, g.max_tok * g.n_embd), + RAD_STR("dtype", g.dtype), RAD_INT("exact", g.wire_exact), + RAD_INT("min_bytes", g.wire_min_bytes)), + RAD_NOWEIGHTS), + {w.h}, {w.h}); + op_add = rw(b, RAD_OP(b, "add", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd), RAD_STR("dtype", g.dtype)), + RAD_NOWEIGHTS), + {w.x, w.h}, {w.x}); + return RAD_OK; +} + +void Gemma4Mlp::step(RadCtx* c, const RadBatch* batch) const { + const int64_t T = batch->n_tok; + RAD_ISSUE(c, op_norm, brows(w.x, T), RAD_W(w_norm), brows(w.h, T)); + ln_gu.step(c, w.h, w.gu, T); + RAD_ISSUE(c, op_act, brows(w.gu, T), brows(w.ffn, T)); + ln_down.step(c, w.ffn, w.h, T); + trc_copy(c, trc_ring(rad_rank(c)), g.n_embd, layer, 2, w.h, T); /* h_mlp: pre-post-norm down-proj output */ + RAD_ISSUE_N(c, op_post, T, brows(w.h, T), RAD_W(w_post), brows(w.h, T)); + if (op_ar) RAD_ISSUE_N(c, op_ar, T * g.n_embd, brows(w.h, T), RAD_NONE); + RAD_ISSUE(c, op_add, brows(w.x, T), brows(w.h, T), brows(w.x, T)); +} + +struct Layer { + bool global = false; + Gemma4Attn attn{}; + Gemma4Mlp mlp{}; + rad_weight w_scalar = 0; /* the per-layer [1] residual scalar, `cur = cur * scalar` */ + rad_op op_scale = 0; +}; + +struct Model { + Names nm{""}; + Geom g{}; + ClassGeom cs{}; /* sliding class */ + ClassGeom cg{}; /* global class */ + int64_t n_main = 0; + + rad_kvgroup kv_s = 0, kv_g = 0; + + rad_buf b_x = 0, b_h = 0, b_hout = 0, b_logits = 0; + rad_buf b_qs = 0, b_ks = 0, b_vs = 0, b_attns = 0; /* sliding rows */ + rad_buf b_qg = 0, b_kg = 0, b_vg = 0, b_atting = 0; /* global rows */ + rad_buf b_gu = 0, b_ffn = 0; + + rad_weight w_tok = 0, w_out = 0, w_lm = 0; + rad_op op_embed = 0, op_embed_ar = 0, op_final = 0, op_gather = 0, op_logits = 0, op_cap = 0; + + std::vector layers; + + uint16_t* trc = nullptr; /* pinned ring [n_main+1][TRC_SITES][n_embd] bf16; set only when tracing */ + bool trc_done = false; +}; + +static Model g_model[MAX_RANKS]; + +static uint16_t* trc_ring(int rank) { return g_model[rank].trc; } + +/* bf16 bit classification: u = raw << 16; exp7 = u & 0x7F80; inf = exp7 && !frac; nan = exp7 && frac. + * amax is over the NaN-free lanes only, so an all-NaN row prints amax=0 (the engine's -vv signature). */ +static void trc_scan_print(const Model& m) { + if (!m.trc) { fprintf(stderr, "radiance: resid trace: no ring\n"); return; } + const int rows = (int)m.n_main + 1; + const int64_t n = m.g.n_embd; + for (int pass = 0; pass < 2; ++pass) { /* pass 0: first inf; pass 1: first nan */ + for (int r = 0; r < rows && pass >= 0; ++r) { + for (int s = 0; s < TRC_SITES; ++s) { + const uint16_t* row = m.trc + ((size_t)r * TRC_SITES + (size_t)s) * (size_t)n; + int n_nan = 0, n_inf = 0; double amax = 0.0; + for (int64_t i = 0; i < n; ++i) { + const uint32_t u = (uint32_t)row[i] << 16; + float f; std::memcpy(&f, &u, 4); + const bool spe = (u & 0x7F80u) == 0x7F80u; + if (spe && (u & 0x007Fu)) ++n_nan; + else if (spe) ++n_inf; + else { const double a = std::fabs((double)f); if (a > amax) amax = a; } + } + const bool hit = pass ? n_nan > 0 : n_inf > 0; + if (!hit) continue; + fprintf(stderr, "TRC %s layer %2d site %-6s: nan=%d inf=%d amax=%.4g\n", + pass ? "NAN" : "INF", r, TRC_N[s], n_nan, n_inf, amax); + break; + } + } + } + /* Always-on summary, so an all-finite run still proves the ring was filled (a trace whose + * copies never land would print amax=0 here, not silence). */ + auto site_amax = [&](int r, int s) { + const uint16_t* row = m.trc + ((size_t)r * TRC_SITES + (size_t)s) * (size_t)n; + double a = 0.0; + for (int64_t i = 0; i < n; ++i) { + const uint32_t u = (uint32_t)row[i] << 16; + if ((u & 0x7F80u) == 0x7F80u) return -1.0; /* inf or nan: say so, do not classify */ + float f; std::memcpy(&f, &u, 4); + const double v = std::fabs((double)f); + if (v > a) a = v; + } + return a; + }; + /* The ring rows: 0 = x0 + layer 0 (sites 1-3), 1..n_main-1 = the layers, n_main = h_fin. + * -1.0 = an inf/nan in the row (the bisection signal); 0 = a zero row (a not-landed copy + * after the sync fix means the sync is broken, not the data). */ + fprintf(stderr, + "TRC summary: x0 %.4g | L0 h_attn %.4g h_mlp %.4g x_l %.4g | L%d x_l %.4g | h_fin %.4g\n", + site_amax(0, 0), + site_amax(0, 1), site_amax(0, 2), site_amax(0, 3), + rows - 2, site_amax(rows - 2, 3), + site_amax(rows - 1, 4)); +} + +static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* ctx) { + if (!b || !meta || !ctx) return RAD_E_INVAL; + if (ctx->rank < 0 || ctx->rank >= MAX_RANKS) return RAD_E_INVAL; + + Model& m = g_model[ctx->rank]; + m = Model{}; + m.nm = Names(ctx->scope); + RAD_ARCH_TRY(geom_from(m.g, meta, ctx, "bf16", RAD_BF16)); + + if (meta->n_expert > 0) { + fprintf(stderr, "radiance: gemma4_bf16 refuses a checkpoint with %lld experts; that is not this arch\n", + (long long)meta->n_expert); + return RAD_E_UNSUPPORTED; + } + + Geom& g = m.g; + Names& nm = m.nm; + + /* ---- geometry RadModelMeta does not name. */ + const int64_t kv_g_all = g4_i(meta, "num_global_key_value_heads", 0); + const int64_t head_g = g4_i(meta, "global_head_dim", 0); + const int64_t sliding_win = g4_i(meta, "sliding_window", 0); + const int64_t kv_shared = g4_i(meta, "num_kv_shared_layers", 0); + const int64_t interval = g4_i(meta, "full_attention_interval", 0); + const double theta_s = g4_f(meta, "rope_parameters.sliding_attention.rope_theta", 0.0); + const double theta_g = g4_f(meta, "rope_parameters.full_attention.rope_theta", 0.0); + const double softcap = g4_f(meta, "final_logit_softcapping", 0.0); + double prf = g4_f(meta, "rope_parameters.full_attention.partial_rotary_factor", -1.0); + + if (head_g <= 0 || kv_g_all <= 0) { + fprintf(stderr, "radiance: gemma4: global_head_dim %lld / num_global_key_value_heads %lld; both required\n", + (long long)head_g, (long long)kv_g_all); + return RAD_E_INVAL; + } + if (head_g & 1) { fprintf(stderr, "radiance: gemma4: global head_dim %lld is odd\n", (long long)head_g); return RAD_E_INVAL; } + if (!(theta_s > 0.0) || !(theta_g > 0.0)) { + fprintf(stderr, "radiance: gemma4: both rope_parameters...rope_theta are required\n"); + return RAD_E_INVAL; + } + if (!(softcap > 0.0)) { fprintf(stderr, "radiance: gemma4: final_logit_softcapping is required\n"); return RAD_E_INVAL; } + if (kv_shared < 0) { fprintf(stderr, "radiance: gemma4: num_kv_shared_layers %lld is negative\n", (long long)kv_shared); return RAD_E_INVAL; } + if (kv_shared > 0) { + fprintf(stderr, "radiance: gemma4: num_kv_shared_layers %lld needs a shared-KV reuse path; refusing\n", + (long long)kv_shared); + return RAD_E_UNSUPPORTED; + } + if (sliding_win <= 0) { fprintf(stderr, "radiance: gemma4: sliding_window %lld is required\n", (long long)sliding_win); return RAD_E_INVAL; } + if (!(prf > 0.0)) prf = 1.0; + const int64_t n_rot_g = (int64_t)(head_g * prf + 0.5); + if (n_rot_g <= 0 || n_rot_g > head_g || (n_rot_g & 1)) { + fprintf(stderr, "radiance: gemma4: global rotary width %lld out of range for head_dim %lld\n", + (long long)n_rot_g, (long long)head_g); + return RAD_E_INVAL; + } + + int64_t kv_g = 0; + if (divide_or_fail(kv_g_all, g.world, "num_global_key_value_heads", &kv_g) < 0) return RAD_E_INVAL; + + /* Plain RMSNorm: x_hat * w, on every norm including the post-projection ones. Gemma 4 + * (unlike Gemma 2/3) does NOT use the (1 + w) form: the vLLM 0.27.1 oracle (which serves + * this checkpoint correctly) uses the stock RMSNorm (weight init 1.0), HF transformers' + * Gemma4RMSNorm multiplies by the weight with no +1 (weight init 1.0), and llama.cpp's + * gemma4 uses LLM_NORM_RMS with no load-time offset. wadd=0.0 = the plain form. */ + g.wadd = 0.0f; + g.wadd_qk = 0.0f; + const float final_wadd = 0.0f; + + m.n_main = g.n_layer; + std::vector is_global; + RAD_ARCH_TRY(g4_schedule(meta, g.n_layer, interval, is_global)); + int n_global = 0; + for (int v : is_global) n_global += v; + + /* the slider comes off geom_from (head_dim 256, 16 kv heads, full rotation, theta_s); the global + * class is built from the container head. */ + m.cs.n_head = g.n_head; m.cs.n_head_kv = g.n_head_kv; m.cs.head_dim = g.head_dim; + m.cs.n_rot = g.head_dim; m.cs.window = sliding_win; m.cs.theta = theta_s; + m.cs.scale = g.rope_scale; m.cs.head_ladder = false; m.cs.has_v = true; + m.cg.n_head = g.n_head; m.cg.n_head_kv = kv_g; m.cg.head_dim = head_g; + m.cg.n_rot = n_rot_g; m.cg.window = 0; m.cg.theta = theta_g; + m.cg.scale = g.rope_scale; m.cg.head_ladder = true; m.cg.has_v = false; + + /* ---- the two KV groups. */ + const uint32_t kvdt = rad_kv_cache_dtype(ctx, RAD_BF16); + RadKVGroupDecl sd{}; + sd.kind = RAD_KV_WINDOW; sd.dtype = kvdt; sd.window = sliding_win; + sd.n_head_kv = g.n_head_kv; sd.head_dim = g.head_dim; + m.kv_s = rad_decl_kv_group(b, nm.f("kv_sliding"), &sd); + RadKVGroupDecl gd{}; + gd.kind = RAD_KV_FULL; gd.dtype = kvdt; gd.window = 0; + gd.n_head_kv = kv_g; gd.head_dim = head_g; + m.kv_g = rad_decl_kv_group(b, nm.f("kv_global"), &gd); + g.kv_dtype = rad_dtype_name(kvdt); + if (m.kv_s == 0 || m.kv_g == 0) { fprintf(stderr, "radiance: gemma4: a KV group failed to declare\n"); return RAD_E_INVAL; } + + /* ---- buffers. The two classes have different row widths, so they keep their own rows. */ + m.b_x = decl_b(b, nm.f("x"), g.act_dtype, {g.max_tok, g.n_embd}); + m.b_h = decl_b(b, nm.f("h"), g.act_dtype, {g.max_tok, g.n_embd}); + m.b_qs = decl_b(b, nm.f("attn_qs"), g.act_dtype, {g.max_tok, m.cs.q_dim()}); + m.b_ks = decl_b(b, nm.f("attn_ks"), g.act_dtype, {g.max_tok, m.cs.kv_dim()}); + m.b_vs = decl_b(b, nm.f("attn_vs"), g.act_dtype, {g.max_tok, m.cs.kv_dim()}); + m.b_attns = decl_b(b, nm.f("attn_outs"), g.act_dtype, {g.max_tok, m.cs.q_dim()}); + m.b_qg = decl_b(b, nm.f("attn_qg"), g.act_dtype, {g.max_tok, m.cg.q_dim()}); + m.b_kg = decl_b(b, nm.f("attn_kg"), g.act_dtype, {g.max_tok, m.cg.kv_dim()}); + m.b_vg = decl_b(b, nm.f("attn_vg"), g.act_dtype, {g.max_tok, m.cg.kv_dim()}); + m.b_atting= decl_b(b, nm.f("attn_outg"), g.act_dtype, {g.max_tok, m.cg.q_dim()}); + m.b_gu = decl_b(b, nm.f("gate_up"), g.act_dtype, {g.max_tok, 2 * g.n_ff}); + m.b_ffn = decl_b(b, nm.f("ffn"), g.act_dtype, {g.max_tok, g.n_ff}); + m.b_hout = decl_b(b, nm.f("h_out"), g.act_dtype, {g.max_logit_rows, g.n_embd}); + m.b_logits = decl_b(b, nm.f("logits"), RAD_F32, {g.max_logit_rows, g.n_vocab}); + RAD_ARCH_TRY(rad_declare_logits(b, m.b_logits)); + + /* ---- the vocabulary edges. Gemma 4 ties the head to the embedding. */ + const char* pre = "model.language_model.layers"; + RAD_ARCH_TRY(map_copy(b, nm.f("token_embd.weight"), nm.ckpt("model.language_model.embed_tokens.weight"))); + RAD_ARCH_TRY(map_copy(b, nm.f("output_norm.weight"), nm.ckpt("model.language_model.norm.weight"))); + RAD_ARCH_TRY(map_copy_alt(b, nm.f("output.weight"), nm.ckpt("lm_head.weight"), nm.ckpt("model.language_model.embed_tokens.weight"))); + m.w_tok = decl_w(b, nm.f("token_embd.weight"), RAD_BF16, {g.n_vocab, g.n_embd}, + RAD_ACCESS_VOCAB, RAD_SHARD_ROW, grp_model()); + /* bf16, not f32: the model group is pinned to the mapped tier (token_embd's embed_lookup is + * host-only), so an f32-declared output_norm would widen and fail to serve from the mapping. */ + m.w_out = decl_w(b, nm.f("output_norm.weight"), RAD_BF16, {g.n_embd}, + RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model()); + m.w_lm = decl_w(b, nm.f("output.weight"), RAD_BF16, {g.n_vocab, g.n_embd}, + RAD_ACCESS_VOCAB, RAD_SHARD_ROW, grp_model()); + + /* The embedding is scaled by sqrt(n_embd) at the LOOKUP, not baked into the table (the + * reference's build_inp_embd and HF's ScaledWordEmbedding both do it there), and the + * checkpoint's table is plain -- so the scale rides the op and the table stays tied to the + * head's. Absent it, the residual stream carries a 73x-too-small token signal and the first + * layer's scale-invariant norm hides nothing downstream. */ + m.op_embed = rw(b, RAD_OP(b, "embed_lookup", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n_embd", g.n_embd), + RAD_INT("n_vocab", g.n_vocab), RAD_STR("dtype", g.dtype), + RAD_INT("vocab_offset", g.vocab_off), + RAD_F64("mult", std::sqrt((double)g.n_embd))), + RAD_WEIGHTS(m.w_tok)), + {}, {m.b_x}); + if (g.world > 1) + m.op_embed_ar = rw(b, RAD_OP(b, "all_reduce", + RAD_PARAMS(RAD_INT("world_size", g.world), + RAD_RANGE("numel", g.n_embd, g.max_tok * g.n_embd), + RAD_STR("dtype", g.dtype), RAD_INT("exact", 1)), + RAD_NOWEIGHTS), + {m.b_x}, {m.b_x}); + + /* ---- the layers. This loop is the model card. */ + Gemma4Attn::Wire sw{}; sw.x = m.b_x; sw.h = m.b_h; sw.q = m.b_qs; sw.k = m.b_ks; sw.v = m.b_vs; sw.attn = m.b_attns; + Gemma4Attn::Wire gw{}; gw.x = m.b_x; gw.h = m.b_h; gw.q = m.b_qg; gw.k = m.b_kg; gw.v = m.b_vg; gw.attn = m.b_atting; + Gemma4Mlp::Wire mw{}; mw.x = m.b_x; mw.h = m.b_h; mw.gu = m.b_gu; mw.ffn = m.b_ffn; + + m.layers.resize((size_t)m.n_main); + for (int64_t l = 0; l < m.n_main; ++l) { + Layer& lay = m.layers[(size_t)l]; + lay.global = (is_global[(size_t)l] == 1); + const ClassGeom& cgeo = lay.global ? m.cg : m.cs; + const rad_kvgroup kg = lay.global ? m.kv_g : m.kv_s; + const Gemma4Attn::Wire& aw = lay.global ? gw : sw; + RAD_ARCH_TRY(rad_bind_layer_kv(b, (int)l, kg)); + RAD_ARCH_TRY(lay.attn.declare(b, nm, g, (int)l, cgeo, kg, aw, pre)); + RAD_ARCH_TRY(lay.mlp.declare(b, nm, g, (int)l, mw, pre)); + + /* the per-layer scalar: `cur = cur * scalar`, a [1] bf16 over the residual stream. */ + RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.layer_scalar", (int)l), + nm.ckpt("%s.%lld.layer_scalar", pre, (long long)l))); + lay.w_scalar = decl_w(b, nm.f("blk.%d.layer_scalar", (int)l), RAD_BF16, {1}, + RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer((int)l)); + lay.op_scale = rw(b, RAD_OP(b, "scale", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd), + RAD_STR("dtype", g.dtype)), + RAD_WEIGHTS(lay.w_scalar)), + {mw.x}, {mw.x}); + } + + /* ---- the tail: final norm, the sampled rows, the lm_head, and the soft-cap. */ + m.op_final = rw(b, RAD_OP(b, "rmsnorm", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd), + RAD_F64("eps", g.eps), RAD_F64("wadd", final_wadd), RAD_STR("dtype", g.dtype)), + RAD_WEIGHTS(m.w_out)), + {m.b_x}, {m.b_h}); + m.op_gather = rw(b, RAD_OP(b, "gather_rows", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), RAD_INT("n", g.n_embd), RAD_STR("dtype", g.dtype)), + RAD_NOWEIGHTS), + {m.b_h}, {m.b_hout}); + m.op_logits = rw(b, RAD_OP(b, "logits_gemm", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), RAD_INT("n_vocab", g.n_vocab), + RAD_INT("n_embd", g.n_embd), RAD_STR("dtype", g.dtype)), + RAD_WEIGHTS(m.w_lm)), + {m.b_hout}, {m.b_logits}); + m.op_cap = rw(b, RAD_OP(b, "logit_softcap", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), RAD_INT("n", g.n_vocab), + RAD_F64("cap", softcap), RAD_STR("dtype", "f32")), + RAD_NOWEIGHTS), + {m.b_logits}, {m.b_logits}); + + rad_note(b, "rank %d/%d: %lld layers (%lld global, %lld sliding), sliding window %lld, " + "slider %lld heads x %lld, global %lld heads x %lld, soft-capped logits", + g.rank, g.world, (long long)g.n_layer, (long long)n_global, + (long long)(g.n_layer - n_global), (long long)sliding_win, + (long long)g.n_head, (long long)g.head_dim, (long long)g.n_head, (long long)head_g); +#if !defined(RAD_ARCH_NO_EXPORTS) + if (trc_on()) { + const int64_t bytes = (m.n_main + 1) * TRC_SITES * g.n_embd * 2; + m.trc = (uint16_t*)rad_dev_alloc(bytes, RAD_MEM_HOST_PINNED); + fprintf(stderr, "radiance: gemma4 resid trace ring %s (%lld bytes)\n", + m.trc ? "pinned" : "FAILED", (long long)bytes); + } +#endif + return RAD_OK; +} + +static void step(RadCtx* c, const RadBatch* batch) { + Model& m = g_model[rad_rank(c)]; + /* Scan on every step from the second on: the ring holds the PREVIOUS step's rows. The + * previous step's copies (x0 early, h_fin last) are drained HERE before the read: the + * engine's step_end sync does not wait for the arch's out-of-band trc copies on the host + * build, so a not-yet-landed copy would read as zero (the h_fin amax=0 the first traced + * run showed). The sync is trace-gated: a normal run pays nothing. */ + if (trc_on() && m.trc) { + if (m.trc_done) { + rad_stream_sync(rad_stream(c)); + trc_scan_print(m); + } + m.trc_done = true; + } + + RAD_ISSUE(c, m.op_embed, + praw(batch->token_ids, RAD_I32, batch->n_tok), RAD_W(m.w_tok), + brows(m.b_x, batch->n_tok)); + if (m.op_embed_ar) + RAD_ISSUE_N(c, m.op_embed_ar, (int64_t)batch->n_tok * m.g.n_embd, + brows(m.b_x, batch->n_tok), RAD_NONE); + trc_copy(c, m.trc, m.g.n_embd, 0, 0, m.b_x, batch->n_tok); /* site x0: post-embedding */ + + for (const Layer& l : m.layers) { + l.attn.step(c, batch); + l.mlp.step(c, batch); + /* the per-layer scalar: `cur = cur * scalar`, the [1] bf16 over the residual stream, + * applied at the end of each layer (after the mlp add) -- exactly the vLLM oracle's + * `hidden_states = hidden_states * self.layer_scalar`. The checkpoint's values (0.03-0.99, + * the vLLM text-only loader remaps `model.language_model.` -> `model.` and loads the + * buffer, gemma4.py:1419/1644) are the trained weights: they rescale the residual toward + * O(1) (the RMSNorms normalise magnitude but keep direction, so the input signal survives + * through the branch, not the identity path). */ + RAD_ISSUE(c, l.op_scale, brows(m.b_x, batch->n_tok), RAD_W(l.w_scalar), + brows(m.b_x, batch->n_tok)); + trc_copy(c, m.trc, m.g.n_embd, l.attn.layer, 3, m.b_x, batch->n_tok); /* site x_l: post residual */ + } + + RAD_ISSUE(c, m.op_final, + brows(m.b_x, batch->n_tok), RAD_W(m.w_out), brows(m.b_h, batch->n_tok)); + trc_copy(c, m.trc, m.g.n_embd, (int)m.n_main, 4, m.b_h, batch->n_tok); /* site h_fin: final-norm, lm_head input */ + + if (batch->n_out > 0) { + RAD_ISSUE_N(c, m.op_gather, batch->n_out, + brows(m.b_h, batch->n_tok), + praw(batch->out_ids, RAD_I32, batch->n_out), + brows(m.b_hout, batch->n_out)); + RAD_ISSUE_N(c, m.op_logits, batch->n_out, + brows(m.b_hout, batch->n_out), RAD_W(m.w_lm), + brows(m.b_logits, batch->n_out)); + RAD_ISSUE_N(c, m.op_cap, batch->n_out, + brows(m.b_logits, batch->n_out), + brows(m.b_logits, batch->n_out)); + } +} + +#ifndef RAD_ARCH_NO_EXPORTS +RAD_ARCH_PLUGIN(gemma4_bf16, "gemma4", "compressed-tensors", "0.1.0", + "Gemma 4 dense (text-only), w4a8 from its CT-INT4 (compressed-tensors) release: " + "sliding-window and proportional-RoPE global attention over a GemmaRMSNorm stream " + "with GELU feed-forward, per-layer scalar scaling and soft-capped logits") +#endif + +} /* namespace gemma4_bf16 */ diff --git a/data/recipes/gemma4-31b-dense-w4a8.recipe b/data/recipes/gemma4-31b-dense-w4a8.recipe new file mode 100644 index 00000000..24f59b1f --- /dev/null +++ b/data/recipes/gemma4-31b-dense-w4a8.recipe @@ -0,0 +1,19 @@ +# gemma4-31b-dense-w4a8.recipe -- Gemma 4 31B dense (text), from its CT-INT4 (w4a16) +# checkpoint, as the w4a8 container serves it. +# +# rad-convert \ +# --recipe data/recipes/gemma4-31b-dense-w4a8.recipe -o gemma4-31b-dense-w4a8.rad +# +# The checkpoint's GEMM weights are CT-INT4 (group-32, a bf16 scale a 32 codes, no zero point); +# the converter unpacks them and re-quantises to group-128 int4 with an f16 scale. The norms +# (input / post-attention / pre- and post-feedforward layernorms, the q/k head norms) and the +# embedding + lm_head are bf16 and are kept as the checkpoint holds them -- a weight no rule +# matches is served in its checkpoint form. A rank-1 weight a pattern happens to match is declined +# by the quantiser and kept as it is, so the norms stay bf16 even under a broad pattern. + +blk.*.attn_q.weight rtn codes=i4 group=128 scale=f16 +blk.*.attn_k.weight rtn codes=i4 group=128 scale=f16 +blk.*.attn_v.weight rtn codes=i4 group=128 scale=f16 +blk.*.attn_o.weight rtn codes=i4 group=128 scale=f16 +blk.*.ffn_gate_up.weight rtn codes=i4 group=128 scale=f16 +blk.*.ffn_down.weight rtn codes=i4 group=128 scale=f16