1 /* Copyright 2021 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/common.h"
17 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
18 #include "tensorflow/lite/kernels/kernel_util.h"
19 #include "tensorflow/lite/micro/kernels/kernel_util.h"
20 
21 namespace tflite {
22 namespace {
23 
24 constexpr int kInputTensor = 0;
25 constexpr int kOutputTensor = 0;
26 
Prepare(TfLiteContext * context,TfLiteNode * node)27 TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
28   TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
29   TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
30   const TfLiteTensor* input = GetInput(context, node, kInputTensor);
31   TF_LITE_ENSURE(context, input != nullptr);
32   TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
33   TF_LITE_ENSURE(context, output != nullptr);
34 
35   return kTfLiteOk;
36 }
37 
38 template <typename FromT, typename ToT>
copyCast(const FromT * in,ToT * out,int num_elements)39 void copyCast(const FromT* in, ToT* out, int num_elements) {
40   std::transform(in, in + num_elements, out,
41                  [](FromT a) { return static_cast<ToT>(a); });
42 }
43 
44 template <typename FromT>
copyToTensor(TfLiteContext * context,const FromT * in,TfLiteEvalTensor * out,int num_elements)45 TfLiteStatus copyToTensor(TfLiteContext* context, const FromT* in,
46                           TfLiteEvalTensor* out, int num_elements) {
47   switch (out->type) {
48     case kTfLiteInt8:
49       copyCast(in, out->data.int8, num_elements);
50       break;
51     case kTfLiteFloat32:
52       copyCast(in, tflite::micro::GetTensorData<float>(out), num_elements);
53       break;
54     default:
55       // Unsupported type.
56       TF_LITE_KERNEL_LOG(context, "Output type %s (%d) not supported.",
57                          TfLiteTypeGetName(out->type), out->type);
58   }
59   return kTfLiteOk;
60 }
61 
Eval(TfLiteContext * context,TfLiteNode * node)62 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
63   const TfLiteEvalTensor* input =
64       tflite::micro::GetEvalInput(context, node, kInputTensor);
65   TfLiteEvalTensor* output =
66       tflite::micro::GetEvalOutput(context, node, kOutputTensor);
67   int num_elements = MatchingFlatSize(tflite::micro::GetTensorShape(input),
68                                       tflite::micro::GetTensorShape(output));
69 
70   switch (input->type) {
71     case kTfLiteInt8:
72       return copyToTensor(context, input->data.int8, output, num_elements);
73     case kTfLiteFloat32:
74       return copyToTensor(context, tflite::micro::GetTensorData<float>(input),
75                           output, num_elements);
76     default:
77       // Unsupported type.
78       TF_LITE_KERNEL_LOG(context, "Input type %s (%d) not supported.",
79                          TfLiteTypeGetName(input->type), input->type);
80   }
81   return kTfLiteOk;
82 }
83 }  // namespace
84 
Register_CAST()85 TfLiteRegistration Register_CAST() {
86   return {/*init=*/nullptr,
87           /*free=*/nullptr,
88           /*prepare=*/Prepare,
89           /*invoke=*/Eval,
90           /*profiling_string=*/nullptr,
91           /*builtin_code=*/0,
92           /*custom_name=*/nullptr,
93           /*version=*/0};
94 }
95 
96 }  // namespace tflite
97