[IE][NGRAPH][BUILD] Enable UNITY build for more targets (#2592)
[platform/upstream/dldt.git] / ngraph / core / src / op / reduce_prod.cpp
1 //*****************************************************************************
2 // Copyright 2017-2020 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //*****************************************************************************
16
17 #include "ngraph/op/reduce_prod.hpp"
18 #include "itt.hpp"
19 #include "ngraph/graph_util.hpp"
20 #include "ngraph/runtime/host_tensor.hpp"
21 #include "ngraph/runtime/reference/product.hpp"
22 #include "ngraph/shape_util.hpp"
23
24 using namespace std;
25 using namespace ngraph;
26
27 constexpr NodeTypeInfo op::v1::ReduceProd::type_info;
28
29 op::v1::ReduceProd::ReduceProd(const Output<Node>& arg,
30                                const Output<Node>& reduction_axes,
31                                bool keep_dims)
32     : ArithmeticReductionKeepDims(arg, reduction_axes, keep_dims)
33 {
34     constructor_validate_and_infer_types();
35 }
36
37 shared_ptr<Node> op::v1::ReduceProd::get_default_value() const
38 {
39     return ngraph::make_constant_from_string("1", get_element_type(), get_shape());
40 }
41
42 shared_ptr<Node> op::v1::ReduceProd::clone_with_new_inputs(const OutputVector& new_args) const
43 {
44     check_new_args_count(this, new_args);
45     return make_shared<ReduceProd>(new_args.at(0), new_args.at(1), get_keep_dims());
46 }
47
48 namespace reduce_prod
49 {
50     template <element::Type_t ET>
51     bool evaluate(const HostTensorPtr& arg,
52                   const HostTensorPtr& out,
53                   const AxisSet& axes,
54                   bool keep_dims)
55     {
56         out->set_shape(reduce(arg->get_shape(), axes, keep_dims));
57         runtime::reference::product(
58             arg->get_data_ptr<ET>(), out->get_data_ptr<ET>(), arg->get_shape(), axes, keep_dims);
59         return true;
60     }
61
62     bool evaluate_product(const HostTensorPtr& arg,
63                           const HostTensorPtr& out,
64                           const AxisSet& axes,
65                           bool keep_dims)
66     {
67         bool rc = true;
68         switch (arg->get_element_type())
69         {
70             TYPE_CASE(i32)(arg, out, axes, keep_dims);
71             break;
72             TYPE_CASE(i64)(arg, out, axes, keep_dims);
73             break;
74             TYPE_CASE(u32)(arg, out, axes, keep_dims);
75             break;
76             TYPE_CASE(u64)(arg, out, axes, keep_dims);
77             break;
78             TYPE_CASE(f16)(arg, out, axes, keep_dims);
79             break;
80             TYPE_CASE(f32)(arg, out, axes, keep_dims);
81             break;
82         default: rc = false; break;
83         }
84         return rc;
85     }
86 }
87
88 bool op::v1::ReduceProd::evaluate(const HostTensorVector& outputs,
89                                   const HostTensorVector& inputs) const
90 {
91     OV_ITT_SCOPED_TASK(itt::domains::nGraphOp, "op::v1::ReduceProd::evaluate");
92     return reduce_prod::evaluate_product(
93         inputs[0], outputs[0], get_reduction_axes(), get_keep_dims());
94 }