Archived
gemma4_bf16: the 12B's image and audio embedders
rad_block_gemma4_unified.h: LayerNorm, the dense with its bias, LayerNorm, the column's and the row's position rows, LayerNorm (eps 1e-5), the weightless RMS norm and the projection; for audio the norm and the projection. The position table is read as its two taps of dimension 1, which a recipe stores (gemma4-12b-bf16, -i8g).
This commit is contained in:
1 parent
a00e5e7094
commit
81be3cb63d
6 files changed
+439
-7
No files matched your search
@@ -0,0 +1,259 @@
|
||||
/* rad_block_gemma4_unified.h -- the media embedders of Gemma 4 12B ("unified", transformers'
|
||||
* Gemma4UnifiedVisionEmbedder and the audio Gemma4UnifiedMultimodalEmbedder), served by gemma4_bf16.
|
||||
* Neither has an encoder: the language model sees each patch or audio frame after a few norms and
|
||||
* linears.
|
||||
*
|
||||
* image: a patch is 48 x 48 x 3 (3 x 3 of the processor's 16-pixel patches), staged by the core's
|
||||
* Gemma 4 processor as u / 255 in bf16, (y, x, channel); a LayerNorm over the patch, a linear with
|
||||
* a bias, a LayerNorm, plus a learned row for the patch's column and one for its row (the
|
||||
* checkpoint's [npos, 2, width] table, read as its two taps of dimension 1), a LayerNorm, then the
|
||||
* multimodal embedder: an RMS norm with no gain and a width -> n_embd linear. The LayerNorms are
|
||||
* nn.LayerNorm's default eps 1e-5, which no config states. One patch is one row.
|
||||
* audio: a frame is 640 samples at 16 kHz as they are, the last zero-padded, in bf16; an RMS norm
|
||||
* with no gain and a 640 -> n_embd linear. One frame is one row.
|
||||
*
|
||||
* Their rows replace the placeholders' embeddings unscaled, as the vision tower's do. Every rank
|
||||
* runs them whole (RAD_SHARD_NONE). */
|
||||
#ifndef RAD_BLOCK_GEMMA4_UNIFIED_H
|
||||
#define RAD_BLOCK_GEMMA4_UNIFIED_H
|
||||
|
||||
#include "rad_arch.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
namespace rad {
|
||||
namespace arch {
|
||||
|
||||
struct Gemma4UnifiedVision {
|
||||
bool on = false;
|
||||
int64_t P = 0, width = 0, patch_dim = 0, npos = 0, n_embd = 0;
|
||||
rad_weight w_ln1 = 0, w_ln1b = 0, w_in = 0, w_inb = 0, w_ln2 = 0, w_ln2b = 0, w_px = 0, w_py = 0;
|
||||
rad_weight w_pn = 0, w_pnb = 0, w_proj = 0;
|
||||
rad_op op_ln1 = 0, op_in = 0, op_ln2 = 0, op_px = 0, op_py = 0, op_padd = 0, op_add = 0;
|
||||
rad_op op_pn = 0, op_norm = 0, op_out = 0;
|
||||
rad_buf b_n = 0, b_x = 0, b_h = 0, b_px = 0, b_py = 0, b_out = 0;
|
||||
|
||||
static bool present(const RadModelMeta* m) {
|
||||
const char* t = rad_meta_gets(m, "vision_config.model_type", "");
|
||||
return t && !std::strcmp(t, "gemma4_unified_vision");
|
||||
}
|
||||
|
||||
int declare(RadBuilder* b, const RadModelMeta* m, const RadBuildCtx* ctx, Names& nm,
|
||||
int64_t model_embd, const char* who) {
|
||||
on = false;
|
||||
if (!present(m) || ctx->max_enc_patches <= 0) return RAD_OK;
|
||||
width = rad_meta_geti(m, "vision_config.mm_embed_dim", 0);
|
||||
npos = rad_meta_geti(m, "vision_config.mm_posemb_size", 0);
|
||||
const int64_t side = rad_meta_geti(m, "vision_config.model_patch_size", 0);
|
||||
const int64_t outd = rad_meta_geti(m, "vision_config.output_proj_dims", width);
|
||||
const double eps = rad_meta_getf(m, "vision_config.rms_norm_eps", 1e-6);
|
||||
if (width <= 0 || npos <= 0 || side <= 0 || outd != width) {
|
||||
fprintf(stderr, "radiance: %s: the container's vision_config is incomplete or not the "
|
||||
"embedder this plugin serves (mm_embed_dim %lld, mm_posemb_size %lld, "
|
||||
"model_patch_size %lld, output_proj_dims %lld)\n", who,
|
||||
(long long)width, (long long)npos, (long long)side, (long long)outd);
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
n_embd = model_embd;
|
||||
patch_dim = 3 * side * side;
|
||||
P = ctx->max_enc_patches;
|
||||
const int A = RAD_ACCESS_PER_REQUEST, N = RAD_SHARD_NONE;
|
||||
const char* V = "model.vision_embedder";
|
||||
struct { const char* to; const char* from; } cp[] = {
|
||||
{ "v.ln1.weight", "patch_ln1.weight" }, { "v.ln1.bias", "patch_ln1.bias" },
|
||||
{ "v.in.weight", "patch_dense.weight" }, { "v.in.bias", "patch_dense.bias" },
|
||||
{ "v.ln2.weight", "patch_ln2.weight" }, { "v.ln2.bias", "patch_ln2.bias" },
|
||||
{ "v.pn.weight", "pos_norm.weight" }, { "v.pn.bias", "pos_norm.bias" } };
|
||||
for (const auto& e : cp)
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("%s", e.to), nm.ckpt("%s.%s", V, e.from)));
|
||||
RAD_ARCH_TRY(map_tap(b, nm.f("v.pos_x.weight"), nm.ckpt("%s.pos_embedding", V), 1, 0));
|
||||
RAD_ARCH_TRY(map_tap(b, nm.f("v.pos_y.weight"), nm.ckpt("%s.pos_embedding", V), 1, 1));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("v.embed.weight"), nm.ckpt("model.embed_vision.embedding_projection.weight")));
|
||||
w_ln1 = decl_w(b, nm.f("v.ln1.weight"), RAD_BF16, {patch_dim}, A, N, grp_model());
|
||||
w_ln1b = decl_w(b, nm.f("v.ln1.bias"), RAD_BF16, {patch_dim}, A, N, grp_model());
|
||||
w_in = decl_w(b, nm.f("v.in.weight"), RAD_BF16, {width, patch_dim}, A, N, grp_model());
|
||||
w_inb = decl_w(b, nm.f("v.in.bias"), RAD_BF16, {width}, A, N, grp_model());
|
||||
w_ln2 = decl_w(b, nm.f("v.ln2.weight"), RAD_BF16, {width}, A, N, grp_model());
|
||||
w_ln2b = decl_w(b, nm.f("v.ln2.bias"), RAD_BF16, {width}, A, N, grp_model());
|
||||
w_px = decl_w(b, nm.f("v.pos_x.weight"), RAD_BF16, {npos, width}, A, N, grp_model());
|
||||
w_py = decl_w(b, nm.f("v.pos_y.weight"), RAD_BF16, {npos, width}, A, N, grp_model());
|
||||
w_pn = decl_w(b, nm.f("v.pn.weight"), RAD_BF16, {width}, A, N, grp_model());
|
||||
w_pnb = decl_w(b, nm.f("v.pn.bias"), RAD_BF16, {width}, A, N, grp_model());
|
||||
w_proj = decl_w(b, nm.f("v.embed.weight"), RAD_BF16, {n_embd, width}, A, N, grp_model());
|
||||
|
||||
b_n = decl_b(b, nm.f("v.n"), RAD_BF16, {P, patch_dim});
|
||||
b_x = decl_b(b, nm.f("v.x"), RAD_BF16, {P, width});
|
||||
b_h = decl_b(b, nm.f("v.h"), RAD_BF16, {P, width});
|
||||
b_px = decl_b(b, nm.f("v.px"), RAD_BF16, {P, width});
|
||||
b_py = decl_b(b, nm.f("v.py"), RAD_BF16, {P, width});
|
||||
b_out = decl_b(b, nm.f("v.out"), RAD_BF16, {P, n_embd});
|
||||
|
||||
auto ln = [&](int64_t n, rad_weight w, rad_weight bias) {
|
||||
return RAD_OP(b, "layernorm", RAD_PARAMS(RAD_RANGE("M", 1, P), RAD_INT("n", n),
|
||||
RAD_F64("eps", 1e-5), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(w, bias));
|
||||
};
|
||||
auto lookup = [&](rad_weight w) {
|
||||
return RAD_OP(b, "embed_lookup",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, P), RAD_INT("n_embd", width),
|
||||
RAD_INT("n_vocab", npos), RAD_STR("dtype", "bf16"),
|
||||
RAD_INT("vocab_offset", 0), RAD_F64("mult", 1.0)),
|
||||
RAD_WEIGHTS(w));
|
||||
};
|
||||
auto add = [&]() {
|
||||
return RAD_OP(b, "add", RAD_PARAMS(RAD_RANGE("M", 1, P), RAD_INT("n", width),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS);
|
||||
};
|
||||
op_ln1 = rw(b, ln(patch_dim, w_ln1, w_ln1b), {}, {b_n});
|
||||
op_in = rw(b, RAD_OP(b, "gemm_nt_bias",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, P), RAD_INT("N", width),
|
||||
RAD_INT("K", patch_dim), RAD_STR("dtype", "bf16"),
|
||||
RAD_STR("act", "none")),
|
||||
RAD_WEIGHTS(w_in, w_inb)),
|
||||
{b_n}, {b_x});
|
||||
op_ln2 = rw(b, ln(width, w_ln2, w_ln2b), {b_x}, {b_h});
|
||||
op_px = rw(b, lookup(w_px), {}, {b_px});
|
||||
op_py = rw(b, lookup(w_py), {}, {b_py});
|
||||
op_padd = rw(b, add(), {b_px, b_py}, {b_px});
|
||||
op_add = rw(b, add(), {b_h, b_px}, {b_h});
|
||||
op_pn = rw(b, ln(width, w_pn, w_pnb), {b_h}, {b_x});
|
||||
op_norm = rw(b, RAD_OP(b, "rmsnorm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, P), RAD_INT("n", width),
|
||||
RAD_F64("eps", eps), RAD_F64("wadd", 0.0),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{b_x}, {b_h});
|
||||
op_out = rw(b, RAD_OP(b, "gemm_nt",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, P), RAD_INT("N", n_embd),
|
||||
RAD_INT("K", width), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(w_proj)),
|
||||
{b_h}, {b_out});
|
||||
|
||||
RadEncoderDecl e{};
|
||||
e.name = "vision";
|
||||
e.modalities = RAD_MM_IMAGE | RAD_MM_VIDEO;
|
||||
e.patch_dim = patch_dim;
|
||||
e.merge = 1;
|
||||
e.max_patches = P;
|
||||
e.n_embd = n_embd;
|
||||
e.out = b_out;
|
||||
RAD_ARCH_TRY(rad_declare_encoder(b, &e));
|
||||
rad_note(b, "vision embedder (no tower): %lldx%lld patches of %lld values -> %lld, "
|
||||
"positions %lld a side; %lld patches a pass -> %lld rows of %lld",
|
||||
(long long)side, (long long)side, (long long)patch_dim, (long long)width,
|
||||
(long long)npos, (long long)P, (long long)P, (long long)n_embd);
|
||||
on = true;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
void step(RadCtx* c, const RadBatch* batch) const {
|
||||
const int64_t Pn = batch->enc_n_patch;
|
||||
if (!on || Pn <= 0 || Pn > P || batch->enc_n_seg <= 0) {
|
||||
rad_step_fail(c, "an encoder pass this program's vision embedder cannot serve: none "
|
||||
"declared, or more patches than it was declared for");
|
||||
return;
|
||||
}
|
||||
const int32_t* crd = batch->enc_coord;
|
||||
RAD_ISSUE_N(c, op_ln1, Pn, praw2(batch->enc_pixels, RAD_BF16, Pn, patch_dim), RAD_W(w_ln1),
|
||||
RAD_W(w_ln1b), brows(b_n, Pn));
|
||||
RAD_ISSUE_N(c, op_in, Pn, brows(b_n, Pn), RAD_W(w_in), RAD_W(w_inb), RAD_NONE, brows(b_x, Pn));
|
||||
RAD_ISSUE_N(c, op_ln2, Pn, brows(b_x, Pn), RAD_W(w_ln2), RAD_W(w_ln2b), brows(b_h, Pn));
|
||||
RAD_ISSUE_N(c, op_px, Pn, praw(crd + Pn, RAD_I32, Pn), RAD_W(w_px), brows(b_px, Pn));
|
||||
RAD_ISSUE_N(c, op_py, Pn, praw(crd, RAD_I32, Pn), RAD_W(w_py), brows(b_py, Pn));
|
||||
RAD_ISSUE_N(c, op_padd, Pn, brows(b_px, Pn), brows(b_py, Pn), brows(b_px, Pn));
|
||||
RAD_ISSUE_N(c, op_add, Pn, brows(b_h, Pn), brows(b_px, Pn), brows(b_h, Pn));
|
||||
RAD_ISSUE_N(c, op_pn, Pn, brows(b_h, Pn), RAD_W(w_pn), RAD_W(w_pnb), brows(b_x, Pn));
|
||||
RAD_ISSUE_N(c, op_norm, Pn, brows(b_x, Pn), RAD_NONE, brows(b_h, Pn));
|
||||
RAD_ISSUE_N(c, op_out, Pn, brows(b_h, Pn), RAD_W(w_proj), brows(b_out, Pn));
|
||||
}
|
||||
};
|
||||
|
||||
struct Gemma4UnifiedAudio {
|
||||
bool on = false;
|
||||
int64_t P = 0, frame = 0, n_embd = 0;
|
||||
rad_weight w_proj = 0;
|
||||
rad_op op_norm = 0, op_out = 0;
|
||||
rad_buf b_n = 0, b_out = 0;
|
||||
|
||||
static bool present(const RadModelMeta* m) {
|
||||
const char* t = rad_meta_gets(m, "audio_config.model_type", "");
|
||||
return t && !std::strcmp(t, "gemma4_unified_audio");
|
||||
}
|
||||
/* Samples a frame: the extractor's audio_samples_per_token, else the embedder's input width. */
|
||||
static int64_t frame_for(const RadModelMeta* m) {
|
||||
return rad_meta_geti(m, "processor.feature_extractor.audio_samples_per_token",
|
||||
rad_meta_geti(m, "audio_config.audio_embed_dim", 0));
|
||||
}
|
||||
/* The longest clip in frames: processor_config.json's audio_seq_length, else 30 s. */
|
||||
static int64_t frames_for(const RadModelMeta* m) {
|
||||
const int64_t f = frame_for(m);
|
||||
const int64_t rate = rad_meta_geti(m, "processor.feature_extractor.sampling_rate", 16000);
|
||||
if (f <= 0 || rate <= 0) return 0;
|
||||
return rad_meta_geti(m, "processor.audio_seq_length", (30 * rate + f - 1) / f);
|
||||
}
|
||||
|
||||
int declare(RadBuilder* b, const RadModelMeta* m, const RadBuildCtx* ctx, Names& nm,
|
||||
int64_t model_embd, const char* who) {
|
||||
on = false;
|
||||
if (!present(m) || ctx->max_audio_frames <= 0) return RAD_OK;
|
||||
frame = frame_for(m);
|
||||
const int64_t width = rad_meta_geti(m, "audio_config.audio_embed_dim", 0);
|
||||
const double eps = rad_meta_getf(m, "audio_config.rms_norm_eps", 1e-6);
|
||||
if (frame <= 0 || width != frame) {
|
||||
fprintf(stderr, "radiance: %s: the container's audio frame (%lld samples) is not the "
|
||||
"embedder's input (audio_embed_dim %lld)\n", who, (long long)frame,
|
||||
(long long)width);
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
n_embd = model_embd;
|
||||
P = ctx->max_audio_frames;
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("a.embed.weight"), nm.ckpt("model.embed_audio.embedding_projection.weight")));
|
||||
w_proj = decl_w(b, nm.f("a.embed.weight"), RAD_BF16, {n_embd, frame}, RAD_ACCESS_PER_REQUEST,
|
||||
RAD_SHARD_NONE, grp_model());
|
||||
b_n = decl_b(b, nm.f("a.n"), RAD_BF16, {P, frame});
|
||||
b_out = decl_b(b, nm.f("a.out"), RAD_BF16, {P, n_embd});
|
||||
op_norm = rw(b, RAD_OP(b, "rmsnorm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, P), RAD_INT("n", frame),
|
||||
RAD_F64("eps", eps), RAD_F64("wadd", 0.0),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{}, {b_n});
|
||||
op_out = rw(b, RAD_OP(b, "gemm_nt",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, P), RAD_INT("N", n_embd),
|
||||
RAD_INT("K", frame), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(w_proj)),
|
||||
{b_n}, {b_out});
|
||||
RadEncoderDecl e{};
|
||||
e.name = "audio";
|
||||
e.modalities = RAD_MM_AUDIO;
|
||||
e.patch_dim = frame;
|
||||
e.merge = 1;
|
||||
e.max_patches = P;
|
||||
e.n_embd = n_embd;
|
||||
e.out = b_out;
|
||||
RAD_ARCH_TRY(rad_declare_encoder(b, &e));
|
||||
rad_note(b, "audio embedder (no encoder): frames of %lld samples -> %lld; %lld frames a pass",
|
||||
(long long)frame, (long long)n_embd, (long long)P);
|
||||
on = true;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
void step(RadCtx* c, const RadBatch* batch) const {
|
||||
const int64_t Pn = batch->enc_n_patch;
|
||||
if (!on || Pn <= 0 || Pn > P || batch->enc_n_seg <= 0) {
|
||||
rad_step_fail(c, "an audio pass this program's audio embedder cannot serve: none "
|
||||
"declared, or more frames than it was declared for");
|
||||
return;
|
||||
}
|
||||
RAD_ISSUE_N(c, op_norm, Pn, praw2(batch->enc_pixels, RAD_BF16, Pn, frame), RAD_NONE,
|
||||
brows(b_n, Pn));
|
||||
RAD_ISSUE_N(c, op_out, Pn, brows(b_n, Pn), RAD_W(w_proj), brows(b_out, Pn));
|
||||
}
|
||||
};
|
||||
|
||||
} /* namespace arch */
|
||||
} /* namespace rad */
|
||||
|
||||
#endif /* RAD_BLOCK_GEMMA4_UNIFIED_H */
|
||||
@@ -1,5 +1,6 @@
|
||||
/* gemma4_bf16.cpp -- Gemma 4 dense, bf16, with its vision tower (rad_block_gemma4_vision.h) and,
|
||||
* for E2B/E4B, its audio encoder (rad_block_gemma4_audio.h).
|
||||
* for E2B/E4B, its audio encoder (rad_block_gemma4_audio.h); for the 12B ("unified"), its image and
|
||||
* audio embedders in their place (rad_block_gemma4_unified.h).
|
||||
*
|
||||
* 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"
|
||||
@@ -17,6 +18,7 @@
|
||||
*/
|
||||
#include <arch/rad_arch.h>
|
||||
#include <arch/rad_block_gemma4_audio.h>
|
||||
#include <arch/rad_block_gemma4_unified.h>
|
||||
#include <arch/rad_block_gemma4_vision.h>
|
||||
#include <arch/rad_gemma4_chat.h>
|
||||
#include <arch/rad_gemma4_dflash.h>
|
||||
@@ -1890,6 +1892,8 @@ struct Model {
|
||||
* embeddings. */
|
||||
Gemma4Vision vis{};
|
||||
Gemma4Audio aud{};
|
||||
Gemma4UnifiedVision uvis{}; /* the 12B's embedders, in place of the tower and encoder */
|
||||
Gemma4UnifiedAudio uaud{};
|
||||
rad_op op_mm = 0;
|
||||
|
||||
G4Junction j0{}; /* the first block's entry: norm + quantiser of the embedding stream */
|
||||
@@ -2406,8 +2410,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
* the lookup and before its all-reduce (RadBatch::mm_embd: rank 0 holds the rows, every other
|
||||
* rank zeros). The reference scales token embeddings by sqrt(n_embd) and scatters the media
|
||||
* features in unscaled. */
|
||||
if ((Gemma4Vision::present(meta) && ctx->max_enc_patches > 0) ||
|
||||
(Gemma4Audio::present(meta) && ctx->max_audio_frames > 0))
|
||||
if (((Gemma4Vision::present(meta) || Gemma4UnifiedVision::present(meta)) &&
|
||||
ctx->max_enc_patches > 0) ||
|
||||
((Gemma4Audio::present(meta) || Gemma4UnifiedAudio::present(meta)) &&
|
||||
ctx->max_audio_frames > 0))
|
||||
m.op_mm = rw(b, RAD_OP(b, "scatter_rows",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
@@ -2825,6 +2831,8 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
RAD_ARCH_TRY(m.aud.declare(b, meta, ctx, nm, g.n_embd,
|
||||
(int)(m.n_main + head_groups) + (int)m.vis.L.size(), "model.",
|
||||
"gemma4_bf16"));
|
||||
RAD_ARCH_TRY(m.uvis.declare(b, meta, ctx, nm, g.n_embd, "gemma4_bf16"));
|
||||
RAD_ARCH_TRY(m.uaud.declare(b, meta, ctx, nm, g.n_embd, "gemma4_bf16"));
|
||||
}
|
||||
|
||||
rad_note(b, "rank %d/%d: %lld layers (%lld global, %lld sliding), sliding window %lld, "
|
||||
@@ -3052,8 +3060,14 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
|
||||
/* An encoder pass runs only its encoder. */
|
||||
if (batch->enc) {
|
||||
if (batch->enc == RAD_MM_AUDIO) m.aud.step(c, batch);
|
||||
else m.vis.step(c, batch);
|
||||
if (batch->enc == RAD_MM_AUDIO) {
|
||||
if (m.uaud.on) m.uaud.step(c, batch);
|
||||
else m.aud.step(c, batch);
|
||||
} else if (m.uvis.on) {
|
||||
m.uvis.step(c, batch);
|
||||
} else {
|
||||
m.vis.step(c, batch);
|
||||
}
|
||||
return;
|
||||
}
|
||||
/* A draft pass runs only the draft model. The trunk already left its final-normed hidden state
|
||||
@@ -3194,12 +3208,15 @@ static int probe(const RadModelMeta* meta, RadArchProbe* out) {
|
||||
if (k > 0) out->enc_merge = k * k;
|
||||
}
|
||||
if (Gemma4Audio::present(meta)) out->audio_frames = Gemma4Audio::frames_for(meta);
|
||||
/* The 12B's embedders: a patch and an audio frame are a row each. */
|
||||
if (Gemma4UnifiedVision::present(meta)) out->enc_merge = 1;
|
||||
if (Gemma4UnifiedAudio::present(meta)) out->audio_frames = Gemma4UnifiedAudio::frames_for(meta);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
#ifndef RAD_ARCH_NO_EXPORTS
|
||||
RAD_ARCH_PLUGIN(gemma4_bf16, "gemma4", "compressed-tensors", "0.1.0",
|
||||
"Gemma 4 dense (text, images and E2B/E4B audio), w4a8 or w8a8 from its bf16 or its CT-INT4 (QAT) "
|
||||
"Gemma 4 dense (text, images, E2B/E4B/12B audio), w4a8 or w8a8 from its bf16 or its CT-INT4 (QAT) "
|
||||
"release: sliding-window and proportional-RoPE global attention over a plain "
|
||||
"RMSNorm stream with GELU feed-forward, per-layer scalar scaling and soft-capped "
|
||||
"logits; Google's assistant head, z-lab's DFlash drafter or a DFlash2 drafter")
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
# gemma4-12b-bf16.recipe -- Gemma 4 12B ("unified": text, images, audio, Google's assistant head) at bf16.
|
||||
# Every weight stays as the release holds it. The image embedder's position table, [npos, 2, width], is read
|
||||
# as its two taps of dimension 1 (the column's rows and the row's), computed at convert and stored bf16 here.
|
||||
#
|
||||
# rad-convert <gemma-4-12B-it> --draft-model <gemma-4-12B-it-assistant> \
|
||||
# --recipe data/recipes/gemma4-12b-bf16.recipe \
|
||||
# --set quantization_config.quant_method=compressed-tensors -o gemma4-12b-bf16.rad
|
||||
|
||||
v.pos_*.weight cast dtype=bf16
|
||||
@@ -0,0 +1,18 @@
|
||||
# gemma4-12b-i8g.recipe -- Gemma 4 12B ("unified": text, images, audio, Google's assistant head) from its bf16
|
||||
# release: every trunk linear int8 with a bf16 scale per 128 columns, served at i8a8, the trunk's and the
|
||||
# assistant's lm_heads int8; the assistant's layers and the image and audio embedders bf16, the position
|
||||
# table's two taps stored bf16 (gemma4-12b-bf16.recipe).
|
||||
#
|
||||
# rad-convert <gemma-4-12B-it> --draft-model <gemma-4-12B-it-assistant> \
|
||||
# --recipe data/recipes/gemma4-12b-i8g.recipe \
|
||||
# --set quantization_config.quant_method=compressed-tensors -o gemma4-12b-i8g.rad
|
||||
|
||||
blk.*.attn_qkv.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
|
||||
mtp.output.weight rtn codes=i8 group=128 scale=bf16 clamp=sym
|
||||
|
||||
v.pos_*.weight cast dtype=bf16
|
||||
+5
-1
@@ -70,6 +70,7 @@ struct RecMap {
|
||||
int mode = 0;
|
||||
std::vector<std::string> src;
|
||||
int concat_dim = 0;
|
||||
std::vector<int> src_index; /* RadNameMap::src_index, one per source */
|
||||
};
|
||||
struct RecKV { std::string name; RadKVGroupDecl d{}; bool zero = false; bool replicated = false;
|
||||
bool dense = false; };
|
||||
@@ -278,7 +279,10 @@ extern "C" int rad_decl_name_map(RadBuilder* b, const RadNameMap* m) {
|
||||
r.declared = m->declared;
|
||||
r.mode = m->mode;
|
||||
r.concat_dim = m->concat_dim;
|
||||
for (int i = 0; i < m->n_src; ++i) r.src.push_back(m->src[i]);
|
||||
for (int i = 0; i < m->n_src; ++i) {
|
||||
r.src.push_back(m->src[i]);
|
||||
r.src_index.push_back(m->src_index[i]);
|
||||
}
|
||||
b->maps.push_back(r);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -10636,6 +10636,131 @@ TEST(nomicbert_probe_reads_the_model_type) {
|
||||
RAD_E_UNSUPPORTED);
|
||||
}
|
||||
|
||||
/* Gemma 4 12B's media embedders (rad_block_gemma4_unified.h) in place of the tower and the
|
||||
* conformer: a picture patch is a 48 x 48 window and an audio frame 640 samples, each one row. The
|
||||
* position table [npos, 2, width] is read as its taps 0 (column) and 1 (row) of dimension 1; a
|
||||
* vision pass is LayerNorm, the dense with its bias, LayerNorm, the two position rows (the column
|
||||
* from coordinate plane 1, the row from plane 0) added in, LayerNorm (eps 1e-5, no config states it),
|
||||
* the weightless RMS norm and the projection; an audio pass the norm and its projection. Every rank
|
||||
* runs both whole and both lint clean; the probe says one patch a row and 750 frames. */
|
||||
TEST(the_gemma4_12b_embeds_images_and_audio_without_an_encoder) {
|
||||
std::vector<const char*> keys(std::begin(k_gem12_keys), std::end(k_gem12_keys));
|
||||
std::vector<const char*> vals(std::begin(k_gem12_vals), std::end(k_gem12_vals));
|
||||
const char* mk[] = { "vision_config.model_type", "vision_config.mm_embed_dim",
|
||||
"vision_config.mm_posemb_size", "vision_config.model_patch_size",
|
||||
"vision_config.output_proj_dims", "vision_config.rms_norm_eps",
|
||||
"vision_config.patch_size", "vision_config.pooling_kernel_size",
|
||||
"audio_config.model_type", "audio_config.audio_embed_dim",
|
||||
"audio_config.rms_norm_eps", "processor.feature_extractor.sampling_rate",
|
||||
"processor.feature_extractor.audio_samples_per_token",
|
||||
"processor.audio_seq_length" };
|
||||
const char* mv[] = { "gemma4_unified_vision", "3840", "1120", "48", "3840", "1e-06", "16", "3",
|
||||
"gemma4_unified_audio", "640", "1e-06", "16000", "640", "750" };
|
||||
keys.insert(keys.end(), std::begin(mk), std::end(mk));
|
||||
vals.insert(vals.end(), std::begin(mv), std::end(mv));
|
||||
RadModelMeta meta = gemma4_12b_meta();
|
||||
meta.n_kv = (int)keys.size(); meta.kv_key = keys.data(); meta.kv_val = vals.data();
|
||||
|
||||
RadArchProbe pr{};
|
||||
REQUIRE_EQ(gemma4_bf16::probe(&meta, &pr), RAD_OK);
|
||||
CHECK_EQ(pr.enc_merge, 1);
|
||||
CHECK_EQ(pr.audio_frames, 750);
|
||||
|
||||
for (int world : { 1, 2, 4 }) {
|
||||
for (int rank = 0; rank < world; ++rank) {
|
||||
RadBuilder b;
|
||||
RadBuildCtx ctx = build_ctx(rank, world);
|
||||
ctx.max_enc_patches = 1120;
|
||||
ctx.max_audio_frames = 750;
|
||||
REQUIRE_EQ(gemma4_bf16::declare(&b, &meta, &ctx), RAD_OK);
|
||||
CHECK_EQ(b.encoder_name, std::string("vision"));
|
||||
CHECK_EQ(b.encoder.patch_dim, 6912);
|
||||
CHECK_EQ(b.encoder.merge, 1);
|
||||
CHECK_EQ(b.encoder.max_patches, 1120);
|
||||
CHECK_EQ(b.encoder.n_embd, 3840);
|
||||
CHECK_EQ(b.audio_encoder_name, std::string("audio"));
|
||||
CHECK_EQ(b.audio_encoder.patch_dim, 640);
|
||||
CHECK_EQ(b.audio_encoder.merge, 1);
|
||||
CHECK_EQ(b.audio_encoder.max_patches, 750);
|
||||
CHECK_EQ(count_op(b, "scatter_rows"), 1);
|
||||
for (int t = 0; t < 2; ++t) {
|
||||
const RecMap* pos = find_map(b, t ? "v.pos_y.weight" : "v.pos_x.weight");
|
||||
REQUIRE(pos != nullptr);
|
||||
CHECK_EQ(pos->mode, (int)RAD_MAP_TAP);
|
||||
CHECK_EQ(pos->concat_dim, 1);
|
||||
CHECK(pos->src == std::vector<std::string>{ "model.vision_embedder.pos_embedding" });
|
||||
REQUIRE_EQ(pos->src_index.size(), (size_t)1);
|
||||
CHECK_EQ(pos->src_index[0], t);
|
||||
const RecWeight* w = find_w(b, t ? "v.pos_y.weight" : "v.pos_x.weight");
|
||||
REQUIRE(w != nullptr);
|
||||
CHECK_EQ(w->d.shape[0], 1120);
|
||||
CHECK_EQ(w->d.shape[1], 3840);
|
||||
}
|
||||
const struct { const char* d; const char* s; } src[] = {
|
||||
{ "v.ln1.weight", "model.vision_embedder.patch_ln1.weight" },
|
||||
{ "v.ln1.bias", "model.vision_embedder.patch_ln1.bias" },
|
||||
{ "v.in.weight", "model.vision_embedder.patch_dense.weight" },
|
||||
{ "v.in.bias", "model.vision_embedder.patch_dense.bias" },
|
||||
{ "v.ln2.weight", "model.vision_embedder.patch_ln2.weight" },
|
||||
{ "v.ln2.bias", "model.vision_embedder.patch_ln2.bias" },
|
||||
{ "v.pn.weight", "model.vision_embedder.pos_norm.weight" },
|
||||
{ "v.pn.bias", "model.vision_embedder.pos_norm.bias" },
|
||||
{ "v.embed.weight", "model.embed_vision.embedding_projection.weight" },
|
||||
{ "a.embed.weight", "model.embed_audio.embedding_projection.weight" } };
|
||||
for (const auto& e : src) {
|
||||
const RecMap* mp = find_map(b, e.d);
|
||||
REQUIRE(mp != nullptr);
|
||||
CHECK(mp->src == std::vector<std::string>{ e.s });
|
||||
CHECK(find_w(b, e.d) != nullptr);
|
||||
}
|
||||
const RecWeight* dense = find_w(b, "v.in.weight");
|
||||
REQUIRE(dense != nullptr);
|
||||
CHECK_EQ(dense->d.shape[0], 3840);
|
||||
CHECK_EQ(dense->d.shape[1], 6912);
|
||||
CHECK_EQ(dense->d.shard, (int)RAD_SHARD_NONE);
|
||||
CHECK(find_w(b, "v.blk.0.qkv") == nullptr); /* no tower */
|
||||
for (const auto& o : b.ops)
|
||||
if (o.op == "layernorm") CHECK_NEAR(pflt(&o, "eps"), 1e-5, 0.0);
|
||||
|
||||
const auto vis = enc_pass(RAD_MM_IMAGE, 280, 6912);
|
||||
RadCtx cv{ .b = &b, .batch = &vis->b };
|
||||
cv.rank = rank; cv.world = world;
|
||||
gemma4_bf16::step(&cv, &vis->b);
|
||||
CHECK(cv.step_fail.empty());
|
||||
CHECK_LINT(b, &cv);
|
||||
const std::vector<std::string> want = { "layernorm", "gemm_nt_bias", "layernorm",
|
||||
"embed_lookup", "embed_lookup", "add", "add",
|
||||
"layernorm", "rmsnorm", "gemm_nt" };
|
||||
CHECK_EQ(join(issued_ops(b, cv)), join(want));
|
||||
REQUIRE_EQ(cv.issues.size(), want.size());
|
||||
CHECK(cv.issues[0].opd[0].raw == vis->pix.data());
|
||||
CHECK(cv.issues[3].opd[0].raw == vis->coord.data() + 280); /* the column */
|
||||
CHECK(cv.issues[4].opd[0].raw == vis->coord.data()); /* the row */
|
||||
CHECK_EQ(cv.issues[3].opd[1].handle, (uint32_t)gemma4_bf16::g_model[rank].uvis.w_px);
|
||||
CHECK_EQ(cv.issues[4].opd[1].handle, (uint32_t)gemma4_bf16::g_model[rank].uvis.w_py);
|
||||
CHECK_EQ(cv.issues.back().n, 280);
|
||||
CHECK_EQ(cv.issues.back().opd.back().handle, b.encoder.out);
|
||||
|
||||
const auto aud = enc_pass(RAD_MM_AUDIO, 125, 640);
|
||||
RadCtx ca{ .b = &b, .batch = &aud->b };
|
||||
ca.rank = rank; ca.world = world;
|
||||
gemma4_bf16::step(&ca, &aud->b);
|
||||
CHECK(ca.step_fail.empty());
|
||||
CHECK_LINT(b, &ca);
|
||||
CHECK_EQ(join(issued_ops(b, ca)), join({ "rmsnorm", "gemm_nt" }));
|
||||
CHECK_EQ(ca.issues.back().n, 125);
|
||||
CHECK_EQ(ca.issues.back().opd.back().handle, b.audio_encoder.out);
|
||||
|
||||
/* More patches than declared is refused, issuing nothing. */
|
||||
const auto big = enc_pass(RAD_MM_IMAGE, 1121, 6912);
|
||||
RadCtx cb{ .b = &b, .batch = &big->b };
|
||||
gemma4_bf16::step(&cb, &big->b);
|
||||
CHECK(!cb.step_fail.empty());
|
||||
CHECK(cb.issues.empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ==================================================================== qwenimage21 */
|
||||
|
||||
namespace {
|
||||
|
||||
Reference in new issue
Block a user