revert-3824-remove_grad_op_type
tensor-tang 8 years ago
parent bfbd066fdd
commit c5183caa04

@ -134,7 +134,7 @@ void MKLDNNFcLayer::resetFwd() {
const MatrixPtr& bias = hasBias ? biases_->getW() : nullptr;
const MatrixPtr& out = output_.value;
if (prevIsOnlyMKLDNN()) {
if (inputIsOnlyMKLDNN()) {
const MatrixPtr& in = getInputValue(0);
inVal_ = std::dynamic_pointer_cast<MKLDNNMatrix>(in);
CHECK(inVal_) << "Input should be MKLDNNMatrix";
@ -154,7 +154,7 @@ void MKLDNNFcLayer::resetFwd() {
// change original output value to mkldnn output value
output_.value = std::dynamic_pointer_cast<Matrix>(outVal_);
if (!nextIsOnlyMKLDNN()) {
if (!outputIsOnlyMKLDNN()) {
convertOutputToOtherDevice();
}
@ -194,19 +194,16 @@ void MKLDNNFcLayer::resetBwd() {
const MatrixPtr& bias = hasBias ? biases_->getWGrad() : nullptr;
// TODO(TJ): merge outgrad
if (nextIsOnlyMKLDNN()) {
// can not directly cast outputgrad to mkldnnmatrix,
// since each layer can not write the inputgrad to mkldnn inputgrad.
// So just create from matrix with outputvalue format.
const MatrixPtr& out = getOutput(MKLDNN_DEVICE).grad;
outGrad_ = MKLDNNMatrix::create(out, outVal_->getPrimitiveDesc());
} else {
const MatrixPtr& out = getOutput(CPU_DEVICE).grad;
// fc do not need to convert from cpu device since output always nc
// only need create from cpu device
outGrad_ = MKLDNNMatrix::create(out, outVal_->getPrimitiveDesc());
}
int device = outputIsOnlyMKLDNN() ? MKLDNN_DEVICE : CPU_DEVICE;
// for MKLDNN device:
// can not directly cast outputgrad to mkldnnmatrix,
// since each layer can not write the inputgrad to mkldnn inputgrad.
// So just create from matrix with outputvalue format.
// for CPU device:
// fc do not need to convert from cpu device since output is always nc format
// only need create from cpu device
const MatrixPtr& out = getOutput(device).grad;
outGrad_ = MKLDNNMatrix::create(out, outVal_->getPrimitiveDesc());
wgtGrad_ = MKLDNNMatrix::create(wgt, wgtVal_->getPrimitiveDesc());
biasGrad_ = hasBias ? MKLDNNMatrix::create(bias, biasVal_->getPrimitiveDesc())
: nullptr;
@ -238,7 +235,7 @@ void MKLDNNFcLayer::resetBwd() {
pipelineBwd_.push_back(*bwdWgt_);
/// backward data
int device = prevIsOnlyMKLDNN() ? MKLDNN_DEVICE : CPU_DEVICE;
device = inputIsOnlyMKLDNN() ? MKLDNN_DEVICE : CPU_DEVICE;
const MatrixPtr& in = getInputGrad(0, device);
if (in == nullptr) {
return;

@ -151,6 +151,8 @@ public:
protected:
/**
* copy image size and sequence info to other device
* @note: can not directly use Layer::copyOutputToOtherDevice since here only
* copy base info and do not copy data value
*/
void copyOutputInfoToOtherDevice() {
for (size_t i = 0; i < outputOtherDevice_.size(); i++) {
@ -165,10 +167,10 @@ protected:
}
/**
* Is previous layer only has MKLDNN type.
* If input only has MKLDNN device.
* Otherwise, only support the previous layer using CPU device.
*/
bool prevIsOnlyMKLDNN(int index = 0) {
bool inputIsOnlyMKLDNN(int index = 0) {
int prevDevice = getPrev(index)->getDeviceId();
if (prevDevice == MKLDNN_DEVICE) {
return true;
@ -183,7 +185,7 @@ protected:
* If output only has MKLDNN device.
* Otherwise, other devices should only using CPU device.
*/
bool nextIsOnlyMKLDNN() {
bool outputIsOnlyMKLDNN() {
for (size_t i = 0; i < outputOtherDevice_.size(); i++) {
CHECK_EQ(outputOtherDevice_[i].deviceId, CPU_DEVICE)
<< "Only support other device is CPU yet";
@ -195,7 +197,7 @@ protected:
* Sync input value data
*/
void syncInputValue() {
if (prevIsOnlyMKLDNN()) {
if (inputIsOnlyMKLDNN()) {
return;
}
real* iData = getInputValue(0, CPU_DEVICE)->getData();
@ -208,7 +210,7 @@ protected:
* Sync output grad data
*/
void syncOutputGrad() {
if (nextIsOnlyMKLDNN()) {
if (outputIsOnlyMKLDNN()) {
return;
}

Loading…
Cancel
Save