|
| 1 | +// Copyright (C) 2024 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | + |
| 5 | +#include "low_precision/broadcast.hpp" |
| 6 | + |
| 7 | +#include <memory> |
| 8 | + |
| 9 | +#include "openvino/opsets/opset1.hpp" |
| 10 | +#include "openvino/opsets/opset3.hpp" |
| 11 | +#include "openvino/pass/pattern/op/or.hpp" |
| 12 | +#include "openvino/pass/pattern/op/wrap_type.hpp" |
| 13 | +#include "low_precision/network_helper.hpp" |
| 14 | + |
| 15 | +#include "itt.hpp" |
| 16 | + |
| 17 | +using namespace ov::pass::low_precision; |
| 18 | + |
| 19 | +BroadcastTransformation::BroadcastTransformation(const Params& params) : TransparentBaseTransformation(params) { |
| 20 | + MATCHER_SCOPE(BroadcastTransformation); |
| 21 | + auto broadcast1 = pattern::wrap_type<ov::opset1::Broadcast>({ |
| 22 | + pattern::wrap_type<ov::opset1::Multiply>(), |
| 23 | + ov::pass::pattern::any_input(), |
| 24 | + ov::pass::pattern::any_input() }); |
| 25 | + |
| 26 | + auto broadcast3 = pattern::wrap_type<ov::opset3::Broadcast>({ |
| 27 | + pattern::wrap_type<ov::opset1::Multiply>(), |
| 28 | + ov::pass::pattern::any_input(), |
| 29 | + ov::pass::pattern::any_input() }); |
| 30 | + |
| 31 | + const auto matcher = std::make_shared<ov::pass::pattern::op::Or>(ov::OutputVector{ broadcast1, broadcast3 }); |
| 32 | + |
| 33 | + ov::graph_rewrite_callback callback = [this](pattern::Matcher& m) { |
| 34 | + auto op = m.get_match_root(); |
| 35 | + if (transformation_callback(op)) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + return transform(*context, m); |
| 39 | + }; |
| 40 | + |
| 41 | + auto m = std::make_shared<ov::pass::pattern::Matcher>(matcher, matcher_name); |
| 42 | + this->register_matcher(m, callback); |
| 43 | +} |
| 44 | + |
| 45 | +bool BroadcastTransformation::canBeTransformed(const TransformationContext& context, std::shared_ptr<ov::Node> layer) const { |
| 46 | + if (!LayerTransformation::canBeTransformed(context, layer)) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + const auto& dequantization = NetworkHelper::getDequantization(layer, defaultPrecisions); |
| 51 | + if (dequantization.empty()) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + if (dequantization.isPerTensor()) { |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + const auto& inputShape = layer->get_input_partial_shape(0); |
| 60 | + if (inputShape.rank().is_dynamic() || inputShape[dequantization.channelDimIndex].is_dynamic()) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + const auto targetShapeConstant = ov::as_type_ptr<ov::opset1::Constant>(layer->get_input_node_shared_ptr(1)); |
| 65 | + const auto& targetShape = targetShapeConstant->cast_vector<int64_t>(); |
| 66 | + if (targetShape[dequantization.channelDimIndex] != inputShape[dequantization.channelDimIndex].get_length()) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + const auto axesMappingConstant = ov::as_type_ptr<ov::opset1::Constant>(layer->get_input_node_shared_ptr(2)); |
| 71 | + const auto& axesMapping = axesMappingConstant->cast_vector<int64_t>(); |
| 72 | + if (static_cast<size_t>(axesMapping[dequantization.channelDimIndex]) != dequantization.channelDimIndex) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + return true; |
| 77 | +} |
0 commit comments