Skip to content

mem_admit: a sliding-window pattern is a BOOL array, so read it like one (Fixes #1425) - #1430

Closed
bong-water-water-bong wants to merge 1 commit into
mainfrom
i1425-memadmit-bool
Closed

bong-water-water-bong wants to merge 1 commit into
mainfrom
i1425-memadmit-bool

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Fixes #1425. The issue reads as "flint is missing from model_need()'s per-arch list", but flint is not priced by a constant at all —
and the real defect is one type predicate, affecting every architecture that ships its pattern as bool.

The diagnosis, from the pin

third_party/llama.cpp/src/models/flint.cpp:14 reads LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN from the file, and
conversion/flint.py:24 writes it as an array of Python bool (gguf_writer.add_sliding_window_pattern([kind == "sliding_attention" …])
→ add_array([...])) — i.e. GGUF type 7.

app/mem_admit.h's header reader accepted t <= 5 || t == 10 || t == 11 as integers, so a bool array was skipped silently (f.ignore(n)
and no error). Consequences: the pattern lookup at :234 never fired for flint — or qwen, zaya, mellum, olmo, which write theirs the same way —
the per-arch "one full layer in N" list was the only path that ever worked, and an architecture not on that list fell to full_share = 0. That is
a lower-bound KV estimate on an admission gate, which is the unsafe direction: it admits loads that then overrun (the #680 class the
refusal line cites).

The key prefix and the polarity were both already right, so neither is touched: h.array() prefixes the arch (arch + "." + key, matching
"%s.attention.sliding_window_pattern"), and "nonzero = windowed" matches hparams.is_swa_impl (1 = SWA, 0 = dense).

-    auto is_int = [](uint32_t t) { return t <= 5 || t == 10 || t == 11; };
+    auto is_int = [](uint32_t t) { return t <= 5 || t == 7 || t == 10 || t == 11; };   // 7 = GGUF bool

Red/green on the same header (tests/mem_admit_test.cpp)

A flint-shaped header: 49 layers, 35 windowed / 14 full, window 4096, at the model's own 262144 context.

row before the fix after
the bool pattern prices its full layers at 262144 441.0000 MiB (want 3962.0000) — FAIL 3962.0000 MiB — ok
the estimate grows with the context FAIL ok
a header with no pattern stays the lower bound (every layer windowed) 441.0000 MiB 441.0000 MiB (unchanged, deliberate)

So at 262144 the estimate was 9x low (441 vs 3962 MiB) for a flint-shaped model — ~3.4 GiB the gate would admit and then overrun with, which
is the order the issue itself estimated for flint's 14 full layers. The negative control pins the documented fallback: a file that genuinely has
no pattern still prices every layer windowed; what changed is that a pattern the file does carry is no longer discarded.

Verification

mem_admit_test → PASS (whole file, both arms above), tools/copyright.py --check rc 0, built and run device-free under
~/lb/thermal-run.sh. No behaviour change for any header whose pattern is an int array or absent.

cc @pi-prs

…one (Fixes #1425)

The issue reads as "flint is missing from model_need()'s per-arch list", but flint is not priced by a
constant at all: llama.cpp's loader reads the pattern from the file
(third_party/llama.cpp/src/models/flint.cpp:14, LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN) and the converter
writes it as an array of Python bool (conversion/flint.py:24 -> gguf_writer.add_sliding_window_pattern ->
add_array([...])), i.e. GGUF type 7.

app/mem_admit.h's GGUF reader accepted t <= 5, 10, 11 as integers - so a bool array was skipped silently,
for EVERY architecture that ships its pattern that way (flint, qwen, zaya, mellum, olmo...). The pattern
lookup at :234 therefore never fired for them, the per-arch "one full layer in N" list was the only path
that worked, and an arch not on that list fell to full_share = 0: a lower-bound KV estimate on an admission
gate, which is the unsafe direction (#680's overrun class). The key prefix and the polarity were both
already right (h.array() prefixes the arch; nonzero = windowed matches hparams.is_swa_impl).

One-line fix: bool is an integer type for this reader.

Red/green, same header both ways (49 layers, 35 windowed / 14 full shape, 4096 window, 262144 context,
in tests/mem_admit_test.cpp):

  before: flint: the bool pattern prices its full layers at 262144  441.0000 MiB (want 3962.0000)  FAIL
          flint: the estimate grows with the context                                              FAIL
  after : flint: the bool pattern prices its full layers at 262144 3962.0000 MiB                  ok
          flint: the estimate grows with the context                                              ok
          flint without a pattern: still the lower bound (every layer windowed)   441.0000 MiB      ok

So at 262144 the estimate was 9x low (441 vs 3962 MiB) for a flint-shaped header - ~3.4 GiB the admission
gate would admit and then overrun with. The negative control keeps the documented behaviour for a header
that genuinely has no pattern, so the fallback is unchanged; what changed is that a pattern the file does
carry is no longer discarded.

Verified: mem_admit_test PASS (whole file), and tools/copyright.py --check rc 0. Device-free.
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis ✅

1425 - Fully compliant

Compliant requirements:

  • Fix the issue where flint is not in model_need()'s sliding-window list, causing its KV estimate to stay a lower bound at any context
  • Read the actual .attention.sliding_window_pattern from the flint GGUF (if it has one)
  • Add a mem_admit test row that a 262144-ctx flint header prices its 14 full layers at n_ctx
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Incorrect GGUF type handling

The fix correctly adds GGUF type 7 (bool) to the list of acceptable integer types for reading sliding window patterns. This ensures that architectures like flint, which write their patterns as bool arrays, are properly handled instead of being silently ignored.

auto is_int = [](uint32_t t) { return t <= 5 || t == 7 || t == 10 || t == 11; };
Test coverage for bool pattern handling

The test suite now includes a specific test case for flint with a bool sliding window pattern, ensuring that full layers are correctly priced at full context size and that the KV estimate grows with context, addressing the core issue described in the ticket.

// engine#1425: flint's sliding-window pattern is a *bool array in the file* (llama.cpp's flint loader
// reads LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, conversion/flint.py writes layer_types as bools), not a
// "one full layer in N" constant - and the reader used to drop bool arrays on the floor, so every layer
// was priced windowed and the estimate never grew with the context. 49 layers, 35 windowed / 14 full.
std::vector<bool> flint_pattern(49, true);
for (int i = 0; i < 49; i += 7) flint_pattern[size_t(i)] = false;   // 7 full layers (49/7 = 7 of them)
const auto flint = Gguf().s("general.architecture", "flint").u32("flint.block_count", 49)
    .u32("flint.embedding_length", 2048).u32("flint.attention.head_count", 16)
    .u32("flint.attention.head_count_kv", 4).u32("flint.attention.key_length", 128)
    .u32("flint.attention.value_length", 128).u32("flint.attention.sliding_window", 4096)
    .bools("flint.attention.sliding_window_pattern", flint_pattern).write(d + "/flint.gguf");
// 49 layers x 4 kv heads x 128 + 128 bytes:
const double flint_layer_kvb = 4.0 * 256 * 2;
const double flint_full = 7.0 * 262144;                  // the 7 full layers attend over the whole context
const double flint_windowed = 42.0 * (4096 + 512);       // the rest see one window per sequence + a ubatch
expect_mib("flint: the bool pattern prices its full layers at 262144", admit::model_need(flint, 262144, 1).kv,
           (flint_full + flint_windowed) * flint_layer_kvb / (1 << 20));
// the same header without the pattern is the documented lower bound (every layer windowed); the two
// numbers below differ by ~19x, which is the size of the error the fix removes at this context
const auto flint_nopat = Gguf().s("general.architecture", "flint").u32("flint.block_count", 49)
    .u32("flint.embedding_length", 2048).u32("flint.attention.head_count", 16)
    .u32("flint.attention.head_count_kv", 4).u32("flint.attention.key_length", 128)
    .u32("flint.attention.value_length", 128).u32("flint.attention.sliding_window", 4096)
    .write(d + "/flint-nopat.gguf");
expect_mib("flint without a pattern: still the lower bound (every layer windowed)",
           admit::model_need(flint_nopat, 262144, 1).kv, 49.0 * (4096 + 512) * flint_layer_kvb / (1 << 20));
// and it must not be flat in context: the whole point of the pattern is that 7 layers scale
expect("flint: the estimate grows with the context", admit::model_need(flint, 262144, 1).kv >
                                                          2.0 * admit::model_need(flint, 8192, 1).kv);

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY at df5ced44 — but this is a duplicate of #1433 and the two conflict; exactly one should land.

The fix is correct and the claim is verifiable in the tree. GGUF_TYPE_BOOL = 7 (ggml/include/gguf.h:61), the integer types are 0..5,
10 (UINT64) and 11 (INT64), and gguf_writer.py:816 is literally def add_sliding_window_pattern(self, value: int | Sequence[bool]) — so a
pattern written as a list of Python bools arrives as type 7 and was dropped by the old is_int. Adding t == 7 is the whole fix.

Why it matters, in this PR's own words, is the part worth keeping: without it model_need() falls back to the per-arch "one full layer in N"
list, and for an arch that is not on it every layer is priced as windowed — a lower bound on an admission gate, which is the unsafe direction.
That ties it to the #680 overrun class, and it is cross-consistent with what lemonade #45 says about flint: 49 layers all priced as windowed,
which is why 24.07 GiB came back at every context size. Reading the file's real pattern (35 sliding / 14 full of 49, in #1433's fixture) would
price 14 layers as full and raise the estimate — correctly.

The duplicate: #1433 makes the identical one-token change to the same is_int in the same file, with a near-identical test helper
(Gguf& bools(...) writing type 9 of type 7). git merge-tree --write-tree df5ced44 80b5eb63 reports 2 conflicts, so they cannot both land.

My recommendation, if the choice is mine to make: keep this one and fold in #1433's two unique pieces —

  • its concrete flint fixture (35 sliding / 14 full of 49), which pins the real shape rather than a generic bool array, and
  • its one-clause docs/serve.md note that flint's loader requires the array.

and close #1433. The reason I would keep this one as the base is the sentence above: "a lower bound on an admission gate, which is the unsafe
direction" is what a future reader needs in order to know why a missing type check is a safety bug rather than a tidiness one. If the other lands
first, take that sentence across instead — the union is what should merge, not either half alone.

bong-water-water-bong pushed a commit that referenced this pull request Nov 8, 2026
…s one (from #1430) (#1425)

Folds in #1430's one unique piece, so this PR is the union the two reviews asked for.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
bong-water-water-bong added a commit that referenced this pull request Nov 8, 2026
…s (flint's KV grows with the context) (#1433)

* mem_admit: read bool arrays, so a GGUF's sliding_window_pattern counts (flint's KV grows with the context) (#1425)

model_need() lets a sliding_window_pattern array in the file decide which layers attend over the
full context. The header reader kept only integer arrays (GGUF types 0-5, 10, 11), and gguf-py writes
a list of Python bools as a bool array (type 7). Most converters at the pin write the pattern that way
(flint, command-r, exaone, mellum, zaya, ...), so for them the file's pattern was skipped. For flint,
which is not in the per-architecture list, every layer was then priced as windowed: 441 MiB of KV at
any context, instead of 7,483 MiB at its own 262144.

Bool now reads as 0/1 like the other integer types. flint's loader requires the array, so flint always
takes the file's pattern; no list entry needed. Docs: docs/serve.md "Will it fit?".

tests/mem_admit_test.cpp: a bool-array fixture (bytes as gguf-py writes them) and a flint header
(49 layers, 35 windowed / 14 full, window 4096, 4 KV heads x 128+128):

  main:  FAIL flint: 14 full layers at 262144 + 35 windowed at 4096 + 512: 441.0000 MiB (want 7483.0000)
         FAIL flint: the estimate grows with the context          (31 ok, 2 FAILED)
  this:  ok   ... 7483.0000 MiB (want 7483.0000); ok grows with the context  (33 ok, PASS)

Fixes #1425

Co-authored-by: bong-water-water-bong <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

* mem_admit: say why the bool type check is a safety fix, not a tidiness one (from #1430) (#1425)

Folds in #1430's one unique piece, so this PR is the union the two reviews asked for.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Closing as superseded: main already carries this fix, from #1433 (3a799f8f, "mem_admit: read bool arrays, so a GGUF's
sliding_window_pattern counts (flint's KV grows with the context)"), and #1425 is closed (10:28Z). I only found that when the branch went
CONFLICTING against main — the conflict is in the comment above the very same predicate, with t == 7 on both sides.

For the record, since the two arrived independently and reach the same conclusion by the same route (bool arrays were dropped by the header
reader, so the pattern never fired and an unlisted arch priced every layer windowed → a lower bound on an admission gate):

  • main's version is the better test — 49 layers with 14 full / 35 windowed at 262144 asserted to the MiB, plus the "estimate grows with the
    context" check — so nothing of mine should replace it;
  • one row mine has that main's does not: a negative control, a flint-shaped header with no pattern array, asserting it stays the
    documented lower bound (every layer windowed). That is the row that pins the direction of the fallback — the thing that made this an
    unsafe-direction bug rather than a display bug — and it is cheap to add if a reviewer wants it; I am not pushing it as a second PR for one
    assertion on behaviour that mem_admit: read bool arrays, so a GGUF's sliding_window_pattern counts (flint's KV grows with the context) #1433 already fixes.

No merge from me; branch withdrawn.

@bong-water-water-bong
bong-water-water-bong deleted the i1425-memadmit-bool branch November 10, 2026 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mem_admit: flint is not in model_need()'s sliding-window list, so its KV estimate stays a lower bound at any context

1 participant