Improve --help for enhanced user experience (#24903)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
+9
-104
@@ -1,13 +1,11 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import dataclasses
|
||||
import functools
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from argparse import Namespace
|
||||
from typing import Any, Optional, Union
|
||||
|
||||
from fastapi import Request
|
||||
@@ -25,13 +23,10 @@ from vllm.utils import FlexibleArgumentParser
|
||||
logger = init_logger(__name__)
|
||||
|
||||
VLLM_SUBCMD_PARSER_EPILOG = (
|
||||
"Tip: Use `vllm [serve|run-batch|bench <bench_type>] "
|
||||
"--help=<keyword>` to explore arguments from help.\n"
|
||||
" - To view a argument group: --help=ModelConfig\n"
|
||||
" - To view a single argument: --help=max-num-seqs\n"
|
||||
" - To search by keyword: --help=max\n"
|
||||
" - To list all groups: --help=listgroup\n"
|
||||
" - To view help with pager: --help=page")
|
||||
"For full list: vllm {subcmd} --help=all\n"
|
||||
"For a section: vllm {subcmd} --help=ModelConfig (case-insensitive)\n" # noqa: E501
|
||||
"For a flag: vllm {subcmd} --help=max-model-len (_ or - accepted)\n" # noqa: E501
|
||||
"Documentation: https://docs.vllm.ai\n")
|
||||
|
||||
|
||||
async def listen_for_disconnect(request: Request) -> None:
|
||||
@@ -196,96 +191,6 @@ def _validate_truncation_size(
|
||||
return truncate_prompt_tokens
|
||||
|
||||
|
||||
def _output_with_pager(text: str):
|
||||
"""Output text using scrolling view if available and appropriate."""
|
||||
|
||||
pagers = ['less -R', 'more']
|
||||
for pager_cmd in pagers:
|
||||
try:
|
||||
proc = subprocess.Popen(pager_cmd.split(),
|
||||
stdin=subprocess.PIPE,
|
||||
text=True)
|
||||
proc.communicate(input=text)
|
||||
return
|
||||
except (subprocess.SubprocessError, OSError, FileNotFoundError):
|
||||
continue
|
||||
|
||||
# No pager worked, fall back to normal print
|
||||
print(text)
|
||||
|
||||
|
||||
def show_filtered_argument_or_group_from_help(parser: argparse.ArgumentParser,
|
||||
subcommand_name: list[str]):
|
||||
|
||||
# Only handle --help=<keyword> for the current subcommand.
|
||||
# Since subparser_init() runs for all subcommands during CLI setup,
|
||||
# we skip processing if the subcommand name is not in sys.argv.
|
||||
# sys.argv[0] is the program name. The subcommand follows.
|
||||
# e.g., for `vllm bench latency`,
|
||||
# sys.argv is `['vllm', 'bench', 'latency', ...]`
|
||||
# and subcommand_name is "bench latency".
|
||||
if len(sys.argv) <= len(subcommand_name) or sys.argv[
|
||||
1:1 + len(subcommand_name)] != subcommand_name:
|
||||
return
|
||||
|
||||
for arg in sys.argv:
|
||||
if arg.startswith('--help='):
|
||||
search_keyword = arg.split('=', 1)[1]
|
||||
|
||||
# Enable paged view for full help
|
||||
if search_keyword == 'page':
|
||||
help_text = parser.format_help()
|
||||
_output_with_pager(help_text)
|
||||
sys.exit(0)
|
||||
|
||||
# List available groups
|
||||
if search_keyword == 'listgroup':
|
||||
output_lines = ["\nAvailable argument groups:"]
|
||||
for group in parser._action_groups:
|
||||
if group.title and not group.title.startswith(
|
||||
"positional arguments"):
|
||||
output_lines.append(f" - {group.title}")
|
||||
if group.description:
|
||||
output_lines.append(" " +
|
||||
group.description.strip())
|
||||
output_lines.append("")
|
||||
_output_with_pager("\n".join(output_lines))
|
||||
sys.exit(0)
|
||||
|
||||
# For group search
|
||||
formatter = parser._get_formatter()
|
||||
for group in parser._action_groups:
|
||||
if group.title and group.title.lower() == search_keyword.lower(
|
||||
):
|
||||
formatter.start_section(group.title)
|
||||
formatter.add_text(group.description)
|
||||
formatter.add_arguments(group._group_actions)
|
||||
formatter.end_section()
|
||||
_output_with_pager(formatter.format_help())
|
||||
sys.exit(0)
|
||||
|
||||
# For single arg
|
||||
matched_actions = []
|
||||
|
||||
for group in parser._action_groups:
|
||||
for action in group._group_actions:
|
||||
# search option name
|
||||
if any(search_keyword.lower() in opt.lower()
|
||||
for opt in action.option_strings):
|
||||
matched_actions.append(action)
|
||||
|
||||
if matched_actions:
|
||||
header = f"\nParameters matching '{search_keyword}':\n"
|
||||
formatter = parser._get_formatter()
|
||||
formatter.add_arguments(matched_actions)
|
||||
_output_with_pager(header + formatter.format_help())
|
||||
sys.exit(0)
|
||||
|
||||
print(f"\nNo group or parameter matching '{search_keyword}'")
|
||||
print("Tip: use `--help=listgroup` to view all groups.")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def get_max_tokens(max_model_len: int, request: Union[ChatCompletionRequest,
|
||||
CompletionRequest],
|
||||
input_length: int, default_sampling_params: dict) -> int:
|
||||
@@ -301,11 +206,11 @@ def get_max_tokens(max_model_len: int, request: Union[ChatCompletionRequest,
|
||||
if val is not None)
|
||||
|
||||
|
||||
def log_non_default_args(args: Union[argparse.Namespace, EngineArgs]):
|
||||
def log_non_default_args(args: Union[Namespace, EngineArgs]):
|
||||
non_default_args = {}
|
||||
|
||||
# Handle argparse.Namespace
|
||||
if isinstance(args, argparse.Namespace):
|
||||
# Handle Namespace
|
||||
if isinstance(args, Namespace):
|
||||
parser = make_arg_parser(FlexibleArgumentParser())
|
||||
for arg, default in vars(parser.parse_args([])).items():
|
||||
if default != getattr(args, arg):
|
||||
@@ -323,6 +228,6 @@ def log_non_default_args(args: Union[argparse.Namespace, EngineArgs]):
|
||||
non_default_args["model"] = default_args.model
|
||||
else:
|
||||
raise TypeError("Unsupported argument type. " \
|
||||
"Must be argparse.Namespace or EngineArgs instance.")
|
||||
"Must be Namespace or EngineArgs instance.")
|
||||
|
||||
logger.info("non-default args: %s", non_default_args)
|
||||
|
||||
Reference in New Issue
Block a user