1 // Copyright (C) 2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
8 #include <ngraph/ngraph.hpp>
9 #include "ngraph_ops/type_relaxed.hpp"
11 #include "low_precision/network_helper.hpp"
12 #include "low_precision/common/dequantization_op.hpp"
14 #include "ngraph_functions/low_precision_transformations/common/add.hpp"
15 #include "ngraph_functions/low_precision_transformations/common/fake_quantize_on_data.hpp"
16 #include "ngraph_functions/low_precision_transformations/common/dequantization_operations.hpp"
22 template <typename Operation, typename OperationDesc>
23 std::shared_ptr<Node> makeElementwise(const std::shared_ptr<ngraph::Node> data, const OperationDesc& description) {
24 std::vector<size_t> shape;
25 if (description.constantShapeIsDefined) {
26 shape = description.constantShape;
28 if (description.values.size() == 1ul) {
29 shape = std::vector<size_t>({});
31 shape = std::vector<size_t>(data->get_output_shape(0).size(), 1ul);
32 shape[shape.size() >= 2 ? 1ul : 0] = description.values.size();
36 const auto operationConst = std::make_shared<ngraph::opset1::Constant>(
37 description.outPrecision,
41 std::shared_ptr<Operation> operation;
42 if ((description.outPrecision == element::undefined) || (description.outPrecision == data->get_output_element_type(0))) {
43 operation = std::make_shared<Operation>(data, operationConst);
45 operation = std::make_shared<op::TypeRelaxed<Operation>>(
46 std::vector<element::Type>{element::f32, element::f32}, std::vector<element::Type>{},
47 ngraph::op::TemporaryReplaceOutputType(data, element::f32).get(),
48 ngraph::op::TemporaryReplaceOutputType(operationConst, element::f32).get());
49 ngraph::pass::low_precision::NetworkHelper::setOutDataPrecision(operation, description.outPrecision);
52 if (is_type<ngraph::opset1::Subtract>(operation) || is_type<ngraph::opset1::Add>(operation)) {
55 ngraph::pass::low_precision::fold<ngraph::opset1::Convert>(operationConst, data->get_output_element_type(0)));
61 std::shared_ptr<Node> makeDequantization(
62 const Output<Node>& data,
63 const DequantizationOperations& dequantizationOperations);
65 std::shared_ptr<ngraph::opset1::FakeQuantize> makeFakeQuantize(
66 const Output<Node>& input,
67 const ngraph::element::Type precision,
68 const FakeQuantizeOnData& fqOnData);
70 std::shared_ptr<ngraph::opset1::FakeQuantize> makeFakeQuantizeTypeRelaxed(
71 const std::shared_ptr<ngraph::Node>& input,
72 const ngraph::element::Type precision,
73 const FakeQuantizeOnData& fqOnData);
75 std::shared_ptr<ngraph::opset1::FakeQuantize> makeFakeQuantize(
76 const Output<Node>& input,
77 const ngraph::element::Type precision,
78 const FakeQuantizeOnDataWithConstant& fqOnData);
80 std::shared_ptr<ngraph::opset1::FakeQuantize> makeFakeQuantizeTypeRelaxed(
81 const std::shared_ptr<ngraph::Node>& input,
82 const ngraph::element::Type precision,
83 const FakeQuantizeOnDataWithConstant& fqOnData);
85 std::shared_ptr<Node> addDequantizationAttribute(const std::shared_ptr<Node>& op);
87 } // namespace subgraph
88 } // namespace builder