model : add support for HrmTextForCausalLM (DFM Mimir 1B) (#27625)

* model : add support for HrmTextForCausalLM (DFM Mimir 1B)

HRM-Text runs two transformer stacks (low, high) in an alternating cycle over the same token stream. The low-cycle state z_l starts from a learned [n_embd] tensor and is broadcast over positions.

- conversion: new writer for the fused gqkv projection (order gate,q,k,v) remapped to llama.cpp q/k/v plus a separate sigmoid gate tensor
- loader: block_count = lps * h_cycles * (l_cycles + 1) cache slots aliasing 2*lps physical blocks via struct copies
- graph: looped build with sigmoid-gated attention, SwiGLU FFN and parameterless RMS norms; learned embedding_scale applied in build_inp_embd
- saver: pointer-deduplicated layer loop (looped archs alias tensors)
- tests: hrm_text fixture (lps 1, h 2, l 3) in test-llama-archs

Limitations:
causal attention only - the upstream prefix-LM mode is not implemented (the prefix_lm GGUF key round-trips unused).
The KV cache holds one entry per pass: 128 layers for Mimir 1B, i.e. 4x a same-width 32-layer model - about 3072 MiB at ctx 4096 in F16 (halves with q8_0 KV + FA).
Every token runs all 128 block passes, so decode cost is roughly 4x a dense model of equal width (2.65 t/s BF16, 8-thread desktop CPU).

Verified against the HF reference: identical argmax at 334/334 positions across 20 prompts (BF16 GGUF vs FP32 golden).
q8_0 requant: 95.8% top-1, all remaining misses inside the HF top-5 (accumulated error over 128 sequential blocks).

AI usage disclosure: YES
Used GLM-5.3 for the majority of code AI-generated under my direction, all gates verified locally.
All in all I could say that I have written less than 20% of the code and most of the heavy lifting has been done by the model. As such, this should be considered experimental.

* Update conversion/hrm_text.py

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@huggingface.co>

* Update src/llama-arch.cpp

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@huggingface.co>

* convert : add gguf_writer methods for hrm_text metadata

replace raw add_uint32/add_bool calls with dedicated GGUFWriter methods, following the add_embedding_scale pattern

Assisted-by: GLM-5.3

* convert : map regular hrm_text tensors via tensor_mapping

delegate unfused checkpoints to the base tensor mapping; training-style attn. names are renamed to self_attn. so the patterns match

Assisted-by: GLM-5.3

* model : format hrm-text build_* calls as in other models

one argument group per line, matching sibling model files

Assisted-by: GLM-5.3

* llama : move hrm z_l_init table entries out of the nemotron group

place the name and tensor-info entries with the other global input tensors

Assisted-by: GLM-5.3

* convert : slim down hrm_text comments

Assisted-by: GLM-5.3

* convert : build hrm_text block tensor names from the {bid} template

The tensor map holds concrete per-block names, so format the template
with the computed layer index before handing it to super().

* llama : name hrm metadata keys in their own hrm. namespace

The four keys are arch-independent, unlike the arch-substituted
Keys.LLM entries, so group them under Keys.HRM (like Keys.Split) and
rename the llm_kv entries to LLM_KV_HRM_*. Only our own GGUFs carry
the old hrm_text.* keys; they are regenerated.

* Update src/llama-model-saver.cpp

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@huggingface.co>

* llama : keep hrm metadata keys arch-substituted

Per review: the GGUF keys stay "{arch}.h_cycles" style, so the Python
members drop the LLM_KV_HRM_ prefix and keep arch templates; C++ keeps
the LLM_KV_HRM_* enums. GGUF output is unchanged - existing files and
HF uploads stay valid.

* Update gguf-py/gguf/constants.py

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@huggingface.co>

* Update src/llama-arch.cpp

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@huggingface.co>

* Update src/llama-arch.cpp

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@huggingface.co>

* convert : rename hrm writer methods to add_hrm_*

Generic names like add_h_cycles/add_prefix_lm are too broad on the
shared GGUFWriter; prefix them with hrm_ like the metadata keys.

* model : fix meta-split lookup for archs with aliased cache slots

Cache tensors of archs that alias physical blocks across looped slots
(hrm_text, nanbeige with num_loops > 1) can reference block indices
without weight tensor names. Take the output projection from the layer
array instead of asserting; all other lookups are unchanged.

* model : replicate hrm_text tensors on meta devices instead of splitting

The aliased cache slots rotate split states differently from their
physical weights, so the meta-split execution invariants (set_rows
requires the cache state to match the token indices) cannot hold for
any device count. Replicate all hrm_text tensors on every meta device
instead; single-device and non-meta paths are unchanged.

Assisted-by: Claude Sonnet

---------

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@huggingface.co>
This commit is contained in:
GeorgeandSigbjørn Skjæret authored and GitHub committed 2026-09-16 15:18:45 +02:00
1 parent 83078fec0d
commit 7d6f5d02bb
16 files changed
+412 -1

No files matched your search

+213
View File
@@ -0,0 +1,213 @@
#include "models.h"
// HRM-Text: alternating low/high transformer stacks over the same token stream.
// Reference: HrmTextModel in transformers, DFM Mimir 1B.
void llama_model_hrm_text::load_arch_hparams(llama_model_loader & ml) {
ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale, false);
ml.get_key(LLM_KV_HRM_LAYERS_PER_STACK, hparams.n_hrm_layers_per_stack);
ml.get_key(LLM_KV_HRM_H_CYCLES, hparams.n_hrm_h_cycles);
ml.get_key(LLM_KV_HRM_L_CYCLES, hparams.n_hrm_l_cycles);
// prefix-LM prefill is not implemented (causal attention only); kept for round-trip
ml.get_key(LLM_KV_HRM_PREFIX_LM, hparams.hrm_prefix_lm, false);
GGML_ASSERT(hparams.n_hrm_layers_per_stack > 0);
GGML_ASSERT(hparams.n_hrm_h_cycles > 0);
GGML_ASSERT(hparams.n_hrm_l_cycles > 0);
// the GGUF block count is the expanded cache-slot count
const uint32_t n_slot = hparams.n_hrm_layers_per_stack * hparams.n_hrm_h_cycles * (hparams.n_hrm_l_cycles + 1);
GGML_ASSERT(hparams.n_layer() == n_slot);
switch (hparams.n_embd) {
case 1536:
type = LLM_TYPE_1B;
break;
default:
type = LLM_TYPE_UNKNOWN;
}
}
void llama_model_hrm_text::load_arch_tensors(llama_model_loader &) {
LLAMA_LOAD_LOCALS;
tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
// output
output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED);
// if output is NULL, init from the input tok embed
if (output == NULL) {
output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);
}
hrm_z_l_init = create_tensor(tn(LLM_TENSOR_HRM_Z_L_INIT), { n_embd }, 0);
const int lps = hparams.n_hrm_layers_per_stack;
// blocks [0, lps) hold the low stack, blocks [lps, 2*lps) hold the high stack.
// the first low and high passes create the layers; later passes alias them.
const int l_first = 0;
const int h_first = hparams.n_hrm_l_cycles * lps;
for (int h = 0; h < (int) hparams.n_hrm_h_cycles; ++h) {
for (int l = 0; l < (int) hparams.n_hrm_l_cycles + 1; ++l) {
const int slot_base = (h * (hparams.n_hrm_l_cycles + 1) + l) * lps;
const int blk_base = l == (int) hparams.n_hrm_l_cycles ? lps : 0;
if (h > 0 || (l > 0 && l < (int) hparams.n_hrm_l_cycles)) {
// alias pass: these cache slots hold the same layers as the first passes
const int src_base = l == (int) hparams.n_hrm_l_cycles ? h_first : l_first;
for (int il = 0; il < lps; ++il) {
layers[slot_base + il] = layers[src_base + il];
}
continue;
}
for (int il = 0; il < lps; ++il) {
auto & layer = layers[slot_base + il];
const int bid = blk_base + il;
create_tensor_qkv(layer, bid, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0);
// sigmoid attention gate, applied to the attention output before o_proj
layer.wqkv_gate =
create_tensor(tn(LLM_TENSOR_ATTN_GATE, "weight", bid), { n_embd, n_embd_head_k * n_head }, 0);
layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", bid), { n_embd_head_k * n_head, n_embd }, 0);
layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", bid), { n_embd, n_ff }, 0);
layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", bid), { n_ff, n_embd }, 0);
layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", bid), { n_embd, n_ff }, 0);
}
}
}
}
std::unique_ptr<llm_graph_context> llama_model_hrm_text::build_arch_graph(const llm_graph_params & params) const {
return std::make_unique<graph>(*this, params);
}
// one stack invocation: lps pre-norm decoder layers, then the parameterless final norm
ggml_tensor * llama_model_hrm_text::graph::build_stack(llm_graph_input_attn_kv * inp_attn,
ggml_tensor * inp_pos,
ggml_tensor * cur,
int slot_base) const {
const float kq_scale = 1.0f / sqrtf(float(n_embd_head_k));
const int lps = model.hparams.n_hrm_layers_per_stack;
for (int il = 0; il < lps; ++il) {
const int s = slot_base + il;
const auto & layer = model.layers[s];
ggml_tensor * inpSA = cur;
cur = build_norm(cur, nullptr, nullptr, LLM_NORM_RMS, s);
cb(cur, "attn_norm", s);
// sigmoid-gated self-attention (same shape as qwen3next attention layers)
{
ggml_tensor * gate = build_lora_mm(layer.wqkv_gate, cur);
cb(gate, "attn_gate_proj", s);
auto [Qcur, Kcur, Vcur] = build_qkv(layer, cur, n_embd_head_k, n_head, n_head_kv, s);
Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
ext_factor, attn_factor, beta_fast, beta_slow);
cb(Qcur, "Qcur", s);
Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
ext_factor, attn_factor, beta_fast, beta_slow);
cb(Kcur, "Kcur", s);
cur = build_attn(inp_attn,
nullptr, nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, s);
cb(cur, "attn_pregate", s);
gate = ggml_sigmoid(ctx0, gate);
cb(gate, "attn_gate_sigmoid", s);
cur = ggml_mul(ctx0, cur, gate);
cb(cur, "attn_gated", s);
cur = build_lora_mm(layer.wo, cur, layer.wo_s);
cb(cur, "attn_out", s);
}
cur = ggml_add(ctx0, cur, inpSA);
cb(cur, "attn_add", s);
inpSA = cur;
cur = build_norm(cur, nullptr, nullptr, LLM_NORM_RMS, s);
cb(cur, "ffn_norm", s);
cur = build_ffn(cur,
layer.ffn_up, nullptr, nullptr,
layer.ffn_gate, nullptr, nullptr,
layer.ffn_down, nullptr, nullptr,
nullptr,
LLM_FFN_SILU, LLM_FFN_PAR, s);
cb(cur, "ffn_out", s);
cur = ggml_add(ctx0, cur, inpSA);
cb(cur, "ffn_add", s);
cur = build_cvec(cur, s);
cb(cur, "l_out", s);
}
cur = build_norm(cur, nullptr, nullptr, LLM_NORM_RMS, slot_base);
cb(cur, "stack_norm", slot_base);
return cur;
}
llama_model_hrm_text::graph::graph(const llama_model & model, const llm_graph_params & params) :
llm_graph_context(params),
model(model) {
ggml_tensor * cur;
// {n_embd, n_tokens}, scaled by hparams.f_embedding_scale inside build_inp_embd
ggml_tensor * zH = build_inp_embd(model.tok_embd);
ggml_tensor * inp_pos = build_inp_pos();
auto * inp_attn = build_attn_inp_kv();
ggml_tensor * inp_out_ids = build_inp_out_ids();
// the learned low-cycle state is [n_embd]; binary ops broadcast it over [n_embd, n_tokens]
ggml_tensor * zL = model.hrm_z_l_init;
for (uint32_t h = 0; h < model.hparams.n_hrm_h_cycles; ++h) {
for (uint32_t l = 0; l < model.hparams.n_hrm_l_cycles; ++l) {
const int slot_base = (h * (model.hparams.n_hrm_l_cycles + 1) + l) * model.hparams.n_hrm_layers_per_stack;
zL = build_stack(inp_attn, inp_pos, ggml_add(ctx0, zH, zL), slot_base);
}
const int slot_base = (h * (model.hparams.n_hrm_l_cycles + 1) + model.hparams.n_hrm_l_cycles) *
model.hparams.n_hrm_layers_per_stack;
zH = build_stack(inp_attn, inp_pos, ggml_add(ctx0, zH, zL), slot_base);
}
cur = zH;
if (inp_out_ids) {
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
}
cb(cur, "result_norm", -1);
res->t_embd = cur;
cur = build_lora_mm(model.output, cur, model.output_s);
cb(cur, "result_output", -1);
res->t_logits = cur;
ggml_build_forward_expand(gf, cur);
}