Add indirection layer for selecting cmath functions from global namespace.

There can be functions beyond round() that aren't present within the std::
namespace depending on libstdc++ version. This CL adds an indirection layer
using a new TF_MICRO_USE_GLOBAL_CMATH_FUNCTIONS flag to select these functions
and converts TfLiteRound() to use this layer.

Other similar indirections will be added in future CLs.

PiperOrigin-RevId: 303425632
Change-Id: Idfe14924329827f71742ab6aedfd6ead9bcb0dbb
This commit is contained in:
A. Unique TensorFlower 2020-03-27 16:03:16 -07:00 committed by TensorFlower Gardener
parent 0c6b402cac
commit a4c2fcf05b
27 changed files with 59 additions and 55 deletions

View File

@ -379,8 +379,8 @@ cc_library(
copts = tflite_copts() + micro_copts(),
deps = [
"//tensorflow/lite/c:common",
"//tensorflow/lite/kernels/internal:cppmath",
"//tensorflow/lite/kernels/internal:quantization_util",
"//tensorflow/lite/kernels/internal:round",
"@flatbuffers",
],
)

View File

@ -243,7 +243,7 @@ cc_library(
":strided_slice_logic",
":types",
":reference_base",
":round",
":cppmath",
":tensor",
":tensor_utils",
":transpose_utils",
@ -294,7 +294,7 @@ cc_library(
":types",
":legacy_types",
":legacy_reference_base",
":round",
":cppmath",
"//third_party/eigen3",
"@gemmlowp",
"//tensorflow/lite/c:common",
@ -346,9 +346,10 @@ cc_test(
)
cc_library(
name = "round",
name = "cppmath",
srcs = [],
hdrs = ["round.h"],
hdrs = ["cppmath.h"],
build_for_embedded = True,
copts = tflite_copts(),
)
@ -359,7 +360,7 @@ cc_library(
copts = tflite_copts() + micro_copts(),
deps = [
":compatibility",
":round",
":cppmath",
":types",
"//tensorflow/lite/kernels:op_macros",
],
@ -467,7 +468,7 @@ cc_library(
":common",
":compatibility",
":quantization_util",
":round",
":cppmath",
":strided_slice_logic",
":tensor",
":tensor_utils",
@ -532,7 +533,7 @@ cc_library(
":common",
":compatibility",
":quantization_util",
":round",
":cppmath",
":strided_slice_logic",
":legacy_types",
":tensor",
@ -600,7 +601,7 @@ cc_library(
deps = [
":common",
":compatibility",
":round",
":cppmath",
"//tensorflow/lite:minimal_logging",
"//tensorflow/lite/c:common",
"//tensorflow/lite/kernels:cpu_backend_context",
@ -621,9 +622,9 @@ cc_library(
deps = [
":common",
":compatibility",
":cppmath",
":cpu_check",
":portable_tensor_utils",
":round",
"//tensorflow/lite/c:common",
"//tensorflow/lite/experimental/ruy",
"//tensorflow/lite/experimental/ruy:detect_arm",

View File

@ -1,4 +1,4 @@
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
/* 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.
@ -12,29 +12,28 @@ 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_ROUND_H_
#define TENSORFLOW_LITE_KERNELS_INTERNAL_ROUND_H_
#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_CPPMATH_H_
#define TENSORFLOW_LITE_KERNELS_INTERNAL_CPPMATH_H_
#include <cmath>
namespace tflite {
// TODO(aselle): See if we can do this only on jdk. Also mikecase, check
// if you need this for java host build.
#if defined(TF_LITE_USE_GLOBAL_ROUND) || \
#if defined(TF_LITE_USE_GLOBAL_CMATH_FUNCTIONS) || \
(defined(__ANDROID__) && !defined(__NDK_MAJOR__)) || defined(ARDUINO)
template <class T>
inline float TfLiteRound(const float x) {
return ::round(x);
}
inline double TfLiteRound(const double x) { return ::round(x); }
#define TF_LITE_GLOBAL_STD_PREFIX
#else
template <class T>
inline T TfLiteRound(const T x) {
return std::round(x);
}
#define TF_LITE_GLOBAL_STD_PREFIX std
#endif
#define DECLARE_STD_GLOBAL_SWITCH1(tf_name, std_name) \
template <class T> \
inline T tf_name(const T x) { \
return TF_LITE_GLOBAL_STD_PREFIX::std_name(x); \
}
DECLARE_STD_GLOBAL_SWITCH1(TfLiteRound, round);
} // namespace tflite
#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_ROUND_H_
#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_CPPMATH_H_

View File

@ -19,7 +19,6 @@ limitations under the License.
#include "tensorflow/lite/kernels/cpu_backend_gemm.h"
#include "tensorflow/lite/kernels/cpu_backend_gemm_params.h"
#include "tensorflow/lite/kernels/internal/common.h"
#include "tensorflow/lite/kernels/internal/round.h"
#include "tensorflow/lite/kernels/internal/types.h"
namespace tflite {

View File

@ -29,12 +29,12 @@ limitations under the License.
#include "fixedpoint/fixedpoint.h"
#include "tensorflow/lite/experimental/ruy/profiler/instrumentation.h"
#include "tensorflow/lite/kernels/internal/cppmath.h"
#include "tensorflow/lite/kernels/internal/optimized/cpu_check.h"
#include "tensorflow/lite/kernels/internal/optimized/im2col_utils.h"
#include "tensorflow/lite/kernels/internal/quantization_util.h"
#include "tensorflow/lite/kernels/internal/reference/integer_ops/pooling.h"
#include "tensorflow/lite/kernels/internal/reference/reference_ops.h"
#include "tensorflow/lite/kernels/internal/round.h"
#include "tensorflow/lite/kernels/internal/strided_slice_logic.h"
#include "tensorflow/lite/kernels/internal/tensor_utils.h"
#include "tensorflow/lite/kernels/internal/types.h"

View File

@ -30,9 +30,9 @@ limitations under the License.
#include "tensorflow/lite/kernels/cpu_backend_gemm_params.h"
#include "tensorflow/lite/kernels/internal/common.h"
#include "tensorflow/lite/kernels/internal/compatibility.h"
#include "tensorflow/lite/kernels/internal/cppmath.h"
#include "tensorflow/lite/kernels/internal/optimized/cpu_check.h"
#include "tensorflow/lite/kernels/internal/optimized/neon_tensor_utils_impl.h"
#include "tensorflow/lite/kernels/internal/round.h"
#ifdef USE_NEON

View File

@ -44,11 +44,11 @@ limitations under the License.
#include "tensorflow/lite/kernels/cpu_backend_gemm.h"
#include "tensorflow/lite/kernels/cpu_backend_gemm_params.h"
#include "tensorflow/lite/kernels/cpu_backend_threadpool.h"
#include "tensorflow/lite/kernels/internal/cppmath.h"
#include "tensorflow/lite/kernels/internal/optimized/cpu_check.h"
#include "tensorflow/lite/kernels/internal/optimized/im2col_utils.h"
#include "tensorflow/lite/kernels/internal/quantization_util.h"
#include "tensorflow/lite/kernels/internal/reference/reference_ops.h"
#include "tensorflow/lite/kernels/internal/round.h"
#include "tensorflow/lite/kernels/internal/strided_slice_logic.h"
#include "tensorflow/lite/kernels/internal/tensor.h"
#include "tensorflow/lite/kernels/internal/tensor_utils.h"

View File

@ -17,8 +17,8 @@ limitations under the License.
#include "tensorflow/lite/c/common.h"
#include "tensorflow/lite/kernels/internal/common.h"
#include "tensorflow/lite/kernels/internal/cppmath.h"
#include "tensorflow/lite/kernels/internal/quantization_util.h"
#include "tensorflow/lite/kernels/internal/round.h"
#include "tensorflow/lite/kernels/internal/types.h"
namespace tflite {

View File

@ -13,13 +13,14 @@ See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/lite/kernels/internal/quantization_util.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include "tensorflow/lite/kernels/internal/compatibility.h"
#include "tensorflow/lite/kernels/internal/quantization_util.h"
#include "tensorflow/lite/kernels/internal/round.h"
#include "tensorflow/lite/kernels/internal/cppmath.h"
namespace tflite {

View File

@ -20,7 +20,7 @@ limitations under the License.
#include <limits>
#include "tensorflow/lite/kernels/internal/compatibility.h"
#include "tensorflow/lite/kernels/internal/round.h"
#include "tensorflow/lite/kernels/internal/cppmath.h"
#include "tensorflow/lite/kernels/internal/types.h"
namespace tflite {

View File

@ -17,7 +17,6 @@ limitations under the License.
#include "tensorflow/lite/c/common.h"
#include "tensorflow/lite/kernels/internal/common.h"
#include "tensorflow/lite/kernels/internal/round.h"
#include "tensorflow/lite/kernels/internal/types.h"
namespace tflite {

View File

@ -18,7 +18,7 @@ limitations under the License.
#include "tensorflow/lite/kernels/internal/common.h"
#include "tensorflow/lite/kernels/internal/compatibility.h"
#include "tensorflow/lite/kernels/internal/round.h"
#include "tensorflow/lite/kernels/internal/cppmath.h"
#include "tensorflow/lite/kernels/internal/types.h"
namespace tflite {

View File

@ -16,8 +16,8 @@ limitations under the License.
#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FULLY_CONNECTED_H_
#include "tensorflow/lite/kernels/internal/common.h"
#include "tensorflow/lite/kernels/internal/cppmath.h"
#include "tensorflow/lite/kernels/internal/quantization_util.h"
#include "tensorflow/lite/kernels/internal/round.h"
#include "tensorflow/lite/kernels/internal/types.h"
namespace tflite {

View File

@ -19,8 +19,8 @@ limitations under the License.
#include "fixedpoint/fixedpoint.h"
#include "tensorflow/lite/kernels/internal/common.h"
#include "tensorflow/lite/kernels/internal/cppmath.h"
#include "tensorflow/lite/kernels/internal/quantization_util.h"
#include "tensorflow/lite/kernels/internal/round.h"
#include "tensorflow/lite/kernels/internal/types.h"
#include "tensorflow/lite/kernels/op_macros.h"

View File

@ -16,8 +16,8 @@ limitations under the License.
#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_POOLING_H_
#include "tensorflow/lite/kernels/internal/common.h"
#include "tensorflow/lite/kernels/internal/cppmath.h"
#include "tensorflow/lite/kernels/internal/quantization_util.h"
#include "tensorflow/lite/kernels/internal/round.h"
#include "tensorflow/lite/kernels/internal/types.h"
namespace tflite {

View File

@ -24,8 +24,8 @@ limitations under the License.
#include "tensorflow/lite/kernels/cpu_backend_context.h"
#include "tensorflow/lite/kernels/internal/common.h"
#include "tensorflow/lite/kernels/internal/compatibility.h"
#include "tensorflow/lite/kernels/internal/cppmath.h"
#include "tensorflow/lite/kernels/internal/reference/portable_tensor_utils_impl.h"
#include "tensorflow/lite/kernels/internal/round.h"
#if defined(_MSC_VER)
#define __restrict__ __restrict

View File

@ -16,7 +16,7 @@ limitations under the License.
#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_QUANTIZE_H_
#include "tensorflow/lite/kernels/internal/common.h"
#include "tensorflow/lite/kernels/internal/round.h"
#include "tensorflow/lite/kernels/internal/cppmath.h"
#include "tensorflow/lite/kernels/internal/types.h"
namespace tflite {

View File

@ -17,6 +17,7 @@ limitations under the License.
#include "tensorflow/lite/experimental/ruy/profiler/instrumentation.h"
#include "tensorflow/lite/kernels/internal/common.h"
#include "tensorflow/lite/kernels/internal/cppmath.h"
#include "tensorflow/lite/kernels/internal/quantization_util.h"
#include "tensorflow/lite/kernels/internal/types.h"
@ -371,7 +372,7 @@ inline bool QuantizedMeanOrSum(const T* input_data, int32 input_zero_point,
-input_zero_point * scale * num_elements_in_axis + 0.5f;
for (size_t idx = 0; idx < num_outputs; ++idx) {
const U value =
static_cast<U>(std::round(temp_sum[idx] * scale + bias)) +
static_cast<U>(TfLiteRound(temp_sum[idx] * scale + bias)) +
output_zero_point;
output_data[idx] = static_cast<T>(value);
}
@ -381,7 +382,7 @@ inline bool QuantizedMeanOrSum(const T* input_data, int32 input_zero_point,
float float_mean = static_cast<float>(temp_sum[idx]) /
static_cast<float>(num_elements_in_axis);
float result =
std::min(std::round(float_mean * scale + bias) + output_zero_point,
std::min(TfLiteRound(float_mean * scale + bias) + output_zero_point,
static_cast<float>(std::numeric_limits<T>::max()));
result =
std::max(result, static_cast<float>(std::numeric_limits<T>::min()));

View File

@ -57,7 +57,6 @@ limitations under the License.
#include "tensorflow/lite/kernels/internal/reference/softmax.h"
#include "tensorflow/lite/kernels/internal/reference/strided_slice.h"
#include "tensorflow/lite/kernels/internal/reference/sub.h"
#include "tensorflow/lite/kernels/internal/round.h"
#include "tensorflow/lite/kernels/internal/strided_slice_logic.h"
#include "tensorflow/lite/kernels/internal/tensor.h"
#include "tensorflow/lite/kernels/internal/types.h"

View File

@ -19,8 +19,8 @@ limitations under the License.
#include "fixedpoint/fixedpoint.h"
#include "tensorflow/lite/kernels/internal/common.h"
#include "tensorflow/lite/kernels/internal/cppmath.h"
#include "tensorflow/lite/kernels/internal/quantization_util.h"
#include "tensorflow/lite/kernels/internal/round.h"
#include "tensorflow/lite/kernels/internal/types.h"
#include "tensorflow/lite/kernels/op_macros.h"

View File

@ -18,8 +18,8 @@ limitations under the License.
#include <cmath>
#include <memory>
#include "tensorflow/lite/kernels/internal/cppmath.h"
#include "tensorflow/lite/kernels/internal/quantization_util.h"
#include "tensorflow/lite/kernels/internal/round.h"
namespace tflite {

View File

@ -506,7 +506,10 @@ tflite_micro_cc_test(
cc_library(
name = "activation_utils",
hdrs = ["activation_utils.h"],
deps = ["//tensorflow/lite/c:common"],
deps = [
"//tensorflow/lite/c:common",
"//tensorflow/lite/kernels/internal:cppmath",
],
)
tflite_micro_cc_test(

View File

@ -16,9 +16,11 @@ limitations under the License.
#ifndef TENSORFLOW_LITE_MICRO_KERNELS_ACTIVATION_UTILS_H_
#define TENSORFLOW_LITE_MICRO_KERNELS_ACTIVATION_UTILS_H_
#include <algorithm>
#include <cmath>
#include "tensorflow/lite/c/builtin_op_data.h"
#include "tensorflow/lite/kernels/internal/cppmath.h"
namespace tflite {
namespace ops {
@ -30,11 +32,11 @@ inline float ActivationValFloat(TfLiteFusedActivation act, float a) {
case kTfLiteActNone:
return a;
case kTfLiteActRelu:
return std::fmax(0.0f, a);
return std::max(0.0f, a);
case kTfLiteActRelu1:
return std::fmax(-1.0f, std::fmin(a, 1.0f));
return std::max(-1.0f, std::min(a, 1.0f));
case kTfLiteActRelu6:
return std::fmax(0.0f, std::fmin(a, 6.0f));
return std::max(0.0f, std::min(a, 6.0f));
case kTfLiteActTanh:
return std::tanh(a);
case kTfLiteActSignBit:

View File

@ -165,7 +165,7 @@ tensorflow/lite/kernels/internal/reference/sub.h \
tensorflow/lite/kernels/internal/reference/logistic.h \
tensorflow/lite/kernels/internal/reference/strided_slice.h \
tensorflow/lite/kernels/internal/reference/integer_ops/pooling.h \
tensorflow/lite/kernels/internal/round.h \
tensorflow/lite/kernels/internal/cppmath.h \
tensorflow/lite/kernels/internal/strided_slice_logic.h \
tensorflow/lite/kernels/internal/tensor.h \
tensorflow/lite/kernels/internal/tensor_ctypes.h \

View File

@ -14,7 +14,7 @@ ifeq ($(TARGET), riscv32_mcu)
-fno-builtin-printf \
-fno-exceptions \
-DTF_LITE_MCU_DEBUG_LOG \
-DTF_LITE_USE_GLOBAL_ROUND \
-DTF_LITE_USE_GLOBAL_CMATH_FUNCTIONS \
-fno-unwind-tables \
-fno-builtin \
-ffunction-sections \

View File

@ -108,8 +108,8 @@ cc_library(
"//tensorflow/lite:minimal_logging",
"//tensorflow/lite/c:common",
"//tensorflow/lite/core/api",
"//tensorflow/lite/kernels/internal:cppmath",
"//tensorflow/lite/kernels/internal:quantization_util",
"//tensorflow/lite/kernels/internal:round",
"//tensorflow/lite/kernels/internal:tensor_utils",
"//tensorflow/lite/kernels/internal:types",
"//tensorflow/lite/schema:schema_fbs",

View File

@ -23,8 +23,8 @@ limitations under the License.
#include "third_party/eigen3/Eigen/Core"
#include "tensorflow/lite/c/common.h"
#include "tensorflow/lite/core/api/error_reporter.h"
#include "tensorflow/lite/kernels/internal/cppmath.h"
#include "tensorflow/lite/kernels/internal/quantization_util.h"
#include "tensorflow/lite/kernels/internal/round.h"
#include "tensorflow/lite/kernels/internal/tensor_utils.h"
#include "tensorflow/lite/kernels/internal/types.h"
#include "tensorflow/lite/minimal_logging.h"