-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-20687][MLLIB] mllib.Matrices.fromBreeze may crash when converting from Breeze sparse matrix #17940
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
[SPARK-20687][MLLIB] mllib.Matrices.fromBreeze may crash when converting from Breeze sparse matrix #17940
Changes from 3 commits
62d78a2
dbbd391
df84eb9
b40a706
bc8e14c
18ce388
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -992,7 +992,20 @@ object Matrices { | |
| new DenseMatrix(dm.rows, dm.cols, dm.data, dm.isTranspose) | ||
| case sm: BSM[Double] => | ||
| // There is no isTranspose flag for sparse matrices in Breeze | ||
| new SparseMatrix(sm.rows, sm.cols, sm.colPtrs, sm.rowIndices, sm.data) | ||
|
|
||
| // Some Breeze CSCMatrices may have extra trailing zeros in | ||
| // .rowIndices and .data, which are added after some matrix | ||
| // operations for efficiency. | ||
| // | ||
| // Therefore the last element of sm.colPtrs would no longer be | ||
| // coherent with the size of sm.rowIndices and sm.data | ||
| // despite sm being a valid CSCMatrix. | ||
| // We need to truncate both arrays (rowIndices, data) | ||
| // to the real size of the vector sm.activeSize to allow valid conversion | ||
|
|
||
|
||
| val truncRowIndices = sm.rowIndices.slice(0, sm.activeSize) | ||
| val truncData = sm.data.slice(0, sm.activeSize) | ||
|
||
| new SparseMatrix(sm.rows, sm.cols, sm.colPtrs, truncRowIndices, truncData) | ||
| case _ => | ||
| throw new UnsupportedOperationException( | ||
| s"Do not support conversion from type ${breeze.getClass.getName}.") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,26 @@ class MatricesSuite extends SparkFunSuite { | |
| } | ||
| } | ||
|
|
||
| test("Test Breeze Conversion Bug - SPARK-20687") { | ||
|
||
| // (2, 0, 0) | ||
| // (2, 0, 0) | ||
| val mat1Brz = Matrices.sparse(2, 3, Array(0, 2, 2, 2), Array(0, 1), Array(2, 2)).asBreeze | ||
| // (2, 1E-15, 1E-15) | ||
| // (2, 1E-15, 1E-15) | ||
| val mat2Brz = Matrices.sparse(2, 3, | ||
| Array(0, 2, 4, 6), | ||
| Array(0, 0, 0, 1, 1, 1), | ||
| Array(2, 1E-15, 1E-15, 2, 1E-15, 1E-15)).asBreeze | ||
| val t1Brz = mat1Brz - mat2Brz | ||
| val t2Brz = mat2Brz - mat1Brz | ||
| // The following operations raise exceptions on un-patch Matrices.fromBreeze | ||
| val t1 = Matrices.fromBreeze(t1Brz) | ||
| val t2 = Matrices.fromBreeze(t2Brz) | ||
| // t1 == t1Brz && t2 == t2Brz | ||
| assert((t1.asBreeze - t1Brz).iterator.map((x) => math.abs(x._2)).sum < 1E-15) | ||
| assert((t2.asBreeze - t2Brz).iterator.map((x) => math.abs(x._2)).sum < 1E-15) | ||
| } | ||
|
|
||
| test("sparse matrix construction") { | ||
| val m = 3 | ||
| val n = 4 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
minor: make this comment more compact in fewer lines.