forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LPT] SpaceToBatch & BatchToSpace implementation
- Loading branch information
Showing
30 changed files
with
1,238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...ow_precision_transformations/transformations/step3_main/shape/batch_to_space.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# BatchToSpaceTransformation transformation {#openvino_docs_OV_UG_lpt_BatchToSpaceTransformation} | ||
|
||
ngraph::pass::low_precision::BatchToSpaceTransformation class represents the `BatchToSpace` operation transformation. |
3 changes: 3 additions & 0 deletions
3
...ow_precision_transformations/transformations/step3_main/shape/space_to_batch.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# SpaceToBatchTransformation transformation {#openvino_docs_OV_UG_lpt_SpaceToBatchTransformation} | ||
|
||
ngraph::pass::low_precision::SpaceToBatchTransformation class represents the `SpaceToBatch` operation transformation. |
34 changes: 34 additions & 0 deletions
34
src/common/low_precision_transformations/include/low_precision/batch_to_space.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (C) 2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <ngraph/ngraph.hpp> | ||
#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<Node> op) const override; | ||
bool transform(TransformationContext& context, ngraph::pattern::Matcher &m) override; | ||
bool isPrecisionPreserved(std::shared_ptr<Node> layer) const noexcept override; | ||
}; | ||
|
||
} // namespace low_precision | ||
} // namespace pass | ||
} // namespace ngraph |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/common/low_precision_transformations/include/low_precision/space_to_batch.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (C) 2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <ngraph/ngraph.hpp> | ||
#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<Node> op) const override; | ||
bool transform(TransformationContext& context, ngraph::pattern::Matcher &m) override; | ||
bool isPrecisionPreserved(std::shared_ptr<Node> layer) const noexcept override; | ||
}; | ||
|
||
} // namespace low_precision | ||
} // namespace pass | ||
} // namespace ngraph |
66 changes: 66 additions & 0 deletions
66
src/common/low_precision_transformations/src/batch_to_space.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (C) 2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "low_precision/batch_to_space.hpp" | ||
|
||
#include <memory> | ||
#include <ngraph/ngraph.hpp> | ||
#include <ngraph/opsets/opset1.hpp> | ||
#include <ngraph/opsets/opset2.hpp> | ||
|
||
#include <ngraph/pattern/op/wrap_type.hpp> | ||
|
||
#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<opset2::BatchToSpace>(); | ||
|
||
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<ngraph::pattern::Matcher>(matcher, matcher_name); | ||
this->register_matcher(m, callback); | ||
} | ||
|
||
bool BatchToSpaceTransformation::canBeTransformed(const TransformationContext& context, std::shared_ptr<Node> 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<Node> pooling = NetworkHelper::separateInStandaloneBranch(m.get_match_root(), defaultPrecisions); | ||
moveDequantizationAfter(context, pooling, NetworkHelper::getDequantization(pooling, defaultPrecisions), false); | ||
return true; | ||
} | ||
|
||
bool BatchToSpaceTransformation::isPrecisionPreserved(std::shared_ptr<Node> layer) const noexcept { | ||
return true; | ||
} | ||
|
||
} // namespace low_precision | ||
} // namespace pass | ||
} // namespace ngraph |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/common/low_precision_transformations/src/space_to_batch.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (C) 2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "low_precision/space_to_batch.hpp" | ||
|
||
#include <memory> | ||
#include <ngraph/ngraph.hpp> | ||
#include <ngraph/opsets/opset1.hpp> | ||
#include <ngraph/opsets/opset2.hpp> | ||
|
||
#include <ngraph/pattern/op/wrap_type.hpp> | ||
|
||
#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<opset2::SpaceToBatch>(); | ||
|
||
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<ngraph::pattern::Matcher>(matcher, matcher_name); | ||
this->register_matcher(m, callback); | ||
} | ||
|
||
bool SpaceToBatchTransformation::canBeTransformed(const TransformationContext& context, std::shared_ptr<Node> 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<Node> pooling = NetworkHelper::separateInStandaloneBranch(m.get_match_root(), defaultPrecisions); | ||
moveDequantizationAfter(context, pooling, NetworkHelper::getDequantization(pooling, defaultPrecisions), false); | ||
return true; | ||
} | ||
|
||
bool SpaceToBatchTransformation::isPrecisionPreserved(std::shared_ptr<Node> layer) const noexcept { | ||
return true; | ||
} | ||
|
||
} // namespace low_precision | ||
} // namespace pass | ||
} // namespace ngraph |
Oops, something went wrong.