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

The 35B on open kernels: where the remaining gap is

Open Cyronius opened this issue 2 months ago • 5 comments

Follow-on to #16. The open kernels are close enough on the 35B (Qwen3.6-35B-A3B) now that the remaining gap is two specific pieces of work rather than a general slowness. Here is where it stands.

Measured on a quiet machine, all 40 layers, our open-kernel path against stock FLM 1.0.2:

open kernels closed engine
reading a 1,000-token prompt 23 ms per token 15
reading a 2,600-token prompt 25 8.5
writing tokens, with 1,000 of context 6.8 per second 12

Reading a prompt used to be three to five times slower than the closed engine. The expert kernel that just landed - it puts eight tokens through each expert in one go instead of one - brought that to 1.6x and 2.9x. Writing has not been worked on at all yet.

Reading: the biggest piece left is still on the CPU. Prompts are read 256 tokens at a time. Everything in that work is on the NPU except one step - attention, where each token is weighed against all the tokens before it - which still runs on the processor. It is also the only part that grows as the prompt gets longer: at 2,600 tokens it is 2.5 seconds of the 3.7 the CPU spends on each batch of 256. The arithmetic itself is small enough that the NPU should finish it in well under a tenth of a second. It costs twice over, because pushing that much data through the processor also slows down the NPU work that comes after it. With it moved I would expect reading to settle near 9.4 ms per token and stop growing with prompt length, against the closed engine's 8.5.

Writing: one step is running at a third of the speed everything else gets. Producing a token takes 128 ms and almost all of it is the NPU; there is no meaningful CPU time to remove. Of that, 75 ms is a single step repeated across 30 of the 40 layers - the first half of each of the model's linear-attention layers, which reads that layer's weights and updates its running state. It reads about 25 MB and takes 2.1 ms, which is 12 GB per second. The same hardware reads at 26 to 33 GB per second everywhere else in this model, so this one step is the outlier, and it is the step itself rather than anything around it: it costs the same run in isolation as it does in a real pass, and the same at one token of context as at a thousand. If it read at the rate the rest do, writing would be about 12.8 tokens a second, which is past the closed engine.

I have a guess at the cause - it hands its results back in very small pieces, where the reading side works in pieces forty times larger - but that is reasoning on paper. The first version of the expert kernel looked like the same kind of problem and turned out to be limited by something else entirely, so the first job is to measure it rather than act on the guess.

Two caveats on the table: the closed-engine numbers were measured on 11 September and not since, and the 2,600-token prompt is the same length as the one the closed engine read but not the same text.

Cyronius avatar Sep 12 '26 21:09 Cyronius

Update, because most of the numbers above are now wrong.

Reading a prompt is done, and it came out slightly better than the estimate in this issue. It is now 8.0 ms per token on a 1,122-token prompt and 8.1 on a 2,593-token one, against the 23 and 25 recorded here, and it has stopped growing with prompt length. The estimate in this issue was that moving host attention onto the NPU would settle reading near 9.4 ms per token and flatten it, which is what happened.

The comparison also changed direction. I measured both engines back to back the same afternoon this time, same container and same prompts, which is the caveat this issue flagged and could not fix. Closed came out at 16.8 ms per token on the 1,122-token prompt and 9.3 on the 2,593-token one, so we are ahead at both lengths rather than 1.6x and 2.9x behind. On a short prompt the gap is much larger, because closed pays a big fixed cost per request that we do not. That fixed cost moved by half between two runs on the same box, so I would not read a precise crossover length out of it.

The biggest remaining piece named here, host attention, is on the NPU now. What replaced it is smaller and different in kind: about a tenth of prefill is dispatches running above their own measured floor, and two thirds of that is the route leaving and re-entering the GEMM hardware context around the attention dispatches on the ten full-attention layers. Hoisting those out of the per-block loop is worth about 0.66 s of a 19 s prefill and needs no kernel work.

Writing tokens is untouched and the arithmetic here needs redoing. Decode is still about 127 ms per token at a thousand tokens of context, which is what this issue recorded. But closed measured 14.5 to 14.8 tokens per second this afternoon, against the 12 in the table above, so the conclusion that fixing the slow linear-attention step would put us past closed no longer follows. It would land near parity at best. Decode has had no work since 13 September, and it is now the whole of the gap.

Prefill work is in #107. One thing that came out of running it through oflm serve for the first time is its own issue, #106: the server cannot apply the OpenMP wait policy the engine asks for, which costs a third of its prefill.

Cyronius avatar Sep 21 '26 20:09 Cyronius

Decode is closed. The open kernels now write tokens as fast as the closed engine on the 35B, and faster on short contexts.

Both engines measured back to back through oflm serve, same session, container and prompts. Writing speed in tokens per second:

context closed open, before open, now
182 tokens 11.1 7.4 13.1
1,122 tokens 11.7 6.9 11.8
2,593 tokens 11.4 6.1 11.2

Closed reads slower here than in my last comment. Its own decode rate moves by a quarter or more between days on this box, which is why I only compare runs from the same session. Reading a prompt is unchanged and still ahead of closed at every length.

Most of the gap was scheduling, not arithmetic. Five PRs, each standing on its own except the last:

  • #113: the linear-attention layer queued its transfers one core at a time, so the eight cores waited on each other. Fixed, bit-exact.
  • #116: the two layer types lived in two xclbins, and switching cost about 1 ms twenty times a token. They now share one. Stacked on #113 and #115.
  • #115: attention, the only part that grew with context, now reads four cached rows per call. Not bit-exact against the old kernel; greedy output matches for 48 tokens and then flips on a near-tie.
  • #114: keeping the next layer's first dispatch queued is worth about 3 ms more.
  • #112: a bug on main today. After a layer's first dispatch, the host reads back which experts the router picked. Occasionally that read returned the previous token's picks, so the layer ran the wrong experts and nothing errored. It came in bursts: a third of repeated runs in one half hour, then none for hours. The fix makes a stale read wait or fail loudly instead.

One unrelated thing I found on the way: the 8-bit 35B fine-tunes (Ornith, Aquila) don't build their linear layer on main; they overflow program memory by 224 bytes.

Two questions for you, also asked in the PRs: is the merged image right as the default for this family, and should submit-ahead stay on, given that it relies on an execution order XRT doesn't document?

Cyronius avatar Sep 24 '26 12:09 Cyronius

we need to figure out if all this performance work goes in the first release or not.

Cyronius avatar Sep 24 '26 12:09 Cyronius

remaining unmerged prs https://github.com/Atomic-Germ/OpenFlowLM-Next/pull/115 and https://github.com/Atomic-Germ/OpenFlowLM-Next/pull/116 can go in 0.2.0

Cyronius avatar Sep 27 '26 20:09 Cyronius

all the PRs mentioned have been merged, is there anything left to do for now?

I might have some work that might help, but I will wait for the settings PR to land first and then I can do a clean PR for some extra performance knobs.

abondis avatar Oct 21 '26 14:10 abondis