Implementation:Vllm project Vllm Model Inspection
| Knowledge Sources | |
|---|---|
| Domains | Model_Inspection, Debugging, Quantization |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Provides utilities for inspecting and formatting PyTorch model architecture into human-readable hierarchical string representations.
Description
This module contains functions to recursively traverse a torch.nn.Module tree and produce a formatted string representation similar to Hugging Face Transformers model summaries. It groups identical numbered children (such as repeated decoder layers) using range notation (e.g., "0-27, 29-47: 47 x LlamaDecoderLayer"), extracts quantization method and scheme information from modules, and formats the output with proper indentation. The main entry point is format_model_inspection, which returns the complete formatted string.
Usage
Use this when you need to inspect or log the architecture of a loaded vLLM model, particularly to verify quantization schemes applied to specific layers, debug model loading issues, or document the structure of a deployed model configuration.
Code Reference
Source Location
- Repository: vllm
- File: vllm/model_inspection.py
- Lines: 1-136
Signature
def _get_module_info(module: nn.Module) -> str:
"""Get info string for a module, including quantization details."""
def _get_child_signature(child: nn.Module) -> str:
"""Get a signature for a child module to detect duplicates."""
def _format_index_ranges(indices: list[int]) -> str:
"""Format indices into range notation (e.g., [0,1,2,4,5,6] -> '0-2, 4-6')."""
def _format_module_tree(
module: nn.Module, name: str = "", indent: int = 0,
) -> list[str]:
"""Format a module tree with indentation, grouping identical layers."""
def format_model_inspection(model: nn.Module) -> str:
"""Format a model into a transformers-style hierarchical string."""
Import
from vllm.model_inspection import format_model_inspection
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | torch.nn.Module | Yes | The PyTorch model to inspect and format |
| module | torch.nn.Module | Yes | Individual module for info extraction (internal helpers) |
| name | str | No | Name prefix for the current module in the tree (default empty) |
| indent | int | No | Current indentation level (default 0) |
Outputs
| Name | Type | Description |
|---|---|---|
| formatted_string | str | Hierarchical string representation of the model architecture with quantization info and grouped layers |
Usage Examples
from vllm.model_inspection import format_model_inspection
import torch.nn as nn
# Inspect a loaded vLLM model
# model = vllm_engine.model_executor.driver_worker.model_runner.model
formatted = format_model_inspection(model)
print(formatted)
# Example output:
# LlamaForCausalLM(
# (model): LlamaModel(
# (embed_tokens): VocabParallelEmbedding(...)
# (layers): ModuleList(
# (0-31): 32 x LlamaDecoderLayer(
# (self_attn): LlamaAttention(quant=AWQLinearMethod, ...)
# (mlp): LlamaMLP(...)
# )
# )
# (norm): LlamaRMSNorm(...)
# )
# (lm_head): ParallelLMHead(...)
# )