-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add matrix inverse #240
Merged
Merged
Add matrix inverse #240
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7e7c612
Add matrix inverse
lzhao4ever 7105d24
add include dir for clapack.h
lzhao4ever 6e0ec6b
Add lapack support for openblas
lzhao4ever 0d88a88
Fix atlas incude dir
lzhao4ever 897797d
Address reviewers comments
lzhao4ever 86f6cbe
Use t_resource.gpu_mem
lzhao4ever 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -335,6 +335,30 @@ void GpuMatrix::transpose(MatrixPtr matTrans, bool memAlloc) { | |
hl_matrix_transpose(data, dataTrans, height_, width_, lda, ldc); | ||
} | ||
|
||
|
||
MatrixPtr GpuMatrix::getInverse() { | ||
MatrixPtr matInv; | ||
inverse(matInv, true); | ||
return matInv; | ||
} | ||
|
||
void GpuMatrix::inverse(MatrixPtr matInv, bool memAlloc) { | ||
CHECK_EQ(height_, width_); | ||
|
||
if (memAlloc) { | ||
matInv = std::make_shared<GpuMatrix>(height_, width_); | ||
} else { | ||
CHECK(matInv != NULL); | ||
} | ||
|
||
real* data = getData(); | ||
real* dataInv = matInv->getData(); | ||
int lda = getStride(); | ||
int ldc = matInv->getStride(); | ||
|
||
hl_matrix_inverse(data, dataInv, height_, lda, ldc); | ||
} | ||
|
||
void GpuMatrix::addBias(Matrix& b, real scale) { | ||
CHECK(b.getHeight() == 1) << "the Bias should be a vector"; | ||
BaseMatrix::addBias(b, scale); | ||
|
@@ -1420,6 +1444,47 @@ void CpuMatrix::transpose(MatrixPtr matTrans, bool memAlloc) { | |
} | ||
} | ||
|
||
|
||
MatrixPtr CpuMatrix::getInverse() { | ||
MatrixPtr matInv; | ||
inverse(matInv, true); | ||
return matInv; | ||
} | ||
|
||
void CpuMatrix::inverse(MatrixPtr matInv, bool memAlloc) { | ||
CHECK_EQ(height_, width_); | ||
|
||
if (memAlloc) { | ||
matInv = std::make_shared<CpuMatrix>(height_, width_); | ||
} else { | ||
CHECK(matInv != NULL); | ||
} | ||
|
||
CHECK_EQ(height_, matInv->getHeight()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CHECK_EQ(width_, matInv->getWidth()); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
CHECK_EQ(width_, matInv->getWidth()); | ||
matInv->copyFrom(*this); | ||
|
||
real* data = getData(); | ||
real* dataInv = matInv->getData(); | ||
int ldc = matInv->getStride(); | ||
|
||
if (height_ == 1) { | ||
CHECK_NE(*data, 0); | ||
*dataInv = 1.0 / (*data); | ||
return; | ||
} | ||
|
||
/* Compute the LU decomposition of the matrix */ | ||
std::vector<int> ipiv(height_); | ||
CBLAS_ORDER order = (matInv->isTransposed() ? CblasColMajor : CblasRowMajor); | ||
int info = getrf<real>(order, height_, height_, dataInv, ldc, ipiv.data()); | ||
CHECK_EQ(info, 0); | ||
|
||
/* Compute the inverse of the matrix given its LU decompsotion */ | ||
info = getri<real>(order, height_, dataInv, ldc, ipiv.data()); | ||
CHECK_EQ(info, 0); | ||
} | ||
|
||
void CpuMatrix::convExpand(Matrix& feature, int feaImgHeight, int feaImgWidth, | ||
int channels, int blockH, int blockW, int strideH, | ||
int strideW, int paddingH, int paddingW, | ||
|
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.
cublasSgetrfBatched is used to calculate a number of small-sized matrices.
So, it is best to comment that the performance of this interface may be poor.
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.
Do you have recommendation of an interface with better performance?
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.
I mean it 's best to add comment here.
Such as adding a
TODO
, so that other people see this note, there may be a better way to reconstruct the API.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.