Archived
rope + embed extensions: proportional-RoPE ladder, runtime embed scale (mult)
ladder param on rope/rope_table across ref/avx/r4d legs; embed_lookup gains its runtime scale under the canonical registry name (mult). Default-preserving: existing checkpoints replay unchanged (kbench rope fixture 44/44).
This commit is contained in:
1 parent
c37ce73d72
commit
8b825a941c
4 files changed
+349
-20
No files matched your search
+31
-5
@@ -1149,13 +1149,31 @@ static void rope_head(const RadTensor* t, int64_t base, int64_t ds, int64_t rot,
|
||||
}
|
||||
}
|
||||
|
||||
static float* inv_freqs(int64_t half, int64_t rot, float theta) {
|
||||
/* `den` is the width the base ladder is measured in: `rot` for a `rotary` ladder (a partial rotary
|
||||
* is a complete rotation of its own width) and `head_dim` for `head`, the proportional variant.
|
||||
* Shared by `rope` and `rope_table` because the two must compute the same angles. */
|
||||
static float* inv_freqs(int64_t half, int64_t den, float theta) {
|
||||
float* f = (float*)scratch_raw((size_t)half * sizeof(float));
|
||||
for (int64_t i = 0; i < half; ++i)
|
||||
f[i] = 1.0f / std::pow(theta, (float)(2 * i) / (float)rot);
|
||||
f[i] = 1.0f / std::pow(theta, (float)(2 * i) / (float)den);
|
||||
return f;
|
||||
}
|
||||
|
||||
/* `ladder` names the width the base frequency ladder is measured in -- see inv_freqs. Absent and
|
||||
* `rotary` divide by `rot` (every existing model); `head` divides by `head_dim`, which it needs. An
|
||||
* unknown value is a schema error, not a silent default. */
|
||||
static int rope_ladder(const RadArgs* a, int64_t rot, int64_t head_dim, int64_t* den) {
|
||||
const char* lad = p_str(a, "ladder", nullptr);
|
||||
if (!lad) { *den = rot; return RAD_OK; }
|
||||
if (!std::strcmp(lad, "rotary")) { *den = rot; return RAD_OK; }
|
||||
if (!std::strcmp(lad, "head")) {
|
||||
if (head_dim <= 0) return RAD_E_SHAPE;
|
||||
*den = head_dim;
|
||||
return RAD_OK;
|
||||
}
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
|
||||
AVX_KERNEL(rope) {
|
||||
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
|
||||
const RadTensor *qkv = &a->t[0], *pos = &a->t[1];
|
||||
@@ -1170,6 +1188,9 @@ AVX_KERNEL(rope) {
|
||||
if (mode < 0) return RAD_E_UNSUPPORTED;
|
||||
const int64_t rot = p_int(a, "rotary_dim", head_dim);
|
||||
if (head_dim <= 0 || n_head <= 0 || rot <= 0 || rot > head_dim || (rot & 1)) return RAD_E_SHAPE;
|
||||
int64_t den;
|
||||
const int lrc = rope_ladder(a, rot, head_dim, &den);
|
||||
if (lrc != RAD_OK) return lrc;
|
||||
|
||||
/* [M] one component a token, or [C, M] component-major with `sections`. */
|
||||
int64_t sec[8] = {};
|
||||
@@ -1194,7 +1215,7 @@ AVX_KERNEL(rope) {
|
||||
const int64_t rs = qkv->rank >= 2 ? qkv->stride[qkv->rank - 2] : stride_row;
|
||||
const int64_t ds = laststride(qkv);
|
||||
const int64_t half = rot / 2;
|
||||
float* invf = inv_freqs(half, rot, theta);
|
||||
float* invf = inv_freqs(half, den, theta);
|
||||
int comp[512] = {};
|
||||
if (half > 512) return RAD_E_UNSUPPORTED;
|
||||
if (multi) {
|
||||
@@ -1416,20 +1437,25 @@ AVX_KERNEL(qk_norm_rope) {
|
||||
AVX_KERNEL(rope_table) {
|
||||
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
|
||||
const RadTensor *pos = &a->t[0], *cs = &a->t[1];
|
||||
const int64_t rot = p_int(a, "rot", 0);
|
||||
const int64_t rot = p_int(a, "rot", 0);
|
||||
const int64_t head_dim = p_int(a, "head_dim", 0);
|
||||
const float theta = p_f32(a, "theta", 10000.0f);
|
||||
const float rscale = p_f32(a, "scale", 1.0f);
|
||||
if (rot < 2 || (rot & 1)) return RAD_E_SHAPE;
|
||||
int64_t den;
|
||||
const int lrc = rope_ladder(a, rot, head_dim, &den);
|
||||
if (lrc != RAD_OK) return lrc;
|
||||
const int64_t M = numel(pos);
|
||||
if (M <= 0 || numel(cs) % rot) return RAD_E_SHAPE;
|
||||
const int64_t rows = numel(cs) / rot;
|
||||
const int64_t half = rot / 2;
|
||||
float* invf = inv_freqs(half, rot, theta);
|
||||
float* invf = inv_freqs(half, den, theta);
|
||||
const RadYarn yarn = rad_yarn_args(a);
|
||||
if (!rad_yarn_valid(yarn) || (yarn.factor != 0.0f && rscale != 1.0f)) return RAD_E_INVAL;
|
||||
if (yarn.factor != 0.0f)
|
||||
for (int64_t i = 0; i < half; ++i) invf[i] = yarn.frequency(invf[i], (int)i);
|
||||
|
||||
|
||||
AVX_PARALLEL_FOR_IF(M * rot >= AVX_PAR_MIN)
|
||||
for (int64_t m = 0; m < M; ++m) {
|
||||
const int64_t pp = ldi(pos, offlin(pos, m));
|
||||
|
||||
@@ -201,11 +201,12 @@ __global__ __launch_bounds__(256) void r4d_qk_norm_rope_gate_kernel(
|
||||
// addresses are never read.
|
||||
//
|
||||
// THE ARITHMETIC IS r4d_model_bf16.hip's rope_kernel, LINE FOR LINE, and that is what makes the
|
||||
// fusion bit-for-bit rather than merely close: `1.0f / powf(theta, 2i/rot)`, the position divided
|
||||
// by `rscale` in f32, `sincosf`, and the products consumed in f32 by the caller. Changing any of
|
||||
// those here silently changes the model's output -- a different rope is a different model.
|
||||
// fusion bit-for-bit rather than merely close: `1.0f / powf(theta, 2i/den)`, where `den` is the
|
||||
// ladder's width (`rot` for `rotary`, `head_dim` for `head`), the position divided by `rscale`
|
||||
// in f32, `sincosf`, and the products consumed in f32 by the caller. Changing any of those here
|
||||
// silently changes the model's output -- a different rope is a different model.
|
||||
__global__ __launch_bounds__(64) void r4d_qk_rope_table_kernel(
|
||||
const void* __restrict__ POS, void* __restrict__ CS, long rows, int rot, float theta,
|
||||
const void* __restrict__ POS, void* __restrict__ CS, long rows, int rot, int den, float theta,
|
||||
float rscale, int cs_f32, int pos_i64, RadYarn yarn) {
|
||||
const int m = blockIdx.x;
|
||||
const long pp = pos_i64 ? ((const long*)POS)[m] : (long)((const int*)POS)[m];
|
||||
@@ -217,7 +218,7 @@ __global__ __launch_bounds__(64) void r4d_qk_rope_table_kernel(
|
||||
const float p = (float)pp / (rscale != 0.0f ? rscale : 1.0f);
|
||||
const size_t o = (size_t)pp * rot;
|
||||
for (int i = threadIdx.x; i < half; i += (int)blockDim.x) {
|
||||
const float inv = yarn.frequency(1.0f / powf(theta, (float)(2 * i) / (float)rot), i);
|
||||
const float inv = yarn.frequency(1.0f / powf(theta, (float)(2 * i) / (float)den), i);
|
||||
float c, s;
|
||||
sincosf(p * inv, &s, &c);
|
||||
if (yarn.factor != 0.0f) { c *= yarn.magnitude; s *= yarn.magnitude; }
|
||||
@@ -386,12 +387,25 @@ static int rad_rope_table_body(const RadArgs* args, const R4dSink& sk) {
|
||||
if (M < 1 || rot < 2 || (rot & 1)) return R4D_NO(RAD_E_SHAPE);
|
||||
if (r4d_numel(cs) % rot) return R4D_NO(RAD_E_SHAPE);
|
||||
|
||||
/* `ladder`: `rotary` (default) measures the base ladder in `rot`; `head` in `head_dim`, the
|
||||
* proportional variant. `rope_table` has no head_dim by default, so `head` needs the parameter. */
|
||||
const long long head_dim = r4d_geti_or(args, "head_dim", 0);
|
||||
const char* lad = r4d_gets(args, "ladder");
|
||||
long long den = rot;
|
||||
if (lad) {
|
||||
if (!std::strcmp(lad, "head")) {
|
||||
if (head_dim <= 0) return R4D_NO(RAD_E_SHAPE);
|
||||
den = head_dim;
|
||||
} else if (std::strcmp(lad, "rotary") && std::strcmp(lad, "head"))
|
||||
return R4D_NO(RAD_E_UNSUPPORTED);
|
||||
}
|
||||
|
||||
const float theta = (float)r4d_getf(args, "theta", 10000.0);
|
||||
const float rscale = (float)r4d_getf(args, "scale", 1.0);
|
||||
const RadYarn yarn = rad_yarn_args(args);
|
||||
if (!rad_yarn_valid(yarn) || (yarn.factor != 0.0f && rscale != 1.0f)) return R4D_NO(RAD_E_INVAL);
|
||||
return R4D_EMIT(sk, r4d_qk_rope_table_kernel, dim3((int)M), dim3(64), 0,
|
||||
r4d_p(po), r4d_wp(cs), (long)(r4d_numel(cs) / rot), (int)rot, theta, rscale,
|
||||
cs->dtype == RAD_F32 ? 1 : 0, po->dtype == RAD_I64 ? 1 : 0, yarn);
|
||||
r4d_p(po), r4d_wp(cs), (long)(r4d_numel(cs) / rot), (int)rot, (int)den, theta,
|
||||
rscale, cs->dtype == RAD_F32 ? 1 : 0, po->dtype == RAD_I64 ? 1 : 0, yarn);
|
||||
}
|
||||
R4D_ENTRY(r4d_rad_rope_table, rad_rope_table_body)
|
||||
+34
-8
@@ -395,8 +395,11 @@ struct RopeFreq {
|
||||
};
|
||||
|
||||
/* Which component and inverse frequency each of the rot/2 frequencies uses. */
|
||||
static int rope_freqs(int mode, int64_t rot, float theta, const int64_t* sec, int n_sec,
|
||||
RopeFreq* f) {
|
||||
/* `den` is the width the BASE ladder is measured in: `rot` for a `rotary` ladder (a partial rotary
|
||||
* is a complete rotation of its own width) and `head_dim` for `head`, the proportional variant. The
|
||||
* loop count and the NeoX/GPT-J pairing still come from `rot`; `axial` overrides `inv` below. */
|
||||
static int rope_freqs(int mode, int64_t rot, int64_t den, float theta, const int64_t* sec,
|
||||
int n_sec, RopeFreq* f) {
|
||||
const int64_t half = rot / 2;
|
||||
if (half > REF_HD_MAX / 2) return RAD_E_UNSUPPORTED;
|
||||
int64_t start[8] = {}, total = 0;
|
||||
@@ -404,7 +407,7 @@ static int rope_freqs(int mode, int64_t rot, float theta, const int64_t* sec, in
|
||||
if ((mode == ROPE_MROPE || mode == ROPE_AXIAL) && n_sec > 0 && total != half) return RAD_E_SHAPE;
|
||||
for (int64_t i = 0; i < half; ++i) {
|
||||
int c = 0;
|
||||
float inv = 1.0f / std::pow(theta, (float)(2 * i) / (float)rot);
|
||||
float inv = 1.0f / std::pow(theta, (float)(2 * i) / (float)den);
|
||||
if (n_sec > 0 && (mode == ROPE_MROPE || mode == ROPE_AXIAL)) {
|
||||
while (c + 1 < n_sec && i >= start[c + 1]) ++c;
|
||||
if (mode == ROPE_AXIAL) {
|
||||
@@ -450,6 +453,21 @@ static int rope_sections(const RadArgs* a, int64_t* sec) {
|
||||
return n;
|
||||
}
|
||||
|
||||
/* `ladder` names the width the base frequency ladder is measured in -- see rope_freqs. Absent and
|
||||
* `rotary` divide by `rot` (every existing model); `head` divides by `head_dim`, which it needs. An
|
||||
* unknown value is a schema error, not a silent default: a mistyped ladder is a silent wrong angle. */
|
||||
static int rope_ladder(const RadArgs* a, int64_t rot, int64_t head_dim, int64_t* den) {
|
||||
const char* lad = p_str(a, "ladder", nullptr);
|
||||
if (!lad) { *den = rot; return RAD_OK; }
|
||||
if (!std::strcmp(lad, "rotary")) { *den = rot; return RAD_OK; }
|
||||
if (!std::strcmp(lad, "head")) {
|
||||
if (head_dim <= 0) return RAD_E_SHAPE;
|
||||
*den = head_dim;
|
||||
return RAD_OK;
|
||||
}
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
int ref_rope(const RadArgs* a, RadStream) {
|
||||
@@ -466,6 +484,8 @@ int ref_rope(const RadArgs* a, RadStream) {
|
||||
if (mode < 0) return RAD_E_UNSUPPORTED;
|
||||
int64_t rot = p_int(a, "rotary_dim", head_dim);
|
||||
if (head_dim <= 0 || n_head <= 0 || rot <= 0 || rot > head_dim || (rot & 1)) return RAD_E_SHAPE;
|
||||
int64_t den;
|
||||
RAD_REF_TRY(rope_ladder(a, rot, head_dim, &den));
|
||||
|
||||
int64_t sec[8] = {};
|
||||
const int n_sec = rope_sections(a, sec);
|
||||
@@ -480,7 +500,7 @@ int ref_rope(const RadArgs* a, RadStream) {
|
||||
if (n_comp < need || n_comp > 8) return RAD_E_SHAPE;
|
||||
}
|
||||
RopeFreq f;
|
||||
RAD_REF_TRY(rope_freqs(mode, rot, theta, sec, multi ? n_sec : 0, &f));
|
||||
RAD_REF_TRY(rope_freqs(mode, rot, den, theta, sec, multi ? n_sec : 0, &f));
|
||||
const RadYarn yarn = rad_yarn_args(a);
|
||||
if (!rad_yarn_valid(yarn)) return RAD_E_INVAL;
|
||||
if (yarn.factor != 0.0f) {
|
||||
@@ -488,6 +508,7 @@ int ref_rope(const RadArgs* a, RadStream) {
|
||||
for (int64_t i = 0; i < rot / 2; ++i) f.inv[i] = yarn.frequency(f.inv[i], (int)i);
|
||||
}
|
||||
|
||||
|
||||
const int64_t stride_row = (n_head + 2 * n_head_kv) * head_dim;
|
||||
if (M <= 0 || numel(qkv) < M * stride_row) return RAD_E_SHAPE;
|
||||
const int64_t rs = qkv->rank >= 2 ? qkv->stride[qkv->rank - 2] : stride_row;
|
||||
@@ -582,7 +603,9 @@ int ref_qk_norm_rope(const RadArgs* a, RadStream) {
|
||||
const int64_t rs = qkv->rank >= 2 ? qkv->stride[qkv->rank - 2] : stride_row;
|
||||
const int64_t ds = laststride(qkv);
|
||||
RopeFreq f;
|
||||
if (!blocks) RAD_REF_TRY(rope_freqs(mode, rot, theta, nullptr, 0, &f));
|
||||
/* No `ladder` on this row yet: the fused norm+rope measures by `rot`, as it always has. */
|
||||
RAD_REF_TRY(rope_freqs(mode, rot, rot, theta, nullptr, 0, &f));
|
||||
|
||||
|
||||
#pragma omp parallel for schedule(static)
|
||||
for (int64_t m = 0; m < M; ++m) {
|
||||
@@ -746,10 +769,13 @@ int ref_attn_local_rel(const RadArgs* a, RadStream) {
|
||||
int ref_rope_table(const RadArgs* a, RadStream) {
|
||||
if (!have(a, 2)) return RAD_E_INVAL;
|
||||
const RadTensor *pos = &a->t[0], *cs = &a->t[1];
|
||||
const int64_t rot = p_int(a, "rot", 0);
|
||||
const int64_t rot = p_int(a, "rot", 0);
|
||||
const int64_t head_dim = p_int(a, "head_dim", 0);
|
||||
const float theta = p_f32(a, "theta", 10000.0f);
|
||||
const float rscale = p_f32(a, "scale", 1.0f);
|
||||
if (rot < 2 || (rot & 1)) return RAD_E_SHAPE;
|
||||
int64_t den;
|
||||
RAD_REF_TRY(rope_ladder(a, rot, head_dim, &den));
|
||||
const int64_t M = numel(pos);
|
||||
if (M <= 0 || numel(cs) % rot) return RAD_E_SHAPE;
|
||||
const int64_t rows = numel(cs) / rot;
|
||||
@@ -763,7 +789,7 @@ int ref_rope_table(const RadArgs* a, RadStream) {
|
||||
if (pp < 0 || pp >= rows) continue;
|
||||
const float p = (float)pp / (rscale != 0.0f ? rscale : 1.0f);
|
||||
for (int64_t i = 0; i < half; ++i) {
|
||||
const float inv = yarn.frequency(1.0f / std::pow(theta, (float)(2 * i) / (float)rot), (int)i);
|
||||
const float inv = yarn.frequency(1.0f / std::pow(theta, (float)(2 * i) / (float)den), (int)i);
|
||||
const float ang = p * inv;
|
||||
stt(cs, offlin(cs, pp * rot + i), std::cos(ang) * yarn.magnitude);
|
||||
stt(cs, offlin(cs, pp * rot + half + i), std::sin(ang) * yarn.magnitude);
|
||||
@@ -820,7 +846,7 @@ int ref_qsa_block_key(const RadArgs* a, RadStream) {
|
||||
if (!rad_yarn_valid(yarn) || (yarn.factor != 0.0f && (rot <= 0 || rscale != 1.0f)))
|
||||
return RAD_E_INVAL;
|
||||
RopeFreq f;
|
||||
RAD_REF_TRY(rope_freqs(mc ? mode : ROPE_NEOX, rot, theta, sec, mc ? n_sec : 0, &f));
|
||||
RAD_REF_TRY(rope_freqs(mc ? mode : ROPE_NEOX, rot, rot, theta, sec, mc ? n_sec : 0, &f));
|
||||
for (int64_t i = 0; i < rot / 2; ++i) f.inv[i] = yarn.frequency(f.inv[i], (int)i);
|
||||
|
||||
#pragma omp parallel for schedule(static)
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "rad_runtime.h"
|
||||
|
||||
#include <cstdarg>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
#include <set>
|
||||
@@ -296,6 +297,7 @@ extern "C" const RadBatch* rad_batch(RadCtx* c) { return c->batch; }
|
||||
extern "C" int rad_rank(RadCtx* c) { return c->rank; }
|
||||
extern "C" int rad_world_size(RadCtx* c) { return c->world; }
|
||||
extern "C" RadStream rad_stream(RadCtx* c) { (void)c; return nullptr; }
|
||||
extern "C" int rad_stream_sync(RadStream s) { (void)s; return RAD_OK; }
|
||||
extern "C" int rad_route_report(RadCtx* c, int layer, const RadRouting* r) {
|
||||
(void)r;
|
||||
c->routed_layers.push_back(layer);
|
||||
@@ -316,6 +318,8 @@ extern "C" void* rad_buf_ptr (RadCtx* c, rad_buf b) { (void)c; return (void
|
||||
#include "../arch/qwen4exp_fp8/qwen4exp_fp8.cpp"
|
||||
/* And the LLaMA family, for how it reads its rotary scaling. */
|
||||
#include "../arch/llama_fp8/llama_fp8.cpp"
|
||||
/* Gemma 4 dense: two attention classes (sliding-window + proportional-RoPE global) on one stream. */
|
||||
#include "../arch/gemma4_bf16/gemma4_bf16.cpp"
|
||||
/* The PLE block on its own: no architecture in this file declares it, and what its step() hands
|
||||
* the two windowed ops is a contract of its own (rad_block_ple.h). */
|
||||
#include <arch/rad_block_ple.h>
|
||||
@@ -363,6 +367,46 @@ RadModelMeta qwen35_meta() {
|
||||
return m;
|
||||
}
|
||||
|
||||
/* Gemma 4 31B's text geometry, cut to TWELVE layers so a failure names a real shape. Twelve is the
|
||||
* smallest count that lands two global layers at (l + 1) % 6 == 0 -- layers 5 and 11 -- over the ten
|
||||
* sliding ones, the 5:1 ratio the model card carries. head_dim 256 here is the SLIDING width; the
|
||||
* global class' 512 width and 4 heads are free-form header keys the plugin must read, not fields on
|
||||
* RadModelMeta. */
|
||||
const char* k_gem4_keys[] = {
|
||||
"sliding_window",
|
||||
"global_head_dim",
|
||||
"num_global_key_value_heads",
|
||||
"rope_parameters.sliding_attention.rope_theta",
|
||||
"rope_parameters.full_attention.rope_theta",
|
||||
"rope_parameters.full_attention.partial_rotary_factor",
|
||||
"num_kv_shared_layers",
|
||||
"final_logit_softcapping",
|
||||
"full_attention_interval",
|
||||
};
|
||||
const char* k_gem4_vals[] = { "1024", "512", "4", "10000", "1000000", "0.25", "0", "30", "6" };
|
||||
|
||||
RadModelMeta gemma4_meta() {
|
||||
RadModelMeta m{};
|
||||
m.arch_id = "gemma4";
|
||||
m.name = "test-gemma4-31b";
|
||||
m.quant = "bf16";
|
||||
m.n_layers = 12;
|
||||
m.n_embd = 5376;
|
||||
m.n_head = 32;
|
||||
m.n_head_kv = 16;
|
||||
m.head_dim = 256;
|
||||
m.n_ff = 21504;
|
||||
m.n_vocab = 262144;
|
||||
m.n_ctx_train = 262144;
|
||||
m.rms_eps = 1e-6f;
|
||||
m.rope_theta = 1e6f;
|
||||
m.rope_scale = 1.0f;
|
||||
m.n_kv = (int)(sizeof(k_gem4_keys) / sizeof(k_gem4_keys[0]));
|
||||
m.kv_key = k_gem4_keys;
|
||||
m.kv_val = k_gem4_vals;
|
||||
return m;
|
||||
}
|
||||
|
||||
RadBuildCtx build_ctx(int rank = 0, int world = 1) {
|
||||
RadBuildCtx c{};
|
||||
c.rank = rank;
|
||||
@@ -3086,3 +3130,222 @@ TEST(the_per_expert_tap_keeps_each_experts_gram_whole) {
|
||||
}
|
||||
CHECK_EQ(bad, 0);
|
||||
}
|
||||
|
||||
/* =============================== gemma4 dense: two attention classes =========================== */
|
||||
|
||||
TEST(declare_is_a_gemma4_trunk) {
|
||||
RadBuilder b;
|
||||
RadModelMeta meta = gemma4_meta();
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
CHECK_OK(gemma4_bf16::declare(&b, &meta, &ctx));
|
||||
|
||||
/* 12 layers: 10 sliding + 2 global (layers 5 and 11). */
|
||||
CHECK_EQ(count_op(b, "attn_paged"), 12);
|
||||
CHECK_EQ(count_op(b, "kv_store"), 12);
|
||||
CHECK_EQ(count_op(b, "rmsnorm_p"), 12); /* the weightless value normalisation, one per layer */
|
||||
CHECK_EQ(count_op(b, "rope"), 24); /* q + k */
|
||||
CHECK_EQ(count_op(b, "gelu_mul"), 12); /* Gemma's FFN activation, not SwiGLU */
|
||||
CHECK_EQ(count_op(b, "silu_mul"), 0);
|
||||
CHECK_EQ(count_op(b, "gemm_nt"), 70); /* 10x6 sliding (q,k,v,o,gu,down) + 2x5 global (no v) */
|
||||
CHECK_EQ(count_op(b, "rmsnorm"), 73); /* 4 attn + 2 ffn per layer, plus the final norm */
|
||||
CHECK_EQ(count_op(b, "add"), 24); /* the two GemmaRMSNorm residuals per layer */
|
||||
CHECK_EQ(count_op(b, "embed_lookup"), 1);
|
||||
CHECK_EQ(count_op(b, "gather_rows"), 1);
|
||||
CHECK_EQ(count_op(b, "logits_gemm"), 1);
|
||||
CHECK_EQ(count_op(b, "logit_softcap"), 1);
|
||||
CHECK_EQ(count_op(b, "all_reduce"), 0); /* world 1 */
|
||||
}
|
||||
|
||||
TEST(gemma4_declares_two_kv_groups_and_binds_the_schedule) {
|
||||
RadBuilder b;
|
||||
RadModelMeta meta = gemma4_meta();
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
CHECK_OK(gemma4_bf16::declare(&b, &meta, &ctx));
|
||||
|
||||
CHECK_EQ((int)b.kv_groups.size(), 2);
|
||||
CHECK_EQ((int)b.kv_groups[0].d.kind, RAD_KV_WINDOW); /* sliding, declared first */
|
||||
CHECK_EQ(b.kv_groups[0].d.window, 1024);
|
||||
CHECK_EQ(b.kv_groups[0].d.n_head_kv, 16);
|
||||
CHECK_EQ(b.kv_groups[0].d.head_dim, 256);
|
||||
CHECK_EQ((int)b.kv_groups[1].d.kind, RAD_KV_FULL); /* global */
|
||||
CHECK_EQ(b.kv_groups[1].d.window, 0);
|
||||
CHECK_EQ(b.kv_groups[1].d.n_head_kv, 4);
|
||||
CHECK_EQ(b.kv_groups[1].d.head_dim, 512);
|
||||
|
||||
/* (l + 1) % 6 == 0 -> global layers 5 and 11; the fake hands group #1 to sliding (declared
|
||||
* first) and #2 to global. */
|
||||
CHECK_EQ((int)b.binds.size(), 12);
|
||||
for (int l = 0; l < 12; ++l) {
|
||||
const bool want_global = ((l + 1) % 6 == 0);
|
||||
CHECK_EQ((int)b.binds[(size_t)l].first, l);
|
||||
CHECK_EQ((int)b.binds[(size_t)l].second, want_global ? 2 : 1);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(gemma4_attention_shapes_for_both_classes) {
|
||||
RadBuilder b;
|
||||
RadModelMeta meta = gemma4_meta();
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
CHECK_OK(gemma4_bf16::declare(&b, &meta, &ctx));
|
||||
|
||||
/* layer 0 (sliding): q 32x256, k/v 16x256, windowed. */
|
||||
{
|
||||
const RecOp* at = find_op(b, "attn_paged", 0);
|
||||
CHECK(at != nullptr);
|
||||
CHECK_EQ(pint(at, "head_dim"), 256);
|
||||
CHECK_EQ(pint(at, "gqa"), 2);
|
||||
CHECK_EQ(pint(at, "window"), 1024);
|
||||
CHECK_EQ(pint(at, "n_head"), 32);
|
||||
CHECK_EQ(pint(at, "block_size"), 16);
|
||||
CHECK_EQ(pstr(at, "q_dtype"), std::string("bf16"));
|
||||
}
|
||||
/* layer 5 (the first global, (5+1)%6==0): q 32x512, k 4x512, windowless. */
|
||||
{
|
||||
const RecOp* at = find_op(b, "attn_paged", 5);
|
||||
CHECK(at != nullptr);
|
||||
CHECK_EQ(pint(at, "head_dim"), 512);
|
||||
CHECK_EQ(pint(at, "gqa"), 8);
|
||||
CHECK_EQ(pint(at, "window"), 0);
|
||||
CHECK_EQ(pint(at, "n_head"), 32);
|
||||
}
|
||||
|
||||
/* rope: the sliding call is standard over the full 256 (theta 1e4); the global call is
|
||||
* proportional over 128 of 512 (the "head" ladder, theta 1e6). rope ops are declared q, k per
|
||||
* layer, so global index = 2*layer + (q ? 0 : 1). */
|
||||
{
|
||||
const RecOp* r = find_op(b, "rope", 0); /* layer 0, q */
|
||||
CHECK(r != nullptr);
|
||||
CHECK_EQ(pint(r, "head_dim"), 256);
|
||||
CHECK_EQ(pint(r, "rotary_dim"), 256);
|
||||
CHECK_NEAR(pflt(r, "theta"), 1e4, 1.0);
|
||||
CHECK_EQ(pstr(r, "ladder"), std::string("rotary"));
|
||||
CHECK_EQ(pint(r, "n_head"), 32);
|
||||
|
||||
const RecOp* rg = find_op(b, "rope", 10); /* layer 5, q */
|
||||
CHECK(rg != nullptr);
|
||||
CHECK_EQ(pint(rg, "head_dim"), 512);
|
||||
CHECK_EQ(pint(rg, "rotary_dim"), 128);
|
||||
CHECK_NEAR(pflt(rg, "theta"), 1e6, 1.0);
|
||||
CHECK_EQ(pstr(rg, "ladder"), std::string("head"));
|
||||
CHECK_EQ(pint(rg, "n_head"), 32);
|
||||
|
||||
/* the k rope is a separate buffer, served by the fused-row kernel as a q-only form over
|
||||
* the k heads: n_head is the head count to rotate (here n_head_kv = 4) and n_head_kv is 0. */
|
||||
const RecOp* rkg = find_op(b, "rope", 11); /* layer 5, k */
|
||||
CHECK(rkg != nullptr);
|
||||
CHECK_EQ(pint(rkg, "n_head"), 4);
|
||||
CHECK_EQ(pint(rkg, "n_head_kv"), 0);
|
||||
}
|
||||
|
||||
/* the weightless value normalisation reads 256 dims on a sliding layer, 512 on a global one;
|
||||
* and the global layer has no v_proj weight at all -- v is k. */
|
||||
{
|
||||
const RecOp* vn = find_op(b, "rmsnorm_p", 0); /* layer 0 */
|
||||
CHECK(vn != nullptr);
|
||||
CHECK_EQ(pint(vn, "n"), 256);
|
||||
const RecOp* vng = find_op(b, "rmsnorm_p", 5); /* layer 5 */
|
||||
CHECK(vng != nullptr);
|
||||
CHECK_EQ(pint(vng, "n"), 512);
|
||||
}
|
||||
CHECK(find_w(b, "blk.0.attn_v.weight") != nullptr); /* sliding has a v projection */
|
||||
CHECK(find_w(b, "blk.5.attn_v.weight") == nullptr); /* global: k == v */
|
||||
CHECK(find_w(b, "blk.5.attn_q.weight") != nullptr);
|
||||
CHECK(find_w(b, "blk.5.attn_o.weight") != nullptr);
|
||||
}
|
||||
|
||||
TEST(gemma4_final_norm_softcap_and_vocab) {
|
||||
RadBuilder b;
|
||||
RadModelMeta meta = gemma4_meta();
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
CHECK_OK(gemma4_bf16::declare(&b, &meta, &ctx));
|
||||
|
||||
const RecOp* cap = find_op(b, "logit_softcap", 0);
|
||||
CHECK(cap != nullptr);
|
||||
CHECK_EQ(pint(cap, "n"), 262144);
|
||||
CHECK_NEAR(pflt(cap, "cap"), 30.0, 1e-9);
|
||||
CHECK_EQ(pstr(cap, "dtype"), std::string("f32"));
|
||||
|
||||
const RecOp* lg = find_op(b, "logits_gemm", 0);
|
||||
CHECK(lg != nullptr);
|
||||
CHECK_EQ(pint(lg, "n_vocab"), 262144);
|
||||
CHECK_EQ(pint(lg, "n_embd"), 5376);
|
||||
|
||||
const RecWeight* emb = find_w(b, "token_embd.weight");
|
||||
CHECK(emb != nullptr);
|
||||
CHECK_EQ(emb->d.shape[0], 262144);
|
||||
CHECK_EQ(emb->d.shape[1], 5376);
|
||||
CHECK_EQ(emb->d.shard, RAD_SHARD_ROW);
|
||||
|
||||
/* lm_head is tied to the embedding. */
|
||||
CHECK(find_map(b, "output.weight") != nullptr);
|
||||
|
||||
/* 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), so the table
|
||||
* stays tied to the head's. The scale rides the op. */
|
||||
const RecOp* el = find_op(b, "embed_lookup", 0);
|
||||
CHECK(el != nullptr);
|
||||
CHECK_NEAR(pflt(el, "mult"), std::sqrt(5376.0), 1e-9);
|
||||
}
|
||||
|
||||
TEST(gemma4_every_op_declares_the_buffers_it_touches) {
|
||||
RadBuilder b;
|
||||
RadModelMeta meta = gemma4_meta();
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
CHECK_OK(gemma4_bf16::declare(&b, &meta, &ctx));
|
||||
for (const auto& o : b.ops)
|
||||
if (o.resolved) CHECK(o.uses_declared);
|
||||
}
|
||||
|
||||
TEST(gemma4_every_weight_has_a_name_map) {
|
||||
RadBuilder b;
|
||||
RadModelMeta meta = gemma4_meta();
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
CHECK_OK(gemma4_bf16::declare(&b, &meta, &ctx));
|
||||
for (const auto& w : b.weights)
|
||||
CHECK(find_map(b, w.name) != nullptr);
|
||||
}
|
||||
|
||||
TEST(a_gemma4_decode_step_runs_both_classes_in_order) {
|
||||
RadBuilder b;
|
||||
RadModelMeta meta = gemma4_meta();
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
CHECK_OK(gemma4_bf16::declare(&b, &meta, &ctx));
|
||||
|
||||
RadCtx c; c.b = &b; c.rank = 0; c.world = 1;
|
||||
RadBatch batch = decode_batch();
|
||||
c.batch = &batch;
|
||||
gemma4_bf16::step(&c, &batch);
|
||||
|
||||
std::vector<std::string> want = { "embed_lookup" };
|
||||
for (int l = 0; l < 12; ++l) {
|
||||
if ((l + 1) % 6 == 0) {
|
||||
/* global: k == v, so no v gemm -- the weightless v norm derives from k instead. */
|
||||
for (const char* s : { "rmsnorm", "gemm_nt", "gemm_nt",
|
||||
"rmsnorm_p", "rmsnorm", "rmsnorm", "rope", "rope",
|
||||
"kv_store", "attn_paged", "gemm_nt", "rmsnorm", "add" })
|
||||
want.push_back(s);
|
||||
} else {
|
||||
/* sliding: a v gemm feeds the weightless v norm. */
|
||||
for (const char* s : { "rmsnorm", "gemm_nt", "gemm_nt", "gemm_nt",
|
||||
"rmsnorm_p", "rmsnorm", "rmsnorm", "rope", "rope",
|
||||
"kv_store", "attn_paged", "gemm_nt", "rmsnorm", "add" })
|
||||
want.push_back(s);
|
||||
}
|
||||
for (const char* s : { "rmsnorm", "gemm_nt", "gelu_mul", "gemm_nt", "rmsnorm", "add" })
|
||||
want.push_back(s);
|
||||
want.push_back("scale"); /* the per-layer scalar over the residual stream */
|
||||
}
|
||||
for (const char* s : { "rmsnorm", "gather_rows", "logits_gemm", "logit_softcap" })
|
||||
want.push_back(s);
|
||||
CHECK_EQ(join(issued_ops(b, c)), join(want));
|
||||
|
||||
/* The sampled-row ops band on n_out; the attention bands on q_len. */
|
||||
for (const auto& i : c.issues) {
|
||||
const RecOp* o = b.op_of(i.op);
|
||||
if (!o) continue;
|
||||
if (o->op == "attn_paged")
|
||||
CHECK_EQ(i.n, (int64_t)batch.max_q_len);
|
||||
else if (o->op == "gather_rows" || o->op == "logits_gemm" || o->op == "logit_softcap")
|
||||
CHECK_EQ(i.n, (int64_t)batch.n_out);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user