Skip to main content
The Engine class is the core component that manages model execution, KV cache, attention backends, and CUDA graph optimization. It handles the low-level details of GPU inference.

Constructor

EngineConfig
required
Engine configuration object containing:
  • Model configuration (architecture, vocab size, etc.)
  • Tensor parallelism settings
  • Memory and performance tuning
  • Backend selection (attention, MoE)
  • CUDA graph settings

Initialization Process

The engine initialization performs several critical steps:
  1. Communication Setup: Initializes distributed communication (NCCL/Gloo) for tensor parallelism
  2. Model Loading: Loads model weights from disk or initializes dummy weights
  3. KV Cache Allocation: Allocates page-based KV cache in GPU memory
  4. Page Table Creation: Sets up page table for efficient memory management
  5. Backend Initialization: Configures attention backend (FlashAttention, FlashInfer, TRT-LLM) and MoE backend if needed
  6. Sampler Setup: Initializes token sampling logic
  7. CUDA Graph Capture: Pre-records frequently used batch sizes for faster execution

Key Methods

forward_batch()

Executes a forward pass for a batch of requests.
Batch
required
Batch object containing:
  • reqs: List of request objects
  • phase: Either “prefill” or “decode”
  • input_ids, positions, out_loc: Prepared tensors
BatchSamplingArgs
required
Sampling arguments for the batch, including temperature, top-k, top-p per request.
Returns: ForwardOutput A named tuple containing:
  • next_tokens_gpu: Sampled tokens on GPU
  • next_tokens_cpu: Sampled tokens copied to CPU
  • copy_done_event: CUDA event for synchronization

shutdown()

Cleanly shuts down the engine and releases resources.
This method:
  • Destroys CUDA graphs
  • Cleans up distributed process groups
  • Releases GPU memory

Key Attributes

nn.Module
The loaded language model
BaseKVCachePool
KV cache pool managing paged memory
torch.Tensor
Page table mapping logical to physical KV cache locations Shape: (max_running_req + 1, aligned_max_seq_len)
BaseAttnBackend
Attention backend implementation (FlashAttention, FlashInfer, or TRT-LLM)
BaseMoeBackend
MoE backend for mixture-of-experts models (if applicable)
Sampler
Token sampling module
GraphRunner
CUDA graph manager for optimized execution
Context
Global context object containing shared state

Architecture

The engine coordinates several subsystems:

Memory Management

The engine automatically determines the number of KV cache pages based on available GPU memory:
You can override this with num_page_override in EngineConfig.

CUDA Graph Optimization

The engine captures CUDA graphs for frequently used batch sizes (specified in cuda_graph_bs). This reduces kernel launch overhead:
During execution:
  • If batch size matches a captured graph → replay graph (faster)
  • Otherwise → execute model normally

Distributed Execution

The engine supports tensor parallelism for large models:

Notes

  • The engine must be initialized before CUDA is initialized elsewhere
  • Only one engine instance should exist per process
  • The engine is not thread-safe; use separate processes for parallelism
  • Call shutdown() to properly clean up resources before process exit
  • The dummy request (table index = max_running_req) is used for CUDA graph capture