diff --git a/include/caffe/util/math_functions.hpp b/include/caffe/util/math_functions.hpp index a78eb5bc978..12823f8da0c 100644 --- a/include/caffe/util/math_functions.hpp +++ b/include/caffe/util/math_functions.hpp @@ -108,7 +108,7 @@ Dtype caffe_cpu_asum(const int n, const Dtype* x); // the branchless, type-safe version from // http://stackoverflow.com/questions/1903954/is-there-a-standard-sign-function-signum-sgn-in-c-c template -inline char caffe_sign(Dtype val) { +inline int8_t caffe_sign(Dtype val) { return (Dtype(0) < val) - (val < Dtype(0)); } @@ -127,12 +127,6 @@ inline char caffe_sign(Dtype val) { } \ } -#define INSTANTIATE_CAFFE_CPU_UNARY_FUNC(name) \ - template <> \ - void caffe_cpu_##name(const int n, const float* x, float* y); \ - template <> \ - void caffe_cpu_##name(const int n, const double* x, double* y) - // output is 1 for the positives, 0 for zero, and -1 for the negatives DEFINE_CAFFE_CPU_UNARY_FUNC(sign, y[i] = caffe_sign(x[i])); @@ -140,7 +134,8 @@ DEFINE_CAFFE_CPU_UNARY_FUNC(sign, y[i] = caffe_sign(x[i])); // The name sngbit is meant to avoid conflicts with std::signbit in the macro. // The extra parens are needed because CUDA < 6.5 defines signbit as a macro, // and we don't want that to expand here when CUDA headers are also included. -DEFINE_CAFFE_CPU_UNARY_FUNC(sgnbit, y[i] = (std::signbit)(x[i])); +DEFINE_CAFFE_CPU_UNARY_FUNC(sgnbit, \ + y[i] = static_cast((std::signbit)(x[i]))); DEFINE_CAFFE_CPU_UNARY_FUNC(fabs, y[i] = std::fabs(x[i])); diff --git a/src/caffe/test/test_net.cpp b/src/caffe/test/test_net.cpp index 930486dd6c5..611d11bd832 100644 --- a/src/caffe/test/test_net.cpp +++ b/src/caffe/test/test_net.cpp @@ -1155,7 +1155,7 @@ TYPED_TEST(NetTest, TestParamPropagateDown) { for (int i = 0; i < num_params; ++i) { const Dtype param_asum = caffe_cpu_asum(params2[i]->count(), params2[i]->cpu_diff()); - EXPECT_EQ(param_asum, param_asums[i]); + EXPECT_FLOAT_EQ(param_asum, param_asums[i]); } // Change a subset of the learning rates to zero; check that we see zero @@ -1172,9 +1172,9 @@ TYPED_TEST(NetTest, TestParamPropagateDown) { const Dtype param_asum = caffe_cpu_asum(params3[i]->count(), params3[i]->cpu_diff()); if (i == 1 || i == 2) { - EXPECT_EQ(0, param_asum); + EXPECT_FLOAT_EQ(0, param_asum); } else { - EXPECT_EQ(param_asum, param_asums[i]); + EXPECT_FLOAT_EQ(param_asum, param_asums[i]); } } @@ -1191,9 +1191,9 @@ TYPED_TEST(NetTest, TestParamPropagateDown) { const Dtype param_asum = caffe_cpu_asum(params4[i]->count(), params4[i]->cpu_diff()); if (i == 0 || i == 3) { - EXPECT_EQ(0, param_asum); + EXPECT_FLOAT_EQ(0, param_asum); } else { - EXPECT_EQ(param_asum, param_asums[i]); + EXPECT_FLOAT_EQ(param_asum, param_asums[i]); } } } diff --git a/src/caffe/util/math_functions.cpp b/src/caffe/util/math_functions.cpp index c3afd2ffbc5..13e17be582b 100644 --- a/src/caffe/util/math_functions.cpp +++ b/src/caffe/util/math_functions.cpp @@ -370,9 +370,6 @@ double caffe_cpu_asum(const int n, const double* x) { return cblas_dasum(n, x, 1); } -INSTANTIATE_CAFFE_CPU_UNARY_FUNC(sign); -INSTANTIATE_CAFFE_CPU_UNARY_FUNC(sgnbit); - template <> void caffe_cpu_scale(const int n, const float alpha, const float *x, float* y) {