vllm.utils.gpu_sync_debug
¶
Detect unintended GPU<->CPU syncs in the hot path.
torch.cuda.set_sync_debug_mode is process-global, so we arm it at "warn"
(which never raises) and decide in _sync_warning_hook whether a given sync
is a failure. The scoping lives in ContextVars, which are per-thread and
per-asyncio-task, so an allow region opened on one thread is invisible to
every other one.
torch's mode only instruments explicit synchronizing calls, so it misses
non_blocking=True CPU<->CUDA copies that the CUDA driver silently stages
through pageable memory (when the CPU tensor is not pinned or not densely
laid out). Those are caught by the wrappers on Tensor.to/Tensor.cuda/
Tensor.copy_ installed by _install_copy_checkers().
Functions:
-
enable_gpu_sync_check–Flip the sync-check gate on, once per worker, after warmup.
-
gpu_sync_allowed–Allow GPU<->CPU syncs inside the
withblock, on this thread only. -
with_gpu_sync_check–Report GPU<->CPU syncs performed by
fnon its calling thread.
_active_check_mode()
¶
The mode the calling thread is being checked in, if any.
_checked_region(mode)
¶
Police syncs on this thread for the duration of the block.
The debug mode is armed per call so that syncs outside a checked region
emit nothing, and refcounted because execute_model and sample_tokens
nest. "warn" rather than "error" because torch's error mode raises on
whichever thread synced, with no way to exempt one.
_cpu_copy_stall_reason(cpu_tensor, device)
¶
Why a non_blocking copy between cpu_tensor and a CUDA tensor would
silently block the host, or None if it is genuinely asynchronous.
device describes the other side of the copy; a non-CUDA device means
the copy does not involve the GPU at all.
_install_compile_time_sync_suppressors()
¶
Allow the syncs torch's compile passes perform.
Warmup-time compiles run before the gate flips, but lazy ones fire inside
execute_model.
_install_copy_checkers()
¶
Wrap the Python-level CPU<->CUDA transfer entry points with the checks above.
The patch is process-global, but it only reports on threads inside a checked region, matching the scoping of the torch-level check. Copies issued from C++ (e.g. inside compiled graphs) bypass it.
_install_warning_hook()
¶
(Re)install the hook and a filter that lets torch's warning reach it.
Done per checked call because pytest runs each test inside
warnings.catch_warnings(), which restores both showwarning and
filters. The hook is left in place afterwards: outside a checked call
the debug mode is disarmed, so torch emits nothing for it to see.
_is_dense(t)
¶
Whether t's elements fill a contiguous storage range, possibly
permuted (e.g. a transposed matrix). Such layouts can be copied with
pitched cudaMemcpy2D/3DAsync directly from/to pinned memory, whereas
gapped layouts (e.g. strided slices) stage through a pageable temp.
_suppressing(fn)
¶
Allow the syncs fn performs on its calling thread.
_sync_warning_hook(message, category, filename, lineno, file=None, line=None)
¶
Turn torch's sync warning into an error, but only on a thread that is being checked and is outside any allow region.
enable_gpu_sync_check()
¶
Flip the sync-check gate on, once per worker, after warmup.
gpu_sync_allowed(first_only=False)
¶
Allow GPU<->CPU syncs inside the with block, on this thread only.
With first_only, only the first entry from a given call site
(filename, lineno) is allowed, so later syncs there are still reported.
with_gpu_sync_check(fn)
¶
Report GPU<->CPU syncs performed by fn on its calling thread.
Active only once enable_gpu_sync_check() has flipped the gate. Other
threads are never policed, so deliberate syncs there (e.g. the EPLB
transfer worker) are unaffected.