2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "NodeExecution.h"
18 #include "NodeDataImpl.h"
19 #include "NodeDomain.h"
20 #include "Validation.h"
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>
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;
41 Index reduced_index(const Index &index, const loco::TensorAxisSet &axes)
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);
52 Shape reduced_shape(const Shape &shape, const loco::TensorAxisSet &axes)
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);
68 template <typename T, loco::ReduceFunc F> struct ReduceFunction
70 static void apply(Buffer<T> &lhs, const Buffer<T> &rhs, const loco::TensorAxisSet &axes)
72 throw std::runtime_error("Not supported ReduceFunc type");
76 template <typename T> struct ReduceFunction<T, loco::ReduceFunc::Mean>
78 static void apply(Buffer<T> &lhs, const Buffer<T> &rhs, const loco::TensorAxisSet &axes)
80 for (IndexEnumerator e{rhs.shape()}; e.valid(); e.advance())
82 const auto &index = e.current();
83 const auto r_index = reduced_index(index, axes);
85 lhs.at(r_index) += rhs.at(index);
89 for (uint32_t i = 0; i < rhs.shape().rank(); ++i)
91 r_cnt *= rhs.shape().dim(i);
93 for (IndexEnumerator e{lhs.shape()}; e.valid(); e.advance())
95 const auto &index = e.current();
96 lhs.at(index) /= static_cast<T>(r_cnt);
101 template <typename T>
102 void apply(Buffer<T> &lhs, const Buffer<T> &rhs, const loco::TensorReduce &node)
106 case loco::ReduceFunc::Mean:
107 ReduceFunction<T, loco::ReduceFunc::Mean>::apply(lhs, rhs, *node.axes());
110 // TODO Support more ReduceFunc type
121 using namespace locomotiv;
123 void execute_node(loco::TensorReduce *node)
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");
131 std::unique_ptr<NodeData> reduce_data = nullptr;
132 Shape r_shape = reduced_shape(*input_shape, *node->axes());
133 switch (input_data->dtype())
135 case loco::DataType::FLOAT32:
137 auto input_bufptr = input_data->as_f32_bufptr();
138 auto reduce_buf = make_buffer<float, LexicalLayout>(r_shape);
140 apply(reduce_buf, *input_bufptr, *node);
142 reduce_data = make_data(reduce_buf);
146 throw std::runtime_error("NYI for this DataType");
149 assert(reduce_data != nullptr);
150 annot_data(node, std::move(reduce_data));
151 annot_domain(node, annot_domain(node->input()));
159 void NodeExecution::execute(loco::TensorReduce *node) { execute_node(node); }
161 } // namespace locomotiv