From 17c5a9bbd67f34b25c3b5ffaba2f72a54ec0a105 Mon Sep 17 00:00:00 2001 From: Kamil Rakoczy Date: Wed, 1 Apr 2020 10:09:02 +0200 Subject: [PATCH] Change std::max, std::min, std::round to tflite::TfLiteMax, tflite::TfLiteMin, tflite::TfLiteRound Signed-off-by: Kamil Rakoczy Signed-off-by: Karol Gugala --- tensorflow/lite/kernels/internal/BUILD | 6 +++- tensorflow/lite/kernels/internal/cppmath.h | 2 +- tensorflow/lite/kernels/internal/max.h | 35 +++++++++++++++++++ tensorflow/lite/kernels/internal/min.h | 35 +++++++++++++++++++ .../lite/kernels/internal/reference/reduce.h | 6 ++-- .../reference/resize_nearest_neighbor.h | 3 +- .../lite/micro/kernels/activation_utils.h | 8 +++-- 7 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 tensorflow/lite/kernels/internal/max.h create mode 100644 tensorflow/lite/kernels/internal/min.h diff --git a/tensorflow/lite/kernels/internal/BUILD b/tensorflow/lite/kernels/internal/BUILD index d6a96efdbf7..6ef54d971df 100644 --- a/tensorflow/lite/kernels/internal/BUILD +++ b/tensorflow/lite/kernels/internal/BUILD @@ -362,7 +362,11 @@ cc_test( cc_library( name = "cppmath", srcs = [], - hdrs = ["cppmath.h"], + hdrs = [ + "cppmath.h", + "max.h", + "min.h", + ], build_for_embedded = True, copts = tflite_copts(), ) diff --git a/tensorflow/lite/kernels/internal/cppmath.h b/tensorflow/lite/kernels/internal/cppmath.h index 611a8d2588a..205ba189d71 100644 --- a/tensorflow/lite/kernels/internal/cppmath.h +++ b/tensorflow/lite/kernels/internal/cppmath.h @@ -20,7 +20,7 @@ limitations under the License. namespace tflite { #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 #else #define TF_LITE_GLOBAL_STD_PREFIX std diff --git a/tensorflow/lite/kernels/internal/max.h b/tensorflow/lite/kernels/internal/max.h new file mode 100644 index 00000000000..c18100272db --- /dev/null +++ b/tensorflow/lite/kernels/internal/max.h @@ -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 + +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 +inline T TfLiteMax(const T& x, const T& y) { + return std::fmax(x, y); +} +#endif + +} // namespace tflite + +#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_MAX_H_ diff --git a/tensorflow/lite/kernels/internal/min.h b/tensorflow/lite/kernels/internal/min.h new file mode 100644 index 00000000000..62035dccd89 --- /dev/null +++ b/tensorflow/lite/kernels/internal/min.h @@ -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 + +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 +inline T TfLiteMin(const T& x, const T& y) { + return std::fmin(x, y); +} +#endif + +} // namespace tflite + +#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_MIN_H_ diff --git a/tensorflow/lite/kernels/internal/reference/reduce.h b/tensorflow/lite/kernels/internal/reference/reduce.h index 17dfd8557ae..95de81668d3 100644 --- a/tensorflow/lite/kernels/internal/reference/reduce.h +++ b/tensorflow/lite/kernels/internal/reference/reduce.h @@ -20,6 +20,8 @@ limitations under the License. #include "tensorflow/lite/kernels/internal/cppmath.h" #include "tensorflow/lite/kernels/internal/quantization_util.h" #include "tensorflow/lite/kernels/internal/types.h" +#include "tensorflow/lite/kernels/internal/min.h" +#include "tensorflow/lite/kernels/internal/max.h" namespace tflite { @@ -382,10 +384,10 @@ inline bool QuantizedMeanOrSum(const T* input_data, int32 input_zero_point, float float_mean = static_cast(temp_sum[idx]) / static_cast(num_elements_in_axis); float result = - std::min(TfLiteRound(float_mean * scale + bias) + output_zero_point, + TfLiteMin(TfLiteRound(float_mean * scale + bias) + output_zero_point, static_cast(std::numeric_limits::max())); result = - std::max(result, static_cast(std::numeric_limits::min())); + TfLiteMax(result, static_cast(std::numeric_limits::min())); output_data[idx] = static_cast(result); } } diff --git a/tensorflow/lite/kernels/internal/reference/resize_nearest_neighbor.h b/tensorflow/lite/kernels/internal/reference/resize_nearest_neighbor.h index ed87863a7e5..1c71ed37c71 100644 --- a/tensorflow/lite/kernels/internal/reference/resize_nearest_neighbor.h +++ b/tensorflow/lite/kernels/internal/reference/resize_nearest_neighbor.h @@ -18,6 +18,7 @@ limitations under the License. #include #include "tensorflow/lite/kernels/internal/types.h" +#include "tensorflow/lite/kernels/internal/cppmath.h" 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; int32 output_value = std::min( align_corners - ? static_cast(std::round((input_value + offset) * scale)) + ? static_cast(TfLiteRound((input_value + offset) * scale)) : static_cast(std::floor((input_value + offset) * scale)), input_size - 1); if (half_pixel_centers) { diff --git a/tensorflow/lite/micro/kernels/activation_utils.h b/tensorflow/lite/micro/kernels/activation_utils.h index 7525bc93b0a..a71826211c0 100644 --- a/tensorflow/lite/micro/kernels/activation_utils.h +++ b/tensorflow/lite/micro/kernels/activation_utils.h @@ -21,6 +21,8 @@ limitations under the License. #include "tensorflow/lite/c/builtin_op_data.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 ops { @@ -32,11 +34,11 @@ inline float ActivationValFloat(TfLiteFusedActivation act, float a) { case kTfLiteActNone: return a; case kTfLiteActRelu: - return std::max(0.0f, a); + return TfLiteMax(0.0f, a); case kTfLiteActRelu1: - return std::max(-1.0f, std::min(a, 1.0f)); + return TfLiteMax(-1.0f, TfLiteMin(a, 1.0f)); case kTfLiteActRelu6: - return std::max(0.0f, std::min(a, 6.0f)); + return TfLiteMax(0.0f, TfLiteMin(a, 6.0f)); case kTfLiteActTanh: return std::tanh(a); case kTfLiteActSignBit: