Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean flaky code #1264

Merged
merged 2 commits into from
Oct 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions include/caffe/util/math_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename Dtype>
inline char caffe_sign(Dtype val) {
inline int8_t caffe_sign(Dtype val) {
return (Dtype(0) < val) - (val < Dtype(0));
}

Expand All @@ -127,20 +127,15 @@ inline char caffe_sign(Dtype val) {
} \
}

#define INSTANTIATE_CAFFE_CPU_UNARY_FUNC(name) \
template <> \
void caffe_cpu_##name<float>(const int n, const float* x, float* y); \
template <> \
void caffe_cpu_##name<double>(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<Dtype>(x[i]));

// This returns a nonzero value if the input has its sign bit set.
// 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<bool>((std::signbit)(x[i])));

DEFINE_CAFFE_CPU_UNARY_FUNC(fabs, y[i] = std::fabs(x[i]));

Expand Down
10 changes: 5 additions & 5 deletions src/caffe/test/test_net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]);
}
}

Expand All @@ -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]);
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/caffe/util/math_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,6 @@ double caffe_cpu_asum<double>(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<float>(const int n, const float alpha, const float *x,
float* y) {
Expand Down