Skip to content

vllm.v1.worker.gpu.model_runner

NOTE: Coding style guide for this file: This model runner is shared by all models: text and multimodal, generative and embedding, public and private. As a result, this file must only contain code that is common to every model. Model-specific behavior belongs in the appropriate model-specific files.

In other words: * Be paranoid about changing this file. It should remain stable. * Be even more paranoid about adding new lines. It should remain minimal.

Even for shared features (for example, different parallelism modes), keep the complexity out of this path. The less common the feature, the more it should be hidden. Prefer utility functions defined elsewhere and call them from here, instead of embedding feature-specific logic directly.

Classes:

BatchReqState

Bases: NamedTuple

CPU request state for a scheduled batch, in batch (sorted) order.

GPUModelRunner

Bases: LoRAModelRunnerMixin

Methods:

_get_num_drafted_and_verified(input_batch)

Drafted and verified draft tokens per request.

Must run before the drafter overwrites the draft tokens being verified.

_init_kv_zero_meta()

Build KV-block zeroing metadata; invoked from gpu_worker.

_merge_ec_connector_no_forward(scheduler_output, output)

Let the EC connector send/recv on a step with no work to run.

draft_context()

Build and run the draft model in its own workspace lane and as the only pipeline stage: it runs whole on the last PP rank.

gather_batch_req_state(scheduler_output, dummy_run)

Gather CPU request state for the scheduled batch, in batch order. Returns (batch_state, uniform_decode_token_count)

initialize_kv_cache(kv_cache_config, is_profiling=False, kv_cache_allocation_context=None, extensible=False)

Initialize the KV cache and everything that depends on its layout.

With extensible=True, num_blocks is a capacity: address space is reserved for it but only the null block is committed before capture.

needs_cudagraph_capture()

Whether capture_model() has any CUDA graphs left to capture.

profile_adaptive_verification()

Seed adaptive verification's cost curves from timed dummy runs, which read KV context from the committed blocks.

profile_cudagraph_memory()

Estimate the GPU memory required to capture CUDA graphs.

reload_weights(weights_iterator=None, weights_path=None, is_checkpoint_format=True)

Reload weights in place.

shutdown()

Release GPU tensors (model weights, KV caches, workspace) so that memory is reclaimable when running in the same process.