Skip to content

Commit 977d60a

Browse files
Merge remote-tracking branch 'origin/main' into feature/puzzletron
2 parents ac8397b + 9050188 commit 977d60a

File tree

5 files changed

+115
-4
lines changed

5 files changed

+115
-4
lines changed

examples/speculative_decoding/collect_hidden_states/compute_hidden_states_hf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ def keep_conversation(entry):
142142
tokenizer = AutoTokenizer.from_pretrained(args.model, trust_remote_code=args.trust_remote_code)
143143
if tokenizer.pad_token is None:
144144
tokenizer.pad_token = tokenizer.eos_token
145-
tokenizer.chat_template = tokenizer.chat_template.replace(REMOVE_THINK_CHAT_TEMPLATE, "")
145+
if tokenizer.chat_template is not None:
146+
tokenizer.chat_template = tokenizer.chat_template.replace(REMOVE_THINK_CHAT_TEMPLATE, "")
146147

147148
output_dir = args.output_dir
148149
output_dir.mkdir(parents=True, exist_ok=True)

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)

tests/examples/speculative_decoding/conftest.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,38 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import json
17+
1618
import pytest
1719
import yaml
1820
from _test_utils.examples.run_command import run_example_command
1921

2022

23+
@pytest.fixture(scope="session")
24+
def tiny_conversations_path(tmp_path_factory):
25+
"""Tiny JSONL with short synthetic conversations for compute_hidden_states_hf tests.
26+
27+
Uses minimal single-turn conversations so that tokenized lengths stay well
28+
within the tiny test model's max_position_embeddings (32) even after chat
29+
template formatting.
30+
"""
31+
tmp_dir = tmp_path_factory.mktemp("tiny_convs")
32+
output_file = tmp_dir / "train.jsonl"
33+
conversations = [
34+
{
35+
"conversation_id": f"test-{i}",
36+
"conversations": [
37+
{"role": "user", "content": "What is 2 plus 2?"},
38+
{"role": "assistant", "content": "4"},
39+
],
40+
}
41+
for i in range(5)
42+
]
43+
with open(output_file, "w") as f:
44+
f.writelines(json.dumps(conv) + "\n" for conv in conversations)
45+
return output_file
46+
47+
2148
@pytest.fixture(scope="session", autouse=True)
2249
def tiny_daring_anteater_path(tmp_path_factory):
2350
tmp_dir = tmp_path_factory.mktemp("daring_anteater")

tests/examples/speculative_decoding/test_eagle_offline_ptq.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def offline_ptq_dirs(tmp_path_factory):
5555
}
5656

5757

58-
def test_collect_hidden_states(tiny_llama_path, tiny_daring_anteater_path, offline_ptq_dirs):
58+
def test_collect_hidden_states(tiny_llama_path, tiny_conversations_path, offline_ptq_dirs):
5959
"""Stage 1: generate .pt hidden state files from the base model."""
6060
run_example_command(
6161
[
@@ -64,11 +64,13 @@ def test_collect_hidden_states(tiny_llama_path, tiny_daring_anteater_path, offli
6464
"--model",
6565
tiny_llama_path,
6666
"--input-data",
67-
str(tiny_daring_anteater_path),
67+
str(tiny_conversations_path),
6868
"--output-dir",
6969
str(offline_ptq_dirs["hidden_states"]),
7070
"--debug-max-num-conversations",
7171
"2",
72+
"--max-seq-len",
73+
"32",
7274
],
7375
"speculative_decoding",
7476
)

tests/unit/torch/quantization/test_config_validation.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,60 @@ def test_error_on_multi_key_legacy_dict(self):
163163
with pytest.raises(ValueError):
164164
normalize_quant_cfg_list([{"*weight_quantizer": {}, "*input_quantizer": {}}])
165165

166+
def test_error_on_empty_cfg_dict_implicit_enable(self):
167+
"""Entry with cfg={} and implicit enable=True is rejected."""
168+
with pytest.raises(ValueError, match="non-empty dict"):
169+
normalize_quant_cfg_list([{"quantizer_name": "*weight_quantizer", "cfg": {}}])
170+
171+
def test_error_on_empty_cfg_dict_explicit_enable_true(self):
172+
"""Entry with cfg={} and explicit enable=True is rejected."""
173+
with pytest.raises(ValueError, match="non-empty dict"):
174+
normalize_quant_cfg_list(
175+
[{"quantizer_name": "*weight_quantizer", "cfg": {}, "enable": True}]
176+
)
177+
178+
def test_error_on_empty_cfg_list_enable_true(self):
179+
"""Entry with cfg=[] and enable=True is rejected."""
180+
with pytest.raises(ValueError, match="non-empty dict"):
181+
normalize_quant_cfg_list(
182+
[{"quantizer_name": "*weight_quantizer", "cfg": [], "enable": True}]
183+
)
184+
185+
def test_error_on_non_dict_non_list_cfg_enable_true(self):
186+
"""Entry with cfg of invalid type (e.g. int) and enable=True is rejected."""
187+
with pytest.raises(ValueError, match="non-empty dict"):
188+
normalize_quant_cfg_list(
189+
[{"quantizer_name": "*weight_quantizer", "cfg": 42, "enable": True}]
190+
)
191+
192+
def test_error_on_cfg_list_with_empty_dict_enable_true(self):
193+
"""Entry with cfg=[{}] and enable=True is rejected (empty dict element)."""
194+
with pytest.raises(ValueError, match="non-empty dict"):
195+
normalize_quant_cfg_list(
196+
[{"quantizer_name": "*weight_quantizer", "cfg": [{}], "enable": True}]
197+
)
198+
199+
def test_error_on_cfg_list_with_non_dict_element_enable_true(self):
200+
"""Entry with cfg=[42] and enable=True is rejected (non-dict element)."""
201+
with pytest.raises(ValueError, match="non-empty dict"):
202+
normalize_quant_cfg_list(
203+
[{"quantizer_name": "*weight_quantizer", "cfg": [42], "enable": True}]
204+
)
205+
206+
def test_empty_cfg_dict_enable_false_accepted(self):
207+
"""Entry with cfg={} and enable=False is allowed (disable-only entry)."""
208+
result = normalize_quant_cfg_list(
209+
[{"quantizer_name": "*input_quantizer", "cfg": {}, "enable": False}]
210+
)
211+
assert result[0]["enable"] is False
212+
213+
def test_empty_cfg_list_enable_false_accepted(self):
214+
"""Entry with cfg=[] and enable=False is allowed (disable-only entry)."""
215+
result = normalize_quant_cfg_list(
216+
[{"quantizer_name": "*input_quantizer", "cfg": [], "enable": False}]
217+
)
218+
assert result[0]["enable"] is False
219+
166220
def test_new_format_with_list_cfg(self):
167221
"""cfg can be a list of dicts for SequentialQuantizer."""
168222
raw = [

0 commit comments

Comments
 (0)