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 * @brief Encode input tensor into depthwise filter represented in "HWCM" layout
41 * (Please check locomotiv README for further information)
44 std::unique_ptr<locomotiv::NodeData> dw_filter_encode(const loco::DepthwiseFilterEncode *node,
45 const Buffer<T> *input_buf)
47 auto encoder = node->encoder();
49 // Make TensorShape from input
50 loco::TensorShape input_shape;
51 input_shape.rank(input_buf->shape().rank());
52 assert(input_shape.rank() == 4);
53 for (uint32_t i = 0; i < input_shape.rank(); ++i)
55 input_shape.dim(i) = input_buf->shape().dim(i);
58 loco::DepthwiseFilterShape node_shape = encoder->shape(input_shape);
60 // Make HWCM (i.e. height, width, depth, multiplier) buffer from DepthwiseFilterShape
61 Buffer<T> node_buf = make_buffer<T, LexicalLayout>(
62 Shape{node_shape.height().value(), node_shape.width().value(), node_shape.depth().value(),
63 node_shape.multiplier().value()});
65 // Copy buffer in an order arranged by encoder
66 for (IndexEnumerator e{node_buf.shape()}; e.valid(); e.advance())
68 loco::DepthwiseFilterIndex index;
69 index.row() = e.current().at(0);
70 index.column() = e.current().at(1);
71 index.channel() = e.current().at(2);
72 index.nth() = e.current().at(3);
74 node_buf.at(e.current()) = input_buf->at(encoder->value(index));
77 return locomotiv::make_data(node_buf);
85 using namespace locomotiv;
87 void execute_node(loco::DepthwiseFilterEncode *enc)
89 auto input_data = annot_data(enc->input());
91 validate(input_data, "Input of DepthwiseFilterEncode not ready");
92 validate(annot_domain(enc->input()) == loco::Domain::Tensor,
93 "Input of DepthwiseFilterEncode is not Tensor");
94 validate(input_data->shape()->rank() == 4, "Input shape mismatch");
96 std::unique_ptr<NodeData> enc_data = nullptr;
98 switch (input_data->dtype())
100 case loco::DataType::FLOAT32:
102 auto input_buf = input_data->as_f32_bufptr();
103 enc_data = dw_filter_encode<float>(enc, input_buf);
107 throw std::runtime_error("NYI for this DataType");
110 assert(enc_data != nullptr);
111 annot_data(enc, std::move(enc_data));
112 annot_domain(enc, loco::Domain::DepthwiseFilter);
120 void NodeExecution::execute(loco::DepthwiseFilterEncode *enc) { execute_node(enc); }
122 } // namespace locomotiv