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
Mark Dequantization subgraphs for u1/2/3/6 precisions (#30922)
Ivan Tikhonov79ba213c9904
diff --git a/src/common/transformations/src/transformations/common_optimizations/moc_transformations.cpp b/src/common/transformations/src/transformations/common_optimizations/moc_transformations.cppindex 900a15f6762..31637108e8b 100644--- a/src/common/transformations/src/transformations/common_optimizations/moc_transformations.cpp+++ b/src/common/transformations/src/transformations/common_optimizations/moc_transformations.cpp@@ -96,6 +96,8 @@#include "transformations/smart_reshape/reshape_sinking.hpp"#include "transformations/symbolic_transformations/symbolic_optimizations.hpp"+using namespace ov::element;+static ov::PartialShape prepare_dynamic_shape(const ov::PartialShape& shape) {auto new_shape = ov::PartialShape::dynamic(shape.rank());if (shape.rank().is_static())@@ -132,21 +134,9 @@ bool ov::pass::MOCTransformations::run_on_model(const std::shared_ptr<ov::Model>REGISTER_PASS(manager, InitNodeInfo)if (m_low_precision_enabled) {// Transformation call example, to check with the real model- manager.register_pass<MarkGatherSubgraph>(element::TypeVector{element::f8e4m3},- element::TypeVector{element::u4});- manager.register_pass<ov::pass::MarkDequantization>(element::TypeVector{ov::element::i32,- ov::element::u32,- ov::element::i16,- ov::element::u16,- ov::element::i8,- ov::element::u8,- ov::element::i4,- ov::element::u4,- ov::element::nf4,- ov::element::f4e2m1,- ov::element::f8e4m3,- ov::element::f8e5m2,- ov::element::f8e8m0});+ manager.register_pass<MarkGatherSubgraph>(TypeVector{f8e4m3}, TypeVector{u4});+ manager.register_pass<ov::pass::MarkDequantization>(+ TypeVector{i32, u32, i16, u16, i8, u8, u6, i4, u4, nf4, u3, u2, u1, f4e2m1, f8e4m3, f8e5m2, f8e8m0});}if (!m_use_shapes) {manager.register_pass<ov::pass::DisableShapeOfConstantFolding>();diff --git a/src/common/transformations/tests/common_optimizations/moc_transformations.cpp b/src/common/transformations/tests/common_optimizations/moc_transformations.cppindex 747aaf433e4..cea50272585 100644--- a/src/common/transformations/tests/common_optimizations/moc_transformations.cpp+++ b/src/common/transformations/tests/common_optimizations/moc_transformations.cpp@@ -10,20 +10,55 @@#include "common_test_utils/ov_test_utils.hpp"#include "openvino/core/model.hpp"+#include "openvino/core/preprocess/pre_post_process.hpp"#include "openvino/op/add.hpp"#include "openvino/op/convert.hpp"+#include "openvino/op/convolution.hpp"#include "openvino/op/matmul.hpp"#include "openvino/op/multiply.hpp"#include "openvino/op/reshape.hpp"#include "openvino/op/squeeze.hpp"+#include "openvino/op/subtract.hpp"#include "openvino/op/unsqueeze.hpp"#include "openvino/opsets/opset12_decl.hpp"#include "openvino/pass/manager.hpp"+#include "openvino/pass/serialize.hpp"+#include "transformations/rt_info/dequantization_node.hpp"+#include "transformations/rt_info/disable_constant_folding.hpp"using namespace testing;using namespace ov;+using namespace ov::element;using namespace ov::opset12;+namespace {++std::shared_ptr<ov::Node> make_dq_weights(const ov::element::Type& quant_type,+ const ov::Shape& w_shape,+ float scale,+ float zp,+ bool dq_markup = false) {+ auto w = ov::op::v0::Constant::create(quant_type, w_shape, {1.0f});+ auto w_f = std::make_shared<ov::op::v0::Convert>(w, ov::element::f32);++ auto zp_const = ov::op::v0::Constant::create(quant_type, {}, {zp});+ auto zp_f = std::make_shared<ov::op::v0::Convert>(zp_const, ov::element::f32);+ auto sub = std::make_shared<ov::op::v1::Subtract>(w_f, zp_f);++ std::vector<size_t> scale_shape(w_shape.size(), 1);+ auto scale_const = ov::op::v0::Constant::create(ov::element::f32, scale_shape, {scale});+ auto mul = std::make_shared<ov::op::v1::Multiply>(sub, scale_const);++ if (dq_markup) {+ mark_as_dequantization_node(sub);+ mark_as_dequantization_node(mul);+ disable_constant_folding(zp_f);+ }+ return mul;+}++} // namespace+TEST(TransformationTests, TestModelTensorsConsistencyUseShapesTrue) {auto input = std::make_shared<opset12::Parameter>(element::f32, Shape{1});auto const1 = opset12::Constant::create(element::f32, Shape{1}, {1});@@ -139,3 +174,88 @@ TEST_F(TransformationTestsF, MOCTest) {EXPECT_EQ(weights->get_friendly_name(), weights_ref->get_friendly_name());}}++class QuantWeightsTestP : public TransformationTestsF, public ::testing::WithParamInterface<ov::element::Type> {+protected:+ ov::element::Type qtype;++ void SetUp() override {+ TransformationTestsF::SetUp();+ qtype = GetParam();+ }+};++/**+ * Model structure for MatMul_Conv_QuantWeights test+ *+ * Input+ * │+ * MatMul ◀── DQ (MatMul weights)+ * │+ * Reshape+ * │+ * Conv ◀── DQ (Conv weights)+ * │+ * Output+ *+ * Test checks that DQ subgraphs for MatMul and Conv weights+ * are not modified by MOC and PrePostProcessing passes.+ *+ */+TEST_P(QuantWeightsTestP, MatMul_Conv_QuantWeights) {+ {+ const float matmul_scale = 0.02f, matmul_zp = 1.f;+ const float conv_scale = 0.05f, conv_zp = 2.f;++ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, 128});+ auto matmul_weight = make_dq_weights(qtype, ov::Shape{128, 64}, matmul_scale, matmul_zp);+ auto matmul = std::make_shared<ov::op::v0::MatMul>(input, matmul_weight, false, false);++ auto reshape_const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {1, 64, 1, 1});+ auto reshape = std::make_shared<ov::op::v1::Reshape>(matmul, reshape_const, false);+ auto conv_weight = make_dq_weights(qtype, ov::Shape{8, 64, 1, 1}, conv_scale, conv_zp);+ auto conv = std::make_shared<ov::op::v1::Convolution>(reshape,+ conv_weight,+ ov::Strides{1, 1},+ ov::CoordinateDiff{1, 1},+ ov::CoordinateDiff{1, 1},+ ov::Strides{1, 1});++ model = std::make_shared<ov::Model>(ov::OutputVector{conv}, ov::ParameterVector{input});++ auto prep = ov::preprocess::PrePostProcessor(model);++ model = prep.build();++ manager.register_pass<ov::pass::MOCTransformations>(false);+ }++ {+ const float matmul_scale = 0.02f, matmul_zp = 1.f;+ const float conv_scale = 0.05f, conv_zp = 2.f;++ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, 128});+ auto matmul_weight = make_dq_weights(qtype, ov::Shape{128, 64}, matmul_scale, matmul_zp, true);+ auto matmul = std::make_shared<ov::op::v0::MatMul>(input, matmul_weight, false, false);++ auto reshape_const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {1, 64, 1, 1});+ auto reshape = std::make_shared<ov::op::v1::Reshape>(matmul, reshape_const, false);+ auto conv_weight = make_dq_weights(qtype, ov::Shape{8, 64, 1, 1}, conv_scale, conv_zp, true);+ auto conv = std::make_shared<ov::op::v1::Convolution>(reshape,+ conv_weight,+ ov::Strides{1, 1},+ ov::CoordinateDiff{1, 1},+ ov::CoordinateDiff{1, 1},+ ov::Strides{1, 1});+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{conv}, ov::ParameterVector{input});+ }++ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::RUNTIME_KEYS);+ comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES);+}++INSTANTIATE_TEST_SUITE_P(+ QuantWeightTypes,+ QuantWeightsTestP,+ ::testing::Values(i32, u32, i16, u16, i8, u8, u6, i4, u4, nf4, u3, u2, u1, f4e2m1, f8e4m3, f8e5m2, f8e8m0));diff --git a/src/core/src/preprocess/pre_post_process.cpp b/src/core/src/preprocess/pre_post_process.cppindex bb24ea3f1df..ab0144b683e 100644--- a/src/core/src/preprocess/pre_post_process.cpp+++ b/src/core/src/preprocess/pre_post_process.cpp@@ -85,7 +85,7 @@ void transformation_pipeline(std::shared_ptr<ov::Model>& model) {REGISTER_PASS(manager, MarkGatherSubgraph, element::TypeVector{element::f8e4m3}, element::TypeVector{element::u4});REGISTER_PASS(manager,MarkDequantization,- TypeVector{i32, u32, i16, u16, i8, u8, i4, u4, nf4, f4e2m1, f8e4m3, f8e5m2, f8e8m0});+ TypeVector{i32, u32, i16, u16, i8, u8, u6, i4, u4, u3, u2, u1, nf4, f4e2m1, f8e4m3, f8e5m2, f8e8m0});REGISTER_PASS(manager, DisableShapeOfConstantFolding, false);REGISTER_PASS(manager, DisableRandomUniformConstantFolding)// Mark quantized and f16/bf16 compressed constants to prevent CF for them,