vllm.compilation.compiler_interface
AlwaysHitShapeEnv
¶
Why do we need this class:
For normal torch.compile
usage, every compilation will have
one Dynamo bytecode compilation and one Inductor compilation.
The Inductor compilation happens under the context of the
Dynamo bytecode compilation, and that context is used to
determine the dynamic shape information, etc.
For our use case, we only run Dynamo bytecode compilation once, and run Inductor compilation multiple times with different shapes plus a general shape. The compilation for specific shapes happens outside of the context of the Dynamo bytecode compilation. At that time, we don't have shape environment to provide to Inductor, and it will fail the Inductor code cache lookup.
By providing a dummy shape environment that always hits, we can make the Inductor code cache lookup always hit, and we can compile the graph for different shapes as needed.
The following dummy methods are obtained by trial-and-error until it works.
Source code in vllm/compilation/compiler_interface.py
CompilerInterface
¶
The interface for a compiler that can be used by vLLM.
Source code in vllm/compilation/compiler_interface.py
compile
¶
compile(
graph: GraphModule,
example_inputs: list[Any],
compiler_config: dict[str, Any],
runtime_shape: Optional[int] = None,
key: Optional[str] = None,
) -> tuple[Optional[Callable], Optional[Any]]
Compile the graph with the given example inputs and compiler config,
with a runtime shape. If the runtime_shape
is None, it means
the example_inputs
have a dynamic shape. Otherwise, the
runtime_shape
specifies the shape of the inputs. Right now we only
support one variable shape for all inputs, which is the batchsize
(number of tokens) during inference.
Dynamo will make sure graph(*example_inputs)
is valid.
The function should return a compiled callable function, as well as a handle that can be used to directly load the compiled function.
The handle should be a plain Python object, preferably a string or a file path for readability.
If the compiler doesn't support caching, it should return None for the handle. If the compiler fails to compile the graph, it should return None for the compiled function as well.
key
is required for StandaloneInductorAdapter, it specifies where to
save the compiled artifact. The compiled artifact gets saved to
cache_dir/key
.
Source code in vllm/compilation/compiler_interface.py
compute_hash
¶
compute_hash(vllm_config: VllmConfig) -> str
Gather all the relevant information from the vLLM config, to compute a hash so that we can cache the compiled model.
See VllmConfig.compute_hash
to check what information
is already considered by default. This function should only
consider the information that is specific to the compiler.
Source code in vllm/compilation/compiler_interface.py
initialize_cache
¶
when the vLLM process uses cache_dir
as the cache directory,
the compiler should initialize itself with the cache directory,
e.g. by re-directing its own cache directory to a sub-directory.
prefix can be used in combination with cache_dir to figure out the base cache directory, e.g. there're multiple parts of model being compiled, but we want to share the same cache directory for all of them.
e.g. cache_dir = "/path/to/dir/backbone", prefix = "backbone" cache_dir = "/path/to/dir/eagle_head", prefix = "eagle_head"
Source code in vllm/compilation/compiler_interface.py
load
¶
load(
handle: Any,
graph: GraphModule,
example_inputs: list[Any],
graph_index: int,
runtime_shape: Optional[int] = None,
) -> Callable
Load the compiled function from the handle. Raises an error if the handle is invalid.
The handle is the second return value of the compile
function.
Source code in vllm/compilation/compiler_interface.py
EagerAdaptor
¶
Bases: CompilerInterface
Source code in vllm/compilation/compiler_interface.py
compile
¶
compile(
graph: GraphModule,
example_inputs: list[Any],
compiler_config: dict[str, Any],
runtime_shape: Optional[int] = None,
key: Optional[str] = None,
) -> tuple[Optional[Callable], Optional[Any]]
Source code in vllm/compilation/compiler_interface.py
InductorAdaptor
¶
Bases: CompilerInterface
The adaptor for the Inductor compiler, version 2.5, 2.6, 2.7.
Source code in vllm/compilation/compiler_interface.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
|
compile
¶
compile(
graph: GraphModule,
example_inputs: list[Any],
compiler_config: dict[str, Any],
runtime_shape: Optional[int] = None,
key: Optional[str] = None,
) -> tuple[Optional[Callable], Optional[Any]]
Source code in vllm/compilation/compiler_interface.py
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
|
compute_hash
¶
compute_hash(vllm_config: VllmConfig) -> str
initialize_cache
¶
Source code in vllm/compilation/compiler_interface.py
load
¶
load(
handle: Any,
graph: GraphModule,
example_inputs: list[Any],
graph_index: int,
runtime_shape: Optional[int] = None,
) -> Callable
Source code in vllm/compilation/compiler_interface.py
metrics_context
¶
metrics_context() -> AbstractContextManager
This method returns the Dynamo metrics context (if it exists, otherwise a null context). It is used by various compile components. Present in torch>=2.6, it's used inside FxGraphCache in torch==2.6 (but not after). It might also be used in various other torch.compile internal functions.
Because it is re-entrant, we always set it (even if entering via Dynamo and the context was already entered). We might want to revisit if it should be set at a different level of compilation.
This is likely a bug in PyTorch: public APIs should not rely on manually setting up internal contexts. But we also rely on non-public APIs which might not provide these guarantees.
Source code in vllm/compilation/compiler_interface.py
InductorStandaloneAdaptor
¶
Bases: CompilerInterface
The adaptor for the Inductor compiler. Requires PyTorch 2.8+. This is not on by default yet, but we plan to turn it on by default for PyTorch 2.8.
Use VLLM_USE_STANDALONE_COMPILE to toggle this on or off.
Source code in vllm/compilation/compiler_interface.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
|
compile
¶
compile(
graph: GraphModule,
example_inputs: list[Any],
compiler_config: dict[str, Any],
runtime_shape: Optional[int] = None,
key: Optional[str] = None,
) -> tuple[Optional[Callable], Optional[Any]]
Source code in vllm/compilation/compiler_interface.py
compute_hash
¶
compute_hash(vllm_config: VllmConfig) -> str
initialize_cache
¶
load
¶
load(
handle: Any,
graph: GraphModule,
example_inputs: list[Any],
graph_index: int,
runtime_shape: Optional[int] = None,
) -> Callable