[Bugfix] Support append/extend/count actions in TrackingArgumentParser (#4639)
Signed-off-by: Junhee Yoo <junhee.yoo@navercorp.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
1b0de21c55
commit
9d8089095e
2 files changed
+157
-3
No files matched your search
@@ -9,6 +9,29 @@ from vllm.utils.argparse_utils import FlexibleArgumentParser
|
||||
UNSET = object()
|
||||
|
||||
|
||||
def build_shadow_kwargs(kwargs: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Build kwargs for the shadow argument with an ``UNSET`` default.
|
||||
|
||||
Actions that mutate their default in place (append/extend/append_const/
|
||||
count) would crash on the bare ``UNSET`` sentinel, so they are remapped to
|
||||
an equivalent store-style action; the shadow value only needs to flip away
|
||||
from ``UNSET`` when the arg is passed explicitly.
|
||||
"""
|
||||
shadow_kwargs = {**kwargs, "default": UNSET}
|
||||
action = kwargs.get("action")
|
||||
|
||||
if action in ("append", "extend"):
|
||||
shadow_kwargs["action"] = "store"
|
||||
|
||||
elif action in ("append_const", "count"):
|
||||
shadow_kwargs["action"] = "store_const"
|
||||
|
||||
if action == "count":
|
||||
shadow_kwargs["const"] = True
|
||||
|
||||
return shadow_kwargs
|
||||
|
||||
|
||||
class TrackingNamespace(argparse.Namespace):
|
||||
"""Proxy that wraps an argparse namespace with explicit keys, which
|
||||
can be filtered down to a dict containing only explicitly passed values.
|
||||
@@ -58,7 +81,7 @@ class TrackingGroup:
|
||||
def add_argument(self, *args: Any, **kwargs: Any) -> argparse.Action:
|
||||
"""Add an argument to the real group and to the shadow group."""
|
||||
action = self._real.add_argument(*args, **kwargs)
|
||||
default_kwargs = {**kwargs, "default": UNSET}
|
||||
default_kwargs = build_shadow_kwargs(kwargs)
|
||||
self._shadow.add_argument(*args, **default_kwargs)
|
||||
return action
|
||||
|
||||
@@ -115,7 +138,7 @@ class TrackingArgumentParser(FlexibleArgumentParser):
|
||||
def add_argument(self, *args: Any, **kwargs: Any) -> argparse.Action:
|
||||
"""Add an arg to the parser & the shadow, where the latter has UNSET for the default."""
|
||||
action = super().add_argument(*args, **kwargs)
|
||||
shadow_kwargs = {**kwargs, "default": UNSET}
|
||||
shadow_kwargs = build_shadow_kwargs(kwargs)
|
||||
self._shadow.add_argument(*args, **shadow_kwargs)
|
||||
return action
|
||||
|
||||
|
||||
Reference in new issue
Block a user