Skip to content

Commit 728defb

Browse files
author
Haonan
committed
copy the data when createSparseMatrix
1 parent 069d000 commit 728defb

File tree

3 files changed

+66
-32
lines changed

3 files changed

+66
-32
lines changed

paddle/gserver/layers/CostLayer.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -462,29 +462,49 @@ bool MultiBinaryLabelCrossEntropy::init(const LayerMap& layerMap,
462462

463463
void MultiBinaryLabelCrossEntropy::forwardImp(Matrix& output, Argument& label,
464464
Matrix& target) {
465-
label.idsToSparseMatrix(output.getWidth(), useGpu_);
465+
MatrixPtr value = nullptr;
466+
if (label.ids) {
467+
CHECK(!label.value);
468+
value = Matrix::createSparseMatrix(
469+
label.ids->getSize(), output.getWidth(), label.ids->getSize(),
470+
NO_VALUE, SPARSE_CSR, false, useGpu_);
471+
label.idsToSparseMatrix(value);
472+
} else {
473+
CHECK(label.value);
474+
value = label.value;
475+
}
466476

467-
if (dynamic_cast<CpuSparseMatrix*>(label.value.get()) ||
468-
dynamic_cast<GpuSparseMatrix*>(label.value.get())) {
469-
target.multiBinaryLabelCrossEntropy(output, *label.value);
477+
if (dynamic_cast<CpuSparseMatrix*>(value.get()) ||
478+
dynamic_cast<GpuSparseMatrix*>(value.get())) {
479+
target.multiBinaryLabelCrossEntropy(output, *value);
470480
} else {
471481
Matrix::resizeOrCreate(targetPerDim_, output.getHeight(), output.getWidth(),
472482
false, useGpu_);
473483

474-
targetPerDim_->binaryLabelCrossEntropy(output, *label.value);
484+
targetPerDim_->binaryLabelCrossEntropy(output, *value);
475485
targetPerDim_->rowSum(target);
476486
}
477487
}
478488

479489
void MultiBinaryLabelCrossEntropy::backwardImp(
480490
Matrix& output, Argument& label, Matrix& outputG) {
481-
label.idsToSparseMatrix(output.getWidth(), useGpu_);
491+
MatrixPtr value = nullptr;
492+
if (label.ids) {
493+
CHECK(!value);
494+
value = Matrix::createSparseMatrix(
495+
label.ids->getSize(), output.getWidth(), label.ids->getSize(),
496+
NO_VALUE, SPARSE_CSR, false, useGpu_);
497+
label.idsToSparseMatrix(value);
498+
} else {
499+
CHECK(label.value);
500+
value = label.value;
501+
}
482502

483-
if (dynamic_cast<CpuSparseMatrix*>(label.value.get()) ||
484-
dynamic_cast<GpuSparseMatrix*>(label.value.get())) {
485-
outputG.multiBinaryLabelCrossEntropyBp(output, *label.value);
503+
if (dynamic_cast<CpuSparseMatrix*>(value.get()) ||
504+
dynamic_cast<GpuSparseMatrix*>(value.get())) {
505+
outputG.multiBinaryLabelCrossEntropyBp(output, *value);
486506
} else {
487-
outputG.binaryLabelCrossEntropyBp(output, *label.value);
507+
outputG.binaryLabelCrossEntropyBp(output, *value);
488508
}
489509
}
490510

paddle/parameter/Argument.cpp

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -572,25 +572,41 @@ void Argument::subArgFrom(const Argument& input, size_t offset, size_t height,
572572
}
573573
}
574574

575-
void Argument::idsToSparseMatrix(int width, bool useGpu) {
576-
if (ids) {
577-
CHECK(!value);
578-
int height = ids->getSize();
579-
int nnz = height;
580-
auto rows = IVector::create(height + 1, useGpu);
581-
auto cols = IVector::create(nnz, useGpu);
582-
rows->setElement(0, 0);
583-
for (int i = 0; i < height; i ++) {
584-
int id = ids->getElement(i);
585-
CHECK_LT(id, width);
586-
rows->setElement(i + 1, i + 1);
587-
cols->setElement(i, id);
588-
}
589-
value = Matrix::createSparseMatrix(
590-
nullptr, rows->getData(), cols->getData(),
591-
height, width, nnz, NO_VALUE, SPARSE_CSR, false, useGpu);
575+
void Argument::idsToSparseMatrix(MatrixPtr sparse_mat) {
576+
int height = ids->getSize();
577+
int width = sparse_mat->getWidth();
578+
579+
CpuIVector cpu_ids(height);
580+
cpu_ids.copyFrom(*ids);
581+
int *id_data = cpu_ids.getData();
582+
583+
int *rows = nullptr;
584+
int *cols = nullptr;
585+
if (sparse_mat->useGpu()) {
586+
auto gpu_sparse_mat =
587+
dynamic_cast<GpuSparseMatrix*>(sparse_mat.get());
588+
rows = gpu_sparse_mat->rows_;
589+
cols = gpu_sparse_mat->cols_;
592590
} else {
593-
CHECK(value);
591+
rows = sparse_mat->getRows();
592+
cols = sparse_mat->getCols();
593+
}
594+
595+
rows[0] = 0;
596+
for (int i = 0; i < height; i ++) {
597+
int id = id_data[i];
598+
CHECK_LT(id, width);
599+
rows[i + 1] = i + 1;
600+
cols[i] = id;
601+
}
602+
603+
if (sparse_mat->useGpu()) {
604+
auto gpu_sparse_mat =
605+
dynamic_cast<GpuSparseMatrix*>(sparse_mat.get());
606+
hl_memcpy_csr_matrix(gpu_sparse_mat->sMatrix_.get(),
607+
nullptr, rows, cols,
608+
HPPL_STREAM_DEFAULT);
609+
hl_stream_synchronize(HPPL_STREAM_DEFAULT);
594610
}
595611
}
596612

paddle/parameter/Argument.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,9 @@ struct Argument {
289289

290290
/*
291291
@brief convert the ids vector to value as a sparse matrix
292-
the ids vector keeps valid
293-
@param the matrix width (id range)
294-
@useGpu
292+
@param[out] the output sparse_mat (already allocated)
295293
*/
296-
void idsToSparseMatrix(int width, bool useGpu);
294+
void idsToSparseMatrix(MatrixPtr sparse_mat);
297295
};
298296

299297
} // namespace paddle

0 commit comments

Comments
 (0)