Skip to content

hrm-text : prefix-LM attention for models with prefix_lm set - #29635

Open
bolgacg wants to merge 1 commit into
ggml-org:masterfrom
bolgacg:hrm-text-prefix-lm-upstream
Open

bolgacg wants to merge 1 commit into
ggml-org:masterfrom
bolgacg:hrm-text-prefix-lm-upstream

Conversation

@bolgacg

@bolgacg bolgacg commented Sep 29, 2026

Copy link
Copy Markdown

Overview

HRM-Text models (DFM Mimir) are trained as prefix LMs: prompt tokens attend to each other in both
directions and only generated tokens are causal. Master reads prefix_lm into hparams.hrm_prefix_lm
since #27625 but still builds a causal mask, so Mimir runs in a mode it was not trained for.

For these models set_input_kq_mask drops the causal check, so a token attends to every token of
its own sequence in the cache or the current ubatch. Generation appends one token per sequence per
step, so generated tokens still see only the past. This reproduces the training mask. Two guards:

  • llama_context::decode warns when a prompt is split across ubatches (tokens in different ubatches
    cannot see each other; use -ub at least the longest prompt).
  • llama-server turns off prompt caching for these models, because a cached prefix was computed
    without the tokens that followed it. It reads the flag from the model metadata, so there is no new
    public API.

Additional information

Checked on DAISY (592 Danish questions from the model's own group) with a Q8_0 GGUF converted by
master's own converter from the official DFM-Mimir weights, greedy, --reasoning off, -ub 2048:

run exact match (%) answers identical to transformers
master, causal only 5.9 28%
master plus this PR 8.3 93%
official transformers (prefix attention) 8.4

With one Wikipedia passage in the prompt (about 1,000 tokens) the PR build scores 65.9.

Test: test-llama-archs -a hrm_text gains a check that the first token's logits change when the
last prompt token changes with prefix_lm set, and stay the same without it. It fails on master
and passes with this PR.

Other models are unaffected. Llama-3.2-1B Q8_0 on a GTX 1060: perplexity on wikitext-2 (40 chunks)
is 14.3649 on both builds; llama-bench pp512 2380 vs 2381 t/s and tg128 90.3 vs 90.7 t/s
(PR vs master, means of the last two of three alternating runs).

History: first opened as noctrex#1 on 6 Sep; @noctrex named it the prefix-LM follow-up on
8 Sep in #27625. @peter-sk has a fuller implementation in schneiderkamplab/llama.cpp (an explicit
llama_decode_prefix API, parallel sequences, prefix caching). This PR is only the mask, so that
work can build on it in later PRs.

Not covered: speculative decoding, parallel slots sharing one ubatch, prompt caching for these models.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. I used Claude Code to help write the patch and the test and to run the evaluation scripts. I reviewed every line myself and can explain each change.

@LarsVonQualen

Copy link
Copy Markdown

Is this PR going to get merged at some point, it seems like a small change 🙂

For hrm-text models with prefix_lm set, the KQ mask drops the causal
check, so the prompt attends in both directions and generated tokens
stay causal. decode warns when a prompt is split across ubatches, and
llama-server disables prompt caching for these models. Adds a
test-llama-archs check for the mask.
@bolgacg
bolgacg force-pushed the hrm-text-prefix-lm-upstream branch from bda5dcd to 0f460f6 Compare November 13, 2026 20:15
@bolgacg
bolgacg requested review from a team as code owners November 13, 2026 20:15
@bolgacg

bolgacg commented Nov 13, 2026

Copy link
Copy Markdown
Author

Thanks @LarsVonQualen. I rebased the branch onto current master and resolved the conflicts in llama-kv-cache.cpp and test-llama-archs.cpp; the change itself is the same.

@CISC, could you take a look when you have time?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

model Model specific server testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants