Repository navigation
Conversation
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
GGUFCudaKernel.forwardtook no lock on the CUDA path while the CPU path already used_cpu_backend_lock. Every layer on a device shares one ggml CUDA backend, and each layer caches plans with fixed input/output buffers, so concurrent forwards on one layer overwrote each other's plan buffers (wrong outputs) and forwards on distinct layers drove the shared stream/graph-capture/memory pool concurrently (ggml abort)._cuda_weight()could also close plans and free the shared weight while another thread was still computing with it.What Changed
threading.RLockmap on_GGMLBridge(cuda_backend_lock()), shared by every layer on that device._get_cuda_backend()now creates/reads backend entries under that lock.GGUFCudaKernel.forwardholds the device lock around weight resolution, plan cache lookup, plan build/run, and plan insert/eviction._finish_ggml_outputand adapter work stay outside the lock._cuda_weight()takes the same re-entrant device lock, so closing stale plans and freeing the shared weight cannot run while another thread is computing with that weight.clear_weight_cache()/__del__free plans and the weight under the same lock.tests/test_gguf_cuda_threadsafe_4314.py: mocked plan/backend records concurrent entries; two threads on the same layer and on distinct layers must never overlap (max concurrent == 1) and must match the serial output; a weight swap is serialized against an in-flight forward.Tests
The regression tests fail on the unfixed code (all 3 assert
max_active == 1vs observed 2).tests/test_gguf_all_formats.pyhas 10 pre-existing CPU-tolerance failures on this host (identical on unmodifiedorigin/main), unrelated to this diff.Could not run: the issue repro against a real ggml CUDA build. The ggml CUDA extension is not built in this container and cannot be built here, so the thread-safety verification is mock-based; no real CUDA backend was exercised.
Model Support / Module Plan
N/A — no new model types or registry entries.
Fixes #4314