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 
27 namespace {
28 constexpr int kInputTensor = 0;
29 constexpr int kOutputTensor = 0;
30 
ExtractShape(const TfLiteEvalTensor * input,int32_t * output_data)31 void ExtractShape(const TfLiteEvalTensor* input, int32_t* output_data) {
32   for (int i = 0; i < input->dims->size; ++i) {
33     output_data[i] = input->dims->data[i];
34   }
35 }
36 
Prepare(TfLiteContext * context,TfLiteNode * node)37 TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
38   TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
39   TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
40 
41   return kTfLiteOk;
42 }
43 
Eval(TfLiteContext * context,TfLiteNode * node)44 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
45   const TfLiteEvalTensor* input =
46       tflite::micro::GetEvalInput(context, node, kInputTensor);
47   TfLiteEvalTensor* output =
48       tflite::micro::GetEvalOutput(context, node, kOutputTensor);
49   if (output->type != kTfLiteInt32) {
50     TF_LITE_KERNEL_LOG(context, "Output type %s (%d) not supported.",
51                        TfLiteTypeGetName(output->type), output->type);
52     return kTfLiteError;
53   } else {
54     ExtractShape(input, tflite::micro::GetTensorData<int32_t>(output));
55   }
56 
57   return kTfLiteOk;
58 }
59 
60 }  // namespace
61 
Register_SHAPE()62 TfLiteRegistration Register_SHAPE() {
63   return {/*init=*/nullptr,
64           /*free=*/nullptr,
65           /*prepare=*/Prepare,
66           /*invoke=*/Eval,
67           /*profiling_string=*/nullptr,
68           /*builtin_code=*/0,
69           /*custom_name=*/nullptr,
70           /*version=*/0};
71 }
72 
73 }  // namespace tflite
74