From 8c34a372323b3becc92c183257d195562882ef1a Mon Sep 17 00:00:00 2001 From: zvier Date: Thu, 10 Sep 2026 11:21:58 +0800 Subject: [PATCH] [Refactor] Extract maybe_run_omni() and defer CLI imports past omni early-return (#56103) Signed-off-by: Jeff Zvier --- vllm/entrypoints/cli/main.py | 119 +++++++++++++++++++---------------- 1 file changed, 64 insertions(+), 55 deletions(-) diff --git a/vllm/entrypoints/cli/main.py b/vllm/entrypoints/cli/main.py index 3dc69dd3ad..d173bc392f 100644 --- a/vllm/entrypoints/cli/main.py +++ b/vllm/entrypoints/cli/main.py @@ -14,7 +14,32 @@ from vllm.logger import init_logger logger = init_logger(__name__) +def maybe_run_omni() -> bool: + # If `--omni` arg is passed to the CLI, delegate to vLLM Omni's entrypoint handling + if "--omni" not in sys.argv: + return False + + # NOTE: Check the spec instead of importing directly here, since things could + # fail with ImportError due to mismatched versions if things are moved around. + spec = find_spec("vllm_omni") + if spec is None: + logger.error( + "--omni flag requires a valid instance of vllm-omni to be installed." + ) + sys.exit(1) + + from vllm_omni.entrypoints.cli.main import main as omni_main + + logger.info("Delegating entrypoint handling to vllm-omni") + omni_main() + + return True + + def main(): + if maybe_run_omni(): + return + import vllm.entrypoints.cli.benchmark.main import vllm.entrypoints.cli.collect_env import vllm.entrypoints.cli.launch @@ -38,65 +63,49 @@ def main(): cli_env_setup() - # If `--omni` arg is passed to the CLI, delegate to vLLM Omni's entrypoint handling - if "--omni" in sys.argv: - # NOTE: Check the spec instead of importing directly here, since things could - # fail with ImportError due to mismatched versions if things are moved around. - spec = find_spec("vllm_omni") - if spec is None: - logger.error( - "--omni flag requires a valid instance of vllm-omni to be installed." + vllm.entrypoints.cli.benchmark.main.maybe_exec_rust_bench() + + # For 'vllm bench *': use CPU instead of UnspecifiedPlatform by default + if len(sys.argv) > 1 and sys.argv[1] == "bench": + logger.debug( + "Bench command detected, must ensure current platform is not " + "UnspecifiedPlatform to avoid device type inference error" + ) + from vllm import platforms + + if platforms.current_platform.is_unspecified(): + from vllm.platforms.cpu import CpuPlatform + + platforms.current_platform = CpuPlatform() + logger.info( + "Unspecified platform detected, switching to CPU Platform instead." ) - sys.exit(1) - from vllm_omni.entrypoints.cli.main import main as omni_main + parser = FlexibleArgumentParser( + description="vLLM CLI", + epilog=VLLM_SUBCMD_PARSER_EPILOG.format(subcmd="[subcommand]"), + ) + parser.add_argument( + "-v", + "--version", + action="version", + version=importlib.metadata.version("vllm"), + ) + subparsers = parser.add_subparsers(required=False, dest="subparser") + cmds = {} + for cmd_module in CMD_MODULES: + new_cmds = cmd_module.cmd_init() + for cmd in new_cmds: + cmd.subparser_init(subparsers).set_defaults(dispatch_function=cmd.cmd) + cmds[cmd.name] = cmd + args = parser.parse_args() + if args.subparser in cmds: + cmds[args.subparser].validate(args) - logger.info("Delegating entrypoint handling to vllm-omni") - omni_main() + if hasattr(args, "dispatch_function"): + args.dispatch_function(args) else: - vllm.entrypoints.cli.benchmark.main.maybe_exec_rust_bench() - - # For 'vllm bench *': use CPU instead of UnspecifiedPlatform by default - if len(sys.argv) > 1 and sys.argv[1] == "bench": - logger.debug( - "Bench command detected, must ensure current platform is not " - "UnspecifiedPlatform to avoid device type inference error" - ) - from vllm import platforms - - if platforms.current_platform.is_unspecified(): - from vllm.platforms.cpu import CpuPlatform - - platforms.current_platform = CpuPlatform() - logger.info( - "Unspecified platform detected, switching to CPU Platform instead." - ) - - parser = FlexibleArgumentParser( - description="vLLM CLI", - epilog=VLLM_SUBCMD_PARSER_EPILOG.format(subcmd="[subcommand]"), - ) - parser.add_argument( - "-v", - "--version", - action="version", - version=importlib.metadata.version("vllm"), - ) - subparsers = parser.add_subparsers(required=False, dest="subparser") - cmds = {} - for cmd_module in CMD_MODULES: - new_cmds = cmd_module.cmd_init() - for cmd in new_cmds: - cmd.subparser_init(subparsers).set_defaults(dispatch_function=cmd.cmd) - cmds[cmd.name] = cmd - args = parser.parse_args() - if args.subparser in cmds: - cmds[args.subparser].validate(args) - - if hasattr(args, "dispatch_function"): - args.dispatch_function(args) - else: - parser.print_help() + parser.print_help() if __name__ == "__main__":