OpenFlowLM-Next icon indicating copy to clipboard operation
OpenFlowLM-Next copied to clipboard

open_npue: the two co-resident hw_context objects, measured — stock FLM only ever opens one

Open Platano78 opened this issue 2 months ago • 3 comments

Before filing I checked the open issues (#29, #30, #32, #33, #35) and the merged #21–#28 stack; none of them touches hw_context co-residency, and PR_open_npue.md is the only place the question is raised. So this is the unclaimed test, not a duplicate.

PR_open_npue.md lists as unmeasured:

The two co-resident hw_context objects are unmeasured, because the LLM never loaded here for want of its own xclbins. A real serve <llm> --embed 1 on a machine with a full xclbin tree is the test that has not happened.

I have that machine — Strix Halo (Ryzen AI Max+ 395), Ubuntu 26.04, kernel 7.0.0-31, in-tree amdxdna 0.7, XRT 2.21.75, full xclbin tree, FLM v1.0.4. Ran it.

Result: stock FLM opens exactly ONE /dev/accel/accel0 fd

flm serve gemma4-it:e4b -e 1 --host 127.0.0.1 --port 8093

Server log confirms both models resident:

[FLM]  Embedding mode enabled: reserving additional 300MB of memory
[FLM]  Loading model: .../Embedding-Gemma-300M-NPU2
[FLM]  Loading model: .../Gemma4-E4B-IT-NPU2

fd count on the flm-real pid, at three points:

stage accel fds
after start 1
after a chat completion (returned real output) 1
after /v1/embeddings (returned a real 768-dim vector) 1

Same for the -a 1 configuration (LLM + whisper ASR), which is what I run in production: also one fd.

Methodological note, because it nearly caught me. The server answers /v1/models about 5 s after launch, long before a 9 GB model could have loaded — it is lazy. An fd count taken at that point is measuring nothing. The numbers above are only after both paths returned real output.

What I read into it — and where I would not

Stock FLM appears to multiplex every model through a single device handle rather than opening a context per model. If that is right, then two co-resident hw_context objects are a new condition open_npue would introduce, not something the existing runtime already survives — so there is no upstream behaviour to appeal to, and your caution in the PR text looks well placed rather than over-careful.

What I have not shown: that a second context fails, or that it is disallowed. I only measured that stock FLM never creates one. Someone would still have to stand open_npue up beside a loaded LLM to answer the actual question.

One adjacent datum in case it is useful: on this box the closed engine serializes at the request level — a concurrency ladder gives 0.39 → 0.32 req/s flat from 1 to 8 concurrent, with per-request latency growing linearly (2.55 s → 13.97 s) and a ~2.5 s floor even for a 3-token answer. That is a different axis from the prefill batching in #32 (tokens within one prompt), so I mention it only as context for anyone assuming the closed engine's prefill throughput implies concurrent-request headroom. It does not.

Happy to run whatever else is useful here — the box has the full xclbin tree and a repro takes about two minutes.

Platano78 avatar Sep 09 '26 14:09 Platano78

Thank you. By "stock flm" do you mean the original project, or something compiled from here? Would be interesting to test flm bench qwen3:8b -i bench-1k.json on both original and new. Download the config from bench config . If you don't have qwen3:8b, please feel free to use any model you have installed - no need to install one.

And xrt-smi examine --report aie-partitions after an embedding is returned and after a llm response.

vegah avatar Sep 09 '26 17:09 vegah

@vegah — thank you, and the correction goes the other way: your instrument refutes my result.

I was wrong: 14 co-resident hw_contexts, not one

xrt-smi examine --report aie-partitions, stock flm serve gemma4-it:e4b -e 1, one /dev/accel/accel0 fd:

Partition Index : 0    Columns: [0, 1, 2, 3, 4, 5, 6, 7]
HW Contexts:  PID 448200, Ctx ID 1..14, all Active

Submissions after one chat completion and one embedding, showing they are really in use:

ctx 1 2 3 4 5 6 7 8 9 10 11 12 13 14
submissions 144 4 20 2 0 85 0 0 7 3 258 210 7 35

All 14 exist at load time, before any inference. The count does not change after the LLM response or after the embedding. Idle control with nothing loaded: No hardware contexts running on device.

My error: I counted file descriptors on /dev/accel/accel0 and reported it as a context count. An fd is not a context — one handle, fourteen contexts. The fd number was correct and measured something other than what I claimed.

What this means for open_npue — the opposite of what I said

I told you two co-resident hw_context objects would be a new condition your design introduces, with no upstream behaviour validating it. That is wrong. Stock FLM already runs fourteen co-resident contexts on one partition as its normal operating state. Co-residency is not the untested part; if anything the PR_open_npue.md caution can be relaxed on that specific point.

What remains genuinely untested is narrower: whether your engine's context coexists cleanly with the LLM's, which is about your allocation and lifetime, not about whether the platform tolerates more than one.

Your other question

By "stock flm" do you mean the original project, or something compiled from here?

The original — the ROCm release binary, FLM v1.0.4, not built from this fork.

flm bench on stock, with your bench-1k.json

No qwen3:8b installed here, so the two closest:

model TTFT (s) prefill tok/s decode tok/s
qwen3:4b 1.887 520.18 18.69
qwen3.5:9b 2.472 396.47 8.84

Box: Ryzen AI Max+ 395 (Strix Halo), Ubuntu 26.04, kernel 7.0.0-31, in-tree amdxdna 0.7, 8 NPU columns. xrt-smi above is a 2.26.0 source build, not the bundled 2.21.75 — flagging it in case the partition report differs across XRT versions.

I can't give you the "new" half of that comparison — I have not built this fork, only read it. If a Linux build is expected to work from main (or linux-clean-build) I am happy to build and re-run both, but I would rather say I haven't than imply a comparison I didn't make.

One more datum you may find useful, clearly labelled as a different axis from #32: on this box the closed engine serializes at the request level — 0.39 → 0.32 req/s flat from 1 to 8 concurrent, per-request latency 2.55 s → 13.97 s, and a ~2.5 s floor even for a 3-token answer. I previously attributed that to the single-fd multiplexing, and that explanation dies with the correction above — with 14 contexts it cannot be the reason. The throughput numbers are measured and stand; the mechanism is now an open question.

Platano78 avatar Sep 09 '26 18:09 Platano78

Closing the loop on the open question I left in my last comment, plus one qualification to something I told you.

The serialization mechanism is not in the closed engine — it is in server.cpp

I said the throughput numbers stood but the mechanism had become an open question. It is answered, and the answer is in this repo's own code rather than in the closed binaries:

// src/server/server.cpp:19-20, 156-170
std::mutex g_npu_access_mutex;
std::atomic<bool> g_npu_in_use{false};

bool NPUAccessManager::try_acquire_npu_access() {
    std::lock_guard<std::mutex> lock(g_npu_access_mutex);
    if (g_npu_in_use.load()) { ... }     // one process-global boolean

requires_npu_access() (server.cpp:183-192) puts /v1/chat/completions, /v1/embeddings and /v1/audio/transcriptions behind that single flag — the comment says "restricted to one user at a time", so it is deliberate, not an accident. Plus a hardcoded npu_cooldown = 333ms (server.cpp:626) on the queue drain.

Confirmed present in the stock closed v1.0.4 binary too (nm -C exports g_npu_access_mutex, g_npu_in_use, NPUAccessManager::try_acquire_npu_access, WebServer::process_next_npu_request), so this is the same mechanism in both.

Measured, on this box

Fired an embedding 0.4 s into a 120-token chat. One server, both paths pre-warmed, different models on different hw_contexts:

alone concurrent
embedding 0.30 s 11.14 s (36.7x)
chat, 120 tok 11.11 s 11.36 s

The embedding finished +0.18 s after the chat completed, then ran at its normal speed. It was not competing for the device — it was waiting on the flag.

Why this may matter to your roadmap: the closed dispatchers are not what is preventing concurrent requests. This gate is upstream of all of them, and it is code you already own.

The qualification — I over-reassured you

I told you co-residency was fine because stock FLM runs 14 contexts. Both halves need narrowing:

  1. Active in xrt-smi means resident, not executing. 14 Active contexts are 14 loaded kernels, not 14 running jobs. My original framing implied more than the field says.
  2. The 14 are one per xclbin, not a concurrency pool — max_xclbins = 16 in npu_utils_xrt.hpp:479 ("hard constraint from the XRT driver"), and on disk Gemma4-E4B-IT-NPU2 has 10 xclbins, Embedding-Gemma-300M-NPU2 has 4. Exactly 14.
  3. Most importantly: under AIE2_TEMPORAL_ONLY every context is allocated start_col 0 across the full array, which matches 14 contexts on Partition 0 spanning columns [0-7]. They co-reside, but they time-share — firmware context-switches between them. The UAPI exposes exactly the counters that implies (migrations, preemptions, suspensions).

So "co-residency works" stands, but it does not imply "concurrent execution works". If open_npue adds a context alongside the LLM's, the platform will allocate it; what it will not do is run both in parallel.

What I would expect from removing the gate — bounded

Recovering the 333 ms cooldown and the pipeline bubbles (tokenization, sampling, HTTP, the gap between one request ending and the next starting) is real, and on a serialized ~2.5 s floor the cooldown alone is ~12%. But with a time-shared array on bandwidth-bound decode, I would not expect anything like N× — better utilization and latency overlap, not parallel throughput. Flagging that so the change is not oversold before someone spends a sprint on it.

Different axis from #32, to be explicit: that is tokens within one prefill; this is concurrent requests. They are independent.

Limits on the above

  • Driver citations (amdxdna_ctx.c, aie2_ctx.c) are mainline master, not the running Ubuntu 7.0.0-31 kernel — no amdxdna .c source ships in this distro's headers. Object lists match, so the structure holds, but treat them as upstream-current.
  • I could not confirm AIE2_TEMPORAL_ONLY is set on this silicon. Module params are root-only; xrt-smi reporting Total Columns: 8 is consistent with it, not proof.
  • I have still not built this fork, so everything above is stock v1.0.4 plus reading your source.

Happy to run the counter-level version of this (polling command_submissions/completions/ preemptions per context through a concurrent burst) if that would be more useful than wall-clock.

Platano78 avatar Sep 09 '26 20:09 Platano78