diff --git a/docs/ARCHITECTURES.md b/docs/ARCHITECTURES.md index 65dec245..f265ec21 100644 --- a/docs/ARCHITECTURES.md +++ b/docs/ARCHITECTURES.md @@ -30,6 +30,7 @@ plugin claims. | `arch/bailing_hybrid_gguf_q6k` | `bailinghybrid` | `gguf_ftype_18` | the same tiny family, Q6_K GGUF release, served whole | | `arch/nemotronh` | `nemotronh` | *(empty)* | Nemotron-3.5-Bolt-74B-A9B (`nemotron_h_omni` around `nemotron_h_puzzle`), its language model from the bf16 checkpoint: 40 Mamba-2, 40 latent-MoE and 8 NoPE attention blocks in the order `layers_block_type` gives, 240 to 496 ungated relu^2 experts a layer in a 1024-wide latent with a relu^2 shared expert; linears and experts as the recipe stores them; pictures and video through its C-RADIO tower (`arch/nemotronh/radio.h`); bf16 KV | | `arch/lfm2_bf16` | `lfm2` | *(empty)* | LiquidAI's LFM2 and LFM2-VL, bf16 or IQ8R, one rank | +| `arch/lfm3_bf16` | `lfm3` | *(empty)* | LiquidAI's LFM3 (LFM3-1.7B), bf16, one rank: LFM2's short convolutions with full and sliding-window (256) GQA attention, sandwich norms, a partial rotary on the full layers and a sigmoid gate per query head. Text only; bf16 linears | | `arch/spark25_bf16` | `spark25` | *(empty)* | Spark X2.6, bf16 checkpoint, mixed sliding/full attention, TP1 or TP with exact collectives ([geometry](#spark25-spark-x26)). Optional DSpark drafter when the container already holds `draft.*` tensors, TP1 only. | | `arch/spark25_w8a8` | `spark25` | `w8a8_fwht128` | the same with rotated int8 weights and activations (`data/recipes/spark-x26-4b-iq8r.recipe`); an optional DSpark drafter stays bf16 | | `arch/embgemma2` | `embeddinggemma2` | *(empty)* | EmbeddingGemma 2: a bidirectional encoder with Gemma 4's vision tower and audio conformer, pooled to one vector | diff --git a/docs/GUIDE.md b/docs/GUIDE.md index a461b8bc..a10b9549 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -317,6 +317,7 @@ and runs. | `museglimmer` | Meta Muse Glimmer 30B: gated attention alternating sliding-window (rotary) and NoPE full layers, Gemma-2 double norms, a 50-block windowed ViT; bf16 | | `museglimmer_ct` | the same trunk as an int4 container (`--set quantization_config.quant_method=compressed-tensors`) | | `lfm2_bf16` | LiquidAI LFM2 and LFM2-VL in bf16, including d1 (§5.13) | +| `lfm3_bf16` | LiquidAI LFM3 in bf16 (LFM3-1.7B) | | `embgemma2` | EmbeddingGemma 2, an embedding model (§5.6) | | `nomicbert` | nomic-embed-text-v2-moe, an embedding model (§5.6) | | `nemotron35_embed` | Nemotron 3.5 Embed 1B/8B and Rerank 8B, BF16 text/image retrieval at TP1 (§5.6) | diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5f0390c2..550cb494 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -366,7 +366,7 @@ endif() # the host backend; convert_test runs rad-convert on the same checkpoints. Both start the built # binaries as children, so they depend on them and on every plugin the checkpoints select # (llama_fp8 for convert_test's --set of an fp8 quantization_config). -set(RAD_E2E_DEPS radiance rad-convert libref libquant nemotron35_embed qwen35_bf16 lfm2_bf16 gemma4 gemma4_bf16 +set(RAD_E2E_DEPS radiance rad-convert libref libquant nemotron35_embed qwen35_bf16 lfm2_bf16 lfm3_bf16 gemma4 gemma4_bf16 llama_dense_fp8 llama_fp8 qwenimage21) if(TARGET libavx) list(APPEND RAD_E2E_DEPS libavx) diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index 2d2fc1db..af8629d6 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -25,6 +25,8 @@ #include "../arch/llama_fp8/llama_fp8.cpp" /* LFM2, for its own layer schedule and short convolution. */ #include "../arch/lfm2_bf16/lfm2_bf16.cpp" +/* LFM3: LFM2's schedule with sliding-window layers, sandwich norms and gated attention. */ +#include "../arch/lfm3_bf16/lfm3_bf16.cpp" /* Gemma 4 dense: two attention classes (sliding-window + proportional-RoPE global) on one stream. */ #include "../arch/gemma4_bf16/gemma4_bf16.cpp" /* The bf16 / fp8-expert Gemma 4 plugin: the 26B-A4B's dense and routed arms side by side. */ @@ -4415,6 +4417,249 @@ TEST(lfm2_issues_the_convolution_once_per_half_of_a_mixed_step) { CHECK_EQ(convs, 6); /* three conv layers, two halves each */ } +/* ==================================================================== LFM3 */ +namespace { + +/* 1.7B's graph cut to four layers, one of each kind after a leading conv: conv, full, sliding, + * conv. The rotary lives under rope_parameters. only. */ +const char* k_lfm3_keys[] = { "layer_types", "conv_L_cache", "tie_word_embeddings", + "sliding_window", + "rope_parameters.full_attention.rope_theta", + "rope_parameters.full_attention.partial_rotary_factor", + "rope_parameters.sliding_attention.rope_theta" }; +const char* k_lfm3_vals[] = { "conv full_attention sliding_attention conv", "7", "true", "256", + "1000000.0", "0.5", "10000" }; + +RadModelMeta lfm3_meta() { + RadModelMeta m{}; + m.arch_id = "lfm3"; m.name = "test-lfm3"; m.quant = ""; + m.n_layers = 4; m.n_embd = 64; m.n_head = 4; m.n_head_kv = 1; m.head_dim = 16; + m.n_ff = 128; m.n_vocab = 512; m.n_ctx_train = 32768; + /* the typed theta is the converter's fallback for a config without a top-level one: not read */ + m.rms_eps = 1e-5f; m.rope_theta = 1e4f; m.rope_scale = 1.0f; + m.n_kv = 7; m.kv_key = k_lfm3_keys; m.kv_val = k_lfm3_vals; + return m; +} + +/* A layer's ops: the mixer between its two norms, then the feed-forward between its two. */ +std::vector lfm3_layer_ops(bool attention) { + std::vector v = { "rmsnorm" }; + if (attention) { + for (const char* s : { "gemm_nt", "gemm_nt", "gemm_nt", "gemm_nt", "rmsnorm_rows", + "rmsnorm_rows", "rope_rows", "rope_rows", "attn_paged", "kv_store", + "sigmoid", "mul", "gemm_nt" }) + v.push_back(s); + } else { + for (const char* s : { "gemm_nt", "mul", "causal_conv1d", "mul", "gemm_nt" }) + v.push_back(s); + } + for (const char* s : { "rmsnorm", "add", "rmsnorm", "gemm_nt", "silu_mul", "gemm_nt", "rmsnorm", + "add" }) + v.push_back(s); + return v; +} + +} /* namespace */ + +TEST(lfm3_declares_three_layer_kinds_with_their_own_rotary_and_window) { + RadBuilder b; + RadModelMeta meta = lfm3_meta(); + RadBuildCtx ctx = build_ctx(); + CHECK_OK(lfm3_bf16::declare(&b, &meta, &ctx)); + + std::vector want = { "embed_lookup" }; + for (bool att : { false, true, true, false }) append(want, lfm3_layer_ops(att)); + for (const char* s : { "gather_rows", "rmsnorm", "logits_gemm" }) want.push_back(s); + CHECK_EQ(join(op_names(b)), join(want)); + + /* Three state groups: paged KV for the full layer, the same bounded by the window for the + * sliding one, and the conv window at the checkpoint's width. */ + REQUIRE(b.kv_groups.size() == 3); + CHECK_EQ(b.kv_groups[0].d.kind, (int)RAD_KV_FULL); + CHECK_EQ(b.kv_groups[1].d.kind, (int)RAD_KV_WINDOW); + CHECK_EQ(b.kv_groups[1].d.window, (int64_t)256); + CHECK_EQ(b.kv_groups[2].d.kind, (int)RAD_KV_CONV); + CHECK_EQ(b.kv_groups[2].d.conv_width, (int64_t)7); + REQUIRE(b.binds.size() == 4); + for (const auto& bd : b.binds) + CHECK_EQ(bd.second, bd.first == 1 ? (rad_kvgroup)1 : bd.first == 2 ? (rad_kvgroup)2 + : (rad_kvgroup)3); + + /* The full layer: half the head rotates, on the head ladder, at its theta, and attention has + * no window. q and k are rotated by one op each. */ + for (int i : { 0, 1 }) { + const RecOp* r = find_op(b, "rope_rows", i); + REQUIRE(r != nullptr); + CHECK_EQ(pint(r, "rotary_dim"), 8); + CHECK_EQ(pstr(r, "ladder"), "head"); + CHECK_EQ(pstr(r, "mode"), "neox"); + CHECK_NEAR(pflt(r, "theta"), 1e6, 1e-3); + } + CHECK_EQ(pint(find_op(b, "attn_paged", 0), "window"), 0); + /* The sliding layer: the whole head at its own theta (the ladders coincide, so none is + * named), and the window on the op as on the group. */ + for (int i : { 2, 3 }) { + const RecOp* r = find_op(b, "rope_rows", i); + REQUIRE(r != nullptr); + CHECK_EQ(pint(r, "rotary_dim"), 16); + CHECK(param(r, "ladder") == nullptr); + CHECK_NEAR(pflt(r, "theta"), 1e4, 1e-6); + } + CHECK_EQ(pint(find_op(b, "attn_paged", 1), "window"), 256); + for (int i : { 0, 1 }) { + CHECK_EQ(pint(find_op(b, "attn_paged", i), "gqa"), 4); + CHECK_EQ(pint(find_op(b, "attn_paged", i), "causal"), 1); + CHECK(param(find_op(b, "attn_paged", i), "scale") == nullptr); /* 1/sqrt(head_dim) */ + } + + /* Plain RMSNorms at the checkpoint's epsilon, four a layer and the final one. */ + CHECK_EQ(count_op(b, "rmsnorm"), 4 * 4 + 1); + const RecOp* norm = find_op(b, "rmsnorm"); + CHECK_NEAR(pflt(norm, "eps"), 1e-5, 1e-9); + CHECK_EQ(param(norm, "wadd") ? pflt(norm, "wadd") : 0.0, 0.0); + CHECK_EQ(pint(find_op(b, "causal_conv1d"), "width"), 7); + + /* The names: under `model`, the post norms beside the pre norms, o_proj and the head norms + * under their LFM3 names, a tied head read from the embedding. */ + for (const auto& [declared, src] : std::vector>{ + { "blk.0.attn_norm.weight", "model.layers.0.operator_norm.weight" }, + { "blk.0.attn_post_norm.weight", "model.layers.0.post_operator_norm.weight" }, + { "blk.0.ffn_post_norm.weight", "model.layers.0.post_ffn_norm.weight" }, + { "blk.0.conv_in.weight", "model.layers.0.conv.in_proj.weight" }, + { "blk.1.attn_output.weight", "model.layers.1.self_attn.o_proj.weight" }, + { "blk.1.attn_q_norm.weight", "model.layers.1.self_attn.q_norm.weight" }, + { "blk.2.attn_k_norm.weight", "model.layers.2.self_attn.k_norm.weight" } }) { + const RecMap* mp = find_map(b, declared); + REQUIRE(mp != nullptr); + CHECK_EQ(mp->src[0], src); + } + const RecMap* head = find_map(b, "output.weight"); + REQUIRE(head != nullptr && head->src.size() == 2); + CHECK_EQ(head->src[0], "lm_head.weight"); + CHECK_EQ(head->src[1], "model.embed_tokens.weight"); + + /* The gate at the attention output's width: gate_proj's row h, head_dim times, for each head + * in turn, so the weight is [n_head * head_dim, n_embd] and channel c reads row c / head_dim. */ + std::vector rows; + for (const auto& mp : b.maps) { + if (mp.declared != "blk.1.attn_gate.weight") continue; + CHECK_EQ(mp.mode, (int)RAD_MAP_ROWS); + for (size_t i = 0; i < mp.src.size(); ++i) { + CHECK_EQ(mp.src[i], "model.layers.1.self_attn.gate_proj.weight"); + rows.push_back(mp.src_index[i]); + } + } + REQUIRE_EQ(rows.size(), (size_t)64); + for (size_t c = 0; c < rows.size(); ++c) CHECK_EQ(rows[c], (int)(c / 16)); + const RecWeight* gate = find_w(b, "blk.1.attn_gate.weight"); + REQUIRE(gate != nullptr); +} + +TEST(lfm3_refuses_what_it_does_not_serve) { + auto declare_with = [](std::vector keys, std::vector vals, + int world = 1) { + RadBuilder b; + RadModelMeta meta = lfm3_meta(); + meta.n_kv = (int)keys.size(); meta.kv_key = keys.data(); meta.kv_val = vals.data(); + RadBuildCtx ctx = build_ctx(0, world); + return lfm3_bf16::declare(&b, &meta, &ctx); + }; + const std::vector k(std::begin(k_lfm3_keys), std::end(k_lfm3_keys)); + const std::vector v(std::begin(k_lfm3_vals), std::end(k_lfm3_vals)); + auto with = [&](const char* key, const char* val) { + std::vector kk = k, vv = v; + bool found = false; + for (size_t i = 0; i < kk.size(); ++i) + if (!std::strcmp(kk[i], key)) { vv[i] = val; found = true; } + if (!found) { kk.push_back(key); vv.push_back(val); } + return declare_with(kk, vv); + }; + auto without = [&](const char* key) { + std::vector kk, vv; + for (size_t i = 0; i < k.size(); ++i) + if (std::strcmp(k[i], key)) { kk.push_back(k[i]); vv.push_back(v[i]); } + return declare_with(kk, vv); + }; + CHECK_OK(declare_with(k, v)); + CHECK_EQ(declare_with(k, v, 2), RAD_E_UNSUPPORTED); /* two ranks */ + CHECK_EQ(without("layer_types"), RAD_E_FORMAT); + CHECK_EQ(with("layer_types", "conv full_attention conv"), RAD_E_FORMAT); /* 3 of 4 */ + CHECK_EQ(with("layer_types", "conv full_attention recurrent conv"), RAD_E_UNSUPPORTED); + CHECK_EQ(with("conv_bias", "true"), RAD_E_UNSUPPORTED); + CHECK_EQ(with("use_sandwich_norm", "false"), RAD_E_UNSUPPORTED); + CHECK_EQ(with("conv_L_cache", "9"), RAD_E_UNSUPPORTED); + /* A sliding layer needs its window and each attention class its own theta: the typed + * rope_theta is not a stand-in. */ + CHECK_EQ(without("sliding_window"), RAD_E_INVAL); + CHECK_EQ(without("rope_parameters.full_attention.rope_theta"), RAD_E_INVAL); + CHECK_EQ(without("rope_parameters.sliding_attention.rope_theta"), RAD_E_INVAL); + CHECK_EQ(with("rope_parameters.full_attention.rope_type", "yarn"), RAD_E_UNSUPPORTED); + CHECK_EQ(with("rope_parameters.full_attention.partial_rotary_factor", "0"), RAD_E_INVAL); + CHECK_EQ(with("rope_parameters.full_attention.partial_rotary_factor", "1.5"), RAD_E_INVAL); + /* A schedule without sliding layers needs neither the window nor their theta. */ + { + std::vector kk = { "layer_types", "conv_L_cache", + "rope_parameters.full_attention.rope_theta" }; + std::vector vv = { "conv full_attention conv conv", "7", "1000000.0" }; + CHECK_OK(declare_with(kk, vv)); + } +} + +TEST(lfm3_issues_a_mixed_step_through_both_attention_groups) { + RadBuilder b; + RadModelMeta meta = lfm3_meta(); + RadBuildCtx ctx = build_ctx(); + CHECK_OK(lfm3_bf16::declare(&b, &meta, &ctx)); + + /* Two decode rows (a verify of 3 and a plain decode) and one prefill chunk of 5. */ + static const int32_t tok[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + static const int32_t pos[9] = { 40, 41, 42, 30, 0, 1, 2, 3, 4 }; + static const int32_t cu[4] = { 0, 3, 4, 9 }; + static const int32_t ctxl[3] = { 40, 30, 0 }; + static const int32_t nacc[3] = { 2, 1, 1 }; + static const int32_t out[3] = { 2, 3, 8 }; + static int32_t cv_idx[3] = { 4, 5, 6 }; + const lfm3_bf16::Model& m = lfm3_bf16::g_model[0]; + RadKVGroupBatch kv[3]{}; + kv[0].group = m.full.kv; + kv[1].group = m.sliding.kv; + kv[2].group = m.kv_conv; kv[2].state_index = cv_idx; + RadBatch mixed{}; + mixed.phase = RAD_PHASE_MIXED; + mixed.n_tok = 9; mixed.n_seq = 3; mixed.max_q_len = 5; + mixed.n_seq_decode = 2; mixed.n_tok_decode = 4; + mixed.token_ids = tok; mixed.positions = pos; mixed.cu_seqlens = cu; mixed.ctx_lens = ctxl; + mixed.num_accepted = nacc; mixed.n_out = 3; mixed.out_ids = out; + mixed.n_kv_groups = 3; mixed.kv = kv; + + RadCtx c{ .b = &b, .batch = &mixed }; + lfm3_bf16::step(&c, &mixed); + CHECK_LINT(b, &c); + CHECK(c.step_fail.empty()); + + /* Each conv layer issues its convolution once per half of the step; each attention layer + * stores and attends through its own group, and gates the output before the projection. */ + int convs = 0, attns = 0, gates = 0; + for (const auto& i : c.issues) { + const RecOp* o = b.op_of(i.op); + if (!o) continue; + convs += o->op == "causal_conv1d"; + attns += o->op == "attn_paged"; + gates += o->op == "sigmoid"; + if (o->op == "attn_paged") CHECK_EQ(i.n, (int64_t)5); + } + CHECK_EQ(convs, 2 * 2); + CHECK_EQ(attns, 2); + CHECK_EQ(gates, 2); + + /* A draft pass or media rows are refused by name. */ + RadBatch draft = mixed; + draft.draft_pass = 1; + RadCtx c2{ .b = &b, .batch = &draft }; + lfm3_bf16::step(&c2, &draft); + CHECK(!c2.step_fail.empty()); +} + /* ==================================================================== LFM2-VL's tower */ namespace { diff --git a/tests/engine_e2e_test.cpp b/tests/engine_e2e_test.cpp index 42a56a9f..b1b79f81 100644 --- a/tests/engine_e2e_test.cpp +++ b/tests/engine_e2e_test.cpp @@ -22,6 +22,7 @@ #include "rad_test.h" #include "e2e_config.h" #include "tiny_golden.h" +#include "lfm3_golden.h" #include "tiny_model.h" #include "mm/media.h" @@ -193,7 +194,7 @@ std::string words(tiny::Rng& r, size_t n) { struct World { /* RAD_E2E_KEEP keeps the checkpoints for tests/mktinygolden.py, which reads them. */ tiny::TempDir dir{ "e2e", std::getenv("RAD_E2E_KEEP") != nullptr }; - std::string qwen35, lfm2, gemma4, llama, llama_rad; + std::string qwen35, lfm2, lfm3, gemma4, llama, llama_rad; /* llama.rad with its lm head int8 per (row, 128 of K). */ std::string llama_i8head; /* The llama as an int4 W4A16 release with zero points (tiny_model.h, write_llama_ct_int4), @@ -231,6 +232,7 @@ struct World { fprintf(stderr, " checkpoints kept in %s\n", dir.path.c_str()); qwen35 = dir / "qwen35"; lfm2 = dir / "lfm2"; + lfm3 = dir / "lfm3"; gemma4 = dir / "gemma4"; llama = dir / "llama"; llama_rad = dir / "llama.rad"; @@ -246,7 +248,7 @@ struct World { g31_df2 = dir / "g31_df2.rad"; g31w = dir / "g31w"; g31w_df2 = dir / "g31w_df2.rad"; - for (const std::string* d : { &qwen35, &lfm2, &gemma4, &llama, &qwen35_kv, &gemma4_kv, + for (const std::string* d : { &qwen35, &lfm2, &lfm3, &gemma4, &llama, &qwen35_kv, &gemma4_kv, &qwen35_spec, &qwen3asr, &llama_int4, &g31, &g31_draft, &g31w }) if (mkdir(d->c_str(), 0755) != 0) return; tiny::Qwen35 qkv; @@ -270,6 +272,7 @@ struct World { tiny::Gemma4 g31ws = g31s; g31ws.window = 1024; if (!tiny::write_qwen35(qwen35, {}) || !tiny::write_lfm2(lfm2, {}) || + !tiny::write_lfm3(lfm3, {}) || !tiny::write_gemma4(gemma4, {}) || !tiny::write_llama(llama, {}) || !tiny::write_qwen35(qwen35_kv, qkv) || !tiny::write_gemma4(gemma4_kv, gkv) || !tiny::write_qwen35(qwen35_spec, qspec) || !tiny::write_qwen3asr(qwen3asr, {}) || @@ -1204,7 +1207,9 @@ TEST(runtime_lora_refuses_targets_it_cannot_serve_at_startup) { /* Each family's forward pass against transformers' own, run in float32 over the same weights * (tests/tiny_golden.h, from tests/mktinygolden.py): the gated delta net with its query scale and * (1 + w) norms, the gated and partial-rotary attention, LFM2's convolutions, Gemma 4's sliding and - * K-equals-V global layers, soft cap and layer scalars. bf16 activations put this engine within + * K-equals-V global layers, soft cap and layer scalars; LFM3 against LiquidAI's own modelling code + * (tests/lfm3_golden.h, from tests/mklfm3golden.py), for its sandwich norms, head gates, partial + * rotary and sliding window. bf16 activations put this engine within * about 0.08 of transformers' log-probabilities; a wrong rotary width, window or epsilon moves * them by 0.5 or more and changes the top token at a tenth of the positions. So at every position * the top token must agree, unless transformers' own top two are within the bound, and the @@ -1214,16 +1219,22 @@ TEST(each_family_computes_what_transformers_computes) { REQUIRE(w.ok); tiny::TempDir d("e2e_gold"); REQUIRE(World::write_corpus(d / "corpus.jsonl", { kTinyGoldPrompt })); - for (const TinyGold& g : kTinyGold) { + std::vector golds; + for (const TinyGold& g : kTinyGold) golds.push_back(&g); + golds.push_back(&kLfm3Gold); + for (const TinyGold* gp : golds) { + const TinyGold& g = *gp; const std::string family = g.family; const std::string model = family == "qwen35" ? w.qwen35 : family == "lfm2" ? w.lfm2 + : family == "lfm3" ? w.lfm3 : w.gemma4; const uint64_t h = fnv1a(tiny::read_text(model + "/model.safetensors")); if (h != g.weights_fnv) fprintf(stderr, " %s: the checkpoint is not the one the golden was made from; " - "regenerate tests/tiny_golden.h with tests/mktinygolden.py\n", - g.family); + "regenerate tests/%s\n", + g.family, family == "lfm3" ? "lfm3_golden.h with tests/mklfm3golden.py" + : "tiny_golden.h with tests/mktinygolden.py"); REQUIRE_EQ(h, g.weights_fnv); const std::string out = d / family; Ran r = radiance(model, { "--kld-record", out, "--kld-corpus", d / "corpus.jsonl" }); @@ -1269,6 +1280,15 @@ TEST(lfm2_chunking_and_batching_change_no_logit) { { "--max-num-batched-tokens", "512", "--max-num-seqs", "4" }); } +/* LFM3 has both states at once: the convolution's rolling window, seven taps wide, and an 8-token + * attention window on its sliding layers, which every document crosses. */ +TEST(lfm3_chunking_and_batching_change_no_logit) { + check_recording_matches(world().lfm3, world().corpus_short, + { "--max-num-batched-tokens", "64", "--max-num-seqs", "1" }); + check_recording_matches(world().lfm3, world().corpus_short, + { "--max-num-batched-tokens", "512", "--max-num-seqs", "4" }); +} + /* Gemma 4's sliding layers attend over a 32-token window, which every document crosses, and its * global layers read V from K at a wider head. */ TEST(gemma4_chunking_and_batching_change_no_logit) { @@ -1386,6 +1406,7 @@ TEST(libavx_alone_serves_every_family_within_rounding_of_libref) { const struct { const char* name; const std::string* model; const std::string* corpus; } kRuns[] = { { "qwen35 on libavx", &w.qwen35, &w.corpus }, { "lfm2 on libavx", &w.lfm2, &w.corpus_short }, + { "lfm3 on libavx", &w.lfm3, &w.corpus_short }, { "gemma4 on libavx", &w.gemma4, &w.corpus_short }, { "llama_fp8 on libavx", &w.llama_rad, &w.corpus_tiny }, { "qwen3asr's decoder on libavx", &w.qwen3asr, &w.corpus_short } }; @@ -1737,7 +1758,8 @@ struct Family { const char* name; std::string World::*model; }; -const Family kSpecFamilies[] = { { "lfm2", &World::lfm2 }, { "gemma4", &World::gemma4 }, +const Family kSpecFamilies[] = { { "lfm2", &World::lfm2 }, { "lfm3", &World::lfm3 }, + { "gemma4", &World::gemma4 }, { "qwen35", &World::qwen35_spec } }; const std::vector& repetitive_prompt() { @@ -1835,7 +1857,7 @@ TEST(concurrent_requests_return_what_each_returns_alone) { for (uint64_t i = 0; i < 4; ++i) prompts.push_back(prompt_ids(100 + i, 60 + 37 * i)); const struct { const char* name; std::string World::*model; const char* n_spec; } kRuns[] = { { "qwen35", &World::qwen35, "4" }, { "lfm2", &World::lfm2, "4" }, - { "gemma4", &World::gemma4, "4" } }; + { "lfm3", &World::lfm3, "4" }, { "gemma4", &World::gemma4, "4" } }; for (const auto& run : kRuns) { std::vector> alone; { @@ -2248,7 +2270,8 @@ TEST(a_requests_last_step_issues_no_step_ahead) { TEST(a_decoded_token_has_the_probabilities_prefilling_it_gives) { REQUIRE(world().ok); const struct { const char* name; std::string World::*model; double tol; } kRuns[] = { - { "lfm2", &World::lfm2, 0.0 }, { "gemma4", &World::gemma4, 0.0 }, + { "lfm2", &World::lfm2, 0.0 }, { "lfm3", &World::lfm3, 0.0 }, + { "gemma4", &World::gemma4, 0.0 }, { "llama_fp8", &World::llama_rad, 0.0 }, { "qwen35", &World::qwen35, 0.1 } }; const std::vector p = prompt_ids(31, 120); for (const auto& run : kRuns) { diff --git a/tests/lfm3_golden.h b/tests/lfm3_golden.h new file mode 100644 index 00000000..cced35d1 --- /dev/null +++ b/tests/lfm3_golden.h @@ -0,0 +1,11 @@ +/* lfm3_golden.h -- GENERATED by mklfm3golden.py from LiquidAI's modeling_lfm3.py (sha256 + * e8cd5254ea99ce6bd731b57b10908c2e8b5010ce8315341f895a38484885eeab) under transformers 5.19.0; + * do not edit. */ +#pragma once +#include "tiny_golden.h" + +static_assert(kTinyGoldPositions == 130 && kTinyGoldTop == 4, "lfm3_golden.h was made for another prompt"); +static const TinyGold kLfm3Gold = { + "lfm3", 0x7113fe64531a84e0ull, + { { 145, 49, 91, 297 }, { 185, 81, 84, 105 }, { 258, 177, 235, 37 }, { 129, 98, 282, 214 }, { 318, 76, 310, 259 }, { 57, 90, 235, 128 }, { 21, 312, 46, 318 }, { 108, 223, 318, 54 }, { 2, 160, 4, 139 }, { 290, 147, 219, 191 }, { 253, 10, 236, 108 }, { 48, 191, 162, 20 }, { 261, 155, 76, 110 }, { 311, 21, 104, 129 }, { 75, 76, 188, 85 }, { 258, 177, 147, 76 }, { 31, 82, 76, 291 }, { 4, 311, 234, 159 }, { 191, 261, 273, 51 }, { 277, 177, 297, 235 }, { 13, 297, 206, 82 }, { 285, 154, 281, 123 }, { 9, 76, 202, 128 }, { 82, 68, 141, 201 }, { 304, 281, 285, 167 }, { 162, 124, 76, 282 }, { 263, 145, 289, 102 }, { 104, 142, 58, 82 }, { 9, 44, 290, 236 }, { 117, 9, 185, 108 }, { 162, 177, 96, 135 }, { 104, 24, 151, 308 }, { 217, 309, 310, 85 }, { 30, 193, 226, 24 }, { 108, 45, 178, 24 }, { 76, 139, 177, 30 }, { 311, 104, 217, 142 }, { 76, 311, 82, 42 }, { 137, 30, 90, 128 }, { 129, 76, 147, 234 }, { 318, 177, 235, 208 }, { 311, 104, 188, 217 }, { 108, 264, 190, 24 }, { 168, 188, 76, 235 }, { 76, 132, 42, 304 }, { 159, 47, 123, 160 }, { 76, 20, 178, 169 }, { 204, 84, 97, 12 }, { 254, 258, 222, 178 }, { 191, 129, 92, 20 }, { 92, 314, 109, 242 }, { 30, 76, 193, 305 }, { 230, 190, 275, 27 }, { 45, 54, 108, 268 }, { 147, 311, 104, 92 }, { 95, 167, 194, 193 }, { 92, 90, 48, 311 }, { 274, 135, 75, 277 }, { 168, 4, 142, 147 }, { 290, 219, 253, 18 }, { 318, 145, 9, 98 }, { 251, 139, 30, 168 }, { 24, 90, 267, 129 }, { 191, 23, 4, 229 }, { 82, 226, 139, 132 }, { 4, 176, 191, 123 }, { 76, 30, 102, 226 }, { 242, 12, 84, 97 }, { 258, 76, 9, 95 }, { 104, 92, 282, 253 }, { 92, 14, 194, 109 }, { 193, 311, 253, 19 }, { 230, 27, 190, 58 }, { 76, 100, 310, 139 }, { 254, 110, 258, 141 }, { 311, 104, 188, 82 }, { 287, 141, 54, 108 }, { 9, 317, 34, 154 }, { 104, 21, 282, 290 }, { 272, 236, 291, 183 }, { 216, 109, 293, 182 }, { 178, 76, 20, 258 }, { 282, 104, 24, 192 }, { 139, 290, 224, 9 }, { 24, 173, 257, 124 }, { 30, 291, 182, 289 }, { 139, 177, 76, 178 }, { 145, 9, 41, 95 }, { 4, 304, 191, 111 }, { 4, 147, 95, 234 }, { 291, 159, 252, 24 }, { 105, 186, 182, 32 }, { 37, 258, 178, 76 }, { 139, 4, 35, 76 }, { 217, 305, 92, 4 }, { 290, 236, 30, 76 }, { 318, 84, 108, 182 }, { 178, 258, 9, 37 }, { 129, 255, 311, 282 }, { 76, 137, 173, 236 }, { 45, 183, 24, 311 }, { 162, 283, 141, 28 }, { 84, 27, 186, 137 }, { 290, 183, 311, 120 }, { 129, 147, 234, 305 }, { 82, 290, 18, 54 }, { 315, 90, 37, 20 }, { 30, 130, 251, 232 }, { 51, 30, 311, 277 }, { 4, 129, 159, 234 }, { 132, 173, 89, 285 }, { 129, 112, 177, 272 }, { 290, 139, 145, 236 }, { 75, 217, 24, 133 }, { 4, 147, 234, 209 }, { 177, 309, 82, 30 }, { 86, 30, 118, 130 }, { 76, 281, 188, 30 }, { 217, 73, 123, 52 }, { 76, 178, 210, 291 }, { 84, 174, 40, 12 }, { 254, 258, 178, 20 }, { 191, 253, 20, 21 }, { 234, 305, 291, 264 }, { 258, 139, 76, 178 }, { 76, 30, 188, 9 }, { 21, 171, 129, 315 }, { 139, 145, 176, 289 }, { 177, 268, 234, 151 }, { 129, 214, 76, 24 } }, + { { -2.98817f, -3.03352f, -3.62797f, -3.67657f }, { -2.75256f, -3.24892f, -3.40979f, -3.64145f }, { -2.54806f, -3.34782f, -3.36844f, -3.38181f }, { -3.38324f, -3.40307f, -3.70412f, -3.82979f }, { -2.86077f, -3.00919f, -3.12698f, -3.48121f }, { -2.69743f, -3.2496f, -3.48024f, -3.73044f }, { -3.01339f, -3.42614f, -3.69295f, -3.80717f }, { -2.68052f, -2.87851f, -3.04736f, -3.43097f }, { -3.34975f, -3.61946f, -3.67596f, -3.77074f }, { -3.47251f, -3.50023f, -3.69769f, -3.76665f }, { -2.68497f, -2.94795f, -2.95461f, -3.4484f }, { -2.59342f, -2.5996f, -3.21076f, -3.5642f }, { -3.25056f, -3.27442f, -3.36633f, -3.40139f }, { -3.38366f, -3.54624f, -3.55954f, -3.64867f }, { -3.0729f, -3.08015f, -3.22645f, -3.43199f }, { -2.57679f, -3.28433f, -3.29774f, -3.39713f }, { -3.05558f, -3.25115f, -3.32656f, -3.68142f }, { -3.39238f, -3.64295f, -3.71494f, -3.71865f }, { -3.03832f, -3.18086f, -3.47281f, -3.6483f }, { -2.96834f, -3.19179f, -3.50139f, -3.55101f }, { -3.21461f, -3.23587f, -3.68982f, -3.75831f }, { -2.93351f, -3.31798f, -3.62313f, -3.8811f }, { -2.85722f, -3.09732f, -3.46968f, -3.61992f }, { -2.86507f, -2.87945f, -2.93571f, -3.31197f }, { -3.1465f, -3.17213f, -3.24937f, -3.39482f }, { -2.93696f, -3.30345f, -3.53538f, -3.63915f }, { -3.68243f, -3.77965f, -3.80771f, -3.83978f }, { -2.4387f, -3.12498f, -3.54755f, -3.63841f }, { -2.42097f, -2.7566f, -2.77697f, -3.53281f }, { -2.07806f, -2.84058f, -3.11873f, -3.29671f }, { -3.2196f, -3.4465f, -3.61645f, -3.81003f }, { -2.33541f, -2.87655f, -3.10338f, -3.56947f }, { -3.08123f, -3.23259f, -3.44803f, -3.49418f }, { -2.97311f, -3.07175f, -3.19934f, -3.43903f }, { -2.81218f, -3.06361f, -3.28119f, -3.40067f }, { -2.65529f, -2.74721f, -3.08692f, -3.37798f }, { -2.80632f, -2.90143f, -3.58749f, -3.61202f }, { -3.09031f, -3.36565f, -3.37148f, -3.6407f }, { -2.4681f, -2.66549f, -3.27267f, -3.46856f }, { -2.83878f, -3.08415f, -3.35113f, -3.38258f }, { -3.16516f, -3.17588f, -3.33834f, -3.35932f }, { -2.79581f, -2.82343f, -3.09567f, -3.7576f }, { -3.11805f, -3.21293f, -3.44694f, -3.77395f }, { -3.06655f, -3.27577f, -3.45764f, -3.49091f }, { -3.29646f, -3.51747f, -3.6609f, -3.66376f }, { -2.55005f, -2.91533f, -3.14713f, -3.79063f }, { -2.8974f, -2.98985f, -3.08671f, -3.53977f }, { -3.20222f, -3.29478f, -3.5184f, -3.53399f }, { -3.25485f, -3.34718f, -3.3772f, -3.48506f }, { -3.14096f, -3.16827f, -3.23319f, -3.33683f }, { -2.2682f, -2.82129f, -3.3308f, -3.78214f }, { -2.95007f, -2.98886f, -3.01542f, -3.52623f }, { -3.08567f, -3.31516f, -3.35947f, -3.46903f }, { -2.08542f, -2.54016f, -2.88981f, -3.24298f }, { -3.38579f, -3.52315f, -3.56608f, -3.72473f }, { -3.07533f, -3.75051f, -3.81663f, -3.81872f }, { -2.91524f, -2.96535f, -3.4311f, -3.66922f }, { -1.79961f, -3.44798f, -3.55039f, -3.61708f }, { -3.52474f, -3.5282f, -3.61163f, -3.66879f }, { -2.64471f, -3.26007f, -3.4517f, -3.51779f }, { -3.36997f, -3.42892f, -3.51201f, -3.62165f }, { -2.61094f, -2.75435f, -2.77842f, -3.13219f }, { -2.90599f, -3.03646f, -3.26554f, -3.52561f }, { -2.44961f, -3.25237f, -3.29373f, -3.63732f }, { -2.59546f, -3.18057f, -3.26597f, -3.49452f }, { -2.67228f, -2.74601f, -3.51893f, -3.69199f }, { -1.8724f, -3.0418f, -3.47451f, -3.56163f }, { -2.89954f, -3.21322f, -3.24527f, -3.35185f }, { -2.86991f, -3.22503f, -3.4125f, -3.43115f }, { -3.21456f, -3.3355f, -3.37872f, -3.48221f }, { -2.87787f, -3.60785f, -3.61514f, -3.71297f }, { -2.92916f, -3.05712f, -3.70969f, -3.78754f }, { -2.92854f, -2.97641f, -3.29878f, -3.81979f }, { -2.15913f, -2.78871f, -3.29692f, -3.39128f }, { -2.23098f, -2.52687f, -2.90817f, -3.51316f }, { -1.76636f, -3.34171f, -3.5421f, -3.88353f }, { -2.14676f, -2.59534f, -2.7658f, -3.36535f }, { -2.56437f, -3.49714f, -4.111f, -4.14245f }, { -2.7581f, -3.33646f, -3.45454f, -3.78166f }, { -2.78005f, -2.87024f, -3.31907f, -3.57987f }, { -3.08461f, -3.27059f, -3.49966f, -3.61359f }, { -2.43154f, -2.97309f, -3.14252f, -3.86298f }, { -3.36001f, -3.42982f, -3.68671f, -3.69937f }, { -2.74569f, -2.75238f, -3.60143f, -3.72109f }, { -3.0447f, -3.16222f, -3.21272f, -3.52635f }, { -2.44938f, -2.52214f, -3.42678f, -3.67548f }, { -3.58031f, -3.62428f, -3.69892f, -3.70914f }, { -3.35625f, -3.40272f, -3.59072f, -3.67175f }, { -2.22529f, -3.21554f, -3.23958f, -3.40542f }, { -2.91392f, -3.26685f, -3.50198f, -3.50583f }, { -2.26024f, -2.56781f, -3.26943f, -3.4575f }, { -2.54826f, -3.39482f, -3.50184f, -3.54028f }, { -2.78551f, -2.93478f, -3.37596f, -3.3985f }, { -2.62991f, -2.82764f, -3.06437f, -3.21632f }, { -2.55374f, -3.04937f, -3.26839f, -3.30087f }, { -2.75576f, -2.81508f, -3.01244f, -3.49987f }, { -2.91304f, -3.04429f, -3.32989f, -3.56549f }, { -2.64294f, -3.18141f, -3.30781f, -3.31579f }, { -3.42033f, -3.46397f, -3.70477f, -3.75033f }, { -2.5952f, -2.77707f, -3.44736f, -3.62995f }, { -3.26617f, -3.48374f, -3.73873f, -3.76906f }, { -2.70772f, -3.05709f, -3.15899f, -3.37786f }, { -3.02327f, -3.23379f, -3.31161f, -3.43362f }, { -3.35371f, -3.40861f, -3.50216f, -3.63273f }, { -3.16782f, -3.60317f, -3.78695f, -3.88504f }, { -2.80047f, -2.85399f, -3.01668f, -3.49934f }, { -3.0473f, -3.2207f, -3.2231f, -3.38915f }, { -2.28023f, -2.95149f, -3.28205f, -3.61107f }, { -3.20435f, -3.54184f, -3.63213f, -3.66349f }, { -3.07869f, -3.34832f, -3.46036f, -3.49455f }, { -2.82819f, -3.12799f, -3.59412f, -3.61222f }, { -3.4679f, -3.61168f, -3.6434f, -3.73977f }, { -1.92931f, -2.91603f, -3.36978f, -3.46185f }, { -1.84544f, -2.90955f, -2.96037f, -3.32282f }, { -2.26342f, -3.05843f, -3.49335f, -3.65416f }, { -3.30213f, -3.46801f, -3.5766f, -3.81802f }, { -3.23004f, -3.3321f, -3.53715f, -3.54795f }, { -3.29145f, -3.30194f, -3.40772f, -3.91265f }, { -3.07903f, -3.62135f, -3.63868f, -3.67339f }, { -2.0276f, -3.30017f, -3.44148f, -3.52596f }, { -2.53874f, -3.62241f, -3.69596f, -3.75478f }, { -3.09376f, -3.48832f, -3.56977f, -3.79638f }, { -3.28494f, -3.28843f, -3.31355f, -3.37836f }, { -3.37997f, -3.5947f, -3.62165f, -3.73509f }, { -2.26115f, -2.9741f, -3.11786f, -3.30654f }, { -1.68903f, -2.01383f, -3.30065f, -3.54365f }, { -3.28156f, -3.40484f, -3.86556f, -3.8965f }, { -1.57758f, -3.01879f, -3.62646f, -3.90701f }, { -3.30083f, -3.74443f, -3.87245f, -3.8887f }, { -2.91798f, -3.1927f, -3.30021f, -3.4292f } } }; diff --git a/tests/mklfm3golden.py b/tests/mklfm3golden.py new file mode 100644 index 00000000..fb43522f --- /dev/null +++ b/tests/mklfm3golden.py @@ -0,0 +1,73 @@ +"""mklfm3golden.py CKPTDIR CODEDIR OUT.h -- the LFM3 golden for tests/engine_e2e_test.cpp. + +transformers has no LFM3 model class; the reference is the modelling code LiquidAI ships with the +checkpoint (configuration_lfm3.py, modeling_lfm3.py, lfm3_conv.py; Apache-2.0). CODEDIR is a +directory holding those files, for example a download of LiquidAI/LFM3-1.7B: its classes are +built over the tiny random checkpoint tests/tiny_model.h writes and run in float32, as +mktinygolden.py does for the other families. Nothing of CODEDIR's weights is read. + +CKPTDIR holds the tiny checkpoints: run `RAD_E2E_KEEP=1 build/tests/engine_e2e_test` and pass the +directory it prints. The header records the FNV-1a hash of lfm3/model.safetensors (the test +refuses a golden made from other weights), the sha256 of the modelling file, and at every +position of mktinygolden.py's prompt the four most likely next tokens with their +log-probabilities.""" +import hashlib, json, os, re, sys +import torch +from safetensors.torch import load_file +import transformers +from transformers.dynamic_module_utils import get_class_from_dynamic_module + +ckpt_dir, code_dir, out = sys.argv[1], sys.argv[2], sys.argv[3] +torch.manual_seed(0) + +here = os.path.dirname(os.path.abspath(__file__)) +PROMPT = json.loads(re.search(r"kTinyGoldPrompt\[\] = (\".*\");", + open(os.path.join(here, "tiny_golden.h")).read()).group(1)) +ids = list(PROMPT.encode()) +TOP = 4 + + +def fnv(path): + h = 0xcbf29ce484222325 + for b in open(path, "rb").read(): + h = ((h ^ b) * 0x100000001b3) & (2**64 - 1) + return h + + +d = os.path.join(ckpt_dir, "lfm3") +cfg = json.load(open(os.path.join(d, "config.json"))) +for k in ("architectures", "model_type"): + cfg.pop(k, None) +# The release's pad and bos ids lie outside the tiny vocabulary. +cfg["pad_token_id"] = None +cfg["bos_token_id"] = None +Config = get_class_from_dynamic_module("configuration_lfm3.Lfm3Config", code_dir) +Model = get_class_from_dynamic_module("modeling_lfm3.Lfm3ForCausalLM", code_dir) +model = Model(Config(**cfg)) +sd = {k: v.float() for k, v in load_file(os.path.join(d, "model.safetensors")).items()} +missing, unexpected = model.load_state_dict(sd, strict=False) +# A tied head is the embedding; anything else missing is a naming error. +missing = [k for k in missing if k != "lm_head.weight"] +if missing or unexpected: + raise SystemExit(f"lfm3: missing {missing}, unexpected {unexpected}") +model = model.float().eval() +with torch.no_grad(): + lp = torch.log_softmax(model(torch.tensor([ids])).logits[0].float(), dim=-1) +v, i = torch.topk(lp, TOP, dim=-1) +code_sha = hashlib.sha256(open(os.path.join(code_dir, "modeling_lfm3.py"), "rb").read()).hexdigest() + + +def rows(t, fmt): + return ", ".join("{ " + ", ".join(fmt(x) for x in row) + " }" for row in t) + + +L = ["/* lfm3_golden.h -- GENERATED by mklfm3golden.py from LiquidAI's modeling_lfm3.py (sha256", + f" * {code_sha}) under transformers {transformers.__version__};", + " * do not edit. */", "#pragma once", '#include "tiny_golden.h"', "", + f"static_assert(kTinyGoldPositions == {len(ids)} && kTinyGoldTop == {TOP}, " + '"lfm3_golden.h was made for another prompt");', + "static const TinyGold kLfm3Gold = {", + f" \"lfm3\", 0x{fnv(os.path.join(d, 'model.safetensors')):016x}ull,", + " { " + rows(i, lambda x: str(int(x))) + " },", + " { " + rows(v, lambda x: f"{float(x):.6g}f") + " } };"] +open(out, "w").write("\n".join(L) + "\n") diff --git a/tests/shipped_plugins_test.cpp b/tests/shipped_plugins_test.cpp index 8e01fecc..4edbcf5a 100644 --- a/tests/shipped_plugins_test.cpp +++ b/tests/shipped_plugins_test.cpp @@ -99,7 +99,7 @@ TEST(the_shipped_architectures_load_with_distinct_selection_keys) { { "gemma3", "" }, { "gemma4", "" }, { "gemma4", "compressed-tensors" }, { "glm5next", "" }, { "hunyuan3d2", "" }, { "hyv3", "" }, { "hyv3", "fp8" }, { "hyv3", "gguf_ftype_15" }, - { "laya", "" }, { "lfm2", "" }, { "llama", "" }, { "mistral3", "" }, + { "laya", "" }, { "lfm2", "" }, { "lfm3", "" }, { "llama", "" }, { "mistral3", "" }, { "llama", "fp8_e4m3" }, { "nemotronh", "" }, { "nomicbert", "" }, { "qwen35", "" }, { "museglimmer", "" }, { "museglimmer", "compressed-tensors" }, diff --git a/tests/tiny_model.h b/tests/tiny_model.h index e625e3e7..86fff0c3 100644 --- a/tests/tiny_model.h +++ b/tests/tiny_model.h @@ -398,6 +398,89 @@ inline bool write_lfm2(const std::string& dir, const Lfm2& s) { return write_checkpoint(dir, c, ts); } +/* ------------------------------------------------------------------ lfm3: conv + full + sliding */ + +/* LFM3 (`lfm3`, served by lfm3_bf16): LFM2's convolutions and attention with sandwich norms, a + * gate per query head, a partial rotary on the full layers and sliding-window layers at their own + * theta. Layers go conv, full, conv, sliding; the window is shorter than every test document and + * the convolution as wide as the release's. */ +struct Lfm3 { + int64_t layers = 4, hidden = 64, heads = 4, kv_heads = 2, ffn = 128, vocab = 320, ctx = 4096; + int64_t conv = 7; /* conv_L_cache */ + int64_t window = 8; /* sliding_window */ + float branch = 0.2f, head = 0.5f; /* see Gains; head is the tied table's amplitude */ + /* With this seed the greedy continuation of the speculation test's prompt repeats itself in + * part, so prompt lookup drafts and the model rejects some of what it drafts. */ + uint64_t seed = 59; +}; + +inline bool write_lfm3(const std::string& dir, const Lfm3& s) { + Rng r(s.seed); + const int64_t H = s.hidden, hd = s.hidden / s.heads, kv = s.kv_heads * hd; + auto kind = [](int64_t l) { return l % 2 == 0 ? 0 : l % 4 == 1 ? 1 : 2; }; + Json types = Json::array(); + for (int64_t l = 0; l < s.layers; ++l) + types.push_back(kind(l) == 0 ? "conv" : kind(l) == 1 ? "full_attention" : "sliding_attention"); + Json c; + c["architectures"] = { "Lfm3ForCausalLM" }; + c["model_type"] = "lfm3"; + c["vocab_size"] = s.vocab; + c["hidden_size"] = H; + c["intermediate_size"] = s.ffn; + c["num_hidden_layers"] = s.layers; + c["num_attention_heads"] = s.heads; + c["num_key_value_heads"] = s.kv_heads; + c["head_dim"] = hd; + c["max_position_embeddings"] = s.ctx; + c["norm_eps"] = 1e-5; + c["rope_parameters"] = { + /* A low theta: over a test document the release's 1e6 turns the unrotated half's would-be + * frequencies by a hundredth of a radian, and a full rotary would pass for the partial one. */ + { "full_attention", { { "rope_type", "default" }, { "rope_theta", 100.0 }, + { "partial_rotary_factor", 0.5 } } }, + { "sliding_attention", { { "rope_type", "default" }, { "rope_theta", 10000.0 } } } }; + c["sliding_window"] = s.window; + c["use_sandwich_norm"] = true; + c["conv_bias"] = false; + c["conv_L_cache"] = s.conv; + c["layer_types"] = types; + c["tie_word_embeddings"] = true; + c["eos_token_id"] = (int)kEndOfText; + + const std::string m = "model."; + std::vector ts; + ts.push_back(noise(m + "embed_tokens.weight", { s.vocab, H }, r, s.head)); + for (int64_t l = 0; l < s.layers; ++l) { + const std::string p = m + "layers." + std::to_string(l) + "."; + ts.push_back(noise(p + "operator_norm.weight", { H }, r, 0.1f, 1.0f)); + if (kind(l) != 0) { + ts.push_back(linear(p + "self_attn.q_proj.weight", H, H, r)); + ts.push_back(linear(p + "self_attn.k_proj.weight", kv, H, r)); + ts.push_back(linear(p + "self_attn.v_proj.weight", kv, H, r)); + ts.push_back(linear(p + "self_attn.o_proj.weight", H, H, r)); + ts.push_back(noise(p + "self_attn.q_norm.weight", { hd }, r, 0.1f, 1.0f)); + ts.push_back(noise(p + "self_attn.k_norm.weight", { hd }, r, 0.1f, 1.0f)); + /* gates spread over (0, 1), a head apart from its neighbours */ + ts.push_back(linear(p + "self_attn.gate_proj.weight", s.heads, H, r, 2.0f)); + } else { + ts.push_back(linear(p + "conv.in_proj.weight", 3 * H, H, r)); + ts.push_back(noise(p + "conv.conv.weight", { H, 1, s.conv }, r, 0.5f)); + ts.push_back(linear(p + "conv.out_proj.weight", H, H, r)); + } + /* The post norms' gains set each branch's size in the stream, as the out projections' + * do in a pre-norm model. */ + ts.push_back(noise(p + "post_operator_norm.weight", { H }, r, 0.02f, s.branch)); + ts.push_back(noise(p + "ffn_norm.weight", { H }, r, 0.1f, 1.0f)); + ts.push_back(linear(p + "feed_forward.w1.weight", s.ffn, H, r)); + ts.push_back(linear(p + "feed_forward.w3.weight", s.ffn, H, r)); + ts.push_back(linear(p + "feed_forward.w2.weight", H, s.ffn, r)); + ts.push_back(noise(p + "post_ffn_norm.weight", { H }, r, 0.02f, s.branch)); + } + /* Signed gains around 0, as LFM2's: a tied head would otherwise score a token's own row. */ + ts.push_back(noise(m + "embedding_norm.weight", { H }, r, 1.0f)); + return write_checkpoint(dir, c, ts); +} + /* ------------------------------------------------------------------ gemma4: sliding + global */ /* Gemma 4 dense (`gemma4`, served by the gemma4 plugin as written), shaped like the 31B: sliding