Skip to content

Commit 476844d

Browse files
authored
Fix underscores in dict keys passed via CLI (#19030)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
1 parent 4e68ae5 commit 476844d

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

tests/test_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,18 @@ def test_dict_args(parser):
259259
"--model-name=something.something",
260260
"--hf-overrides.key1",
261261
"val1",
262+
# Test nesting
262263
"--hf-overrides.key2.key3",
263264
"val2",
264265
"--hf-overrides.key2.key4",
265266
"val3",
267+
# Test = sign
266268
"--hf-overrides.key5=val4",
269+
# Test underscore to dash conversion
270+
"--hf_overrides.key_6",
271+
"val5",
272+
"--hf_overrides.key-7.key_8",
273+
"val6",
267274
]
268275
parsed_args = parser.parse_args(args)
269276
assert parsed_args.model_name == "something.something"
@@ -274,6 +281,10 @@ def test_dict_args(parser):
274281
"key4": "val3",
275282
},
276283
"key5": "val4",
284+
"key_6": "val5",
285+
"key-7": {
286+
"key_8": "val6",
287+
},
277288
}
278289

279290

vllm/utils.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,17 +1456,24 @@ def parse_args( # type: ignore[override]
14561456
if '--config' in args:
14571457
args = self._pull_args_from_config(args)
14581458

1459+
def repl(match: re.Match) -> str:
1460+
"""Replaces underscores with dashes in the matched string."""
1461+
return match.group(0).replace("_", "-")
1462+
1463+
# Everything between the first -- and the first .
1464+
pattern = re.compile(r"(?<=--)[^\.]*")
1465+
14591466
# Convert underscores to dashes and vice versa in argument names
14601467
processed_args = []
14611468
for arg in args:
14621469
if arg.startswith('--'):
14631470
if '=' in arg:
14641471
key, value = arg.split('=', 1)
1465-
key = '--' + key[len('--'):].replace('_', '-')
1472+
key = pattern.sub(repl, key, count=1)
14661473
processed_args.append(f'{key}={value}')
14671474
else:
1468-
processed_args.append('--' +
1469-
arg[len('--'):].replace('_', '-'))
1475+
key = pattern.sub(repl, arg, count=1)
1476+
processed_args.append(key)
14701477
elif arg.startswith('-O') and arg != '-O' and len(arg) == 2:
14711478
# allow -O flag to be used without space, e.g. -O3
14721479
processed_args.append('-O')

0 commit comments

Comments
 (0)