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 | #include "tensorflow/lite/c/common.h" |
16 | #include "tensorflow/lite/kernels/internal/reference/reference_ops.h" |
17 | #include "tensorflow/lite/kernels/internal/tensor.h" |
18 | #include "tensorflow/lite/kernels/internal/tensor_ctypes.h" |
19 | #include "tensorflow/lite/kernels/kernel_util.h" |
20 | |
21 | namespace tflite { |
22 | namespace ops { |
23 | namespace builtin { |
24 | namespace exp { |
25 | |
26 | // This file has reference implementation of Exp. |
27 | enum KernelType { |
28 | kReference, |
29 | }; |
30 | |
31 | struct ExpContext { |
32 | ExpContext(TfLiteContext* context, TfLiteNode* node) { |
33 | input = GetInput(context, node, 0); |
34 | output = GetOutput(context, node, 0); |
35 | } |
36 | const TfLiteTensor* input; |
37 | TfLiteTensor* output; |
38 | }; |
39 | |
40 | TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { |
41 | TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); |
42 | TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); |
43 | |
44 | ExpContext op_context(context, node); |
45 | TfLiteIntArray* output_dims = TfLiteIntArrayCopy(op_context.input->dims); |
46 | op_context.output->type = op_context.input->type; |
47 | return context->ResizeTensor(context, op_context.output, output_dims); |
48 | } |
49 | |
50 | template <KernelType kernel_type> |
51 | TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { |
52 | ExpContext op_context(context, node); |
53 | |
54 | #define TF_LITE_EXP(kernel_type, data_type) \ |
55 | kernel_type::Exp<data_type>(GetTensorData<data_type>(op_context.input), \ |
56 | NumElements(op_context.input), \ |
57 | GetTensorData<data_type>(op_context.output)) |
58 | |
59 | // TODO(kanlig): supports half, bfloat16, float64, complex64, and complex128. |
60 | if (kernel_type == kReference) { |
61 | switch (op_context.input->type) { |
62 | case kTfLiteFloat32: |
63 | TF_LITE_EXP(reference_ops, float); |
64 | break; |
65 | default: |
66 | TF_LITE_KERNEL_LOG(context, |
67 | "Type %d is currently not supported by Exp." , |
68 | op_context.input->type); |
69 | return kTfLiteError; |
70 | } |
71 | } |
72 | #undef TF_LITE_EXP |
73 | return kTfLiteOk; |
74 | } |
75 | |
76 | } // namespace exp |
77 | |
78 | TfLiteRegistration* Register_EXP_REF() { |
79 | static TfLiteRegistration r = {nullptr, nullptr, exp::Prepare, |
80 | exp::Eval<exp::kReference>}; |
81 | return &r; |
82 | } |
83 | |
84 | // TODO(kanlig): add optimized implementation of Exp. |
85 | TfLiteRegistration* Register_EXP() { return Register_EXP_REF(); } |
86 | |
87 | } // namespace builtin |
88 | } // namespace ops |
89 | } // namespace tflite |
90 | |