[UX] Reject incompatible nested runtime overrides (#49247)

Signed-off-by: Taneem Ibrahim <taneem.ibrahim@gmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Taneem Ibrahim
2026-07-24 07:16:22 -07:00
committed by GitHub
co-authored by mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent 8eac21a602
commit 7b40fb9645
4 changed files with 56 additions and 23 deletions
+18 -3
View File
@@ -406,26 +406,41 @@ class _TestNestedConfig:
a: _TestConfigFields = field(default_factory=lambda: _TestConfigFields(a=0))
@dataclass
class _TestDerivedConfigFields(_TestConfigFields):
pass
def test_update_config():
# Simple update
config1 = _TestConfigFields(a=0)
new_config1 = update_config(config1, {"a": 42})
assert new_config1.a == 42
# Nonexistent field
with pytest.raises(AssertionError):
with pytest.raises(ValueError, match=r"_TestConfigFields\.nonexistent"):
new_config1 = update_config(config1, {"nonexistent": 1})
# Nested update with dataclass
config2 = _TestNestedConfig()
new_inner_config = _TestConfigFields(a=1, c="new_value")
new_config2 = update_config(config2, {"a": new_inner_config})
assert new_config2.a == new_inner_config
# Declared field type, not the live value's subtype, defines valid overrides
config_with_derived = _TestNestedConfig(a=_TestDerivedConfigFields(a=0))
new_config2 = update_config(config_with_derived, {"a": new_inner_config})
assert new_config2.a is new_inner_config
# Nested update with unrelated dataclass
with pytest.raises(ValueError, match=r"_TestNestedConfig\.a"):
update_config(config2, {"a": _TestNestedConfig()})
# Nested update with dict
config3 = _TestNestedConfig()
new_config3 = update_config(config3, {"a": {"c": "new_value"}})
assert new_config3.a.c == "new_value"
# Nested update with invalid type
with pytest.raises(AssertionError):
new_config3 = update_config(config3, {"a": "new_value"})
with pytest.raises(ValueError, match=r"_TestNestedConfig\.a"):
update_config(config3, {"a": "new_value"})
# Invalid nested field preserves its full path
with pytest.raises(ValueError, match=r"_TestNestedConfig\.a\.nonexistent"):
update_config(config3, {"a": {"nonexistent": 1}})
@pytest.mark.parametrize(
+1 -1
View File
@@ -831,7 +831,7 @@ def test_update_config(model_runner):
model_runner.update_config({"load_config": {"load_format": "dummy"}})
assert model_runner.load_config.load_format == "dummy"
# Raise error on non-existing config
with pytest.raises(AssertionError):
with pytest.raises(ValueError, match="do_not_exist_config"):
model_runner.update_config({"do_not_exist_config": "dummy"})
+31 -15
View File
@@ -13,7 +13,7 @@ import textwrap
from collections.abc import Callable, Mapping, Sequence, Set
from dataclasses import MISSING, field, fields, is_dataclass
from itertools import pairwise
from typing import TYPE_CHECKING, Any, Protocol, TypeVar, cast, overload
from typing import TYPE_CHECKING, Any, Protocol, TypeVar, cast, get_type_hints, overload
import torch
from pydantic import ConfigDict
@@ -227,22 +227,38 @@ class SupportsMetricsInfo(Protocol):
def metrics_info(self) -> dict[str, str]: ...
def update_config(config: ConfigT, overrides: dict[str, Any]) -> ConfigT:
processed_overrides = {}
def update_config(config: ConfigT, overrides: Mapping[str, Any]) -> ConfigT:
return _update_config(config, overrides, type(config).__name__)
def _update_config(
config: ConfigT, overrides: Mapping[str, Any], config_path: str
) -> ConfigT:
processed_overrides: dict[str, Any] = {}
field_types = get_type_hints(type(config))
for field_name, value in overrides.items():
assert hasattr(config, field_name), (
f"{type(config)} has no field `{field_name}`"
)
field_path = f"{config_path}.{field_name}"
if not hasattr(config, field_name):
raise ValueError(f"{field_path} is not a valid config field")
current_value = getattr(config, field_name)
if is_dataclass(current_value) and not is_dataclass(value):
assert isinstance(value, dict), (
f"Overrides to {type(config)}.{field_name} must be a dict"
f" or {type(current_value)}, but got {type(value)}"
)
value = update_config(
current_value, # type: ignore[type-var]
value,
)
if is_dataclass(current_value):
expected_type = field_types[field_name]
if isinstance(value, Mapping):
value = _update_config(
current_value, # type: ignore[type-var]
value,
field_path,
)
elif not isinstance(value, expected_type):
expected_type_name = getattr(
expected_type, "__name__", str(expected_type)
)
raise ValueError(
f"Override for {field_path} must be a mapping or "
f"{expected_type_name}, got {type(value).__name__}"
)
processed_overrides[field_name] = value
return replace(config, **processed_overrides)
+6 -4
View File
@@ -5273,10 +5273,12 @@ class GPUModelRunner(
def update_config(self, overrides: dict[str, Any]) -> None:
allowed_config_names = {"load_config", "model_config"}
for config_name, config_overrides in overrides.items():
assert config_name in allowed_config_names, (
f"Config `{config_name}` not supported. "
f"Allowed configs: {allowed_config_names}"
)
if config_name not in allowed_config_names:
allowed = ", ".join(sorted(allowed_config_names))
raise ValueError(
f"Config override '{config_name}' is not supported. "
f"Supported configs: {allowed}"
)
config = getattr(self, config_name)
new_config = update_config(config, config_overrides)
setattr(self, config_name, new_config)