1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include <stdint.h>
17#include <string.h>
18
19#include "tensorflow/lite/c/common.h"
20#include "tensorflow/lite/kernels/internal/tensor.h"
21#include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
22#include "tensorflow/lite/kernels/kernel_util.h"
23
24namespace tflite {
25namespace ops {
26namespace builtin {
27namespace zeros_like {
28
29constexpr int kInputTensor = 0;
30constexpr int kOutputTensor = 0;
31
32TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
33 TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
34 TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
35 const TfLiteTensor* input;
36 TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInputTensor, &input));
37 TfLiteTensor* output;
38 TF_LITE_ENSURE_OK(context,
39 GetOutputSafe(context, node, kOutputTensor, &output));
40 output->type = input->type;
41
42 return context->ResizeTensor(context, output,
43 TfLiteIntArrayCopy(input->dims));
44}
45
46TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
47 const TfLiteTensor* input;
48 TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInputTensor, &input));
49 TfLiteTensor* output;
50 TF_LITE_ENSURE_OK(context,
51 GetOutputSafe(context, node, kOutputTensor, &output));
52 const int num_elements = NumElements(input);
53 switch (input->type) {
54 case kTfLiteInt64:
55 memset(GetTensorData<int64_t>(output), 0, num_elements * sizeof(int64_t));
56 break;
57 case kTfLiteInt32:
58 memset(GetTensorData<int32_t>(output), 0, num_elements * sizeof(int32_t));
59 break;
60 case kTfLiteFloat32:
61 memset(GetTensorData<float>(output), 0, num_elements * sizeof(float));
62 break;
63 default:
64 TF_LITE_KERNEL_LOG(context,
65 "ZerosLike only currently supports int64, int32, "
66 "and float32, got %d.",
67 input->type);
68 return kTfLiteError;
69 }
70 return kTfLiteOk;
71}
72
73} // namespace zeros_like
74
75TfLiteRegistration* Register_ZEROS_LIKE() {
76 static TfLiteRegistration r = {/*init=*/nullptr, /*free=*/nullptr,
77 zeros_like::Prepare, zeros_like::Eval};
78 return &r;
79}
80
81} // namespace builtin
82} // namespace ops
83} // namespace tflite
84