-
Notifications
You must be signed in to change notification settings - Fork 400
Expand file tree
/
Copy pathtest_config_validation.py
More file actions
612 lines (531 loc) · 25.1 KB
/
test_config_validation.py
File metadata and controls
612 lines (531 loc) · 25.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Test of quantization config validations."""
import pytest
from pydantic import ValidationError
from modelopt.torch.quantization.config import (
FP8_2D_BLOCKWISE_WEIGHT_ONLY_CFG,
FP8_DEFAULT_CFG,
FP8_PER_CHANNEL_PER_TOKEN_CFG,
INT4_AWQ_CFG,
NVFP4_DEFAULT_CFG,
W4A8_AWQ_BETA_CFG,
QuantizeConfig,
find_quant_cfg_entry_by_path,
need_calibration,
normalize_quant_cfg_list,
)
def test_need_calibration():
assert need_calibration(FP8_DEFAULT_CFG)
assert not need_calibration(FP8_PER_CHANNEL_PER_TOKEN_CFG)
assert not need_calibration(FP8_2D_BLOCKWISE_WEIGHT_ONLY_CFG)
assert need_calibration(INT4_AWQ_CFG)
assert need_calibration(W4A8_AWQ_BETA_CFG)
assert need_calibration(NVFP4_DEFAULT_CFG)
def test_need_calibration_with_list_cfg():
"""need_calibration must handle sequential (list) cfg entries without crashing."""
# Static list-cfg on a non-weight quantizer → needs calibration
cfg_static = {
"quant_cfg": [
{
"quantizer_name": "*input_quantizer",
"cfg": [
{"num_bits": 4, "block_sizes": {-1: 128, "type": "static"}},
{"num_bits": (4, 3)},
],
"enable": True,
},
],
"algorithm": "max",
}
assert need_calibration(cfg_static)
# Dynamic list-cfg on a non-weight quantizer → no calibration needed
cfg_dynamic = {
"quant_cfg": [
{
"quantizer_name": "*input_quantizer",
"cfg": [{"num_bits": (4, 3), "type": "dynamic"}],
"enable": True,
},
],
"algorithm": "max",
}
assert not need_calibration(cfg_dynamic)
class TestNormalizeQuantCfgList:
def test_new_format_passthrough(self):
"""New-format entries are returned unchanged (only canonical defaults added)."""
raw = [{"quantizer_name": "*weight_quantizer", "cfg": {"num_bits": 8, "axis": 0}}]
result = normalize_quant_cfg_list(raw)
assert len(result) == 1
assert result[0]["quantizer_name"] == "*weight_quantizer"
assert result[0]["cfg"] == {"num_bits": 8, "axis": 0}
assert result[0]["enable"] is True # defaulted
def test_new_format_enable_false(self):
"""Explicit enable=False is preserved."""
raw = [{"quantizer_name": "*", "enable": False}]
result = normalize_quant_cfg_list(raw)
assert result[0]["enable"] is False
assert result[0]["cfg"] is None # defaulted
def test_new_format_explicit_enable_true_no_cfg(self):
"""Explicit enable=True with no cfg is valid and cfg defaults to None."""
raw = [{"quantizer_name": "*", "enable": True}]
result = normalize_quant_cfg_list(raw)
assert result[0]["enable"] is True
assert result[0]["cfg"] is None
def test_legacy_single_key_dict(self):
"""Legacy {'*path': {attrs}} is converted to new format."""
raw = [{"*weight_quantizer": {"num_bits": 8, "axis": 0}}]
result = normalize_quant_cfg_list(raw)
assert result[0]["quantizer_name"] == "*weight_quantizer"
assert result[0]["cfg"] == {"num_bits": 8, "axis": 0}
assert result[0]["enable"] is True # defaulted
def test_legacy_single_key_dict_with_enable(self):
"""Legacy {'*path': {'enable': False}} splits enable out from cfg."""
raw = [{"*input_quantizer": {"enable": False}}]
result = normalize_quant_cfg_list(raw)
assert result[0]["quantizer_name"] == "*input_quantizer"
assert result[0]["enable"] is False
assert result[0]["cfg"] is None
def test_legacy_nn_class_scoped(self):
"""Legacy {'nn.Linear': {'*': {attrs}}} is converted with parent_class."""
raw = [{"nn.Linear": {"*": {"enable": False}}}]
result = normalize_quant_cfg_list(raw)
assert result[0]["parent_class"] == "nn.Linear"
assert result[0]["quantizer_name"] == "*"
assert result[0]["enable"] is False
def test_normalization_cfg_defaults_to_none(self):
"""Entries without cfg get cfg=None after normalization."""
raw = [{"quantizer_name": "*lm_head*", "enable": False}]
result = normalize_quant_cfg_list(raw)
assert "cfg" in result[0]
assert result[0]["cfg"] is None
def test_normalization_enable_defaults_to_true(self):
"""Entries with cfg but no enable get enable=True after normalization."""
raw = [{"quantizer_name": "*", "cfg": {"num_bits": 4}}]
result = normalize_quant_cfg_list(raw)
assert result[0]["enable"] is True
def test_empty_list(self):
"""Empty list is returned unchanged."""
assert normalize_quant_cfg_list([]) == []
def test_multiple_entries_order_preserved(self):
"""The order of entries is preserved."""
raw = [
{"quantizer_name": "*", "enable": False},
{"quantizer_name": "*weight_quantizer", "cfg": {"num_bits": 8}},
]
result = normalize_quant_cfg_list(raw)
assert result[0]["quantizer_name"] == "*"
assert result[1]["quantizer_name"] == "*weight_quantizer"
def test_error_on_quantizer_name_only(self):
"""Entry with only quantizer_name and no cfg or enable is rejected."""
with pytest.raises(ValueError, match="must specify 'cfg', 'enable'"):
normalize_quant_cfg_list([{"quantizer_name": "*"}])
def test_error_on_empty_dict(self):
"""An empty dict entry is rejected."""
with pytest.raises(ValueError):
normalize_quant_cfg_list([{}])
def test_error_on_multi_key_legacy_dict(self):
"""A multi-key legacy dict (no quantizer_name, no nn.* keys) is rejected."""
with pytest.raises(ValueError):
normalize_quant_cfg_list([{"*weight_quantizer": {}, "*input_quantizer": {}}])
def test_error_on_empty_cfg_dict_implicit_enable(self):
"""Entry with cfg={} and implicit enable=True is rejected."""
with pytest.raises(ValueError, match="non-empty dict"):
normalize_quant_cfg_list([{"quantizer_name": "*weight_quantizer", "cfg": {}}])
def test_error_on_empty_cfg_dict_explicit_enable_true(self):
"""Entry with cfg={} and explicit enable=True is rejected."""
with pytest.raises(ValueError, match="non-empty dict"):
normalize_quant_cfg_list(
[{"quantizer_name": "*weight_quantizer", "cfg": {}, "enable": True}]
)
def test_error_on_empty_cfg_list_enable_true(self):
"""Entry with cfg=[] and enable=True is rejected."""
with pytest.raises(ValueError, match="non-empty dict"):
normalize_quant_cfg_list(
[{"quantizer_name": "*weight_quantizer", "cfg": [], "enable": True}]
)
def test_error_on_non_dict_non_list_cfg_enable_true(self):
"""Entry with cfg of invalid type (e.g. int) and enable=True is rejected."""
with pytest.raises(ValueError, match="non-empty dict"):
normalize_quant_cfg_list(
[{"quantizer_name": "*weight_quantizer", "cfg": 42, "enable": True}]
)
def test_error_on_cfg_list_with_empty_dict_enable_true(self):
"""Entry with cfg=[{}] and enable=True is rejected (empty dict element)."""
with pytest.raises(ValueError, match="non-empty dict"):
normalize_quant_cfg_list(
[{"quantizer_name": "*weight_quantizer", "cfg": [{}], "enable": True}]
)
def test_error_on_cfg_list_with_non_dict_element_enable_true(self):
"""Entry with cfg=[42] and enable=True is rejected (non-dict element)."""
with pytest.raises(ValueError, match="non-empty dict"):
normalize_quant_cfg_list(
[{"quantizer_name": "*weight_quantizer", "cfg": [42], "enable": True}]
)
def test_empty_cfg_dict_enable_false_accepted(self):
"""Entry with cfg={} and enable=False is allowed (disable-only entry)."""
result = normalize_quant_cfg_list(
[{"quantizer_name": "*input_quantizer", "cfg": {}, "enable": False}]
)
assert result[0]["enable"] is False
def test_empty_cfg_list_enable_false_accepted(self):
"""Entry with cfg=[] and enable=False is allowed (disable-only entry)."""
result = normalize_quant_cfg_list(
[{"quantizer_name": "*input_quantizer", "cfg": [], "enable": False}]
)
assert result[0]["enable"] is False
def test_new_format_with_list_cfg(self):
"""cfg can be a list of dicts for SequentialQuantizer."""
raw = [
{
"quantizer_name": "*weight_quantizer",
"cfg": [
{"num_bits": 4, "block_sizes": {-1: 128, "type": "static"}},
{"num_bits": (4, 3)},
],
}
]
result = normalize_quant_cfg_list(raw)
assert len(result) == 1
assert result[0]["cfg"] == raw[0]["cfg"]
assert result[0]["enable"] is True
def test_legacy_flat_dict_conversion(self):
"""Legacy flat dict {'*': {...}, '*weight_quantizer': {...}} is converted to list."""
raw = {"*": {"enable": False}, "*weight_quantizer": {"num_bits": 8, "axis": 0}}
result = normalize_quant_cfg_list(raw)
assert len(result) == 2
assert result[0]["quantizer_name"] == "*"
assert result[0]["enable"] is False
assert result[0]["cfg"] is None
assert result[1]["quantizer_name"] == "*weight_quantizer"
assert result[1]["cfg"] == {"num_bits": 8, "axis": 0}
assert result[1]["enable"] is True
def test_legacy_enable_only_produces_cfg_none(self):
"""Legacy {'*': {'enable': False}} should produce cfg=None, not cfg={}."""
raw = [{"*": {"enable": False}}]
result = normalize_quant_cfg_list(raw)
assert result[0]["cfg"] is None
assert result[0]["enable"] is False
def test_legacy_nn_class_enable_only_produces_cfg_none(self):
"""Legacy nn.* scoped format with only enable produces cfg=None."""
raw = [{"nn.Linear": {"*": {"enable": False}}}]
result = normalize_quant_cfg_list(raw)
assert result[0]["cfg"] is None
assert result[0]["enable"] is False
assert result[0]["parent_class"] == "nn.Linear"
def test_legacy_default_key(self):
"""Legacy 'default' key is converted to quantizer_name='*'."""
raw = [{"default": {"enable": False}}]
result = normalize_quant_cfg_list(raw)
assert result[0]["quantizer_name"] == "*"
assert result[0]["enable"] is False
assert result[0]["cfg"] is None
def test_legacy_default_key_with_cfg(self):
"""Legacy 'default' key with cfg attributes maps to '*'."""
raw = [{"default": {"num_bits": 8, "axis": None}}]
result = normalize_quant_cfg_list(raw)
assert result[0]["quantizer_name"] == "*"
assert result[0]["cfg"] == {"num_bits": 8, "axis": None}
assert result[0]["enable"] is True
def test_legacy_flat_dict_with_default_key(self):
"""Legacy flat dict containing 'default' key converts it to '*'."""
raw = {"default": {"enable": False}, "*weight_quantizer": {"num_bits": 8}}
result = normalize_quant_cfg_list(raw)
default_entries = [e for e in result if e["quantizer_name"] == "*"]
assert len(default_entries) == 1
assert default_entries[0]["enable"] is False
def test_legacy_nn_class_multi_key(self):
"""Legacy nn.* scoped format with multiple sub-keys produces multiple entries."""
raw = [
{
"nn.Linear": {
"*input_quantizer": {"enable": False},
"*weight_quantizer": {"num_bits": 4},
}
}
]
result = normalize_quant_cfg_list(raw)
assert len(result) == 2
paths = {e["quantizer_name"] for e in result}
assert paths == {"*input_quantizer", "*weight_quantizer"}
for e in result:
assert e["parent_class"] == "nn.Linear"
def test_legacy_nn_class_with_cfg(self):
"""Legacy nn.* scoped format with actual quantizer attributes (not just enable)."""
raw = [{"nn.Linear": {"*weight_quantizer": {"num_bits": 4, "axis": 0}}}]
result = normalize_quant_cfg_list(raw)
assert len(result) == 1
assert result[0]["parent_class"] == "nn.Linear"
assert result[0]["quantizer_name"] == "*weight_quantizer"
assert result[0]["cfg"] == {"num_bits": 4, "axis": 0}
assert result[0]["enable"] is True
def test_legacy_list_valued_cfg(self):
"""Legacy dict format with list-valued cfg (SequentialQuantizer) normalizes correctly."""
raw = [
{
"*weight_quantizer": [
{"num_bits": 4, "block_sizes": {-1: 128, "type": "static"}},
{"num_bits": 8, "axis": 0},
]
}
]
result = normalize_quant_cfg_list(raw)
assert len(result) == 1
assert result[0]["quantizer_name"] == "*weight_quantizer"
assert isinstance(result[0]["cfg"], list)
assert len(result[0]["cfg"]) == 2
assert result[0]["cfg"][0]["num_bits"] == 4
assert result[0]["cfg"][1]["num_bits"] == 8
assert result[0]["enable"] is True
class TestFindQuantCfgEntry:
def test_finds_last_match(self):
"""When multiple entries share the same quantizer_name, returns the last one."""
entries = normalize_quant_cfg_list(
[
{"quantizer_name": "*weight_quantizer", "cfg": {"num_bits": 8}},
{"quantizer_name": "*input_quantizer", "cfg": {"num_bits": 4}},
{"quantizer_name": "*weight_quantizer", "cfg": {"num_bits": 4}},
]
)
result = find_quant_cfg_entry_by_path(entries, "*weight_quantizer")
assert result["cfg"] == {"num_bits": 4}
def test_exact_match_only(self):
"""Does not do fnmatch — only exact string equality on quantizer_name."""
entries = normalize_quant_cfg_list(
[{"quantizer_name": "*weight_quantizer", "cfg": {"num_bits": 8}}]
)
with pytest.raises(KeyError):
find_quant_cfg_entry_by_path(entries, "model.layer.weight_quantizer")
def test_raises_on_missing(self):
"""Raises KeyError when no entry matches."""
entries = normalize_quant_cfg_list(
[{"quantizer_name": "*weight_quantizer", "cfg": {"num_bits": 8}}]
)
with pytest.raises(KeyError):
find_quant_cfg_entry_by_path(entries, "*input_quantizer")
def test_single_entry(self):
entries = normalize_quant_cfg_list([{"quantizer_name": "*", "enable": False}])
result = find_quant_cfg_entry_by_path(entries, "*")
assert result["enable"] is False
def test_empty_list(self):
with pytest.raises(KeyError):
find_quant_cfg_entry_by_path([], "*")
def test_need_calibration_with_legacy_dict_format():
"""need_calibration should accept legacy dict-format quant_cfg without crashing."""
legacy_config = {
"quant_cfg": {"*input_quantizer": {"num_bits": 8, "axis": None}},
"algorithm": "max",
}
assert need_calibration(legacy_config)
def test_need_calibration_with_legacy_list_of_single_key_dicts():
"""need_calibration should accept legacy list-of-single-key-dicts format."""
legacy_config = {
"quant_cfg": [{"*input_quantizer": {"num_bits": 8, "axis": None}}],
"algorithm": "max",
}
assert need_calibration(legacy_config)
class TestMatchQuantizerCfg:
"""Tests for _match_quantizer_cfg in algorithms.py."""
def test_wildcard_matches_bare_name(self):
"""'*weight_quantizer' matches bare 'weight_quantizer'."""
from modelopt.torch.quantization.algorithms import _match_quantizer_cfg
quant_cfg = normalize_quant_cfg_list(
[{"quantizer_name": "*weight_quantizer", "cfg": {"num_bits": 8}}]
)
matched, enable = _match_quantizer_cfg(quant_cfg, "weight_quantizer")
assert matched == {"num_bits": 8}
assert enable is True
def test_star_matches_any_bare_name(self):
"""'*' matches any bare quantizer name."""
from modelopt.torch.quantization.algorithms import _match_quantizer_cfg
quant_cfg = normalize_quant_cfg_list([{"quantizer_name": "*", "enable": False}])
matched, enable = _match_quantizer_cfg(quant_cfg, "weight_quantizer")
assert matched is None # enable-only entry has cfg=None
assert enable is False
def test_path_scoped_pattern_matches_matching_suffix(self):
"""'*mlp*weight_quantizer' matches bare 'weight_quantizer' (suffix match)."""
from modelopt.torch.quantization.algorithms import _match_quantizer_cfg
quant_cfg = normalize_quant_cfg_list(
[{"quantizer_name": "*mlp*weight_quantizer", "cfg": {"num_bits": 4}}]
)
matched, enable = _match_quantizer_cfg(quant_cfg, "weight_quantizer")
assert matched == {"num_bits": 4}
def test_path_scoped_pattern_does_not_match_different_suffix(self):
"""'*mlp*weight_quantizer' does NOT match bare 'input_quantizer'."""
from modelopt.torch.quantization.algorithms import _match_quantizer_cfg
quant_cfg = normalize_quant_cfg_list(
[{"quantizer_name": "*mlp*weight_quantizer", "cfg": {"num_bits": 4}}]
)
matched, enable = _match_quantizer_cfg(quant_cfg, "input_quantizer")
assert matched is None
assert enable is None
def test_last_match_wins(self):
"""Later entries override earlier ones."""
from modelopt.torch.quantization.algorithms import _match_quantizer_cfg
quant_cfg = normalize_quant_cfg_list(
[
{"quantizer_name": "*weight_quantizer", "cfg": {"num_bits": 8}},
{"quantizer_name": "*weight_quantizer", "cfg": {"num_bits": 4}},
]
)
matched, _ = _match_quantizer_cfg(quant_cfg, "weight_quantizer")
assert matched == {"num_bits": 4}
def test_no_match_returns_none(self):
"""No matching entry returns (None, None)."""
from modelopt.torch.quantization.algorithms import _match_quantizer_cfg
quant_cfg = normalize_quant_cfg_list(
[{"quantizer_name": "*weight_quantizer", "cfg": {"num_bits": 8}}]
)
matched, enable = _match_quantizer_cfg(quant_cfg, "output_quantizer")
assert matched is None
assert enable is None
def test_bracket_pattern_matches_correctly(self):
"""'*[kv]_bmm_quantizer' matches 'k_bmm_quantizer' and 'v_bmm_quantizer'."""
from modelopt.torch.quantization.algorithms import _match_quantizer_cfg
quant_cfg = normalize_quant_cfg_list(
[{"quantizer_name": "*[kv]_bmm_quantizer", "cfg": {"num_bits": (4, 3)}}]
)
matched_k, _ = _match_quantizer_cfg(quant_cfg, "k_bmm_quantizer")
matched_v, _ = _match_quantizer_cfg(quant_cfg, "v_bmm_quantizer")
matched_w, _ = _match_quantizer_cfg(quant_cfg, "weight_quantizer")
assert matched_k is not None
assert matched_v is not None
assert matched_w is None
def test_path_scoped_does_not_overmatch(self):
"""'*mixer*weight_quantizer' should NOT match 'input_quantizer'.
Regression test: the old rsplit('*') logic would strip to 'weight_quantizer' and
overmatch any quantizer ending in 'weight_quantizer', but should not match unrelated names.
"""
from modelopt.torch.quantization.algorithms import _match_quantizer_cfg
quant_cfg = normalize_quant_cfg_list(
[
{"quantizer_name": "*", "enable": False},
{"quantizer_name": "*mixer*weight_quantizer", "cfg": {"num_bits": 4}},
]
)
# input_quantizer should only match the disable-all, not the mixer pattern
matched, enable = _match_quantizer_cfg(quant_cfg, "input_quantizer")
assert matched is None # cfg is None (enable-only entry)
assert enable is False
class TestQuantizeConfigValidators:
"""Tests for QuantizeConfig Pydantic field validators."""
def test_normalize_validator_converts_legacy_dict(self):
"""The 'before' validator auto-normalizes legacy dict format."""
cfg = QuantizeConfig(
quant_cfg={"*": {"enable": False}, "*weight_quantizer": {"num_bits": 8}},
algorithm="max",
)
assert isinstance(cfg.quant_cfg, list)
assert all("quantizer_name" in e for e in cfg.quant_cfg)
def test_validate_quant_cfg_entries_catches_invalid_cfg(self):
"""The 'after' validator surfaces QuantizerAttributeConfig errors early."""
with pytest.raises(ValidationError):
QuantizeConfig(
quant_cfg=[
{
"quantizer_name": "*weight_quantizer",
"cfg": {"num_bits": 8, "axis": 0, "block_sizes": {-1: 128}},
}
],
algorithm="max",
)
def test_validate_quant_cfg_entries_accepts_valid_cfg(self):
"""The 'after' validator passes for valid configs."""
cfg = QuantizeConfig(
quant_cfg=[
{"quantizer_name": "*weight_quantizer", "cfg": {"num_bits": 8, "axis": 0}},
{"quantizer_name": "*input_quantizer", "enable": False},
],
algorithm="max",
)
assert len(cfg.quant_cfg) == 2
class TestNVFP4BlockSizeValidation:
"""NVFP4 block_size must be constrained to {16, 32} per Blackwell MMA tile geometry."""
def test_nvfp4_block_16_accepted(self):
"""block_size=16 is the canonical NVFP4 tile — must pass."""
cfg = QuantizeConfig(
quant_cfg=[
{
"quantizer_name": "*weight_quantizer",
"cfg": {
"num_bits": (2, 1),
"block_sizes": {-1: 16, "type": "dynamic", "scale_bits": (4, 3)},
},
},
],
algorithm="max",
)
assert cfg.quant_cfg[0]["cfg"]["block_sizes"][-1] == 16
def test_nvfp4_block_32_accepted(self):
"""block_size=32 is the alternative Blackwell MMA tile — must pass."""
cfg = QuantizeConfig(
quant_cfg=[
{
"quantizer_name": "*weight_quantizer",
"cfg": {
"num_bits": (2, 1),
"block_sizes": {-1: 32, "type": "dynamic", "scale_bits": (4, 3)},
},
},
],
algorithm="max",
)
assert cfg.quant_cfg[0]["cfg"]["block_sizes"][-1] == 32
@pytest.mark.parametrize("bad_size", [8, 64, 128, 4, 256])
def test_nvfp4_illegal_block_size_rejected(self, bad_size):
"""block_size ∉ {16, 32} must fail at config construction time."""
with pytest.raises(ValidationError, match="NVFP4 block_size must be 16 or 32"):
QuantizeConfig(
quant_cfg=[
{
"quantizer_name": "*weight_quantizer",
"cfg": {
"num_bits": (2, 1),
"block_sizes": {-1: bad_size, "scale_bits": (4, 3)},
},
},
],
algorithm="max",
)
def test_non_nvfp4_block_size_unaffected(self):
"""INT4 block_size=128 must still pass — constraint is NVFP4-only."""
cfg = QuantizeConfig(
quant_cfg=[
{
"quantizer_name": "*weight_quantizer",
"cfg": {
"num_bits": 4,
"block_sizes": {-1: 128, "type": "static"},
},
},
],
algorithm="max",
)
assert cfg.quant_cfg[0]["cfg"]["block_sizes"][-1] == 128
def test_nvfp4_without_scale_bits_unaffected(self):
"""num_bits=(2,1) without scale_bits=(4,3) is not NVFP4 — no constraint."""
cfg = QuantizeConfig(
quant_cfg=[
{
"quantizer_name": "*weight_quantizer",
"cfg": {
"num_bits": (2, 1),
"block_sizes": {-1: 32, "type": "dynamic", "scale_bits": (8, 0)},
},
},
],
algorithm=None,
)
assert cfg.quant_cfg[0]["cfg"]["block_sizes"][-1] == 32