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