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;
39 std::unique_ptr<locomotiv::NodeData> filter_encode(const loco::FilterEncode *node,
40 const Buffer<T> *input_buf)
42 auto encoder = node->encoder();
44 // Make TensorShape from input
45 loco::TensorShape input_shape;
46 input_shape.rank(input_buf->shape().rank());
47 assert(input_shape.rank() == 4);
48 for (uint32_t i = 0; i < input_shape.rank(); ++i)
50 input_shape.dim(i) = input_buf->shape().dim(i);
53 loco::FilterShape node_shape = encoder->shape(input_shape);
55 // Make NHWC buffer from FilterShape
57 make_buffer<T, LexicalLayout>(Shape{node_shape.count().value(), node_shape.height().value(),
58 node_shape.width().value(), node_shape.depth().value()});
60 // Copy buffer in an order arranged by encoder
61 for (IndexEnumerator e{node_buf.shape()}; e.valid(); e.advance())
63 loco::FilterIndex index;
64 index.nth() = e.current().at(0);
65 index.row() = e.current().at(1);
66 index.column() = e.current().at(2);
67 index.channel() = e.current().at(3);
69 node_buf.at(e.current()) = input_buf->at(encoder->value(index));
72 return locomotiv::make_data(node_buf);
80 using namespace locomotiv;
82 void execute_node(loco::FilterEncode *enc)
84 auto input_data = annot_data(enc->input());
86 validate(input_data, "Input of FilterEncode not ready");
87 validate(annot_domain(enc->input()) == loco::Domain::Tensor,
88 "Input of FilterEncode is not Tensor");
89 validate(input_data->shape()->rank() == 4, "Input shape mismatch");
91 std::unique_ptr<NodeData> enc_data = nullptr;
93 switch (input_data->dtype())
95 case loco::DataType::S32:
97 auto input_buf = input_data->as_s32_bufptr();
98 enc_data = filter_encode<int32_t>(enc, input_buf);
101 case loco::DataType::FLOAT32:
103 auto input_buf = input_data->as_f32_bufptr();
104 enc_data = filter_encode<float>(enc, input_buf);
108 throw std::runtime_error("NYI for this DataType");
111 assert(enc_data != nullptr);
112 annot_data(enc, std::move(enc_data));
113 annot_domain(enc, loco::Domain::Filter);
121 void NodeExecution::execute(loco::FilterEncode *enc) { execute_node(enc); }
123 } // namespace locomotiv