transformer_lens.model_bridge.sources.vllm.plugin module¶
vLLM plugin entry point.
Monkey-patches Worker.load_model to install capture hooks after weights
load and before compile_or_warm_up_model — the only window where hooks
make it into the compiled FX graph (PyTorch #117758).
Two hook flavors, selected by configure(enable_batching=...):
Compiled (default): in-place writes to a pre-allocated GPU tensor; no
.cpu()(illegal during CUDA-graph capture); SymInt-indexed slicing only (Python.shapeaccess forces specialization). Single prompt. Interventions ride the same hook as an affine transformoutput = output * scale_buf + bias_buf(defaults identity). The driver swaps buffer contents between forwards viatl_set_interventions— the FX graph references the buffers, so swaps take effect without recompiling. Memory cost: the affine allocates a transient output-shape tensor per hook per forward, even at identity — peak forward memory ~1.5× capture-only. Branching to skip the affine would defeat the swap trick and break the graph.Batched (
enable_batching=True, runsenforce_eager): the hook reads per-request token boundaries viasegment_by_request(only valid inside a forward, untraceable under compile — hence eager), slices each request’s rows to CPU, and appends to per-(req_id, hook) accumulators across chunked-prefill forwards. Interventions apply directly to the tensor via_apply_op.
- transformer_lens.model_bridge.sources.vllm.plugin.clear_config() None¶
Unset the spec channel. Boot sites call this in a
finallyafterLLM(...)so a later non-TL engine can’t inherit the specs.
- transformer_lens.model_bridge.sources.vllm.plugin.configure(capture_specs: Dict[str, Tuple[str, int]], max_num_batched_tokens: int, dtype: dtype, enable_batching: bool = False, enable_position_interventions: bool = False) None¶
Set capture specs, buffer length, dtype, and hook flavor before
LLM(...).
- transformer_lens.model_bridge.sources.vllm.plugin.register() None¶
Idempotent monkey-patch of
Worker.load_model.vLLM calls
register()once per process at entry-points discovery. Idempotent so re-imports (notebook restarts, repeatedboot_vllmin the same process) don’t double-wrap.No
unregister()symmetry: the patch stays for process lifetime. Benign becausepatched_load_modelno-ops when no spec channel is populated — and boot sites callclear_config()after eachLLM(...), so any subsequent non-TLLLM(...)in the same process hits the no-op path.