-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-3974][MLlib] Distributed Block Matrix Abstractions #3200
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
b693209
Ready for Pull request
f378e16
[SPARK-3974] Block Matrix Abstractions ready
aa8f086
[SPARK-3974] Additional comments added
589fbb6
[SPARK-3974] Code review feedback addressed
19c17e8
[SPARK-3974] Changed blockIdRow and blockIdCol
b05aabb
[SPARK-3974] Updated tests to reflect changes
brkyvz 645afbe
[SPARK-3974] Pull latest master
brkyvz 49b9586
[SPARK-3974] Updated testing utils from master
brkyvz d033861
[SPARK-3974] Removed SubMatrixInfo and added constructor without part…
brkyvz 9ae85aa
[SPARK-3974] Made partitioner a variable inside BlockMatrix instead o…
brkyvz ab6cde0
[SPARK-3974] Modifications cleaning code up, making size calculation …
brkyvz ba414d2
[SPARK-3974] fixed frobenius norm
brkyvz 239ab4b
[SPARK-3974] Addressed @jkbradley's comments
brkyvz 1e8bb2a
[SPARK-3974] Change return type of cache and persist
brkyvz 1a63b20
[SPARK-3974] Remove setPartition method. Isn't required
brkyvz eebbdf7
preliminary changes addressing code review
brkyvz f9d664b
updated API and modified partitioning scheme
brkyvz 1694c9e
almost finished addressing comments
brkyvz 140f20e
Merge branch 'master' of github.com:apache/spark into SPARK-3974
brkyvz 5eecd48
fixed gridPartitioner and added tests
brkyvz 24ec7b8
update grid partitioner
mengxr e1d3ee8
minor updates
mengxr feb32a7
update tests
mengxr a8eace2
Merge pull request #2 from mengxr/brkyvz-SPARK-3974
brkyvz 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
minor updates
- Loading branch information
commit e1d3ee871c8d73eb111336c6d22d160728582be2
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,17 +52,17 @@ private[mllib] class GridPartitioner( | |
| * Returns the index of the partition the input coordinate belongs to. | ||
| * | ||
| * @param key The coordinate (i, j) or a tuple (i, j, k), where k is the inner index used in | ||
| * multiplication. | ||
| * multiplication. k is ignored in computing partitions. | ||
| * @return The index of the partition, which the coordinate belongs to. | ||
| */ | ||
| override def getPartition(key: Any): Int = { | ||
| key match { | ||
| case (i: Int, j: Int) => | ||
| getPartitionId(i, j) | ||
| case (i: Int, j: Int, _) => | ||
| case (i: Int, j: Int, _: Int) => | ||
| getPartitionId(i, j) | ||
| case _ => | ||
| throw new IllegalArgumentException(s"Unrecognized key: $key") | ||
| throw new IllegalArgumentException(s"Unrecognized key: $key.") | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -73,7 +73,6 @@ private[mllib] class GridPartitioner( | |
| i / rowsPerPart + j / colsPerPart * rowPartitions | ||
| } | ||
|
|
||
| /** Checks whether the partitioners have the same characteristics */ | ||
| override def equals(obj: Any): Boolean = { | ||
| obj match { | ||
| case r: GridPartitioner => | ||
|
|
@@ -87,10 +86,12 @@ private[mllib] class GridPartitioner( | |
|
|
||
| private[mllib] object GridPartitioner { | ||
|
|
||
| /** Creates a new [[GridPartitioner]] instance. */ | ||
| def apply(rows: Int, cols: Int, rowsPerPart: Int, colsPerPart: Int): GridPartitioner = { | ||
| new GridPartitioner(rows, cols, rowsPerPart, colsPerPart) | ||
| } | ||
|
|
||
| /** Creates a new [[GridPartitioner]] instance with the input suggested number of partitions. */ | ||
| def apply(rows: Int, cols: Int, suggestedNumPartitions: Int): GridPartitioner = { | ||
| require(suggestedNumPartitions > 0) | ||
| val scale = 1.0 / math.sqrt(suggestedNumPartitions) | ||
|
|
@@ -103,24 +104,25 @@ private[mllib] object GridPartitioner { | |
| /** | ||
| * Represents a distributed matrix in blocks of local matrices. | ||
| * | ||
| * @param rdd The RDD of SubMatrices (local matrices) that form this matrix | ||
| * @param nRows Number of rows of this matrix. If the supplied value is less than or equal to zero, | ||
| * the number of rows will be calculated when `numRows` is invoked. | ||
| * @param nCols Number of columns of this matrix. If the supplied value is less than or equal to | ||
| * zero, the number of columns will be calculated when `numCols` is invoked. | ||
| * @param blocks The RDD of sub-matrix blocks (blockRowIndex, blockColIndex, sub-matrix) that form | ||
| * this distributed matrix. | ||
| * @param rowsPerBlock Number of rows that make up each block. The blocks forming the final | ||
| * rows are not required to have the given number of rows | ||
| * @param colsPerBlock Number of columns that make up each block. The blocks forming the final | ||
| * columns are not required to have the given number of columns | ||
| * @param nRows Number of rows of this matrix. If the supplied value is less than or equal to zero, | ||
| * the number of rows will be calculated when `numRows` is invoked. | ||
| * @param nCols Number of columns of this matrix. If the supplied value is less than or equal to | ||
| * zero, the number of columns will be calculated when `numCols` is invoked. | ||
| */ | ||
| class BlockMatrix( | ||
| val rdd: RDD[((Int, Int), Matrix)], | ||
| private var nRows: Long, | ||
| private var nCols: Long, | ||
| val blocks: RDD[((Int, Int), Matrix)], | ||
| val rowsPerBlock: Int, | ||
| val colsPerBlock: Int) extends DistributedMatrix with Logging { | ||
| val colsPerBlock: Int, | ||
| private var nRows: Long, | ||
| private var nCols: Long) extends DistributedMatrix with Logging { | ||
|
|
||
| private type SubMatrix = ((Int, Int), Matrix) // ((blockRowIndex, blockColIndex), matrix) | ||
| private type MatrixBlock = ((Int, Int), Matrix) // ((blockRowIndex, blockColIndex), sub-matrix) | ||
|
|
||
| /** | ||
| * Alternate constructor for BlockMatrix without the input of the number of rows and columns. | ||
|
|
@@ -135,45 +137,48 @@ class BlockMatrix( | |
| rdd: RDD[((Int, Int), Matrix)], | ||
| rowsPerBlock: Int, | ||
| colsPerBlock: Int) = { | ||
| this(rdd, 0L, 0L, rowsPerBlock, colsPerBlock) | ||
| this(rdd, rowsPerBlock, colsPerBlock, 0L, 0L) | ||
| } | ||
|
|
||
| private lazy val dims: (Long, Long) = getDim | ||
|
|
||
| override def numRows(): Long = { | ||
| if (nRows <= 0L) nRows = dims._1 | ||
| if (nRows <= 0L) estimateDim() | ||
| nRows | ||
| } | ||
|
|
||
| override def numCols(): Long = { | ||
| if (nCols <= 0L) nCols = dims._2 | ||
| if (nCols <= 0L) estimateDim() | ||
| nCols | ||
| } | ||
|
|
||
| val numRowBlocks = math.ceil(numRows() * 1.0 / rowsPerBlock).toInt | ||
| val numColBlocks = math.ceil(numCols() * 1.0 / colsPerBlock).toInt | ||
|
|
||
| private[mllib] var partitioner: GridPartitioner = | ||
| GridPartitioner(numRowBlocks, numColBlocks, suggestedNumPartitions = rdd.partitions.size) | ||
|
|
||
| /** Returns the dimensions of the matrix. */ | ||
| private def getDim: (Long, Long) = { | ||
| val (rows, cols) = rdd.map { case ((blockRowIndex, blockColIndex), mat) => | ||
| (blockRowIndex * rowsPerBlock + mat.numRows, blockColIndex * colsPerBlock + mat.numCols) | ||
| }.reduce((x0, x1) => (math.max(x0._1, x1._1), math.max(x0._2, x1._2))) | ||
|
|
||
| (math.max(rows, nRows), math.max(cols, nCols)) | ||
| GridPartitioner(numRowBlocks, numColBlocks, suggestedNumPartitions = blocks.partitions.size) | ||
|
|
||
| /** Estimates the dimensions of the matrix. */ | ||
| private def estimateDim(): Unit = { | ||
| val (rows, cols) = blocks.map { case ((blockRowIndex, blockColIndex), mat) => | ||
| (blockRowIndex.toLong * rowsPerBlock + mat.numRows, | ||
| blockColIndex.toLong * colsPerBlock + mat.numCols) | ||
| }.reduce { (x0, x1) => | ||
| (math.max(x0._1, x1._1), math.max(x0._2, x1._2)) | ||
| } | ||
|
Member
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. style: indentation |
||
| if (nRows <= 0L) nRows = rows | ||
| assert(rows <= nRows, s"The number of rows $rows is more than claimed $nRows.") | ||
| if (nCols <= 0L) nCols = cols | ||
| assert(cols <= nCols, s"The number of columns $cols is more than claimed $nCols.") | ||
| } | ||
|
|
||
| /** Cache the underlying RDD. */ | ||
| def cache(): BlockMatrix = { | ||
| rdd.cache() | ||
| /** Caches the underlying RDD. */ | ||
| def cache(): this.type = { | ||
| blocks.cache() | ||
| this | ||
| } | ||
|
|
||
| /** Set the storage level for the underlying RDD. */ | ||
| def persist(storageLevel: StorageLevel): BlockMatrix = { | ||
| rdd.persist(storageLevel) | ||
| /** Persists the underlying RDD with the specified storage level. */ | ||
| def persist(storageLevel: StorageLevel): this.type = { | ||
| blocks.persist(storageLevel) | ||
| this | ||
| } | ||
|
|
||
|
|
@@ -185,22 +190,22 @@ class BlockMatrix( | |
| s"Int.MaxValue. Currently numCols: ${numCols()}") | ||
| require(numRows() * numCols() < Int.MaxValue, "The length of the values array must be " + | ||
| s"less than Int.MaxValue. Currently numRows * numCols: ${numRows() * numCols()}") | ||
| val nRows = numRows().toInt | ||
| val nCols = numCols().toInt | ||
| val mem = nRows * nCols / 125000 | ||
| val m = numRows().toInt | ||
| val n = numCols().toInt | ||
| val mem = m * n / 125000 | ||
| if (mem > 500) logWarning(s"Storing this matrix will require $mem MB of memory!") | ||
|
|
||
| val parts = rdd.collect() | ||
| val values = new Array[Double](nRows * nCols) | ||
| parts.foreach { case ((blockRowIndex, blockColIndex), block) => | ||
| val localBlocks = blocks.collect() | ||
| val values = new Array[Double](m * n) | ||
| localBlocks.foreach { case ((blockRowIndex, blockColIndex), submat) => | ||
| val rowOffset = blockRowIndex * rowsPerBlock | ||
| val colOffset = blockColIndex * colsPerBlock | ||
| block.foreachActive { (i, j, v) => | ||
| val indexOffset = (j + colOffset) * nRows + rowOffset + i | ||
| submat.foreachActive { (i, j, v) => | ||
| val indexOffset = (j + colOffset) * m + rowOffset + i | ||
| values(indexOffset) = v | ||
| } | ||
| } | ||
| new DenseMatrix(nRows, nCols, values) | ||
| new DenseMatrix(m, n, values) | ||
| } | ||
|
|
||
| /** Collects data and assembles a local dense breeze matrix (for test only). */ | ||
|
|
||
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.
The list of arguments cannot provide the complete info about the matrix. For example, if the last block row and the last block column are all missing. Then you cannot figure out the exact matrix size from this list of arguments.
It would be necessary to have
numRows,numCols,rowsPerBlock,colsPerBlock, and the RDD as input. We can provide factory methods (in follow-up PRs) to create block matrices from other formats, which could figure out the exactnumRowsandnumColsand use them in the constructor.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.
Will it really be the case that the whole row of blocks will be missing for the last row? That means that those rows (or columns) contain no information. Then why store (use) them?
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.
We cannot make such assumption about the data. It is not rare that we have an empty column/row, which is the last column/row and the only column/row in the last column/row block. For example, in the popular mnist-digit dataset, the last column of the training data is empty.