Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / compiler / locomotiv / src / Node / TensorReduce.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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 "NodeExecution.h"
18 #include "NodeDataImpl.h"
19 #include "NodeDomain.h"
20 #include "Validation.h"
21
22 #include <nncc/core/ADT/tensor/Shape.h>
23 #include <nncc/core/ADT/tensor/Buffer.h>
24 #include <nncc/core/ADT/tensor/Index.h>
25 #include <nncc/core/ADT/tensor/IndexEnumerator.h>
26 #include <nncc/core/ADT/tensor/LexicalLayout.h>
27
28 using nncc::core::ADT::tensor::Index;
29 using nncc::core::ADT::tensor::IndexEnumerator;
30 using nncc::core::ADT::tensor::LexicalLayout;
31 using nncc::core::ADT::tensor::make_buffer;
32 using nncc::core::ADT::tensor::Shape;
33 using nncc::core::ADT::tensor::Buffer;
34
35 #include <cassert>
36 #include <stdexcept>
37
38 namespace
39 {
40
41 Index reduced_index(const Index &index, const loco::TensorAxisSet &axes)
42 {
43   Index r_index;
44
45   r_index.resize(index.rank());
46   for (uint32_t i = 0; i < index.rank(); ++i)
47     r_index.at(i) = (axes.defined(i)) ? 0 : index.at(i);
48
49   return r_index;
50 }
51
52 Shape reduced_shape(const Shape &shape, const loco::TensorAxisSet &axes)
53 {
54   Shape r_shape;
55
56   r_shape.resize(shape.rank());
57   for (uint32_t i = 0; i < shape.rank(); ++i)
58     r_shape.dim(i) = (axes.defined(i)) ? 1 : shape.dim(i);
59
60   return r_shape;
61 }
62
63 } // namespace
64
65 namespace
66 {
67
68 template <typename T, loco::ReduceFunc F> struct ReduceFunction
69 {
70   static void apply(Buffer<T> &lhs, const Buffer<T> &rhs, const loco::TensorAxisSet &axes)
71   {
72     throw std::runtime_error("Not supported ReduceFunc type");
73   }
74 };
75
76 template <typename T> struct ReduceFunction<T, loco::ReduceFunc::Mean>
77 {
78   static void apply(Buffer<T> &lhs, const Buffer<T> &rhs, const loco::TensorAxisSet &axes)
79   {
80     for (IndexEnumerator e{rhs.shape()}; e.valid(); e.advance())
81     {
82       const auto &index = e.current();
83       const auto r_index = reduced_index(index, axes);
84
85       lhs.at(r_index) += rhs.at(index);
86     }
87
88     uint32_t r_cnt = 1;
89     for (uint32_t i = 0; i < rhs.shape().rank(); ++i)
90       if (axes.defined(i))
91         r_cnt *= rhs.shape().dim(i);
92
93     for (IndexEnumerator e{lhs.shape()}; e.valid(); e.advance())
94     {
95       const auto &index = e.current();
96       lhs.at(index) /= static_cast<T>(r_cnt);
97     }
98   }
99 };
100
101 template <typename T>
102 void apply(Buffer<T> &lhs, const Buffer<T> &rhs, const loco::TensorReduce &node)
103 {
104   switch (node.func())
105   {
106     case loco::ReduceFunc::Mean:
107       ReduceFunction<T, loco::ReduceFunc::Mean>::apply(lhs, rhs, *node.axes());
108       break;
109
110     // TODO Support more ReduceFunc type
111     default:
112       break;
113   }
114 }
115
116 } // namespace
117
118 namespace
119 {
120
121 using namespace locomotiv;
122
123 void execute_node(loco::TensorReduce *node)
124 {
125   auto input_data = annot_data(node->input());
126   validate(input_data, "Input not ready");
127   auto input_shape = input_data->shape();
128   validate(annot_domain(node->input()) == loco::Domain::Tensor,
129            "Input domain of TensorReduce is not Tensor");
130
131   std::unique_ptr<NodeData> reduce_data = nullptr;
132   Shape r_shape = reduced_shape(*input_shape, *node->axes());
133   switch (input_data->dtype())
134   {
135     case loco::DataType::FLOAT32:
136     {
137       auto input_bufptr = input_data->as_f32_bufptr();
138       auto reduce_buf = make_buffer<float, LexicalLayout>(r_shape);
139
140       apply(reduce_buf, *input_bufptr, *node);
141
142       reduce_data = make_data(reduce_buf);
143       break;
144     }
145     default:
146       throw std::runtime_error("NYI for this DataType");
147   }
148
149   assert(reduce_data != nullptr);
150   annot_data(node, std::move(reduce_data));
151   annot_domain(node, annot_domain(node->input()));
152 }
153
154 } // namespace
155
156 namespace locomotiv
157 {
158
159 void NodeExecution::execute(loco::TensorReduce *node) { execute_node(node); }
160
161 } // namespace locomotiv