-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[torch.compile] Add compile-only mode #38675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zou3519
wants to merge
1
commit into
main
Choose a base branch
from
compile-only-pr1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
| """Compile-only mode: populate vLLM's torch.compile cache | ||
| without loading real model weights or allocating KV caches. | ||
|
|
||
| The compile-only flag causes the model loader to be wrapped with | ||
| FakeTensorMode (see ``fake_loader.wrap_loader_with_fake``), so the | ||
| user's original ``load_format`` is preserved and the real loader's | ||
| full pipeline runs — just with fake tensors instead of real weights. | ||
| """ | ||
|
|
||
| import argparse | ||
|
|
||
| from vllm.logger import init_logger | ||
| from vllm.usage.usage_lib import UsageContext | ||
|
|
||
| logger = init_logger(__name__) | ||
|
|
||
|
|
||
| def compile_model( | ||
| model: str, | ||
| *, | ||
| tensor_parallel_size: int = 1, | ||
| pipeline_parallel_size: int = 1, | ||
| quantization: str | None = None, | ||
| dtype: str = "auto", | ||
| trust_remote_code: bool = False, | ||
| **kwargs, | ||
| ) -> None: | ||
| """Pre-populate vLLM's torch.compile cache for a model. | ||
|
|
||
| Runs compilation using fake weights (zero GPU memory) | ||
| so that vLLM's torch.compile cache is populated. Subsequent | ||
| ``vllm serve`` or ``LLM(...)`` calls for the same model | ||
| configuration will hit the warm cache and skip compilation. | ||
|
|
||
| Args: | ||
| model: HuggingFace model name or path. | ||
| tensor_parallel_size: Number of tensor parallel GPUs. | ||
| pipeline_parallel_size: Number of pipeline parallel stages. | ||
| quantization: Quantization method (e.g. "fp8"). | ||
| dtype: Model dtype. | ||
| trust_remote_code: Trust remote code from HuggingFace. | ||
| **kwargs: Additional arguments passed to ``EngineArgs``. | ||
| """ | ||
| from vllm.engine.arg_utils import EngineArgs | ||
|
|
||
| engine_args = EngineArgs( | ||
| model=model, | ||
| tensor_parallel_size=tensor_parallel_size, | ||
| pipeline_parallel_size=pipeline_parallel_size, | ||
| quantization=quantization, | ||
| dtype=dtype, | ||
|
Check failure on line 53 in vllm/compile_only.py
|
||
| trust_remote_code=trust_remote_code, | ||
| enforce_eager=False, | ||
| **kwargs, | ||
| ) | ||
|
|
||
| vllm_config = engine_args.create_engine_config(usage_context=UsageContext.LLM_CLASS) | ||
| vllm_config.compilation_config.compile_only = True | ||
|
|
||
| _run_compile_with_config(vllm_config) | ||
|
|
||
|
|
||
| def run_compile_only(args: argparse.Namespace) -> None: | ||
| """Run compile-only mode from CLI arguments.""" | ||
| from vllm.engine.arg_utils import EngineArgs | ||
|
|
||
| engine_args = EngineArgs.from_cli_args(args) | ||
| engine_args.enforce_eager = False | ||
|
|
||
| vllm_config = engine_args.create_engine_config(usage_context=UsageContext.LLM_CLASS) | ||
| vllm_config.compilation_config.compile_only = True | ||
|
|
||
| _run_compile_with_config(vllm_config) | ||
|
|
||
|
|
||
| def _run_compile_with_config(vllm_config) -> None: | ||
| """Shared compile-only logic.""" | ||
| from vllm.plugins import load_general_plugins | ||
| from vllm.v1.executor import Executor | ||
|
|
||
| load_general_plugins() | ||
|
|
||
| executor_class = Executor.get_class(vllm_config) | ||
| executor = executor_class(vllm_config) | ||
| executor.collective_rpc("compile_or_warm_up_model") | ||
|
|
||
| logger.info("Compile-only mode complete. Cache populated.") | ||
| executor.shutdown() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
|
||
| import argparse | ||
|
|
||
| from vllm.entrypoints.cli.types import CLISubcommand | ||
| from vllm.entrypoints.openai.cli_args import make_arg_parser | ||
| from vllm.entrypoints.utils import VLLM_SUBCMD_PARSER_EPILOG | ||
| from vllm.utils.argparse_utils import FlexibleArgumentParser | ||
|
|
||
| DESCRIPTION = """[Experimental] Populate vLLM's torch.compile cache for a model. | ||
|
|
||
| This command is experimental and a work in progress. Not all models and | ||
| configurations are supported yet. | ||
|
|
||
| This runs compilation using fake weights (zero GPU memory) so that | ||
| vLLM's torch.compile cache is populated. Subsequent ``vllm serve`` or | ||
| ``LLM(...)`` calls for the same model will hit the warm cache and skip | ||
| compilation. | ||
| """ | ||
|
|
||
|
|
||
| class CompileSubcommand(CLISubcommand): | ||
| """The ``compile`` subcommand for the vLLM CLI.""" | ||
|
|
||
| name = "compile" | ||
|
|
||
| @staticmethod | ||
| def cmd(args: argparse.Namespace) -> None: | ||
| from vllm.compile_only import run_compile_only | ||
|
|
||
| if hasattr(args, "model_tag") and args.model_tag is not None: | ||
| args.model = args.model_tag | ||
| run_compile_only(args) | ||
|
|
||
| def subparser_init( | ||
| self, subparsers: argparse._SubParsersAction | ||
| ) -> FlexibleArgumentParser: | ||
| compile_parser = subparsers.add_parser( | ||
| self.name, | ||
| help="[Experimental] Populate vLLM's torch.compile cache for a model.", | ||
| description=DESCRIPTION, | ||
| usage="vllm compile [model_tag] [options]", | ||
| ) | ||
| compile_parser = make_arg_parser(compile_parser) | ||
| compile_parser.epilog = VLLM_SUBCMD_PARSER_EPILOG.format(subcmd=self.name) | ||
| return compile_parser | ||
|
|
||
|
|
||
| def cmd_init() -> list[CLISubcommand]: | ||
| return [CompileSubcommand()] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.