|
|
@@ -15,8 +15,10 @@ |
|
|
|
|
|
namespace caffe {
|
|
|
|
|
|
-template<typename Dtype>
|
|
|
-class MathFunctionsTest : public ::testing::Test {
|
|
|
+template <typename TypeParam>
|
|
|
+class MathFunctionsTest : public MultiDeviceTest<TypeParam> {
|
|
|
+ typedef typename TypeParam::Dtype Dtype;
|
|
|
+
|
|
|
protected:
|
|
|
MathFunctionsTest()
|
|
|
: blob_bottom_(new Blob<Dtype>()),
|
|
|
@@ -64,22 +66,27 @@ class MathFunctionsTest : public ::testing::Test { |
|
|
Blob<Dtype>* const blob_top_;
|
|
|
};
|
|
|
|
|
|
-TYPED_TEST_CASE(MathFunctionsTest, TestDtypes);
|
|
|
+template <typename Dtype>
|
|
|
+class CPUMathFunctionsTest
|
|
|
+ : public MathFunctionsTest<CPUDevice<Dtype> > {
|
|
|
+};
|
|
|
+
|
|
|
+TYPED_TEST_CASE(CPUMathFunctionsTest, TestDtypes);
|
|
|
|
|
|
-TYPED_TEST(MathFunctionsTest, TestNothing) {
|
|
|
+TYPED_TEST(CPUMathFunctionsTest, TestNothing) {
|
|
|
// The first test case of a test suite takes the longest time
|
|
|
// due to the set up overhead.
|
|
|
}
|
|
|
|
|
|
-TYPED_TEST(MathFunctionsTest, TestHammingDistanceCPU) {
|
|
|
+TYPED_TEST(CPUMathFunctionsTest, TestHammingDistance) {
|
|
|
int n = this->blob_bottom_->count();
|
|
|
const TypeParam* x = this->blob_bottom_->cpu_data();
|
|
|
const TypeParam* y = this->blob_top_->cpu_data();
|
|
|
EXPECT_EQ(this->ReferenceHammingDistance(n, x, y),
|
|
|
caffe_cpu_hamming_distance<TypeParam>(n, x, y));
|
|
|
}
|
|
|
|
|
|
-TYPED_TEST(MathFunctionsTest, TestAsumCPU) {
|
|
|
+TYPED_TEST(CPUMathFunctionsTest, TestAsum) {
|
|
|
int n = this->blob_bottom_->count();
|
|
|
const TypeParam* x = this->blob_bottom_->cpu_data();
|
|
|
TypeParam std_asum = 0;
|
|
|
@@ -90,7 +97,7 @@ TYPED_TEST(MathFunctionsTest, TestAsumCPU) { |
|
|
EXPECT_LT((cpu_asum - std_asum) / std_asum, 1e-2);
|
|
|
}
|
|
|
|
|
|
-TYPED_TEST(MathFunctionsTest, TestSignCPU) {
|
|
|
+TYPED_TEST(CPUMathFunctionsTest, TestSign) {
|
|
|
int n = this->blob_bottom_->count();
|
|
|
const TypeParam* x = this->blob_bottom_->cpu_data();
|
|
|
caffe_cpu_sign<TypeParam>(n, x, this->blob_bottom_->mutable_cpu_diff());
|
|
|
@@ -100,7 +107,7 @@ TYPED_TEST(MathFunctionsTest, TestSignCPU) { |
|
|
}
|
|
|
}
|
|
|
|
|
|
-TYPED_TEST(MathFunctionsTest, TestSgnbitCPU) {
|
|
|
+TYPED_TEST(CPUMathFunctionsTest, TestSgnbit) {
|
|
|
int n = this->blob_bottom_->count();
|
|
|
const TypeParam* x = this->blob_bottom_->cpu_data();
|
|
|
caffe_cpu_sgnbit<TypeParam>(n, x, this->blob_bottom_->mutable_cpu_diff());
|
|
|
@@ -110,7 +117,7 @@ TYPED_TEST(MathFunctionsTest, TestSgnbitCPU) { |
|
|
}
|
|
|
}
|
|
|
|
|
|
-TYPED_TEST(MathFunctionsTest, TestFabsCPU) {
|
|
|
+TYPED_TEST(CPUMathFunctionsTest, TestFabs) {
|
|
|
int n = this->blob_bottom_->count();
|
|
|
const TypeParam* x = this->blob_bottom_->cpu_data();
|
|
|
caffe_abs<TypeParam>(n, x, this->blob_bottom_->mutable_cpu_diff());
|
|
|
@@ -120,7 +127,7 @@ TYPED_TEST(MathFunctionsTest, TestFabsCPU) { |
|
|
}
|
|
|
}
|
|
|
|
|
|
-TYPED_TEST(MathFunctionsTest, TestScaleCPU) {
|
|
|
+TYPED_TEST(CPUMathFunctionsTest, TestScale) {
|
|
|
int n = this->blob_bottom_->count();
|
|
|
TypeParam alpha = this->blob_bottom_->cpu_diff()[caffe_rng_rand() %
|
|
|
this->blob_bottom_->count()];
|
|
|
@@ -133,11 +140,10 @@ TYPED_TEST(MathFunctionsTest, TestScaleCPU) { |
|
|
}
|
|
|
}
|
|
|
|
|
|
-TYPED_TEST(MathFunctionsTest, TestCopyCPU) {
|
|
|
+TYPED_TEST(CPUMathFunctionsTest, TestCopy) {
|
|
|
const int n = this->blob_bottom_->count();
|
|
|
const TypeParam* bottom_data = this->blob_bottom_->cpu_data();
|
|
|
TypeParam* top_data = this->blob_top_->mutable_cpu_data();
|
|
|
- Caffe::set_mode(Caffe::CPU);
|
|
|
caffe_copy(n, bottom_data, top_data);
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
EXPECT_EQ(bottom_data[i], top_data[i]);
|
|
|
@@ -146,8 +152,14 @@ TYPED_TEST(MathFunctionsTest, TestCopyCPU) { |
|
|
|
|
|
#ifndef CPU_ONLY
|
|
|
|
|
|
+template <typename Dtype>
|
|
|
+class GPUMathFunctionsTest : public MathFunctionsTest<GPUDevice<Dtype> > {
|
|
|
+};
|
|
|
+
|
|
|
+TYPED_TEST_CASE(GPUMathFunctionsTest, TestDtypes);
|
|
|
+
|
|
|
// TODO: Fix caffe_gpu_hamming_distance and re-enable this test.
|
|
|
-TYPED_TEST(MathFunctionsTest, DISABLED_TestHammingDistanceGPU) {
|
|
|
+TYPED_TEST(GPUMathFunctionsTest, DISABLED_TestHammingDistance) {
|
|
|
int n = this->blob_bottom_->count();
|
|
|
const TypeParam* x = this->blob_bottom_->cpu_data();
|
|
|
const TypeParam* y = this->blob_top_->cpu_data();
|
|
|
@@ -158,7 +170,7 @@ TYPED_TEST(MathFunctionsTest, DISABLED_TestHammingDistanceGPU) { |
|
|
EXPECT_EQ(reference_distance, computed_distance);
|
|
|
}
|
|
|
|
|
|
-TYPED_TEST(MathFunctionsTest, TestAsumGPU) {
|
|
|
+TYPED_TEST(GPUMathFunctionsTest, TestAsum) {
|
|
|
int n = this->blob_bottom_->count();
|
|
|
const TypeParam* x = this->blob_bottom_->cpu_data();
|
|
|
TypeParam std_asum = 0;
|
|
|
@@ -170,7 +182,7 @@ TYPED_TEST(MathFunctionsTest, TestAsumGPU) { |
|
|
EXPECT_LT((gpu_asum - std_asum) / std_asum, 1e-2);
|
|
|
}
|
|
|
|
|
|
-TYPED_TEST(MathFunctionsTest, TestSignGPU) {
|
|
|
+TYPED_TEST(GPUMathFunctionsTest, TestSign) {
|
|
|
int n = this->blob_bottom_->count();
|
|
|
caffe_gpu_sign<TypeParam>(n, this->blob_bottom_->gpu_data(),
|
|
|
this->blob_bottom_->mutable_gpu_diff());
|
|
|
@@ -181,7 +193,7 @@ TYPED_TEST(MathFunctionsTest, TestSignGPU) { |
|
|
}
|
|
|
}
|
|
|
|
|
|
-TYPED_TEST(MathFunctionsTest, TestSgnbitGPU) {
|
|
|
+TYPED_TEST(GPUMathFunctionsTest, TestSgnbit) {
|
|
|
int n = this->blob_bottom_->count();
|
|
|
caffe_gpu_sgnbit<TypeParam>(n, this->blob_bottom_->gpu_data(),
|
|
|
this->blob_bottom_->mutable_gpu_diff());
|
|
|
@@ -192,7 +204,7 @@ TYPED_TEST(MathFunctionsTest, TestSgnbitGPU) { |
|
|
}
|
|
|
}
|
|
|
|
|
|
-TYPED_TEST(MathFunctionsTest, TestFabsGPU) {
|
|
|
+TYPED_TEST(GPUMathFunctionsTest, TestFabs) {
|
|
|
int n = this->blob_bottom_->count();
|
|
|
caffe_gpu_abs<TypeParam>(n, this->blob_bottom_->gpu_data(),
|
|
|
this->blob_bottom_->mutable_gpu_diff());
|
|
|
@@ -203,7 +215,7 @@ TYPED_TEST(MathFunctionsTest, TestFabsGPU) { |
|
|
}
|
|
|
}
|
|
|
|
|
|
-TYPED_TEST(MathFunctionsTest, TestScaleGPU) {
|
|
|
+TYPED_TEST(GPUMathFunctionsTest, TestScale) {
|
|
|
int n = this->blob_bottom_->count();
|
|
|
TypeParam alpha = this->blob_bottom_->cpu_diff()[caffe_rng_rand() %
|
|
|
this->blob_bottom_->count()];
|
|
|
@@ -216,11 +228,10 @@ TYPED_TEST(MathFunctionsTest, TestScaleGPU) { |
|
|
}
|
|
|
}
|
|
|
|
|
|
-TYPED_TEST(MathFunctionsTest, TestCopyGPU) {
|
|
|
+TYPED_TEST(GPUMathFunctionsTest, TestCopy) {
|
|
|
const int n = this->blob_bottom_->count();
|
|
|
const TypeParam* bottom_data = this->blob_bottom_->gpu_data();
|
|
|
TypeParam* top_data = this->blob_top_->mutable_gpu_data();
|
|
|
- Caffe::set_mode(Caffe::GPU);
|
|
|
caffe_copy(n, bottom_data, top_data);
|
|
|
bottom_data = this->blob_bottom_->cpu_data();
|
|
|
top_data = this->blob_top_->mutable_cpu_data();
|
|
|
|