Skip to content

Commit e3e933f

Browse files
committed
revert unintended change
Signed-off-by: Shengliang Xu <shengliangx@nvidia.com>
1 parent 64aa23c commit e3e933f

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

modelopt/torch/quantization/config.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,10 @@ def normalize_quant_cfg_list(v: dict | list) -> list[QuantizerCfgEntry]:
15601560
- An empty entry ``{}``.
15611561
- An entry with only ``quantizer_name`` and no other keys — the only effect would be an
15621562
implicit ``enable=True``, which must be stated explicitly.
1563+
- An entry with ``enable=True`` (explicit or implicit) whose ``cfg`` is not a non-empty
1564+
``dict`` or ``list`` — e.g. ``{"quantizer_name": "*", "cfg": {}}`` or
1565+
``{"quantizer_name": "*", "cfg": 42}``. An enabled quantizer must have a valid
1566+
configuration.
15631567
15641568
**Normalization** — after conversion and validation every entry is put into canonical form:
15651569
@@ -1577,7 +1581,8 @@ def normalize_quant_cfg_list(v: dict | list) -> list[QuantizerCfgEntry]:
15771581
15781582
Raises:
15791583
ValueError: If any entry has only ``quantizer_name`` with neither ``cfg`` nor ``enable``,
1580-
or if the entry format is not recognized.
1584+
if ``enable=True`` with an empty or non-dict/list ``cfg``, or if the entry format
1585+
is not recognized.
15811586
"""
15821587

15831588
def _warn_legacy():
@@ -1662,6 +1667,28 @@ def _dict_to_entry(key: str, value) -> list[QuantizerCfgEntry]:
16621667
"enable=True is not allowed; set it explicitly)."
16631668
)
16641669

1670+
# Validate: when cfg is present and enable=True, cfg must be a non-empty
1671+
# dict or list. An empty cfg would attempt to create a
1672+
# QuantizerAttributeConfig with no actual configuration.
1673+
cfg = entry.get("cfg")
1674+
enable = entry.get("enable", True)
1675+
if enable and cfg is not None:
1676+
if isinstance(cfg, dict):
1677+
is_invalid = len(cfg) == 0
1678+
elif isinstance(cfg, list):
1679+
is_invalid = len(cfg) == 0 or any(
1680+
not isinstance(item, dict) or len(item) == 0 for item in cfg
1681+
)
1682+
else:
1683+
is_invalid = True
1684+
if is_invalid:
1685+
raise ValueError(
1686+
f"Invalid quant_cfg entry: {raw!r} — 'cfg' must be a non-empty dict "
1687+
f"or a non-empty list of non-empty dicts when enabling a quantizer "
1688+
f"(got {type(cfg).__name__}: {cfg!r}). Either provide quantizer "
1689+
"attributes in 'cfg' or remove 'cfg' and set 'enable' explicitly."
1690+
)
1691+
16651692
# Normalize: make enable and cfg always explicit.
16661693
entry.setdefault("enable", True)
16671694
entry.setdefault("cfg", None)

0 commit comments

Comments
 (0)