@@ -116,8 +116,6 @@ void MKLDNNConvLayer::resetFwd(std::vector<primitive>& pipeline,
116
116
resetFwdBuffers (fwdPD_, in, wgt, bias, out);
117
117
118
118
resetFwdPipeline (pipeline, fwdPD_, in, wgt, bias, out);
119
-
120
- printValueFormatFlow ();
121
119
}
122
120
123
121
void MKLDNNConvLayer::resetBwd (std::vector<primitive>& pipeline,
@@ -135,12 +133,6 @@ void MKLDNNConvLayer::resetBwd(std::vector<primitive>& pipeline,
135
133
resetBwdBuffers (bwdWgtPD, bwdDataPD, in, wgt, bias, out);
136
134
137
135
resetBwdPipeline (pipeline, bwdWgtPD, bwdDataPD, in, wgt, bias, out);
138
-
139
- printGradFormatFlow ();
140
- }
141
-
142
- void MKLDNNConvLayer::updateInputData () {
143
- cpuInVal_->setData (getInputValue (0 , CPU_DEVICE)->getData ());
144
136
}
145
137
146
138
void MKLDNNConvLayer::updateWeights (const UpdateCallback& callback) {
@@ -211,11 +203,18 @@ void MKLDNNConvLayer::resetFwdBuffers(
211
203
MKLDNNMatrixPtr& bias,
212
204
MKLDNNMatrixPtr& out) {
213
205
CHECK (pd);
214
- resetInValue (pd, in);
206
+ resetInValue (
207
+ in, std::make_shared<memory::primitive_desc>(pd->src_primitive_desc ()));
208
+
209
+ resetOutValue (out, pd->dst_primitive_desc ());
215
210
216
- resetWgtBiasValue (pd, wgt, bias );
211
+ resetWithMatrix (wgt, weight_-> getW (), pd-> weights_primitive_desc () );
217
212
218
- resetOutValue (pd, out);
213
+ if (biases_ && biases_->getW ()) {
214
+ resetWithMatrix (bias, biases_->getW (), pd->bias_primitive_desc ());
215
+ } else {
216
+ bias = nullptr ;
217
+ }
219
218
}
220
219
221
220
void MKLDNNConvLayer::resetFwdPipeline (
@@ -225,104 +224,12 @@ void MKLDNNConvLayer::resetFwdPipeline(
225
224
MKLDNNMatrixPtr& wgt,
226
225
MKLDNNMatrixPtr& bias,
227
226
MKLDNNMatrixPtr& out) {
228
- if (cvtInVal_) {
229
- pipeline.push_back (*cvtInVal_);
230
- }
231
-
232
227
if (bias) {
233
228
fwd_.reset (new conv_fwd (*pd, *in, *wgt, *bias, *out));
234
229
} else {
235
230
fwd_.reset (new conv_fwd (*pd, *in, *wgt, *out));
236
231
}
237
232
pipeline.push_back (*fwd_);
238
-
239
- if (cvtOutVal_) {
240
- pipeline.push_back (*cvtOutVal_);
241
- }
242
- }
243
-
244
- void MKLDNNConvLayer::resetInValue (
245
- std::shared_ptr<conv_fwd::primitive_desc>& pd, MKLDNNMatrixPtr& in) {
246
- const MatrixPtr& inMat = inputLayers_[0 ]->getOutputValue ();
247
- in = MKLDNNMatrix::create (inMat, pd->src_primitive_desc ());
248
-
249
- // create buffer and reorder if input value do not match
250
- cpuInVal_ = nullptr ;
251
- cvtInVal_ = nullptr ;
252
-
253
- MKLDNNMatrixPtr dnnIn = std::dynamic_pointer_cast<MKLDNNMatrix>(inMat);
254
- CHECK_EQ (inputIsOnlyMKLDNN (), dnnIn != nullptr );
255
- if (dnnIn != nullptr && dnnIn->getPrimitiveDesc () == in->getPrimitiveDesc ()) {
256
- in = dnnIn;
257
- return ;
258
- }
259
- if (dnnIn) {
260
- if (dnnIn->getFormat () == format::nc) {
261
- CHECK (ih_ == 1 && iw_ == 1 ) << " when input is nc format" ;
262
- // create a new one with nchw format and same data
263
- memory::dims inDims = memory::dims{bs_, ic_, 1 , 1 };
264
- dnnIn = MKLDNNMatrix::create (inMat, inDims, format::nchw, engine_);
265
- }
266
- if (dnnIn->getPrimitiveDesc () == in->getPrimitiveDesc ()) {
267
- in = dnnIn;
268
- return ;
269
- }
270
- cpuInVal_ = dnnIn;
271
- in = MKLDNNMatrix::create (nullptr , pd->src_primitive_desc ());
272
- cvtInVal_ = MKLDNNMatrix::createReorder (cpuInVal_, in);
273
- CHECK (cvtInVal_) << " should not be emptry" ;
274
- } else {
275
- memory::dims inDims = memory::dims{bs_, ic_, ih_, iw_};
276
- cpuInVal_ = MKLDNNMatrix::create (inMat, inDims, format::nchw, engine_);
277
- if (cpuInVal_->getPrimitiveDesc () != in->getPrimitiveDesc ()) {
278
- // create new mkldnn matrix
279
- in = MKLDNNMatrix::create (nullptr , pd->src_primitive_desc ());
280
- cvtInVal_ = MKLDNNMatrix::createReorder (cpuInVal_, in);
281
- CHECK (cvtInVal_) << " should not be emptry" ;
282
- } else {
283
- in = cpuInVal_;
284
- }
285
- }
286
- }
287
-
288
- void MKLDNNConvLayer::resetWgtBiasValue (
289
- std::shared_ptr<conv_fwd::primitive_desc>& pd,
290
- MKLDNNMatrixPtr& wgt,
291
- MKLDNNMatrixPtr& bias) {
292
- wgt = MKLDNNMatrix::create (weight_->getW (), pd->weights_primitive_desc ());
293
- VLOG (MKLDNN_FMTS) << " Weight value format: " << wgt->getFormat ();
294
-
295
- bias = (biases_ && biases_->getW ())
296
- ? MKLDNNMatrix::create (biases_->getW (), pd->bias_primitive_desc ())
297
- : nullptr ;
298
- }
299
-
300
- void MKLDNNConvLayer::resetOutValue (
301
- std::shared_ptr<conv_fwd::primitive_desc>& pd, MKLDNNMatrixPtr& out) {
302
- out = MKLDNNMatrix::create (output_.value , pd->dst_primitive_desc ());
303
-
304
- // create reorder if output value has cpu device and pd do not match
305
- cpuOutVal_ = nullptr ;
306
- cvtOutVal_ = nullptr ;
307
- if (!outputIsOnlyMKLDNN ()) {
308
- const MatrixPtr& cpuOut = getOutput (CPU_DEVICE).value ;
309
- memory::dims outDims = memory::dims{bs_, oc_, oh_, ow_};
310
- cpuOutVal_ = MKLDNNMatrix::create (cpuOut, outDims, format::nchw, engine_);
311
- if (cpuOutVal_->getPrimitiveDesc () != pd->dst_primitive_desc ()) {
312
- out = MKLDNNMatrix::create (nullptr , pd->dst_primitive_desc ());
313
- cvtOutVal_ = MKLDNNMatrix::createReorder (out, cpuOutVal_);
314
- CHECK (cvtOutVal_) << " should not be empty" ;
315
- } else {
316
- cpuOut->setData (output_.value ->getData ());
317
- cpuOutVal_ = out;
318
- }
319
- // when output is cpu device, change the mkldnn output value and make them
320
- // share the same data. Then if next layer use inputlayer->getOuputValue()
321
- // to achieve the input value, it will get the right data.
322
- output_.value = std::dynamic_pointer_cast<Matrix>(cpuOutVal_);
323
- return ;
324
- }
325
- output_.value = std::dynamic_pointer_cast<Matrix>(out);
326
233
}
327
234
328
235
void MKLDNNConvLayer::resetBwdWgtPD (
@@ -331,8 +238,8 @@ void MKLDNNConvLayer::resetBwdWgtPD(
331
238
loadConvSettings (wgtDims, biasDims, strides, dilations, padL, padR);
332
239
333
240
// create backward weight using input, output and weight value memory desc
334
- CHECK (inVal_) << " Should have input value" ;
335
- CHECK (outVal_) << " Should have output value" ;
241
+ CHECK (inVal_) << " Should have internal input value" ;
242
+ CHECK (outVal_) << " Should have internal output value" ;
336
243
CHECK (wgtVal_) << " Should have weight value" ;
337
244
algorithm algo = algorithm::convolution_direct;
338
245
padding_kind padKind = padding_kind::zero;
@@ -372,8 +279,8 @@ void MKLDNNConvLayer::resetBwdDataPD(
372
279
373
280
memory::dims wgtDims, biasDims, strides, dilations, padL, padR;
374
281
loadConvSettings (wgtDims, biasDims, strides, dilations, padL, padR);
375
- CHECK (inVal_) << " Should have input value" ;
376
- CHECK (outVal_) << " Should have output value" ;
282
+ CHECK (inVal_) << " Should have internal input value" ;
283
+ CHECK (outVal_) << " Should have internal output value" ;
377
284
// create backward data using input and output value memory desc
378
285
// but using weight memory desc with any format
379
286
auto bwdDataDesc = conv_bwdData::desc (algorithm::convolution_direct,
@@ -399,12 +306,27 @@ void MKLDNNConvLayer::resetBwdBuffers(
399
306
MKLDNNMatrixPtr& bias,
400
307
MKLDNNMatrixPtr& out) {
401
308
CHECK (wgtPD);
402
- resetOutGrad (wgtPD, out );
309
+ resetOutGrad (out, wgtPD-> diff_dst_primitive_desc () );
403
310
404
- resetWgtBiasGrad (wgtPD, wgt, bias);
311
+ resetWithMatrix (
312
+ wgt, weight_->getWGrad (), wgtPD->diff_weights_primitive_desc ());
313
+ CHECK (wgtVal_ != nullptr &&
314
+ wgt->getPrimitiveDesc () == wgtVal_->getPrimitiveDesc ())
315
+ << " primitive desc of weight grad and value should be equal" ;
405
316
406
- resetInGrad (dataPD, in);
317
+ bias = nullptr ;
318
+ if (biases_ && biases_->getWGrad ()) {
319
+ resetWithMatrix (
320
+ bias, biases_->getWGrad (), wgtPD->diff_bias_primitive_desc ());
321
+ CHECK (bias && biasVal_ &&
322
+ bias->getPrimitiveDesc () == biasVal_->getPrimitiveDesc ())
323
+ << " primitive desc of bias grad should equal the bias value" ;
324
+ }
407
325
326
+ if (dataPD == nullptr ) {
327
+ return ;
328
+ }
329
+ resetInGrad (in, dataPD->diff_src_primitive_desc ());
408
330
resetWgtValBwdData (dataPD, wgtValBwdData_);
409
331
}
410
332
@@ -416,10 +338,7 @@ void MKLDNNConvLayer::resetBwdPipeline(
416
338
MKLDNNMatrixPtr& wgt,
417
339
MKLDNNMatrixPtr& bias,
418
340
MKLDNNMatrixPtr& out) {
419
- if (cvtOutGrad_) {
420
- pipeline.push_back (*cvtOutGrad_);
421
- }
422
-
341
+ CHECK (inVal_);
423
342
// add bwdWgt handle
424
343
if (bias) {
425
344
bwdWgt_.reset (new conv_bwdWgt (*wgtPD, *inVal_, *out, *wgt, *bias));
@@ -431,99 +350,13 @@ void MKLDNNConvLayer::resetBwdPipeline(
431
350
if (dataPD == nullptr ) {
432
351
return ;
433
352
}
434
-
435
353
if (cvtWgtVal_) {
436
354
pipeline.push_back (*cvtWgtVal_);
437
355
}
438
-
439
356
// add bwdData handle
440
357
CHECK (wgtValBwdData_) << " Should have weight memory" ;
441
358
bwdData_.reset (new conv_bwdData (*dataPD, *out, *wgtValBwdData_, *in));
442
359
pipeline.push_back (*bwdData_);
443
-
444
- if (cvtInGrad_) {
445
- pipeline.push_back (*cvtInGrad_);
446
- }
447
- }
448
-
449
- void MKLDNNConvLayer::resetOutGrad (
450
- std::shared_ptr<conv_bwdWgt::primitive_desc>& wgtPD, MKLDNNMatrixPtr& out) {
451
- cpuOutGrad_ = nullptr ;
452
- cvtOutGrad_ = nullptr ;
453
- CHECK (outVal_ != nullptr &&
454
- outVal_->getPrimitiveDesc () == wgtPD->diff_dst_primitive_desc ())
455
- << " primitive desc of out grad and value should be equal" ;
456
- if (outputIsOnlyMKLDNN ()) {
457
- MKLDNNLayer::resetOutGrad (out, outVal_->getPrimitiveDesc ());
458
- } else {
459
- const MatrixPtr& cpuOut = getOutput (CPU_DEVICE).grad ;
460
- // always share the same grad data of CPU output
461
- // then the activation can get the right grad from output_.grad
462
- output_.grad ->setData (cpuOut->getData ());
463
- // same PrimitiveDesc with cpuInVal_
464
- CHECK (cpuOutVal_);
465
- cpuOutGrad_ = MKLDNNMatrix::create (cpuOut, cpuOutVal_->getPrimitiveDesc ());
466
- // create reorder if primitive desc does not match
467
- if (cpuOutGrad_->getPrimitiveDesc () != outVal_->getPrimitiveDesc ()) {
468
- out = MKLDNNMatrix::create (nullptr , outVal_->getPrimitiveDesc ());
469
- cvtOutGrad_ = MKLDNNMatrix::createReorder (cpuOutGrad_, out);
470
- CHECK (cvtOutGrad_);
471
- } else {
472
- out = cpuOutGrad_;
473
- }
474
- }
475
- }
476
-
477
- void MKLDNNConvLayer::resetWgtBiasGrad (
478
- std::shared_ptr<conv_bwdWgt::primitive_desc>& wgtPD,
479
- MKLDNNMatrixPtr& wgt,
480
- MKLDNNMatrixPtr& bias) {
481
- wgt = MKLDNNMatrix::create (weight_->getWGrad (),
482
- wgtPD->diff_weights_primitive_desc ());
483
- CHECK (nullptr != wgtVal_ &&
484
- wgt->getPrimitiveDesc () == wgtVal_->getPrimitiveDesc ())
485
- << " primitive desc of weight grad and value should be equal" ;
486
- VLOG (MKLDNN_FMTS) << " weight grad format: " << wgt->getFormat ();
487
-
488
- bias = nullptr ;
489
- if (biasVal_ == nullptr ) {
490
- return ;
491
- }
492
- bias = MKLDNNMatrix::create (biases_->getWGrad (),
493
- wgtPD->diff_bias_primitive_desc ());
494
- CHECK (bias->getPrimitiveDesc () == biasVal_->getPrimitiveDesc ())
495
- << " primitive desc of bias grad should equal the bias value" ;
496
- }
497
-
498
- void MKLDNNConvLayer::resetInGrad (
499
- std::shared_ptr<conv_bwdData::primitive_desc>& dataPD,
500
- MKLDNNMatrixPtr& in) {
501
- in = nullptr ;
502
- cpuInGrad_ = nullptr ;
503
- cvtInGrad_ = nullptr ;
504
- if (dataPD == nullptr ) {
505
- return ;
506
- }
507
-
508
- if (inputIsOnlyMKLDNN ()) {
509
- MKLDNNLayer::resetInGrad (in, dataPD->diff_src_primitive_desc ());
510
- CHECK (nullptr != inVal_ &&
511
- in->getPrimitiveDesc () == inVal_->getPrimitiveDesc ())
512
- << " primitive desc of input grad and value should be equal" ;
513
- } else {
514
- const MatrixPtr& cpuIn = getInputGrad (0 , CPU_DEVICE);
515
- // same PrimitiveDesc with cpuInVal_
516
- CHECK (cpuInVal_);
517
- cpuInGrad_ = MKLDNNMatrix::create (cpuIn, cpuInVal_->getPrimitiveDesc ());
518
- in = cpuInGrad_;
519
- // create reorder if PrimitiveDesc does not match
520
- if (cpuInGrad_->getPrimitiveDesc () != dataPD->diff_src_primitive_desc ()) {
521
- in = MKLDNNMatrix::create (getInputGrad (0 , MKLDNN_DEVICE),
522
- dataPD->diff_src_primitive_desc ());
523
- cvtInGrad_ = MKLDNNMatrix::createReorder (in, cpuInGrad_);
524
- CHECK (cvtInGrad_);
525
- }
526
- }
527
360
}
528
361
529
362
void MKLDNNConvLayer::resetWgtValBwdData (
@@ -537,8 +370,7 @@ void MKLDNNConvLayer::resetWgtValBwdData(
537
370
// since the primitive_desc would be different with wgtVal_
538
371
CHECK (wgtVal_) << " should have weight value" ;
539
372
if (dataPD->weights_primitive_desc () != wgtVal_->getPrimitiveDesc ()) {
540
- wgtValBwdData_ =
541
- MKLDNNMatrix::create (nullptr , dataPD->weights_primitive_desc ());
373
+ wgtValBwdData_ = MKLDNNMatrix::create (dataPD->weights_primitive_desc ());
542
374
cvtWgtVal_ = MKLDNNMatrix::createReorder (wgtVal_, wgtValBwdData_);
543
375
CHECK (cvtWgtVal_);
544
376
} else {
0 commit comments