From e28c0c66e5918c2155c4351e7dffd7240a22f623 Mon Sep 17 00:00:00 2001 From: eshoguli Date: Wed, 6 Sep 2023 20:24:14 +0100 Subject: [PATCH] [LPT] SpaceToBatch & BatchToSpace implementation --- docs/IE_PLUGIN_DG/layout.xml | 2 + .../low_precision_transformations/lpt.md | 2 + .../pipeline/step3_main.md | 2 + .../step3_main/shape/batch_to_space.md | 3 + .../step3_main/shape/space_to_batch.md | 3 + .../include/low_precision/batch_to_space.hpp | 34 ++++ .../common/fake_quantize_dequantization.hpp | 1 + .../low_precision/common/ie_lpt_exception.hpp | 5 + .../include/low_precision/space_to_batch.hpp | 34 ++++ .../src/batch_to_space.cpp | 66 +++++++ .../src/fake_quantize_dequantization.cpp | 20 +++ .../src/low_precision.cpp | 4 + .../src/markup_precisions.cpp | 3 + .../src/space_to_batch.cpp | 66 +++++++ .../tests/batch_to_space_transformation.cpp | 168 ++++++++++++++++++ .../tests/layer_transformation.hpp | 22 +++ .../simple_low_precision_transformer.hpp | 4 + .../tests/space_to_batch_transformation.cpp | 167 +++++++++++++++++ .../batch_to_space_transformation.cpp | 55 ++++++ .../space_to_batch_transformation.cpp | 53 ++++++ .../batch_to_space_transformation.cpp | 57 ++++++ .../space_to_batch_transformation.cpp | 57 ++++++ .../batch_to_space_transformation.hpp | 44 +++++ .../space_to_batch_transformation.hpp | 44 +++++ .../batch_to_space_transformation.cpp | 60 +++++++ .../space_to_batch_transformation.cpp | 60 +++++++ .../batch_to_space_function.hpp | 36 ++++ .../space_to_batch_function.hpp | 36 ++++ .../src/batch_to_space_function.cpp | 65 +++++++ .../src/space_to_batch_function.cpp | 65 +++++++ 30 files changed, 1238 insertions(+) create mode 100644 docs/IE_PLUGIN_DG/plugin_transformation_pipeline/low_precision_transformations/transformations/step3_main/shape/batch_to_space.md create mode 100644 docs/IE_PLUGIN_DG/plugin_transformation_pipeline/low_precision_transformations/transformations/step3_main/shape/space_to_batch.md create mode 100644 src/common/low_precision_transformations/include/low_precision/batch_to_space.hpp create mode 100644 src/common/low_precision_transformations/include/low_precision/space_to_batch.hpp create mode 100644 src/common/low_precision_transformations/src/batch_to_space.cpp create mode 100644 src/common/low_precision_transformations/src/space_to_batch.cpp create mode 100644 src/common/low_precision_transformations/tests/batch_to_space_transformation.cpp create mode 100644 src/common/low_precision_transformations/tests/space_to_batch_transformation.cpp create mode 100644 src/plugins/intel_cpu/tests/functional/shared_tests_instances/low_precision_transformations/batch_to_space_transformation.cpp create mode 100644 src/plugins/intel_cpu/tests/functional/shared_tests_instances/low_precision_transformations/space_to_batch_transformation.cpp create mode 100644 src/plugins/intel_gpu/tests/functional/shared_tests_instances/low_precision_transformations/batch_to_space_transformation.cpp create mode 100644 src/plugins/intel_gpu/tests/functional/shared_tests_instances/low_precision_transformations/space_to_batch_transformation.cpp create mode 100644 src/tests/functional/plugin/shared/include/low_precision_transformations/batch_to_space_transformation.hpp create mode 100644 src/tests/functional/plugin/shared/include/low_precision_transformations/space_to_batch_transformation.hpp create mode 100644 src/tests/functional/plugin/shared/src/low_precision_transformations/batch_to_space_transformation.cpp create mode 100644 src/tests/functional/plugin/shared/src/low_precision_transformations/space_to_batch_transformation.cpp create mode 100644 src/tests/ngraph_helpers/lpt_ngraph_functions/include/lpt_ngraph_functions/batch_to_space_function.hpp create mode 100644 src/tests/ngraph_helpers/lpt_ngraph_functions/include/lpt_ngraph_functions/space_to_batch_function.hpp create mode 100644 src/tests/ngraph_helpers/lpt_ngraph_functions/src/batch_to_space_function.cpp create mode 100644 src/tests/ngraph_helpers/lpt_ngraph_functions/src/space_to_batch_function.cpp diff --git a/docs/IE_PLUGIN_DG/layout.xml b/docs/IE_PLUGIN_DG/layout.xml index 512e6cc42747d4..03dda0379e060f 100644 --- a/docs/IE_PLUGIN_DG/layout.xml +++ b/docs/IE_PLUGIN_DG/layout.xml @@ -37,6 +37,7 @@ + @@ -62,6 +63,7 @@ + diff --git a/docs/IE_PLUGIN_DG/plugin_transformation_pipeline/low_precision_transformations/lpt.md b/docs/IE_PLUGIN_DG/plugin_transformation_pipeline/low_precision_transformations/lpt.md index ef29530fc221a7..4b8dfd6fd5e933 100644 --- a/docs/IE_PLUGIN_DG/plugin_transformation_pipeline/low_precision_transformations/lpt.md +++ b/docs/IE_PLUGIN_DG/plugin_transformation_pipeline/low_precision_transformations/lpt.md @@ -188,6 +188,7 @@ Transformations: * :doc:`AddTransformation ` * :doc:`AvgPoolTransformation ` * :doc:`ClampTransformation ` +* :doc:`BatchToSpaceTransformation ` * :doc:`ConcatTransformation ` * :doc:`ConvolutionTransformation ` * :doc:`ConvolutionBackpropDataTransformation ` @@ -211,6 +212,7 @@ Transformations: * :doc:`ReshapeTransformation ` * :doc:`SqueezeTransformation ` * :doc:`ShuffleChannelsTransformation ` +* :doc:`SpaceToBatchTransformation ` * :doc:`SplitTransformation ` * :doc:`StridedSliceTransformation ` * :doc:`TransposeTransformation ` diff --git a/docs/IE_PLUGIN_DG/plugin_transformation_pipeline/low_precision_transformations/pipeline/step3_main.md b/docs/IE_PLUGIN_DG/plugin_transformation_pipeline/low_precision_transformations/pipeline/step3_main.md index da8688ba7a7b93..0b4e7208ccdcbb 100644 --- a/docs/IE_PLUGIN_DG/plugin_transformation_pipeline/low_precision_transformations/pipeline/step3_main.md +++ b/docs/IE_PLUGIN_DG/plugin_transformation_pipeline/low_precision_transformations/pipeline/step3_main.md @@ -12,6 +12,7 @@ Main transformations are the majority of low precision transformations. Transfor * :doc:`AddTransformation ` * :doc:`AvgPoolTransformation ` +* :doc:`BatchToSpaceTransformation ` * :doc:`ClampTransformation ` * :doc:`ConcatTransformation ` * :doc:`ConvolutionTransformation ` @@ -34,6 +35,7 @@ Main transformations are the majority of low precision transformations. Transfor * :doc:`ReduceSumTransformation ` * :doc:`ReluTransformation ` * :doc:`ReshapeTransformation ` +* :doc:`SpaceToBatchTransformation ` * :doc:`SqueezeTransformation ` * :doc:`ShuffleChannelsTransformation ` * :doc:`SplitTransformation ` diff --git a/docs/IE_PLUGIN_DG/plugin_transformation_pipeline/low_precision_transformations/transformations/step3_main/shape/batch_to_space.md b/docs/IE_PLUGIN_DG/plugin_transformation_pipeline/low_precision_transformations/transformations/step3_main/shape/batch_to_space.md new file mode 100644 index 00000000000000..e5e2ed95e2a02d --- /dev/null +++ b/docs/IE_PLUGIN_DG/plugin_transformation_pipeline/low_precision_transformations/transformations/step3_main/shape/batch_to_space.md @@ -0,0 +1,3 @@ +# BatchToSpaceTransformation transformation {#openvino_docs_OV_UG_lpt_BatchToSpaceTransformation} + +ngraph::pass::low_precision::BatchToSpaceTransformation class represents the `BatchToSpace` operation transformation. diff --git a/docs/IE_PLUGIN_DG/plugin_transformation_pipeline/low_precision_transformations/transformations/step3_main/shape/space_to_batch.md b/docs/IE_PLUGIN_DG/plugin_transformation_pipeline/low_precision_transformations/transformations/step3_main/shape/space_to_batch.md new file mode 100644 index 00000000000000..e97eac7866531c --- /dev/null +++ b/docs/IE_PLUGIN_DG/plugin_transformation_pipeline/low_precision_transformations/transformations/step3_main/shape/space_to_batch.md @@ -0,0 +1,3 @@ +# SpaceToBatchTransformation transformation {#openvino_docs_OV_UG_lpt_SpaceToBatchTransformation} + +ngraph::pass::low_precision::SpaceToBatchTransformation class represents the `SpaceToBatch` operation transformation. diff --git a/src/common/low_precision_transformations/include/low_precision/batch_to_space.hpp b/src/common/low_precision_transformations/include/low_precision/batch_to_space.hpp new file mode 100644 index 00000000000000..820ac0b898e69e --- /dev/null +++ b/src/common/low_precision_transformations/include/low_precision/batch_to_space.hpp @@ -0,0 +1,34 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include +#include "low_precision/layer_transformation.hpp" + +namespace ngraph { +namespace pass { +namespace low_precision { + +/** + * @ingroup ie_transformation_common_api + * @brief BatchToSpaceTransformation propagates dequantization operations through BatchToSpace operation. + * + * For more details about the transformation, refer to + * [BatchToSpaceTransformation](@ref openvino_docs_OV_UG_lpt_BatchToSpaceTransformation) page + * in the Inference Engine Developer Guide. + */ +class LP_TRANSFORMATIONS_API BatchToSpaceTransformation : public LayerTransformation { +public: + OPENVINO_RTTI("BatchToSpaceTransformation", "0"); + BatchToSpaceTransformation(const Params& params = Params()); + bool canBeTransformed(const TransformationContext& context, std::shared_ptr op) const override; + bool transform(TransformationContext& context, ngraph::pattern::Matcher &m) override; + bool isPrecisionPreserved(std::shared_ptr layer) const noexcept override; +}; + +} // namespace low_precision +} // namespace pass +} // namespace ngraph diff --git a/src/common/low_precision_transformations/include/low_precision/common/fake_quantize_dequantization.hpp b/src/common/low_precision_transformations/include/low_precision/common/fake_quantize_dequantization.hpp index 774e0ac26c9749..b8f43ed151a78a 100644 --- a/src/common/low_precision_transformations/include/low_precision/common/fake_quantize_dequantization.hpp +++ b/src/common/low_precision_transformations/include/low_precision/common/fake_quantize_dequantization.hpp @@ -33,6 +33,7 @@ class LP_TRANSFORMATIONS_API FakeQuantizeDequantization { bool multiplyHasZeroOrDenormal() const; bool isShared() const; bool isLowPrecision() const; + bool isPerTensor() const; std::shared_ptr copyWithNewInput(const std::shared_ptr& input) const; bool checkElementwise(const std::shared_ptr& elementwise) const; diff --git a/src/common/low_precision_transformations/include/low_precision/common/ie_lpt_exception.hpp b/src/common/low_precision_transformations/include/low_precision/common/ie_lpt_exception.hpp index 89d9c1975524e7..f090255f6f3caf 100644 --- a/src/common/low_precision_transformations/include/low_precision/common/ie_lpt_exception.hpp +++ b/src/common/low_precision_transformations/include/low_precision/common/ie_lpt_exception.hpp @@ -14,6 +14,7 @@ * @brief A macro used to throw the exception with a notable description for low precision transformations */ #define THROW_IE_LPT_EXCEPTION(node) throw ::ngraph::pass::low_precision::InferenceEngineLptException(__FILE__, __LINE__, node) +#define THROW_IE_LPT_EXCEPTION_BASE throw ::ngraph::pass::low_precision::InferenceEngineLptException(__FILE__, __LINE__) namespace ngraph { namespace pass { @@ -49,6 +50,10 @@ class LP_TRANSFORMATIONS_API InferenceEngineLptException : public Exception { << filename << ":" << line << " Exception during low precision transformation for " << node << " node with type '" << node.get_type_name() << "', name '" << node.get_friendly_name() << "'. "; } + + InferenceEngineLptException(const std::string& filename, const size_t line) { + *this << filename << ":" << line << " Exception during low precision transformation. "; + } }; } // namespace low_precision diff --git a/src/common/low_precision_transformations/include/low_precision/space_to_batch.hpp b/src/common/low_precision_transformations/include/low_precision/space_to_batch.hpp new file mode 100644 index 00000000000000..8f866dff2341c1 --- /dev/null +++ b/src/common/low_precision_transformations/include/low_precision/space_to_batch.hpp @@ -0,0 +1,34 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include +#include "low_precision/layer_transformation.hpp" + +namespace ngraph { +namespace pass { +namespace low_precision { + +/** + * @ingroup ie_transformation_common_api + * @brief SpaceToBatchTransformation propagates dequantization operations through SpaceToBatch operation. + * + * For more details about the transformation, refer to + * [SpaceToBatchTransformation](@ref openvino_docs_OV_UG_lpt_SpaceToBatchTransformation) page + * in the Inference Engine Developer Guide. + */ +class LP_TRANSFORMATIONS_API SpaceToBatchTransformation : public LayerTransformation { +public: + OPENVINO_RTTI("SpaceToBatchTransformation", "0"); + SpaceToBatchTransformation(const Params& params = Params()); + bool canBeTransformed(const TransformationContext& context, std::shared_ptr op) const override; + bool transform(TransformationContext& context, ngraph::pattern::Matcher &m) override; + bool isPrecisionPreserved(std::shared_ptr layer) const noexcept override; +}; + +} // namespace low_precision +} // namespace pass +} // namespace ngraph diff --git a/src/common/low_precision_transformations/src/batch_to_space.cpp b/src/common/low_precision_transformations/src/batch_to_space.cpp new file mode 100644 index 00000000000000..b171fda76d3164 --- /dev/null +++ b/src/common/low_precision_transformations/src/batch_to_space.cpp @@ -0,0 +1,66 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "low_precision/batch_to_space.hpp" + +#include +#include +#include +#include + +#include + +#include "low_precision/network_helper.hpp" +#include "itt.hpp" + +namespace ngraph { +namespace pass { +namespace low_precision { + +BatchToSpaceTransformation::BatchToSpaceTransformation(const Params& params) : LayerTransformation(params) { + MATCHER_SCOPE(BatchToSpaceTransformation); + auto matcher = pattern::wrap_type(); + + ngraph::graph_rewrite_callback callback = [this](pattern::Matcher& m) { + auto op = m.get_match_root(); + if (transformation_callback(op)) { + return false; + } + return transform(*context, m); + }; + + auto m = std::make_shared(matcher, matcher_name); + this->register_matcher(m, callback); +} + +bool BatchToSpaceTransformation::canBeTransformed(const TransformationContext& context, std::shared_ptr op) const { + if (!LayerTransformation::canBeTransformed(context, op)) { + return false; + } + + const FakeQuantizeDequantization dequantization = NetworkHelper::getDequantization(op, defaultPrecisions); + if (dequantization.empty()) { + return false; + } + + return dequantization.isPerTensor(); +} + +bool BatchToSpaceTransformation::transform(TransformationContext& context, ngraph::pattern::Matcher& m) { + if (!canBeTransformed(context, m.get_match_root())) { + return false; + } + + const std::shared_ptr pooling = NetworkHelper::separateInStandaloneBranch(m.get_match_root(), defaultPrecisions); + moveDequantizationAfter(context, pooling, NetworkHelper::getDequantization(pooling, defaultPrecisions), false); + return true; +} + +bool BatchToSpaceTransformation::isPrecisionPreserved(std::shared_ptr layer) const noexcept { + return true; +} + +} // namespace low_precision +} // namespace pass +} // namespace ngraph diff --git a/src/common/low_precision_transformations/src/fake_quantize_dequantization.cpp b/src/common/low_precision_transformations/src/fake_quantize_dequantization.cpp index 071d10ac51eadd..305686e571d3c6 100644 --- a/src/common/low_precision_transformations/src/fake_quantize_dequantization.cpp +++ b/src/common/low_precision_transformations/src/fake_quantize_dequantization.cpp @@ -89,6 +89,26 @@ bool FakeQuantizeDequantization::isLowPrecision() const { return DataPrecision::isSupported(data.get_element_type()); } +bool FakeQuantizeDequantization::isPerTensor() const { + if (multiplyConstant == nullptr) { + THROW_IE_LPT_EXCEPTION_BASE << "multiply constant can not be empty"; + } + + const std::vector& scales = multiplyConstant->cast_vector(); + if (scales.size() != 1ull) { + return false; + } + + if (subtractConstant != nullptr) { + const std::vector& scales = subtractConstant->cast_vector(); + if (scales.size() != 1ull) { + return false; + } + } + + return true; +} + bool FakeQuantizeDequantization::checkShape(const std::shared_ptr& elementwise) { std::shared_ptr convert; std::shared_ptr constant; diff --git a/src/common/low_precision_transformations/src/low_precision.cpp b/src/common/low_precision_transformations/src/low_precision.cpp index 7ca3b64775c5b2..cb2fffac24a07d 100644 --- a/src/common/low_precision_transformations/src/low_precision.cpp +++ b/src/common/low_precision_transformations/src/low_precision.cpp @@ -43,6 +43,7 @@ #include "low_precision/add.hpp" #include "low_precision/assign_and_read_value.hpp" #include "low_precision/avg_pool.hpp" +#include "low_precision/batch_to_space.hpp" #include "low_precision/clamp.hpp" #include "low_precision/convolution.hpp" #include "low_precision/convolution_backprop_data.hpp" @@ -66,6 +67,7 @@ #include "low_precision/relu.hpp" #include "low_precision/squeeze.hpp" #include "low_precision/subtract.hpp" +#include "low_precision/space_to_batch.hpp" #include "low_precision/split.hpp" #include "low_precision/shuffle_channels.hpp" #include "low_precision/strided_slice.hpp" @@ -237,6 +239,7 @@ bool ngraph::pass::low_precision::LowPrecision::run_on_model(const std::shared_p ADD_MATCHER(common, AddTransformation, params) ADD_MATCHER(common, AssignAndReadValueTransformation, f, params) ADD_MATCHER(common, AvgPoolTransformation, params) + ADD_MATCHER(common, BatchToSpaceTransformation, params) ADD_MATCHER(common, ClampTransformation, params) ADD_MATCHER(common, ConcatTransformation, params) ADD_MATCHER(common, ConvolutionTransformation, params) @@ -262,6 +265,7 @@ bool ngraph::pass::low_precision::LowPrecision::run_on_model(const std::shared_p ADD_MATCHER(common, ReshapeTransformation, params) ADD_MATCHER(common, SqueezeTransformation, params) ADD_MATCHER(common, ShuffleChannelsTransformation, params) + ADD_MATCHER(common, SpaceToBatchTransformation, params) ADD_MATCHER(common, SplitTransformation, params) ADD_MATCHER(common, StridedSliceTransformation, params) ADD_MATCHER(common, TransposeTransformation, params) diff --git a/src/common/low_precision_transformations/src/markup_precisions.cpp b/src/common/low_precision_transformations/src/markup_precisions.cpp index 3b942eb1a86c59..481d32904f9e9e 100644 --- a/src/common/low_precision_transformations/src/markup_precisions.cpp +++ b/src/common/low_precision_transformations/src/markup_precisions.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -152,10 +153,12 @@ bool ngraph::pass::low_precision::MarkupPrecisions::isPrecisionPreserved(const s { name() }, { name() }, // TODO: there are conditions + { name() }, { name() }, { name() }, { name() }, { name() }, + { name() }, { name() }, { name() }, { name() }, diff --git a/src/common/low_precision_transformations/src/space_to_batch.cpp b/src/common/low_precision_transformations/src/space_to_batch.cpp new file mode 100644 index 00000000000000..b993cc6ba5721d --- /dev/null +++ b/src/common/low_precision_transformations/src/space_to_batch.cpp @@ -0,0 +1,66 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "low_precision/space_to_batch.hpp" + +#include +#include +#include +#include + +#include + +#include "low_precision/network_helper.hpp" +#include "itt.hpp" + +namespace ngraph { +namespace pass { +namespace low_precision { + +SpaceToBatchTransformation::SpaceToBatchTransformation(const Params& params) : LayerTransformation(params) { + MATCHER_SCOPE(SpaceToBatchTransformation); + auto matcher = pattern::wrap_type(); + + ngraph::graph_rewrite_callback callback = [this](pattern::Matcher& m) { + auto op = m.get_match_root(); + if (transformation_callback(op)) { + return false; + } + return transform(*context, m); + }; + + auto m = std::make_shared(matcher, matcher_name); + this->register_matcher(m, callback); +} + +bool SpaceToBatchTransformation::canBeTransformed(const TransformationContext& context, std::shared_ptr op) const { + if (!LayerTransformation::canBeTransformed(context, op)) { + return false; + } + + const FakeQuantizeDequantization dequantization = NetworkHelper::getDequantization(op, defaultPrecisions); + if (dequantization.empty()) { + return false; + } + + return dequantization.isPerTensor(); +} + +bool SpaceToBatchTransformation::transform(TransformationContext& context, ngraph::pattern::Matcher& m) { + if (!canBeTransformed(context, m.get_match_root())) { + return false; + } + + const std::shared_ptr pooling = NetworkHelper::separateInStandaloneBranch(m.get_match_root(), defaultPrecisions); + moveDequantizationAfter(context, pooling, NetworkHelper::getDequantization(pooling, defaultPrecisions), false); + return true; +} + +bool SpaceToBatchTransformation::isPrecisionPreserved(std::shared_ptr layer) const noexcept { + return true; +} + +} // namespace low_precision +} // namespace pass +} // namespace ngraph diff --git a/src/common/low_precision_transformations/tests/batch_to_space_transformation.cpp b/src/common/low_precision_transformations/tests/batch_to_space_transformation.cpp new file mode 100644 index 00000000000000..0d98b11bc9303b --- /dev/null +++ b/src/common/low_precision_transformations/tests/batch_to_space_transformation.cpp @@ -0,0 +1,168 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "layer_transformation.hpp" + +#include +#include + +#include + +#include +#include +#include +#include + +#include "common_test_utils/ov_test_utils.hpp" +#include "simple_low_precision_transformer.hpp" +#include "lpt_ngraph_functions/batch_to_space_function.hpp" +#include "lpt_ngraph_functions/common/dequantization_operations.hpp" + + +using namespace testing; +using namespace ov::pass; +using namespace ov; + +class BatchToSpaceTransformationTestValues { +public: + class Actual { + public: + ov::element::Type input_type; + ngraph::builder::subgraph::DequantizationOperations dequantization_before; + ov::element::Type preicsionAfterOperation; + ngraph::builder::subgraph::DequantizationOperations dequantization_after; + }; + + class Expected { + public: + ov::element::Type input_type; + ngraph::builder::subgraph::DequantizationOperations dequantization_before; + ov::element::Type preicsionAfterOperation; + ngraph::builder::subgraph::DequantizationOperations dequantization_after; + }; + + TestTransformationParams params; + std::vector block_shape; + std::vector crops_begin; + std::vector crops_end; + Actual actual; + Expected expected; +}; + +typedef std::tuple< + ngraph::PartialShape, + BatchToSpaceTransformationTestValues> BatchToSpaceTransformationParams; + +class BatchToSpaceTransformation : public LayerTransformation, + public testing::WithParamInterface { +public: + void SetUp() override { + const ngraph::PartialShape input_shape = std::get<0>(GetParam()); + const BatchToSpaceTransformationTestValues test_values = std::get<1>(GetParam()); + + actualFunction = ngraph::builder::subgraph::BatchToSpaceFunction::get( + input_shape, + test_values.actual.input_type, + test_values.actual.dequantization_before, + test_values.block_shape, + test_values.crops_begin, + test_values.crops_end, + test_values.actual.dequantization_after); + + SimpleLowPrecisionTransformer transform; + transform.add(test_values.params); + transform.transform(actualFunction); + + referenceFunction = ngraph::builder::subgraph::BatchToSpaceFunction::get( + input_shape, + test_values.expected.input_type, + test_values.expected.dequantization_before, + test_values.block_shape, + test_values.crops_begin, + test_values.crops_end, + test_values.expected.dequantization_after); + } + + static std::string getTestCaseName(testing::TestParamInfo obj) { + const ngraph::PartialShape shape = std::get<0>(obj.param); + const BatchToSpaceTransformationTestValues testValues = std::get<1>(obj.param); + + std::ostringstream result; + result << testValues.actual.input_type << "_"<< + shape << "_" << toString(testValues.params) << "_" << + testValues.actual.dequantization_before << "_" << + testValues.actual.dequantization_after << "_" << + testValues.expected.dequantization_before << "_" << + testValues.expected.dequantization_after << "_"; + return result.str(); + } +}; + +TEST_P(BatchToSpaceTransformation, CompareFunctions) { + actualFunction->validate_nodes_and_infer_types(); + + const auto testValues = std::get<1>(GetParam()); + const auto output_precision = LayerTransformation::get_output_precision(actualFunction); + ASSERT_EQ(testValues.expected.preicsionAfterOperation, output_precision); + + auto res = compare_functions(actualFunction, referenceFunction, true, false, false); + ASSERT_TRUE(res.first) << res.second; + + ASSERT_TRUE(LayerTransformation::allNamesAreUnique(actualFunction)) << "Not all names are unique"; +} + +namespace testValues { +const std::vector input_shapes = { + {4, 3, 50, 86} +}; + +const std::vector test_values = { + // per-tensor dequantization + { + LayerTransformation::createParamsU8I8(), + {1, 1, 2, 2}, + {0, 0, 0, 0}, + {0, 0, 0, 1}, + { + ov::element::u8, + { ov::element::f32, {128.f}, {0.01f}}, + ov::element::f32, + {} + }, + { + ov::element::u8, + {}, + ov::element::u8, + { ov::element::f32, {128.f}, {0.01f}} + } + }, + // per-channel dequantization + { + LayerTransformation::createParamsU8I8(), + {1, 1, 2, 2}, + {0, 0, 0, 0}, + {0, 0, 0, 1}, + { + ov::element::u8, + {ov::element::f32, {{128.f, 64.f, 32.f}}, {{0.02f, 0.01f, 0.03f}}}, + ov::element::f32, + {} + }, + { + ov::element::u8, + {ov::element::f32, {{128.f, 64.f, 32.f}}, { {0.02f, 0.01f, 0.03f} }}, + ov::element::f32, + {} + } + } +}; + +INSTANTIATE_TEST_SUITE_P( + smoke_LPT, + BatchToSpaceTransformation, + ::testing::Combine( + ::testing::ValuesIn(input_shapes), + ::testing::ValuesIn(test_values)), + BatchToSpaceTransformation::getTestCaseName); +} // namespace testValues diff --git a/src/common/low_precision_transformations/tests/layer_transformation.hpp b/src/common/low_precision_transformations/tests/layer_transformation.hpp index be51793f91e7ef..9dae0c321854aa 100644 --- a/src/common/low_precision_transformations/tests/layer_transformation.hpp +++ b/src/common/low_precision_transformations/tests/layer_transformation.hpp @@ -217,6 +217,28 @@ class LayerTransformation : public ov::test::TestsCommon { return true; } + template + static ov::element::Type get_output_precision(const std::shared_ptr& model) { + auto result = ov::element::undefined; + for (const auto& op : model->get_ops()) { + if (ov::is_type(op)) { + if (result != ov::element::undefined) { + THROW_IE_LPT_EXCEPTION(*op) << "not one operation"; + } + if (op->get_output_size() != 1) { + THROW_IE_LPT_EXCEPTION(*op) << "not expected outputs count"; + } + result = op->get_output_element_type(0); + } + } + + if (result == ov::element::undefined) { + THROW_IE_LPT_EXCEPTION_BASE << "operation was not found"; + } + + return result; + } + protected: std::shared_ptr actualFunction; std::shared_ptr referenceFunction; diff --git a/src/common/low_precision_transformations/tests/simple_low_precision_transformer.hpp b/src/common/low_precision_transformations/tests/simple_low_precision_transformer.hpp index 3beb10915e8b12..53b55f22195efe 100644 --- a/src/common/low_precision_transformations/tests/simple_low_precision_transformer.hpp +++ b/src/common/low_precision_transformations/tests/simple_low_precision_transformer.hpp @@ -29,6 +29,10 @@ class SimpleLowPrecisionTransformer : public ngraph::pass::FunctionPass{ void add(const std::shared_ptr function, const TestTransformationParams& params) { commonGraphRewrite->add_matcher(function, TestTransformationParams::toParams(params)); } + template + void add(const TestTransformationParams& params) { + commonGraphRewrite->add_matcher(TestTransformationParams::toParams(params)); + } void transform(std::shared_ptr& model); bool run_on_model(const std::shared_ptr& m) override; diff --git a/src/common/low_precision_transformations/tests/space_to_batch_transformation.cpp b/src/common/low_precision_transformations/tests/space_to_batch_transformation.cpp new file mode 100644 index 00000000000000..25d20cfe80faec --- /dev/null +++ b/src/common/low_precision_transformations/tests/space_to_batch_transformation.cpp @@ -0,0 +1,167 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "layer_transformation.hpp" + +#include +#include + +#include + +#include +#include +#include +#include + +#include "common_test_utils/ov_test_utils.hpp" +#include "simple_low_precision_transformer.hpp" +#include "lpt_ngraph_functions/space_to_batch_function.hpp" +#include "lpt_ngraph_functions/common/dequantization_operations.hpp" + + +using namespace testing; +using namespace ov::pass; +using namespace ov; + +class SpaceToBatchTransformationTestValues { +public: + class Actual { + public: + ov::element::Type input_type; + ngraph::builder::subgraph::DequantizationOperations dequantization_before; + ov::element::Type preicsionAfterOperation; + ngraph::builder::subgraph::DequantizationOperations dequantization_after; + }; + + class Expected { + public: + ov::element::Type input_type; + ngraph::builder::subgraph::DequantizationOperations dequantization_before; + ov::element::Type preicsionAfterOperation; + ngraph::builder::subgraph::DequantizationOperations dequantization_after; + }; + + TestTransformationParams params; + std::vector block_shape; + std::vector pads_begin; + std::vector pads_end; + Actual actual; + Expected expected; +}; + +typedef std::tuple< + ngraph::PartialShape, + SpaceToBatchTransformationTestValues> SpaceToBatchTransformationParams; + +class SpaceToBatchTransformation : public LayerTransformation, public testing::WithParamInterface { +public: + void SetUp() override { + const ngraph::PartialShape input_shape = std::get<0>(GetParam()); + const SpaceToBatchTransformationTestValues test_values = std::get<1>(GetParam()); + + actualFunction = ngraph::builder::subgraph::SpaceToBatchFunction::get( + input_shape, + test_values.actual.input_type, + test_values.actual.dequantization_before, + test_values.block_shape, + test_values.pads_begin, + test_values.pads_end, + test_values.actual.dequantization_after); + + SimpleLowPrecisionTransformer transform; + transform.add(test_values.params); + transform.transform(actualFunction); + + referenceFunction = ngraph::builder::subgraph::SpaceToBatchFunction::get( + input_shape, + test_values.expected.input_type, + test_values.expected.dequantization_before, + test_values.block_shape, + test_values.pads_begin, + test_values.pads_end, + test_values.expected.dequantization_after); + } + + static std::string getTestCaseName(testing::TestParamInfo obj) { + const ngraph::PartialShape shape = std::get<0>(obj.param); + const SpaceToBatchTransformationTestValues testValues = std::get<1>(obj.param); + + std::ostringstream result; + result << testValues.actual.input_type << "_"<< + shape << "_" << toString(testValues.params) << "_" << + testValues.actual.dequantization_before << "_" << + testValues.actual.dequantization_after << "_" << + testValues.expected.dequantization_before << "_" << + testValues.expected.dequantization_after << "_"; + return result.str(); + } +}; + +TEST_P(SpaceToBatchTransformation, CompareFunctions) { + actualFunction->validate_nodes_and_infer_types(); + + const auto testValues = std::get<1>(GetParam()); + const auto output_precision = LayerTransformation::get_output_precision(actualFunction); + ASSERT_EQ(testValues.expected.preicsionAfterOperation, output_precision); + + auto res = compare_functions(actualFunction, referenceFunction, true, false, false); + ASSERT_TRUE(res.first) << res.second; + + ASSERT_TRUE(LayerTransformation::allNamesAreUnique(actualFunction)) << "Not all names are unique"; +} + +namespace testValues { +const std::vector shapes = { + {1, 3, 100, 171}, +}; + +const std::vector testValues = { + // per-tensor dequantization + { + LayerTransformation::createParamsU8I8(), + {1, 1, 2, 2}, + {0, 0, 2, 2}, + {0, 0, 2, 3}, + { + ov::element::u8, + { ov::element::f32, {128.f}, {0.01f}}, + ov::element::f32, + {} + }, + { + ov::element::u8, + {}, + ov::element::u8, + { ov::element::f32, {128.f}, {0.01f}} + } + }, + // per-channel dequantization + { + LayerTransformation::createParamsU8I8(), + {1, 1, 2, 2}, + {0, 0, 2, 2}, + {0, 0, 2, 3}, + { + ov::element::u8, + {ov::element::f32, {{128.f, 64.f, 32.f}}, {{0.02f, 0.01f, 0.03f}}}, + ov::element::f32, + {} + }, + { + ov::element::u8, + {ov::element::f32, {{128.f, 64.f, 32.f}}, { {0.02f, 0.01f, 0.03f} }}, + ov::element::f32, + {} + } + } +}; + +INSTANTIATE_TEST_SUITE_P( + smoke_LPT, + SpaceToBatchTransformation, + ::testing::Combine( + ::testing::ValuesIn(shapes), + ::testing::ValuesIn(testValues)), + SpaceToBatchTransformation::getTestCaseName); +} // namespace testValues diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/low_precision_transformations/batch_to_space_transformation.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/low_precision_transformations/batch_to_space_transformation.cpp new file mode 100644 index 00000000000000..ccabe0179c3407 --- /dev/null +++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/low_precision_transformations/batch_to_space_transformation.cpp @@ -0,0 +1,55 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include "low_precision_transformations/batch_to_space_transformation.hpp" +#include "common_test_utils/test_constants.hpp" + +using namespace LayerTestsDefinitions; + +namespace { +const std::vector netPrecisions = { + ngraph::element::f32, + // ngraph::element::f16 +}; + +const std::vector trasformationParamValues = { + LayerTestsUtils::LayerTransformationParamsNGraphFactory::createParams() +}; + +const std::vector params = { + // per-tensor quantization + { + { 4, 3, 50, 86 }, + { 1, 1, 2, 2 }, { 0, 0, 0, 0 }, { 0, 0, 0, 1 }, + { 256ul, ngraph::Shape{ 1, 1, 1, 1 }, { 0.f }, { 2.55f }, { 0.f }, { 2.55f } }, + "BatchToSpace", + "U8" + }, + // per-channel quantization + { + { 4, 3, 50, 86 }, + { 1, 1, 2, 2 }, { 0, 0, 0, 0 }, { 0, 0, 0, 1 }, + { + 256ul, + ngraph::Shape{ 1, 3, 1, 1 }, + { 0.f, 0.f, 0.f }, + { 255.f, 255.f/2.f, 255.f/3.f }, + { 0.f, 0.f, 0.f }, + { 255.f, 255.f/2.f, 255.f/3.f }, + }, + "BatchToSpace", + "FP32" + } +}; + +INSTANTIATE_TEST_SUITE_P(smoke_LPT, BatchToSpaceTransformation, + ::testing::Combine( + ::testing::ValuesIn(netPrecisions), + ::testing::Values(ov::test::utils::DEVICE_CPU), + ::testing::ValuesIn(trasformationParamValues), + ::testing::ValuesIn(params)), + BatchToSpaceTransformation::getTestCaseName); +} // namespace diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/low_precision_transformations/space_to_batch_transformation.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/low_precision_transformations/space_to_batch_transformation.cpp new file mode 100644 index 00000000000000..bee750520a2f58 --- /dev/null +++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/low_precision_transformations/space_to_batch_transformation.cpp @@ -0,0 +1,53 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include "low_precision_transformations/space_to_batch_transformation.hpp" +#include "common_test_utils/test_constants.hpp" + +using namespace LayerTestsDefinitions; + +namespace { +const std::vector netPrecisions = { + ngraph::element::f32, + // ngraph::element::f16 +}; + +const std::vector trasformationParamValues = { + LayerTestsUtils::LayerTransformationParamsNGraphFactory::createParams() +}; + +const std::vector params = { + { + { 1, 3, 100, 171 }, + { 1, 1, 2, 2 }, { 0, 0, 2, 2 }, { 0, 0, 2, 3 }, + { 256ul, ngraph::Shape{ 1, 1, 1, 1 }, { 0.f }, { 2.55f }, { 0.f }, { 2.55f } }, + "SpaceToBatch", + "U8" + }, + { + {1, 3, 100, 171}, + { 1, 1, 2, 2 }, { 0, 0, 2, 2 }, { 0, 0, 2, 3 }, + { + 256ul, + ngraph::Shape{ 1, 3, 1, 1 }, + { 0.f, 0.f, 0.f }, + { 255.f, 255.f/2.f, 255.f/3.f }, + { 0.f, 0.f, 0.f }, + { 255.f, 255.f/2.f, 255.f/3.f }, + }, + "SpaceToBatch", + "FP32" + } +}; + +INSTANTIATE_TEST_SUITE_P(smoke_LPT, SpaceToBatchTransformation, + ::testing::Combine( + ::testing::ValuesIn(netPrecisions), + ::testing::Values(ov::test::utils::DEVICE_CPU), + ::testing::ValuesIn(trasformationParamValues), + ::testing::ValuesIn(params)), + SpaceToBatchTransformation::getTestCaseName); +} // namespace diff --git a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/low_precision_transformations/batch_to_space_transformation.cpp b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/low_precision_transformations/batch_to_space_transformation.cpp new file mode 100644 index 00000000000000..fe9f298c4e100c --- /dev/null +++ b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/low_precision_transformations/batch_to_space_transformation.cpp @@ -0,0 +1,57 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include "low_precision_transformations/batch_to_space_transformation.hpp" +#include "common_test_utils/test_constants.hpp" + +using namespace LayerTestsDefinitions; + +namespace { +const std::vector netPrecisions = { + ngraph::element::f32, + ngraph::element::f16 +}; + +const std::vector trasformationParamValues = { + LayerTestsUtils::LayerTransformationParamsNGraphFactory::createParams(), +}; + +const std::vector params = { + { + { 4, 3, 50, 86 }, + { 1, 1, 2, 2 }, { 0, 0, 0, 0 }, { 0, 0, 0, 1 }, + { 256ul, ngraph::Shape{ 1, 1, 1, 1 }, { 0.f }, { 2.55f }, { 0.f }, { 2.55f } }, + "batch_to_space", + "U8" + }, + { + { 4, 3, 50, 86 }, + { 1, 1, 2, 2 }, { 0, 0, 0, 0 }, { 0, 0, 0, 1 }, + { + 256ul, + ngraph::Shape{ 1, 3, 1, 1 }, + { 0.f, 0.f, 0.f }, + { 255.f, 255.f/2.f, 255.f/3.f }, + { 0.f, 0.f, 0.f }, + { 255.f, 255.f/2.f, 255.f/3.f }, + }, + "batch_to_space", + "FP32" + } +}; + +INSTANTIATE_TEST_SUITE_P(smoke_LPT, BatchToSpaceTransformation, + ::testing::Combine( + ::testing::ValuesIn(netPrecisions), + ::testing::Values(ov::test::utils::DEVICE_GPU), + ::testing::ValuesIn(trasformationParamValues), + ::testing::ValuesIn(params)), + BatchToSpaceTransformation::getTestCaseName); +} // namespace + + + + diff --git a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/low_precision_transformations/space_to_batch_transformation.cpp b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/low_precision_transformations/space_to_batch_transformation.cpp new file mode 100644 index 00000000000000..a3850df0986dc8 --- /dev/null +++ b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/low_precision_transformations/space_to_batch_transformation.cpp @@ -0,0 +1,57 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include "low_precision_transformations/space_to_batch_transformation.hpp" +#include "common_test_utils/test_constants.hpp" + +using namespace LayerTestsDefinitions; + +namespace { +const std::vector netPrecisions = { + ngraph::element::f32, + ngraph::element::f16 +}; + +const std::vector trasformationParamValues = { + LayerTestsUtils::LayerTransformationParamsNGraphFactory::createParams(), +}; + +const std::vector params = { + { + { 1, 3, 100, 171 }, + { 1, 1, 2, 2 }, { 0, 0, 2, 2 }, { 0, 0, 2, 3 }, + { 256ul, ngraph::Shape{ 1, 1, 1, 1 }, { 0.f }, { 2.55f }, { 0.f }, { 2.55f } }, + "space_to_batch", + "U8" + }, + { + {1, 3, 100, 171}, + { 1, 1, 2, 2 }, { 0, 0, 2, 2 }, { 0, 0, 2, 3 }, + { + 256ul, + ngraph::Shape{ 1, 3, 1, 1 }, + { 0.f, 0.f, 0.f }, + { 255.f, 255.f/2.f, 255.f/3.f }, + { 0.f, 0.f, 0.f }, + { 255.f, 255.f/2.f, 255.f/3.f }, + }, + "space_to_batch", + "FP32" + } +}; + +INSTANTIATE_TEST_SUITE_P(smoke_LPT, SpaceToBatchTransformation, + ::testing::Combine( + ::testing::ValuesIn(netPrecisions), + ::testing::Values(ov::test::utils::DEVICE_GPU), + ::testing::ValuesIn(trasformationParamValues), + ::testing::ValuesIn(params)), + SpaceToBatchTransformation::getTestCaseName); +} // namespace + + + + diff --git a/src/tests/functional/plugin/shared/include/low_precision_transformations/batch_to_space_transformation.hpp b/src/tests/functional/plugin/shared/include/low_precision_transformations/batch_to_space_transformation.hpp new file mode 100644 index 00000000000000..90f80b08240fe3 --- /dev/null +++ b/src/tests/functional/plugin/shared/include/low_precision_transformations/batch_to_space_transformation.hpp @@ -0,0 +1,44 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include + +#include "shared_test_classes/base/low_precision_transformations/layer_transformation.hpp" +#include "lpt_ngraph_functions/common/fake_quantize_on_data.hpp" + + +namespace LayerTestsDefinitions { +class BatchToSpaceTransformationParam { +public: + ngraph::PartialShape input_shape; + std::vector block_shape; + std::vector crops_begin; + std::vector crops_end; + ngraph::builder::subgraph::FakeQuantizeOnData fake_quantize; + std::string layer_type; + std::string expected_kernel_type; +}; + +typedef std::tuple< + ngraph::element::Type, + std::string, + ngraph::pass::low_precision::LayerTransformation::Params, + BatchToSpaceTransformationParam +> BatchToSpaceTransformationParams; + +class BatchToSpaceTransformation : + public testing::WithParamInterface, + public LayerTestsUtils::LayerTransformation { +public: + static std::string getTestCaseName(const testing::TestParamInfo& obj); + +protected: + void SetUp() override; + void Run() override; +}; + +} // namespace LayerTestsDefinitions diff --git a/src/tests/functional/plugin/shared/include/low_precision_transformations/space_to_batch_transformation.hpp b/src/tests/functional/plugin/shared/include/low_precision_transformations/space_to_batch_transformation.hpp new file mode 100644 index 00000000000000..65580a6123d78d --- /dev/null +++ b/src/tests/functional/plugin/shared/include/low_precision_transformations/space_to_batch_transformation.hpp @@ -0,0 +1,44 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include + +#include "shared_test_classes/base/low_precision_transformations/layer_transformation.hpp" +#include "lpt_ngraph_functions/common/fake_quantize_on_data.hpp" + + +namespace LayerTestsDefinitions { +class SpaceToBatchTransformationParam { +public: + ngraph::PartialShape input_shape; + std::vector block_shape; + std::vector pads_begin; + std::vector pads_end; + ngraph::builder::subgraph::FakeQuantizeOnData fake_quantize; + std::string layer_type; + std::string expected_kernel_type; +}; + +typedef std::tuple< + ngraph::element::Type, + std::string, + ngraph::pass::low_precision::LayerTransformation::Params, + SpaceToBatchTransformationParam +> SpaceToBatchTransformationParams; + +class SpaceToBatchTransformation : + public testing::WithParamInterface, + public LayerTestsUtils::LayerTransformation { +public: + static std::string getTestCaseName(const testing::TestParamInfo& obj); + +protected: + void SetUp() override; + void Run() override; +}; + +} // namespace LayerTestsDefinitions diff --git a/src/tests/functional/plugin/shared/src/low_precision_transformations/batch_to_space_transformation.cpp b/src/tests/functional/plugin/shared/src/low_precision_transformations/batch_to_space_transformation.cpp new file mode 100644 index 00000000000000..88bd97371ff7ac --- /dev/null +++ b/src/tests/functional/plugin/shared/src/low_precision_transformations/batch_to_space_transformation.cpp @@ -0,0 +1,60 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "low_precision_transformations/batch_to_space_transformation.hpp" + +#include +#include +#include + +#include +#include "lpt_ngraph_functions/batch_to_space_function.hpp" + +namespace LayerTestsDefinitions { + +std::string BatchToSpaceTransformation::getTestCaseName(const testing::TestParamInfo& obj) { + ngraph::element::Type input_type; + std::string target_device; + ngraph::pass::low_precision::LayerTransformation::Params params; + BatchToSpaceTransformationParam param; + std::tie(input_type, target_device, params, param) = obj.param; + + std::ostringstream result; + result << input_type << "_" << target_device << "_" << toString(params) << "_" << param.input_shape << "_" << param.fake_quantize; + return result.str(); +} + +void BatchToSpaceTransformation::SetUp() { + ngraph::element::Type input_type; + ngraph::pass::low_precision::LayerTransformation::Params params; + BatchToSpaceTransformationParam param; + std::tie(input_type, targetDevice, params, param) = this->GetParam(); + + function = ngraph::builder::subgraph::BatchToSpaceFunction::get( + param.input_shape, + input_type, + param.fake_quantize, + param.block_shape, + param.crops_begin, + param.crops_end); +} + +void BatchToSpaceTransformation::Run() { + LayerTestsCommon::Run(); + + const auto params = std::get<3>(GetParam()); + auto actual_type = getRuntimePrecisionByType(params.layer_type); + const auto expected_type = params.expected_kernel_type; + if ((expected_type == "FP32") && (actual_type == "FP16")) { + actual_type = "FP32"; + } + EXPECT_EQ(actual_type, expected_type); +} + +TEST_P(BatchToSpaceTransformation, CompareWithRefImpl) { + SKIP_IF_CURRENT_TEST_IS_DISABLED(); + Run(); +}; + +} // namespace LayerTestsDefinitions diff --git a/src/tests/functional/plugin/shared/src/low_precision_transformations/space_to_batch_transformation.cpp b/src/tests/functional/plugin/shared/src/low_precision_transformations/space_to_batch_transformation.cpp new file mode 100644 index 00000000000000..a65e380786e225 --- /dev/null +++ b/src/tests/functional/plugin/shared/src/low_precision_transformations/space_to_batch_transformation.cpp @@ -0,0 +1,60 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "low_precision_transformations/space_to_batch_transformation.hpp" + +#include +#include +#include + +#include +#include "lpt_ngraph_functions/space_to_batch_function.hpp" + +namespace LayerTestsDefinitions { + +std::string SpaceToBatchTransformation::getTestCaseName(const testing::TestParamInfo& obj) { + ngraph::element::Type input_type; + std::string target_device; + ngraph::pass::low_precision::LayerTransformation::Params params; + SpaceToBatchTransformationParam param; + std::tie(input_type, target_device, params, param) = obj.param; + + std::ostringstream result; + result << input_type << "_" << target_device << "_" << toString(params) << "_" << param.input_shape << "_" << param.fake_quantize; + return result.str(); +} + +void SpaceToBatchTransformation::SetUp() { + ngraph::element::Type input_type; + ngraph::pass::low_precision::LayerTransformation::Params params; + SpaceToBatchTransformationParam param; + std::tie(input_type, targetDevice, params, param) = this->GetParam(); + + function = ngraph::builder::subgraph::SpaceToBatchFunction::get( + param.input_shape, + input_type, + param.fake_quantize, + param.block_shape, + param.pads_begin, + param.pads_end); +} + +void SpaceToBatchTransformation::Run() { + LayerTestsCommon::Run(); + + const auto params = std::get<3>(GetParam()); + auto actual_type = getRuntimePrecisionByType(params.layer_type); + const auto expected_type = params.expected_kernel_type; + if ((expected_type == "FP32") && (actual_type == "FP16")) { + actual_type = "FP32"; + } + EXPECT_EQ(actual_type, expected_type); +} + +TEST_P(SpaceToBatchTransformation, CompareWithRefImpl) { + SKIP_IF_CURRENT_TEST_IS_DISABLED(); + Run(); +}; + +} // namespace LayerTestsDefinitions diff --git a/src/tests/ngraph_helpers/lpt_ngraph_functions/include/lpt_ngraph_functions/batch_to_space_function.hpp b/src/tests/ngraph_helpers/lpt_ngraph_functions/include/lpt_ngraph_functions/batch_to_space_function.hpp new file mode 100644 index 00000000000000..21763d762617fe --- /dev/null +++ b/src/tests/ngraph_helpers/lpt_ngraph_functions/include/lpt_ngraph_functions/batch_to_space_function.hpp @@ -0,0 +1,36 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include +#include "lpt_ngraph_functions/common/dequantization_operations.hpp" +#include "lpt_ngraph_functions/common/fake_quantize_on_data.hpp" + +namespace ngraph { +namespace builder { +namespace subgraph { + +class BatchToSpaceFunction { +public: + static std::shared_ptr get(const ngraph::PartialShape& input_shape, + const ngraph::element::Type input_type, + const FakeQuantizeOnData& fq_on_data, + const std::vector& block_shape, + const std::vector& crops_begin, + const std::vector& crops_end); + + static std::shared_ptr get(const ngraph::PartialShape& input_shape, + const ngraph::element::Type input_type, + const ngraph::builder::subgraph::DequantizationOperations& dequantization_before, + const std::vector& block_shape, + const std::vector& crops_begin, + const std::vector& crops_end, + const ngraph::builder::subgraph::DequantizationOperations& dequantization_after = {}); +}; + +} // namespace subgraph +} // namespace builder +} // namespace ngraph diff --git a/src/tests/ngraph_helpers/lpt_ngraph_functions/include/lpt_ngraph_functions/space_to_batch_function.hpp b/src/tests/ngraph_helpers/lpt_ngraph_functions/include/lpt_ngraph_functions/space_to_batch_function.hpp new file mode 100644 index 00000000000000..1a85d0edd9e228 --- /dev/null +++ b/src/tests/ngraph_helpers/lpt_ngraph_functions/include/lpt_ngraph_functions/space_to_batch_function.hpp @@ -0,0 +1,36 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include +#include "lpt_ngraph_functions/common/dequantization_operations.hpp" +#include "lpt_ngraph_functions/common/fake_quantize_on_data.hpp" + +namespace ngraph { +namespace builder { +namespace subgraph { + +class SpaceToBatchFunction { +public: + static std::shared_ptr get(const ngraph::PartialShape& input_shape, + const ngraph::element::Type input_type, + const FakeQuantizeOnData& fq_on_data, + const std::vector& block_shape, + const std::vector& pads_begin, + const std::vector& pads_end); + + static std::shared_ptr get(const ngraph::PartialShape& input_shape, + const ngraph::element::Type input_type, + const ngraph::builder::subgraph::DequantizationOperations& dequantization_before, + const std::vector& block_shape, + const std::vector& pads_begin, + const std::vector& pads_end, + const ngraph::builder::subgraph::DequantizationOperations& dequantization_after); +}; + +} // namespace subgraph +} // namespace builder +} // namespace ngraph diff --git a/src/tests/ngraph_helpers/lpt_ngraph_functions/src/batch_to_space_function.cpp b/src/tests/ngraph_helpers/lpt_ngraph_functions/src/batch_to_space_function.cpp new file mode 100644 index 00000000000000..42b71279f4a283 --- /dev/null +++ b/src/tests/ngraph_helpers/lpt_ngraph_functions/src/batch_to_space_function.cpp @@ -0,0 +1,65 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "lpt_ngraph_functions/batch_to_space_function.hpp" + +#include +#include "lpt_ngraph_functions/common/builders.hpp" + +namespace ngraph { +namespace builder { +namespace subgraph { + +std::shared_ptr BatchToSpaceFunction::get(const ngraph::PartialShape& input_shape, + const ngraph::element::Type input_type, + const FakeQuantizeOnData& fq_on_data, + const std::vector& block_shape, + const std::vector& crops_begin, + const std::vector& crops_end) { + const auto input = std::make_shared(input_type, input_shape); + + std::shared_ptr parent = fq_on_data.empty() ? + std::dynamic_pointer_cast(input) : + makeFakeQuantize(input, input_type, fq_on_data); + + parent = std::make_shared( + parent, + std::make_shared(ngraph::element::i64, ngraph::Shape{ block_shape.size() }, block_shape), + std::make_shared(ngraph::element::i64, ngraph::Shape{ crops_begin.size() }, crops_begin), + std::make_shared(ngraph::element::i64, ngraph::Shape{ crops_end.size()}, crops_end)); + + ngraph::ResultVector results{std::make_shared(parent)}; + return std::make_shared(results, ngraph::ParameterVector{ input }, "BatchToSpaceFunction"); +} + +std::shared_ptr BatchToSpaceFunction::get(const ngraph::PartialShape& input_shape, + const ngraph::element::Type input_type, + const ngraph::builder::subgraph::DequantizationOperations& dequantization_before, + const std::vector& block_shape, + const std::vector& crops_begin, + const std::vector& crops_end, + const ngraph::builder::subgraph::DequantizationOperations& dequantization_after) { + const auto input = std::make_shared(input_type, input_shape); + + std::shared_ptr parent = dequantization_before.empty() ? + std::dynamic_pointer_cast(input) : + makeDequantization(input, dequantization_before); + + parent = std::make_shared( + parent, + std::make_shared(ngraph::element::i64, ngraph::Shape{ block_shape.size() }, block_shape), + std::make_shared(ngraph::element::i64, ngraph::Shape{ crops_begin.size() }, crops_begin), + std::make_shared(ngraph::element::i64, ngraph::Shape{ crops_end.size()}, crops_end)); + + if (!dequantization_after.empty()) { + parent = makeDequantization(parent, dequantization_after); + } + + ngraph::ResultVector results{std::make_shared(parent)}; + return std::make_shared(results, ngraph::ParameterVector{ input }, "BatchToSpaceFunction"); +} + +} // namespace subgraph +} // namespace builder +} // namespace ngraph diff --git a/src/tests/ngraph_helpers/lpt_ngraph_functions/src/space_to_batch_function.cpp b/src/tests/ngraph_helpers/lpt_ngraph_functions/src/space_to_batch_function.cpp new file mode 100644 index 00000000000000..8757ee7cf572ba --- /dev/null +++ b/src/tests/ngraph_helpers/lpt_ngraph_functions/src/space_to_batch_function.cpp @@ -0,0 +1,65 @@ +// Copyright (C) 2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "lpt_ngraph_functions/space_to_batch_function.hpp" + +#include +#include "lpt_ngraph_functions/common/builders.hpp" + +namespace ngraph { +namespace builder { +namespace subgraph { + +std::shared_ptr SpaceToBatchFunction::get(const ngraph::PartialShape& input_shape, + const ngraph::element::Type input_type, + const FakeQuantizeOnData& fq_on_data, + const std::vector& block_shape, + const std::vector& pads_begin, + const std::vector& pads_end) { + const auto input = std::make_shared(input_type, input_shape); + + std::shared_ptr parent = fq_on_data.empty() ? + std::dynamic_pointer_cast(input) : + makeFakeQuantize(input, input_type, fq_on_data); + + parent = std::make_shared( + parent, + std::make_shared(ngraph::element::i64, ngraph::Shape{ block_shape.size() }, block_shape), + std::make_shared(ngraph::element::i64, ngraph::Shape{ pads_begin.size() }, pads_begin), + std::make_shared(ngraph::element::i64, ngraph::Shape{ pads_end.size() }, pads_end)); + + ngraph::ResultVector results{std::make_shared(parent)}; + return std::make_shared(results, ngraph::ParameterVector{ input }, "SpaceToBatchFunction"); +} + +std::shared_ptr SpaceToBatchFunction::get(const ngraph::PartialShape& input_shape, + const ngraph::element::Type input_type, + const ngraph::builder::subgraph::DequantizationOperations& dequantization_before, + const std::vector& block_shape, + const std::vector& pads_begin, + const std::vector& pads_end, + const ngraph::builder::subgraph::DequantizationOperations& dequantization_after) { + const auto input = std::make_shared(input_type, input_shape); + + std::shared_ptr parent = dequantization_before.empty() ? + std::dynamic_pointer_cast(input) : + makeDequantization(input, dequantization_before); + + parent = std::make_shared( + parent, + std::make_shared(ngraph::element::i64, ngraph::Shape{ block_shape.size() }, block_shape), + std::make_shared(ngraph::element::i64, ngraph::Shape{ pads_begin.size() }, pads_begin), + std::make_shared(ngraph::element::i64, ngraph::Shape{ pads_end.size() }, pads_end)); + + if (!dequantization_after.empty()) { + parent = makeDequantization(parent, dequantization_after); + } + + ngraph::ResultVector results{std::make_shared(parent)}; + return std::make_shared(results, ngraph::ParameterVector{ input }, "SpaceToBatchFunction"); +} + +} // namespace subgraph +} // namespace builder +} // namespace ngraph