Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matrix API refactor #934

Merged
merged 2 commits into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion paddle/gserver/evaluators/Evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ClassificationErrorEvaluator : public Evaluator {
useGpu(arguments[0].deviceId));
errorMat->zeroMem();
if (label != nullptr) {
errorMat->classificationError(output, label);
errorMat->classificationError(*output, *label);
} else if (dynamic_cast<CpuSparseMatrix*>(multiBinaryLabel.get()) ||
dynamic_cast<GpuSparseMatrix*>(multiBinaryLabel.get())) {
errorMat->classificationErrorMulti(
Expand Down
12 changes: 6 additions & 6 deletions paddle/gserver/layers/ContextProjection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ void ContextProjection::forward() {
REGISTER_TIMER_INFO("ContextProjectionForward", getName().c_str());
bool isPadding = config_.trainable_padding();
out_->value->contextProjectionForward(
in_->value,
state_ ? state_ : isPadding ? weight_->getW() : nullptr,
*(in_->value),
state_ ? state_.get() : isPadding ? weight_->getW().get() : nullptr,
*startPositions,
config_.context_length(),
config_.context_start(),
Expand Down Expand Up @@ -128,24 +128,24 @@ void ContextProjection::backward(const UpdateCallback& callback) {
bool isPadding = config_.trainable_padding();
if (!out_->grad->useGpu()) {
out_->grad->contextProjectionBackward(
in_->grad,
isPadding ? weight_->getWGrad() : nullptr,
in_->grad.get(),
isPadding ? weight_->getWGrad().get() : nullptr,
*startPositions,
config_.context_length(),
config_.context_start(),
beginPad_,
isPadding);
} else {
if (in_->grad) {
out_->grad->contextProjectionBackwardData(in_->grad,
out_->grad->contextProjectionBackwardData(*(in_->grad),
*startPositions,
config_.context_length(),
config_.context_start());
}

if (isPadding && weight_->getWGrad()) {
out_->grad->contextProjectionBackwardWeight(
weight_->getWGrad(),
*(weight_->getWGrad()),
*startPositions,
config_.context_length(),
config_.context_start(),
Expand Down
6 changes: 3 additions & 3 deletions paddle/gserver/layers/ConvexCombinationLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void ConvexCombinationLayer::forward(PassType passType) {
tmpRow0->setData(inV0->getData() + i * weightDim);
tmpRow1->setData(outV->getData() + i * dataDim);

tmpRow1->mul(tmpRow0, tmpMtx0, 1, 0);
tmpRow1->mul(*tmpRow0, *tmpMtx0, 1, 0);
}
}

Expand All @@ -136,7 +136,7 @@ void ConvexCombinationLayer::backward(const UpdateCallback& callback) {
tmpRow1->setData(outG->getData() + i * dataDim);
tmpMtx0->setData(inV1->getData() + i * weightDim * dataDim);

tmpRow0->mul(tmpRow1, tmpMtx0->getTranspose(), 1, 1);
tmpRow0->mul(*tmpRow1, *(tmpMtx0->getTranspose()), 1, 1);
}
}

Expand All @@ -146,7 +146,7 @@ void ConvexCombinationLayer::backward(const UpdateCallback& callback) {
tmpRow1->setData(outG->getData() + i * dataDim);
tmpMtx0->setData(inG1->getData() + i * weightDim * dataDim);

tmpMtx0->mul(tmpRow0->getTranspose(), tmpRow1, 1, 1);
tmpMtx0->mul(*(tmpRow0->getTranspose()), *tmpRow1, 1, 1);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions paddle/gserver/layers/ExpandConvBaseLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void ExpandConvBaseLayer::expandFwdOnce(MatrixPtr image,
Matrix::create(wgtData, subM, subK, false, useGpu_); // mark transpose
MatrixPtr B = Matrix::create(expInData, subK, subN, false, useGpu_);
MatrixPtr C = Matrix::create(outData, subM, subN, false, useGpu_);
C->mul(A, B, 1, 1);
C->mul(*A, *B, 1, 1);

A->clear();
B->clear();
Expand Down Expand Up @@ -185,7 +185,7 @@ void ExpandConvBaseLayer::bpropActs(MatrixPtr out,
MatrixPtr C = Matrix::create(expandInData, subK, subN, false, useGpu_);
MatrixPtr B = Matrix::create(localGradData, subM, subN, false, useGpu_);
MatrixPtr A = Matrix::create(wgtData, subM, subK, true, useGpu_);
C->mul(A, B); // mul
C->mul(*A, *B); // mul

// clear the temporary matrix
A->clear();
Expand Down Expand Up @@ -252,7 +252,7 @@ void ExpandConvBaseLayer::bpropWeights(MatrixPtr image,
MatrixPtr A = Matrix::create(expandInData, subK, subN, true, useGpu_);
MatrixPtr B = Matrix::create(gradData, subM, subN, false, useGpu_);
MatrixPtr C = Matrix::create(wGradData, subM, subK, false, useGpu_);
C->mul(B, A, 1, 1);
C->mul(*B, *A, 1, 1);

A->clear();
B->clear();
Expand Down
7 changes: 4 additions & 3 deletions paddle/gserver/layers/FullMatrixProjection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ FullMatrixProjection::FullMatrixProjection(const ProjectionConfig& config,

void FullMatrixProjection::forward() {
REGISTER_TIMER_INFO("FwMulTimer", getName().c_str());
out_->value->mul(in_->value, weight_->getW(), 1, 1);
out_->value->mul(*(in_->value), *(weight_->getW()), 1, 1);
}

void FullMatrixProjection::backward(const UpdateCallback& callback) {
Expand All @@ -37,7 +37,8 @@ void FullMatrixProjection::backward(const UpdateCallback& callback) {
/* Calculate the W-gradient for the current layer */
if (weight_->getWGrad()) {
REGISTER_TIMER_INFO("GradMulTimer", getName().c_str());
weight_->getWGrad()->mul(in_->value->getTranspose(), out_->grad, 1, 1);
weight_->getWGrad()->mul(
*(in_->value->getTranspose()), *(out_->grad), 1, 1);
}

// If callback does not change value, backward propagation error
Expand All @@ -47,7 +48,7 @@ void FullMatrixProjection::backward(const UpdateCallback& callback) {
/* Calculate the input layers error */
if (in_->grad) {
REGISTER_TIMER_INFO("BpMulTimer", getName().c_str());
in_->grad->mul(out_->grad, weight_->getW()->getTranspose(), 1, 1);
in_->grad->mul(*(out_->grad), *(weight_->getW()->getTranspose()), 1, 1);
}

hl_set_sync_flag(syncFlag);
Expand Down
8 changes: 4 additions & 4 deletions paddle/gserver/layers/FullyConnectedLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ void FullyConnectedLayer::forward(PassType passType) {
auto input = getInput(i);
CHECK(input.value) << "The input of 'fc' layer must be matrix";
REGISTER_TIMER_INFO("FwMulTimer", getName().c_str());
i == 0 ? outV->mul(input.value, weights_[i]->getW(), 1, 0)
: outV->mul(input.value, weights_[i]->getW(), 1, 1);
i == 0 ? outV->mul(*input.value, *weights_[i]->getW(), 1, 0)
: outV->mul(*input.value, *weights_[i]->getW(), 1, 1);
}

/* add the bias-vector */
Expand Down Expand Up @@ -123,7 +123,7 @@ void FullyConnectedLayer::backward(const UpdateCallback& callback) {
MatrixPtr oGrad = getOutputGrad();
{
REGISTER_TIMER_INFO("GradMulTimer", getName().c_str());
weights_[i]->getWGrad()->mul(input_T, oGrad, 1, 1);
weights_[i]->getWGrad()->mul(*input_T, *oGrad, 1, 1);
}
}

Expand All @@ -136,7 +136,7 @@ void FullyConnectedLayer::backward(const UpdateCallback& callback) {
if (NULL != preGrad) {
MatrixPtr weights_T = weights_[i]->getW()->getTranspose();
REGISTER_TIMER_INFO("BpMulTimer", getName().c_str());
preGrad->mul(getOutputGrad(), weights_T, 1, 1);
preGrad->mul(*getOutputGrad(), *weights_T, 1, 1);
}

hl_set_sync_flag(syncFlag);
Expand Down
2 changes: 1 addition & 1 deletion paddle/gserver/layers/LinearChainCRF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ real LinearChainCRF::forward(real* x, int* s, int length) {
matX->rowMax(*maxX_);
expX_->assign(*matX);
// subtract max to avoid overflow or underflow
expX_->mul(maxX_, ones_, (real)-1, (real)1);
expX_->mul(*maxX_, *ones_, (real)-1, (real)1);
expX_->exp2();

real* a = a_->getData();
Expand Down
26 changes: 13 additions & 13 deletions paddle/gserver/layers/LstmLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ void LstmLayer::forwardSequence(int batchSize,
}
if (prevOutput_) {
frameGate->setData(lstmValue.gateValue);
frameGate->mul(prevOutput_, weight_->getW(), 1, 1);
frameGate->mul(*prevOutput_, *weight_->getW(), 1, 1);
}
}
AsyncGpuBlock asyncGpuBlock;
Expand All @@ -338,7 +338,7 @@ void LstmLayer::forwardSequence(int batchSize,
frameOutput->setData(lstmValue.outputValue);
nextFrame(reversed_, getSize());
frameGate->setData(lstmValue.gateValue);
frameGate->mul(frameOutput, weight_->getW(), 1, 1);
frameGate->mul(*frameOutput, *weight_->getW(), 1, 1);
}
}
if (n != numSequences - 1) {
Expand All @@ -348,7 +348,7 @@ void LstmLayer::forwardSequence(int batchSize,
if (!reversed_) {
if (!prevState_) lstmValue.prevStateValue = nullptr;
if (prevOutput_) {
frameGate->mul(frameOutput, weight_->getW(), 1, 1);
frameGate->mul(*frameOutput, *weight_->getW(), 1, 1);
}
} else {
lstmValue.prevStateValue = nullptr;
Expand Down Expand Up @@ -470,7 +470,7 @@ void LstmLayer::backwardSequence(int batchSize,
frameGate->setData(lstmGrad.gateGrad);
nextFrame(reversed_, getSize());
frameOutput->setData(lstmGrad.outputGrad);
frameOutput->mul(frameGate, weightT, 1, 1);
frameOutput->mul(*frameGate, *weightT, 1, 1);
} else {
nextFrame(reversed_, getSize());
}
Expand All @@ -479,14 +479,14 @@ void LstmLayer::backwardSequence(int batchSize,
if (weight_->getWGrad()) {
if (!reversed_) {
weight_->getWGrad()->mul(
output_.value->subMatrix(start, length - 1)->getTranspose(),
gate_.grad->subMatrix(start + 1, length - 1),
*output_.value->subMatrix(start, length - 1)->getTranspose(),
*gate_.grad->subMatrix(start + 1, length - 1),
1,
1);
} else {
weight_->getWGrad()->mul(
output_.value->subMatrix(start + 1, length - 1)->getTranspose(),
gate_.grad->subMatrix(start, length - 1),
*output_.value->subMatrix(start + 1, length - 1)->getTranspose(),
*gate_.grad->subMatrix(start, length - 1),
1,
1);
}
Expand Down Expand Up @@ -541,15 +541,15 @@ void LstmLayer::forwardBatch(int batchSize,

if (n != 0) {
MatrixPtr batch1 = batchValue_->getBatchValue(n - 1, batchSize);
gateValue->mul(batch1, weight_->getW(), 1, 1);
gateValue->mul(*batch1, *weight_->getW(), 1, 1);
} else if (prevOutput_) {
Matrix::resizeOrCreate(prevBatchOutput2_,
gateValue->getHeight(),
getSize(),
false,
useGpu_);
batchValue_->prevOutput2Batch(*prevOutput_, *prevBatchOutput2_);
gateValue->mul(prevBatchOutput2_, weight_->getW(), 1, 1);
gateValue->mul(*prevBatchOutput2_, *weight_->getW(), 1, 1);

batchValue_->prevOutput2Batch(*prevState_,
*totalState_->subMatrix(0, numSequences));
Expand Down Expand Up @@ -672,16 +672,16 @@ void LstmLayer::backwardBatch(int batchSize,

if (n != 0) {
MatrixPtr tmp = batchGrad_->getBatchValue(n - 1, batchSize);
tmp->mul(gateGrad, weightT, 1, 1);
tmp->mul(*gateGrad, *weightT, 1, 1);
}

if (n != 0 && weight_->getWGrad()) {
/* backward weight */
MatrixPtr outputValue = batchValue_->getBatchValue(n - 1, batchSize);
weight_->getWGrad()->mul(outputValue->getTranspose(), gateGrad, 1, 1);
weight_->getWGrad()->mul(*outputValue->getTranspose(), *gateGrad, 1, 1);
} else if (prevOutput_ && weight_->getWGrad()) {
weight_->getWGrad()->mul(
prevBatchOutput2_->getTranspose(), gateGrad, 1, 1);
*prevBatchOutput2_->getTranspose(), *gateGrad, 1, 1);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions paddle/gserver/layers/MDLstmLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ void MDLstmLayer::forwardOneSequence(int start, CoordIterator& coordIter) {
if (coordIter.getPrePos(delays_, i, prePos)) {
int preOffset = coordIter.offset(prePos);
frameGate_[start + offset].value->mul(
frameOutput_[start + preOffset].value, weight_->getW(), 1.0, 1.0);
*frameOutput_[start + preOffset].value, *weight_->getW(), 1.0, 1.0);
}
}
forwardGate2OutputSequence(start, coordIter);
Expand Down Expand Up @@ -747,11 +747,11 @@ void MDLstmLayer::backwardOneSequence(int start, CoordIterator& coordIter) {
if (coordIter.getPrePos(delays_, i, prePos)) {
int preOffset = coordIter.offset(prePos);
frameOutput_[start + preOffset].grad->mul(
frameGate_[start + offset].grad, weightT, 1.0, 1.0);
*frameGate_[start + offset].grad, *weightT, 1.0, 1.0);
if (weight_->getWGrad()) {
weight_->getWGrad()->mul(
frameOutput_[start + preOffset].value->getTranspose(),
frameGate_[start + offset].grad,
*frameOutput_[start + preOffset].value->getTranspose(),
*frameGate_[start + offset].grad,
1.0,
1.0);
}
Expand Down
6 changes: 3 additions & 3 deletions paddle/gserver/layers/OuterProdLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void OuterProdLayer::forward(PassType passType) {
tmpRow0->setData(inV0->getData() + i * dim0);
tmpRow1->setData(inV1->getData() + i * dim1);

tmpMtx0->mul(tmpRow0->getTranspose(), tmpRow1);
tmpMtx0->mul(*tmpRow0->getTranspose(), *tmpRow1);
}
}
}
Expand All @@ -121,7 +121,7 @@ void OuterProdLayer::backward(const UpdateCallback& callback) {
tmpRow0->setData(inG0->getData() + i * dim0);
tmpRow1->setData(inV1->getData() + i * dim1);

tmpRow0->mul(tmpRow1, tmpMtx0->getTranspose(), 1, 1);
tmpRow0->mul(*tmpRow1, *tmpMtx0->getTranspose(), 1, 1);
}
}

Expand All @@ -131,7 +131,7 @@ void OuterProdLayer::backward(const UpdateCallback& callback) {
tmpRow0->setData(inV0->getData() + i * dim0);
tmpRow1->setData(inG1->getData() + i * dim1);

tmpRow1->mul(tmpRow0, tmpMtx0, 1, 1);
tmpRow1->mul(*tmpRow0, *tmpMtx0, 1, 1);
}
}
}
Expand Down
Loading