ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Reshape.cpp
Go to the documentation of this file.
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
25namespace luci_interpreter
26{
27
28namespace kernels
29{
30
31static 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 {
53 }
54 return shape;
55}
56
57static 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
81 LUCI_INTERPRETER_CHECK(num_output_elements == num_input_elements);
82}
83
84Reshape::Reshape(const Tensor *input, const Tensor *shape, Tensor *output)
85 : Kernel({input, shape}, {output})
86{
87}
88
90{
91 Shape output_shape = extractShapeFromTensor(shape());
92 resolveUnknownDimension(input()->shape(), &output_shape);
94}
95
96void Reshape::execute() const
97{
98 const auto *input_data = input()->data<void>();
99 auto *output_data = output()->data<void>();
100
101 const size_t element_size = getDataTypeSize(input()->element_type());
102 const int32_t num_elements = input()->shape().num_elements();
103 std::memcpy(output_data, input_data, num_elements * element_size);
104}
105
106} // namespace kernels
107} // namespace luci_interpreter
int32_t num_elements() const
Definition Tensor.h:53
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
const Shape & shape() const
Definition Tensor.h:107
const T * data() const
Definition Tensor.h:127
Reshape(const Tensor *input, const Tensor *shape, Tensor *output)
Definition Reshape.cpp:84
const Tensor * shape() const
Definition Reshape.h:33
void execute() const override
Definition Reshape.cpp:96
const Tensor * input() const
Definition Reshape.h:32
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
const luci_interpreter::RuntimeShape output_shape
size_t getDataTypeSize(DataType data_type)
Definition DataType.h:33
Definition Shape.h:28