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> feature_decode(const loco::FeatureDecode *node,
41 const Buffer<T> *input_buf)
43 auto decoder = node->decoder();
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);
53 loco::TensorShape node_shape = decoder->shape(input_shape);
55 // Make tensor buffer from TensorShape
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()});
60 // Copy buffer in an order arranged by decoder
61 for (IndexEnumerator e{node_buf.shape()}; e.valid(); e.advance())
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()});
67 node_buf.at(e.current()) = input_buf->at(buf_index);
70 return locomotiv::make_data(node_buf);
78 using namespace locomotiv;
80 void execute_node(loco::FeatureDecode *dec)
82 auto input_data = annot_data(dec->input());
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");
89 std::unique_ptr<NodeData> dec_data = nullptr;
91 switch (input_data->dtype())
93 case loco::DataType::S32:
95 auto input_buf = input_data->as_s32_bufptr();
96 dec_data = feature_decode<int32_t>(dec, input_buf);
99 case loco::DataType::FLOAT32:
101 auto input_buf = input_data->as_f32_bufptr();
102 dec_data = feature_decode<float>(dec, input_buf);
106 throw std::runtime_error("NYI for this DataType");
109 assert(dec_data != nullptr);
110 annot_data(dec, std::move(dec_data));
111 annot_domain(dec, loco::Domain::Tensor);
119 void NodeExecution::execute(loco::FeatureDecode *dec) { execute_node(dec); }
121 } // namespace locomotiv