Repository navigation
metal : fix incorrect output on AMD discrete GPUs - #19567
Closed
ai-janitor wants to merge 1 commit into
Closed
ai-janitor wants to merge 1 commit into
ai-janitor wants to merge 1 commit into
Conversation
Disable concurrent compute encoders on non-UMA GPUs. AMD discrete GPUs produce garbage output with MTLDispatchTypeConcurrent because memory barriers (MTLBarrierScopeBuffers) don't properly synchronize between concurrent dispatches on these devices. Also fix NSMakeRange in memset_tensor — second arg is length, not end offset, which caused buffer overwrite on the private buffer path. Closes ggml-org#19563 Co-Authored-By: Claude Opus 4.6 <[email protected]>
Contributor
|
@ai-janitor please read the contributing guidelines https://github.com/ggml-org/llama.cpp/blob/master/CONTRIBUTING.md and please stop submitting PRs which are not going to be accepted. |
Open
6 tasks
This was referenced Nov 21, 2026
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.
Summary
Fixes Metal backend producing garbage output on AMD discrete GPUs (e.g. Radeon Pro 5300M). Two bugs fixed:
NSMakeRangebuffer overwrite inmemset_tensor— secondary bug on private buffer pathCloses #19563
Root Cause
The Metal backend enables concurrent compute command encoders by default (
MTLDispatchTypeConcurrent), relying onmemoryBarrierWithScope:MTLBarrierScopeBuffersto synchronize between dependent dispatches. This works on Apple Silicon but produces incorrect results on AMD discrete GPUs — the barriers don't properly synchronize memory on these devices.How we found it
Bisected by GPU layer count:
ngl=0(CPU): correctngl=1: correctngl=2: garbage starts — more layers = more concurrent dispatches = more stale readsngl=99, n=1: first token plausible (PP completes before read-back)ngl=99, n=2+: all garbage (@@@@@)This pattern pointed to an intra-command-buffer synchronization issue rather than buffer allocation, KV cache, or kernel correctness. Tested with
GGML_METAL_CONCURRENCY_DISABLE=1— output became correct immediately.The fix
Gate
use_concurrencyonhas_unified_memory(same pattern used foruse_shared_buffers). This cleanly separates Apple Silicon (UMA, concurrency works) from AMD discrete (non-UMA, concurrency broken). TheGGML_METAL_CONCURRENCY_DISABLEenv var still works as an override.Secondary fix:
memset_tensorNSMakeRangeNSMakeRange(location, length)was called asNSMakeRange(bid_dst.offs, bid_dst.offs + size)— second arg should besize, notoffset + size. Overwrites past intended range when tensor is at non-zero offset in a private buffer. Only affects the private buffer path (non-UMA GPUs).Test Results
Tested on AMD Radeon Pro 5300M, macOS 26.2, Qwen2.5-1.5B-Instruct Q4_K_M:
@@@@@garbageNo impact on Apple Silicon —
has_unified_memory = trueso behavior is unchanged.