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;
31 TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInputTensor, &input));
32 TfLiteTensor* output;
33 TF_LITE_ENSURE_OK(context,
34 GetOutputSafe(context, node, kOutputTensor, &output));
35 output->type = input->type;
36
37 return kTfLiteOk;
38 }
39
40 template <typename T>
resetZeros(T * out,const int num_elements)41 void resetZeros(T* out, const int num_elements) {
42 for (int i = 0; i < num_elements; ++i) {
43 out[i] = static_cast<T>(0);
44 }
45 }
46
Eval(TfLiteContext * context,TfLiteNode * node)47 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
48 const TfLiteEvalTensor* input =
49 tflite::micro::GetEvalInput(context, node, kInputTensor);
50 TfLiteEvalTensor* output =
51 tflite::micro::GetEvalOutput(context, node, kOutputTensor);
52 int flat_size = MatchingFlatSize(tflite::micro::GetTensorShape(input),
53 tflite::micro::GetTensorShape(output));
54 switch (input->type) {
55 case kTfLiteInt64:
56 resetZeros(tflite::micro::GetTensorData<int64_t>(output), flat_size);
57 break;
58 case kTfLiteInt32:
59 resetZeros(tflite::micro::GetTensorData<int32_t>(output), flat_size);
60 break;
61 case kTfLiteInt8:
62 resetZeros(tflite::micro::GetTensorData<int8_t>(output), flat_size);
63 break;
64 case kTfLiteFloat32:
65 resetZeros(tflite::micro::GetTensorData<float>(output), flat_size);
66 break;
67 default:
68 TF_LITE_KERNEL_LOG(context,
69 "ZerosLike only currently supports int64, int32, "
70 "and float32, got %d.",
71 input->type);
72 return kTfLiteError;
73 }
74 return kTfLiteOk;
75 }
76 } // namespace
77
Register_ZEROS_LIKE()78 TfLiteRegistration Register_ZEROS_LIKE() {
79 return {/*init=*/nullptr,
80 /*free=*/nullptr,
81 /*prepare=*/Prepare,
82 /*invoke=*/Eval,
83 /*profiling_string=*/nullptr,
84 /*builtin_code=*/0,
85 /*custom_name=*/nullptr,
86 /*version=*/0};
87 }
88
89 } // namespace tflite
90