Skip to content

Commit 56f6e23

Browse files
committed
refine mkldnntester, support comparing values near zero
1 parent 0049ce0 commit 56f6e23

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

paddle/gserver/tests/MKLDNNTester.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -273,31 +273,37 @@ void MKLDNNTester::printVector(const VectorPtr& v) {
273273
VLOG(MKLDNN_ALL) << std::endl << ostr.str();
274274
}
275275

276-
double MKLDNNTester::getDelta(const real* d1,
277-
const real* d2,
276+
double MKLDNNTester::getDelta(const real* refer,
277+
const real* value,
278278
size_t len,
279279
const float failRate,
280280
const float thres) {
281281
double delta = 0, sum = 0;
282282
int failCnt = 0;
283283
const double eps = 1e-5;
284-
double maxOut = 0;
284+
double maxRatio = 0;
285285
for (size_t i = 0; i < len; ++i) {
286-
double ref = fabs(d2[i]);
287-
double diff = fabs(d1[i] - d2[i]);
286+
double ref = fabs(refer[i]);
287+
double val = fabs(value[i]);
288+
double diff = fabs(refer[i] - value[i]);
288289
delta += diff;
289290
sum += ref;
290-
if (ref > eps && fabs(d1[i]) > eps && diff / ref > thres) {
291-
maxOut = std::max(maxOut, diff / ref);
291+
if (ref < eps && val < eps) { // both values are very small
292+
continue;
293+
}
294+
double ratio = diff / ref;
295+
if (ratio > thres) {
296+
maxRatio = std::max(maxRatio, ratio);
292297
failCnt++;
293298
}
294299
}
295-
EXPECT_TRUE(std::isnormal(sum));
296300
EXPECT_FALSE(std::isinf(sum));
301+
EXPECT_FALSE(std::isnan(sum));
297302
EXPECT_FALSE(std::isnan(delta));
298303
VLOG(MKLDNN_ALL) << "reference avg data: " << sum / len
299304
<< ", delta: " << delta / sum << ", failCnt:" << failCnt;
300-
return (failCnt / (float)len) > failRate ? maxOut : delta / sum;
305+
double res = sum > eps ? delta / sum : eps;
306+
return (failCnt / (float)len) > failRate ? maxRatio : res;
301307
}
302308

303309
double MKLDNNTester::compareMatrix(const MatrixPtr& m1, const MatrixPtr& m2) {
@@ -543,12 +549,12 @@ void MKLDNNTester::getOutResult(const std::string& configPath,
543549
void MKLDNNTester::compareResult(DataOut& ref, DataOut& dnn, float eps) {
544550
CHECK_EQ(ref.outValues.size(), dnn.outValues.size());
545551
CHECK_EQ(ref.paraValues.size(), dnn.paraValues.size());
546-
VLOG(MKLDNN_TESTS) << "compare value size: " << ref.outValues.size();
547552
for (size_t i = 0; i < ref.outValues.size(); i++) {
553+
VLOG(MKLDNN_TESTS) << "compare value index: " << i;
548554
EXPECT_LE(fabs(compareMatrix(ref.outValues[i], dnn.outValues[i])), eps);
549555
}
550-
VLOG(MKLDNN_TESTS) << "compare param size: " << ref.outValues.size();
551556
for (size_t i = 0; i < ref.paraValues.size(); i++) {
557+
VLOG(MKLDNN_TESTS) << "compare param index: " << i;
552558
EXPECT_LE(fabs(compareVector(ref.paraValues[i], dnn.paraValues[i])), eps);
553559
}
554560
}

paddle/gserver/tests/MKLDNNTester.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ class MKLDNNTester {
128128

129129
/**
130130
* Get delta percent
131-
* if many(>failRate) wrong(abs(dnn-ref)/abs(ref)>thres) points return the
132-
* max(diff/ref)
133-
* else return sum(abs(a-b)) / sum(abs(b))
131+
* if many(>failRate) wrong(abs(val-ref)/abs(ref) > thres) points
132+
* return the max(diff/ref)
133+
* else return sum(abs(diff)) / sum(abs(ref))
134134
* The return value should be smaller than eps when passing.
135135
*/
136-
static double getDelta(const real* d1,
137-
const real* d2,
136+
static double getDelta(const real* refer,
137+
const real* value,
138138
size_t len,
139139
const float failRate = 1e-3,
140140
const float thres = 0.1);

paddle/gserver/tests/test_MKLDNN.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,7 @@ static void getMKLDNNBatchNormConfig(TestConfig& cfg,
234234
cfg.inputDefs.push_back({INPUT_DATA, "layer_2_moving_var", 1, size_t(pm.ic)});
235235
cfg.inputDefs.back().isStatic = true;
236236
LayerInputConfig* input = cfg.layerConfig.add_inputs();
237-
// TODO(TJ): uncomment me when refine and support comparing all zeroes vector
238-
// cfg.layerConfig.set_active_type("relu");
237+
cfg.layerConfig.set_active_type("relu");
239238
cfg.layerConfig.add_inputs();
240239
cfg.layerConfig.add_inputs();
241240
ImageConfig* img_conf = input->mutable_image_conf();

0 commit comments

Comments
 (0)