|
8 | 8 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
9 | 9 | # See the License for the specific language governing permissions and
|
10 | 10 | # limitations under the License.
|
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +import openvino as ov |
| 14 | +import pytest |
| 15 | +import torch |
| 16 | + |
| 17 | +from nncf.common.quantization.structs import QuantizationPreset |
| 18 | +from nncf.openvino.graph.nncf_graph_builder import GraphConverter |
| 19 | +from nncf.openvino.statistics.aggregator import OVStatisticsAggregator |
| 20 | +from nncf.parameters import QuantizationMode |
| 21 | +from nncf.quantization.advanced_parameters import OverflowFix |
| 22 | +from nncf.quantization.algorithms.min_max.algorithm import MinMaxQuantization |
11 | 23 | from nncf.tensor import Tensor
|
| 24 | +from tests.cross_fw.shared.comparator import compare_stats |
| 25 | +from tests.cross_fw.shared.json import load_json |
12 | 26 | from tests.cross_fw.test_templates.test_calculate_quantizer_parameters import TemplateTestFQParams
|
| 27 | +from tests.openvino.native.common import convert_torch_model |
| 28 | +from tests.openvino.native.common import get_actual_reference_for_current_openvino |
| 29 | +from tests.openvino.native.common import get_dataset_for_test |
| 30 | +from tests.openvino.native.models import SYNTHETIC_MODELS |
| 31 | +from tests.openvino.native.models import ConvModel |
| 32 | +from tests.openvino.native.models import FPModel |
| 33 | +from tests.openvino.native.models import LinearModel |
| 34 | +from tests.openvino.native.models import MatMul2DModel |
| 35 | +from tests.openvino.native.models import UnifiedScalesModel |
| 36 | +from tests.openvino.native.models import WeightsModel |
| 37 | +from tests.openvino.native.models import get_torch_model_info |
| 38 | + |
| 39 | +REFERENCE_SCALES_DIR = Path("reference_scales") |
| 40 | + |
| 41 | + |
| 42 | +def get_fq_nodes_stats_algo(model): |
| 43 | + nodes = {} |
| 44 | + for op in model.get_ops(): |
| 45 | + if op.get_type_name() == "FakeQuantize": |
| 46 | + input_low = op.input_value(1).get_node().data |
| 47 | + input_high = op.input_value(2).get_node().data |
| 48 | + output_low = op.input_value(3).get_node().data |
| 49 | + output_high = op.input_value(4).get_node().data |
| 50 | + |
| 51 | + nodes[op.get_friendly_name()] = { |
| 52 | + "input_low": input_low, |
| 53 | + "input_high": input_high, |
| 54 | + "output_low": output_low, |
| 55 | + "output_high": output_high, |
| 56 | + } |
| 57 | + elif op.get_type_name() == "FakeConvert": |
| 58 | + scale = op.input_value(1).get_node().data |
| 59 | + shift = op.input_value(2).get_node().data |
| 60 | + |
| 61 | + nodes[op.get_friendly_name()] = { |
| 62 | + "scale": scale, |
| 63 | + "shift": shift, |
| 64 | + } |
| 65 | + return nodes |
| 66 | + |
| 67 | + |
| 68 | +def quantize_model(ov_model, q_params): |
| 69 | + dataset = get_dataset_for_test(ov_model) |
| 70 | + graph = GraphConverter.create_nncf_graph(ov_model) |
| 71 | + |
| 72 | + min_max_algo = MinMaxQuantization(subset_size=1, **q_params) |
| 73 | + statistics_aggregator = OVStatisticsAggregator(dataset) |
| 74 | + statistic_points = min_max_algo.get_statistic_points(ov_model, graph) |
| 75 | + statistics_aggregator.register_statistic_points(statistic_points) |
| 76 | + statistics_aggregator.collect_statistics(ov_model, graph) |
| 77 | + quantized_model = min_max_algo.apply(ov_model, graph, statistics_aggregator.statistic_points) |
| 78 | + return quantized_model |
| 79 | + |
| 80 | + |
| 81 | +@pytest.fixture(params=[True, False], ids=["inplace", "out_of_place"], name="inplace_statistics") |
| 82 | +def fixture_inplace_statistics(request): |
| 83 | + return request.param |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.parametrize( |
| 87 | + "preset", |
| 88 | + [QuantizationPreset.PERFORMANCE, QuantizationPreset.MIXED], |
| 89 | + ids=[QuantizationPreset.PERFORMANCE.value, QuantizationPreset.MIXED.value], |
| 90 | +) |
| 91 | +@pytest.mark.parametrize("model_creator_func", SYNTHETIC_MODELS.values()) |
| 92 | +def test_synthetic_models_fq_scales(model_creator_func, preset, inplace_statistics): |
| 93 | + model = model_creator_func() |
| 94 | + quantized_model = quantize_model(model.ov_model, {"preset": preset, "inplace_statistics": inplace_statistics}) |
| 95 | + nodes = get_fq_nodes_stats_algo(quantized_model) |
| 96 | + |
| 97 | + ref_stats_name = model.ref_graph_name.split(".")[0] + f"_{preset.value}.json" |
| 98 | + ref_stats_path = get_actual_reference_for_current_openvino(REFERENCE_SCALES_DIR / ref_stats_name) |
| 99 | + |
| 100 | + # Uncomment lines below to generate reference for new models. |
| 101 | + # from tests.shared.helpers import dump_to_json |
| 102 | + # dump_to_json(ref_stats_path, nodes) |
| 103 | + |
| 104 | + ref_nodes = load_json(ref_stats_path) |
| 105 | + compare_stats(ref_nodes, nodes) |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.parametrize( |
| 109 | + "mode", |
| 110 | + [QuantizationMode.FP8_E4M3, QuantizationMode.FP8_E5M2], |
| 111 | + ids=[QuantizationMode.FP8_E4M3.value, QuantizationMode.FP8_E5M2.value], |
| 112 | +) |
| 113 | +@pytest.mark.parametrize("model_creator_func", [UnifiedScalesModel]) |
| 114 | +def test_synthetic_models_fc_scales(model_creator_func, mode): |
| 115 | + model = model_creator_func() |
| 116 | + quantized_model = quantize_model(model.ov_model, {"mode": mode}) |
| 117 | + real_nodes = [op for op in quantized_model.get_ops() if op.get_type_name() == "FakeConvert"] |
| 118 | + |
| 119 | + ref_stats_name = model.ref_graph_name.split(".")[0] + f"_{mode.value}.json" |
| 120 | + ref_stats_path = get_actual_reference_for_current_openvino(REFERENCE_SCALES_DIR / ref_stats_name) |
| 121 | + ref_nodes = load_json(ref_stats_path) |
| 122 | + |
| 123 | + assert len(ref_nodes) == len(real_nodes), "The number of the real FakeConvert nodes is not correct" |
| 124 | + stat_nodes = get_fq_nodes_stats_algo(quantized_model) |
| 125 | + |
| 126 | + # Uncomment lines below to generate reference for new models. |
| 127 | + # from tests.shared.helpers import dump_to_json |
| 128 | + # dump_to_json(ref_stats_path, nodes) |
| 129 | + |
| 130 | + compare_stats(ref_nodes, stat_nodes) |
| 131 | + |
| 132 | + |
| 133 | +@pytest.mark.parametrize( |
| 134 | + "overflow_fix", |
| 135 | + [OverflowFix.DISABLE, OverflowFix.ENABLE, OverflowFix.FIRST_LAYER], |
| 136 | + ids=[OverflowFix.DISABLE.value, OverflowFix.ENABLE.value, OverflowFix.FIRST_LAYER.value], |
| 137 | +) |
| 138 | +def test_overflow_fix_scales(overflow_fix): |
| 139 | + model = WeightsModel() |
| 140 | + quantized_model = quantize_model(model.ov_model, {"overflow_fix": overflow_fix}) |
| 141 | + nodes = get_fq_nodes_stats_algo(quantized_model) |
| 142 | + |
| 143 | + ref_stats_name = model.ref_graph_name.split(".")[0] + f"_overflow_fix_{overflow_fix.value}.json" |
| 144 | + ref_stats_path = get_actual_reference_for_current_openvino(REFERENCE_SCALES_DIR / ref_stats_name) |
| 145 | + |
| 146 | + # Uncomment lines below to generate reference for new models. |
| 147 | + # from tests.shared.helpers import dump_to_json |
| 148 | + # dump_to_json(ref_stats_path, nodes) |
| 149 | + |
| 150 | + ref_nodes = load_json(ref_stats_path) |
| 151 | + compare_stats(ref_nodes, nodes) |
| 152 | + |
| 153 | + |
| 154 | +@pytest.mark.parametrize( |
| 155 | + "preset", |
| 156 | + [QuantizationPreset.PERFORMANCE, QuantizationPreset.MIXED], |
| 157 | + ids=[QuantizationPreset.PERFORMANCE.value, QuantizationPreset.MIXED.value], |
| 158 | +) |
| 159 | +@pytest.mark.parametrize("model_name", ("mobilenet-v2", "resnet-18", "ssd-vgg-300")) |
| 160 | +def test_real_models_fq_scales(model_name, preset, inplace_statistics, tmp_path): |
| 161 | + torch.manual_seed(0) # To use the same initialized model |
| 162 | + model_cls, input_shape = get_torch_model_info(model_name) |
| 163 | + ov_model = convert_torch_model(model_cls(), input_shape, tmp_path) |
| 164 | + |
| 165 | + quantized_model = quantize_model(ov_model, {"preset": preset, "inplace_statistics": inplace_statistics}) |
| 166 | + nodes = get_fq_nodes_stats_algo(quantized_model) |
| 167 | + ref_stats_name = model_name + f"_{preset.value}.json" |
| 168 | + ref_stats_path = get_actual_reference_for_current_openvino(REFERENCE_SCALES_DIR / ref_stats_name) |
| 169 | + |
| 170 | + # Uncomment lines below to generate reference for new models. |
| 171 | + # from tests.shared.helpers import dump_to_json |
| 172 | + # dump_to_json(ref_stats_path, nodes) |
| 173 | + |
| 174 | + ref_nodes = load_json(ref_stats_path) |
| 175 | + compare_stats(ref_nodes, nodes) |
| 176 | + |
| 177 | + |
| 178 | +REF_NODES_SHAPES = { |
| 179 | + "LinearModel": {"Input/fq_output_0": (), "MatMul/fq_weights_1": (1, 5)}, |
| 180 | + "ConvModel": {"Conv/fq_weights_1": (3, 1, 1, 1), "Sub/fq_output_0": ()}, |
| 181 | + "MatMul2DModel": {"Input/fq_output_0": (), "MatMul/fq_weights_1": (1, 2)}, |
| 182 | +} |
| 183 | + |
| 184 | + |
| 185 | +@pytest.mark.parametrize( |
| 186 | + "model_creator_func, ref_shapes", zip([LinearModel, ConvModel, MatMul2DModel], REF_NODES_SHAPES.values()) |
| 187 | +) |
| 188 | +def test_synthetic_models_fq_shapes(model_creator_func, ref_shapes, inplace_statistics): |
| 189 | + model = model_creator_func() |
| 190 | + quantized_model = quantize_model( |
| 191 | + model.ov_model, {"preset": QuantizationPreset.PERFORMANCE, "inplace_statistics": inplace_statistics} |
| 192 | + ) |
| 193 | + nodes = get_fq_nodes_stats_algo(quantized_model) |
| 194 | + for node_name, node in nodes.items(): |
| 195 | + assert node["input_low"].shape == ref_shapes[node_name] |
| 196 | + assert node["input_high"].shape == ref_shapes[node_name] |
| 197 | + assert node["output_low"].shape == ref_shapes[node_name] |
| 198 | + assert node["output_high"].shape == ref_shapes[node_name] |
| 199 | + |
| 200 | + |
| 201 | +@pytest.mark.parametrize("const_dtype", [ov.Type.f16, ov.Type.f32, ov.Type.bf16]) |
| 202 | +@pytest.mark.parametrize("input_dtype", [ov.Type.f16, ov.Type.f32, ov.Type.bf16]) |
| 203 | +def test_fq_precision_orig_fp32model(const_dtype, input_dtype, inplace_statistics): |
| 204 | + model = FPModel(const_dtype, input_dtype) |
| 205 | + quantized_model = quantize_model( |
| 206 | + model.ov_model, {"preset": QuantizationPreset.PERFORMANCE, "inplace_statistics": inplace_statistics} |
| 207 | + ) |
| 208 | + for op in quantized_model.get_ops(): |
| 209 | + if op.get_type_name() == "FakeQuantize": |
| 210 | + inp_node = op.input(0) |
| 211 | + fq_input_node = inp_node.get_source_output().get_node() |
| 212 | + if fq_input_node.get_type_name() == "Constant": |
| 213 | + assert op.get_element_type() == const_dtype |
| 214 | + elif op.get_type_name() == "Convert": |
| 215 | + inp_node = op.input(0) |
| 216 | + fq_input_node = inp_node.get_source_output().get_node() |
| 217 | + if fq_input_node.get_type_name() == "Constant": |
| 218 | + assert op.get_element_type() == input_dtype |
13 | 219 |
|
14 | 220 |
|
15 | 221 | class TestFQParams(TemplateTestFQParams):
|
|
0 commit comments