Skip to content

opencl : fix Q8 tensor readback from generic buffers#164

Draft
gianni-cor wants to merge 5 commits into
temp-9341from
fix-opencl-q8-tensor-readback
Draft

opencl : fix Q8 tensor readback from generic buffers#164
gianni-cor wants to merge 5 commits into
temp-9341from
fix-opencl-q8-tensor-readback

Conversation

@gianni-cor

@gianni-cor gianni-cor commented Jun 19, 2026

Copy link
Copy Markdown

Summary

Fixes OpenCL tensor readback for quantized tensor types when the tensor is backed by the generic OpenCL buffer layout instead of the SOA quantized-weight layout.

The crash happened because ggml_backend_opencl_buffer_get_tensor() selected Q4/Q5/Q6/Q8 restore kernels based only on tensor->type. KV-cache tensors with cache-type-k=q8_0 can use the generic OpenCL tensor extra, so the readback path could mis-cast that extra as a quantized SOA extra and pass invalid OpenCL handles to the restore kernel.

This change gates each quantized restore path on whether tensor->extra belongs to the matching SOA extra pool. Generic tensors now fall through to the normal data_device readback path.

Validation

  • Rebuilt ggml-opencl, llama-completion, and test-backend-ops on Android.
  • Verified a standalone q8 OpenCL generic-buffer readback repro fails before the fix and succeeds after it.
  • Verified a GPU q8 prompt-cache run completes on GPUOpenCL: QUALCOMM Adreno(TM) 830.

Assisted-by: GPT-5.5
Co-authored-by: Cursor <cursoragent@cursor.com>
@gianni-cor gianni-cor requested a review from a team as a code owner June 19, 2026 16:14
gianni-cor and others added 4 commits June 19, 2026 20:12
Assisted-by: GPT-5.5
Co-authored-by: Cursor <cursoragent@cursor.com>
Mixed or unknown Q8_0 layouts now produce a clear unsupported-layout error instead of an assertion.

Co-authored-by: Cursor <cursoragent@cursor.com>
…outs. The SOA path copies the split q and d buffers linearly, which is only correct when those buffers are still in logical block order.

On Adreno, Q8_0 SOA weights may be transposed in-place after upload for matmul performance. Once that happens, tensor view_offs no longer maps directly to a linear physical offset in the transposed q and d buffers. A view or shape-changing copy could therefore copy the wrong blocks.

This change tracks whether a Q8_0 SOA extra was Adreno-transposed and rejects unsafe transposed SOA copies. Transposed SOA copies remain allowed only for full, non-view, same-shape tensors, where copying the raw physical representation is still valid.

Co-authored-by: Cursor <cursoragent@cursor.com>
…uristic, but cache tensors are mutable and participate in view copies during cache shifting. Transposing their SOA q/d buffers makes view_offs unsafe and caused OpenCL scheduling to reject the preallocated cache view copy.

This change limits the Adreno Q8_0 transpose to weight buffers only, and makes readback/matmul dispatch use the recorded adreno_transposed flag rather than re-running the shape heuristic.

Co-authored-by: Cursor <cursoragent@cursor.com>
@gianni-cor gianni-cor marked this pull request as draft June 20, 2026 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants