c591676aede6fd49d2d3253f9ac10c494ddd6308
[platform/core/ml/nnfw.git] / compiler / locomotiv / src / Node / MatrixDecode.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> matrix_decode(const loco::MatrixDecode *node,
41                                                    const Buffer<T> *input_buf)
42 {
43   auto decoder = node->decoder();
44
45   // Make MatrixShape from input. Note that matrix in locomotiv represented as HW
46   loco::MatrixShape input_shape;
47   assert(input_buf->shape().rank() == 2);
48   input_shape.height() = input_buf->shape().dim(0);
49   input_shape.width() = input_buf->shape().dim(1);
50
51   loco::TensorShape node_shape = decoder->shape(input_shape);
52
53   // Make tensor buffer from TensorShape
54   Buffer<T> node_buf =
55       make_buffer<T, LexicalLayout>(Shape{node_shape.dim(0).value(), node_shape.dim(1).value()});
56
57   // Copy buffer in an order arranged by decoder
58   for (IndexEnumerator e{node_buf.shape()}; e.valid(); e.advance())
59   {
60     loco::MatrixIndex matrix_index = decoder->value(e.current());
61     Index buf_index({matrix_index.row(), matrix_index.column()});
62
63     node_buf.at(e.current()) = input_buf->at(buf_index);
64   }
65
66   return locomotiv::make_data(node_buf);
67 }
68
69 } // namespace
70
71 namespace locomotiv
72 {
73
74 void NodeExecution::execute(loco::MatrixDecode *matrix_dec)
75 {
76   auto input_data = annot_data(matrix_dec->input());
77
78   validate(input_data, "Input not ready");
79   validate(annot_domain(matrix_dec->input()) == loco::Domain::Matrix,
80            "Input domain should be Matrix");
81   validate(input_data->shape()->rank() == 2, "Input data rank must be 2");
82
83   std::unique_ptr<NodeData> matrix_dec_data = nullptr;
84
85   switch (input_data->dtype())
86   {
87     case loco::DataType::S32:
88     {
89       auto input_buf = input_data->as_s32_bufptr();
90       matrix_dec_data = matrix_decode<int32_t>(matrix_dec, input_buf);
91       break;
92     }
93     case loco::DataType::FLOAT32:
94     {
95       auto input_buf = input_data->as_f32_bufptr();
96       matrix_dec_data = matrix_decode<float>(matrix_dec, input_buf);
97       break;
98     }
99     default:
100       throw std::runtime_error("NYI for this DataType");
101   }
102
103   assert(matrix_dec_data != nullptr);
104
105   annot_data(matrix_dec, std::move(matrix_dec_data));
106   annot_domain(matrix_dec, loco::Domain::Tensor);
107 }
108
109 } // namespace locomotiv