Skip to content

Commit bef9227

Browse files
authored
Merge pull request #5440 from tensor-tang/mkldnn_addto_withbias
enable bias for mkldnn_addto
2 parents 86e58b2 + 93e22e7 commit bef9227

File tree

3 files changed

+99
-15
lines changed

3 files changed

+99
-15
lines changed

paddle/gserver/layers/MKLDNNAddtoLayer.cpp

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,22 @@ void MKLDNNAddtoLayer::resetFwd(std::vector<primitive>& pipeline,
6262
MKLDNNMatrixPtr& wgt,
6363
MKLDNNMatrixPtr& bias,
6464
MKLDNNMatrixPtr& out) {
65-
if (biases_) {
66-
LOG(FATAL) << "not implemented yet";
67-
}
68-
resetFwdBuffers(inVals_, out);
65+
resetFwdBuffers(inVals_, bias, out);
6966
in = inVals_[0];
7067

7168
std::shared_ptr<sum::primitive_desc> fwdPD;
72-
resetFwdPD(fwdPD, inVals_, out);
69+
std::shared_ptr<sum::primitive_desc> biasPD;
70+
resetFwdPD(fwdPD, biasPD, inVals_, bias, out);
7371

74-
resetFwdPipeline(pipeline, fwdPD, inVals_, out);
72+
resetFwdPipeline(pipeline, fwdPD, biasPD, inVals_, bias, out);
7573
}
7674

7775
void MKLDNNAddtoLayer::resetBwd(std::vector<primitive>& pipeline,
7876
MKLDNNMatrixPtr& in,
7977
MKLDNNMatrixPtr& wgt,
8078
MKLDNNMatrixPtr& bias,
8179
MKLDNNMatrixPtr& out) {
82-
resetBwdBuffers(inGrads_, out);
80+
resetBwdBuffers(inGrads_, bias, out);
8381
in = inGrads_[0];
8482

8583
// backward only need share output grad to input grad
@@ -89,6 +87,20 @@ void MKLDNNAddtoLayer::resetBwd(std::vector<primitive>& pipeline,
8987
inputLayers_[i]->getOutputGrad()->setData(inGrads_[i]->getData());
9088
}
9189
}
90+
91+
// backward bias
92+
bwdBias_ = nullptr;
93+
if (bias) {
94+
std::vector<double> scales(bs_, 1.0);
95+
std::vector<memory::primitive_desc> srcPDs(bs_, bias->getPrimitiveDesc());
96+
auto biasPD = sum::primitive_desc(bias->getMemoryDesc(), scales, srcPDs);
97+
std::vector<primitive::at> srcs;
98+
for (size_t i = 0; i < grads_.size(); ++i) {
99+
srcs.push_back(*(grads_[i]));
100+
}
101+
bwdBias_.reset(new sum(biasPD, srcs, *bias));
102+
pipeline.push_back(*bwdBias_);
103+
}
92104
}
93105

94106
void MKLDNNAddtoLayer::updateWeights(const UpdateCallback& callback) {
@@ -97,7 +109,25 @@ void MKLDNNAddtoLayer::updateWeights(const UpdateCallback& callback) {
97109
}
98110
}
99111

112+
void MKLDNNAddtoLayer::prepareBias(MKLDNNMatrixPtr& bias,
113+
const MatrixPtr& biasMat,
114+
const MKLDNNMatrixPtr& out,
115+
std::vector<MKLDNNMatrixPtr>& outs) {
116+
auto pd = MKLDNNMatrix::createPrimitiveDesc(
117+
{(int)layerSize_}, memory::format::x, engine_);
118+
bias = MKLDNNMatrix::create(pd, biasMat);
119+
outs.clear();
120+
real* data = out->getData();
121+
CHECK_EQ(bs_ * layerSize_, out->getElementCnt());
122+
for (int i = 0; i < bs_; ++i) {
123+
MatrixPtr tmp =
124+
Matrix::create(data + i * layerSize_, 1, layerSize_, false, false);
125+
outs.push_back(MKLDNNMatrix::create(bias->getPrimitiveDesc(), tmp));
126+
}
127+
}
128+
100129
void MKLDNNAddtoLayer::resetFwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
130+
MKLDNNMatrixPtr& bias,
101131
MKLDNNMatrixPtr& out) {
102132
inputs.resize(inputLayers_.size());
103133
for (size_t i = 0; i < inputs.size(); i++) {
@@ -110,10 +140,18 @@ void MKLDNNAddtoLayer::resetFwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
110140
}
111141

112142
resetOutValue(out, inputs[0]->getPrimitiveDesc());
143+
144+
if (biases_ && biases_->getW()) {
145+
prepareBias(bias, biases_->getW(), out, vals_);
146+
} else {
147+
bias = nullptr;
148+
}
113149
}
114150

115151
void MKLDNNAddtoLayer::resetFwdPD(std::shared_ptr<sum::primitive_desc>& pd,
152+
std::shared_ptr<sum::primitive_desc>& biasPD,
116153
std::vector<MKLDNNMatrixPtr>& inputs,
154+
MKLDNNMatrixPtr bias,
117155
MKLDNNMatrixPtr out) {
118156
std::vector<double> scales(inputs.size(), 1.0);
119157
std::vector<memory::primitive_desc> srcPDs;
@@ -123,22 +161,47 @@ void MKLDNNAddtoLayer::resetFwdPD(std::shared_ptr<sum::primitive_desc>& pd,
123161
CHECK(out);
124162
pd.reset(new sum::primitive_desc(out->getMemoryDesc(), scales, srcPDs));
125163
CHECK_PRIMITIVE_DESC_EQ(out, pd->dst_primitive_desc());
164+
165+
biasPD = nullptr;
166+
if (bias) {
167+
std::vector<double> scales(2, 1.0);
168+
std::vector<memory::primitive_desc> srcPDs(2, bias->getPrimitiveDesc());
169+
biasPD.reset(
170+
new sum::primitive_desc(bias->getMemoryDesc(), scales, srcPDs));
171+
CHECK_PRIMITIVE_DESC_EQ(bias, biasPD->dst_primitive_desc());
172+
}
126173
}
127174

128175
void MKLDNNAddtoLayer::resetFwdPipeline(
129176
std::vector<primitive>& pipeline,
130177
std::shared_ptr<sum::primitive_desc>& pd,
178+
std::shared_ptr<sum::primitive_desc>& biasPD,
131179
std::vector<MKLDNNMatrixPtr>& inputs,
180+
MKLDNNMatrixPtr& bias,
132181
MKLDNNMatrixPtr& out) {
133182
std::vector<primitive::at> srcs;
134183
for (size_t i = 0; i < inputs.size(); i++) {
135184
srcs.push_back(*(inputs[i]));
136185
}
137186
fwd_.reset(new sum(*pd, srcs, *out));
138187
pipeline.push_back(*fwd_);
188+
189+
fwdBias_.clear();
190+
if (biasPD == nullptr || bias == nullptr) {
191+
return;
192+
}
193+
fwdBias_.resize(vals_.size());
194+
for (size_t i = 0; i < vals_.size(); ++i) {
195+
std::vector<primitive::at> srcs;
196+
srcs.push_back(*(vals_[i]));
197+
srcs.push_back(*bias);
198+
fwdBias_[i].reset(new sum(*biasPD, srcs, *vals_[i]));
199+
pipeline.push_back(*fwdBias_[i]);
200+
}
139201
}
140202

141203
void MKLDNNAddtoLayer::resetBwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
204+
MKLDNNMatrixPtr& bias,
142205
MKLDNNMatrixPtr& out) {
143206
CHECK(outVal_);
144207
resetOutGrad(out, outVal_->getPrimitiveDesc());
@@ -149,6 +212,12 @@ void MKLDNNAddtoLayer::resetBwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
149212
resetInGrad(inputs[i], inVal_->getPrimitiveDesc(), i);
150213
CHECK_PRIMITIVE_DESC_EQ(inputs[i], out->getPrimitiveDesc());
151214
}
215+
216+
if (biases_ && biases_->getWGrad()) {
217+
prepareBias(bias, biases_->getWGrad(), out, grads_);
218+
} else {
219+
bias = nullptr;
220+
}
152221
}
153222

154223
} // namespace paddle

paddle/gserver/layers/MKLDNNAddtoLayer.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@ class MKLDNNAddtoLayer : public MKLDNNLayer {
3232
// layer size == ic * ih * iw == oc * oh *ow, and can not be changed
3333
size_t layerSize_;
3434

35-
// TODO(TJ): this part has not been optimized by MKL-DNN
3635
std::unique_ptr<Weight> biases_;
3736

37+
// buffers for adding bias
38+
std::vector<MKLDNNMatrixPtr> vals_;
39+
std::vector<MKLDNNMatrixPtr> grads_;
40+
// primitives for adding bias
41+
std::vector<std::shared_ptr<mkldnn::primitive>> fwdBias_;
42+
std::shared_ptr<mkldnn::primitive> bwdBias_;
43+
3844
public:
3945
explicit MKLDNNAddtoLayer(const LayerConfig& config) : MKLDNNLayer(config) {}
4046

@@ -91,20 +97,34 @@ class MKLDNNAddtoLayer : public MKLDNNLayer {
9197
* reset pipeline.
9298
*/
9399
void resetFwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
100+
MKLDNNMatrixPtr& bias,
94101
MKLDNNMatrixPtr& out);
95102
void resetFwdPD(std::shared_ptr<mkldnn::sum::primitive_desc>& pd,
103+
std::shared_ptr<mkldnn::sum::primitive_desc>& biasPD,
96104
std::vector<MKLDNNMatrixPtr>& inputs,
105+
MKLDNNMatrixPtr bias,
97106
MKLDNNMatrixPtr out);
98107
void resetFwdPipeline(std::vector<mkldnn::primitive>& pipeline,
99108
std::shared_ptr<mkldnn::sum::primitive_desc>& pd,
109+
std::shared_ptr<mkldnn::sum::primitive_desc>& biasPD,
100110
std::vector<MKLDNNMatrixPtr>& inputs,
111+
MKLDNNMatrixPtr& bias,
101112
MKLDNNMatrixPtr& out);
102113

103114
/**
104115
* Backward functions: reset buffers(inputs, output, bias)
105116
*/
106117
void resetBwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
118+
MKLDNNMatrixPtr& bias,
107119
MKLDNNMatrixPtr& out);
120+
121+
/**
122+
* prepare for bias
123+
*/
124+
void prepareBias(MKLDNNMatrixPtr& bias,
125+
const MatrixPtr& biasMat,
126+
const MKLDNNMatrixPtr& out,
127+
std::vector<MKLDNNMatrixPtr>& outs);
108128
};
109129

110130
} // namespace paddle

paddle/gserver/tests/test_MKLDNN.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,8 @@ void testAddtoLayer(const testImageDesc& pm, const size_t nInputs) {
300300
TestConfig dnnConfig;
301301
getAddtoConfig(dnnConfig, pm, nInputs);
302302
dnnConfig.layerConfig.set_type("mkldnn_addto");
303-
// TODO(TJ): test with bias
304-
for (auto withBias : {false}) {
305-
if (withBias) {
306-
dnnConfig.biasSize = pm.ic * pm.ih * pm.iw;
307-
} else {
308-
dnnConfig.biasSize = 0;
309-
}
303+
for (auto withBias : {false, true}) {
304+
dnnConfig.biasSize = withBias ? pm.ic * pm.ih * pm.iw : 0;
310305
RUN_MKLDNN_TEST_LAYER(dnnConfig, "addto", pm)
311306
}
312307
}

0 commit comments

Comments
 (0)