Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / compiler / locomotiv / src / Node / FilterEncode.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
19 #include "NodeDataImpl.h"
20 #include "NodeDomain.h"
21 #include "Validation.h"
22
23 #include <nncc/core/ADT/tensor/LexicalLayout.h>
24 #include <nncc/core/ADT/tensor/IndexEnumerator.h>
25
26 #include <stdexcept>
27 #include <cassert>
28
29 namespace
30 {
31
32 using nncc::core::ADT::tensor::Buffer;
33 using nncc::core::ADT::tensor::make_buffer;
34 using nncc::core::ADT::tensor::LexicalLayout;
35 using nncc::core::ADT::tensor::Shape;
36 using nncc::core::ADT::tensor::IndexEnumerator;
37
38 template <typename T>
39 std::unique_ptr<locomotiv::NodeData> filter_encode(const loco::FilterEncode *node,
40                                                    const Buffer<T> *input_buf)
41 {
42   auto encoder = node->encoder();
43
44   // Make TensorShape from input
45   loco::TensorShape input_shape;
46   input_shape.rank(input_buf->shape().rank());
47   assert(input_shape.rank() == 4);
48   for (uint32_t i = 0; i < input_shape.rank(); ++i)
49   {
50     input_shape.dim(i) = input_buf->shape().dim(i);
51   }
52
53   loco::FilterShape node_shape = encoder->shape(input_shape);
54
55   // Make NHWC buffer from FilterShape
56   Buffer<T> node_buf =
57       make_buffer<T, LexicalLayout>(Shape{node_shape.count().value(), node_shape.height().value(),
58                                           node_shape.width().value(), node_shape.depth().value()});
59
60   // Copy buffer in an order arranged by encoder
61   for (IndexEnumerator e{node_buf.shape()}; e.valid(); e.advance())
62   {
63     loco::FilterIndex index;
64     index.nth() = e.current().at(0);
65     index.row() = e.current().at(1);
66     index.column() = e.current().at(2);
67     index.channel() = e.current().at(3);
68
69     node_buf.at(e.current()) = input_buf->at(encoder->value(index));
70   }
71
72   return locomotiv::make_data(node_buf);
73 }
74
75 } // namespace
76
77 namespace
78 {
79
80 using namespace locomotiv;
81
82 void execute_node(loco::FilterEncode *enc)
83 {
84   auto input_data = annot_data(enc->input());
85
86   validate(input_data, "Input of FilterEncode not ready");
87   validate(annot_domain(enc->input()) == loco::Domain::Tensor,
88            "Input of FilterEncode is not Tensor");
89   validate(input_data->shape()->rank() == 4, "Input shape mismatch");
90
91   std::unique_ptr<NodeData> enc_data = nullptr;
92
93   switch (input_data->dtype())
94   {
95     case loco::DataType::S32:
96     {
97       auto input_buf = input_data->as_s32_bufptr();
98       enc_data = filter_encode<int32_t>(enc, input_buf);
99       break;
100     }
101     case loco::DataType::FLOAT32:
102     {
103       auto input_buf = input_data->as_f32_bufptr();
104       enc_data = filter_encode<float>(enc, input_buf);
105       break;
106     }
107     default:
108       throw std::runtime_error("NYI for this DataType");
109   }
110
111   assert(enc_data != nullptr);
112   annot_data(enc, std::move(enc_data));
113   annot_domain(enc, loco::Domain::Filter);
114 }
115
116 } // namespace
117
118 namespace locomotiv
119 {
120
121 void NodeExecution::execute(loco::FilterEncode *enc) { execute_node(enc); }
122
123 } // namespace locomotiv