qxmx — Fused FlashAttention for prefill: problem statement + history #
STATUS (updated 2026-07-19): SOLVED. Path A (
sycl::joint_matrix) landed in commitb375466(M1.7c). Prefill 616 → 741 tok/s (+20%) at the new default chunk=512, all gates green (fa_kernel_testALL PASS,attn_forward_batch_test5/5, greedy ' Paris', logits mean|diff|=0.1082). See memoryqxmx_m1_7c_fused_fa_landed.mdfor the shipped design +qxmx_joint_matrix_b70.mdfor the B70 tile-support probe. The doc below is the original problem statement as written BEFORE the attempt; kept as the record of what was known/tried and as the brief that informed the successful approach.Key finding the doc anticipated but didn't know: B70's
matrix_combinationsquery OVER-PROMISES. Only fp16/bf16 16×16×16 SG16 tiles actually JIT; tf32 (m=0 n=16 k=8) and fp16 32×32×64 are listed but JIT-fail ("undefined builtin" / "not supported"). The shipped kernel uses fp16 16×16×16, NOT the tf32 the doc's Path A speculated about. A second finding: K/V dequant was 76% of v1 kernel time (48× WG redundancy) — hoisting it to a per-chunk global fp16 shadow was the bigger win than the DPAS fusion itself.
Status: open problem, hard, blocked once. This document is a
self-contained brief for an agent asked to attempt it fresh.
Target: Intel Arc Pro B70 (Xe2/Battlemage, 256 EU, 128 KB SLM/WG,
sub_group sizes {16, 32}), oneAPI 2026.x icpx -fsycl.
Goal: replace the current 3-kernel FA split with a fused tiled kernel
that keeps S/P in registers and reaches parity with llama.cpp's FA on the
same hardware. Measured prize: prefill 616 → ~900+ tok/s (llama-bench
pp4096 ub=2048 hits 980 tok/s on this card with the same model).
1. The gap, precisely #
Prefill on the 5289-token bench/session_4mb.txt prompt, naive DeltaNet
(the shipped SSM path), default chunk=256:
- ours: 616 tok/s (8.6 s), attn 24.7 ms/chunk-1, growing to 172 ms by chunk 20 (attn is 9% of chunk-1, ~42% by chunk-20).
- llama-bench (Ternary-Bonsai-27B Q2_0, same B70): ub=256 → 630 tok/s,
ub=2048 → 980 tok/s (plateau).
-fa auto(their FA path is on).
At matched ub=256 we are within ~2% of llama.cpp. The 37% gap to 980 is llama.cpp running ub=2048, where their FA scales and ours does not.
Why we don't scale with chunk (per-phase profile, 2026-07-19) #
Per-token attention cost at chunk-1 (no prior context — pure in-chunk causal work) grows with chunk size:
| chunk | attn/tok (chunk 1) | total tok/s |
|---|---|---|
| 256 | 0.096 ms | 616 |
| 512 | 0.121 ms | 623 |
| 1024 | 0.183 ms | 607 |
| 2048 | 0.305 ms | 578 |
| 4096 | 0.550 ms | 522 |
Bigger chunk amortizes per-tile launch overhead (good — FFN drops 8% per-token from 256→512) but the in-chunk O(N²) causal attention work dominates and gets worse per-token. Attention is the wall that blocks chunk-size scaling.
The structural reason: our FA is a per-chunk re-FA, not a tiled pass #
gpu_flash_attn (src/qxmx_fa.cpp:299) runs, per chunk, a K-tile loop:
int64_t total = pos_start + n_tok; // walks 0..pos_start+n_tok-1
int n_tiles = (total + TILE_T - 1) / TILE_T;
for (int ti = 0; ti < n_tiles; ti++) { // rescans prior context EVERY chunk
fa_dequant_kt(q, kv_k, t0, tile_len, ws);
fa_qk_gemm(q, n_tok, ws); // small GEMM: M=n_tok, K=HD, N=TILE_T
fa_softmax_merge(q, pos_start, t0, tile_len, n_tok, scale, ws);
fa_dequant_vt(q, kv_v, t0, tile_len, ws);
fa_pv_gemm(q, n_tok, ws); // small GEMM: M=n_tok, K=TILE_T, N=HD
}
fa_finalize(q, out, n_tok, ws);
Per K-tile that's 6 kernel launches, ~16 tiles/chunk at chunk=256 →
~96 launches/chunk. Each QK/PV GEMM is small (M=256, K=256, N=128,
M=256, K=128, N=256) and runs through gemm_tf32_batched at 4.2 TFLOPs
(see qxmx_gemm_tf32.cpp) — 3% of the 133 T-MAC/s Xe2 ceiling. S and P
are materialized to global memory between the QK and PV GEMMs (~3 MB per
tile at CHUNK=256).
A fused FA-2 kernel (one WG per (q-head, Q-tile)) keeps Q in registers, streams K/V through SLM, holds S/P in registers, and writes only the final O. No per-tile launch overhead, no S/P materialization, the QK/PV compute is the same DPAS work but issued by one WG with full occupancy. That is what llama.cpp's FA does, and it is why they scale to ub=2048.
Important: incremental FA (carrying (m,l,O) across chunks) is NOT the answer #
A no-rescan probe (QXMX_FA_NO_RESCAN, skip prior-context tiles, OUTPUT
WRONG) showed chunk-20 attn 172→24 ms and prefill 616→754 tok/s. It is
tempting to conclude "ship incremental FA carrying (m,l,O) state for +22%."
That conclusion is wrong. The prior-context QK/PV GEMMs are necessary per-query work (each new query must attend to prior keys — causal attention). The no-rescan probe skipped necessary work, not just redundant work. The 754 figure is an unachievable ceiling.
Reconciliation: for 2048 tokens, llama.cpp one-pass ub=2048 does ~2.1M QK dots/query-head; our 8× chunk=256 over growing context does ~2.36M. We do less total work than llama.cpp (we only scan the growing prefix, not the full context every time), yet they are faster. The gap is per-op efficiency (launch overhead + small-tile GEMM + S/P materialization), not algorithm. Do not propose incremental FA as the fix. The fix is fusion.
2. What we tried (the history — read before re-attempting) #
The full git log: git log --oneline | grep -E "M1.7b". Key commits:
M1.7b.1 (bcef29a) — fused FlashAttention-2 kernel, plain SYCL, scalar QK #
Aila-style: one WG per (q-head, query-token), LWS=128. K walked in
TILE_T=128 blocks. Scalar for d+=8 unrolled QK dot (no DPAS).
Online-softmax via SLM merge_state; reduce_over_group for tile_max/sum;
sycl::native::exp. T13 rotated-domain V (accumulate P·V_rotated_scaled,
butterfly once per output row at the end).
Correct. Greedy gate passed. But the scalar HD=256 dot capped prefill at ~230 tok/s. No DPAS = no XMX throughput. (M1.7b.6 shipped this as the fallback; 230 tok/s — slower than the 3-kernel split that replaced it.)
M1.7b.4 (7303d84) — ESIMD-vectorized FA (no DPAS) #
Rewrote the M1.7b.1 scalar K-dot with ESIMD vector ops. 2.35× faster attn but still no DPAS. Hit the ESIMD constraints hard:
- 64-lane cap per WG (ESIMD LWS ≤ 64).
- No
sycl::exp/reduce_over_group/ sub_group shuffles — ESIMD and plain-SYCL subgroup ops are mutually exclusive in the same kernel. - Register pressure: 8×256 float O accumulator = 8 KB = a full GRF.
M1.7b.5 — inline-asm DPAS in plain SYCL (THE BLOCKED ATTEMPT) #
The idea: crib just the DPAS inline-asm invocation from sycl-tla
(include/cute/arch/mma_xe.hpp, XE_DPAS_TT), with NO cute dependency,
so a plain sycl::parallel_for kernel can issue DPAS without
[[intel::sycl_explicit_simd]]. That would let the same kernel also use
sycl::exp / reduce_over_group / sub_group shuffles (which ESIMD
forbids) — exactly what a fused FA needs (DPAS for QK/PV, subgroup ops
for softmax).
Code: src/qxmx_dpas.h (deleted in ee09d1c cleanup; recover with
git show ee09d1c^:src/qxmx_dpas.h). Wrapper:
struct dpas_tf32_m1 {
static constexpr int M = 1, N = 16, K = 8;
using AV = dpas_vec<uint32_t, 128>; /* GRF, M*K=8 tf32 padded */
using BV = dpas_vec<uint32_t, 8>; /* K=8 tf32 */
using DV = dpas_vec<float, 16>; /* M*N=16 fp32 */
static void fma(DV& d, const AV& a, const BV& b) {
DV dl = d;
#ifdef __SYCL_DEVICE_ONLY__
asm("{\n"
".decl DST v_type=G type=f num_elts=16 alias=<%0,0>\n"
".decl SRC1_UD v_type=G type=UD num_elts=8 alias=<%1,0>\n"
".decl SRC2_UD v_type=G type=UD num_elts=128 alias=<%2,0>\n"
"dpas.tf.tf.8.1 (M1, 16) DST.0 DST.0 SRC1_UD.0 SRC2_UD(0,0)\n"
"}\n"
: "+rw"(dl) : "rw"(b), "rw"(a));
#endif
d = dl;
}
};
dpas_vec<T,N> is T __attribute__((ext_vector_type(N))) — OpenCL-style
vector that lowers to a GRF region for inline asm (this is what sycl-tla's
vector_t expands to in device code; sycl::marray hits "impossible
constraint: cannot store value into a register" for the 128-element A/D
operands).
Probe: tests/dpas_asm_probe.cpp (also deleted; git show ee09d1c^).
Validates qxmx_dpas.h::dpas_tf32_m1::fma against a CPU fp32 reference in a
sycl::single_task.
THE FAILURE (reproduced 2026-07-19, exact output):
Build program log for 'Intel(R) Graphics [0xe223]':
error: parsing vISA inline assembly failed:
near line 775: syntax error, unexpected IDENT
error: backend compiler failed build.
The vISA JIT rejects the asm dialect. Host compilation (icpx -fsycl)
succeeds — the #ifdef __SYCL_DEVICE_ONLY__ block is hidden on host,
so the failure is only at device-JIT time. This is the wall.
M1.7b.6 (5b1a490) — plain-SYCL scalar FA (fallback, shipped briefly) #
Aila-style, one WG per (q-head, query-token), LWS=128, scalar QK dot (no DPAS). 230 tok/s — slower than the 3-kernel split (439 at the time, 616 now). Fusion without DPAS is a loss: the scalar HD=256 dot is too slow.
M1.7b.7 (d9fd3df) — 3-kernel split (shipped, current path) #
Decomposed FA into per-K-tile pipeline: dequant K → QK GEMM
(gemm_tf32_batched, DPAS) → softmax merge → dequant V → PV GEMM
(gemm_tf32_batched) → finalize. 439 tok/s then, 616 now (after the
naive DeltaNet + GEMM tuning wins). This is the current code in
src/qxmx_fa.cpp. It works, it's correct, it uses DPAS — but at the cost
of per-tile launch overhead and S/P materialization that a fused kernel
would avoid.
3. The working DPAS primitive (use this) #
The DPAS path that DOES work is xmx::dpas from ESIMD. See
src/qxmx_gemm_tf32.cpp:84:
q.parallel_for(sycl::nd_range<1>(...),
[=](sycl::nd_item<1> it) [[intel::sycl_explicit_simd]] {
esimd::simd<float, DPAS_M*DPAS_N> acc(0.f);
for (int k = 0; k < K8; k++) {
esimd::simd<tf32, K_STEP*DPAS_N> b; /* load */
esimd::simd<tf32, DPAS_M*K_STEP> a; /* load */
acc = xmx::dpas<SD, DPAS_M, float>(acc, b, a);
}
/* epilogue: write acc */
});
xmx::dpas<SD=8, DPAS_M, float>(acc, b, a)— ESIMD DPAS, tf32 operands.- Requires
[[intel::sycl_explicit_simd]]on the kernel. - This runs at 4.2 TFLOPs in
gemm_tf32_batched(small GEMM, M=256); the M1.2 spikegemm_gpu_v4(ternary u2 DPAS) reaches 52-71 TFLOPs. - The constraint that blocks fusion: an ESIMD kernel cannot use
sycl::exp,reduce_over_group, or sub_group shuffles. So a fused FA that wants DPAS for QK/PV and subgroup softmax in the same kernel is impossible via this path. That is exactly why M1.7b.5 tried inline-asm DPAS in plain SYCL — to escape the ESIMD restriction.
gemm_gpu_v4 (src/qxmx_gemm.cpp) is the ternary u2 DPAS path for the
FFN/attn projections (weight GEMMs). It is irrelevant to FA's QK/PV which
are fp32×fp32 (tf32) activations, not ternary weights.
4. What to try (the unexplored paths, ranked) #
Path A — sycl::joint_matrix (plain SYCL DPAS, the untried middle) #
sycl::joint_matrix<sycl::sub_group, T, use::a, TM, TK, layout::row_major>
is the SYCL 2020 standard matrix-multiply API. It issues DPAS without
[[intel::sycl_explicit_simd]] and without inline asm — plain SYCL,
works in the same kernel as sycl::exp, reduce_over_group, sub_group
shuffles. This is exactly the property M1.7b.5 was reaching for via inline
asm.
Reference implementations in the tree:
src/aila/src/ops/AttentionOps.cpp—attention_decode_joint_matrix_tiled(lines 323-455) usesjoint_matrix<sycl::sub_group, bf16, use::a, ...>joint_matrix_mad. Read this first; it is a working DPAS attention path in plain SYCL on this exact hardware family.
src/sycl-tla/applications/flash_attention_v2/— Intel's BMG-tuned FA-2. Seekernel/legacy/xe_flash_attn_prefill.hppandcollective/legacy/xe_flash_attn_prefill_mma.hpp. This is the reference FA-2 prefill kernel for Battlemage. Theexamples/06_bmg_flash_attention/directory has runnable harnesses (not currently built;06_xe_fmha_fwd.cppis the prefill entry).
The plan: write a fused FA kernel using joint_matrix for QK and PV,
with softmax as plain-SYCL subgroup ops in the same kernel. One WG per
(q-head, Q-tile). Tile sizes to crib from sycl-tla's BMG FA config (see
build-bmg/examples/06_bmg_flash_attention/ ctest names for the hdim
variants). Validate against the M1.7b.7 3-kernel split as the correctness
oracle (it is already in tree, well-tested: fa_kernel_test 15/15,
attn_forward_batch_test 5/5, greedy ' Paris' unchanged).
Why this is the best first try: it is the path M1.7b.5 was trying to synth via inline asm, now available as a supported API. aila + sycl-tla give concrete crib-able reference code. The only reason it wasn't tried before is that M1.7b.5 went down the inline-asm dead end first.
Risk: joint_matrix tile support is device-dependent. aila's code
falls back to a baseline kernel when "no compatible joint_matrix tile on
this device" (see AttentionOps.cpp:567-598). Verify the B70 (device 0xE223)
exposes a compatible tf32 or bf16 tile for the shapes we need (TM×TK×TN).
The aila attention_decode_joint_matrix path runs with TM=32, TK=32,
TN=16, SG=16 — check those work on B70 before committing to a tile config.
Path B — ESIMD DPAS with an ESIMD-native softmax #
Stay in ESIMD (the working xmx::dpas path) but replace the forbidden
sycl::exp/reduce_over_group with ESIMD equivalents:
esimd::mathhasexp/log2(ESIMD-native, nosycl::expneeded).- Subgroup reductions in ESIMD:
esimd::reduce+esimd::merge+ lane shuffles viaesimd::shuffle(cf. sycl-tlainclude/cute/atom/...). - Online softmax is per-row (one row of S per lane), so the "reduce" is
within a subgroup, not across the WG — ESIMD
reduce_over_sub_groupif it exists, or a manual 5-step shuffle tree.
Reference: sycl-tla's non-legacy FA kernels
(applications/flash_attention_v2/collective/) are ESIMD + ESIMD-native
softmax. This is the harder path (more ESIMD idiom to learn) but avoids
the joint_matrix device-tile-support question.
Path C — fix the inline-asm dialect #
The M1.7b.5 asm failed with "parsing vISA inline assembly failed: syntax
error, unexpected IDENT" at line 775. The likely cause: the asm dialect
in qxmx_dpas.h is from an older IGC/vISA; oneAPI 2026.x's vISA may have
changed the syntax (or the .decl/dpas token forms). To revive this
path: find a working inline-asm DPAS example that compiles+JITs on
oneAPI 2026.x — search intel/llvm sycl_ext_intel_esimd.md DPAS API
section and the IGC DPAS.md for the current vISA dialect. Compare
verbatim against qxmx_dpas.h's asm. This is the riskiest path (vISA
asm is brittle, undocumented surface, easy to get wrong); only attempt
if A and B both fail.
5. Constraints + gates (do not violate) #
Hard constraints (architecture, see qxmx_sycl_gotchas.md) #
- In-order
sycl::queue. Never.wait()between device kernels except the final prefill sync and thephase()profiling waits. - USM shared for GPU-RMW buffers; malloc_device for GPU-only. The
existing
fa_split_workspace(src/qxmx_gpu.h:42) is allmalloc_shared. - No per-call malloc. Workspace is an engine member, init-allocated
for CHUNK + TILE_T (see
fa_split_allocin qxmx_gpu.h:55). Any new workspace goes there. - M must be a multiple of 16 (gemm_gpu_v4 N-tile constraint; engine chunks are multiples of 16, with a <16 tail falling back to decode).
Correctness gate (unchanged from M1.7) #
fa_kernel_test15/15 (tests/qxmx_fa... — actually the current test isattn_forward_batch_test+fa_kernel_test; check meson.build).- Greedy output unchanged:
qxmx_diff <model> "The capital of France is"→ ' Paris' (mean|diff| = 0.108 baseline, unchanged). - Reassociation drift OK in the GEMM epilogue; greedy tokens must match.
- Causal: query at pos
qposattends only to keys<= qpos. - T13 rotated-domain V composition (validated in
fa_t13_test): accumulate P·V_rotated_scaled, butterfly once per output row, ×NRM ×vnorm_corr at the end. Do NOT butterfly per V-block. See memoryqxmx_m1_7b_flash_attention.mdfor the T13 math.
Perf gate #
- Prefill ≥ 700 tok/s on
bench/session_4mb.txt(5289 tokens). Current is 616; the 3-kernel split's per-tile overhead is the ~100 ms/chunk we're chasing. A fused kernel that matches llama.cpp's per-tile efficiency should clear 700 and ideally reach toward 900. - Attn must NOT grow with context at fixed chunk (the rescan symptom). At chunk=256, chunk-20 attn should be within ~2× of chunk-1 attn (currently 7× — 172 vs 25 ms). This is the structural test.
- Cross-check microbench against in-engine
QXMX_PROFILE=1 QXMX_FFN_DEBUG=1numbers. Never benchmark with trivial-zero data (see memoryqxmx_gemm_value_dependence.md— a zero A+B+1.0-scales GEMM runs 65% faster than real values on Xe2 due to FMA dependency-chain stalls).
Do not waste time on #
- Incremental FA carrying (m,l,O) state across chunks. The prior-context
QK/PV GEMMs are necessary per-query work; carrying state doesn't avoid
them. The no-rescan probe's 754 tok/s is an unachievable ceiling. See
§1.3 above + memory
qxmx_m1_7b_flash_attention.md. - Chunk size >256 as a primary lever. Attention's in-chunk O(N²) grows faster than FFN's amortization win. Chunk size only helps AFTER fusion lands (a fused FA scales because the per-tile launch overhead is gone).
- Split-K / A-load prefetch on the FFN GEMMs. Both measured NO-GO
(2026-07-19,
gemm_sk_test). FFN is at its kernel-design ceiling; not the lever. See memoryqxmx_gemm_kernel_facts.md. - Re-doing the M1.7b.5 inline-asm approach verbatim. It failed at JIT
with a specific vISA syntax error. If attempting Path C, first identify
what changed in the vISA dialect — do not copy
qxmx_dpas.hand expect it to work.
6. Reference file map (all in /home/clee/src/qxmx unless noted) #
| what | path |
|---|---|
| current FA (3-kernel split, shipped) | src/qxmx_fa.cpp, src/qxmx_fa.h |
| FA workspace (init-allocated) | src/qxmx_gpu.h:42-73 |
| FA call site (attn_forward_b) | src/qxmx_gpu.cpp:771-815 (grep gpu_flash_attn) |
| engine prefill chunk loop | src/qxmx_gpu.cpp:1240-1280 (engine::prefill, run_chunk_b) |
| working tf32 DPAS primitive | src/qxmx_gemm_tf32.cpp (gemm_tf32_batched, xmx::dpas) |
| working ternary u2 DPAS (FFN) | src/qxmx_gemm.cpp (gemm_gpu_v4, gemm_gpu_v4_dual) |
| M1.7b.6 plain-SYCL scalar FA (fallback) | git show 5b1a490:src/qxmx_fa.cpp |
| M1.7b.5 inline-asm DPAS (BLOCKED) | git show ee09d1c^:src/qxmx_dpas.h, tests/dpas_asm_probe.cpp |
| M1.7b.1 fused scalar FA (original) | git show bcef29a:qxmx_fa.cpp (pre-src/ reorg) |
| aila joint_matrix attention reference | /home/clee/src/aila/src/ops/AttentionOps.cpp:323-455 |
| sycl-tla BMG FA-2 reference | /home/clee/src/sycl-tla/applications/flash_attention_v2/ |
| sycl-tla FA runnable harness | /home/clee/src/sycl-tla/examples/06_bmg_flash_attention/06_xe_fmha_fwd.cpp |
| KV cache layout (K q8_0/fp8/fp16, V TurboQuant 4-bit WHT) | src/qxmx_gpu.h:75+ (grep KROWBYTES, VROW_U32, VBLK) |
| model dims | src/qxmx.h (D_MODEL 5120, N_HEAD 24, N_HEAD_KV 4, HEAD_DIM 256, ROT_DIM 64) |
| FA test harnesses | tests/attn_forward_batch_test.cpp, tests/fa_kernel_test.cpp (meson targets) |
Memory files (in ~/.config/maki/, project-scoped) #
qxmx_m1_7b_flash_attention.md— this problem's analysis + the no-rescan-probe-is-misleading finding.qxmx_gemm_kernel_facts.md— the FFN GEMM levers + the dead ones.qxmx_sycl_gotchas.md— in-order queue, USM kinds, the malloc_host RMW bug.qxmx_gemm_value_dependence.md— the trivial-zero-data microbench trap.qxmx_b70_hardware_facts.md— 256 EU, 128 KB SLM/WG, subgroups {16,32}.
Commands #
source /opt/intel/oneapi/setvars.sh >/dev/null 2>&1
meson setup --reconfigure build >/dev/null 2>&1 && meson compile -C build <target>
M=~/models/bonsai/Ternary-Bonsai-27B-Q2_g64.gguf
./build/qxmx_diff "$M" "The capital of France is" # decode gate (0.108, ' Paris')
QXMX_PROFILE=1 ./build/qxmx_run "$M" -f bench/session_4mb.txt -n 1 # prefill tok/s + phase breakdown
QXMX_PROFILE=1 QXMX_FFN_DEBUG=1 ./build/qxmx_run "$M" -f bench/session_4mb.txt -n 1 # + per-layer ffn
./build/attn_forward_batch_test # FA bit-exactness vs decode oracle (5/5)
icpx gotcha: for any ad-hoc icpx compile outside meson, pass
--gcc-install-dir=/usr/lib/gcc/x86_64-linux-gnu/14 (see
shell_gotchas.md). Inside meson it's handled by meson/icpx.ini.
7. The one-sentence summary #
The gap to llama.cpp is not algorithmic — it is that our FA is a 3-kernel
split with per-tile launch overhead and small-tile GEMM inefficiency,
while theirs is a fused kernel that keeps S/P in registers; the fix is a
fused tiled FA using sycl::joint_matrix (Path A, untried, with aila +
sycl-tla as reference) or ESIMD-native softmax (Path B), and the inline-asm
DPAS path (M1.7b.5, Path C) is blocked by a vISA JIT syntax error that
needs a fresh dialect investigation before it's worth re-attempting.