vllm.compilation.backends
¶
Classes:
-
CompilerManager–A manager to manage the compilation process, including
-
PiecewiseCompileInterpreter–Code adapted from
torch.fx.passes.shape_prop.ShapeProp. -
VllmBackend–The compilation backend for
torch.compilewith vLLM.
Functions:
-
set_model_tag–Context manager to set the model tag.
-
wrap_with_cudagraph_if_needed–Wrap a piecewise backend with CUDA graph wrapper if needed.
CompilerManager
¶
A manager to manage the compilation process, including caching the compiled graph, loading the compiled graph, and compiling the graph.
The cache is a dict mapping
(runtime_shape, graph_index, backend_name)
to any_data returned from the compiler.
When serializing the cache, we save it to a Python file for readability. We don't use json here because json doesn't support int as key.
Methods:
-
compile_context–Provide compilation context for the duration of compilation to set
-
initialize_cache–Initialize the cache directory for the compiler.
compile_context(compile_range)
¶
Provide compilation context for the duration of compilation to set any torch global properties we want to scope to a single Inductor compilation (e.g. partition rules, pass context).
initialize_cache(cache_dir, disable_cache=False, prefix='')
¶
Initialize the cache directory for the compiler.
The organization of the cache directory is as follows: cache_dir=/path/to/hash_str/rank_i_j/prefix/ inside cache_dir, there will be: - vllm_compile_cache.py - computation_graph.py - transformed_code.py
for multiple prefixes, they can share the same base cache dir of /path/to/hash_str/rank_i_j/ , to store some common compilation artifacts.
PiecewiseCompileInterpreter
¶
Bases: Interpreter
Code adapted from torch.fx.passes.shape_prop.ShapeProp.
It runs the given split graph interpreter, and for each submodule in
compile_submod_names, creates a PiecewiseBackend and compiles all
ranges up front.
NOTE: the order in compile_submod_names matters, because
it will be used to determine the order of the compiled piecewise
graphs. The first graph will handle logging, and the last graph
has some special cudagraph output handling.
Note: This class shares similar logic with reconstruct_serializable_fn_from_mega_artifact in caching.py. Both create PiecewiseBackend instances and wrap them with cudagraph. The key difference is: - reconstruct_serializable_fn_from_mega_artifact: PiecewiseBackend receives pre-compiled runnables (compiled_runnables is set, graph is None) - this class: PiecewiseBackend receives the FX graph to compile (graph is set, compiled_runnables is None)
If modifying the backend creation/wrapping logic, consider updating both.
VllmBackend
¶
The compilation backend for torch.compile with vLLM.
It is used for compilation mode of CompilationMode.VLLM_COMPILE,
where we customize the compilation.
The major work of this backend is to split the graph into piecewise graphs, and pass them to the piecewise backend.
This backend also adds the PostGradPassManager to Inductor config, which handles the post-grad passes.
Methods:
-
collect_standalone_compile_artifacts–Collect inductor cache artifacts from all piecewise backends.
_log_compilation_config()
¶
Log vLLM compilation config for TORCH_TRACE/tlparse.
collect_standalone_compile_artifacts()
¶
Collect inductor cache artifacts from all piecewise backends.
Returns:
-
tuple(tuple[Any, dict[str, list[int]] | None, dict[str, bool] | None]) –(standalone_compile_artifacts, sym_shape_indices_map, returns_tuple_map) - standalone_compile_artifacts: StandaloneCompiledArtifacts with compiled artifacts - sym_shape_indices_map: dict mapping submod_name to sym_shape_indices - returns_tuple_map: dict mapping submod_name to returns_tuple
_decompose_size_nodes(graph)
¶
Decompose x.size() into per-dim sym_size.int calls.
torch.Size objects cannot cross split boundaries because aot_autograd cannot handle them as submodule outputs. This replaces each size() call with individual sym_size.int(x, dim) nodes: - Dynamic dims (SymInt) → new sym_size.int node - Static dims (plain int) → inlined as literal constant
_merge_empty_only_subgraphs(node_to_subgraph_id, split_op_graphs)
¶
Merge a partition that only contains an empty allocation op into the previous partition. This avoids generating standalone empty submodules, which can lead to empty cudagraph captures.
set_model_tag(tag, is_encoder=False)
¶
Context manager to set the model tag.
wrap_with_cudagraph_if_needed(piecewise_backend, vllm_config, compilation_config, is_first_graph, is_last_graph)
¶
Wrap a piecewise backend with CUDA graph wrapper if needed. This function is shared between VllmBackend and construct_serializable_fn_from_inductor_cache.
Parameters:
-
(piecewise_backend¶Any) –The backend to wrap
-
(vllm_config¶VllmConfig) –The vLLM configuration
-
(compilation_config¶CompilationConfig) –The compilation configuration
-
(is_first_graph¶bool) –Whether this is the first graph in the sequence
-
(is_last_graph¶bool) –Whether this is the last graph in the sequence
Returns:
-
Any–The wrapped backend if CUDA graphs are enabled, otherwise the original backend