Repository navigation
opencl: fix several bugs where the backend aborts - #27630
Merged
lhez merged 9 commits intoSep 12, 2026
Merged
Conversation
jeffyaw
added a commit
to YawLabs/llama.cpp
that referenced
this pull request
Sep 3, 2026
…rting Adopt upstream PR ggml-org#27630 (retry + CL_MEM_ALLOC_HOST_PTR fallback for set_tensor/get_tensor staging buffers and buffer-type allocs, device-gated CL 3.0 C-version query, zero-size ndrange skip, CONV_2D contiguity gate), with fork extensions: q1_0/bf16 set and q5_0/q5_1/bf16 get paths routed through the same helpers, terminal failures abort with tensor name, size, CL error and remedy on stderr, download-helper fallback made READ_WRITE (the q5_K restore kernel RMWs dst), q4_K/q5_K MoE get_tensor staging leak fixed, IQ4_NL outer staging alloc moved below the early-returning Adreno branch. Assisted-by: Claude Fable 5
wanghqc
marked this pull request as ready for review
September 5, 2026 19:43
max-krasnyansky
approved these changes
Sep 9, 2026
CL_DEVICE_OPENCL_C_ALL_VERSIONS is an OpenCL 3.0 device query, but get_opencl_c_version() gated it on the platform version. Qualcomm exposes a 3.0 platform with 2.0 devices, so on an Adreno 619 the query returns CL_INVALID_VALUE and CL_CHECK aborts during backend init. The backend was unusable on such devices, with no fallback. Gate the 3.0 query on the device version as well as the platform's, and fall through to CL_DEVICE_OPENCL_C_VERSION if it still fails instead of aborting. No behaviour change on 3.0 devices; they take the same branch as before. Verified on Adreno 619 and 642L, which aborted at init and now come up, and on Adreno 840 and 740, which are unchanged.
clEnqueueNDRangeKernel returns CL_INVALID_GLOBAL_WORK_SIZE if any global_work_size[i] is 0, and the CL_CHECK around it aborts the process. Most drivers accept a zero-size range and do nothing, so this stays invisible on them; strict drivers enforce the spec and take the process down. The spec violation is ours, not a device quirk. Skip the enqueue when any global dimension is zero, at the single choke point every dispatch funnels through. A dimension that is wrongly zero still surfaces as a wrong result rather than being masked. Of the 74 ops in test-backend-ops, SET_ROWS is the only one that emits a zero-size dispatch (a row-less tensor, ne01 == 0). Verified on Adreno 619 and 642L, which aborted there and now run; Adreno 840 and 740 never enter the path.
The OpenCL CONV_2D kernel walks both operands as if they were contiguous WHCN. A channel-most-contiguous (CWHN) input or kernel reaches the backend as a permuted, non-contiguous tensor with unchanged ne, so it is read with the wrong strides. supports_op accepted it on type alone, so instead of falling back the backend returned garbage. test-backend-ops -o CONV_2D on an Adreno 840: 2014/2026, with 12 cwhn=1 cases failing at ERR ~= 2.0 against a 5e-4 tolerance, e.g. CONV_2D(ne_input=[141,133,25,2],ne_kernel=[1,1,25,1],cwhn=1). Require contiguous src0/src1/dst, matching what the CUDA and Vulkan backends already do for this layout (ggml-org#25617). The affected shapes now report "not supported" and fall back instead of computing a wrong result. Adreno 840, test-backend-ops -o CONV_2D: 2014/2026 -> 2002/2002, 0 FAIL.
…ilure Adreno X1-85 driver reports max_mem_alloc ~1.99 GB but intermittently returns CL_OUT_OF_RESOURCES on the 834 MB temp upload buffer for Qwen3.5-9B output.weight Q6_K after the model layers fragment the heap. Three-step retry: normal alloc → clFinish + retry → CL_MEM_ALLOC_HOST_PTR with map(WRITE_INVALIDATE)+memcpy+unmap. ALLOC_HOST_PTR is the zero-copy host pool on Adreno (USE_HOST_PTR triggers an internal driver copy and shares the same device pool that's exhausted). Unblocks Qwen3.5-9B llama-bench without the -ot "output\.weight=CPU" workaround.
…r-type paths Extract ggml_cl_create_temp_upload_buffer (set_tensor) and ggml_cl_create_temp_download_buffer (get_tensor) helpers that wrap the 3-step retry: normal alloc -> clFinish+retry -> CL_MEM_ALLOC_HOST_PTR + map/unmap (upload) or direct (download). Apply across all 10 set_tensor quant types (Q4_0/Q4_1/Q5_0/Q5_1/MXFP4/Q8_0/IQ4_NL/Q4_K/ Q5_K/Q6_K) and 9 get_tensor paths. Add clFinish+retry + ALLOC_HOST_PTR fallback to ggml_backend_opencl_buffer_type_alloc_buffer with a WARN on the host-pinned path (compute scratch is hot during inference, so falling back has perf cost — better than failing to load though). Centralizes the X1-85 fragmentation workaround originally added inline for Q6_K in the previous commit; same pattern, broader coverage.
X1-85 driver caps OpenCL context at ~6 GB even when more global memory is reported free (clinfo shows 7991 MB total, 6967 MB free at start). Once aggregate allocations cross ~6 GB the driver returns CL_OUT_OF_RESOURCES for any further alloc regardless of size: traced Mistral-7B Q4_K_M f16 KV at d=16k allocates 4.07 GB model + 1.98 GB KV + 97.5 MB compute scratch fits, but the next 17 MB tail buffer fails. Existing retry+ALLOC_HOST_PTR fallback can't rescue this — host pool returns CL_OUT_OF_RESOURCES too once the device pool is exhausted. Upgrade the alloc-failed message from LOG_INFO to LOG_ERROR with the practical workarounds (reduce -ngl, lower -c/-ub, quantize KV).
lhez
force-pushed
the
hq/opencl-device-robustness-r0822
branch
from
September 9, 2026 20:17
7efe9da to
ce7e22e
Compare
lhez
force-pushed
the
hq/opencl-device-robustness-r0822
branch
from
September 9, 2026 20:26
f9c5d67 to
9af3202
Compare
max-krasnyansky
approved these changes
Sep 11, 2026
lhez
approved these changes
Sep 11, 2026
1 task done
quimmedes
pushed a commit
to quimmedes/cafe-llama.cpp
that referenced
this pull request
Sep 16, 2026
jeffyaw
added a commit
to YawLabs/llama.cpp
that referenced
this pull request
Sep 23, 2026
ggml-opencl.cpp was the only conflict, 21 hunks. The fork had already adopted upstream PR ggml-org#27630 by hand in 70a5566, so both sides carry that fix and git could not match them. Kept the fork side, which is a superset: the named retry helper, the OOM classifier, CL_INVALID_BUFFER_SIZE handling, aborts that name tensor and size, and the READ_WRITE flag the q5_K restore kernel needs. Took upstream on two cosmetic hunks. Assisted-by: Claude Opus 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR is to fix several bugs exposed on various Adreno GPUs to make the OpenCL backend more robust:
• CL_DEVICE_OPENCL_C_ALL_VERSIONS is an OpenCL 3.0 device query but was gated on the platform version. For some old Adreno devices, the SW reports OpenCL 3.0 platform with 2.0 devices, and in this case the query returns CL_INVALID_VALUE and the backend aborts during init. The fix is to gate on the device version too, and falls back to CL_DEVICE_OPENCL_C_VERSION instead of aborting. This issue may occur for some old Areno A6x devices.
• clEnqueueNDRangeKernel was called with a zero global work size. The spec says CL_INVALID_GLOBAL_WORK_SIZE; Some driver may abort the process. The fix is to skip the kernel if if any dimension is zero.
• CONV_2D accepted non‐contiguous operands and read them with the wrong strides, returning garbage rather than declining and letting the CPU take it. The fix is to fall back to CPU. Tested with Adreno 740 and 840: CONV_2D went from 2014 passing of 2026 to 2002 of 2002. The gate now declines 24 shapes; 12 of them had been returning wrong answers, and the other 12 happened to be correct anyway.
Also makes an allocation failure recoverable: set_tensor / get_tensor and the buffer‐type allocation retry and fall back to host‐pinned memory when the device pool is fragmented, and the per‐context memory wall now reports an actionable error instead of a bare failure.
Additional information
Requirements