|
|
@ -53,28 +53,19 @@ void MKLPackedRecurrentLayer::forwardBatch(int batchSize,
|
|
|
|
REGISTER_TIMER_INFO("RecurrentFwBatch", getName().c_str());
|
|
|
|
REGISTER_TIMER_INFO("RecurrentFwBatch", getName().c_str());
|
|
|
|
/* forward one batch */
|
|
|
|
/* forward one batch */
|
|
|
|
for (size_t n = 0; n < batchValue_->getNumBatch(); n++) {
|
|
|
|
for (size_t n = 0; n < batchValue_->getNumBatch(); n++) {
|
|
|
|
MatrixPtr batch2 = batchValue_->getBatchValue(n);
|
|
|
|
MatrixPtr batchValue = batchValue_->getBatchValue(n);
|
|
|
|
|
|
|
|
|
|
|
|
if (n != 0) {
|
|
|
|
if (n != 0) {
|
|
|
|
MatrixPtr batch1 =
|
|
|
|
MatrixPtr preBatchValue =
|
|
|
|
batchValue_->getBatchValue(n - 1, batch2->getHeight());
|
|
|
|
batchValue_->getBatchValue(n - 1, batchValue->getHeight());
|
|
|
|
|
|
|
|
|
|
|
|
// batch2->mul(*batch1, *weight_->getW(), 1, 1);
|
|
|
|
packed_weight_->compute(batchValue, preBatchValue);
|
|
|
|
packed_weight_->compute(batch2, batch1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma omp parallel for collapse(2)
|
|
|
|
|
|
|
|
for (size_t i = 0; i < batch2->getHeight(); i++) {
|
|
|
|
|
|
|
|
for (size_t j = 0; j < batch2->getWidth(); j++) {
|
|
|
|
|
|
|
|
*(batch2->getData() + i * batch2->getWidth() + j) =
|
|
|
|
|
|
|
|
*(batch2->getData() + i * batch2->getWidth() + j) > 0
|
|
|
|
|
|
|
|
? *(batch2->getData() + i * batch2->getWidth() + j)
|
|
|
|
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Argument arg;
|
|
|
|
|
|
|
|
arg.value = batchValue;
|
|
|
|
|
|
|
|
activation_->forward(arg).check();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
batchValue_->copyBackSeq(*output_.value);
|
|
|
|
batchValue_->copyBackSeq(*output_.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -94,25 +85,27 @@ void MKLPackedRecurrentLayer::backwardBatch(int batchSize,
|
|
|
|
REGISTER_TIMER_INFO("RecurrentBwData", getName().c_str());
|
|
|
|
REGISTER_TIMER_INFO("RecurrentBwData", getName().c_str());
|
|
|
|
/* backward one batch */
|
|
|
|
/* backward one batch */
|
|
|
|
for (int n = (int)numBatch - 1; n >= 0; n--) {
|
|
|
|
for (int n = (int)numBatch - 1; n >= 0; n--) {
|
|
|
|
MatrixPtr batch2 = batchGrad_->getBatchValue(n);
|
|
|
|
MatrixPtr batchGrad = batchGrad_->getBatchValue(n);
|
|
|
|
MatrixPtr batch1 = batchValue_->getBatchValue(n, batch2->getHeight());
|
|
|
|
MatrixPtr batchValue =
|
|
|
|
|
|
|
|
batchValue_->getBatchValue(n, batchGrad->getHeight());
|
|
|
|
|
|
|
|
|
|
|
|
Argument arg;
|
|
|
|
Argument arg;
|
|
|
|
arg.value = batch1;
|
|
|
|
arg.value = batchValue;
|
|
|
|
arg.grad = batch2;
|
|
|
|
arg.grad = batchGrad;
|
|
|
|
activation_->backward(arg).check();
|
|
|
|
activation_->backward(arg).check();
|
|
|
|
|
|
|
|
|
|
|
|
if (n != 0) {
|
|
|
|
if (n != 0) {
|
|
|
|
batch1 = batchGrad_->getBatchValue(n - 1, batch2->getHeight());
|
|
|
|
batchValue = batchGrad_->getBatchValue(n - 1, batchGrad->getHeight());
|
|
|
|
// batch1->mul(*batch2, *weightT, 1, 1);
|
|
|
|
packed_weightT_->compute(batchValue, batchGrad);
|
|
|
|
packed_weightT_->compute(batch1, batch2);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (backwardByBatch && weight_->getWGrad()) {
|
|
|
|
if (backwardByBatch && weight_->getWGrad()) {
|
|
|
|
if (n != 0) {
|
|
|
|
if (n != 0) {
|
|
|
|
/* backward weight */
|
|
|
|
/* backward weight */
|
|
|
|
batch1 = batchValue_->getBatchValue(n - 1, batch2->getHeight());
|
|
|
|
batchValue =
|
|
|
|
weight_->getWGrad()->mul(*batch1->getTranspose(), *batch2, 1, 1);
|
|
|
|
batchValue_->getBatchValue(n - 1, batchGrad->getHeight());
|
|
|
|
|
|
|
|
weight_->getWGrad()->mul(
|
|
|
|
|
|
|
|
*batchValue->getTranspose(), *batchGrad, 1, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -124,19 +117,14 @@ void MKLPackedRecurrentLayer::backwardBatch(int batchSize,
|
|
|
|
REGISTER_TIMER_INFO("RecurrentBwWeight", getName().c_str());
|
|
|
|
REGISTER_TIMER_INFO("RecurrentBwWeight", getName().c_str());
|
|
|
|
for (size_t seq = 0; seq < numSequences; ++seq) {
|
|
|
|
for (size_t seq = 0; seq < numSequences; ++seq) {
|
|
|
|
int len = starts[seq + 1] - starts[seq];
|
|
|
|
int len = starts[seq + 1] - starts[seq];
|
|
|
|
if (!reversed_) {
|
|
|
|
weight_->getWGrad()->mul(
|
|
|
|
weight_->getWGrad()->mul(
|
|
|
|
*output_.value
|
|
|
|
*output_.value->subMatrix(starts[seq], len - 1)->getTranspose(),
|
|
|
|
->subMatrix(reversed_ ? starts[seq] + 1 : starts[seq], len - 1)
|
|
|
|
*output_.grad->subMatrix(starts[seq] + 1, len - 1),
|
|
|
|
->getTranspose(),
|
|
|
|
1,
|
|
|
|
*output_.grad->subMatrix(reversed_ ? starts[seq] : starts[seq] + 1,
|
|
|
|
1);
|
|
|
|
len - 1),
|
|
|
|
} else {
|
|
|
|
1,
|
|
|
|
weight_->getWGrad()->mul(
|
|
|
|
1);
|
|
|
|
*output_.value->subMatrix(starts[seq] + 1, len - 1)->getTranspose(),
|
|
|
|
|
|
|
|
*output_.grad->subMatrix(starts[seq], len - 1),
|
|
|
|
|
|
|
|
1,
|
|
|
|
|
|
|
|
1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|