Skip to content

Commit 36dd770

Browse files
author
wanghaox
committed
add roi operator unittest
2 parents 7960928 + 53bd51e commit 36dd770

File tree

85 files changed

+2441
-7522
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+2441
-7522
lines changed

benchmark/IntelOptimizedPaddle.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ Machine:
1212

1313
System: CentOS release 6.3 (Final), Docker 1.12.1.
1414

15-
PaddlePaddle: paddlepaddle/paddle:latest (TODO: will rerun after 0.11.0)
16-
17-
- MKL-DNN tag v0.10
18-
- MKLML 2018.0.20170720
15+
PaddlePaddle: paddlepaddle/paddle:latest (for MKLML and MKL-DNN), paddlepaddle/paddle:latest-openblas (for OpenBLAS)
16+
- MKL-DNN tag v0.11
17+
- MKLML 2018.0.1.20171007
1918
- OpenBLAS v0.2.20
19+
(TODO: will rerun after 0.11.0)
2020

2121
On each machine, we will test and compare the performance of training on single node using MKL-DNN / MKLML / OpenBLAS respectively.
2222

@@ -31,15 +31,26 @@ Input image size - 3 * 224 * 224, Time: images/second
3131

3232
| BatchSize | 64 | 128 | 256 |
3333
|--------------|-------| -----| --------|
34-
| OpenBLAS | 7.82 | 8.62 | 10.34 |
35-
| MKLML | 11.02 | 12.86 | 15.33 |
36-
| MKL-DNN | 27.69 | 28.8 | 29.27 |
34+
| OpenBLAS | 7.80 | 9.00 | 10.80 |
35+
| MKLML | 12.12 | 13.70 | 16.18 |
36+
| MKL-DNN | 28.46 | 29.83 | 30.44 |
37+
38+
39+
chart on batch size 128
40+
TBD
41+
42+
- ResNet-50
43+
44+
| BatchSize | 64 | 128 | 256 |
45+
|--------------|-------| ------| -------|
46+
| OpenBLAS | 25.22 | 25.68 | 27.12 |
47+
| MKLML | 32.52 | 31.89 | 33.12 |
48+
| MKL-DNN | 81.69 | 82.35 | 84.08 |
3749

3850

3951
chart on batch size 128
4052
TBD
4153

42-
- ResNet
4354
- GoogLeNet
4455

4556
### Laptop

paddle/gserver/activations/ActivationFunction.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,37 @@ Error __must_check backward(Argument& act) {
212212
}
213213
END_DEFINE_ACTIVATION(sequence_softmax)
214214

215+
/*
216+
* @brief SoftSign Activation.
217+
* \f[
218+
* f(z) = \frac{z}{1 + |z|}
219+
* \f]
220+
*/
221+
BEGIN_DEFINE_ACTIVATION(softsign)
222+
private:
223+
MatrixPtr denominator_;
224+
225+
Error __must_check forward(Argument& act) {
226+
size_t height = act.value->getHeight();
227+
size_t width = act.value->getWidth();
228+
Matrix::resizeOrCreate(
229+
denominator_, height, width, false, useGpu(act.deviceId));
230+
denominator_->assign(*act.value);
231+
denominator_->abs2();
232+
denominator_->add(1.);
233+
234+
act.value->dotDiv(*act.value, *denominator_);
235+
return Error();
236+
}
237+
238+
Error __must_check backward(Argument& act) {
239+
denominator_->square2();
240+
denominator_->scalarDiv(*denominator_, 1.);
241+
act.grad->dotMul(*act.grad, *denominator_);
242+
return Error();
243+
}
244+
END_DEFINE_ACTIVATION(softsign)
245+
215246
/**
216247
* @brief Relu Activation.
217248
* forward. y = max(0, z)

paddle/gserver/layers/MKLDNNAddtoLayer.cpp

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ bool MKLDNNAddtoLayer::init(const LayerMap& layerMap,
3838
}
3939

4040
void MKLDNNAddtoLayer::reshape(
41-
int& bs, int& ic, int& ih, int& iw, int oc, int& oh, int& ow) {
41+
int& bs, int& ic, int& ih, int& iw, int& oc, int& oh, int& ow) {
4242
CHECK_EQ(layerSize_, getSize()) << "this layer size can not be changed";
4343
reshapeInput(bs, ih, iw);
4444
ic = inputLayers_[0]->getSize() / ih / iw;
4545
CHECK_EQ((size_t)ic * ih * iw, inputLayers_[0]->getSize());
46-
CHECK_EQ(inputElemenCnt_, (size_t)bs * ic * ih * iw);
46+
CHECK_EQ(inputLayers_[0]->getOutputValue()->getElementCnt(),
47+
(size_t)bs * ic * ih * iw);
4748
for (size_t i = 0; i < inputLayers_.size(); i++) {
4849
CHECK_EQ(int64_t(bs), inputLayers_[i]->getOutput().getBatchSize());
4950
CHECK_EQ(layerSize_, inputLayers_[i]->getSize());
@@ -57,47 +58,43 @@ void MKLDNNAddtoLayer::reshape(
5758
}
5859

5960
void MKLDNNAddtoLayer::resetFwd(std::vector<primitive>& pipeline,
60-
MKLDNNMatrixPtr& in,
61-
MKLDNNMatrixPtr& wgt,
62-
MKLDNNMatrixPtr& bias,
61+
std::vector<MKLDNNMatrixPtr>& inputs,
6362
MKLDNNMatrixPtr& out) {
64-
resetFwdBuffers(inVals_, bias, out);
65-
in = inVals_[0];
63+
resetFwdBuffers(inputs, biasVal_, out);
6664

6765
std::shared_ptr<sum::primitive_desc> fwdPD;
6866
std::shared_ptr<sum::primitive_desc> biasPD;
69-
resetFwdPD(fwdPD, biasPD, inVals_, bias, out);
67+
resetFwdPD(fwdPD, biasPD, inputs, biasVal_, out);
7068

71-
resetFwdPipeline(pipeline, fwdPD, biasPD, inVals_, bias, out);
69+
resetFwdPipeline(pipeline, fwdPD, biasPD, inputs, biasVal_, out);
7270
}
7371

7472
void MKLDNNAddtoLayer::resetBwd(std::vector<primitive>& pipeline,
75-
MKLDNNMatrixPtr& in,
76-
MKLDNNMatrixPtr& wgt,
77-
MKLDNNMatrixPtr& bias,
73+
std::vector<MKLDNNMatrixPtr>& inputs,
7874
MKLDNNMatrixPtr& out) {
79-
resetBwdBuffers(inGrads_, bias, out);
80-
in = inGrads_[0];
75+
resetBwdBuffers(inputs, biasGrad_, out);
8176

8277
// backward only need share output grad to input grad
83-
for (size_t i = 0; i < inGrads_.size(); i++) {
84-
if (inGrads_[i] != nullptr) {
85-
inGrads_[i] = out;
86-
inputLayers_[i]->getOutputGrad()->setData(inGrads_[i]->getData());
78+
for (size_t i = 0; i < inputs.size(); i++) {
79+
if (inputs[i] != nullptr) {
80+
inputs[i] = out;
81+
inputLayers_[i]->getOutputGrad()->setData(inputs[i]->getData());
8782
}
8883
}
8984

9085
// backward bias
9186
bwdBias_ = nullptr;
92-
if (bias) {
87+
if (biasGrad_) {
9388
std::vector<float> scales(bs_, 1.0);
94-
std::vector<memory::primitive_desc> srcPDs(bs_, bias->getPrimitiveDesc());
95-
auto biasPD = sum::primitive_desc(bias->getMemoryDesc(), scales, srcPDs);
89+
std::vector<memory::primitive_desc> srcPDs(bs_,
90+
biasGrad_->getPrimitiveDesc());
91+
auto biasPD =
92+
sum::primitive_desc(biasGrad_->getMemoryDesc(), scales, srcPDs);
9693
std::vector<primitive::at> srcs;
9794
for (size_t i = 0; i < grads_.size(); ++i) {
9895
srcs.push_back(*(grads_[i]));
9996
}
100-
bwdBias_.reset(new sum(biasPD, srcs, *bias));
97+
bwdBias_.reset(new sum(biasPD, srcs, *biasGrad_));
10198
pipeline.push_back(*bwdBias_);
10299
}
103100
}
@@ -208,7 +205,7 @@ void MKLDNNAddtoLayer::resetBwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
208205

209206
inputs.resize(inputLayers_.size());
210207
for (size_t i = 0; i < inputs.size(); i++) {
211-
resetInGrad(inputs[i], inVal_->getPrimitiveDesc(), i);
208+
resetInGrad(inputs[i], inVals_[i]->getPrimitiveDesc(), i);
212209
CHECK_PRIMITIVE_DESC_EQ(inputs[i], out->getPrimitiveDesc());
213210
}
214211

paddle/gserver/layers/MKLDNNAddtoLayer.h

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ namespace paddle {
2626
*/
2727
class MKLDNNAddtoLayer : public MKLDNNLayer {
2828
protected:
29-
std::vector<MKLDNNMatrixPtr> inVals_;
30-
std::vector<MKLDNNMatrixPtr> inGrads_;
31-
3229
// layer size == ic * ih * iw == oc * oh *ow, and can not be changed
3330
size_t layerSize_;
3431

@@ -50,52 +47,19 @@ class MKLDNNAddtoLayer : public MKLDNNLayer {
5047
const ParameterMap& parameterMap) override;
5148

5249
void reshape(
53-
int& bs, int& ic, int& ih, int& iw, int oc, int& oh, int& ow) override;
50+
int& bs, int& ic, int& ih, int& iw, int& oc, int& oh, int& ow) override;
5451

5552
void resetFwd(std::vector<mkldnn::primitive>& pipeline,
56-
MKLDNNMatrixPtr& in,
57-
MKLDNNMatrixPtr& wgt,
58-
MKLDNNMatrixPtr& bias,
53+
std::vector<MKLDNNMatrixPtr>& inputs,
5954
MKLDNNMatrixPtr& out) override;
6055

6156
void resetBwd(std::vector<mkldnn::primitive>& pipeline,
62-
MKLDNNMatrixPtr& in,
63-
MKLDNNMatrixPtr& wgt,
64-
MKLDNNMatrixPtr& bias,
57+
std::vector<MKLDNNMatrixPtr>& inputs,
6558
MKLDNNMatrixPtr& out) override;
6659

6760
void updateWeights(const UpdateCallback& callback) override;
6861

69-
void printValueFormat() override {
70-
for (size_t i = 0; i < inVals_.size(); ++i) {
71-
VLOG(MKLDNN_FMTS) << i << " input: " << inVals_[i]->getFormat() << " >>>";
72-
}
73-
if (outVal_) {
74-
VLOG(MKLDNN_FMTS) << outVal_->getFormat() << " >>> ";
75-
}
76-
if (extOutVal_) {
77-
VLOG(MKLDNN_FMTS) << extOutVal_->getFormat();
78-
}
79-
}
80-
81-
void printGradFormat() override {
82-
if (extOutGrad_) {
83-
VLOG(MKLDNN_FMTS) << extOutGrad_->getFormat();
84-
}
85-
if (outGrad_) {
86-
VLOG(MKLDNN_FMTS) << outGrad_->getFormat() << " <<< ";
87-
}
88-
for (size_t i = 0; i < inGrads_.size(); ++i) {
89-
VLOG(MKLDNN_FMTS) << i << " input: " << inGrads_[i]->getFormat() << "<<<";
90-
}
91-
}
92-
9362
protected:
94-
/**
95-
* Forward functions: reset buffers(inputs, output, bias),
96-
* reset primitive descriptor,
97-
* reset pipeline.
98-
*/
9963
void resetFwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
10064
MKLDNNMatrixPtr& bias,
10165
MKLDNNMatrixPtr& out);
@@ -110,17 +74,10 @@ class MKLDNNAddtoLayer : public MKLDNNLayer {
11074
std::vector<MKLDNNMatrixPtr>& inputs,
11175
MKLDNNMatrixPtr& bias,
11276
MKLDNNMatrixPtr& out);
113-
114-
/**
115-
* Backward functions: reset buffers(inputs, output, bias)
116-
*/
11777
void resetBwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
11878
MKLDNNMatrixPtr& bias,
11979
MKLDNNMatrixPtr& out);
12080

121-
/**
122-
* prepare for bias
123-
*/
12481
void prepareBias(MKLDNNMatrixPtr& bias,
12582
const MatrixPtr& biasMat,
12683
const MKLDNNMatrixPtr& out,

paddle/gserver/layers/MKLDNNBatchNormLayer.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,20 @@ void MKLDNNBatchNormLayer::calMovingMeanAndVar() {
116116
}
117117

118118
void MKLDNNBatchNormLayer::reshape(
119-
int& bs, int& ic, int& ih, int& iw, int oc, int& oh, int& ow) {
119+
int& bs, int& ic, int& ih, int& iw, int& oc, int& oh, int& ow) {
120120
reshapeInput(bs, ih, iw);
121121
oh = ih;
122122
ow = iw;
123123
// ic_ and oc can not be changed
124-
CHECK_EQ(inputElemenCnt_ / bs / ih / iw, (size_t)ic)
124+
CHECK_EQ((size_t)ic,
125+
inputLayers_[0]->getOutputValue()->getElementCnt() / bs / ih / iw)
125126
<< "Input channel can not be changed";
126127
reshapeOutput(oh, ow);
127128
resizeOutput(bs, oc * oh * ow);
128129
}
129130

130131
void MKLDNNBatchNormLayer::resetFwd(std::vector<primitive>& pipeline,
131-
MKLDNNMatrixPtr& in,
132-
MKLDNNMatrixPtr& wgt,
133-
MKLDNNMatrixPtr& bias,
132+
std::vector<MKLDNNMatrixPtr>& inputs,
134133
MKLDNNMatrixPtr& out) {
135134
// In training phase, it will always calculate mean and var,
136135
// so useGlobalStats must be false.
@@ -140,25 +139,23 @@ void MKLDNNBatchNormLayer::resetFwd(std::vector<primitive>& pipeline,
140139
useGlobalStats_ = false;
141140
}
142141

143-
resetFwdBuffers(in, wgt, out);
142+
resetFwdBuffers(inputs[0], wgtVal_, out);
144143

145-
resetFwdPD(fwdPD_, in, wgt, out);
144+
resetFwdPD(fwdPD_, inputs[0], wgtVal_, out);
146145

147-
resetFwdPipeline(pipeline, fwdPD_, in, wgt, out);
146+
resetFwdPipeline(pipeline, fwdPD_, inputs[0], wgtVal_, out);
148147
}
149148

150149
void MKLDNNBatchNormLayer::resetBwd(std::vector<primitive>& pipeline,
151-
MKLDNNMatrixPtr& in,
152-
MKLDNNMatrixPtr& wgt,
153-
MKLDNNMatrixPtr& bias,
150+
std::vector<MKLDNNMatrixPtr>& inputs,
154151
MKLDNNMatrixPtr& out) {
155152
std::shared_ptr<bn_bwd::primitive_desc> pd;
156153

157-
resetBwdBuffers(in, wgt, out);
154+
resetBwdBuffers(inputs[0], wgtGrad_, out);
158155

159-
resetBwdPD(pd, in, wgt, out);
156+
resetBwdPD(pd, inputs[0], wgtGrad_, out);
160157

161-
resetBwdPipeline(pipeline, pd, in, wgt, out);
158+
resetBwdPipeline(pipeline, pd, inputs[0], wgtGrad_, out);
162159
}
163160

164161
void MKLDNNBatchNormLayer::forward(PassType passType) {
@@ -260,9 +257,9 @@ void MKLDNNBatchNormLayer::resetFwdPipeline(
260257
void MKLDNNBatchNormLayer::resetBwdBuffers(MKLDNNMatrixPtr& in,
261258
MKLDNNMatrixPtr& wgt,
262259
MKLDNNMatrixPtr& out) {
263-
CHECK(inVal_ && outVal_);
260+
CHECK(inVals_[0] && outVal_);
264261
resetOutGrad(out, outVal_->getPrimitiveDesc());
265-
resetInGrad(in, inVal_->getPrimitiveDesc());
262+
resetInGrad(in, inVals_[0]->getPrimitiveDesc());
266263
if (gradScaleShift_) {
267264
CHECK(wgtVal_);
268265
resetWithMatrix(wgt, gradScaleShift_, wgtVal_->getPrimitiveDesc());
@@ -297,11 +294,12 @@ void MKLDNNBatchNormLayer::resetBwdPipeline(
297294
if (pd == nullptr) {
298295
return;
299296
}
300-
CHECK(inVal_);
297+
CHECK(inVals_[0]);
301298
bwdData_.reset(
302299
wgt && wgtVal_
303-
? new bn_bwd(*pd, *inVal_, *mean_, *var_, *out, *wgtVal_, *in, *wgt)
304-
: new bn_bwd(*pd, *inVal_, *mean_, *var_, *out, *in));
300+
? new bn_bwd(
301+
*pd, *inVals_[0], *mean_, *var_, *out, *wgtVal_, *in, *wgt)
302+
: new bn_bwd(*pd, *inVals_[0], *mean_, *var_, *out, *in));
305303
pipeline.push_back(*bwdData_);
306304
}
307305

paddle/gserver/layers/MKLDNNBatchNormLayer.h

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,14 @@ class MKLDNNBatchNormLayer : public MKLDNNLayer {
7373
void forward(PassType passType) override;
7474

7575
void reshape(
76-
int& bs, int& ic, int& ih, int& iw, int oc, int& oh, int& ow) override;
76+
int& bs, int& ic, int& ih, int& iw, int& oc, int& oh, int& ow) override;
7777

7878
void resetFwd(std::vector<mkldnn::primitive>& pipeline,
79-
MKLDNNMatrixPtr& in,
80-
MKLDNNMatrixPtr& wgt,
81-
MKLDNNMatrixPtr& bias,
79+
std::vector<MKLDNNMatrixPtr>& inputs,
8280
MKLDNNMatrixPtr& out) override;
8381

8482
void resetBwd(std::vector<mkldnn::primitive>& pipeline,
85-
MKLDNNMatrixPtr& in,
86-
MKLDNNMatrixPtr& wgt,
87-
MKLDNNMatrixPtr& bias,
83+
std::vector<MKLDNNMatrixPtr>& inputs,
8884
MKLDNNMatrixPtr& out) override;
8985

9086
void updateWeights(const UpdateCallback& callback) override;
@@ -98,11 +94,7 @@ class MKLDNNBatchNormLayer : public MKLDNNLayer {
9894
* moving = moving * AvgFraction + local * (1 - AvgFraction)
9995
*/
10096
void calMovingMeanAndVar();
101-
/**
102-
* Forward functions: reset buffers(input, weight, output),
103-
* reset primitive descriptor,
104-
* reset pipeline.
105-
*/
97+
10698
void resetFwdBuffers(MKLDNNMatrixPtr& in,
10799
MKLDNNMatrixPtr& wgt,
108100
MKLDNNMatrixPtr& out);
@@ -115,12 +107,6 @@ class MKLDNNBatchNormLayer : public MKLDNNLayer {
115107
MKLDNNMatrixPtr& in,
116108
MKLDNNMatrixPtr& wgt,
117109
MKLDNNMatrixPtr& out);
118-
119-
/**
120-
* Backward functions: reset buffers(input, weight, output),
121-
* reset primitive descriptor,
122-
* reset pipeline.
123-
*/
124110
void resetBwdBuffers(MKLDNNMatrixPtr& in,
125111
MKLDNNMatrixPtr& wgt,
126112
MKLDNNMatrixPtr& out);

0 commit comments

Comments
 (0)