1 | /* Copyright 2018 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/kernels/internal/reference/neg.h" |
17 | |
18 | #include <stdint.h> |
19 | |
20 | #include "tensorflow/lite/c/common.h" |
21 | #include "tensorflow/lite/kernels/internal/optimized/optimized_ops.h" |
22 | #include "tensorflow/lite/kernels/internal/tensor.h" |
23 | #include "tensorflow/lite/kernels/internal/tensor_ctypes.h" |
24 | #include "tensorflow/lite/kernels/kernel_util.h" |
25 | |
26 | namespace tflite { |
27 | namespace ops { |
28 | namespace builtin { |
29 | namespace neg { |
30 | |
31 | constexpr int kInputTensor = 0; |
32 | constexpr int kOutputTensor = 0; |
33 | |
34 | TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { |
35 | TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); |
36 | TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); |
37 | const TfLiteTensor* input; |
38 | TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInputTensor, &input)); |
39 | TfLiteTensor* output; |
40 | TF_LITE_ENSURE_OK(context, |
41 | GetOutputSafe(context, node, kOutputTensor, &output)); |
42 | |
43 | output->type = input->type; |
44 | return context->ResizeTensor(context, output, |
45 | TfLiteIntArrayCopy(input->dims)); |
46 | } |
47 | |
48 | TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { |
49 | const TfLiteTensor* input; |
50 | TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInputTensor, &input)); |
51 | TfLiteTensor* output; |
52 | TF_LITE_ENSURE_OK(context, |
53 | GetOutputSafe(context, node, kOutputTensor, &output)); |
54 | switch (input->type) { |
55 | case kTfLiteInt64: |
56 | reference_ops::Negate( |
57 | GetTensorShape(input), GetTensorData<int64_t>(input), |
58 | GetTensorShape(output), GetTensorData<int64_t>(output)); |
59 | break; |
60 | case kTfLiteInt32: |
61 | reference_ops::Negate( |
62 | GetTensorShape(input), GetTensorData<int32_t>(input), |
63 | GetTensorShape(output), GetTensorData<int32_t>(output)); |
64 | break; |
65 | case kTfLiteFloat32: |
66 | reference_ops::Negate(GetTensorShape(input), GetTensorData<float>(input), |
67 | GetTensorShape(output), |
68 | GetTensorData<float>(output)); |
69 | break; |
70 | default: |
71 | TF_LITE_KERNEL_LOG( |
72 | context, |
73 | "Neg only currently supports int64, int32, and float32, got %d." , |
74 | input->type); |
75 | return kTfLiteError; |
76 | } |
77 | return kTfLiteOk; |
78 | } |
79 | |
80 | } // namespace neg |
81 | |
82 | TfLiteRegistration* Register_NEG() { |
83 | static TfLiteRegistration r = {/*init=*/nullptr, /*free=*/nullptr, |
84 | neg::Prepare, neg::Eval}; |
85 | return &r; |
86 | } |
87 | |
88 | } // namespace builtin |
89 | } // namespace ops |
90 | } // namespace tflite |
91 | |