Next.js
vLLM is a high-throughput, memory-efficient inference and serving engine for large language models (LLMs). It provides an OpenAI-compatible API server and offline batch inference pipeline that supports 240+ model architectures (LLaMA, GPT, Qwen, DeepSeek, Mamba, etc.) with advanced memory management via PagedAttention, speculative decoding, quantization, and distributed execution across multiple GPUs and hardware platforms.
Tech Stack
Key Features
- PagedAttention & Block-level KV Cache
- 240+ Model Architectures
- Multiple Attention Backends
- Quantization Support
- Speculative Decoding
- Fused Mixture-of-Experts
- Structured Output Enforcement
- OpenAI-Compatible API Server
- Multi-Platform Hardware Support
- Prefix Caching
- KV Cache Offloading
- Flexible Model Loading
- Comprehensive Benchmarking Suite
Coding Conventions
Standards and patterns used in this codebase
Use dataclasses for configuration objects throughout the engine, with fields having default values and type annotations. Configuration classes are centralized in vllm/config.py.
@dataclass
class CacheConfig:
block_size: int = 16
swap_space_bytes: int = 4 * GiB
cache_dtype: str = 'auto'Hardware platform abstraction via a platform layer pattern - each platform (CUDA, ROCm, CPU, TPU, XPU) implements a common interface, enabling conditional logic based on current_platform rather than direct hardware checks.
from vllm.platforms import current_platform
if current_platform.is_cuda():
...Benchmark and serving code uses async/await with aiohttp and asyncio for concurrent HTTP requests. Backend request functions are async generators that yield RequestFuncOutput objects.
async def async_request_openai_completions(
request_func_input: RequestFuncInput,
pbar: Optional[tqdm] = None,
) -> RequestFuncOutput:
async with aiohttp.ClientSession(timeout=timeout) as session:
async with session.post(api_url, headers=headers, json=payload) as response:
...Benchmark and tool scripts use argparse with a dedicated FlexibleArgumentParser or standard ArgumentParser, defining arguments in a dedicated function and using `if __name__ == '__main__':` entry points.
def main(args):
...
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='...')
parser.add_argument('--model', type=str, required=True)
parser.add_argument('--num-prompts', type=int, default=10)
args = parser.parse_args()
main(args)Use structured output dataclasses (e.g., RequestFuncOutput) with success boolean flags and error fields rather than raising exceptions for expected failure modes in benchmark/client code.
@dataclass
class RequestFuncOutput:
success: bool = False
latency: float = 0.0
error: str = ''
...Setup.py uses environment variables (e.g., VLLM_TARGET_DEVICE, MAX_JOBS, NVCC_THREADS) to control build behavior, with helper functions to read them with defaults. Extensions are conditionally compiled based on platform detection.
def _is_cuda() -> bool:
has_cuda = torch.version.cuda is not None
return has_cuda and VLLM_TARGET_DEVICE == 'cuda'
def get_nvcc_cuda_version() -> Version:
...Use Python type hints consistently including Optional, List, Dict, Tuple from typing module, and use dataclass fields with explicit types. Function signatures include return type annotations.
async def async_request_openai_completions(
request_func_input: RequestFuncInput,
pbar: Optional[tqdm] = None,
) -> RequestFuncOutput:Use snake_case for functions, variables, and modules. Use PascalCase for classes. Constants and environment variable names use UPPER_SNAKE_CASE. Prefix internal/private helpers with underscore.
VLLM_TARGET_DEVICE = os.environ.get('VLLM_TARGET_DEVICE', 'cuda')
def _is_cuda() -> bool:
...
class RequestFuncInput:
...Benchmark scripts follow a consistent pattern: define request generation, run async workload with tqdm progress bars, collect timing metrics, and print/save structured results (JSON). They support configurable backends via --backend flag.
benchmark_results = {
'model': args.model,
'backend': args.backend,
'throughput': total_tokens / elapsed_time,
}
if args.output_json:
with open(args.output_json, 'w') as f:
json.dump(benchmark_results, f, indent=2)Use lazy/conditional imports for optional or heavy dependencies, often guarded by try/except or platform checks, to avoid import errors on platforms where certain packages are unavailable.
try:
import nixl
except ImportError:
nixl = None
if TYPE_CHECKING:
from vllm.config import ModelConfigHTTP streaming responses are handled by iterating over response content line-by-line, parsing SSE (Server-Sent Events) format with 'data: ' prefix stripping and '[DONE]' sentinel detection.
async for chunk_bytes in response.content:
chunk_bytes = chunk_bytes.strip()
if not chunk_bytes:
continue
chunk = remove_prefix(chunk_bytes.decode('utf-8'), 'data: ')
if chunk == '[DONE]':
break
data = json.loads(chunk)Model implementations follow a registry pattern where each model file registers supported architectures. Models reuse shared layer implementations from model_executor/layers rather than reimplementing attention, linear, etc.
# In model file, models are registered and discovered via architecture name mapping
# Models compose shared layers: Linear, RotaryEmbedding, Attention, FusedMoE