1 /* Copyright 2019 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 #ifndef TENSORFLOW_LITE_MICRO_KERNELS_ACTIVATION_UTILS_H_
17 #define TENSORFLOW_LITE_MICRO_KERNELS_ACTIVATION_UTILS_H_
18 
19 #include <algorithm>
20 #include <cmath>
21 
22 #include "tensorflow/lite/c/builtin_op_data.h"
23 #include "tensorflow/lite/kernels/internal/cppmath.h"
24 #include "tensorflow/lite/kernels/internal/max.h"
25 #include "tensorflow/lite/kernels/internal/min.h"
26 
27 namespace tflite {
28 namespace ops {
29 namespace micro {
30 
31 // Returns the floating point value for a fused activation:
ActivationValFloat(TfLiteFusedActivation act,float a)32 inline float ActivationValFloat(TfLiteFusedActivation act, float a) {
33   switch (act) {
34     case kTfLiteActNone:
35       return a;
36     case kTfLiteActRelu:
37       return TfLiteMax(0.0f, a);
38     case kTfLiteActReluN1To1:
39       return TfLiteMax(-1.0f, TfLiteMin(a, 1.0f));
40     case kTfLiteActRelu6:
41       return TfLiteMax(0.0f, TfLiteMin(a, 6.0f));
42     case kTfLiteActTanh:
43       return std::tanh(a);
44     case kTfLiteActSignBit:
45       return std::signbit(a);
46     case kTfLiteActSigmoid:
47       return 1.0f / (1.0f + std::exp(-a));
48   }
49   return 0.0f;  // To indicate an unsupported activation (i.e. when a new fused
50                 // activation is added to the enum and not handled here).
51 }
52 
53 }  // namespace micro
54 }  // namespace ops
55 }  // namespace tflite
56 
57 #endif  // TENSORFLOW_LITE_MICRO_KERNELS_ACTIVATION_UTILS_H_
58