Skip to content

metal : fix incorrect output on AMD discrete GPUs - #19567

Closed
ai-janitor wants to merge 1 commit into
ggml-org:masterfrom
ai-janitor:fix/metal-amd-correctness
Closed

ai-janitor wants to merge 1 commit into
ggml-org:masterfrom
ai-janitor:fix/metal-amd-correctness

Conversation

@ai-janitor

Copy link
Copy Markdown

Summary

Fixes Metal backend producing garbage output on AMD discrete GPUs (e.g. Radeon Pro 5300M). Two bugs fixed:

  1. Concurrent compute encoders break on non-UMA GPUs — root cause of garbage output
  2. NSMakeRange buffer overwrite in memset_tensor — secondary bug on private buffer path

Closes #19563

Root Cause

The Metal backend enables concurrent compute command encoders by default (MTLDispatchTypeConcurrent), relying on memoryBarrierWithScope:MTLBarrierScopeBuffers to 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): correct
  • ngl=1: correct
  • ngl=2: garbage starts — more layers = more concurrent dispatches = more stale reads
  • ngl=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_concurrency on has_unified_memory (same pattern used for use_shared_buffers). This cleanly separates Apple Silicon (UMA, concurrency works) from AMD discrete (non-UMA, concurrency broken). The GGML_METAL_CONCURRENCY_DISABLE env var still works as an override.

// before
res->use_concurrency = getenv("GGML_METAL_CONCURRENCY_DISABLE") == nil;

// after
res->use_concurrency = props_dev->has_unified_memory && getenv("GGML_METAL_CONCURRENCY_DISABLE") == nil;

Secondary fix: memset_tensor NSMakeRange

NSMakeRange(location, length) was called as NSMakeRange(bid_dst.offs, bid_dst.offs + size) — second arg should be size, not offset + 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:

Test Before After
ngl=99, "capital of France" @@@@@ garbage "Paris" ✓
ngl=99, haiku (50 tokens) garbage coherent output ✓
ngl=2 (failure boundary) garbage "Paris" ✓
Bench pp512/tg128 99/63 t/s 99/59 t/s (no meaningful regression)

No impact on Apple Silicon — has_unified_memory = true so behavior is unchanged.

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]>
@am17an

am17an commented Feb 12, 2026

Copy link
Copy Markdown
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Apple Metal https://en.wikipedia.org/wiki/Metal_(API) ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Metal backend produces garbage output on AMD discrete GPUs (Radeon Pro 5300M)

2 participants