fix(cpp): add cross-process file lock for JIT kernel builds - #3019
Merged
Qubitium merged 1 commit intoAug 19, 2026
Merged
Conversation
Ndgandhi23
marked this pull request as draft
August 19, 2026 03:22
Ndgandhi23
force-pushed
the
fix/concurrent-jit-build-cache-lock
branch
from
August 19, 2026 03:35
d1a22b8 to
2c19be5
Compare
Two quantize() processes on one host share the same JIT build cache, but the existing lock is process-local. If a process is killed mid build, torch's lock file is left behind and every later load() waits on it forever at 0% cpu (ModelCloud#3015). Serialize builds with an fcntl.flock that the kernel auto-releases when the holding process dies. Once the lock is held, any leftover torch lock file is removed. The wait is bounded by GPTQMODEL_TORCH_OPS_LOCK_TIMEOUT and falls back to non-JIT paths on timeout. Cache clears run under the same lock so they cannot wipe another process's in-flight build. Ref ModelCloud#3015
Ndgandhi23
force-pushed
the
fix/concurrent-jit-build-cache-lock
branch
from
August 19, 2026 03:53
2c19be5 to
4a1ee5a
Compare
Ndgandhi23
marked this pull request as ready for review
August 19, 2026 03:54
Qubitium
approved these changes
Aug 19, 2026
Collaborator
|
@Ndgandhi23 LGTM. Thank you! |
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
Two
quantize()processes on one host share the same JIT build cache, but the lock guarding it is process-local. torch serializes builds with a lock file that has no timeout or owner check. If a process is killed mid-build, that file is left behind and every laterload()waits on it forever at 0% cpu. Reproduced locally without a GPU. Surfaced while investigating #3015.The fix is an
fcntl.flockaround the build step. The kernel releases the lock automatically when the holding process dies, so a killed process can never leave the cache locked. Once the lock is held, any leftover torch lock file is removed. The wait is also bounded, so a stuck holder degrades the load into the existing non-JIT fallback instead of hanging a worker.What Changed
gptqmodel/utils/cpp.py:_cross_process_build_lockcontext manager using stdlibfcntl, with a no-op fallback wherefcntlis unavailable. The lock file lives outside the build dir so cache clears never delete it.TorchOpsJitExtension.load()runs its cache-probe and compile section under this lock, and removes any stale torch lock file after acquiring it.GPTQMODEL_TORCH_OPS_LOCK_TIMEOUT(default 600s, or 5x the kernel's compile baseline if larger). On timeout the load fails cleanly into the non-JIT path.clear_cache()and force-rebuild delete the cache under the same lock, so a wipe cannot land under another process's in-flight build.load()tail reindented into the newwithblock. It is byte-identical apart from the stale-lock removal call.tests/test_torch_ops_jit_extension.py: three new tests — a stale torch lock file is removed before compiling, a SIGKILLed lock holder releases the lock so load proceeds, and a live holder hits the bounded wait instead of hanging. The subprocess tests skip wherefcntlis unavailable.README.md: new "JIT kernel cache and multi-process quantization" section documenting the cache dir and both env vars.Tests
Paste the exact test commands and results here:
Review Requirements
Notes
flockon NFS-mounted cache dirs has weaker advisory semantics; worst case is today's behavior.Assisted with Claude Code; every change personally designed and reviewed.