Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / Reshape.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "kernels/Reshape.h"
19
20 #include "kernels/Utils.h"
21
22 #include <cassert>
23 #include <cstring>
24
25 namespace luci_interpreter
26 {
27
28 namespace kernels
29 {
30
31 static Shape extractShapeFromTensor(const Tensor *tensor)
32 {
33   Shape shape(tensor->shape().num_elements());
34   if (tensor->element_type() == DataType::S32)
35   {
36     const auto *shape_data = tensor->data<int32_t>();
37     for (int i = 0; i < tensor->shape().num_elements(); ++i)
38     {
39       shape.dim(i) = shape_data[i];
40     }
41   }
42   else if (tensor->element_type() == DataType::S64)
43   {
44     const auto *shape_data = tensor->data<int64_t>();
45     for (int i = 0; i < tensor->shape().num_elements(); ++i)
46     {
47       shape.dim(i) = static_cast<int32_t>(shape_data[i]);
48     }
49   }
50   else
51   {
52     LUCI_INTERPRETER_CHECK(false);
53   }
54   return shape;
55 }
56
57 static void resolveUnknownDimension(const Shape &input_shape, Shape *output_shape)
58 {
59   const int32_t num_input_elements = input_shape.num_elements();
60   int32_t num_output_elements = 1;
61   int unknown_dim_index = -1;
62   for (int i = 0; i < output_shape->num_dims(); ++i)
63   {
64     const int32_t value = output_shape->dim(i);
65     if (value == -1)
66     {
67       assert(unknown_dim_index == -1);
68       unknown_dim_index = i;
69     }
70     else
71     {
72       num_output_elements *= value;
73     }
74   }
75   if (unknown_dim_index != -1)
76   {
77     output_shape->dim(unknown_dim_index) = num_input_elements / num_output_elements;
78     num_output_elements *= output_shape->dim(unknown_dim_index);
79   }
80   assert(num_output_elements == num_input_elements);
81 }
82
83 Reshape::Reshape(const Tensor *input, const Tensor *shape, Tensor *output)
84   : Kernel({input, shape}, {output})
85 {
86 }
87
88 void Reshape::configure()
89 {
90   Shape output_shape = extractShapeFromTensor(shape());
91   resolveUnknownDimension(input()->shape(), &output_shape);
92   output()->resize(output_shape);
93 }
94
95 void Reshape::execute() const
96 {
97   const auto *input_data = input()->data<void>();
98   auto *output_data = output()->data<void>();
99
100   const size_t element_size = getDataTypeSize(input()->element_type());
101   const int32_t num_elements = input()->shape().num_elements();
102   std::memcpy(output_data, input_data, num_elements * element_size);
103 }
104
105 } // namespace kernels
106 } // namespace luci_interpreter