Repository navigation
vulkan: create the GPU that was registered, even if the loader reorders devices - #4
Open
alanshurafa wants to merge 2 commits into
Open
alanshurafa wants to merge 2 commits into
alanshurafa wants to merge 2 commits into
Conversation
ggml-vulkan cached device_indices at instance init and re-enumerated physical devices later to create the device. When the loader's order changes in between (seen on Windows with an iGPU and a dGPU), the same index picks the other GPU while the logs still name the first. Keep the handles from the first enumeration and use them everywhere. Also add the change as patches/ggml/0004 so sync-ggml.sh keeps it. Co-Authored-By: Claude Opus <noreply@anthropic.com>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
…ter#246's 0004 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
DRAFT: Alan rewrites this in his own words before posting (CONTRIBUTING.md).
Summary
On computers with two graphics chips, Handy now uses the fast one it says it's using instead of sometimes slipping onto the slow one.
ggml-vulkan picks its device list once, in
ggml_vk_instance_init(), and stores each device as an index intovkEnumeratePhysicalDevices()(vk_instance.device_indices). The backend registry caches each device's name, type and PCI id from that list. TheVkDeviceitself is created later, inggml_vk_get_device(), which enumerates again and takesphysical_devices[device_indices[idx]]. If the loader returns the GPUs in a different order the second time,VulkanNgets created on a different GPU than the one it was registered and logged as. On Windows the order can change while the process is running, which is what cjpais/Handy#2358 reports: Handy said "RTX 5090" but ran Whisper on the Intel iGPU, about 40x slower, until restart.This change saves the
vk::PhysicalDevicehandles from the init enumeration (vk_instance.physical_devices, parallel todevice_indices) and uses them everywhere the code used to re-enumerate and index: device creation, the init-time info print, description, memory, device type and PCI id. Physical device handles belong to the instance and stay valid for its lifetime, so this keeps every path on the same GPU. When the order doesn't change, the selected devices are exactly the same as before.Scope
ggml/src/ggml-vulkan/ggml-vulkan-types.h,ggml/src/ggml-vulkan/ggml-vulkan.cpp(vendored ggml): the fix.patches/ggml/0004-vulkan-keep-physical-device-handles.patch: the same change as a downstream patch soscripts/sync-ggml.shkeeps it.ggml/UPSTREAM: lists the new patch.The patch is numbered 0004 because that's the next free number on main. handy-computer#246 also adds a 0004; whichever lands second needs renumbering to 0005.
Related
Fixes cjpais/Handy#2358 (thanks to the reporter for the analysis, which held up line by line).
ggml-org/llama.cpp master and ggml-org/ggml master still have the same re-enumerate-and-index code in
ggml_vk_get_device(),ggml_backend_vk_get_device_memory(),ggml_backend_vk_get_device_type()andggml_backend_vk_get_device_pci_id(). I didn't find an open or merged PR there for this. The patch doesn't apply cleanly to ggml master (context drift inggml_vk_instance_init()), so sending it upstream would need a small rebase.AI Assistance
Claude Code wrote the change, the test layer and this draft; a person reviews it.
Validation
Windows 11, MSVC 2022 Build Tools, Vulkan SDK 1.4.363.0, laptop with an Intel UHD iGPU and an RTX 3080 Laptop GPU. ggml lists
Vulkan0= Intel UHD andVulkan1= RTX 3080, the same layout as in the issue. Built with-DTRANSCRIBE_VULKAN=ON(Release, Ninja). Modelmoonshine-tiny-Q8_0.gguf, audiosamples/jfk.wav,transcribe-cli --backend vulkan --device N.I couldn't make the real loader reorder GPUs on demand, so I wrote a small throwaway Vulkan layer (not part of this PR). It passes
vkEnumeratePhysicalDevicesthrough, rotates the returned order by one from the Nth call on, and logs the device eachvkCreateDevicetargets. The "before" binary is a main build with no ggml changes.Reorder injected after registration, just before the device is created:
--devicevkCreateDevicewent toWith the fix, ggml enumerates physical devices once per process (the layer sees one enumeration instead of eleven), so a later reorder can't reach it.
No reorder (layer logging only, and once without the layer): both builds create Intel UHD for
--device 0and the RTX 3080 for--device 1, transcripts are identical.GGML_VK_VISIBLE_DEVICES=1still givesVulkan0= RTX 3080 and creates it there.ctest --test-dir build-vk: 47 passed, 1 skipped (transcribe_cli_diarize_smoke, needs a model).The patch file applies forward to the
upstream/mainggml files and in reverse to the committed tree (git apply --check [-R] --directory=ggml, assync-ggml.shdoes). I didn't run the full sync script.Not tested: the real Windows reorder from the issue (monitor switch or GPU wake on a desktop with one monitor per GPU), Linux, macOS/MoltenVK, and a system where a GPU is removed while the process runs (this change doesn't try to handle that).
🤖 Generated with Claude Code