2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "kernels/Reshape.h"
20 #include "kernels/Utils.h"
25 namespace luci_interpreter
31 static Shape extractShapeFromTensor(const Tensor *tensor)
33 Shape shape(tensor->shape().num_elements());
34 if (tensor->element_type() == DataType::S32)
36 const auto *shape_data = tensor->data<int32_t>();
37 for (int i = 0; i < tensor->shape().num_elements(); ++i)
39 shape.dim(i) = shape_data[i];
42 else if (tensor->element_type() == DataType::S64)
44 const auto *shape_data = tensor->data<int64_t>();
45 for (int i = 0; i < tensor->shape().num_elements(); ++i)
47 shape.dim(i) = static_cast<int32_t>(shape_data[i]);
52 LUCI_INTERPRETER_CHECK(false);
57 static void resolveUnknownDimension(const Shape &input_shape, Shape *output_shape)
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)
64 const int32_t value = output_shape->dim(i);
67 assert(unknown_dim_index == -1);
68 unknown_dim_index = i;
72 num_output_elements *= value;
75 if (unknown_dim_index != -1)
77 output_shape->dim(unknown_dim_index) = num_input_elements / num_output_elements;
78 num_output_elements *= output_shape->dim(unknown_dim_index);
80 assert(num_output_elements == num_input_elements);
83 Reshape::Reshape(const Tensor *input, const Tensor *shape, Tensor *output)
84 : Kernel({input, shape}, {output})
88 void Reshape::configure()
90 Shape output_shape = extractShapeFromTensor(shape());
91 resolveUnknownDimension(input()->shape(), &output_shape);
92 output()->resize(output_shape);
95 void Reshape::execute() const
97 const auto *input_data = input()->data<void>();
98 auto *output_data = output()->data<void>();
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);
105 } // namespace kernels
106 } // namespace luci_interpreter