Skip to content

fix: make trunk_kern per-model instead of process-wide (#68) - #76

Closed
Cid-oe wants to merge 11 commits into
sqliteai:mainfrom
Cid-oe:fix/issue-68
Closed

Cid-oe wants to merge 11 commits into
sqliteai:mainfrom
Cid-oe:fix/issue-68

Conversation

@Cid-oe

@Cid-oe Cid-oe commented Sep 25, 2026

Copy link
Copy Markdown

Closes #68

Description

This resolves the issue where a Qwen load sets the trunk kernel for the whole process.

Before this change, trunk_kern was a file-static in src/model.c read directly by matvec_t_inner for every 4-bit trunk tensor, meaning that loading a Qwen container after another container would unexpectedly shift the first container's computations to i8mm.

With this fix:

  • trunk_kern is moved into the waste_model struct.
  • matvec_t_inner is updated to read m->trunk_kern.
  • WASTE_TRUNK_KERNEL stays as a process-wide pin overriding the per-model default.
  • waste_model_set_sdot4 is preserved for tests/sweep.c, honoring the global preference as it did before.

Cid-oe and others added 11 commits September 2, 2026 01:45
A Qwen load sets the trunk kernel for the whole process because
 was a static variable read by  globally.
If multiple models were loaded into a single process, the first model's
kernel choice would incorrectly affect subsequent ones, changing
computations dynamically.

This patch:
- Adds  to
- Initializes  in
- Modifies  to read  instead of the static.
- Leaves  and  alone, as they
  still define the process-wide default properly.

Closes sqliteai#68
@marcobambini

Copy link
Copy Markdown
Member

Thanks for taking #68 on — the diagnosis in the description is right, and
trunk_kern on waste_model with matvec_t_inner reading m-> is the shape
the issue asked for. I am closing this one, and I want to be specific about why
rather than just point at the other PR.

It is superseded. #75 implements the same idea, is based on current main,
carries the tests/run.sh check the issue asked for ("two contexts in one
process … logits taken before and after a Qwen waste_open"), and I have
verified it end to end. It is the one that lands.

This branch cannot be merged as it stands. It branches from c66c7b3
(0.7.2) and conflicts with main in Makefile, src/model.c and
serve/chatfmt.py. It also carries ten commits of an AMD ROCm/HIP backend —
src/hip.hip, tools/hipbw.cpp, ENGINEERING_NOTES.md, the "Day 1" … "Day 5"
series — which is a separate piece of work (see #11 for the equivalent CUDA
question) and needs its own PR, its own measurements and its own discussion.

On current main, the fix as written is incomplete — and I measured what it
does.
0.7.2 has no Qwen support at all (grep -c arch_qwen src/model.c → 0),
so this branch cannot exercise the bug it fixes; and on main the kernel is
read in three places, not one — matvec_t_inner, matvec_t_batch and
prequant_ok — with sdot4_sg read in four. I transposed exactly this patch onto main
(m->trunk_kern in matvec_t_inner, initialised right after
model_opts_init, 9 lines in model.c + 1 in model.h — the same diffstat as
here) and ran the Qwen fixture through test_forward, same tokens, logits
compared byte for byte:

tree Qwen default vs WASTE_TRUNK_KERNEL=2 vs WASTE_TRUNK_KERNEL=0
main 09fcff3 0 / 256 differ (i8mm is the default) 256 / 256, max abs 3.00e-04
this patch on main 256 / 256, max abs 2.33e-04 256 / 256, max abs 1.70e-04

The default now matches neither kernel. The reason is the two halves:
matvec_t_inner reads m->trunk_kern, which is copied from the static
before the arch_qwen branch writes it, so it stays TK_F32; while
matvec_t_batch and prequant_ok still read the static, which the Qwen load
has set to TK_I8MM (and matvec_t_prequant, which prequant_ok gates, takes
the static sdot4_sg). One model therefore runs its
projections through two different kernels — arithmetic that corresponds to no
supported WASTE_TRUNK_KERNEL setting and that nothing reports. A Qwen
container also silently loses the i8mm default it is measured with
(LEARNED §83: 7.45 → 9.54 tok/s).

sdot4_sg stays a file-static too, and tests/sweep.c is not updated: it calls
waste_model_set_sdot4(arm[a], …) on an already loaded model to flip arms
inside one process, which is the whole point of sweep trunk=0,2,3,1
(LEARNED §83) — after this change that call no longer reaches
matvec_t_inner, so the arm it names stops being the arm it measures. #75
adds waste_model_set_kernel(m, …) for exactly this and ports both sweep.c
and kernel_kl.c to it.

Separately, and this is the part that would need removing whatever happened to
#68.
The commit b8988e6 — titled as the #68 fix — also changes three
serve/ files in ways the message does not mention:

  • serve/server.py writes every incoming chat request body to
    /home/cid/models/<id>.json and /home/cid/models/last_request.json, plus
    last_hermes_tokens.json, and prints [FORENSIC] lines with each message's
    role and length. That is a hardcoded path from your machine, and it means a
    server that would persist its users' prompts to disk on every request.
  • it deletes the comment blocks explaining why the engine lock spans prompt
    building and generation, and why chat_error is a 400 rather than a 501.
    Those are load-bearing here — they record defects that were actually hit.
  • it hardcodes model_info["ctx_max"] = 65536 for any arch whose name contains
    kimi, overriding what the container reports.
  • serve/api.py silently remaps reasoning_effort: "medium" to "high" and
    drops the note saying K3's own encoder documents medium and then rejects
    it, so a request asking for one effort is answered at another without being
    told.
  • serve/chatfmt.py replaces the by-name refusal of tool definitions on a
    container whose tokenizer has no tool markers with a hand-written JSON prompt
    injected as a system block. docs/SERVE.md is deliberate about that path:
    what the format cannot express is refused by name rather than approximated.

None of that reads as intentional for this PR — it looks like a debugging branch
that got committed along with the fix. Worth checking your working tree before
the next one.

If you would like to take the ROCm/HIP work forward, please open it as its own
PR off current main, and put the numbers behind tools/hipbw.cpp in the
description — #11 sets out what the equivalent CUDA case would have to prove,
and the same bar applies. That part is genuinely interesting and nobody has
built it.

CI never ran on this branch — gh pr checks 76 reports no checks — so none of
this had a chance to show up before review.

Closing against #75.

🤖 Generated with Claude Code

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A Qwen load sets the trunk kernel for the whole process, so a Kimi context already open switches to i8mm

2 participants