Skip to content

fix: serialize GGUF CUDA forward per device (#4314) - #4336

Merged
Qubitium merged 1 commit into
mainfrom
pr/4314
Nov 16, 2026
Merged

Qubitium merged 1 commit into
mainfrom
pr/4314

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

GGUFCudaKernel.forward took 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

  • Added a per-device threading.RLock map on _GGMLBridge (cuda_backend_lock()), shared by every layer on that device. _get_cuda_backend() now creates/reads backend entries under that lock.
  • GGUFCudaKernel.forward holds the device lock around weight resolution, plan cache lookup, plan build/run, and plan insert/eviction. _finish_ggml_output and 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.
  • Added 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

export GPTQ_BYPASS_SAFETY_CHECKS=1 PYTHONPATH=$PWD CUDA_VISIBLE_DEVICES=
python3 -m pytest tests/test_gguf_cuda_threadsafe_4314.py -q
# 3 passed

python3 -m pytest tests/test_gguf_cuda_threadsafe_4314.py tests/kernels/test_gguf_cpp.py \
  tests/kernels/test_gguf_cpp_activation_quantization.py tests/test_gguf_bias_range.py \
  tests/test_gguf_cpu_autocast_cancellation.py tests/test_gguf_dynamic_format.py \
  tests/test_gguf_qlinear_llama.py tests/test_internal_gguf.py -q
# 125 passed, 34 skipped

ruff check gptqmodel/nn_modules/qlinear/gguf_cpp.py tests/test_gguf_cuda_threadsafe_4314.py
# All checks passed!

The regression tests fail on the unfixed code (all 3 assert max_active == 1 vs observed 2).

tests/test_gguf_all_formats.py has 10 pre-existing CPU-tolerance failures on this host (identical on unmodified origin/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

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

Labels

None yet

Projects

None yet

1 participant