Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / compiler / locomotiv / src / Node / DepthwiseFilterEncode.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 /**
39  * @brief  Encode input tensor into depthwise filter represented in "HWCM" layout
40  *
41  * (Please check locomotiv README for further information)
42  */
43 template <typename T>
44 std::unique_ptr<locomotiv::NodeData> dw_filter_encode(const loco::DepthwiseFilterEncode *node,
45                                                       const Buffer<T> *input_buf)
46 {
47   auto encoder = node->encoder();
48
49   // Make TensorShape from input
50   loco::TensorShape input_shape;
51   input_shape.rank(input_buf->shape().rank());
52   assert(input_shape.rank() == 4);
53   for (uint32_t i = 0; i < input_shape.rank(); ++i)
54   {
55     input_shape.dim(i) = input_buf->shape().dim(i);
56   }
57
58   loco::DepthwiseFilterShape node_shape = encoder->shape(input_shape);
59
60   // Make HWCM (i.e. height, width, depth, multiplier) buffer from DepthwiseFilterShape
61   Buffer<T> node_buf = make_buffer<T, LexicalLayout>(
62       Shape{node_shape.height().value(), node_shape.width().value(), node_shape.depth().value(),
63             node_shape.multiplier().value()});
64
65   // Copy buffer in an order arranged by encoder
66   for (IndexEnumerator e{node_buf.shape()}; e.valid(); e.advance())
67   {
68     loco::DepthwiseFilterIndex index;
69     index.row() = e.current().at(0);
70     index.column() = e.current().at(1);
71     index.channel() = e.current().at(2);
72     index.nth() = e.current().at(3);
73
74     node_buf.at(e.current()) = input_buf->at(encoder->value(index));
75   }
76
77   return locomotiv::make_data(node_buf);
78 }
79
80 } // namespace
81
82 namespace
83 {
84
85 using namespace locomotiv;
86
87 void execute_node(loco::DepthwiseFilterEncode *enc)
88 {
89   auto input_data = annot_data(enc->input());
90
91   validate(input_data, "Input of DepthwiseFilterEncode not ready");
92   validate(annot_domain(enc->input()) == loco::Domain::Tensor,
93            "Input of DepthwiseFilterEncode is not Tensor");
94   validate(input_data->shape()->rank() == 4, "Input shape mismatch");
95
96   std::unique_ptr<NodeData> enc_data = nullptr;
97
98   switch (input_data->dtype())
99   {
100     case loco::DataType::FLOAT32:
101     {
102       auto input_buf = input_data->as_f32_bufptr();
103       enc_data = dw_filter_encode<float>(enc, input_buf);
104       break;
105     }
106     default:
107       throw std::runtime_error("NYI for this DataType");
108   }
109
110   assert(enc_data != nullptr);
111   annot_data(enc, std::move(enc_data));
112   annot_domain(enc, loco::Domain::DepthwiseFilter);
113 }
114
115 } // namespace
116
117 namespace locomotiv
118 {
119
120 void NodeExecution::execute(loco::DepthwiseFilterEncode *enc) { execute_node(enc); }
121
122 } // namespace locomotiv