-
Notifications
You must be signed in to change notification settings - Fork 29k
[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 3 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. | ||
|
|
@@ -277,17 +278,39 @@ object MatrixFactorizationModel extends Loader[MatrixFactorizationModel] { | |
| 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 | ||
| case (users, items) => | ||
|
||
| val m = users.size | ||
| val n = math.min(items.size, num) | ||
| val output = new Array[(Int, (Int, Double))](m * n) | ||
| var j = 0 | ||
| users.foreach (user => { | ||
|
||
| def order(a: (Int, Double)) = a._2 | ||
| val pq: BoundedPriorityQueue[(Int, Double)] = | ||
|
||
| new BoundedPriorityQueue[(Int, Double)](n)(Ordering.by(order)) | ||
|
||
| items.foreach (item => { | ||
|
||
| /** | ||
| * 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) | ||
|
||
| */ | ||
| var rate: Double = 0 | ||
| var k = 0 | ||
| while(k < rank) { | ||
|
||
| rate += user._2(k) * item._2(k) | ||
|
||
| k += 1 | ||
| } | ||
| pq += ((item._1, rate)) | ||
|
||
| }) | ||
| val pqIter = pq.iterator | ||
| var i = 0 | ||
| while(i < n) { | ||
|
||
| output(j + i) = (user._1, pqIter.next()) | ||
|
||
| i += 1 | ||
| } | ||
| j += n | ||
| }) | ||
| output.toSeq | ||
| } | ||
| ratings.topByKey(num)(Ordering.by(_._2)) | ||
| } | ||
|
|
@@ -297,23 +320,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])]] = { | ||
|
||
| 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.
Put case statement on previous line:
flatMap { case (... =>