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