提交 c5183caa 编写于 作者: T tensor-tang

rename

上级 bfbd066f
...@@ -134,7 +134,7 @@ void MKLDNNFcLayer::resetFwd() { ...@@ -134,7 +134,7 @@ void MKLDNNFcLayer::resetFwd() {
const MatrixPtr& bias = hasBias ? biases_->getW() : nullptr; const MatrixPtr& bias = hasBias ? biases_->getW() : nullptr;
const MatrixPtr& out = output_.value; const MatrixPtr& out = output_.value;
if (prevIsOnlyMKLDNN()) { if (inputIsOnlyMKLDNN()) {
const MatrixPtr& in = getInputValue(0); const MatrixPtr& in = getInputValue(0);
inVal_ = std::dynamic_pointer_cast<MKLDNNMatrix>(in); inVal_ = std::dynamic_pointer_cast<MKLDNNMatrix>(in);
CHECK(inVal_) << "Input should be MKLDNNMatrix"; CHECK(inVal_) << "Input should be MKLDNNMatrix";
...@@ -154,7 +154,7 @@ void MKLDNNFcLayer::resetFwd() { ...@@ -154,7 +154,7 @@ void MKLDNNFcLayer::resetFwd() {
// change original output value to mkldnn output value // change original output value to mkldnn output value
output_.value = std::dynamic_pointer_cast<Matrix>(outVal_); output_.value = std::dynamic_pointer_cast<Matrix>(outVal_);
if (!nextIsOnlyMKLDNN()) { if (!outputIsOnlyMKLDNN()) {
convertOutputToOtherDevice(); convertOutputToOtherDevice();
} }
...@@ -194,19 +194,16 @@ void MKLDNNFcLayer::resetBwd() { ...@@ -194,19 +194,16 @@ void MKLDNNFcLayer::resetBwd() {
const MatrixPtr& bias = hasBias ? biases_->getWGrad() : nullptr; const MatrixPtr& bias = hasBias ? biases_->getWGrad() : nullptr;
// TODO(TJ): merge outgrad // TODO(TJ): merge outgrad
if (nextIsOnlyMKLDNN()) { int device = outputIsOnlyMKLDNN() ? MKLDNN_DEVICE : CPU_DEVICE;
// can not directly cast outputgrad to mkldnnmatrix, // for MKLDNN device:
// since each layer can not write the inputgrad to mkldnn inputgrad. // can not directly cast outputgrad to mkldnnmatrix,
// So just create from matrix with outputvalue format. // since each layer can not write the inputgrad to mkldnn inputgrad.
const MatrixPtr& out = getOutput(MKLDNN_DEVICE).grad; // So just create from matrix with outputvalue format.
outGrad_ = MKLDNNMatrix::create(out, outVal_->getPrimitiveDesc()); // for CPU device:
} else { // fc do not need to convert from cpu device since output is always nc format
const MatrixPtr& out = getOutput(CPU_DEVICE).grad; // only need create from cpu device
// fc do not need to convert from cpu device since output always nc const MatrixPtr& out = getOutput(device).grad;
// only need create from cpu device outGrad_ = MKLDNNMatrix::create(out, outVal_->getPrimitiveDesc());
outGrad_ = MKLDNNMatrix::create(out, outVal_->getPrimitiveDesc());
}
wgtGrad_ = MKLDNNMatrix::create(wgt, wgtVal_->getPrimitiveDesc()); wgtGrad_ = MKLDNNMatrix::create(wgt, wgtVal_->getPrimitiveDesc());
biasGrad_ = hasBias ? MKLDNNMatrix::create(bias, biasVal_->getPrimitiveDesc()) biasGrad_ = hasBias ? MKLDNNMatrix::create(bias, biasVal_->getPrimitiveDesc())
: nullptr; : nullptr;
...@@ -238,7 +235,7 @@ void MKLDNNFcLayer::resetBwd() { ...@@ -238,7 +235,7 @@ void MKLDNNFcLayer::resetBwd() {
pipelineBwd_.push_back(*bwdWgt_); pipelineBwd_.push_back(*bwdWgt_);
/// backward data /// backward data
int device = prevIsOnlyMKLDNN() ? MKLDNN_DEVICE : CPU_DEVICE; device = inputIsOnlyMKLDNN() ? MKLDNN_DEVICE : CPU_DEVICE;
const MatrixPtr& in = getInputGrad(0, device); const MatrixPtr& in = getInputGrad(0, device);
if (in == nullptr) { if (in == nullptr) {
return; return;
......
...@@ -151,6 +151,8 @@ public: ...@@ -151,6 +151,8 @@ public:
protected: protected:
/** /**
* copy image size and sequence info to other device * 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() { void copyOutputInfoToOtherDevice() {
for (size_t i = 0; i < outputOtherDevice_.size(); i++) { for (size_t i = 0; i < outputOtherDevice_.size(); i++) {
...@@ -165,10 +167,10 @@ protected: ...@@ -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. * Otherwise, only support the previous layer using CPU device.
*/ */
bool prevIsOnlyMKLDNN(int index = 0) { bool inputIsOnlyMKLDNN(int index = 0) {
int prevDevice = getPrev(index)->getDeviceId(); int prevDevice = getPrev(index)->getDeviceId();
if (prevDevice == MKLDNN_DEVICE) { if (prevDevice == MKLDNN_DEVICE) {
return true; return true;
...@@ -183,7 +185,7 @@ protected: ...@@ -183,7 +185,7 @@ protected:
* If output only has MKLDNN device. * If output only has MKLDNN device.
* Otherwise, other devices should only using CPU device. * Otherwise, other devices should only using CPU device.
*/ */
bool nextIsOnlyMKLDNN() { bool outputIsOnlyMKLDNN() {
for (size_t i = 0; i < outputOtherDevice_.size(); i++) { for (size_t i = 0; i < outputOtherDevice_.size(); i++) {
CHECK_EQ(outputOtherDevice_[i].deviceId, CPU_DEVICE) CHECK_EQ(outputOtherDevice_[i].deviceId, CPU_DEVICE)
<< "Only support other device is CPU yet"; << "Only support other device is CPU yet";
...@@ -195,7 +197,7 @@ protected: ...@@ -195,7 +197,7 @@ protected:
* Sync input value data * Sync input value data
*/ */
void syncInputValue() { void syncInputValue() {
if (prevIsOnlyMKLDNN()) { if (inputIsOnlyMKLDNN()) {
return; return;
} }
real* iData = getInputValue(0, CPU_DEVICE)->getData(); real* iData = getInputValue(0, CPU_DEVICE)->getData();
...@@ -208,7 +210,7 @@ protected: ...@@ -208,7 +210,7 @@ protected:
* Sync output grad data * Sync output grad data
*/ */
void syncOutputGrad() { void syncOutputGrad() {
if (nextIsOnlyMKLDNN()) { if (outputIsOnlyMKLDNN()) {
return; return;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册