Archived
nomicbert: architecture plugin for nomic-embed-text-v2-moe
Post-norm encoder with fused Wqkv and biases, NeoX rotary, non-causal attention, a dense MLP on even layers and a top-2 of 8 expert MLP on odd layers, mean pooling into the pooled f32 buffer for /v1/embeddings. The checkpoint is f32; data/recipes/nomic-embed-v2-moe-bf16.recipe casts the linears, the token embedding and the token-type table to bf16 (the device kernels read bf16), norms and biases stay f32. The checkpoint's shared experts.bias is not applied: against a llama.cpp run of the same checkpoint at Q8_0 the variant without it matches best (cosine 0.9994; the others 0.9990 to 0.9993), and llama.cpp's own nomic-bert-moe does not carry it. Verified on a gfx1201 card and on libavx/libref against a pure-float32 torch reference: mean cosine 0.99995, min 0.99983 over 20 texts. Unit tests declare and lint a cut-down model. Not verified: inputs above 512 tokens against a reference on the device, other quantisations.
This commit is contained in:
1 parent
4853db97db
commit
fd40bb430c
8 files changed
+865
-4
No files matched your search
@@ -62,6 +62,7 @@ To support the development of radiance: [Patreon](https://patreon.com/StillDeadC
|
||||
| Qwen3.8-Flash-Next bf16, a disk-streamed reference | `qwen3.8-flash-next-bf16.rad` (335 GiB) | [convert](#convert-a-checkpoint) | `flashnext-bf16.yaml` |
|
||||
| Stable Diffusion XL bf16 and its finetunes (Illustrious, NoobAI, Pony): text to image, one card | `illustrious-xl-v2.0-bf16.rad` (6.4 GiB), from any single-file SDXL checkpoint | [convert](#convert-a-checkpoint) | `sdxl.yaml` |
|
||||
| EmbeddingGemma 2 bf16: text, image, video and audio embeddings, one card | `embeddinggemma2-bf16.rad` (1.4 GiB) | [convert](#convert-a-checkpoint) | `embeddinggemma2.yaml` |
|
||||
| nomic-embed-text-v2-moe bf16: text embeddings, one card | `nomic-embed-v2-moe-bf16.rad` (959 MB) | [convert](#convert-a-checkpoint) | - |
|
||||
| Anima Aesthetic FP8: anime and illustration, text to image, one card (Base and Turbo convert the same way) | `anima-aesthetic-fp8.rad` (3.0 GiB) | [convert](#convert-a-checkpoint) | `anima.yaml` |
|
||||
| Qwen3-ASR 1.7B bf16: speech to text, `/v1/audio/transcriptions` and live over `/v1/realtime`, 30 languages, one card | the checkpoint as it ships (4.1 GiB) | [Qwen/Qwen3-ASR-1.7B-hf](https://huggingface.co/Qwen/Qwen3-ASR-1.7B-hf) | — |
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
# Built in radiance's tree, or standalone against an installed radiance
|
||||
# (`cmake -S arch/nomicbert -B build -DCMAKE_PREFIX_PATH=<radiance prefix>`); see cmake/RadAddPlugin.cmake.
|
||||
if(NOT COMMAND rad_add_plugin)
|
||||
cmake_minimum_required(VERSION 3.21)
|
||||
project(nomicbert LANGUAGES CXX)
|
||||
find_package(radiance REQUIRED)
|
||||
rad_plugin_defaults()
|
||||
endif()
|
||||
|
||||
rad_add_plugin(nomicbert architectures SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/nomicbert.cpp)
|
||||
@@ -0,0 +1,549 @@
|
||||
/* nomicbert -- Nomic BERT (model_type nomic_bert), the encoder behind nomic-embed-text-v2-moe.
|
||||
* bf16 weights and activations, with f32 norms and biases.
|
||||
*
|
||||
* An embedding model: it declares a pooled output (rad_declare_pooled) and no logits, so the
|
||||
* engine runs it with no sampler and no KV cache, carries every input whole in one step, and
|
||||
* returns each sequence's pooled row. Attention is bidirectional on every layer, so nothing is
|
||||
* paged: a token attends to later tokens, and there is nothing to carry between steps.
|
||||
*
|
||||
* ============================== THE SHAPE ==============================
|
||||
*
|
||||
* 12 layers, hidden 768, 12 heads of 64, ffn 3072, vocab 250048, type vocab 1
|
||||
* post-norm block, the config's `prenorm: false`:
|
||||
* a = out_proj(attn(rope(Wqkv h + b))) + b_out; h = LN1(h + a)
|
||||
* dense layer: m = fc2(gelu(fc1 h + b1)) + b2; h = LN2(h + m)
|
||||
* MoE layer (every second, moe_every_n_layers 2): router top-2 over eight experts, no
|
||||
* renormalisation, no shared bias; h = LN2(h + moe(h))
|
||||
* fused Wqkv [q|k|v], no GQA, non-causal, scale 1/sqrt(64) = 0.125
|
||||
* NeoX rotary over all 64 dimensions, theta 1e4, on q and k only, no position table
|
||||
* embeddings: word + token_type, then emb_ln; no final norm and no pooler tensor
|
||||
* output: the mean of every real token, then cast to the f32 pooled buffer
|
||||
*
|
||||
* The expert tables are one concatenated tensor each: w1 [8 * 3072, 768] as [out, in], w2
|
||||
* [8 * 3072, 768] as [in, out] per expert, so w2 is sliced then transposed at convert
|
||||
* (RAD_MAP_TRANSPOSE with src_index and src_rows). Every expert's two tensors take distinct group
|
||||
* slots.
|
||||
*
|
||||
* The block order and the unused shared experts.bias were settled against the production
|
||||
* llama.cpp embedder by scripts/nomic_ref.py; the winning variant and the vectors it produced are
|
||||
* in tests/data/nomic/golden.json (norm=post, bias=none). The checkpoint's `experts.bias` tensors
|
||||
* are therefore not declared and stay unconverted.
|
||||
*/
|
||||
#include <arch/rad_arch.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
namespace nomicbert {
|
||||
|
||||
using namespace rad::arch;
|
||||
|
||||
/* Post-norm, the reading scripts/nomic_ref.py and tests/data/nomic/golden.json settle: the
|
||||
* attention is on the raw stream, then LN1, then the feed-forward on LN1's output, then LN2 over
|
||||
* that output plus the feed-forward's. The shared experts.bias is not applied (bias=none there). */
|
||||
constexpr bool kPostNorm = true;
|
||||
|
||||
struct Layer {
|
||||
bool moe = false;
|
||||
rad_weight w_norm1 = 0, b_norm1 = 0, w_qkv = 0, b_qkv = 0, w_out = 0, b_out = 0;
|
||||
rad_weight w_norm2 = 0, b_norm2 = 0;
|
||||
rad_weight w_fc1 = 0, b_fc1 = 0, w_fc2 = 0, b_fc2 = 0;
|
||||
rad_weight w_router = 0;
|
||||
std::vector<rad_weight> w_w1, w_w2;
|
||||
|
||||
rad_op op_qkv = 0, op_rope = 0, op_attn = 0, op_out = 0, op_norm1 = 0, op_norm2 = 0;
|
||||
rad_op op_fc1 = 0, op_fc2 = 0;
|
||||
rad_op op_router = 0, op_topk = 0, op_scatter = 0, op_egu = 0, op_gelu = 0, op_edn = 0;
|
||||
rad_op op_gather = 0, op_add = 0;
|
||||
};
|
||||
|
||||
struct Model {
|
||||
Names nm{""};
|
||||
Geom g{};
|
||||
int64_t n_expert = 0, n_expert_used = 0, n_type = 1;
|
||||
|
||||
rad_buf b_x = 0, b_h = 0, b_o = 0, b_qkv = 0, b_att = 0, b_ff = 0;
|
||||
rad_buf b_egu = 0, b_eact = 0, b_edn = 0, b_emoe = 0;
|
||||
rad_buf b_logits = 0, b_ids = 0, b_ew = 0, b_sorted = 0, b_eoff = 0, b_ecnt = 0;
|
||||
rad_buf b_mean = 0, b_pooled = 0;
|
||||
|
||||
rad_weight w_tok = 0, w_type = 0, w_emb_ln = 0, b_emb_ln = 0;
|
||||
rad_op op_embed = 0, op_type = 0, op_emb_ln = 0, op_mean = 0, op_cast = 0;
|
||||
|
||||
std::vector<Layer> layers;
|
||||
};
|
||||
|
||||
static Model g_model[MAX_RANKS];
|
||||
|
||||
/* The layer schedule, read rather than derived: a wrong interval would turn a dense layer into a
|
||||
* MoE layer whose tensors the checkpoint does not hold, which convert refuses but only after the
|
||||
* graph was built. Layer l is routed when (l + 1) % moe_every_n_layers == 0, as nomic's
|
||||
* modeling_hf_nomic_bert builds it. */
|
||||
static bool is_moe_layer(int64_t l, int64_t every) {
|
||||
return every > 0 && (l + 1) % every == 0;
|
||||
}
|
||||
|
||||
static int probe(const RadModelMeta* meta, RadArchProbe* out) {
|
||||
if (!meta || !out) return RAD_E_INVAL;
|
||||
out->draft_depth = 0;
|
||||
const char* mt = rad_meta_gets(meta, "model_type", nullptr);
|
||||
if (mt && *mt && std::strcmp(mt, "nomic_bert") && std::strcmp(mt, "nomicbert")) {
|
||||
fprintf(stderr, "radiance: nomicbert: model_type '%s' is not nomic_bert\n", mt);
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
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;
|
||||
/* One 475M-parameter encoder on one card; tensor parallelism would only add an all-reduce per
|
||||
* layer. */
|
||||
if (ctx->world_size > 1) {
|
||||
fprintf(stderr, "radiance: nomicbert runs on one card (--tp 1); the model is 475M "
|
||||
"parameters\n");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
|
||||
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));
|
||||
Geom& g = m.g;
|
||||
Names& nm = m.nm;
|
||||
const int64_t T = g.max_tok, E = g.n_embd;
|
||||
|
||||
/* The variant flags this plugin does not serve, refused by name. */
|
||||
if (rad_meta_geti(meta, "prenorm", 0) != 0) {
|
||||
fprintf(stderr, "radiance: nomicbert: the container sets prenorm; this plugin declares "
|
||||
"the post-norm block\n");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
if (rad_meta_geti(meta, "causal", 0) != 0) {
|
||||
fprintf(stderr, "radiance: nomicbert: the container sets causal; this encoder attends "
|
||||
"over the whole input\n");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
if (rad_meta_geti(meta, "rotary_emb_interleaved", 0) != 0 ||
|
||||
rad_meta_getf(meta, "rotary_emb_fraction", 1.0) != 1.0 ||
|
||||
rad_meta_gets(meta, "rotary_emb_scale_base", nullptr) ||
|
||||
rad_meta_gets(meta, "rotary_scaling_factor", nullptr)) {
|
||||
fprintf(stderr, "radiance: nomicbert: this plugin serves NeoX rotary over the whole head, "
|
||||
"theta 1e4, with no scale base and no scaling factor\n");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
const char* act = rad_meta_gets(meta, "activation_function", nullptr);
|
||||
if (act && *act && std::strcmp(act, "gelu")) {
|
||||
fprintf(stderr, "radiance: nomicbert: activation_function '%s'; this plugin serves exact "
|
||||
"gelu\n", act);
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
if (rad_meta_geti(meta, "moe_normalize_expert_weights", 0) != 0) {
|
||||
fprintf(stderr, "radiance: nomicbert: moe_normalize_expert_weights is set; the router's "
|
||||
"top-k weights are used without renormalisation\n");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
if (rad_meta_geti(meta, "num_shared_experts", 0) != 0) {
|
||||
fprintf(stderr, "radiance: nomicbert: num_shared_experts is set; this checkpoint has no "
|
||||
"shared expert\n");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
|
||||
m.n_expert = meta->n_expert;
|
||||
m.n_expert_used = meta->n_expert_used;
|
||||
m.n_type = rad_meta_geti(meta, "type_vocab_size", 1);
|
||||
const int64_t every = rad_meta_geti(meta, "moe_every_n_layers", 0);
|
||||
const bool any_moe = m.n_expert > 0;
|
||||
if ((g.n_layer <= 0) || (E <= 0) || (g.n_head <= 0) || (g.head_dim <= 0) ||
|
||||
(g.n_ff <= 0) || (g.n_vocab <= 0) || (g.n_head_kv != g.n_head) ||
|
||||
(g.head_dim * g.n_head != E)) {
|
||||
fprintf(stderr, "radiance: nomicbert: the container does not describe the model "
|
||||
"(layers %lld, hidden %lld, heads %lld of %lld, ffn %lld, vocab %lld)\n",
|
||||
(long long)g.n_layer, (long long)E, (long long)g.n_head,
|
||||
(long long)g.head_dim, (long long)g.n_ff, (long long)g.n_vocab);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (any_moe && (every <= 0 || m.n_expert_used <= 0 || m.n_expert_used > m.n_expert)) {
|
||||
fprintf(stderr, "radiance: nomicbert: %lld experts with top_k %lld and "
|
||||
"moe_every_n_layers %lld does not describe a routed layer\n",
|
||||
(long long)m.n_expert, (long long)m.n_expert_used, (long long)every);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (m.n_type != 1) {
|
||||
fprintf(stderr, "radiance: nomicbert: type_vocab_size %lld; this checkpoint carries one "
|
||||
"token-type row\n", (long long)m.n_type);
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
const int64_t K = m.n_expert_used;
|
||||
const int64_t P = T * K;
|
||||
|
||||
/* ---- activations. */
|
||||
m.b_x = decl_b(b, nm.f("x"), RAD_BF16, {T, E});
|
||||
m.b_h = decl_b(b, nm.f("h"), RAD_BF16, {T, E});
|
||||
m.b_o = decl_b(b, nm.f("o"), RAD_BF16, {T, E});
|
||||
m.b_qkv = decl_b(b, nm.f("qkv"), RAD_BF16, {T, 3 * E});
|
||||
m.b_att = decl_b(b, nm.f("attn"), RAD_BF16, {T, E});
|
||||
m.b_ff = decl_b(b, nm.f("ff"), RAD_BF16, {T, g.n_ff});
|
||||
if (any_moe) {
|
||||
m.b_egu = decl_b(b, nm.f("moe_egu"), RAD_BF16, {P, g.n_ff});
|
||||
m.b_eact = decl_b(b, nm.f("moe_eact"), RAD_BF16, {P, g.n_ff});
|
||||
m.b_edn = decl_b(b, nm.f("moe_edn"), RAD_BF16, {P, E});
|
||||
m.b_emoe = decl_b(b, nm.f("moe_out"), RAD_BF16, {T, E});
|
||||
m.b_logits = decl_b(b, nm.f("moe_logits"), RAD_BF16, {T, m.n_expert});
|
||||
m.b_ids = decl_b(b, nm.f("moe_ids"), RAD_I32, {T, K});
|
||||
m.b_ew = decl_b(b, nm.f("moe_w"), RAD_BF16, {T, K});
|
||||
m.b_sorted = decl_b(b, nm.f("moe_sorted"), RAD_I32, {P});
|
||||
m.b_eoff = decl_b(b, nm.f("moe_eoff"), RAD_I32, {m.n_expert + 1});
|
||||
/* PERSIST: rad_route_report reads the histogram, not a declared op, so liveness would
|
||||
* otherwise treat the scatter's write as dead. */
|
||||
m.b_ecnt = decl_b(b, nm.f("moe_ecnt"), RAD_I32, {m.n_expert}, RAD_BUF_PERSIST);
|
||||
}
|
||||
m.b_mean = decl_b(b, nm.f("mean"), RAD_BF16, {g.max_seqs, E});
|
||||
m.b_pooled = decl_b(b, nm.f("pooled"), RAD_F32, {g.max_seqs, E}, RAD_BUF_PERSIST);
|
||||
RAD_ARCH_TRY(rad_declare_pooled(b, m.b_pooled));
|
||||
|
||||
/* ---- the model-level weights. Norms and biases stay f32 where the kernels read a wider
|
||||
* gain; the embedding, the token-type table and the linears are bf16. table_add_bf16 reads a
|
||||
* bf16 table, and the CPU rows (libref, libavx) read any dtype the tensor carries. */
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("token_embd.weight"), "embeddings.word_embeddings.weight"));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("token_type.weight"),
|
||||
"embeddings.token_type_embeddings.weight"));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("emb_ln.weight"), "emb_ln.weight"));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("emb_ln.bias"), "emb_ln.bias"));
|
||||
m.w_tok = decl_w(b, nm.f("token_embd.weight"), RAD_BF16, {g.n_vocab, E},
|
||||
RAD_ACCESS_VOCAB, RAD_SHARD_NONE, grp_model());
|
||||
m.w_type = decl_w(b, nm.f("token_type.weight"), RAD_BF16, {1, E},
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model());
|
||||
m.w_emb_ln = decl_w(b, nm.f("emb_ln.weight"), RAD_F32, {E},
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model());
|
||||
m.b_emb_ln = decl_w(b, nm.f("emb_ln.bias"), RAD_F32, {E},
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model());
|
||||
|
||||
auto layernorm_op = [&](rad_weight w, rad_weight bias) {
|
||||
return RAD_OP(b, "layernorm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n", E),
|
||||
RAD_F64("eps", g.eps), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(w, bias));
|
||||
};
|
||||
auto gemm_bias_op = [&](int64_t N, int64_t Width, rad_weight w, rad_weight bias,
|
||||
const char* act_name) {
|
||||
if (act_name)
|
||||
return RAD_OP(b, "gemm_nt_bias",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("N", N),
|
||||
RAD_INT("K", Width), RAD_STR("dtype", "bf16"),
|
||||
RAD_STR("act", act_name)),
|
||||
RAD_WEIGHTS(w, bias));
|
||||
return RAD_OP(b, "gemm_nt_bias",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("N", N),
|
||||
RAD_INT("K", Width), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(w, bias));
|
||||
};
|
||||
auto gemm_op = [&](int64_t N, int64_t Width, rad_weight w) {
|
||||
return RAD_OP(b, "gemm_nt",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("N", N),
|
||||
RAD_INT("K", Width), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(w));
|
||||
};
|
||||
auto table_add_op = [&](int64_t rows, rad_weight w) {
|
||||
return RAD_OP(b, "table_add",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("n", E), RAD_INT("L", 1),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(w));
|
||||
};
|
||||
auto add_op = [&](rad_buf a, rad_buf c, rad_buf y) {
|
||||
return rw(b, RAD_OP(b, "add",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n", E),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{a, c}, {y});
|
||||
};
|
||||
auto moe_gemm_op = [&](int64_t N, int64_t Width, const char* order,
|
||||
const std::vector<rad_weight>& tab, rad_buf a, rad_buf y) {
|
||||
rad_op h = rad_decl_op(b, "moe_gemm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("N", N),
|
||||
RAD_INT("K", Width), RAD_INT("n_expert", m.n_expert),
|
||||
RAD_INT("top_k", K), RAD_STR("a_order", order),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
tab.data(), (int)tab.size());
|
||||
return rw(b, h, {a, m.b_sorted, m.b_eoff}, {y});
|
||||
};
|
||||
|
||||
m.op_embed = rw(b, RAD_OP(b, "embed_lookup",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n_embd", E),
|
||||
RAD_INT("n_vocab", g.n_vocab),
|
||||
RAD_STR("dtype", "bf16"), RAD_INT("vocab_offset", 0),
|
||||
RAD_F64("mult", 1.0)),
|
||||
RAD_WEIGHTS(m.w_tok)),
|
||||
{}, {m.b_o});
|
||||
m.op_type = rw(b, table_add_op(T, m.w_type), {m.b_o}, {m.b_o});
|
||||
m.op_emb_ln = rw(b, layernorm_op(m.w_emb_ln, m.b_emb_ln), {m.b_o}, {m.b_x});
|
||||
|
||||
m.layers.resize((size_t)g.n_layer);
|
||||
for (int64_t l = 0; l < g.n_layer; ++l) {
|
||||
Layer& y = m.layers[(size_t)l];
|
||||
y.moe = any_moe && is_moe_layer(l, every);
|
||||
const long long ll = (long long)l;
|
||||
const RadWeightGroup gl = grp_layer((int)l);
|
||||
auto src = [&](const char* s) { return nm.ckpt("encoder.layers.%lld.%s", ll, s); };
|
||||
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%lld.attn_norm.weight", ll), src("norm1.weight")));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%lld.attn_norm.bias", ll), src("norm1.bias")));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%lld.attn_qkv.weight", ll), src("attn.Wqkv.weight")));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%lld.attn_qkv.bias", ll), src("attn.Wqkv.bias")));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%lld.attn_out.weight", ll),
|
||||
src("attn.out_proj.weight")));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%lld.attn_out.bias", ll),
|
||||
src("attn.out_proj.bias")));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%lld.ffn_norm.weight", ll), src("norm2.weight")));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%lld.ffn_norm.bias", ll), src("norm2.bias")));
|
||||
|
||||
const int A = RAD_ACCESS_PER_TOKEN, N = RAD_SHARD_NONE;
|
||||
y.w_norm1 = decl_w(b, nm.f("blk.%lld.attn_norm.weight", ll), RAD_F32, {E}, A, N, gl);
|
||||
y.b_norm1 = decl_w(b, nm.f("blk.%lld.attn_norm.bias", ll), RAD_F32, {E}, A, N, gl);
|
||||
y.w_qkv = decl_w(b, nm.f("blk.%lld.attn_qkv.weight", ll), RAD_BF16, {3 * E, E}, A, N, gl);
|
||||
y.b_qkv = decl_w(b, nm.f("blk.%lld.attn_qkv.bias", ll), RAD_F32, {3 * E}, A, N, gl);
|
||||
y.w_out = decl_w(b, nm.f("blk.%lld.attn_out.weight", ll), RAD_BF16, {E, E}, A, N, gl);
|
||||
y.b_out = decl_w(b, nm.f("blk.%lld.attn_out.bias", ll), RAD_F32, {E}, A, N, gl);
|
||||
y.w_norm2 = decl_w(b, nm.f("blk.%lld.ffn_norm.weight", ll), RAD_F32, {E}, A, N, gl);
|
||||
y.b_norm2 = decl_w(b, nm.f("blk.%lld.ffn_norm.bias", ll), RAD_F32, {E}, A, N, gl);
|
||||
|
||||
if (!y.moe) {
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%lld.ffn_fc1.weight", ll), src("mlp.fc1.weight")));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%lld.ffn_fc1.bias", ll), src("mlp.fc1.bias")));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%lld.ffn_fc2.weight", ll), src("mlp.fc2.weight")));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%lld.ffn_fc2.bias", ll), src("mlp.fc2.bias")));
|
||||
y.w_fc1 = decl_w(b, nm.f("blk.%lld.ffn_fc1.weight", ll), RAD_BF16, {g.n_ff, E},
|
||||
A, N, gl);
|
||||
y.b_fc1 = decl_w(b, nm.f("blk.%lld.ffn_fc1.bias", ll), RAD_F32, {g.n_ff}, A, N, gl);
|
||||
y.w_fc2 = decl_w(b, nm.f("blk.%lld.ffn_fc2.weight", ll), RAD_BF16, {E, g.n_ff},
|
||||
A, N, gl);
|
||||
y.b_fc2 = decl_w(b, nm.f("blk.%lld.ffn_fc2.bias", ll), RAD_F32, {E}, A, N, gl);
|
||||
} else {
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%lld.ffn_router.weight", ll),
|
||||
src("mlp.router.layer.weight")));
|
||||
y.w_router = decl_w(b, nm.f("blk.%lld.ffn_router.weight", ll), RAD_BF16,
|
||||
{m.n_expert, E}, A, N, gl);
|
||||
/* mlp.experts.bias is deliberately not mapped: the verified variant applies no
|
||||
* expert bias, so the tensor is left in the checkpoint. */
|
||||
/* Each projection is a consecutive run of expert handles: all w1 in expert order,
|
||||
* then all w2. A grouped GEMM takes the run's first handle and its length. The name
|
||||
* maps come first: declare reads a weight's planned encoding when the weight is
|
||||
* declared, and a slice mapped afterwards would be read as plain bf16. */
|
||||
for (int64_t e = 0; e < m.n_expert; ++e) {
|
||||
const long long ee = (long long)e;
|
||||
RAD_ARCH_TRY(map_slice(b, nm.f("blk.%lld.ffn_exp_w1.%lld.weight", ll, ee),
|
||||
src("mlp.experts.mlp.w1"), (int)e, g.n_ff));
|
||||
RAD_ARCH_TRY(map_transpose_slice(b, nm.f("blk.%lld.ffn_exp_w2.%lld.weight", ll, ee),
|
||||
src("mlp.experts.mlp.w2"), (int)e, g.n_ff));
|
||||
}
|
||||
y.w_w1.resize((size_t)m.n_expert);
|
||||
y.w_w2.resize((size_t)m.n_expert);
|
||||
for (int64_t e = 0; e < m.n_expert; ++e) {
|
||||
const long long ee = (long long)e;
|
||||
y.w_w1[(size_t)e] = decl_w(b, nm.f("blk.%lld.ffn_exp_w1.%lld.weight", ll, ee),
|
||||
RAD_BF16, {g.n_ff, E}, A, N,
|
||||
grp_expert_slot((int)l, (int)e, 0));
|
||||
if (!y.w_w1[(size_t)e]) return RAD_E_INVAL;
|
||||
}
|
||||
for (int64_t e = 0; e < m.n_expert; ++e) {
|
||||
const long long ee = (long long)e;
|
||||
y.w_w2[(size_t)e] = decl_w(b, nm.f("blk.%lld.ffn_exp_w2.%lld.weight", ll, ee),
|
||||
RAD_BF16, {E, g.n_ff}, A, N,
|
||||
grp_expert_slot((int)l, (int)e, 1));
|
||||
if (!y.w_w2[(size_t)e]) return RAD_E_INVAL;
|
||||
}
|
||||
}
|
||||
|
||||
const rad_buf att_in = kPostNorm ? m.b_x : m.b_h;
|
||||
y.op_qkv = rw(b, gemm_bias_op(3 * E, E, y.w_qkv, y.b_qkv, nullptr),
|
||||
{att_in}, {m.b_qkv});
|
||||
y.op_rope = rw(b, RAD_OP(b, "rope",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("head_dim", g.head_dim),
|
||||
RAD_INT("n_head", g.n_head),
|
||||
RAD_INT("n_head_kv", g.n_head_kv),
|
||||
RAD_F64("theta", meta->rope_theta), RAD_F64("scale", 1.0),
|
||||
RAD_STR("mode", "neox")),
|
||||
RAD_NOWEIGHTS),
|
||||
{m.b_qkv}, {m.b_qkv});
|
||||
y.op_attn = rw(b, RAD_OP(b, "attn_dense",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("head_dim", g.head_dim),
|
||||
RAD_INT("gqa", 1), RAD_INT("causal", 0),
|
||||
RAD_STR("dtype", "bf16"), RAD_F64("scale", 0.125),
|
||||
RAD_INT("max_seqlen", T)),
|
||||
RAD_NOWEIGHTS),
|
||||
{m.b_qkv}, {m.b_att});
|
||||
y.op_out = rw(b, gemm_bias_op(E, E, y.w_out, y.b_out, nullptr),
|
||||
{m.b_att, m.b_x}, {m.b_x});
|
||||
y.op_norm1 = rw(b, layernorm_op(y.w_norm1, y.b_norm1), {m.b_x}, {m.b_h});
|
||||
|
||||
if (!y.moe) {
|
||||
y.op_fc1 = rw(b, gemm_bias_op(g.n_ff, E, y.w_fc1, y.b_fc1, "gelu"),
|
||||
{m.b_h}, {m.b_ff});
|
||||
if constexpr (kPostNorm)
|
||||
y.op_fc2 = rw(b, gemm_bias_op(E, g.n_ff, y.w_fc2, y.b_fc2, nullptr),
|
||||
{m.b_ff, m.b_h}, {m.b_o});
|
||||
else
|
||||
y.op_fc2 = rw(b, gemm_bias_op(E, g.n_ff, y.w_fc2, y.b_fc2, nullptr),
|
||||
{m.b_ff, m.b_x}, {m.b_x});
|
||||
} else {
|
||||
y.op_router = rw(b, gemm_op(m.n_expert, E, y.w_router), {m.b_h}, {m.b_logits});
|
||||
y.op_topk = rw(b, RAD_OP(b, "router_topk",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T),
|
||||
RAD_INT("n_expert", m.n_expert),
|
||||
RAD_INT("top_k", K), RAD_INT("norm", 0),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{m.b_logits}, {m.b_ids, m.b_ew});
|
||||
y.op_scatter = rw(b, RAD_OP(b, "moe_scatter",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T),
|
||||
RAD_INT("n_expert", m.n_expert),
|
||||
RAD_INT("top_k", K),
|
||||
RAD_INT("expert_base", 0)),
|
||||
RAD_NOWEIGHTS),
|
||||
{m.b_ids}, {m.b_sorted, m.b_eoff, m.b_ecnt});
|
||||
y.op_egu = moe_gemm_op(g.n_ff, E, "token", y.w_w1, m.b_h, m.b_egu);
|
||||
y.op_gelu = rw(b, RAD_OP(b, "gelu",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, P), RAD_INT("n", g.n_ff),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{m.b_egu}, {m.b_eact});
|
||||
y.op_edn = moe_gemm_op(E, g.n_ff, "sorted", y.w_w2, m.b_eact, m.b_edn);
|
||||
y.op_gather = rw(b, RAD_OP(b, "moe_gather",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n", E),
|
||||
RAD_INT("top_k", K), RAD_STR("dtype", "bf16"),
|
||||
RAD_STR("act", "none")),
|
||||
RAD_NOWEIGHTS),
|
||||
{m.b_edn, m.b_ew, m.b_sorted}, {m.b_emoe});
|
||||
if constexpr (kPostNorm)
|
||||
y.op_add = add_op(m.b_h, m.b_emoe, m.b_o);
|
||||
else
|
||||
y.op_add = add_op(m.b_x, m.b_emoe, m.b_x);
|
||||
}
|
||||
|
||||
if constexpr (kPostNorm)
|
||||
y.op_norm2 = rw(b, layernorm_op(y.w_norm2, y.b_norm2), {m.b_o}, {m.b_x});
|
||||
else
|
||||
y.op_norm2 = rw(b, layernorm_op(y.w_norm2, y.b_norm2), {m.b_x}, {m.b_h});
|
||||
|
||||
if (!y.op_qkv || !y.op_rope || !y.op_attn || !y.op_out || !y.op_norm1 || !y.op_norm2)
|
||||
return RAD_E_INVAL;
|
||||
if (y.moe && (!y.op_router || !y.op_topk || !y.op_scatter || !y.op_egu || !y.op_gelu ||
|
||||
!y.op_edn || !y.op_gather || !y.op_add))
|
||||
return RAD_E_INVAL;
|
||||
if (!y.moe && (!y.op_fc1 || !y.op_fc2)) return RAD_E_INVAL;
|
||||
}
|
||||
|
||||
m.op_mean = rw(b, RAD_OP(b, "seg_mean",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n", E),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{m.b_x}, {m.b_mean});
|
||||
m.op_cast = rw(b, RAD_OP(b, "cast",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_seqs), RAD_INT("n", E),
|
||||
RAD_STR("from", "bf16"), RAD_STR("to", "f32")),
|
||||
RAD_NOWEIGHTS),
|
||||
{m.b_mean}, {m.b_pooled});
|
||||
|
||||
rad_note(b, "embedding model: %lld layers (%lld routed every %lld), hidden %lld, ffn %lld, "
|
||||
"%lld heads of %lld, %lld experts top %lld; post-norm %d, no expert bias; "
|
||||
"mean-pooled to %lld",
|
||||
(long long)g.n_layer, (long long)m.n_expert, (long long)every, (long long)E,
|
||||
(long long)g.n_ff, (long long)g.n_head, (long long)g.head_dim,
|
||||
(long long)m.n_expert, (long long)m.n_expert_used, kPostNorm ? 1 : 0,
|
||||
(long long)E);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
static void step(RadCtx* c, const RadBatch* batch) {
|
||||
const Model& m = g_model[rad_rank(c)];
|
||||
const Geom& g = m.g;
|
||||
const int64_t T = batch->n_tok, S = batch->n_seq, E = g.n_embd;
|
||||
if (T <= 0 || S <= 0) return;
|
||||
const int64_t K = m.n_expert_used;
|
||||
const RadOperand cu = praw(batch->cu_seqlens, RAD_I32, S + 1);
|
||||
const RadOperand pos = praw(batch->positions, RAD_I32, T);
|
||||
|
||||
RAD_ISSUE(c, m.op_embed, praw(batch->token_ids, RAD_I32, T), RAD_W(m.w_tok),
|
||||
brows(m.b_o, T));
|
||||
RAD_ISSUE(c, m.op_type, brows(m.b_o, T), RAD_W(m.w_type), brows(m.b_o, T));
|
||||
RAD_ISSUE(c, m.op_emb_ln, brows(m.b_o, T), RAD_W(m.w_emb_ln), RAD_W(m.b_emb_ln),
|
||||
brows(m.b_x, T));
|
||||
|
||||
for (int64_t l = 0; l < g.n_layer; ++l) {
|
||||
const Layer& y = m.layers[(size_t)l];
|
||||
|
||||
auto attention = [&] {
|
||||
const rad_buf att_in = kPostNorm ? m.b_x : m.b_h;
|
||||
RAD_ISSUE_N(c, y.op_qkv, T, brows(att_in, T), RAD_W(y.w_qkv), RAD_W(y.b_qkv),
|
||||
RAD_NONE, brows(m.b_qkv, T));
|
||||
RAD_ISSUE(c, y.op_rope, brows(m.b_qkv, T), pos);
|
||||
RAD_ISSUE(c, y.op_attn, bcol(m.b_qkv, 0, E, T), bcol(m.b_qkv, E, E, T),
|
||||
bcol(m.b_qkv, 2 * E, E, T), cu, brows(m.b_att, T));
|
||||
RAD_ISSUE_N(c, y.op_out, T, brows(m.b_att, T), RAD_W(y.w_out), RAD_W(y.b_out),
|
||||
brows(m.b_x, T), brows(m.b_x, T));
|
||||
};
|
||||
|
||||
if constexpr (kPostNorm) {
|
||||
attention();
|
||||
RAD_ISSUE(c, y.op_norm1, brows(m.b_x, T), RAD_W(y.w_norm1), RAD_W(y.b_norm1),
|
||||
brows(m.b_h, T));
|
||||
} else {
|
||||
RAD_ISSUE(c, y.op_norm1, brows(m.b_x, T), RAD_W(y.w_norm1), RAD_W(y.b_norm1),
|
||||
brows(m.b_h, T));
|
||||
attention();
|
||||
}
|
||||
|
||||
if (!y.moe) {
|
||||
RAD_ISSUE_N(c, y.op_fc1, T, brows(m.b_h, T), RAD_W(y.w_fc1), RAD_W(y.b_fc1),
|
||||
RAD_NONE, brows(m.b_ff, T));
|
||||
if constexpr (kPostNorm)
|
||||
RAD_ISSUE_N(c, y.op_fc2, T, brows(m.b_ff, T), RAD_W(y.w_fc2),
|
||||
RAD_W(y.b_fc2), brows(m.b_h, T), brows(m.b_o, T));
|
||||
else
|
||||
RAD_ISSUE_N(c, y.op_fc2, T, brows(m.b_ff, T), RAD_W(y.w_fc2),
|
||||
RAD_W(y.b_fc2), brows(m.b_x, T), brows(m.b_x, T));
|
||||
if constexpr (kPostNorm)
|
||||
RAD_ISSUE(c, y.op_norm2, brows(m.b_o, T), RAD_W(y.w_norm2), RAD_W(y.b_norm2),
|
||||
brows(m.b_x, T));
|
||||
} else {
|
||||
if constexpr (!kPostNorm)
|
||||
RAD_ISSUE(c, y.op_norm2, brows(m.b_x, T), RAD_W(y.w_norm2), RAD_W(y.b_norm2),
|
||||
brows(m.b_h, T));
|
||||
RAD_ISSUE(c, y.op_router, brows(m.b_h, T), RAD_W(y.w_router), brows(m.b_logits, T));
|
||||
RAD_ISSUE(c, y.op_topk, brows(m.b_logits, T), brows(m.b_ids, T), brows(m.b_ew, T));
|
||||
RAD_ISSUE(c, y.op_scatter, brows(m.b_ids, T), brows(m.b_sorted, T * K),
|
||||
brows(m.b_eoff, m.n_expert + 1), brows(m.b_ecnt, m.n_expert));
|
||||
RAD_ISSUE_N(c, y.op_egu, T, brows(m.b_h, T), RAD_WTAB(y.w_w1[0], m.n_expert),
|
||||
brows(m.b_sorted, T * K), brows(m.b_eoff, m.n_expert + 1),
|
||||
brows(m.b_egu, T * K));
|
||||
RAD_ISSUE_N(c, y.op_gelu, T * K, brows(m.b_egu, T * K), brows(m.b_eact, T * K));
|
||||
RAD_ISSUE_N(c, y.op_edn, T, brows(m.b_eact, T * K), RAD_WTAB(y.w_w2[0], m.n_expert),
|
||||
brows(m.b_sorted, T * K), brows(m.b_eoff, m.n_expert + 1),
|
||||
brows(m.b_edn, T * K));
|
||||
RAD_ISSUE_N(c, y.op_gather, T, brows(m.b_edn, T * K), brows(m.b_ew, T),
|
||||
brows(m.b_sorted, T * K), RAD_NONE, RAD_NONE, brows(m.b_emoe, T));
|
||||
if constexpr (kPostNorm)
|
||||
RAD_ISSUE(c, y.op_add, brows(m.b_h, T), brows(m.b_emoe, T), brows(m.b_o, T));
|
||||
else
|
||||
RAD_ISSUE(c, y.op_add, brows(m.b_x, T), brows(m.b_emoe, T), brows(m.b_x, T));
|
||||
if constexpr (kPostNorm)
|
||||
RAD_ISSUE(c, y.op_norm2, brows(m.b_o, T), RAD_W(y.w_norm2), RAD_W(y.b_norm2),
|
||||
brows(m.b_x, T));
|
||||
}
|
||||
}
|
||||
|
||||
RAD_ISSUE(c, m.op_mean, brows(m.b_x, T), cu, brows(m.b_mean, S));
|
||||
RAD_ISSUE_N(c, m.op_cast, S, brows(m.b_mean, S), brows(m.b_pooled, S));
|
||||
}
|
||||
|
||||
} /* namespace nomicbert */
|
||||
|
||||
#ifndef RAD_ARCH_NO_EXPORTS
|
||||
RAD_ARCH_PLUGIN(nomicbert, "nomicbert", "", "0.1.0",
|
||||
"Nomic BERT (nomic-embed-text-v2-moe): a bidirectional post-norm encoder with "
|
||||
"NeoX rotary attention, dense and routed GELU feed-forwards, mean-pooled to one "
|
||||
"vector an input; bf16")
|
||||
RAD_ARCH_PROBE(nomicbert)
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
# nomic-embed-v2-moe-bf16.recipe -- nomic-embed-text-v2-moe from the HF checkpoint, as the
|
||||
# nomicbert plugin serves it.
|
||||
#
|
||||
# rad-convert /data --recipe data/recipes/nomic-embed-v2-moe-bf16.recipe -o nomic.rad
|
||||
#
|
||||
# The checkpoint stores every tensor f32 (config.json says torch_dtype float32). The plugin runs
|
||||
# the linears, the token table and the token-type table in bf16 and keeps norms and biases f32, so
|
||||
# this recipe casts exactly the weights that plugin declares bf16. Without it rad-convert refuses
|
||||
# the checkpoint at declare: a declare-time bf16 weight cannot hold an f32 checkpoint's codes.
|
||||
#
|
||||
# token_type.weight is bf16 because libr4d's table_add_bf16 reads a bf16 table; libref's and
|
||||
# libavx's table_add read any dtype the tensor carries, so the same container serves all three.
|
||||
# The checkpoint's mlp.experts.bias tensors are not mapped by the plugin at all and do not appear
|
||||
# in the plan.
|
||||
|
||||
token_embd.weight cast dtype=bf16
|
||||
token_type.weight cast dtype=bf16
|
||||
blk.*.attn_qkv.weight cast dtype=bf16
|
||||
blk.*.attn_out.weight cast dtype=bf16
|
||||
blk.*.ffn_fc1.weight cast dtype=bf16
|
||||
blk.*.ffn_fc2.weight cast dtype=bf16
|
||||
blk.*.ffn_router.weight cast dtype=bf16
|
||||
blk.*.ffn_exp_w1.*.weight cast dtype=bf16
|
||||
blk.*.ffn_exp_w2.*.weight cast dtype=bf16
|
||||
@@ -20,6 +20,7 @@ plugin claims.
|
||||
| `arch/glm5next` | `glm5next` | *(empty)* | GLM-5.3-Flash from AMD's Quark MXFP4 export: four mHC residual streams over 34 KDA and 11 sparse latent-attention (MLA) layers with a DSA indexer, 288 MXFP4 experts at top-8 under sigmoid routing, block-FP8 attention and dense layers at f32 scales, each weight served as stored; its MTP layer is a serial drafter. Pictures through its vision tower (`arch/common/rad_block_glm_vision.h`); bf16 KV |
|
||||
| `arch/lfm2_bf16` | `lfm2` | *(empty)* | LiquidAI's LFM2 and LFM2-VL, bf16, one rank |
|
||||
| `arch/embgemma2` | `embeddinggemma2` | *(empty)* | EmbeddingGemma 2: a bidirectional encoder with Gemma 4's vision tower and audio conformer, pooled to one vector |
|
||||
| `arch/nomicbert` | `nomicbert` | *(empty)* | nomic-embed-text-v2-moe: a bidirectional post-norm encoder, 12 layers with 8 experts at top 2 on every second layer, mean-pooled to one vector an input |
|
||||
| `arch/gemma4_bf16` | `gemma4` | `compressed-tensors` | Gemma 4 31B dense, and the E2B/E4B form at one rank, with its vision tower: sliding-window and proportional-RoPE global (`attention_k_eq_v`) attention, GELU ffn, softcapped logits; int4 codes with an f16 scale per 128 from Google's CT-INT4 (QAT) release or from bf16, served w4a8, or int8 codes on the same grid from bf16 (w8a8, the Q8 container). Optional draft model (`rad-convert --draft-model`): Google's assistant, with no cache of its own (`rad_drafter_shared_kv`), or z-lab's DFlash drafter, the block `arch/gemma4` serves (`arch/common/rad_gemma4_dflash.h`), or RedHatAI's DFlash2 speculator (`arch/common/rad_block_dflash2.h`) |
|
||||
| `arch/gemma3` | `gemma3` | *(empty)* | Gemma 3 dense text (TranslateGemma 27B) from the bf16 checkpoint: sliding-window and global attention at one head geometry, GemmaRMSNorm, GELU ffn; linears bf16, or int8, MXFP4 or asymmetric int4 over an activation scaled per (row, 128), as the recipe stores them. Optional DFlash2 drafter (`rad-convert --draft-model`) |
|
||||
| `arch/gemma4` | `gemma4` | *(empty)* | Gemma 4 text from Google's bf16 checkpoint (the 26B-A4B MoE and the dense sizes without per-layer embeddings) with z-lab's DFlash drafter; routed experts bf16 or block fp8 |
|
||||
@@ -852,6 +853,79 @@ Traps:
|
||||
|
||||
---
|
||||
|
||||
# `nomicbert`: an embedding model
|
||||
|
||||
`arch/nomicbert` declares a pooled output (`rad_declare_pooled`) and no logits, so it is an
|
||||
embedding program like `embgemma2`: no sampler, no KV cache, one step per input, and the pooled
|
||||
row on `/v1/embeddings` ([GUIDE §5.6](GUIDE.md)). One card (`--tp 1`).
|
||||
|
||||
```
|
||||
text 12 layers, hidden 768, ffn 3072, vocab 250048, 12 heads of 64, type vocab 1
|
||||
trained to 2048 positions; 475M parameters
|
||||
post-norm block: a = out_proj(attn(rope(Wqkv h + b))) + b_out; h = LN1(h + a)
|
||||
even layers: dense MLP, fc2(gelu(fc1 h + b1)) + b2, exact gelu
|
||||
odd layers (1, 3, 5, 7, 9, 11): 8 experts at top 2, softmax weights,
|
||||
no renormalisation, no shared expert
|
||||
fused Wqkv [q|k|v], no GQA, non-causal, scale 1/sqrt(64) = 0.125
|
||||
NeoX rotary over all 64 dimensions, theta 1e4, on q and k only, no position table
|
||||
embeddings: word + token_type, then emb_ln; no final norm
|
||||
mean over every real token, cast to the f32 pooled buffer; the server L2-normalises
|
||||
```
|
||||
|
||||
The block order is the one the verified variant uses: attention on the raw stream, then LN1, the
|
||||
feed-forward on LN1's output, then LN2 over that output plus the feed-forward's. The checkpoint's
|
||||
shared `experts.bias` is not applied, and the plugin does not map it. A container that sets
|
||||
`prenorm`, `causal`, interleaved, partial or scaled rotary, a gelu variant, renormalised routing
|
||||
or a shared expert is refused at declare.
|
||||
|
||||
**Tensor names.** `N` is a layer; even layers are dense and odd layers are routed. `E` is an
|
||||
expert. Norms and biases stay f32; the token-type table is bf16 because `table_add`'s device row
|
||||
reads a bf16 table. The token embedding, linears, router and expert tables are bf16.
|
||||
|
||||
| container | checkpoint | shape | map |
|
||||
|---|---|---|---|
|
||||
| `token_embd.weight` | `embeddings.word_embeddings.weight` | [250048, 768] | copy |
|
||||
| `token_type.weight` | `embeddings.token_type_embeddings.weight` | [1, 768] | copy |
|
||||
| `emb_ln.*` | `emb_ln.*` | [768] | copy |
|
||||
| `blk.N.attn_norm.*`, `blk.N.ffn_norm.*` | `encoder.layers.N.norm1.*`, `encoder.layers.N.norm2.*` | [768] | copy |
|
||||
| `blk.N.attn_qkv.*`, `blk.N.attn_out.*` | `encoder.layers.N.attn.Wqkv.*`, `encoder.layers.N.attn.out_proj.*` | [2304, 768]/[2304], [768, 768]/[768] | copy |
|
||||
| `blk.N.ffn_fc1.*`, `blk.N.ffn_fc2.*` (even N) | `encoder.layers.N.mlp.fc1.*`, `encoder.layers.N.mlp.fc2.*` | [3072, 768]/[3072], [768, 3072]/[768] | copy |
|
||||
| `blk.N.ffn_router.weight` (odd N) | `encoder.layers.N.mlp.router.layer.weight` | [8, 768] | copy |
|
||||
| `blk.N.ffn_exp_w1.E.weight` (odd N) | `encoder.layers.N.mlp.experts.mlp.w1` | [3072, 768] | slice E |
|
||||
| `blk.N.ffn_exp_w2.E.weight` (odd N) | `encoder.layers.N.mlp.experts.mlp.w2` | [768, 3072] | slice E, transpose |
|
||||
| not declared (odd N) | `encoder.layers.N.mlp.experts.bias` | [768] | not mapped |
|
||||
|
||||
The checkpoint stores `w1` and `w2` as one stacked tensor each, `[24576, 768]`; `E` takes the
|
||||
expert's dim-0 slice, and `w2` is transposed to `[768, 3072]` for the grouped GEMM. Every expert
|
||||
has two group slots, one for each projection. Nothing else is mapped: there is no final norm, no
|
||||
pooler, no position table and no output head.
|
||||
|
||||
**The recipe.** The checkpoint is f32 (1.9 GB).
|
||||
|
||||
```sh
|
||||
rad-convert <checkpoint dir> --recipe data/recipes/nomic-embed-v2-moe-bf16.recipe \
|
||||
-o nomic-embed-v2-moe-bf16.rad
|
||||
```
|
||||
|
||||
It takes 3.5 s and writes a 959 MB container (907 MiB of weights). The checkpoint directory must
|
||||
contain `tokenizer.json`. The recipe casts the token embedding, the token-type table, the
|
||||
attention, dense MLP and expert weights to bf16 and leaves the norms and biases f32.
|
||||
|
||||
**Verified.** 20 texts against a pure-float32 reference: mean cosine 0.99995, min 0.99983 on a
|
||||
gfx1201 card, with libavx and libref the same to 4 decimals. Against a Q6_K quantisation of the
|
||||
same model the mean is 0.9944 (min 0.990); the Q8_0 one agrees with the reference at 0.9994, so
|
||||
the Q6_K quantisation is the outlier, about 0.006 off the true model, and stored vectors from a
|
||||
Q6_K server drift by about that much.
|
||||
|
||||
**Measured.** On one gfx1201 card, short texts take 2 ms a request at one client, 3854 requests/s
|
||||
at 64 concurrent clients and 4400 texts/s for a batch of 32 in one request; a 1354-character text
|
||||
takes 3 ms; about 1.2 GB of VRAM. On 24 CPU cores libavx has a median 18 ms a text.
|
||||
|
||||
**Not verified.** Inputs above 512 tokens against a device reference, other precisions, and tensor
|
||||
parallel (the model is served on one card).
|
||||
|
||||
---
|
||||
|
||||
# `gemma4`: Gemma 4 text and its DFlash drafter
|
||||
|
||||
`arch/gemma4` serves a container converted from Google's bf16 checkpoint
|
||||
|
||||
+11
-3
@@ -281,6 +281,7 @@ and runs.
|
||||
| `gemma4_bf16` | Gemma 4 dense in bf16, with its vision tower |
|
||||
| `lfm2_bf16` | LiquidAI LFM2 and LFM2-VL in bf16, including d1 (§5.13) |
|
||||
| `embgemma2` | EmbeddingGemma 2, an embedding model (§5.6) |
|
||||
| `nomicbert` | nomic-embed-text-v2-moe, an embedding model (§5.6) |
|
||||
| `qwenimage21` | Qwen-Image 2.1 and 2.1-Turbo, an image model (§5.6) |
|
||||
| `krea2` | Krea 2 Raw and Turbo, an image model from text (§5.6) |
|
||||
| `anima` | Anima Base, Aesthetic and Turbo, an anime and illustration image model from text (§5.6) |
|
||||
@@ -2233,9 +2234,16 @@ token ids mixed, with its `add_special` and `parse_special` and their defaults);
|
||||
returns `tokens` as llama-server does, as `{id, piece}` objects with `with_pieces`. `/detokenize`
|
||||
returns the text as both `prompt` (vLLM) and `content` (llama-server).
|
||||
|
||||
**An embedding model** (EmbeddingGemma 2) serves `/v1/embeddings` only; completions and chat answer
|
||||
501. One input is one vector and must fit one step (`--max-num-batched-tokens`); a longer one is
|
||||
refused with `context_length_exceeded`.
|
||||
**An embedding model** (EmbeddingGemma 2, nomic-embed-text-v2-moe) serves `/v1/embeddings` only;
|
||||
completions and chat answer 501. One input is one vector and must fit one step
|
||||
(`--max-num-batched-tokens`); a longer one is refused with `context_length_exceeded`.
|
||||
|
||||
nomic-embed-text-v2-moe is converted from its f32 checkpoint with `rad-convert <checkpoint dir>
|
||||
--recipe data/recipes/nomic-embed-v2-moe-bf16.recipe -o nomic-embed-v2-moe-bf16.rad` (959 MB);
|
||||
the directory must contain `tokenizer.json`. The `search_query: ` and `search_document: ` prefixes
|
||||
are the client's text and the engine adds neither; the checkpoint's prompt names work through
|
||||
`prompt_name`. The checkpoint's shared `experts.bias` tensors are not applied and stay in the
|
||||
checkpoint.
|
||||
|
||||
```sh
|
||||
# text, a batch of texts, or token ids
|
||||
|
||||
@@ -62,6 +62,8 @@
|
||||
#include "../arch/llama_dense_fp8/llama_dense_fp8.cpp"
|
||||
/* EmbeddingGemma 2: a pooled encoder with no logits. */
|
||||
#include "../arch/embgemma2/embgemma2.cpp"
|
||||
/* Nomic BERT: a pooled encoder with dense and routed post-norm layers. */
|
||||
#include "../arch/nomicbert/nomicbert.cpp"
|
||||
/* Qwen-Image 2.1: a generator conditioned by Qwen3-VL. */
|
||||
#include "../arch/qwenimage21/qwenimage21.cpp"
|
||||
/* Anima: a cross-attention generator conditioned through two tokenizers. */
|
||||
@@ -9609,6 +9611,182 @@ TEST(gemma4_e4b_declares_its_audio_encoder) {
|
||||
CHECK_EQ(pr.audio_frames, 3000);
|
||||
}
|
||||
|
||||
/* ==================================================================== nomicbert */
|
||||
|
||||
namespace {
|
||||
|
||||
/* Nomic BERT cut to two layers: one dense and one routed at moe_every_n_layers 2, two heads of
|
||||
* 32, ffn 128, four experts top 2, vocab 128. Its config spells the dimensions n_* and reads
|
||||
* post-norm. */
|
||||
MetaKV nb_meta() {
|
||||
RadModelMeta m{};
|
||||
m.arch_id = "nomicbert"; m.name = "test-nomicbert"; m.quant = "";
|
||||
m.n_layers = 2; m.n_embd = 64; m.n_head = 2; m.n_head_kv = 2; m.head_dim = 32;
|
||||
m.n_ff = 128; m.n_vocab = 128; m.n_ctx_train = 2048;
|
||||
m.rms_eps = 1e-5f; m.rope_theta = 1e4f; m.rope_scale = 1.0f;
|
||||
m.n_expert = 4; m.n_expert_used = 2;
|
||||
MetaKV k(m);
|
||||
k.set("model_type", "nomic_bert")
|
||||
.set("moe_every_n_layers", "2")
|
||||
.set("moe_normalize_expert_weights", "0")
|
||||
.set("num_shared_experts", "0")
|
||||
.set("prenorm", "0")
|
||||
.set("causal", "0")
|
||||
.set("activation_function", "gelu")
|
||||
.set("rotary_emb_interleaved", "0")
|
||||
.set("rotary_emb_fraction", "1.0")
|
||||
.set("type_vocab_size", "1");
|
||||
return k;
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* A post-norm encoder: word and token-type embeddings, emb_ln, then two layers, one dense and one
|
||||
* with a top-2 router over four experts, each layer's attention bidirectional at 1/sqrt(64) over
|
||||
* NeoX rotary, and a mean pool cast into the f32 pooled buffer. No logits and no lm_head. */
|
||||
TEST(nomicbert_declares_a_pooled_post_norm_encoder) {
|
||||
MetaKV mk = nb_meta();
|
||||
RadBuilder b;
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
REQUIRE_EQ(nomicbert::declare(&b, mk.meta(), &ctx), RAD_OK);
|
||||
CHECK_EQ(b.logits, (rad_buf)0);
|
||||
REQUIRE(b.pooled != 0);
|
||||
const RadBufDecl& p = b.bufs[b.pooled - 1].d;
|
||||
CHECK_EQ(p.dtype, (uint32_t)RAD_F32);
|
||||
CHECK_EQ(p.shape[0], ctx.max_seqs);
|
||||
CHECK_EQ(p.shape[1], (int64_t)64);
|
||||
|
||||
CHECK_EQ(count_op(b, "logits_gemm"), 0);
|
||||
CHECK_EQ(count_op(b, "embed_lookup"), 1);
|
||||
CHECK_EQ(count_op(b, "table_add"), 1); /* the token-type table only */
|
||||
CHECK_EQ(count_op(b, "layernorm"), 5); /* emb_ln and two norms on each of two layers */
|
||||
CHECK_EQ(count_op(b, "gemm_nt_bias"), 6); /* qkv and out twice, fc1 and fc2 once */
|
||||
CHECK_EQ(count_op(b, "gemm_nt"), 1); /* the router */
|
||||
CHECK_EQ(count_op(b, "rope"), 2);
|
||||
CHECK_EQ(count_op(b, "attn_dense"), 2);
|
||||
CHECK_EQ(count_op(b, "router_topk"), 1);
|
||||
CHECK_EQ(count_op(b, "moe_scatter"), 1);
|
||||
CHECK_EQ(count_op(b, "moe_gemm"), 2);
|
||||
CHECK_EQ(count_op(b, "gelu"), 1);
|
||||
CHECK_EQ(count_op(b, "moe_gather"), 1);
|
||||
CHECK_EQ(count_op(b, "add"), 1); /* the routed residual */
|
||||
CHECK_EQ(count_op(b, "seg_mean"), 1);
|
||||
CHECK_EQ(count_op(b, "cast"), 1);
|
||||
|
||||
std::vector<std::string> orders;
|
||||
for (const auto& o : b.ops) {
|
||||
if (o.op == "attn_dense") {
|
||||
CHECK_EQ(pint(&o, "head_dim"), 32);
|
||||
CHECK_EQ(pint(&o, "gqa"), 1);
|
||||
CHECK_EQ(pint(&o, "causal"), 0);
|
||||
CHECK_EQ(pint(&o, "max_seqlen"), (long long)ctx.max_tok);
|
||||
CHECK_NEAR(pflt(&o, "scale"), 0.125, 0.0);
|
||||
}
|
||||
if (o.op == "router_topk") {
|
||||
CHECK_EQ(pint(&o, "norm"), 0);
|
||||
CHECK_EQ(pint(&o, "n_expert"), 4);
|
||||
CHECK_EQ(pint(&o, "top_k"), 2);
|
||||
}
|
||||
if (o.op == "moe_gemm") {
|
||||
CHECK_EQ(pint(&o, "n_expert"), 4);
|
||||
CHECK_EQ(pint(&o, "top_k"), 2);
|
||||
orders.push_back(pstr(&o, "a_order"));
|
||||
}
|
||||
if (o.op == "table_add") {
|
||||
CHECK_EQ(pint(&o, "n"), 64);
|
||||
CHECK_EQ(pint(&o, "L"), 1);
|
||||
CHECK_EQ(pstr(&o, "dtype"), std::string("bf16"));
|
||||
REQUIRE_EQ(o.w.size(), (size_t)1);
|
||||
CHECK_EQ(b.weights[o.w[0] - 1].name, std::string("token_type.weight"));
|
||||
CHECK_EQ(b.weights[o.w[0] - 1].d.dtype, (uint32_t)RAD_BF16);
|
||||
}
|
||||
}
|
||||
CHECK(orders == (std::vector<std::string>{ "token", "sorted" }));
|
||||
|
||||
/* Every declared weight has a name map, and each projection's expert handles are one
|
||||
* consecutive run with a slot per projection. */
|
||||
for (const auto& w : b.weights) CHECK(find_map(b, w.name) != nullptr);
|
||||
/* The dtypes the device rows require. table_add_bf16 reads a bf16 table, so the token-type
|
||||
* table is bf16; every linear and the token table are bf16. layernorm and gemm_nt_bias take
|
||||
* an f32 gain or bias, so norms and biases stay f32. */
|
||||
const RecWeight* tt = find_w(b, "token_type.weight");
|
||||
REQUIRE(tt != nullptr);
|
||||
CHECK_EQ(tt->d.dtype, (uint32_t)RAD_BF16);
|
||||
const RecWeight* tw = find_w(b, "token_embd.weight");
|
||||
REQUIRE(tw != nullptr);
|
||||
CHECK_EQ(tw->d.dtype, (uint32_t)RAD_BF16);
|
||||
for (const char* n : { "emb_ln.weight", "emb_ln.bias", "blk.0.attn_norm.weight",
|
||||
"blk.0.attn_norm.bias", "blk.0.attn_qkv.bias",
|
||||
"blk.0.attn_out.bias", "blk.0.ffn_norm.weight",
|
||||
"blk.0.ffn_norm.bias", "blk.0.ffn_fc1.bias",
|
||||
"blk.0.ffn_fc2.bias", "blk.1.ffn_norm.weight",
|
||||
"blk.1.ffn_norm.bias" }) {
|
||||
const RecWeight* w = find_w(b, n);
|
||||
REQUIRE(w != nullptr);
|
||||
CHECK_EQ(w->d.dtype, (uint32_t)RAD_F32);
|
||||
}
|
||||
for (const char* n : { "blk.0.attn_qkv.weight", "blk.0.attn_out.weight",
|
||||
"blk.0.ffn_fc1.weight", "blk.0.ffn_fc2.weight",
|
||||
"blk.1.attn_qkv.weight", "blk.1.attn_out.weight",
|
||||
"blk.1.ffn_router.weight", "blk.1.ffn_exp_w1.0.weight",
|
||||
"blk.1.ffn_exp_w2.0.weight" }) {
|
||||
const RecWeight* w = find_w(b, n);
|
||||
REQUIRE(w != nullptr);
|
||||
CHECK_EQ(w->d.dtype, (uint32_t)RAD_BF16);
|
||||
}
|
||||
auto index_of = [&](const std::string& name) {
|
||||
for (size_t i = 0; i < b.weights.size(); ++i) if (b.weights[i].name == name) return (int)i;
|
||||
return -1;
|
||||
};
|
||||
const int w10 = index_of("blk.1.ffn_exp_w1.0.weight");
|
||||
REQUIRE(w10 >= 0);
|
||||
for (int e = 0; e < 4; ++e) {
|
||||
const std::string n1 = "blk.1.ffn_exp_w1." + std::to_string(e) + ".weight";
|
||||
const std::string n2 = "blk.1.ffn_exp_w2." + std::to_string(e) + ".weight";
|
||||
const RecWeight* a = find_w(b, n1);
|
||||
const RecWeight* c = find_w(b, n2);
|
||||
REQUIRE(a != nullptr && c != nullptr);
|
||||
CHECK_EQ(index_of(n1), w10 + e);
|
||||
CHECK_EQ(index_of(n2), w10 + 4 + e);
|
||||
CHECK_EQ(a->d.group.expert, e);
|
||||
CHECK_EQ(a->d.group.slot, 0);
|
||||
CHECK_EQ(c->d.group.expert, e);
|
||||
CHECK_EQ(c->d.group.slot, 1);
|
||||
CHECK_EQ(a->d.shape[0], (int64_t)128);
|
||||
CHECK_EQ(a->d.shape[1], (int64_t)64);
|
||||
CHECK_EQ(c->d.shape[0], (int64_t)64);
|
||||
CHECK_EQ(c->d.shape[1], (int64_t)128);
|
||||
}
|
||||
}
|
||||
|
||||
/* The text pass over three whole inputs lints clean and its last issue writes the pooled rows. */
|
||||
TEST(nomicbert_text_pass_lints_clean_and_ends_at_the_pooled_buffer) {
|
||||
MetaKV mk = nb_meta();
|
||||
RadBuilder b;
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
REQUIRE_EQ(nomicbert::declare(&b, mk.meta(), &ctx), RAD_OK);
|
||||
const auto text = kit(b, {}, { 12, 7, 30 });
|
||||
RadCtx c{ .b = &b, .batch = &text->b };
|
||||
nomicbert::step(&c, &text->b);
|
||||
CHECK(c.step_fail.empty());
|
||||
CHECK(!c.issues.empty());
|
||||
CHECK_LINT(b, &c);
|
||||
const RecIssue& last = c.issues.back();
|
||||
CHECK_EQ(last.n, (int64_t)3);
|
||||
CHECK_EQ(last.opd.back().handle, b.pooled);
|
||||
for (const auto& op : issued_ops(b, c)) CHECK(op != std::string("logits_gemm"));
|
||||
}
|
||||
|
||||
/* The probe accepts the config's model_type spelling and refuses another architecture. */
|
||||
TEST(nomicbert_probe_reads_the_model_type) {
|
||||
MetaKV mk = nb_meta();
|
||||
RadArchProbe p{};
|
||||
CHECK_OK(nomicbert::probe(mk.meta(), &p));
|
||||
MetaKV other = nb_meta().set("model_type", "bert");
|
||||
CHECK_EQ(stderr_of_rc([&] { return nomicbert::probe(other.meta(), &p); }),
|
||||
RAD_E_UNSUPPORTED);
|
||||
}
|
||||
|
||||
/* ==================================================================== qwenimage21 */
|
||||
|
||||
namespace {
|
||||
@@ -10193,6 +10371,23 @@ const std::set<std::string>& libref_vocab() {
|
||||
return v;
|
||||
}
|
||||
|
||||
/* Nomic BERT declares only ops libref serves, so a CPU-only engine can start it. */
|
||||
TEST(nomicbert_declares_on_librefs_vocabulary) {
|
||||
const std::set<std::string>& vocab = libref_vocab();
|
||||
if (vocab.empty()) {
|
||||
fprintf(stderr, " SKIP: libref.so not found under $RADIANCE_HOME/kernels\n");
|
||||
return;
|
||||
}
|
||||
MetaKV mk = nb_meta();
|
||||
RadBuilder b;
|
||||
b.vocab = &vocab;
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
REQUIRE_EQ(stderr_of_rc([&] { return nomicbert::declare(&b, mk.meta(), &ctx); }), RAD_OK);
|
||||
for (const auto& op : b.asked_undeclared)
|
||||
fprintf(stderr, " nomicbert asks for '%s'\n", op.c_str());
|
||||
CHECK(b.asked_undeclared.empty());
|
||||
}
|
||||
|
||||
/* One architecture and configuration. `pinned` lists the device-only ops it may still ask for on
|
||||
* libref's vocabulary: an upper bound that may only shrink. An op libref gains drops out of the
|
||||
* expectation by itself; one the plugin stops asking for must be removed here. */
|
||||
|
||||
@@ -96,7 +96,7 @@ TEST(the_shipped_architectures_load_with_distinct_selection_keys) {
|
||||
{ "gemma4", "compressed-tensors" }, { "glm5next", "" },
|
||||
{ "hyv3", "" }, { "hyv3", "fp8" }, { "hyv3", "gguf_ftype_15" },
|
||||
{ "lfm2", "" }, { "llama", "" },
|
||||
{ "llama", "fp8_e4m3" }, { "qwen35", "" },
|
||||
{ "llama", "fp8_e4m3" }, { "nomicbert", "" }, { "qwen35", "" },
|
||||
{ "qwen35", "fp8_e4m3" }, { "qwen35", "w8a8_fwht128" }, { "qwen35moe", "fp8_e4m3" },
|
||||
{ "qwen3asr", "" }, { "krea2", "" }, { "ltx2", "" }, { "minimaxh3", "" },
|
||||
{ "minimaxmusic3", "" }, { "qwen4exp", "" }, { "qwenimage21", "" }, { "sdxl", "" },
|
||||
|
||||
Reference in new issue
Block a user