Archived
tests, docs: lfm3 in the family tests, with its golden and declare cases
A tiny LFM3 (conv, full attention, conv, sliding attention; window 8, conv width 7) joins the engine tests every family runs: the golden against the reference implementation, chunking and batching, libavx against libref, decode against prefill, concurrent requests and speculation by prompt lookup. Its golden is made by mklfm3golden.py from the modelling code LiquidAI ships with the checkpoint, since transformers has no LFM3 class. arch_test covers what the plugin declares for each layer kind, what it refuses, and a step through both attention groups.
This commit is contained in:
1 parent
6c7bcefefa
commit
6997f8da14
9 files changed
+448
-11
No files matched your search
@@ -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<Tensor> 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
|
||||
|
||||
Reference in new issue
Block a user