1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/lite/c/builtin_op_data.h"
17 #include "tensorflow/lite/c/common.h"
18 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
19 #include "tensorflow/lite/kernels/kernel_util.h"
20 #include "tensorflow/lite/kernels/op_macros.h"
21 #include "tensorflow/lite/micro/kernels/kernel_util.h"
22 #include "tensorflow/lite/micro/memory_helpers.h"
23 #include "tensorflow/lite/micro/micro_utils.h"
24
25 namespace tflite {
26 namespace ops {
27 namespace micro {
28 namespace reshape {
29
30 constexpr int kInputTensor = 0;
31 constexpr int kOutputTensor = 0;
32
ReshapeOutput(TfLiteContext * context,TfLiteNode * node)33 TfLiteStatus ReshapeOutput(TfLiteContext* context, TfLiteNode* node) {
34 const TfLiteTensor* input = GetInput(context, node, kInputTensor);
35 TF_LITE_ENSURE(context, input != nullptr);
36 TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
37 TF_LITE_ENSURE(context, output != nullptr);
38 // Tensorflow's Reshape allows one of the shape components to have the
39 // special -1 value, meaning it will be calculated automatically based on the
40 // input. Here we calculate what that dimension should be so that the number
41 // of output elements in the same as the number of input elements.
42 int num_input_elements = NumElements(input);
43 TfLiteIntArray* output_shape = output->dims;
44
45 if (NumInputs(node) == 1 && // Legacy scalar supported with params.
46 output_shape->size == 1 && output_shape->data[0] == 0) {
47 // Legacy tflite models use a shape parameter of [0] to indicate scalars,
48 // so adjust accordingly. TODO(b/111614235): Allow zero-sized buffers during
49 // toco conversion.
50 output_shape->size = 0;
51 }
52
53 int num_output_elements = 1;
54 int stretch_dim = -1;
55 for (int i = 0; i < output_shape->size; ++i) {
56 int value = output_shape->data[i];
57 if (value == -1) {
58 TF_LITE_ENSURE_EQ(context, stretch_dim, -1);
59 stretch_dim = i;
60 } else {
61 num_output_elements *= value;
62 }
63 }
64 if (stretch_dim != -1) {
65 output_shape->data[stretch_dim] = num_input_elements / num_output_elements;
66 num_output_elements *= output_shape->data[stretch_dim];
67 }
68
69 TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type);
70 TF_LITE_ENSURE_EQ(context, num_input_elements, num_output_elements);
71 return kTfLiteOk;
72 }
73
Prepare(TfLiteContext * context,TfLiteNode * node)74 TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
75 TF_LITE_ENSURE(context, NumInputs(node) == 1 || NumInputs(node) == 2);
76 TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
77 TF_LITE_ENSURE_EQ(context, ReshapeOutput(context, node), kTfLiteOk);
78 return kTfLiteOk;
79 }
80
Eval(TfLiteContext * context,TfLiteNode * node)81 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
82 const TfLiteEvalTensor* input =
83 tflite::micro::GetEvalInput(context, node, kInputTensor);
84 TfLiteEvalTensor* output =
85 tflite::micro::GetEvalOutput(context, node, kOutputTensor);
86
87 // TODO(b/162522304): storing input bytes in OpData increases some models
88 // significantly, possibly due to alignment issues.
89 size_t input_bytes;
90 TF_LITE_ENSURE_STATUS(TfLiteTypeSizeOf(input->type, &input_bytes));
91 input_bytes *= ElementCount(*input->dims);
92
93 // Do nothing for in-place reshape.
94 if (input->data.raw != output->data.raw) {
95 // Otherwise perform reshape with copy.
96 for (size_t i = 0; i < input_bytes; ++i) {
97 output->data.raw[i] = input->data.raw[i];
98 }
99 }
100 return kTfLiteOk;
101 }
102
103 } // namespace reshape
104
Register_RESHAPE()105 TfLiteRegistration Register_RESHAPE() {
106 return {/*init=*/nullptr,
107 /*free=*/nullptr,
108 /*prepare=*/reshape::Prepare,
109 /*invoke=*/reshape::Eval,
110 /*profiling_string=*/nullptr,
111 /*builtin_code=*/0,
112 /*custom_name=*/nullptr,
113 /*version=*/0};
114 }
115
116 } // namespace micro
117 } // namespace ops
118 } // namespace tflite
119