Handle optional bool-or-string CLI args in get_kwargs (#40951)
Signed-off-by: Christian Van <cvan20191@gmail.com> Co-authored-by: Christian Van <cvan20191@gmail.com>
This commit is contained in:
co-authored by
Christian Van
parent
1029e5ef28
commit
0d382ecde8
@@ -9,7 +9,7 @@ from typing import Annotated, Literal
|
||||
import pytest
|
||||
from pydantic import Field
|
||||
|
||||
from vllm.config import AttentionConfig, CompilationConfig, config
|
||||
from vllm.config import AttentionConfig, CompilationConfig, ModelConfig, config
|
||||
from vllm.engine.arg_utils import (
|
||||
EngineArgs,
|
||||
_expand_json_human_readable_numbers,
|
||||
@@ -116,6 +116,10 @@ class DummyConfig:
|
||||
"""Regular bool with default True"""
|
||||
optional_bool: bool | None = None
|
||||
"""Optional bool with default None"""
|
||||
|
||||
optional_bool_or_str: bool | str | None = None
|
||||
"""Optional bool-or-str with default None"""
|
||||
|
||||
optional_literal: Literal["x", "y"] | None = None
|
||||
"""Optional literal with default None"""
|
||||
tuple_n: tuple[int, ...] = Field(default_factory=lambda: (1, 2, 3))
|
||||
@@ -170,6 +174,11 @@ def test_get_kwargs():
|
||||
# bools should not have their type set
|
||||
assert kwargs["regular_bool"].get("type") is None
|
||||
assert kwargs["optional_bool"].get("type") is None
|
||||
# optional bool-or-str should accept an optional string value
|
||||
assert kwargs["optional_bool_or_str"]["type"] is str
|
||||
assert kwargs["optional_bool_or_str"]["nargs"] == "?"
|
||||
assert kwargs["optional_bool_or_str"]["const"] is True
|
||||
assert "action" not in kwargs["optional_bool_or_str"]
|
||||
# optional literals should have None as a choice
|
||||
assert kwargs["optional_literal"]["choices"] == ["x", "y", "None"]
|
||||
# tuples should have the correct nargs
|
||||
@@ -197,6 +206,32 @@ def test_get_kwargs():
|
||||
assert kwargs["nested_config"]["type"]('{"field": 2}') == NestedConfig(2) # type: ignore[call-arg]
|
||||
|
||||
|
||||
def test_hf_token_get_kwargs():
|
||||
kwargs = get_kwargs(ModelConfig)["hf_token"]
|
||||
|
||||
assert kwargs["type"] is str
|
||||
assert kwargs["nargs"] == "?"
|
||||
assert kwargs["const"] is True
|
||||
assert "action" not in kwargs
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("cli_args", "expected"),
|
||||
[
|
||||
([], None),
|
||||
(["--hf-token"], True),
|
||||
(["--hf-token", "hf_secret"], "hf_secret"),
|
||||
(["--hf-token", "None"], "None"),
|
||||
],
|
||||
)
|
||||
def test_hf_token_cli_arg(cli_args, expected):
|
||||
parser = EngineArgs.add_cli_args(FlexibleArgumentParser())
|
||||
|
||||
args = parser.parse_args(cli_args)
|
||||
|
||||
assert args.hf_token == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("arg", "expected"),
|
||||
[
|
||||
|
||||
@@ -335,6 +335,11 @@ def _compute_kwargs(cls: ConfigType) -> dict[str, dict[str, Any]]:
|
||||
kwargs[name]["type"] = parse_dataclass
|
||||
kwargs[name]["help"] += _maybe_add_docs_url(dataclass_cls)
|
||||
kwargs[name]["help"] += f"\n\n{json_tip}"
|
||||
elif type_hints == {bool, str, type(None)}:
|
||||
# Optional-valued flag: bare flag -> True, value -> str.
|
||||
kwargs[name]["type"] = str
|
||||
kwargs[name]["nargs"] = "?"
|
||||
kwargs[name]["const"] = True
|
||||
elif contains_type(type_hints, bool):
|
||||
# Creates --no-<name> and --<name> flags
|
||||
kwargs[name]["action"] = argparse.BooleanOptionalAction
|
||||
@@ -801,16 +806,7 @@ class EngineArgs:
|
||||
"--served-model-name", **model_kwargs["served_model_name"]
|
||||
)
|
||||
model_group.add_argument("--config-format", **model_kwargs["config_format"])
|
||||
# This one is a special case because it can bool
|
||||
# or str. TODO: Handle this in get_kwargs
|
||||
model_group.add_argument(
|
||||
"--hf-token",
|
||||
type=str,
|
||||
nargs="?",
|
||||
const=True,
|
||||
default=model_kwargs["hf_token"]["default"],
|
||||
help=model_kwargs["hf_token"]["help"],
|
||||
)
|
||||
model_group.add_argument("--hf-token", **model_kwargs["hf_token"])
|
||||
model_group.add_argument("--hf-overrides", **model_kwargs["hf_overrides"])
|
||||
model_group.add_argument("--pooler-config", **model_kwargs["pooler_config"])
|
||||
model_group.add_argument(
|
||||
|
||||
Reference in New Issue
Block a user