-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForAll #17742
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
Changes from 4 commits
14cdbf6
f607e6c
ae03124
2fd97a0
206a023
8eab55b
a5261b7
44a4f74
17df4cf
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 |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ import org.apache.spark.mllib.util.{Loader, Saveable} | |
| import org.apache.spark.rdd.RDD | ||
| import org.apache.spark.sql.{Row, SparkSession} | ||
| import org.apache.spark.storage.StorageLevel | ||
| import org.apache.spark.util.BoundedPriorityQueue | ||
|
|
||
| /** | ||
| * Model representing the result of matrix factorization. | ||
|
|
@@ -276,18 +277,39 @@ object MatrixFactorizationModel extends Loader[MatrixFactorizationModel] { | |
| num: Int): RDD[(Int, Array[(Int, Double)])] = { | ||
| val srcBlocks = blockify(rank, srcFeatures) | ||
| val dstBlocks = blockify(rank, dstFeatures) | ||
| val ratings = srcBlocks.cartesian(dstBlocks).flatMap { | ||
| case ((srcIds, srcFactors), (dstIds, dstFactors)) => | ||
| val m = srcIds.length | ||
| val n = dstIds.length | ||
| val ratings = srcFactors.transpose.multiply(dstFactors) | ||
| val output = new Array[(Int, (Int, Double))](m * n) | ||
| var k = 0 | ||
| ratings.foreachActive { (i, j, r) => | ||
| output(k) = (srcIds(i), (dstIds(j), r)) | ||
| k += 1 | ||
| } | ||
| output.toSeq | ||
| val ratings = srcBlocks.cartesian(dstBlocks).flatMap { case (srcIter, dstIter) => | ||
| val m = srcIter.size | ||
| val n = math.min(dstIter.size, num) | ||
| val output = new Array[(Int, (Int, Double))](m * n) | ||
| var j = 0 | ||
| srcIter.foreach { case (srcId, srcFactor) => | ||
| def order(a: (Int, Double)) = a._2 | ||
| val pq: BoundedPriorityQueue[(Int, Double)] = | ||
|
Contributor
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. We could remove the type sig from the |
||
| new BoundedPriorityQueue[(Int, Double)](n)(Ordering.by(order)) | ||
|
Contributor
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. I believe you can just do |
||
| dstIter.foreach { case (dstId, dstFactor) => | ||
| /** | ||
| * blas.ddot (F2jBLAS) is the same performance with the following code. | ||
| * the performace of blas.ddot with NativeBLAS is very bad. | ||
| * blas.ddot (F2jBLAS) is about 10% improvement comparing with linalg.dot. | ||
| * val rate = blas.ddot(rank, user._2, 1, item._2, 1) | ||
|
Contributor
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. We can perhaps say here instead "The below code is equivalent to |
||
| */ | ||
| var score: Double = 0 | ||
| var k = 0 | ||
| while (k < rank) { | ||
| score += srcFactor(k) * dstFactor(k) | ||
| k += 1 | ||
| } | ||
| pq += ((dstId, score)) | ||
| } | ||
| val pqIter = pq.iterator | ||
| var i = 0 | ||
| while (i < n) { | ||
| output(j + i) = (srcId, pqIter.next()) | ||
| i += 1 | ||
| } | ||
| j += n | ||
| } | ||
| output.toSeq | ||
| } | ||
| ratings.topByKey(num)(Ordering.by(_._2)) | ||
| } | ||
|
|
@@ -297,23 +319,10 @@ object MatrixFactorizationModel extends Loader[MatrixFactorizationModel] { | |
| */ | ||
|
Contributor
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. We should adjust the comment here as we're not using Level-3 BLAS any more. |
||
| private def blockify( | ||
| rank: Int, | ||
| features: RDD[(Int, Array[Double])]): RDD[(Array[Int], DenseMatrix)] = { | ||
| features: RDD[(Int, Array[Double])]): RDD[Seq[(Int, Array[Double])]] = { | ||
|
Contributor
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. We can remove
Contributor
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. Let's match what I've done in https://github.com/apache/spark/pull/17845/files#diff-be65dd1d6adc53138156641b610fcadaR440 - i.e.
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. With this change, it seems to me that the performance can be less sensitive to
Contributor
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. Yes, less sensitive. See https://issues.apache.org/jira/browse/SPARK-20443. It may be that we make the block size tunable - or by experiments set a block size that seems generally optimal (2048 in those experiments seems best). But we would need to perform experiments over a wide range of data sizes (and check both |
||
| val blockSize = 4096 // TODO: tune the block size | ||
| val blockStorage = rank * blockSize | ||
| features.mapPartitions { iter => | ||
| iter.grouped(blockSize).map { grouped => | ||
| val ids = mutable.ArrayBuilder.make[Int] | ||
| ids.sizeHint(blockSize) | ||
| val factors = mutable.ArrayBuilder.make[Double] | ||
| factors.sizeHint(blockStorage) | ||
| var i = 0 | ||
| grouped.foreach { case (id, factor) => | ||
| ids += id | ||
| factors ++= factor | ||
| i += 1 | ||
| } | ||
| (ids.result(), new DenseMatrix(rank, i, factors.result())) | ||
| } | ||
| iter.grouped(blockSize) | ||
| } | ||
| } | ||
|
|
||
|
|
||
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'd like to more detail to the doc string comment for this method to explain the approach used for efficiency.