OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[PT FE] Fix fake_quantize conversion for FX graph (#30650)
Maxim Vafinc168ebba59ec
diff --git a/src/frontends/pytorch/src/op/quantize.cpp b/src/frontends/pytorch/src/op/quantize.cppindex 0dbae60de02..d2273dda454 100644--- a/src/frontends/pytorch/src/op/quantize.cpp+++ b/src/frontends/pytorch/src/op/quantize.cpp@@ -4,6 +4,7 @@#include "openvino/frontend/pytorch/node_context.hpp"#include "openvino/op/unsqueeze.hpp"+#include "pt_framework_node.hpp"#include "utils_quantize.hpp"namespace ov {@@ -69,19 +70,70 @@ OutputVector translate_quantize_per_channel_fx(const NodeContext& context) {}OutputVector translate_fake_quantize_per_tensor_affine_fx(const NodeContext& context) {- num_inputs_check(context, 6, 6);- auto out = translate_quantize_per_tensor(context);- auto axis_0 = context.mark_node(v0::Constant::create(element::i32, Shape{}, {0}));+ num_inputs_check(context, 5, 6);- return {context.mark_node(std::make_shared<v0::Unsqueeze>(out[0], axis_0))};+ const auto input = context.get_input(0);+ const auto scale = context.get_input(1);+ const auto zero_point = context.get_input(2);+ const auto num_inputs = context.get_input_size();+ OutputVector res;+ if (num_inputs == 5) {+ const auto low = context.const_input<int64_t>(3);+ const auto high = context.const_input<int64_t>(4);+ const auto quantized = quantize_fx(context,+ input,+ scale,+ zero_point,+ Output<Node>{},+ low,+ high,+ element::f32,+ QuantizedPtNodeType::QUANTIZE_PER_TENSOR);+ res = {quantized};+ } else {+ const auto fake_quant_enabled = context.const_input<bool>(3);+ PYTORCH_OP_CONVERSION_CHECK(fake_quant_enabled == true, "Disabled fake_quant is not supported.");+ const auto low = context.const_input<int64_t>(4);+ const auto high = context.const_input<int64_t>(5);+ const auto quantized = quantize_fx(context,+ input,+ scale,+ zero_point,+ Output<Node>{},+ low,+ high,+ element::f32,+ QuantizedPtNodeType::QUANTIZE_PER_TENSOR);+ // Returning cache mask is not supported+ auto cachemask = std::make_shared<PtFrameworkNode>(context.get_decoder(), context.inputs(), 1, false, true);+ context.mark_node(cachemask);+ auto attrs = cachemask->get_attrs();+ attrs[PtFrameworkNode::failed_conversion_key] = "Cache mask is not supported for fake_quantize.";+ cachemask->set_attrs(attrs);+ res = {quantized, cachemask};+ }+ return {context.mark_node(make_list_construct(OutputVector{res}))};}OutputVector translate_fake_quantize_per_channel_affine_fx(const NodeContext& context) {num_inputs_check(context, 6, 6);- auto out = translate_quantize_per_channel(context);- auto axis_0 = context.mark_node(v0::Constant::create(element::i32, Shape{}, {0}));- return {context.mark_node(std::make_shared<v0::Unsqueeze>(out[0], axis_0))};+ const auto input = context.get_input(0);+ const auto scale = context.get_input(1);+ const auto zero_point = context.get_input(2);+ const auto axis = context.get_input(3);+ const auto low = context.const_input<int64_t>(4);+ const auto high = context.const_input<int64_t>(5);+ const auto res = quantize_fx(context,+ input,+ scale,+ zero_point,+ axis,+ low,+ high,+ element::f32,+ QuantizedPtNodeType::QUANTIZE_PER_CHANNEL);+ return {context.mark_node(make_list_construct(OutputVector{res}))};}} // namespace opdiff --git a/src/frontends/pytorch/src/op_table.cpp b/src/frontends/pytorch/src/op_table.cppindex 996b36d3eb3..a3c20a46434 100644--- a/src/frontends/pytorch/src/op_table.cpp+++ b/src/frontends/pytorch/src/op_table.cpp@@ -915,6 +915,7 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_fx() {{"aten.expand_copy.default", op::translate_expand},{"aten.eye.m", op::translate_eye_fx},{"aten.fake_quantize_per_channel_affine_cachemask.default", op::translate_fake_quantize_per_channel_affine_fx},+ {"aten.fake_quantize_per_tensor_affine_cachemask.default", op::translate_fake_quantize_per_tensor_affine_fx},{"aten.fill.Scalar", op::translate_fill},{"aten.fill_.Scalar", op::inplace_op<op::translate_fill>},{"aten.fill.Tensor", op::translate_fill},diff --git a/tests/layer_tests/pytorch_tests/pytorch_layer_test_class.py b/tests/layer_tests/pytorch_tests/pytorch_layer_test_class.pyindex 5874639b98f..97398bb19b4 100644--- a/tests/layer_tests/pytorch_tests/pytorch_layer_test_class.py+++ b/tests/layer_tests/pytorch_tests/pytorch_layer_test_class.py@@ -19,16 +19,16 @@ import openvino.properties.hint as hintslogging.basicConfig(level=logging.DEBUG)-def skip_check(param):- return skip_if_export(param) if PytorchLayerTest.use_torch_export() else skip_if_fx(param)+def skip_check(*param, reason="Unsupported"):+ return skip_if_export(*param, reason=reason) if PytorchLayerTest.use_torch_export() else skip_if_fx(*param, reason=reason)-def skip_if_export(param, reason="Unsupported on torch.export"):- return pytest.param(param, marks=pytest.mark.skipif(PytorchLayerTest.use_torch_export(), reason=reason))+def skip_if_export(*param, reason="Unsupported on torch.export"):+ return pytest.param(*param, marks=pytest.mark.skipif(PytorchLayerTest.use_torch_export(), reason=reason))-def skip_if_fx(param, reason="Unsupported on torch.fx"):- return pytest.param(param, marks=pytest.mark.skipif(PytorchLayerTest.use_torch_compile_backend(), reason=reason))+def skip_if_fx(*param, reason="Unsupported on torch.fx"):+ return pytest.param(*param, marks=pytest.mark.skipif(PytorchLayerTest.use_torch_compile_backend(), reason=reason))class PytorchLayerTest:diff --git a/tests/layer_tests/pytorch_tests/test_fake_quantize.py b/tests/layer_tests/pytorch_tests/test_fake_quantize.pyindex 42c000d344f..2aa8d303f94 100644--- a/tests/layer_tests/pytorch_tests/test_fake_quantize.py+++ b/tests/layer_tests/pytorch_tests/test_fake_quantize.py@@ -6,7 +6,7 @@ import platformimport numpy as npimport pytestimport torch-from pytorch_layer_test_class import PytorchLayerTest+from pytorch_layer_test_class import PytorchLayerTest, skip_checkclass TestFakeQuantizePerTensorAffine(PytorchLayerTest):@@ -18,7 +18,7 @@ class TestFakeQuantizePerTensorAffine(PytorchLayerTest):def create_model(self, scale, zero_point, quant_min, quant_max):class fake_quantize_per_tensor_affine(torch.nn.Module):def __init__(self, scale, zero_point, quant_min, quant_max):- super(fake_quantize_per_tensor_affine, self).__init__()+ super().__init__()self.scale = scaleself.zero_point = zero_pointself.quant_min = quant_min@@ -29,23 +29,22 @@ class TestFakeQuantizePerTensorAffine(PytorchLayerTest):x, self.scale, self.zero_point, self.quant_min, self.quant_max)- ref_net = None-return (fake_quantize_per_tensor_affine(scale, zero_point, quant_min, quant_max),- ref_net,+ None,"aten::fake_quantize_per_tensor_affine",)@pytest.mark.nightly@pytest.mark.precommit+ @pytest.mark.precommit_torch_export@pytest.mark.precommit_fx_backend@pytest.mark.parametrize("scale, zero_point, quant_min, quant_max",[(1.0, 1, 0, 255),(0.01, 0, 0, 255),- (-0.01, 0, 0, 255),+ skip_check(-0.01, 0, 0, 255, reason="Negative scale is not supported"),(0.5, 0, -128, 127),(0.5, -1, -128, 127),(1.0, 0, 0, 127),@@ -74,7 +73,7 @@ class TestFakeQuantizePerTensorAffineCacheMaskTensorQParams(PytorchLayerTest):def create_model(self, scale, zero_point, quant_min, quant_max):class _fake_quantize_per_tensor_affine_cachemask_tensor_qparams(torch.nn.Module):def __init__(self, scale, zero_point, quant_min, quant_max):- super(_fake_quantize_per_tensor_affine_cachemask_tensor_qparams, self).__init__()+ super().__init__()self.scale = torch.tensor(scale)self.zero_point = torch.tensor(zero_point)self.fake_quant_enabled = torch.tensor(1)@@ -82,25 +81,25 @@ class TestFakeQuantizePerTensorAffineCacheMaskTensorQParams(PytorchLayerTest):self.quant_max = quant_maxdef forward(self, x):- return torch._fake_quantize_per_tensor_affine_cachemask_tensor_qparams(+ x, _ = torch._fake_quantize_per_tensor_affine_cachemask_tensor_qparams(x, self.scale, self.zero_point, self.fake_quant_enabled, self.quant_min, self.quant_max)-- ref_net = None+ return xreturn (_fake_quantize_per_tensor_affine_cachemask_tensor_qparams(scale, zero_point, quant_min, quant_max),- ref_net,+ None,"aten::_fake_quantize_per_tensor_affine_cachemask_tensor_qparams",)@pytest.mark.precommit_fx_backend+ @pytest.mark.precommit_torch_export@pytest.mark.parametrize("scale, zero_point, quant_min, quant_max",[(1.0, 1, 0, 255),(0.01, 0, 0, 255),- (-0.01, 0, 0, 255),+ skip_check(-0.01, 0, 0, 255, reason="Negative scale is not supported"),(0.5, 0, -128, 127),(0.5, -1, -128, 127),(1.0, 0, 0, 127),@@ -129,7 +128,7 @@ class TestFakeQuantizePerChannelAffine(PytorchLayerTest):def create_model(self, scale, zero_point, axis, quant_min, quant_max):class fake_quantize_per_channel_affine(torch.nn.Module):def __init__(self, scale, zero_point, axis, quant_min, quant_max):- super(fake_quantize_per_channel_affine, self).__init__()+ super().__init__()self.scale = scaleself.zero_point = zero_pointself.axis = axis@@ -141,24 +140,23 @@ class TestFakeQuantizePerChannelAffine(PytorchLayerTest):x, self.scale, self.zero_point, self.axis, self.quant_min, self.quant_max)- ref_net = None-return (fake_quantize_per_channel_affine(scale, zero_point, axis, quant_min, quant_max),- ref_net,+ None,"aten::fake_quantize_per_channel_affine",)@pytest.mark.nightly@pytest.mark.precommit+ @pytest.mark.precommit_torch_export@pytest.mark.precommit_fx_backend@pytest.mark.parametrize("scale, zero_point, axis, quant_min, quant_max",[(torch.tensor([0.005, 0.7]), torch.zeros(2), 1, 0, 255),(torch.tensor([1.5, -0.7, -0.1]), torch.tensor([1, 0, -1], dtype=torch.int32), 0, -128, 127),- (torch.tensor([-0.005, 0.7]), torch.tensor([0, 1], dtype=torch.int32), 1, 0, 127),- (torch.tensor([-0.005, -0.7, 0.1]), torch.tensor([1, 0, 1], dtype=torch.int32), 0, 0, 255),+ skip_check(torch.tensor([-0.005, 0.7]), torch.tensor([0, 1], dtype=torch.int32), 1, 0, 127, reason="Negative scale is not supported"),+ skip_check(torch.tensor([-0.005, -0.7, 0.1]), torch.tensor([1, 0, 1], dtype=torch.int32), 0, 0, 255, reason="Negative scale is not supported"),],)@pytest.mark.xfail(condition=platform.system() == 'Darwin' and platform.machine() == 'arm64',