Skip to main content
LLMEngine is the main entry point for running inference with miniVLLM. It manages worker processes for multi-GPU tensor parallelism, a token-level scheduler with paged KV cache, and a model runner that supports CUDA graph replay for fast decode.

Constructor

Creates the engine, spawns worker processes for ranks 1..world_size-1, initialises the rank-0 ModelRunner (which triggers weight loading, warmup, KV cache allocation, and CUDA graph capture), then creates the Scheduler.
The Scheduler is initialised after ModelRunner because ModelRunner.__init__ calls dist.init_process_group(), a collective barrier that blocks until every worker rank has joined. Creating the scheduler before that barrier would deadlock on multi-GPU setups.

Config parameters

The config dict is shared between the scheduler, the model runner, and memory management. All keys consumed by ModelRunner must be present.

Scheduling and memory

string
required
HuggingFace model ID or local path to the model checkpoint. Currently supported: "Qwen/Qwen3-0.6B" and "meta-llama/Llama-3.2-1B-Instruct".The model is identified by the final path component (e.g. "Qwen3-0.6B"), so local paths and HF IDs both work as long as the name matches a supported model.
int
default:"1"
Number of GPUs to use for tensor-parallel inference. Ranks 1..world_size-1 are spawned as separate processes and communicate via NCCL.
int
default:"16"
Maximum number of sequences concurrently active in the scheduler (passed to Scheduler).
int
default:"1024"
Maximum total tokens across all sequences in a single forward pass (passed to Scheduler).
int
default:"1024"
Initial KV cache block pool size hint. Overridden at runtime by ModelRunner.allocate_kv_cache(), which measures actual free GPU memory and writes the true value back into this key.
int
default:"256"
Tokens per KV cache block. Must be consistent across engine, scheduler, and attention layers.
int
default:"50256"
EOS token ID used by the scheduler to stop generation.
The default 50256 is the GPT-2 EOS token. For Qwen3-0.6B use 151645; verify your tokenizer’s eos_token_id for other models.
bool
default:"False"
When True, disables CUDA graph capture so all forward passes run eagerly. Useful for debugging. When False, CUDA graphs are captured during initialization for fast decode replay.
float
default:"0.9"
Fraction of free GPU memory to use for the KV cache pool. 0.9 means 90% of available memory after model weights is used for blocks.
int
required
Maximum tokens in a single warmup batch. Used by ModelRunner.warmup_model() to size the dry-run forward pass: batch_size = max_num_batch_tokens // max_model_length.
int
required
Maximum total sequence length (prompt + generated tokens). Used both for warmup sizing and for CUDA graph buffer pre-allocation.

Model architecture

These keys are required when using Qwen/Qwen3-0.6B:
int
required
Vocabulary size. For Qwen3-0.6B: 151936.
int
required
Model hidden dimension. For Qwen3-0.6B: 1024.
int
required
Total query attention heads. For Qwen3-0.6B: 16.
int
required
Attention head dimension. For Qwen3-0.6B: 128.
int
required
Key/value head count (GQA). For Qwen3-0.6B: 8.
int
required
MLP hidden dimension. For Qwen3-0.6B: 3072.
int
required
Number of transformer decoder layers. For Qwen3-0.6B: 28.
bool
required
Whether to share the embedding and LM head weights. For Qwen3-0.6B: True.
int
required
RoPE base frequency. For Qwen3-0.6B: 1000000.
float
required
Epsilon for RMSNorm layers. For Qwen3-0.6B: 1e-6.
bool
required
Whether QKV projections include bias. For Qwen3-0.6B: False.
float
required
Attention scale multiplier. Typically 1.0.
int
required
Maximum position index for the RoPE cache. Must be >= max_model_length. For Qwen3-0.6B: 32768.
bool
required
Whether MLP projections include bias. For Qwen3-0.6B: False.

Methods

generate

Tokenizes every prompt, adds all sequences to the scheduler, then calls step() in a loop until every sequence has finished. Returns a dict with decoded text and raw token IDs, sorted in the same order as the input prompts.

Parameters

list[str]
required
List of plain-text prompt strings. Each string is encoded with the model’s tokenizer before being submitted to the scheduler.
SamplingParams
required
Sampling configuration applied to every prompt in this batch. See SamplingParams for field details.

Return value

list[str]
Decoded completion strings, one per input prompt, in the same order.
list[list[int]]
Raw completion token IDs (prompt tokens excluded), one list per input prompt.

add_prompt

Tokenizes prompt and enqueues it as a new Sequence in the scheduler’s waiting queue. Use this method together with step() when you need fine-grained control over the generation loop.
str
required
Plain-text prompt string.
SamplingParams
required
Sampling configuration for this sequence.

step

Runs one scheduling + forward-pass iteration:
  1. Calls Scheduler.schedule() to select the next batch and determine whether it is a prefill or a decode step.
  2. Calls ModelRunner.run() via IPC to execute the forward pass and sample one token per sequence.
  3. Calls Scheduler.postprocess() to append sampled tokens and check stopping conditions.
  4. Returns metadata for sequences that finished during this step.

Return value

list[tuple[int, list[int]]]
List of (seq_id, completion_token_ids) pairs for sequences that finished in this step. Empty when no sequence finished.
int
During prefill: total tokens in the scheduled sequences. During decode: number of sequences stepped (one token each).
bool
True when the step processed a prefill batch; False for a decode batch.

exit

Gracefully shuts down the engine. Sends an "exit" IPC call to all worker processes, deletes the ModelRunner, and joins worker processes. This method is also registered with atexit and called automatically when the Python process exits.

Usage example