-
Notifications
You must be signed in to change notification settings - Fork 6k
Matrix::MUL operators using and test Daoyuan's Paddle Function, SparseMatrixArg and Function Test #1147
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
Merged
tianbingsz
merged 13 commits into
PaddlePaddle:develop
from
tianbingsz:paddle_func_sparse
Jan 30, 2017
Merged
Matrix::MUL operators using and test Daoyuan's Paddle Function, SparseMatrixArg and Function Test #1147
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
936301f
Use and test Daoyuan's SparseMatrixArg.
1f0cbcf
add GpuMatrix::mul, CpuMatrix::mul operators
2df8eec
Pass Unit test for GpuMatrix::mul(GpuMatrix, GpuMatrix) and CpuMatrix…
1ca2846
Pass unit test for CpuMatrix::mul(CpuMatrix, CpuSparseMatrix)
4751cc8
Pass unit test for SparseCpuMatrix::mul(CpuMatrix, CpuMatrix),
171eaff
clean the code a little bit.
9ade63e
clean code a little bit.
316bf75
clean code in function/MulOp.cpp
077f936
Support SparseMatrixArg unit test using Daoyuan's new Function Test.
bc5d7bb
Add Sparse = dense * dense unit test with Daoyuan's Function test
b3be735
Daoyuan's comments.
999cd14
Further address Daoyuan's comments, clean the code.
5b1a5c1
Daoyuan's comments.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,8 @@ See the License for the specific language governing permissions and | |
| limitations under the License. */ | ||
|
|
||
| #include "Function.h" | ||
| #include "paddle/math/Vector.h" | ||
| #include "paddle/math/Matrix.h" | ||
| #include "paddle/math/SparseMatrix.h" | ||
| #include "paddle/math/tests/TensorCheck.h" | ||
| #include "paddle/testing/TestUtil.h" | ||
|
|
||
|
|
@@ -69,7 +70,7 @@ class FunctionCompare { | |
| } | ||
|
|
||
| // output need only contains shape, do not contains data. | ||
| void addOutputs(const BufferArg& output) { | ||
| void addOutputs(const BufferArg& output, ArgType argType = ASSIGN_TO) { | ||
| size_t size = | ||
| output.shape().getElements() * sizeOfValuType(output.valueType()); | ||
| cpuMemory_.emplace_back(std::make_shared<CpuMemoryHandle>(size)); | ||
|
|
@@ -79,12 +80,40 @@ class FunctionCompare { | |
| std::make_shared<BufferArg>(cpuMemory_.back()->getBuf(), | ||
| output.valueType(), | ||
| output.shape(), | ||
| ASSIGN_TO)); | ||
| argType)); | ||
| gpuOutputs_.emplace_back( | ||
| std::make_shared<BufferArg>(gpuMemory_.back()->getBuf(), | ||
| output.valueType(), | ||
| output.shape(), | ||
| ASSIGN_TO)); | ||
| argType)); | ||
| } | ||
|
|
||
| /// add and init output sparse matrix | ||
| void addOutputs(const SparseMatrixArg& output, ArgType argType = ASSIGN_TO) { | ||
| cpuSparse_ = std::make_shared<CpuSparseMatrix>( | ||
| output.shape()[0], | ||
| output.shape()[1], | ||
| output.nnz(), | ||
| static_cast<SparseValueType>(output.dataType()), | ||
| static_cast<SparseFormat>(output.dataFormat())); | ||
|
|
||
| gpuSparse_ = std::make_shared<GpuSparseMatrix>( | ||
| output.shape()[0], | ||
| output.shape()[1], | ||
| output.nnz(), | ||
| static_cast<SparseValueType>(output.dataType()), | ||
| static_cast<SparseFormat>(output.dataFormat())); | ||
|
|
||
| /// init sparse matrix | ||
| hl_stream_t stream(HPPL_STREAM_1); | ||
| cpuSparse_->randomizeUniform(); | ||
| gpuSparse_->copyFrom(*cpuSparse_, stream); | ||
| hl_stream_synchronize(stream); | ||
|
|
||
| cpuOutputs_.emplace_back( | ||
| std::make_shared<SparseMatrixArg>(*cpuSparse_, argType)); | ||
| gpuOutputs_.emplace_back( | ||
| std::make_shared<SparseMatrixArg>(*gpuSparse_, argType)); | ||
| } | ||
|
|
||
| void addInputs(const SequenceArg& input) { | ||
|
|
@@ -107,10 +136,36 @@ class FunctionCompare { | |
| // TODO: need be implemented. | ||
| } | ||
|
|
||
| void addInputs(const SparseMatrixArg& input) { | ||
| cpuSparse_ = std::make_shared<CpuSparseMatrix>( | ||
| input.shape()[0], | ||
| input.shape()[1], | ||
| input.nnz(), | ||
| static_cast<SparseValueType>(input.dataType()), | ||
| static_cast<SparseFormat>(input.dataFormat())); | ||
|
|
||
| gpuSparse_ = std::make_shared<GpuSparseMatrix>( | ||
| input.shape()[0], | ||
| input.shape()[1], | ||
| input.nnz(), | ||
| static_cast<SparseValueType>(input.dataType()), | ||
| static_cast<SparseFormat>(input.dataFormat())); | ||
|
|
||
| /// init sparse matrix | ||
| hl_stream_t stream(HPPL_STREAM_1); | ||
| cpuSparse_->randomizeUniform(); | ||
| gpuSparse_->copyFrom(*cpuSparse_, stream); | ||
| hl_stream_synchronize(stream); | ||
|
|
||
| cpuInputs_.emplace_back(std::make_shared<SparseMatrixArg>(*cpuSparse_)); | ||
| gpuInputs_.emplace_back(std::make_shared<SparseMatrixArg>(*gpuSparse_)); | ||
| } | ||
|
|
||
| void run() { | ||
| // prepare cpu/gpu arguments | ||
| initInputs(); | ||
|
|
||
| initOutputs(); | ||
|
||
| // function calculate | ||
| auto callFunction = [](FunctionBase* function, | ||
| std::vector<BufferArgPtr>& inputs, | ||
|
|
@@ -129,7 +184,7 @@ class FunctionCompare { | |
| callFunction(cpuFunc_.get(), cpuInputs_, cpuOutputs_); | ||
| callFunction(gpuFunc_.get(), gpuInputs_, gpuOutputs_); | ||
|
|
||
| // check outputs and inouts | ||
| // check outputs | ||
| compareOutputs(); | ||
| } | ||
|
|
||
|
|
@@ -140,6 +195,10 @@ class FunctionCompare { | |
| protected: | ||
| void initInputs() { | ||
| for (size_t i = 0; i < cpuInputs_.size(); i++) { | ||
| if (cpuInputs_[i]->isSparseArg()) { | ||
| continue; /// sparse matrix already init | ||
| } | ||
|
|
||
| initArg(*cpuInputs_[i]); | ||
|
|
||
| // TODO: Need a BufferCopy used to copy from one BufferArg to another. | ||
|
|
@@ -152,14 +211,32 @@ class FunctionCompare { | |
| } | ||
| } | ||
|
|
||
| void initOutputs() { | ||
| for (size_t i = 0; i < cpuOutputs_.size(); i++) { | ||
| if (cpuOutputs_[i]->isSparseArg()) { | ||
| continue; /// sparse matrix already init | ||
| } | ||
|
|
||
| initArg(*cpuOutputs_[i]); | ||
|
|
||
| // TODO: Need a BufferCopy used to copy from one BufferArg to another. | ||
| CpuVector cpuVector(cpuOutputs_[i]->shape().getElements(), | ||
| (real*)cpuOutputs_[i]->data()); | ||
| GpuVector gpuVector(gpuOutputs_[i]->shape().getElements(), | ||
| (real*)gpuOutputs_[i]->data()); | ||
|
|
||
| gpuVector.copyFrom(cpuVector); | ||
| } | ||
| } | ||
|
|
||
| void compareOutputs() { | ||
| for (size_t i = 0; i < cpuOutputs_.size(); i++) { | ||
| // TODO, Need a BufferCheck used to compare the two buffers. | ||
| auto cpu = cpuOutputs_[i]; | ||
| auto gpu = gpuOutputs_[i]; | ||
| CpuVector cpuVector(cpu->shape().getElements(), (real*)cpu->data()); | ||
| GpuVector gpuVector(cpu->shape().getElements(), (real*)gpu->data()); | ||
|
|
||
| const auto cpu = cpuOutputs_[i]; | ||
| const auto gpu = gpuOutputs_[i]; | ||
| CHECK_EQ(cpu->numElements(), gpu->numElements()); | ||
| CpuVector cpuVector(cpu->numElements(), (real*)cpu->data()); | ||
| GpuVector gpuVector(gpu->numElements(), (real*)gpu->data()); | ||
| autotest::TensorCheckErr(cpuVector, gpuVector); | ||
| } | ||
| } | ||
|
|
@@ -195,6 +272,8 @@ class FunctionCompare { | |
| std::vector<BufferArgPtr> cpuOutputs_; | ||
| std::vector<BufferArgPtr> gpuInputs_; | ||
| std::vector<BufferArgPtr> gpuOutputs_; | ||
| std::shared_ptr<CpuSparseMatrix> cpuSparse_; | ||
| std::shared_ptr<GpuSparseMatrix> gpuSparse_; | ||
| }; | ||
|
|
||
| } // namespace paddle | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SparseFormat和SparseValueType放在Matrix.h里面并不合适;Matrix.h和SparseMatrix.h里面的数据结构最终是不准备用的,所以可以考虑把这两个TensorType.h里面去。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done