Repository navigation
npu: one partition width and one hardware context per model, so the NPU can be shared (engine#2689 item 1) - #2697
Conversation
… (engine#2689, part 1 of item 1) elf_partition.h reads .note.xrt.configuration (the column count a full ELF asks the driver for) from the bytes, returning 0 for anything that is not one. q4nx_i8_sealed_prefill_instance names each sealed prefill instance 'main:p_<decode name>', since one hardware context refuses a second ELF with an instance key it already holds (measured 11-16: 'code 501: main:d'; with p_ names, decode main:o / main:gu and prefill main:p_o / main:p_gu ran exact in one context). Test npu_partition (host only, in CI's build): 12/12. The reader also gives 4/8/8/4 on real c4/c8/per-role/p_-prefill ELFs (checked locally against an independent parser; the ELFs are not in the tree). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…led prefill joins it (engine#2689, item 1) NPU_PARTITION=<cols> (unset = unchanged): every full ELF the model opens must declare <cols> columns (elf_partition.h), or its load is refused by name; the first ELF creates the model's one hw_context and every later one is add_config'ed into it. A duplicate instance key fails that ELF's load with the runtime's reason. NPU_I8_SEALED_PREFILL_ELF names one sealed M=128 ELF whose instances are main:p_<role>; a b8 container's prefill roles open it instead of their per-role ELFs. In partition mode a role it does not cover is refused (its per-role ELF would be a second main:seq in the context). WIP: the device gate (A per-role vs B shared byte-identical, C refusal) is pending; config keys and docs follow before the PR. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
ready for review at fb408da Item 1 of #2689. The gate is a single |
|
*READY — and the driver behaviour it records is the part worth keeping: "the driver gives a process's second same-width context the other free half, and it refuses a 4-column context next to an 8-column one in the same process." That is a hardware constraint, not a design choice, and it explains why "one context per ELF" was never going to support two models on the array. The refusals are the design, and each names its file and width — that is the pattern this repo has settled on (Q4NX
Three things I would pin:
Credited: doing item 1 of three as its own PR with the tracking issue left open; the measured driver facts quoted rather than paraphrased; Not verified by me: the host ctest and the device loads. The refused-by-name paths, the unset-default equivalence and the two-model question are what I can check, and the first two are right. |
|
VERDICT at fb408da: READY. The predicate is a bounded pure function, and the policy on "unknown" — which is where an invariant like this usually leaks — is already right. The parser reads every byte through a bounds check
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at fb408da: READY
An ELF-format reader with every refusal armed, and a table invariant that is checked in the direction that matters (no prefill key may equal a decode key).
What the change enforces, and why it is the driver's rule rather than a style
"With the driver's spatial placement (
amdxdna temporal_only=0), two processes can run on separate column halves at the same time. That only works if each model holds exactly one hardware context at one width. The driver gives a process's second same-width context the other free half, and it refuses a 4-column context next to an 8-column one in the same process."
So the engine reads the partition width itself and refuses a mismatched ELF by name at load, instead of letting the driver fail halfway — which on this box is the difference between an error message and a wedged context.
The reader is a pure function over a byte range, and every boundary is armed
PASS a 4-column note reads 4 PASS an 8-column note reads 8
PASS wrong magic reads 0 PASS a 64-bit ELF reads 0 (the full ELFs are 32-bit)
PASS a note of another name reads 0 PASS a configuration section shorter than a note reads 0
PASS a truncated section table reads 0, not out of bounds
PASS a file shorter than an ELF header reads 0
reads 0, not out of bounds is the sentence that makes the rest reviewable: every malformed input is a refusal, not a crash and not a plausible width. 0 is the sentinel and it is used consistently for "not this note".
Mutant — drop the ELFCLASS32 check:
FAIL a 64-bit ELF reads 0 (the full ELFs are 32-bit)
It bites, and the arm's text says why the constraint exists (the full ELFs are 32-bit) rather than just asserting a magic number.
The table invariant
PASS every role with a decode instance has a prefill instance, and only those
PASS each prefill instance is its decode name with a p_ prefix
PASS no prefill instance key equals any decode instance key
PASS a role with no decode instance has none
no prefill instance key equals any decode instance key is the one that maps to the driver's own refusal ("one context refuses a duplicate"): if the prefill instance reused a decode key, the model would ask the driver for a second context with the same key and fail at run time instead of at load. 12 arms, ok.
Host-only, and stated
"Host-only: the ELF partition-note reader (elf_partition.h) on crafted ELFs, and the sealed prefill instance table" — so this PR's evidence is checkable on any box, and the device claim it rests on (the driver's placement and its two refusals) is quoted as a measurement from 11-15 rather than re-derived here. That is the correct split: the code change is host-testable, the hardware behaviour is cited.
What I would want next, and it is a note rather than a gap: the 8-column + 4-column mix is the case the driver refuses, and it is currently enforced by construction (one width per model) rather than by a test that builds such a pair and requires a named refusal. An arm that constructs the forbidden pair and asserts the refusal message would be the mirror of no prefill instance key equals any decode instance key.
|
VERDICT at The measurements are the kind that justify a new mode
Those are driver behaviours, named with the arm they were observed in, not inferences from the docs. A change that replaces "one context per ELF" with "one context per model" needs exactly that: a statement of what the driver does today, because the new mode exists only to stop doing it. Refuse-by-name, and the default preserves the old shapeEvery full ELF must declare The parser arms cover the failure directions that matterReading a file's note is parsing hostile input, and the arms treat it that way: magic, class, name, truncation, and two out-of-bounds cases. The ask: the policy is one layer above the arms
If that header cannot be unit-tested as it stands, the acceptable answer is a sentence in the body saying where those two are exercised (a device run at |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at fb408da: READY
Both claims hold on the real artifacts, not just on the crafted ones, and both mutants bite. I checked the reader against every full ELF I could find on this box and against the exact two files the device arm used. Two non-blocking notes at the end.
Built and run here
$ ninja npu_partition_test && ./npu_partition_test
PASS a 4-column note reads 4 PASS a truncated section table reads 0, not out of bounds
PASS an 8-column note reads 8 PASS a file shorter than an ELF header reads 0
PASS wrong magic reads 0 PASS every role with a decode instance has a prefill instance, and only those
PASS a 64-bit ELF reads 0 PASS each prefill instance is its decode name with a p_ prefix
PASS a note of another name reads 0 PASS no prefill instance key equals any decode instance key
PASS a configuration section shorter than a note reads 0 PASS a role with no decode instance has none
ok
Mutants, each restored and rebuilt afterwards:
- reader stubbed to
return 8→ 7 arms fail (a 4-column note reads 4+ the six rejection arms), exactly as the body says. q4nx_i8_sealed_prefill_instance(D)returning the decode keymain:d→ 2 arms fail (each prefill instance is its decode name with a p_ prefix,no prefill instance key equals any decode instance key).
Gates: ctest -R '^(copyright_gate|ci_targets|tests_referenced|npu_decl_check|npu_partition)$' → 5/5 passed (pre_pr_checks_agreement cannot pass out of an out-of-source build dir — pre-pr-checks: not inside a git work tree — so I ran the script itself in the source tree: 13 checks green, 2 skipped only for the missing gpu-kernels tree here).
The reader against real ELFs, which is where a 32-bit-only parser usually dies
I compiled elf_partition.h standalone (/tmp/rd.cpp, no NPU SDK needed) and ran it over every *.elf under ~ that I could reach (6,000 files): 1,150 read 8, 23 read 4, 4,791 read 0 — and every 0 is an elfs_main_core_*/probeElfs_* per-core ELF with no .note.xrt.configuration at all, i.e. not something init_elf ever hands it (is_full_elf() gates the call). No full ELF read 0 and no read a garbage width. All 266 ELFs under ~/models/npu read 8.
The two files the device arm B actually used, i.e. the artifact this PR's claim rests on:
$ /tmp/rd ~/.cache/scratch-cl-npurt/npuconc/kq17-shared/SEALED-q17-{pre,dec}-c4.elf
4 …/SEALED-q17-pre-c4.elf
4 …/SEALED-q17-dec-c4.elf
$ /tmp/rd /home/bcloud/ogaload/families/kernels-qwen17-b8/full_i8_{QKV_K2048_N4096,D_K6144_N2048}_b8.elf
8 …/full_i8_QKV_K2048_N4096_b8.elf
8 …/full_i8_D_K6144_N2048_b8.elf
So arm C's refusal (8-column set with NPU_PARTITION=4) is reading a real 8, and arm B's acceptance is reading a real 4 — not a crafted note.
The p_ prefix, checked in the ELF bytes rather than in the table
The claim that makes one shared context legal is that the prefill ELF's instances never collide with the decode ELF's. On the two shipped artifacts:
$ strings -a SEALED-q17-pre-c4.elf | grep -E 'p_qkvfa|qkvfa' | sort -u
p_qkvfa
$ strings -a SEALED-q17-dec-c4.elf | grep -E 'p_qkvfa|qkvfa' | sort -u
qkvfa
One carries p_qkvfa, the other qkvfa; no main:p_* name appears in the decode ELF. That is the fact the "no duplicate instance key" arm asserts, confirmed outside the source tree.
Notes, not change requests
static std::shared_ptr<xrt::hw_context> shared;(npu_engine_i8ctx_inc.h) is process-scoped, not model-scoped — the wording here and indocs/npu.mdis "one context per model". While a process serves one model the two are the same thing (verified: unified.cpp opens either the private route or the layer lane, never both; forward_serve builds one forward), so this is latent rather than a bug today. It becomes a bug the day a second ELF set exists in one process: that set's ELFs wouldadd_configinto the first model's context with the samemain:*keys and get thecode 501the docs already name, where today each set opens its own context. If item 2 of #2689 (multi-user decode) puts a second consumer on the process, this is the line that decides who owns the context. Worth either keying it to the model's own state (thepoolbeside it is keyed by path and can hold the first host's context) or saying "one model per process" in the one comment that currently says "per model".NPU_PARTITIONoutside 1..8 turns the mode off in silence:npu_partition_cols()is(c >= 1 && c <= 8) ? c : 0, soNPU_PARTITION=0,=9or a typo like=a4leaves no trace and the run is the old multi-context route while the operator believes sharing is on. Onefprintfwhen the variable is set but the value is not 1-8 would make the misconfiguration say its own name.
Everything else — the width refusal naming the file and its width, the by-name refusal for an uncovered prefill role, the unchanged default path (shared_ptr with a single owner where unique_ptr was), the Apache notice on the new file, and the docs paragraph in this PR — checks out. Green gates plus my own verification at fb408dad.
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
Refs #2689 (item 1 of three; the tracking issue stays open)
One width, one hardware context per model. Today the engine opens one
hw_contextper full ELF: a b8 b8container's Qwen3-1.7B set is 8 contexts (4 decode roles + 4 prefill roles), each asking the driver for the
whole array. That is the wrong shape for sharing the NPU: with spatial placement two models each get a column
half, but only if each model holds exactly one context at one width. Measured on this box (11-15): the driver
gives a process's second same-width context the other free half, and it refuses a 4-column context next to
an 8-column one in the same process.
What changed
NPU_PARTITION=<cols>(npu/forward/engine-src/npu_engine_i8ctx_inc.h,npu/forward/include/elf_partition.h).Partition mode: every full ELF the model opens must declare
<cols>columns in its.note.xrt.configuration, or the load is refused naming the file and its width. The first ELF createsthe model's one context and every later ELF joins it with
hw_context::add_configinstead of opening itsown. Unset (the default), every ELF still opens its own context exactly as before.
NPU_I8_SEALED_PREFILL_ELF=<file>(npu/target/xdna/xdna_target.cpp,q4nx_i8_sealed_prefill_instance).One sealed M=128 ELF for a b8 container's prefill roles, so prefill can live in the decode context. One
context cannot hold two instances with the same key — the runtime refuses the second ELF, measured as
code 501: main:d— so its instances are the decode names with ap_prefix (main:p_qkvfa,main:p_o,main:p_gu,main:p_d). In partition mode a role the ELF does not cover is refused by name, because itsper-role ELF would be a second
main:seq.tests/npu_partition_test.cpp(new,ctest -R npu_partition, registered in CI's target list) and adocs/npu.mdparagraph with the two settings and thesealed/build.sh … -c 4 --partition 4line thatproduces such a set.
Host proof
12 arms: the partition note reads 4 and 8; wrong magic, a 64-bit ELF, a note of another name, a note
section shorter than a note, a truncated section table and a file shorter than an ELF header all read 0
rather than a guess; and the prefill instance table pairs with every role that has a decode instance, keeps
the
p_prefix, and is disjoint from the decode keys.Mutants, each restored afterwards:
8→ 7 arms fail (a 4-column note reads 4, all six rejection arms);q4nx_i8_sealed_prefill_instancereturning the decode key → both table arms fail(
each prefill instance is its decode name with a p_ prefix,no prefill instance key equals any decode instance key).Device proof (one
npu-holdticket, 36 s, production driverE81A7B40,temporal_onlyat its default)A = today's route:
kernels-qwen17-b8, per-role 8-column ELFs, 8 contexts.B = partition route:
NPU_PARTITION=4+NPU_I8_SEALED_ELF=SEALED-q17-dec-c4.elf+
NPU_I8_SEALED_PREFILL_ELF=SEALED-q17-pre-c4.elf, same container.B's own log, the part that matters here: the prefill ELF opens the model's context and the decode ELF joins
it — one 4-column context for both roles, where A has two contexts per role. Every generated position of all
three prompts has a bit-identical f32 logits dump in both arms (24 files compared with
cmp), so theshared route is the same computation, not merely the same text.
C =
NPU_PARTITION=4with the 8-column set: the load is refused before anything runs —Kernel log: 12
amdxdna/c6:00.1lines before the ticket and 12 after it — no fault line, no timeout, noreset. Decode rate is not a target here, but B was not slower: 4.46 tok/s against A's 3.90 for the same
prompts and the same binary.
What this does not do
Items 2 and 3 of #2689 stay open: a refused load should name the process holding the array (the
--load-queue-msqueue and its message exist; the holder name and half-array tokens innpu-holdareinfrastructure), and a multi-user decode step should fill N of the 16 M=16 rows instead of 1. The device side
of the second is already measured: an M=16 decode step is flat in N from 1 to 16 rows (1B ≈ 146 ms, 1.7B ≈
214 ms, exact at every N). Nothing here changes a default: both variables are opt-in.
🤖 Generated with Claude Code