koboldcpp/docs
meatposes d6b61ac0d3
sycl: fix use-after-return of the SDPA scale in the oneDNN flash-attention path (#25880)
* sycl: fix use-after-return of the SDPA scale in the oneDNN flash-attention path

The scale was uploaded with an async memcpy sourced from a stack local. On the
in-order queue that copy is ordered behind the K/V staging kernels; once n_kv is
large enough (>= ~26k observed on Arc Pro B70) the staging outlives the host
stack frame and the copy reads recycled memory, feeding the SDPA a garbage scale.
Output then collapses to a single repeated token and the KV cache is poisoned
for the rest of the session.

Short contexts win the race by accident, and test-backend-ops caps
FLASH_ATTN_EXT at kv=1024, which is why CI never caught it. The previous
device_count > 1 wait_and_throw() gate (and reverting it, PR #25741) fixes the
symptom only by keeping the frame alive across the copy at the cost of a host
sync on every FA call.

Fix: cache one device scalar per (device, value) -- the scale is constant per
model -- and upload it synchronously once. The single-device fast path (no
per-call host sync) is then safe: every device-side hazard already serializes
on the in-order queue. The multi-GPU conservative wait is kept unchanged.

Also:
- GGML_SYCL_FA_ONEDNN_MAX_KV env (0 = unlimited): optional n_kv ceiling that
  routes very long sequences to the native FA kernel.
- test-backend-ops: FLASH_ATTN_EXT F16 cases up to kv=65536 (Qwen3.6-27B
  geometry hsk=hsv=256 GQA 6, and hsk=128 GQA 4), closing the kv=1024 blind
  spot. Note the race itself needs a live multi-op pipeline to reproduce;
  single-op runs pass even on broken builds.

Verified on Arc Pro B70 (bmg_g31), Qwen3.6-27B Q4_K, -c 131072: output
byte-identical at temp 0 to the native FA path through 32k-deep prefill, with
prefill depth-flat at 820-840 t/s (vs 340-350 native at 32k depth).

Assisted-by: Claude Fable 5

* sycl: handle GGML_SYCL_FA_ONEDNN_MAX_KV like the other runtime env vars and document it

Review feedback on #25880:
- read the variable once at backend init into g_ggml_sycl_fa_onednn_max_kv via
  ggml_sycl_get_env, and print it in the startup env listing (-lv 4 shows it)
- document GGML_SYCL_FA_ONEDNN and GGML_SYCL_FA_ONEDNN_MAX_KV in the SYCL.md
  runtime table

Also trim the added FLASH_ATTN_EXT cases to kv={4096,16384}: the 32768/65536
shapes exceed the legacy NMSE threshold on both the oneDNN and native kernels
(long-sequence fp16 accumulation drift, present before this PR) and would fail
CI for an unrelated reason.

Assisted-by: Claude Fable 5

* sycl: clarify GGML_SYCL_FA_ONEDNN_MAX_KV default is disabled

Assisted-by: Claude Fable 5

* sycl: state default behavior of GGML_SYCL_FA_ONEDNN_MAX_KV explicitly

Assisted-by: Claude Fable 5

* Update ggml/src/ggml-sycl/fattn-onednn.cpp

Co-authored-by: Neo Zhang <zhang.jianyu@outlook.com>

* sycl: write the SDPA scale from a kernel instead of caching it

The per-(device, value) scale cache was a function-local static
unordered_map with no synchronization, so concurrent backend instances
could access and rehash it at the same time.

Write the scalar with a single_task instead. The value is captured into
the command, so no host memory has to outlive the call -- which is what
the use-after-return fix needed in the first place. That removes the
shared container, the leaked device allocation and the string key, and
it also closes the remaining async-memcpy-from-a-stack-local on the
first flash-attention call.

Ordering does not rely on timing: the queue is created with
sycl::property::queue::in_order and the dnnl stream wraps that same
queue, so the write completes before the SDPA reads the scalar. The
multi-GPU wait_and_throw() branch is unchanged.

Also drop the <cstdlib> include, which is unused.

Assisted-by: Claude Opus 5

---------

Co-authored-by: Neo Zhang <zhang.jianyu@outlook.com>
2026-07-28 11:37:25 +03:00
..
android android: fix missing screenshots for Android.md (#18156) 2025-12-19 09:32:04 +02:00
backend sycl: fix use-after-return of the SDPA scale in the oneDNN flash-attention path (#25880) 2026-07-28 11:37:25 +03:00
development docs: add exception about weight folding (#26168) 2026-07-27 12:00:56 +02:00
multimodal docs : fix duplicated "the" in granitevision and model-conversion docs (#23767) 2026-05-27 09:34:06 +02:00
ops webgpu : add CONV_2D_DW (depthwise conv2d) kernel (#25847) 2026-07-22 17:24:44 +09:00
android.md docs/android.md: Add dependency libandroid-spawn for building in termux (#21812) 2026-06-22 05:48:31 +02:00
autoparser.md common : fix env names to all have LLAMA_ARG_ prefix (#23778) 2026-05-27 14:52:47 +03:00
build-riscv64-spacemit.md ci : update spacemit toolchain url and enhance curl command (#23642) 2026-05-25 10:43:24 +02:00
build-s390x.md docs: update s390x build docs (#19643) 2026-02-16 00:33:34 +08:00
build.md HIP: remove rocWMMA FlashAttention (#26046) 2026-07-24 17:53:54 +02:00
docker.md sycl : Improve SYCL doc (#23025) 2026-06-04 08:02:54 +03:00
function-calling.md docs: Update documentation with Granite 4.0/4.1 (#23404) 2026-05-22 20:35:46 +08:00
install.md docs: Add instructions to install llama.cpp from conda-forge (#22219) 2026-06-15 17:12:25 +02:00
llguidance.md llguidance build fixes for Windows (#11664) 2025-02-14 12:46:08 -08:00
multi-gpu.md Write a readme on Multi-GPU usage in llama.cpp (#22729) 2026-05-07 17:48:40 +02:00
multimodal.md mtmd: refactor preprocessor, add mtmd_image_preproc_out (#24736) 2026-06-18 12:04:39 +02:00
ops.md webgpu : add CONV_2D_DW (depthwise conv2d) kernel (#25847) 2026-07-22 17:24:44 +09:00
preset.md server: (router) rework -hf preset repo (#24739) 2026-06-18 12:45:23 +02:00
speculative.md spec : add DFlash support (#22105) 2026-06-28 16:01:34 +03:00