Skip to content

Commit c85ffe7

Browse files
Fix tests
1 parent 1efa37f commit c85ffe7

35 files changed

+1876
-73
lines changed

nncf/experimental/quantization/quantizer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424

2525
class IntDtype(Enum):
26+
"""
27+
Enum of possible integer types.
28+
"""
29+
2630
INT8 = "INT8"
2731
UINT8 = "UINT8"
2832

@@ -112,7 +116,7 @@ def transform_prior_quantization(self, model: TModel) -> TModel:
112116
"""
113117

114118
@abstractmethod
115-
def get_quantization_setup(self, model: TModel, nncf_graph: NNCFGraph) -> ExtendedFXQuantizerSetup:
119+
def get_quantization_setup(self, model: TModel, nncf_graph: NNCFGraph) -> SingleConfigQuantizerSetup:
116120
"""
117121
Builds SingleConfigQuantizerSetup for the given model.
118122

nncf/experimental/torch/fx/quantization/quantizer/openvino_adapter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import torch.fx
1313

1414
from nncf.common.graph.graph import NNCFGraph
15-
from nncf.experimental.quantization.quantizer import ExtendedFXQuantizerSetup
15+
from nncf.common.quantization.quantizer_setup import SingleConfigQuantizerSetup
1616
from nncf.experimental.quantization.quantizer import Quantizer
1717
from nncf.experimental.torch.fx.quantization.quantizer.openvino_quantizer import OpenVINOQuantizer
1818

@@ -28,5 +28,5 @@ def __init__(self, quantizer: OpenVINOQuantizer):
2828
def transform_prior_quantization(self, model: torch.fx.GraphModule) -> torch.fx.GraphModule:
2929
return self._quantizer.transform_for_annotation(model)
3030

31-
def get_quantization_setup(self, model: torch.fx.GraphModule, nncf_graph: NNCFGraph) -> ExtendedFXQuantizerSetup:
31+
def get_quantization_setup(self, model: torch.fx.GraphModule, nncf_graph: NNCFGraph) -> SingleConfigQuantizerSetup:
3232
return self._quantizer.get_nncf_quantization_setup(model, nncf_graph)

nncf/experimental/torch/fx/quantization/quantizer/openvino_quantizer.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
from nncf.common.quantization.quantizer_setup import SingleConfigQuantizerSetup
3737
from nncf.common.quantization.structs import QuantizationScheme
3838
from nncf.common.utils.api_marker import api
39-
from nncf.experimental.quantization.quantizer import ExtendedFXQuantizerSetup
40-
from nncf.experimental.quantization.quantizer import IntDtype
4139
from nncf.experimental.torch.fx.nncf_graph_builder import GraphConverter
4240
from nncf.experimental.torch.fx.node_utils import get_graph_node_by_name
4341
from nncf.quantization.advanced_parameters import FP8QuantizationParameters
@@ -137,16 +135,9 @@ def set_ignored_scope(
137135

138136
def get_nncf_quantization_setup(
139137
self, model: torch.fx.GraphModule, nncf_graph: NNCFGraph
140-
) -> ExtendedFXQuantizerSetup:
138+
) -> SingleConfigQuantizerSetup:
141139
self._min_max_algo._set_backend_entity(model)
142-
base_setup = self._min_max_algo.find_quantization_setup(model, nncf_graph)
143-
dtype_map = {}
144-
for id_, qp in base_setup.quantization_points.items():
145-
dtype_map[id_] = None if qp.qconfig.mode == QuantizationScheme.SYMMETRIC else IntDtype.UINT8.value
146-
147-
state = base_setup.get_state()
148-
state[ExtendedFXQuantizerSetup.QUANTIZER_DTYPE_NAME] = dtype_map
149-
return ExtendedFXQuantizerSetup.from_state(state)
140+
return self._min_max_algo.find_quantization_setup(model, nncf_graph)
150141

151142
def annotate(self, model: torch.fx.GraphModule) -> torch.fx.GraphModule:
152143
"""

nncf/quantization/algorithms/min_max/algorithm.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ def _get_quantizer_setup(
718718

719719
def _add_weight_quantization_target_point(
720720
self, quantization_point: SingleConfigQuantizationPoint, nncf_graph: NNCFGraph
721-
) -> TargetPoint:
721+
) -> list[TargetPoint]:
722722
"""
723723
Adds weight quantization target point to the set of existing points.
724724
@@ -728,7 +728,7 @@ def _add_weight_quantization_target_point(
728728
weight_quantization_target_points = self._get_weight_quantization_target_points(quantization_point, nncf_graph)
729729
for weight_quantization_target_point in weight_quantization_target_points:
730730
self._quantization_target_points_to_qconfig[weight_quantization_target_point] = quantization_point.qconfig
731-
return weight_quantization_target_point
731+
return weight_quantization_target_points
732732

733733
def _add_activation_quantization_target_point(
734734
self, quantization_point: SingleConfigQuantizationPoint, nncf_graph: NNCFGraph
@@ -854,17 +854,19 @@ def fill_quantization_target_points(
854854
if isinstance(quantizer_setup, ExtendedQuantizerSetup):
855855
extra_params = quantizer_setup.get_extra_params()
856856
else:
857-
extra_params = collections.defaultdict(lambda: collections.defaultdict(None))
857+
extra_params = collections.defaultdict(lambda: collections.defaultdict(lambda: None))
858858

859859
for id_, quantization_point in quantization_points:
860860
if quantization_point.is_weight_quantization_point():
861-
tp = self._add_weight_quantization_target_point(quantization_point, nncf_graph)
861+
tps = self._add_weight_quantization_target_point(quantization_point, nncf_graph)
862862
elif quantization_point.is_activation_quantization_point():
863863
tp = self._add_activation_quantization_target_point(quantization_point, nncf_graph)
864+
tps = [tp]
864865
else:
865866
msg = "Incorrect quantization point"
866867
raise nncf.InternalError(msg)
867-
self._extra_params[tp] = extra_params[id_]
868+
for tp in tps:
869+
self._extra_params[tp] = extra_params[id_]
868870

869871
return self._quantization_target_points_to_qconfig, self._unified_scale_groups, self._extra_params
870872

nncf/quantization/algorithms/min_max/onnx_backend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def create_unified_scales_quantizers_insertion_commands(
158158
) -> list[ONNXQuantizerInsertionCommand]:
159159
return [
160160
ONNXMinMaxAlgoBackend.create_quantizer_insertion_command(
161-
nncf_graph, target_point, quantizer_config, parameters
161+
nncf_graph, target_point, quantizer_config, parameters, extra_params
162162
)
163163
for target_point in target_points
164164
]

nncf/quantization/algorithms/min_max/torch_fx_backend.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,13 @@ def _create_quantizer(
200200
eps = 1e-16
201201
if dtype is None:
202202
if quantizer_config.mode != QuantizationScheme.SYMMETRIC:
203-
msg = (
204-
"Wrong usage of dtype parameter: it should be specified for the QuantizationScheme.ASYMMETRIC mode"
203+
dtype = IntDtype.UINT8
204+
else:
205+
dtype = (
206+
IntDtype.INT8
207+
if quantizer_config.signedness_to_force or torch.any(parameters.input_low.data < 0.0)
208+
else IntDtype.UINT8
205209
)
206-
raise nncf.InternalError(msg)
207-
dtype = (
208-
IntDtype.INT8
209-
if quantizer_config.signedness_to_force or torch.any(parameters.input_low.data < 0.0)
210-
else IntDtype.UINT8
211-
)
212210

213211
if per_channel:
214212
observer = torch.ao.quantization.observer.PerChannelMinMaxObserver
@@ -237,6 +235,10 @@ def _create_quantizer(
237235
scale, zero_point = get_scale_zp_from_input_low_input_high(
238236
level_low, level_high, parameters.input_low.data, parameters.input_high.data
239237
)
238+
239+
scale = scale.view(-1)
240+
zero_point = zero_point.view(-1)
241+
240242
fakequantizer = FakeQuantize(
241243
observer=observer,
242244
quant_max=level_high,

tests/cross_fw/test_templates/test_ptq_params.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# limitations under the License.
1111
from abc import abstractmethod
1212
from collections import Counter
13+
from collections import defaultdict
1314
from copy import deepcopy
1415

1516
import pytest
@@ -361,6 +362,7 @@ def test_unified_scales_command_creation(self, mocker):
361362

362363
algo._quantization_target_points_to_qconfig = q_tp_vs_qcf
363364
algo._unified_scale_groups = [unified_scales_group]
365+
algo._extra_params = defaultdict(lambda: defaultdict(lambda: None))
364366

365367
mock_transformer = mocker.MagicMock()
366368
mocker.patch(
@@ -407,9 +409,9 @@ def test_empty_statistics(self, mode, mocker):
407409

408410
dummy_tp = {target_point: QuantizerConfig()}
409411
if mode == "target_point":
410-
dummy_tps = (dummy_tp, {})
412+
dummy_tps = (dummy_tp, {}, defaultdict(lambda: None))
411413
else:
412-
dummy_tps = ({}, ((target_point,),))
414+
dummy_tps = ({}, ((target_point,),), defaultdict(lambda: None))
413415
stat_points.add_statistic_point(
414416
StatisticPoint(target_point, DummyMinMaxTensorCollector(None, None), algo._algorithm_key)
415417
)

tests/cross_fw/test_templates/test_unified_scales.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ def test_unified_groups(
4747
nncf_graph = NNCFGraphFactory.create(backend_model)
4848
algo = MinMaxQuantization()
4949
algo._set_backend_entity(backend_model)
50-
_, groups = algo._get_quantization_target_points(backend_model, nncf_graph)
50+
_, groups, _ = algo._get_quantization_target_points(backend_model, nncf_graph)
5151
assert [[target.target_node_name for target in groups] for groups in groups] == unified_group

tests/post_training/data/ptq_reference_data.yaml

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ torchvision/resnet18_backend_FX_TORCH:
4343
error_message: "Openvino Model Files Not Found!"
4444
message: "Issue-166847"
4545
torchvision/resnet18_backend_CUDA_FX_TORCH:
46+
metric_value: 0.6946
47+
exception_xfail_reason:
48+
type: "FileNotFoundError"
49+
error_message: "Openvino Model Files Not Found!"
50+
message: "Issue-166847"
4651
torchvision/resnet18_backend_OV_QUANTIZER_NNCF:
4752
metric_value: 0.6946
4853
torchvision/resnet18_backend_OV_QUANTIZER_AO:
@@ -51,10 +56,6 @@ torchvision/resnet18_backend_X86_QUANTIZER_NNCF:
5156
metric_value: 0.6946
5257
torchvision/resnet18_backend_X86_QUANTIZER_AO:
5358
metric_value: 0.6946
54-
exception_xfail_reason:
55-
type: "FileNotFoundError"
56-
error_message: "Openvino Model Files Not Found!"
57-
message: "Issue-166847"
5859
torchvision/mobilenet_v3_small_BC_backend_FP32:
5960
metric_value: 0.6766
6061
torchvision/mobilenet_v3_small_BC_backend_OV:
@@ -92,6 +93,11 @@ torchvision/vit_b_16_backend_FX_TORCH:
9293
error_message: "Openvino Model Files Not Found!"
9394
message: "Issue-166847"
9495
torchvision/vit_b_16_backend_CUDA_FX_TORCH:
96+
metric_value: 0.80922
97+
exception_xfail_reason:
98+
type: "FileNotFoundError"
99+
error_message: "Openvino Model Files Not Found!"
100+
message: "Issue-166847"
95101
torchvision/vit_b_16_backend_OV_QUANTIZER_NNCF:
96102
metric_value: 0.80922
97103
torchvision/vit_b_16_backend_OV_QUANTIZER_AO:
@@ -100,10 +106,6 @@ torchvision/vit_b_16_backend_X86_QUANTIZER_NNCF:
100106
metric_value: 0.80922
101107
torchvision/vit_b_16_backend_X86_QUANTIZER_AO:
102108
metric_value: 0.80922
103-
exception_xfail_reason:
104-
type: "FileNotFoundError"
105-
error_message: "Openvino Model Files Not Found!"
106-
message: "Issue-166847"
107109
torchvision/swin_v2_s_backend_FP32:
108110
metric_value: 0.83712
109111
torchvision/swin_v2_s_backend_OV:
@@ -115,6 +117,11 @@ torchvision/swin_v2_s_backend_FX_TORCH:
115117
error_message: "Openvino Model Files Not Found!"
116118
message: "Issue-166847"
117119
torchvision/swin_v2_s_backend_CUDA_FX_TORCH:
120+
metric_value: 0.8360
121+
exception_xfail_reason:
122+
type: "FileNotFoundError"
123+
error_message: "Openvino Model Files Not Found!"
124+
message: "Issue-166847"
118125
torchvision/swin_v2_s_backend_OV_QUANTIZER_NNCF:
119126
metric_value: 0.8360
120127
torchvision/swin_v2_s_backend_OV_QUANTIZER_AO:
@@ -123,10 +130,6 @@ torchvision/swin_v2_s_backend_X86_QUANTIZER_NNCF:
123130
metric_value: 0.8360
124131
torchvision/swin_v2_s_backend_X86_QUANTIZER_AO:
125132
metric_value: 0.8360
126-
exception_xfail_reason:
127-
type: "FileNotFoundError"
128-
error_message: "Openvino Model Files Not Found!"
129-
message: "Issue-166847"
130133
timm/crossvit_9_240_backend_CUDA_TORCH:
131134
metric_value: 0.7275
132135
timm/crossvit_9_240_backend_FP32:

tests/post_training/model_scope.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@
9090
"compression_params": {
9191
"subset_size": 2,
9292
},
93-
"backends": [
94-
BackendType.FX_TORCH,
95-
BackendType.CUDA_FX_TORCH,
93+
"backends": FX_BACKENDS
94+
+ [
9695
BackendType.TORCH,
9796
BackendType.CUDA_TORCH,
9897
BackendType.OV,

0 commit comments

Comments
 (0)