Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / compiler / locomotiv / src / Node / TensorConstantPad.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/IndexEnumerator.h>
24 #include <nncc/core/ADT/tensor/LexicalLayout.h>
25
26 #include <cassert>
27
28 using nncc::core::ADT::tensor::Shape;
29 using nncc::core::ADT::tensor::Index;
30 using nncc::core::ADT::tensor::IndexEnumerator;
31 using nncc::core::ADT::tensor::LexicalLayout;
32 using nncc::core::ADT::tensor::make_buffer;
33
34 namespace
35 {
36
37 using namespace locomotiv;
38
39 void execute_node(loco::TensorConstantPad *pad)
40 {
41   validate(pad, "TensorConstantPad is nullptr");
42
43   auto input_data = annot_data(pad->input());
44   auto input_domain = annot_domain(pad->input());
45   validate(input_data, "Input not ready");
46   validate(input_domain == loco::Domain::Tensor, "Input domain of TensorConstantPad is not Tensor");
47
48   auto input_shape = input_data->shape();
49   const uint32_t input_rank = input_shape->rank();
50
51   auto padding = pad->padding();
52   validate(input_rank == padding->rank(), "input and padding should have same rank");
53
54   auto constant_node = pad->constant();
55   auto constant_data = annot_data(constant_node);
56   validate(constant_data->dtype() == input_data->dtype(), "constant and input have same data type");
57   validate(constant_data->shape()->rank() == 1 && constant_data->shape()->dim(0) == 1,
58            "constant should have one rank with one dimension at zero axis");
59
60   std::unique_ptr<NodeData> pad_data = nullptr;
61   Index base_index;
62   base_index.resize(input_rank);
63
64   // Tensor is padded by relocating its base.
65   // padded output index = input index + base index
66   for (uint32_t axis = 0; axis < padding->rank(); axis++)
67   {
68     base_index.at(axis) = padding->front(axis);
69   }
70
71   // calculate output shape
72   Shape output_shape;
73   output_shape.resize(input_rank);
74   for (uint32_t i = 0; i < input_rank; i++)
75   {
76     output_shape.dim(i) = input_shape->dim(i) + padding->front(i) + padding->back(i);
77   }
78
79   switch (input_data->dtype())
80   {
81     case loco::DataType::FLOAT32:
82     {
83       auto input_buf = input_data->as_f32_bufptr();
84       auto constant_data_buf = constant_data->as_f32_bufptr();
85       const auto constant_value = constant_data_buf->at(Index{0});
86
87       auto output_buf = make_buffer<float, LexicalLayout>(output_shape);
88
89       for (IndexEnumerator ie{*input_shape}, oe{output_shape}; oe.valid(); oe.advance())
90       {
91         auto input_index = ie.current();
92         auto output_index = oe.current();
93
94         if ((input_index + base_index) == output_index)
95         {
96           output_buf.at(output_index) = input_buf->at(input_index);
97           ie.advance();
98         }
99         else
100         {
101           output_buf.at(output_index) = constant_value;
102         }
103       }
104
105       pad_data = make_data(output_buf);
106       break;
107     }
108     default:
109       throw std::runtime_error("NYI for this DataType");
110   }
111
112   assert(pad_data != nullptr);
113   annot_data(pad, std::move(pad_data));
114   annot_domain(pad, annot_domain(pad->input()));
115 }
116
117 } // namespace
118
119 namespace locomotiv
120 {
121
122 void NodeExecution::execute(loco::TensorConstantPad *pad) { execute_node(pad); }
123
124 } // namespace locomotiv