Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / core / src / op / min.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/min.hpp"
18 #include "itt.hpp"
19 #include "ngraph/graph_util.hpp"
20 #include "ngraph/runtime/host_tensor.hpp"
21 #include "ngraph/runtime/reference/min.hpp"
22 #include "ngraph/shape_util.hpp"
23
24 NGRAPH_SUPPRESS_DEPRECATED_START
25
26 using namespace std;
27 using namespace ngraph;
28
29 constexpr NodeTypeInfo op::v0::Min::type_info;
30
31 op::v0::Min::Min(const Output<Node>& arg, const AxisSet& reduction_axes)
32     : ArithmeticReduction(arg, reduction_axes)
33 {
34     constructor_validate_and_infer_types();
35 }
36
37 op::v0::Min::Min(const Output<Node>& arg, const Output<Node>& reduction_axes)
38     : ArithmeticReduction(arg, reduction_axes)
39 {
40     constructor_validate_and_infer_types();
41 }
42
43 shared_ptr<Node> op::v0::Min::clone_with_new_inputs(const OutputVector& new_args) const
44 {
45     check_new_args_count(this, new_args);
46     return make_shared<op::v0::Min>(new_args.at(0), get_reduction_axes());
47 }
48
49 shared_ptr<Node> op::v0::Min::get_default_value() const
50 {
51     switch (get_element_type())
52     {
53     case element::Type_t::boolean:
54         return make_constant_from_string("1", get_element_type(), get_shape());
55     case element::Type_t::bf16:
56     case element::Type_t::f16:
57     case element::Type_t::f32:
58     case element::Type_t::f64:
59         return make_constant_from_string("INFINITY", get_element_type(), get_shape());
60     case element::Type_t::i8:
61         return make_constant_from_string(
62             to_string(numeric_limits<int8_t>::max()), get_element_type(), get_shape());
63     case element::Type_t::i16:
64         return make_constant_from_string(
65             to_string(numeric_limits<int16_t>::max()), get_element_type(), get_shape());
66     case element::Type_t::i32:
67         return make_constant_from_string(
68             to_string(numeric_limits<int32_t>::max()), get_element_type(), get_shape());
69     case element::Type_t::i64:
70         return make_constant_from_string(
71             to_string(numeric_limits<int64_t>::max()), get_element_type(), get_shape());
72     case element::Type_t::u8:
73         return make_constant_from_string(
74             to_string(numeric_limits<uint8_t>::max()), get_element_type(), get_shape());
75     case element::Type_t::u16:
76         return make_constant_from_string(
77             to_string(numeric_limits<uint16_t>::max()), get_element_type(), get_shape());
78     case element::Type_t::u32:
79         return make_constant_from_string(
80             to_string(numeric_limits<uint32_t>::max()), get_element_type(), get_shape());
81     case element::Type_t::u64:
82         return make_constant_from_string(
83             to_string(numeric_limits<uint64_t>::max()), get_element_type(), get_shape());
84     case element::Type_t::u1:
85     case element::Type_t::undefined:
86     case element::Type_t::dynamic:
87     default: throw runtime_error("Min default value not defined for type");
88     }
89 }
90
91 namespace
92 {
93     template <element::Type_t ET>
94     bool evaluate(const HostTensorPtr& arg, const HostTensorPtr& out, const AxisSet& axes)
95     {
96         out->set_shape(reduce(arg->get_shape(), axes, false));
97         runtime::reference::min(
98             arg->get_data_ptr<ET>(), out->get_data_ptr<ET>(), arg->get_shape(), axes);
99         return true;
100     }
101
102     bool evaluate_min(const HostTensorPtr& arg, const HostTensorPtr& out, const AxisSet& axes)
103     {
104         bool rc = true;
105         switch (arg->get_element_type())
106         {
107             TYPE_CASE(i32)(arg, out, axes);
108             break;
109             TYPE_CASE(i64)(arg, out, axes);
110             break;
111             TYPE_CASE(u32)(arg, out, axes);
112             break;
113             TYPE_CASE(u64)(arg, out, axes);
114             break;
115             TYPE_CASE(f16)(arg, out, axes);
116             break;
117             TYPE_CASE(f32)(arg, out, axes);
118             break;
119         default: rc = false; break;
120         }
121         return rc;
122     }
123 }
124
125 bool op::v0::Min::evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const
126 {
127     OV_ITT_SCOPED_TASK(itt::domains::nGraphOp, "op::v0::Min::evaluate");
128     return evaluate_min(inputs[0], outputs[0], get_reduction_axes());
129 }
130
131 constexpr NodeTypeInfo op::v1::ReduceMin::type_info;
132
133 op::v1::ReduceMin::ReduceMin(const Output<Node>& arg,
134                              const Output<Node>& reduction_axes,
135                              bool keep_dims)
136     : ArithmeticReductionKeepDims(arg, reduction_axes, keep_dims)
137 {
138     constructor_validate_and_infer_types();
139 }
140
141 shared_ptr<Node> op::v1::ReduceMin::clone_with_new_inputs(const OutputVector& new_args) const
142 {
143     check_new_args_count(this, new_args);
144     return make_shared<op::v1::ReduceMin>(new_args.at(0), new_args.at(1), get_keep_dims());
145 }
146
147 bool op::v1::ReduceMin::evaluate(const HostTensorVector& outputs,
148                                  const HostTensorVector& inputs) const
149 {
150     OV_ITT_SCOPED_TASK(itt::domains::nGraphOp, "op::v1::ReduceMin::evaluate");
151     return evaluate_min(inputs[0], outputs[0], get_reduction_axes());
152 }