Skip to content

Commit 3e9db5c

Browse files
committed
refactor: mark use_sequential deprecated/frozen instead of key-rename validator
Per reviewer feedback on #1251, re-introduce use_sequential as a real field marked deprecated and frozen. Pydantic emits DeprecationWarning on use and blocks post-construction reassignment; a model_validator(mode='after') copies the legacy value into layerwise when layerwise was not set. Replaces the mode='before' key-rename validator added in b4c6a03. Signed-off-by: realAsma <akuriparambi@nvidia.com>
1 parent b4c6a03 commit 3e9db5c

2 files changed

Lines changed: 23 additions & 18 deletions

File tree

modelopt/torch/quantization/config.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
import warnings
155155
from typing import Any, Literal, cast
156156

157-
from pydantic import ValidationInfo, field_validator, model_validator
157+
from pydantic import Field, ValidationInfo, field_validator, model_validator
158158
from typing_extensions import Required, TypedDict
159159

160160
from modelopt.torch.opt.config import ModeloptBaseConfig, ModeloptField
@@ -1227,6 +1227,12 @@ class QuantizeAlgorithmConfig(ModeloptBaseConfig):
12271227
),
12281228
)
12291229

1230+
use_sequential: bool | None = Field(
1231+
default=None,
1232+
deprecated="Use 'layerwise' instead.",
1233+
frozen=True,
1234+
)
1235+
12301236
layerwise_checkpoint_dir: str | None = ModeloptField(
12311237
default=None,
12321238
title="Checkpoint directory for layerwise calibration.",
@@ -1237,20 +1243,11 @@ class QuantizeAlgorithmConfig(ModeloptBaseConfig):
12371243
),
12381244
)
12391245

1240-
@model_validator(mode="before")
1241-
@classmethod
1242-
def _rename_legacy_fields(cls, values):
1243-
"""Rewrite renamed fields to their current names for backward compatibility.
1244-
1245-
Preserves the stored value so checkpoints saved before a field was renamed
1246-
continue to load. Explicit values on the new name take precedence.
1247-
"""
1248-
legacy_to_current = {"use_sequential": "layerwise"}
1249-
if isinstance(values, dict):
1250-
for legacy, current in legacy_to_current.items():
1251-
if legacy in values:
1252-
values.setdefault(current, values.pop(legacy))
1253-
return values
1246+
@model_validator(mode="after")
1247+
def _migrate_use_sequential(self):
1248+
if self.use_sequential is not None and not self.layerwise:
1249+
object.__setattr__(self, "layerwise", self.use_sequential)
1250+
return self
12541251

12551252
@model_validator(mode="after")
12561253
def validate_layerwise_checkpoint_dir(self):

tests/unit/torch/quantization/test_config_validation.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,13 +532,21 @@ class TestLegacyFieldRename:
532532
"""Old PTQ checkpoints saved the field as ``use_sequential`` before #1251."""
533533

534534
def test_legacy_use_sequential_true_maps_to_layerwise(self):
535-
cfg = MaxCalibConfig(use_sequential=True)
535+
with pytest.warns(DeprecationWarning):
536+
cfg = MaxCalibConfig(use_sequential=True)
536537
assert cfg.layerwise is True
537538

538539
def test_legacy_use_sequential_false_maps_to_layerwise(self):
539-
cfg = MaxCalibConfig(use_sequential=False)
540+
with pytest.warns(DeprecationWarning):
541+
cfg = MaxCalibConfig(use_sequential=False)
540542
assert cfg.layerwise is False
541543

542544
def test_explicit_layerwise_wins_over_legacy(self):
543-
cfg = MaxCalibConfig(use_sequential=False, layerwise=True)
545+
with pytest.warns(DeprecationWarning):
546+
cfg = MaxCalibConfig(use_sequential=False, layerwise=True)
544547
assert cfg.layerwise is True
548+
549+
def test_use_sequential_is_frozen(self):
550+
cfg = MaxCalibConfig()
551+
with pytest.raises(ValidationError):
552+
cfg.use_sequential = True

0 commit comments

Comments
 (0)