Skip to content

Commit 19493a2

Browse files
Update tests
1 parent 34a1120 commit 19493a2

10 files changed

Lines changed: 245 additions & 97 deletions

File tree

src/nncf/quantization/quantize_model.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ def quantize(
201201
if backend == BackendType.ONNX:
202202
from nncf.onnx.quantization.quantize_model import quantize_impl
203203

204+
if advanced_parameters and advanced_parameters.calibration_device:
205+
msg = "ONNX backend does not support the `calibration_device` option."
206+
raise nncf.ParameterNotSupportedError(msg)
207+
204208
return quantize_impl( # type: ignore[no-any-return]
205209
model=model,
206210
calibration_dataset=calibration_dataset,
@@ -217,6 +221,10 @@ def quantize(
217221
if backend == BackendType.TORCH:
218222
from nncf.torch.function_hook.quantization.quantize_model import quantize_impl
219223

224+
if advanced_parameters and advanced_parameters.calibration_device:
225+
msg = "Torch backend does not support the `calibration_device` option."
226+
raise nncf.ParameterNotSupportedError(msg)
227+
220228
return quantize_impl( # type: ignore[no-any-return]
221229
model=model,
222230
calibration_dataset=calibration_dataset,
@@ -233,6 +241,10 @@ def quantize(
233241
if backend == BackendType.TORCH_FX:
234242
from nncf.experimental.torch.fx.quantization.quantize_model import quantize_impl
235243

244+
if advanced_parameters and advanced_parameters.calibration_device:
245+
msg = "TorchFX backend does not support the `calibration_device` option."
246+
raise nncf.ParameterNotSupportedError(msg)
247+
236248
return quantize_impl( # type: ignore[no-any-return]
237249
model=model,
238250
calibration_dataset=calibration_dataset,
@@ -372,6 +384,10 @@ def quantize_with_accuracy_control(
372384
if backend == BackendType.ONNX:
373385
from nncf.onnx.quantization.quantize_model import quantize_with_accuracy_control_impl
374386

387+
if advanced_quantization_parameters and advanced_quantization_parameters.calibration_device:
388+
msg = "ONNX backend does not support the `calibration_device` option."
389+
raise nncf.ParameterNotSupportedError(msg)
390+
375391
return quantize_with_accuracy_control_impl( # type: ignore[no-any-return]
376392
model,
377393
calibration_dataset,
@@ -528,6 +544,10 @@ def compress_weights(
528544
msg = "Torch backend does not support statistics caching."
529545
raise nncf.ParameterNotSupportedError(msg)
530546

547+
if advanced_parameters and advanced_parameters.calibration_device:
548+
msg = "Torch backend does not support the `calibration_device` option."
549+
raise nncf.ParameterNotSupportedError(msg)
550+
531551
if compression_format == CompressionFormat.FQ and group_size != -1:
532552
msg = "Torch backend does not support FQ compression format for group-wise quantization."
533553
raise nncf.ParameterNotSupportedError(msg)
@@ -578,6 +598,10 @@ def compress_weights(
578598
msg = "TorchFX does not supports statistics caching."
579599
raise nncf.ParameterNotSupportedError(msg)
580600

601+
if advanced_parameters and advanced_parameters.calibration_device:
602+
msg = "TorchFX backend does not support the `calibration_device` option."
603+
raise nncf.ParameterNotSupportedError(msg)
604+
581605
if compression_format in [CompressionFormat.FQ, CompressionFormat.FQ_LORA, CompressionFormat.FQ_LORA_NLS]:
582606
msg = "Torch FX backend does not support FQ, FQ_LORA and FQ_LORA_NLS compression formats."
583607
raise nncf.ParameterNotSupportedError(msg)
@@ -649,6 +673,10 @@ def compress_weights(
649673
if advanced_parameters and advanced_parameters.statistics_path:
650674
msg = "ONNX does not supports statistics caching."
651675
raise nncf.ParameterNotSupportedError(msg)
676+
677+
if advanced_parameters and advanced_parameters.calibration_device:
678+
msg = "ONNX backend does not support the `calibration_device` option."
679+
raise nncf.ParameterNotSupportedError(msg)
652680
compression_weights_impl = onnx_compress_weights_impl
653681
if compression_weights_impl is None:
654682
msg = f"Unsupported type of backend: {backend}"

src/nncf/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
# See the License for the specific language governing permissions and
1010
# limitations under the License.
1111

12-
__version__ = "3.2.0.dev0+00576e031"
12+
__version__ = "3.2.0"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright (c) 2026 Intel Corporation
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
from abc import ABC
12+
from abc import abstractmethod
13+
from typing import TypeVar
14+
15+
import pytest
16+
17+
import nncf
18+
from nncf.data.dataset import Dataset
19+
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
20+
21+
TModel = TypeVar("TModel")
22+
23+
24+
class TemplateTestQuantizeApi(ABC):
25+
@staticmethod
26+
@abstractmethod
27+
def get_simple_model() -> TModel:
28+
"""Returns a minimal model for the backend."""
29+
30+
def test_quantize_calibration_device(self):
31+
model = self.get_simple_model()
32+
with pytest.raises(nncf.ParameterNotSupportedError):
33+
nncf.quantize(
34+
model,
35+
Dataset([0]),
36+
advanced_parameters=AdvancedQuantizationParameters(calibration_device="SOME_DEVICE"),
37+
)

tests/cross_fw/test_templates/template_test_weights_compression.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,3 +983,17 @@ def test_compression_skipped_with_transposed_activations(self, transpose_a_suppo
983983
all_layers=True,
984984
**kwargs,
985985
)
986+
987+
def test_compress_weights_calibration_device(self):
988+
model = self.get_awq_model(non_mergable_pattern=False, is_3d_weights=False)
989+
dataset = Dataset([self.to_tensor(np.ones([2, 8, 8]))])
990+
with pytest.raises(nncf.ParameterNotSupportedError):
991+
compress_weights(
992+
model,
993+
mode=CompressWeightsMode.INT4_SYM,
994+
ratio=1.0,
995+
group_size=2,
996+
dataset=dataset,
997+
awq=True,
998+
advanced_parameters=CompressionParams(calibration_device="SOME_DEVICE"),
999+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright (c) 2026 Intel Corporation
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
import numpy as np
12+
import pytest
13+
14+
import nncf
15+
from nncf import Dataset
16+
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
17+
from tests.cross_fw.test_templates.template_test_quantize_api import TemplateTestQuantizeApi
18+
from tests.onnx.models import LinearModel
19+
20+
INPUT_SHAPE = [1, 3, 32, 32]
21+
22+
23+
class TestONNXQuantizeApi(TemplateTestQuantizeApi):
24+
@staticmethod
25+
def get_simple_model():
26+
return LinearModel().onnx_model
27+
28+
def test_quantize_with_accuracy_control_calibration_device(self):
29+
model = self.get_simple_model()
30+
dataset = Dataset([np.ones(INPUT_SHAPE, dtype=np.float32)])
31+
with pytest.raises(nncf.ParameterNotSupportedError):
32+
nncf.quantize_with_accuracy_control(
33+
model,
34+
dataset,
35+
dataset,
36+
lambda model, dataset: (1.0, None),
37+
advanced_quantization_parameters=AdvancedQuantizationParameters(calibration_device="SOME_DEVICE"),
38+
)

tests/openvino/native/quantization/test_quantize_api.py

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,57 +8,67 @@
88
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
99
# See the License for the specific language governing permissions and
1010
# limitations under the License.
11+
import numpy as np
12+
import openvino as ov
1113
import pytest
12-
from openvino import Model
13-
from openvino import Shape
14-
from openvino import Type
15-
from openvino import op
16-
from openvino import opset13 as opset
1714

1815
import nncf
1916
from nncf import Dataset
20-
from tests.cross_fw.shared.datasets import MockDataset
17+
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
18+
from tests.cross_fw.test_templates.template_test_quantize_api import TemplateTestQuantizeApi
19+
from tests.openvino.native.models import LinearModel
2120

22-
INPUT_SHAPE = [2, 1, 1, 1]
21+
LINEAR_MODEL_INPUT_SHAPE = [1, 3, 4, 2]
2322

2423

25-
def get_mock_model() -> Model:
26-
param_node = op.Parameter(Type.f32, Shape(INPUT_SHAPE))
27-
softmax_axis = 1
28-
softmax_node = opset.softmax(param_node, softmax_axis)
29-
return Model(softmax_node, [param_node], "mock")
24+
class TestOVQuantizeApi(TemplateTestQuantizeApi):
25+
@staticmethod
26+
def get_simple_model() -> ov.Model:
27+
return LinearModel().ov_model
3028

29+
def test_quantize_calibration_device(self, monkeypatch):
30+
model = self.get_simple_model()
31+
dataset = Dataset([np.ones(LINEAR_MODEL_INPUT_SHAPE, dtype=np.float32)])
32+
captured_devices = []
3133

32-
def test_non_positive_subset_size():
33-
model_to_test = get_mock_model()
34+
original_compile = ov.Core.compile_model
3435

35-
with pytest.raises(nncf.ValidationError) as e:
36-
nncf.quantize(model_to_test, Dataset(MockDataset(INPUT_SHAPE)), subset_size=0)
37-
assert "Subset size must be positive." in e.info
36+
def mock_compile(self, model, device_name="CPU", config=None):
37+
captured_devices.append(device_name)
38+
return original_compile(self, model, device_name="CPU", config=config)
3839

40+
monkeypatch.setattr(ov.Core, "compile_model", mock_compile)
41+
nncf.quantize(
42+
model,
43+
dataset,
44+
advanced_parameters=AdvancedQuantizationParameters(calibration_device="SOME_DEVICE"),
45+
)
46+
assert all(d == "SOME_DEVICE" for d in captured_devices)
3947

40-
def test_quantize_calibration_device(monkeypatch):
41-
import numpy as np
42-
import openvino as ov
48+
def test_quantize_with_accuracy_control_calibration_device(self, monkeypatch):
49+
model = self.get_simple_model()
50+
dataset = Dataset([np.ones(LINEAR_MODEL_INPUT_SHAPE, dtype=np.float32)])
51+
captured_devices = []
4352

44-
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
45-
from tests.openvino.native.models import LinearModel
53+
original_compile = ov.Core.compile_model
4654

47-
model_to_test = LinearModel().ov_model
48-
input_shape = [inp.shape for inp in model_to_test.inputs][0]
49-
dataset = Dataset([np.random.rand(*input_shape).astype(np.float32) for _ in range(2)])
50-
captured_devices = []
55+
def mock_compile(self, model, device_name="CPU", config=None):
56+
captured_devices.append(device_name)
57+
return original_compile(self, model, device_name="CPU", config=config)
5158

52-
original_compile = ov.Core.compile_model
59+
monkeypatch.setattr(ov.Core, "compile_model", mock_compile)
60+
nncf.quantize_with_accuracy_control(
61+
model,
62+
dataset,
63+
dataset,
64+
lambda model, dataset: (1.0, None),
65+
advanced_quantization_parameters=AdvancedQuantizationParameters(calibration_device="SOME_DEVICE"),
66+
)
67+
assert "SOME_DEVICE" in captured_devices
5368

54-
def mock_compile(self, model, device_name="CPU", config=None):
55-
captured_devices.append(device_name)
56-
return original_compile(self, model, device_name="CPU", config=config)
69+
def test_non_positive_subset_size(self):
70+
model_to_test = self.get_simple_model()
5771

58-
monkeypatch.setattr(ov.Core, "compile_model", mock_compile)
59-
nncf.quantize(
60-
model_to_test,
61-
dataset,
62-
advanced_parameters=AdvancedQuantizationParameters(calibration_device="GPU"),
63-
)
64-
assert any(d == "GPU" for d in captured_devices)
72+
with pytest.raises(nncf.ValidationError) as e:
73+
nncf.quantize(model_to_test, Dataset([np.ones(LINEAR_MODEL_INPUT_SHAPE, dtype=np.float32)]), subset_size=0)
74+
assert "Subset size must be positive." in e.info

tests/openvino/native/quantization/test_weights_compression.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,26 +2768,26 @@ def test_phi_rope_model(self):
27682768
)
27692769
assert self.get_num_int8_nodes(compressed_model) == 0
27702770

2771+
def test_compress_weights_calibration_device(self, monkeypatch):
2772+
model = AWQMatmulModel().ov_model
2773+
dataset = Dataset([np.ones([2, 8, 8])])
2774+
captured_devices = []
27712775

2772-
def test_compress_weights_calibration_device(monkeypatch):
2773-
model = AWQMatmulModel().ov_model
2774-
dataset = Dataset([np.ones([2, 8, 8])])
2775-
captured_devices = []
2776-
2777-
original_compile = ov.Core.compile_model
2776+
original_compile = ov.Core.compile_model
27782777

2779-
def mock_compile(self, model, device_name="CPU", config=None):
2780-
captured_devices.append(device_name)
2781-
return original_compile(self, model, device_name="CPU", config=config)
2778+
def mock_compile(self, model, device_name="CPU", config=None):
2779+
captured_devices.append(device_name)
2780+
return original_compile(self, model, device_name="CPU", config=config)
27822781

2783-
monkeypatch.setattr(ov.Core, "compile_model", mock_compile)
2784-
compress_weights(
2785-
model,
2786-
mode=CompressWeightsMode.INT4_SYM,
2787-
ratio=1.0,
2788-
group_size=2,
2789-
dataset=dataset,
2790-
awq=True,
2791-
advanced_parameters=AdvancedCompressionParameters(calibration_device="GPU"),
2792-
)
2793-
assert any(d == "GPU" for d in captured_devices)
2782+
monkeypatch.setattr(ov.Core, "compile_model", mock_compile)
2783+
monkeypatch.setenv("NNCF_DISABLE_OPTIMIZED_COMPRESSION", "1")
2784+
compress_weights(
2785+
model,
2786+
mode=CompressWeightsMode.INT4_SYM,
2787+
ratio=1.0,
2788+
group_size=2,
2789+
dataset=dataset,
2790+
awq=True,
2791+
advanced_parameters=AdvancedCompressionParameters(calibration_device="SOME_DEVICE"),
2792+
)
2793+
assert all(d == "SOME_DEVICE" for d in captured_devices)

0 commit comments

Comments
 (0)