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"
19 #include "NodeDataImpl.h"
20 #include "NodeDomain.h"
21 #include "Validation.h"
23 #include <nncc/core/ADT/tensor/LexicalLayout.h>
24 #include <nncc/core/ADT/tensor/IndexEnumerator.h>
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;
40 std::unique_ptr<locomotiv::NodeData> matrix_decode(const loco::MatrixDecode *node,
41 const Buffer<T> *input_buf)
43 auto decoder = node->decoder();
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);
51 loco::TensorShape node_shape = decoder->shape(input_shape);
53 // Make tensor buffer from TensorShape
55 make_buffer<T, LexicalLayout>(Shape{node_shape.dim(0).value(), node_shape.dim(1).value()});
57 // Copy buffer in an order arranged by decoder
58 for (IndexEnumerator e{node_buf.shape()}; e.valid(); e.advance())
60 loco::MatrixIndex matrix_index = decoder->value(e.current());
61 Index buf_index({matrix_index.row(), matrix_index.column()});
63 node_buf.at(e.current()) = input_buf->at(buf_index);
66 return locomotiv::make_data(node_buf);
74 void NodeExecution::execute(loco::MatrixDecode *matrix_dec)
76 auto input_data = annot_data(matrix_dec->input());
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");
83 std::unique_ptr<NodeData> matrix_dec_data = nullptr;
85 switch (input_data->dtype())
87 case loco::DataType::S32:
89 auto input_buf = input_data->as_s32_bufptr();
90 matrix_dec_data = matrix_decode<int32_t>(matrix_dec, input_buf);
93 case loco::DataType::FLOAT32:
95 auto input_buf = input_data->as_f32_bufptr();
96 matrix_dec_data = matrix_decode<float>(matrix_dec, input_buf);
100 throw std::runtime_error("NYI for this DataType");
103 assert(matrix_dec_data != nullptr);
105 annot_data(matrix_dec, std::move(matrix_dec_data));
106 annot_domain(matrix_dec, loco::Domain::Tensor);
109 } // namespace locomotiv