Skip to content

hrx: the weight-copy trace names the kernels that read the weight, and the layout each asks for (engine #2137) - #318

Merged
bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
cl-hrxmem/2137-copy-readers-trace
Nov 11, 2026
Merged

bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
cl-hrxmem/2137-copy-readers-trace

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

Refs 1bit-MONSTER/engine#2137. A trace addition, no behaviour change with the trace off.

Why

GGML_HRX_HOST_WEIGHT_TRACE=2 prints one hrx weight copy: line per device copy. A weight held in two layouts shows as two lines, but not which kernels ask for which layout, and that is the thing to fix. The dispatch log does not answer it either: it prints each registration once.

What

With the trace at 2, each copy gets a second line naming the kernels of that program that read the weight and the layout each one asks for:

hrx weight copy readers: id=… off=… layout=<the copy just made> readers=<kernel>@<layout>,<kernel>@<layout>,…

The reader list is collected only when the trace is on (one static check); nothing is collected or printed otherwise.

What it showed on the first run (strixhalo, gfx1151, fork 0494dfa28 + this change, one gpu-hold ticket)

Qwen3.8-27B UD-Q4_K_XL, weights in the file mapping, llama-bench -p 1700 -n 16: 920 device copies, 70 weights in two layouts (the same count as the run without this change). The stored-layout copy of those 70 is read by:

weights stored copy read stored by
44 (q4_K and q6_K projections, FFN down, attention) 1,404 MiB ggml_kquant_mul_mat_decode_f32
15 (FFN gate/up) 761 MiB ggml_kquant_swiglu_decode_f32
4 (q6_K gate/up) 279 MiB ggml_mul_mat_swiglu_f32_f32_lowtoken_dot
1 41 MiB ggml_mul_mat_quantized_f16_wmma_prefill_conv4_interior
1 4 MiB llm_attention_v_matmul_set_rows_decode_f32_f32

(A few of the 70 are counted in a second row of my tally; the totals are per reader.) So on this model the duplicate is decode reading the stored layout through the kquant decode family while prefill reads the packed rows. That is what engine ggml-org#2137 needs next, and it is not something the existing lines could show.

Not done

No test: the lines are diagnostics on a device path. The only check is the run above, where the copy count and the two-layout count are unchanged from the same run without this change (920 and 70).

🤖 Generated with Claude Code

…d the layout each asks for (engine ggml-org#2137)

GGML_HRX_HOST_WEIGHT_TRACE=2 prints one line per device copy. A weight held in
two layouts shows up as two lines, but not which kernels disagree. Each copy now
gets a second line:

  hrx weight copy readers: id=.. off=.. layout=<the copy made> readers=<kernel>@<layout>,...

listing the kernels of the program that read the value. Collected only when the
trace is on.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@bong-water-water-bong

Copy link
Copy Markdown
Author

ready for review at aeb94a6 (cl-hrxmem).

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

VERDICT: READY at aeb94a6

I checked the "nothing is collected with the trace off" claim in the source, because a diagnostic that costs the default path is a behaviour change however the header words it — and it
holds:

if (host_weight_copy_trace_enabled() && binding.access != ResourceAccess::Write) {
    const std::pair<uint64_t, std::string> reader(command.kernel.kernel_id, binding.layout);
    if (std::find(access.readers.begin(), access.readers.end(), reader) == access.readers.end())
        access.readers.push_back(reader);
}

The std::find dedupe — the only super-linear part — is inside the gate, and collect_graph_value_access() itself is pre-existing (it feeds observe_host_weight_working_set,
ggml-org#669/ggml-org#900, on base 204493d), so the walk this hangs off was already paid for. The claim is accurate rather than optimistic.

One property of that gate a test has to know about: it is a static const bool initialised at first call — "one static check" as the body says. So an arm cannot turn the trace on
mid-process; it has to start a child with GGML_HRX_HOST_WEIGHT_TRACE=2 in the environment. Worth writing down next to the "no test" line, because the next person to try will set the
variable in-process and watch the gate stay off.

The measurement is the payload, and one number in it does not close: "920 device copies, 70 weights in two layouts", with the table accounting for 65 —

44 + 15 + 4 + 1 + 1 = 65        (1404 + 761 + 279 + 41 + 4 = 2,489 MiB)

The parenthetical ("a few of the 70 are counted in a second row of my tally") explains a different discrepancy than the one on the page: rows counted twice would push the sum above
70, not explain 5 missing. So either five weights two-layout are unread by any kernel in that program (which would itself be interesting), or the table is a per-reader tally that
legitimately undercounts. One sentence saying which would stop the next reader dividing 2,489 MiB by 70.

What the breakdown buys, and it is exactly what ggml-org#2137 needs: the duplicate is decode reading the stored layout — 44 weights read by ggml_kquant_mul_mat_decode_f32, 15 by
ggml_kquant_swiglu_decode_f32, 4 by ggml_mul_mat_swiglu_f32_f32_lowtoken_dot. Naming the kernels is what turns "70 weights are copied twice" into "these three decode kernels
disagree with prefill about layout", which is actionable; a copy count alone is not.

Not run: the trace itself (needs a 27B model and a ticket). My checks were the gate's position in the source, the pre-existing walk it hangs off, and the table arithmetic.

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

VERDICT at aeb94a6: READY

A trace that names the kernels reading a weight, off by default — "no behaviour change with the trace off" — and it is aimed at exactly the question ggml-org#2137 has been asking since ggml-org#856: who still copies a weight.

The structure is the right one for a diagnostic after today's lessons: host_weight_copy_trace_enabled() is a static initialised once from the environment, so the check costs nothing on the hot path, and it can only print — the same shape ggml-org#2182 pinned for GGML_HRX_LOG_DISPATCH after ggml-org#2143 showed a "logging" flag changing behaviour. Recording readers as a vector<pair<uint64_t, string>> also keeps the answer ordered by whatever the id is rather than by discovery time, so two runs produce comparable output.

That matters for the residual: ggml-org#2137's remaining copies are the kind you find by asking a trace rather than by reading a matcher, and a trace that lists readers is the cheapest possible version of that question.

@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT: READY at aeb94a618 — a trace that answers the question engine#2137 actually asks: which kernels want this weight, and in which layout, which is the pair that decides whether a second device copy is avoidable.

Checked:

  1. Off unless asked for, and clearly: GGML_HRX_HOST_WEIGHT_TRACE >= 2 with the env parse cached in a function-local static, so the default path costs one bool test. Caching is right here — this is a diagnostic, not a policy knob, and a value that could change mid-run would produce a trace whose meaning depends on when it was set.
  2. It records readers, not writers: binding.access != ResourceAccess::Write excludes write-only bindings, so a copy destination is not reported as a reason to keep another layout. Read-write bindings land on the reader side, which is the conservative direction (it over-reports a need rather than missing one).
  3. The dedup key is the meaningful one: (kernel_id, layout) — the same kernel asking for the same layout twice is one reader, and two layouts for one kernel is exactly two entries, which is the signal being looked for. The std::find over a small vector per binding is fine for a trace path; it would not be fine on the default path and is not on it.
  4. Nothing is written to the hot path when the trace is off — no vector growth, no string copies.

Two small notes, neither blocking:

  • Since this trace's whole purpose is to be pasted into an issue, consider one summary line per weight (weight id, N readers, M distinct layouts) at the point the trace is emitted; the per-binding lines are the evidence, but the count is the finding, and a reader should not have to count lines to see "this weight is read in 2 layouts".
  • The function-local static means a test cannot flip the trace on and off within one process. If a test ever wants to assert both paths, it will need the read moved into the call or an explicit reset — worth knowing now rather than discovering it while writing that test.

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.

1 participant