-
Notifications
You must be signed in to change notification settings - Fork 364
fix(ios): guard Metal residency set with supportsResidencySets check #536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shubhamsinnh
wants to merge
12
commits into
RunanywhereAI:main
Choose a base branch
from
shubhamsinnh:fix/issue-480-metal-residency-crash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c6d8aec
Expose adaptive context APIs for Swift LLM
shubhamsinnh 25099a2
fix(llm): preserve adaptive context defaults and errors
shubhamsinnh 61651cb
docs(llm): document adaptive context entry points
shubhamsinnh 8cc1ffc
refactor(llm): DRY lifecycle wrappers with shared template
shubhamsinnh a58bfd1
fix: resolve C++ compilation errors in adaptive context PR
shubhamsinnh 4783629
fix(swift): honor custom thinking tags in adaptive context
shubhamsinnh df3862a
fix(swift): preserve adaptive context error codes
shubhamsinnh e4535bc
fix(llm): null-terminate extracted thinking slices
shubhamsinnh 30ce3ca
fix(swift): set adaptive-context streaming flag on options
shubhamsinnh 4ae4bd5
fix(ios): guard Metal residency set with supportsResidencySets check
shubhamsinnh 3c78846
fix(ios): use supportsFamily:MTLGPUFamilyApple7 in residency guard patch
shubhamsinnh dc71c7a
fix(ios): allow residency sets on Apple6 GPUs
shubhamsinnh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
45 changes: 45 additions & 0 deletions
45
engines/llamacpp/patches/001-metal-residency-set-guard.patch
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| From: RunAnywhere SDK <dev@runanywhere.ai> | ||
| Date: Tue, 8 Jul 2026 10:30:00 +0530 | ||
| Subject: [PATCH] ggml-metal: guard residency set with supportsFamily check | ||
|
|
||
| The @available(iOS 18.0, ...) check passes on A12/A13 devices (iPhone XS | ||
| Max, iPhone 11) because these devices run iOS 18, but their GPU does not | ||
| support MTLResidencySet. Calling newResidencySetWithDescriptor: on these | ||
| devices triggers a Metal debug assertion failure: | ||
|
|
||
| -[MTLDebugDevice newResidencySetWithDescriptor:error:]:2785: | ||
| failed assertion 'device does not support residency sets.' | ||
|
|
||
| Fix: also check [device supportsFamily:MTLGPUFamilyApple6] when | ||
| initializing the use_residency_sets flag in the device properties, so | ||
| the entire residency set code path is skipped on unsupported GPUs. | ||
| (There is no `-[MTLDevice supportsResidencySets]` API; supportsFamily: | ||
| is the real, documented way to query GPU-family-gated capabilities and | ||
| is the real runtime capability check for this GPU-family-gated feature.) | ||
|
|
||
| Residency sets require Apple GPU family 6+ (A13+). | ||
|
|
||
| Fixes: https://github.com/RunanywhereAI/runanywhere-sdks/issues/480 | ||
| --- | ||
| ggml/src/ggml-metal/ggml-metal-device.m | 7 +++++-- | ||
| 1 file changed, 5 insertions(+), 2 deletions(-) | ||
|
|
||
| diff --git a/ggml/src/ggml-metal/ggml-metal-device.m b/ggml/src/ggml-metal/ggml-metal-device.m | ||
| index 0000000..0000001 100644 | ||
| --- a/ggml/src/ggml-metal/ggml-metal-device.m | ||
| +++ b/ggml/src/ggml-metal/ggml-metal-device.m | ||
| @@ -824,9 +824,12 @@ static void ggml_metal_device_init(ggml_metal_device_t dev) { | ||
| } | ||
| } | ||
|
|
||
| - dev->props.use_residency_sets = true; | ||
| + dev->props.use_residency_sets = false; | ||
| #if defined(GGML_METAL_HAS_RESIDENCY_SETS) | ||
| - dev->props.use_residency_sets = getenv("GGML_METAL_NO_RESIDENCY") == nil; | ||
| + if ([dev->mtl_device supportsFamily:MTLGPUFamilyApple6]) { | ||
| + dev->props.use_residency_sets = getenv("GGML_METAL_NO_RESIDENCY") == nil; | ||
| + } | ||
| + // else: device GPU does not support residency sets (A12 and older), leave as false | ||
| #endif | ||
|
|
||
| dev->props.use_shared_buffers = dev->props.has_unified_memory; | ||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Patch comment contradicts itself on A13 residency-set support.
Lines 5-8 state A12/A13 GPUs "do not support MTLResidencySet," but line 20 states "Residency sets require Apple GPU family 6+ (A13+)," implying A13 does support them. If A13 truly lacks support, the guard at line 39 (
supportsFamily:MTLGPUFamilyApple6) would still enable residency sets on A13 devices, leaving the crash unfixed on iPhone 11. Clarify whether the iPhone 11 crash is real-hardware or simulator-only, and update the comment to resolve the contradiction.🤖 Prompt for AI Agents