Change std::max, std::min, std::round to tflite::TfLiteMax, tflite::TfLiteMin, tflite::TfLiteRound
Signed-off-by: Kamil Rakoczy <krakoczy@antmicro.com> Signed-off-by: Karol Gugala <kgugala@antmicro.com>
This commit is contained in:
parent
e651638ee4
commit
17c5a9bbd6
@ -362,7 +362,11 @@ cc_test(
|
|||||||
cc_library(
|
cc_library(
|
||||||
name = "cppmath",
|
name = "cppmath",
|
||||||
srcs = [],
|
srcs = [],
|
||||||
hdrs = ["cppmath.h"],
|
hdrs = [
|
||||||
|
"cppmath.h",
|
||||||
|
"max.h",
|
||||||
|
"min.h",
|
||||||
|
],
|
||||||
build_for_embedded = True,
|
build_for_embedded = True,
|
||||||
copts = tflite_copts(),
|
copts = tflite_copts(),
|
||||||
)
|
)
|
||||||
|
@ -20,7 +20,7 @@ limitations under the License.
|
|||||||
namespace tflite {
|
namespace tflite {
|
||||||
|
|
||||||
#if defined(TF_LITE_USE_GLOBAL_CMATH_FUNCTIONS) || \
|
#if defined(TF_LITE_USE_GLOBAL_CMATH_FUNCTIONS) || \
|
||||||
(defined(__ANDROID__) && !defined(__NDK_MAJOR__)) || defined(ARDUINO)
|
(defined(__ANDROID__) && !defined(__NDK_MAJOR__)) || defined(ARDUINO) || defined(__ZEPHYR__)
|
||||||
#define TF_LITE_GLOBAL_STD_PREFIX
|
#define TF_LITE_GLOBAL_STD_PREFIX
|
||||||
#else
|
#else
|
||||||
#define TF_LITE_GLOBAL_STD_PREFIX std
|
#define TF_LITE_GLOBAL_STD_PREFIX std
|
||||||
|
35
tensorflow/lite/kernels/internal/max.h
Normal file
35
tensorflow/lite/kernels/internal/max.h
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
==============================================================================*/
|
||||||
|
#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_MAX_H_
|
||||||
|
#define TENSORFLOW_LITE_KERNELS_INTERNAL_MAX_H_
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
namespace tflite {
|
||||||
|
|
||||||
|
#if defined(TF_LITE_USE_GLOBAL_MAX) || defined(__ZEPHYR__)
|
||||||
|
inline float TfLiteMax(const float& x, const float& y) {
|
||||||
|
return std::max(x, y);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
template <class T>
|
||||||
|
inline T TfLiteMax(const T& x, const T& y) {
|
||||||
|
return std::fmax(x, y);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} // namespace tflite
|
||||||
|
|
||||||
|
#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_MAX_H_
|
35
tensorflow/lite/kernels/internal/min.h
Normal file
35
tensorflow/lite/kernels/internal/min.h
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
==============================================================================*/
|
||||||
|
#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_MIN_H_
|
||||||
|
#define TENSORFLOW_LITE_KERNELS_INTERNAL_MIN_H_
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
namespace tflite {
|
||||||
|
|
||||||
|
#if defined(TF_LITE_USE_GLOBAL_MIN) || defined(__ZEPHYR__)
|
||||||
|
inline float TfLiteMin(const float& x, const float& y) {
|
||||||
|
return std::min(x, y);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
template <class T>
|
||||||
|
inline T TfLiteMin(const T& x, const T& y) {
|
||||||
|
return std::fmin(x, y);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} // namespace tflite
|
||||||
|
|
||||||
|
#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_MIN_H_
|
@ -20,6 +20,8 @@ limitations under the License.
|
|||||||
#include "tensorflow/lite/kernels/internal/cppmath.h"
|
#include "tensorflow/lite/kernels/internal/cppmath.h"
|
||||||
#include "tensorflow/lite/kernels/internal/quantization_util.h"
|
#include "tensorflow/lite/kernels/internal/quantization_util.h"
|
||||||
#include "tensorflow/lite/kernels/internal/types.h"
|
#include "tensorflow/lite/kernels/internal/types.h"
|
||||||
|
#include "tensorflow/lite/kernels/internal/min.h"
|
||||||
|
#include "tensorflow/lite/kernels/internal/max.h"
|
||||||
|
|
||||||
namespace tflite {
|
namespace tflite {
|
||||||
|
|
||||||
@ -382,10 +384,10 @@ inline bool QuantizedMeanOrSum(const T* input_data, int32 input_zero_point,
|
|||||||
float float_mean = static_cast<float>(temp_sum[idx]) /
|
float float_mean = static_cast<float>(temp_sum[idx]) /
|
||||||
static_cast<float>(num_elements_in_axis);
|
static_cast<float>(num_elements_in_axis);
|
||||||
float result =
|
float result =
|
||||||
std::min(TfLiteRound(float_mean * scale + bias) + output_zero_point,
|
TfLiteMin(TfLiteRound(float_mean * scale + bias) + output_zero_point,
|
||||||
static_cast<float>(std::numeric_limits<T>::max()));
|
static_cast<float>(std::numeric_limits<T>::max()));
|
||||||
result =
|
result =
|
||||||
std::max(result, static_cast<float>(std::numeric_limits<T>::min()));
|
TfLiteMax(result, static_cast<float>(std::numeric_limits<T>::min()));
|
||||||
output_data[idx] = static_cast<T>(result);
|
output_data[idx] = static_cast<T>(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ limitations under the License.
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
#include "tensorflow/lite/kernels/internal/types.h"
|
#include "tensorflow/lite/kernels/internal/types.h"
|
||||||
|
#include "tensorflow/lite/kernels/internal/cppmath.h"
|
||||||
|
|
||||||
namespace tflite {
|
namespace tflite {
|
||||||
|
|
||||||
@ -34,7 +35,7 @@ inline int32 GetNearestNeighbor(const int input_value, const int32 input_size,
|
|||||||
const float offset = half_pixel_centers ? 0.5f : 0.0f;
|
const float offset = half_pixel_centers ? 0.5f : 0.0f;
|
||||||
int32 output_value = std::min(
|
int32 output_value = std::min(
|
||||||
align_corners
|
align_corners
|
||||||
? static_cast<int32>(std::round((input_value + offset) * scale))
|
? static_cast<int32>(TfLiteRound((input_value + offset) * scale))
|
||||||
: static_cast<int32>(std::floor((input_value + offset) * scale)),
|
: static_cast<int32>(std::floor((input_value + offset) * scale)),
|
||||||
input_size - 1);
|
input_size - 1);
|
||||||
if (half_pixel_centers) {
|
if (half_pixel_centers) {
|
||||||
|
@ -21,6 +21,8 @@ limitations under the License.
|
|||||||
|
|
||||||
#include "tensorflow/lite/c/builtin_op_data.h"
|
#include "tensorflow/lite/c/builtin_op_data.h"
|
||||||
#include "tensorflow/lite/kernels/internal/cppmath.h"
|
#include "tensorflow/lite/kernels/internal/cppmath.h"
|
||||||
|
#include "tensorflow/lite/kernels/internal/max.h"
|
||||||
|
#include "tensorflow/lite/kernels/internal/min.h"
|
||||||
|
|
||||||
namespace tflite {
|
namespace tflite {
|
||||||
namespace ops {
|
namespace ops {
|
||||||
@ -32,11 +34,11 @@ inline float ActivationValFloat(TfLiteFusedActivation act, float a) {
|
|||||||
case kTfLiteActNone:
|
case kTfLiteActNone:
|
||||||
return a;
|
return a;
|
||||||
case kTfLiteActRelu:
|
case kTfLiteActRelu:
|
||||||
return std::max(0.0f, a);
|
return TfLiteMax(0.0f, a);
|
||||||
case kTfLiteActRelu1:
|
case kTfLiteActRelu1:
|
||||||
return std::max(-1.0f, std::min(a, 1.0f));
|
return TfLiteMax(-1.0f, TfLiteMin(a, 1.0f));
|
||||||
case kTfLiteActRelu6:
|
case kTfLiteActRelu6:
|
||||||
return std::max(0.0f, std::min(a, 6.0f));
|
return TfLiteMax(0.0f, TfLiteMin(a, 6.0f));
|
||||||
case kTfLiteActTanh:
|
case kTfLiteActTanh:
|
||||||
return std::tanh(a);
|
return std::tanh(a);
|
||||||
case kTfLiteActSignBit:
|
case kTfLiteActSignBit:
|
||||||
|
Loading…
Reference in New Issue
Block a user