FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
CaffeOnACL/src/caffe/test/test_math_functions.cpp at master · rootkit/CaffeOnACL · GitHub
rootkit
CaffeOnACL
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
CaffeOnACL
/
src
/
caffe
/
test
/
test_math_functions.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
203 lines (173 loc) · 6.44 KB
Breadcrumbs
CaffeOnACL
/
src
/
caffe
/
test
/
test_math_functions.cpp
Copy path
File metadata and controls
203 lines (173 loc) · 6.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#
include
<
stdint.h
>
//
for uint32_t & uint64_t
#
include
<
time.h
>
#
include
<
cmath
>
//
for std::fabs
#
include
"
gtest/gtest.h
"
#
include
"
caffe/blob.hpp
"
#
include
"
caffe/common.hpp
"
#
include
"
caffe/filler.hpp
"
#
include
"
caffe/util/math_functions.hpp
"
#
include
"
caffe/test/test_caffe_main.hpp
"
namespace
caffe
{
template
<
typename
TypeParam>
class
MathFunctionsTest
:
public
MultiDeviceTest
<TypeParam> {
typedef
typename
TypeParam::Dtype Dtype;
protected:
MathFunctionsTest
()
: blob_bottom_(
new
Blob<Dtype>()),
blob_top_
(
new
Blob<Dtype>()) {
}
virtual
void
SetUp
() {
Caffe::set_random_seed
(
1701
);
this
->
blob_bottom_
->
Reshape
(
11
,
17
,
19
,
23
);
this
->
blob_top_
->
Reshape
(
11
,
17
,
19
,
23
);
//
fill the values
FillerParameter filler_param;
GaussianFiller<Dtype>
filler
(filler_param);
filler.
Fill
(
this
->
blob_bottom_
);
filler.
Fill
(
this
->
blob_top_
);
}
virtual
~MathFunctionsTest
() {
delete
blob_bottom_;
delete
blob_top_;
}
Blob<Dtype>*
const
blob_bottom_;
Blob<Dtype>*
const
blob_top_;
};
template
<
typename
Dtype>
class
CPUMathFunctionsTest
: public MathFunctionsTest<CPUDevice<Dtype> > {
};
TYPED_TEST_CASE
(CPUMathFunctionsTest, TestDtypes);
TYPED_TEST
(CPUMathFunctionsTest, TestNothing) {
//
The first test case of a test suite takes the longest time
//
due to the set up overhead.
}
TYPED_TEST
(CPUMathFunctionsTest, TestAsum) {
int
n =
this
->
blob_bottom_
->
count
();
const
TypeParam* x =
this
->
blob_bottom_
->
cpu_data
();
TypeParam std_asum =
0
;
for
(
int
i =
0
; i < n; ++i) {
std_asum +=
std::fabs
(x[i]);
}
TypeParam cpu_asum = caffe_cpu_asum<TypeParam>(n, x);
EXPECT_LT
((cpu_asum - std_asum) / std_asum,
1e-2
);
}
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
());
const
TypeParam* signs =
this
->
blob_bottom_
->
cpu_diff
();
for
(
int
i =
0
; i < n; ++i) {
EXPECT_EQ
(signs[i], x[i] >
0
?
1
: (x[i] <
0
? -
1
:
0
));
}
}
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
());
const
TypeParam* signbits =
this
->
blob_bottom_
->
cpu_diff
();
for
(
int
i =
0
; i < n; ++i) {
EXPECT_EQ
(signbits[i], x[i] <
0
?
1
:
0
);
}
}
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
());
const
TypeParam* abs_val =
this
->
blob_bottom_
->
cpu_diff
();
for
(
int
i =
0
; i < n; ++i) {
EXPECT_EQ
(abs_val[i], x[i] >
0
? x[i] : -x[i]);
}
}
TYPED_TEST
(CPUMathFunctionsTest, TestScale) {
int
n =
this
->
blob_bottom_
->
count
();
TypeParam alpha =
this
->
blob_bottom_
->
cpu_diff
()[
caffe_rng_rand
() %
this
->
blob_bottom_
->
count
()];
caffe_cpu_scale<TypeParam>(n, alpha,
this
->
blob_bottom_
->
cpu_data
(),
this
->
blob_bottom_
->
mutable_cpu_diff
());
const
TypeParam* scaled =
this
->
blob_bottom_
->
cpu_diff
();
const
TypeParam* x =
this
->
blob_bottom_
->
cpu_data
();
for
(
int
i =
0
; i < n; ++i) {
EXPECT_EQ
(scaled[i], x[i] * alpha);
}
}
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_copy
(n, bottom_data, top_data);
for
(
int
i =
0
; i < n; ++i) {
EXPECT_EQ
(bottom_data[i], top_data[i]);
}
}
#
ifndef
CPU_ONLY
template
<
typename
Dtype>
class
GPUMathFunctionsTest
:
public
MathFunctionsTest
<GPUDevice<Dtype> > {
};
TYPED_TEST_CASE
(GPUMathFunctionsTest, TestDtypes);
TYPED_TEST
(GPUMathFunctionsTest, TestAsum) {
int
n =
this
->
blob_bottom_
->
count
();
const
TypeParam* x =
this
->
blob_bottom_
->
cpu_data
();
TypeParam std_asum =
0
;
for
(
int
i =
0
; i < n; ++i) {
std_asum +=
std::fabs
(x[i]);
}
TypeParam gpu_asum;
caffe_gpu_asum<TypeParam>(n,
this
->
blob_bottom_
->
gpu_data
(), &gpu_asum);
EXPECT_LT
((gpu_asum - std_asum) / std_asum,
1e-2
);
}
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
());
const
TypeParam* signs =
this
->
blob_bottom_
->
cpu_diff
();
const
TypeParam* x =
this
->
blob_bottom_
->
cpu_data
();
for
(
int
i =
0
; i < n; ++i) {
EXPECT_EQ
(signs[i], x[i] >
0
?
1
: (x[i] <
0
? -
1
:
0
));
}
}
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
());
const
TypeParam* signbits =
this
->
blob_bottom_
->
cpu_diff
();
const
TypeParam* x =
this
->
blob_bottom_
->
cpu_data
();
for
(
int
i =
0
; i < n; ++i) {
EXPECT_EQ
(signbits[i], x[i] <
0
?
1
:
0
);
}
}
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
());
const
TypeParam* abs_val =
this
->
blob_bottom_
->
cpu_diff
();
const
TypeParam* x =
this
->
blob_bottom_
->
cpu_data
();
for
(
int
i =
0
; i < n; ++i) {
EXPECT_EQ
(abs_val[i], x[i] >
0
? x[i] : -x[i]);
}
}
TYPED_TEST
(GPUMathFunctionsTest, TestScale) {
int
n =
this
->
blob_bottom_
->
count
();
TypeParam alpha =
this
->
blob_bottom_
->
cpu_diff
()[
caffe_rng_rand
() %
this
->
blob_bottom_
->
count
()];
caffe_gpu_scale<TypeParam>(n, alpha,
this
->
blob_bottom_
->
gpu_data
(),
this
->
blob_bottom_
->
mutable_gpu_diff
());
const
TypeParam* scaled =
this
->
blob_bottom_
->
cpu_diff
();
const
TypeParam* x =
this
->
blob_bottom_
->
cpu_data
();
for
(
int
i =
0
; i < n; ++i) {
EXPECT_EQ
(scaled[i], x[i] * alpha);
}
}
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_copy
(n, bottom_data, top_data);
bottom_data =
this
->
blob_bottom_
->
cpu_data
();
top_data =
this
->
blob_top_
->
mutable_cpu_data
();
for
(
int
i =
0
; i < n; ++i) {
EXPECT_EQ
(bottom_data[i], top_data[i]);
}
}
#
endif
}
//
namespace caffe
Back
|
FazBrowse Home
|
New Git URL