You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by mpjlu <gi...@git.apache.org> on 2017/04/24 10:50:20 UTC

[GitHub] spark pull request #17742: [Spark-20446][ML][MLLIB]Optimize MLLIB ALS recomm...

GitHub user mpjlu opened a pull request:

    https://github.com/apache/spark/pull/17742

    [Spark-20446][ML][MLLIB]Optimize MLLIB ALS recommendForAll

    ## What changes were proposed in this pull request?
    
    The recommendForAll of MLLIB ALS is very slow.
    GC is a key problem of the current method.
    The task use the following code to keep temp result:
    val output = new Array[(Int, (Int, Double))](m*n)
    m = n = 4096 (default value, no method to set)
    so output is about 4k * 4k * (4 + 4 + 8) = 256M. This is a large memory and cause serious GC problem, and it is frequently OOM.
    
    Actually, we don't need to save all the temp result. Support we recommend topK (topK is about 10, or 20) product for each user, we only need 4k * topK * (4 + 4 + 8) memory to save the temp result.
    
    The Test Environment:
    3 workers: each work 10 core, each work 30G memory, each work 1 executor.
    The Data: User 480,000, and Item 17,000
    
    BlockSize:     1024  2048  4096  8192
    Old method:  245s  332s  488s  OOM
    This solution: 121s  118s   117s  120s
    
    
    
    ## How was this patch tested?
    The existing UT.


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/mpjlu/spark OptimizeAls

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/spark/pull/17742.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #17742
    
----
commit 14cdbf63e79ebcf2d1207c79b0b4ba73e15729b2
Author: Peng <pe...@intel.com>
Date:   2017-04-24T08:32:16Z

    Optimize ALS recommendForAll

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/76258/
    Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Merged build finished. Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113862880
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -276,44 +277,53 @@ 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
    +    /**
    +     * Use dot to replace blas 3 gemm is the key approach to improve efficiency.
    +     * By this change, we can get the topK elements of each block to reduce the GC time.
    +     * Comparing with BLAS.dot, hand-written dot is high efficiency.
    +     */
    +    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) =>
    +          val pq = new BoundedPriorityQueue[(Int, Double)](n)(Ordering.by(_._2))
    --- End diff --
    
    Thanks @srowen 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    retest this please


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76455 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76455/testReport)** for PR 17742 at commit [`17df4cf`](https://github.com/apache/spark/commit/17df4cf8aabd5f4e28551e31d43b0231c1e68676).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    A single buffer doesn't lead to long GC pause. If it request lot of memory, it might trigger GC to collect other objects. But itself is a single object, which can be easily GC'ed. The problem here is having many small long-living objects as in `output`.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Merged build finished. Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-20446][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76111 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76111/testReport)** for PR 17742 at commit [`f607e6c`](https://github.com/apache/spark/commit/f607e6c441559c5d48cd4c9dac58960b98c2198a).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Merged build finished. Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Could you post updated performance numbers?
    
    I think we can do the same optimization in `ml` version. We could do it in a follow up PR perhaps?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    I find why F2j BLAS is much faster than Native BLAS for xiangrui's method (use GEMM) here. 
    https://issues.apache.org/jira/browse/SPARK-21305


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/76455/
    Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113761236
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -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())
    --- End diff --
    
    Ah right - good point. Fine to leave as it is 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-20446][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Merged build finished. Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Thanks very much @MLnick . 
    I am doing more test about mllib solution. When it is solid enough, then we can submit a follow up PR for ML optimization. How do you think about it?  


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76180 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76180/testReport)** for PR 17742 at commit [`2fd97a0`](https://github.com/apache/spark/commit/2fd97a043c9ca72a04012a4b4cef06ecc023917c).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    @MLnick , I did not test rank in range 100 to 200. I am on vacation and my colleague is doing this test, I will update this result soon. 
    Thanks.



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    This LGTM - it would be nice to have this and #17845 in 2.2 (especially since the `ml` recommend all methods are new in 2.2 and this gives a massive performance gain).
    
    cc @srowen @jkbradley 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    @mpjlu do you have link to the code for Method 1?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    I have submitted PR for ALS optimization with GEMM. and it is ready for review. 
    The performance is about 50% improvement comparing with the master method.
    https://github.com/apache/spark/pull/18624


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/76418/
    Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76418 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76418/testReport)** for PR 17742 at commit [`44a4f74`](https://github.com/apache/spark/commit/44a4f7483daf82f42e1b12c659384c955281068b).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/76180/
    Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r114726541
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -274,46 +275,62 @@ object MatrixFactorizationModel extends Loader[MatrixFactorizationModel] {
           srcFeatures: RDD[(Int, Array[Double])],
           dstFeatures: RDD[(Int, Array[Double])],
           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
    +    val srcBlocks = blockify(srcFeatures)
    +    val dstBlocks = blockify(dstFeatures)
    +    /**
    +     * The previous approach used for computing top-k recommendations aimed to group
    +     * individual factor vectors into blocks, so that Level 3 BLAS operations (gemm) could
    +     * be used for efficiency. However, this causes excessive GC pressure due to the large
    +     * arrays required for intermediate result storage, as well as a high sensitivity to the
    +     * block size used.
    +     * The following approach still groups factors into blocks, but instead computes the
    +     * top-k elements per block, using Level 1 BLAS (dot) and an efficient
    +     * BoundedPriorityQueue. This avoids any large intermediate data structures and results
    +     * in significantly reduced GC pressure as well as shuffle data, which far outweighs
    +     * any cost incurred from not using Level 3 BLAS operations.
    +     */
    +    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
    +      val pq = new BoundedPriorityQueue[(Int, Double)](n)(Ordering.by(_._2))
    +      srcIter.foreach { case (srcId, srcFactor) =>
    +        dstIter.foreach { case (dstId, dstFactor) =>
    +          /**
    +           * Compared with BLAS.dot, the hand-written version below is more efficient than a call
    +           * to the native BLAS backend and the same performance as the fallback F2jBLAS backend.
    +           */
    +          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
             }
    -        output.toSeq
    +        j += n
    +        pq.clear()
    +      }
    +      output.toSeq
         }
         ratings.topByKey(num)(Ordering.by(_._2))
       }
     
       /**
    -   * Blockifies features to use Level-3 BLAS.
    +   * Blockifies features to improve the efficiency of cartesian product
        */
       private def blockify(
    -      rank: Int,
    -      features: RDD[(Int, Array[Double])]): RDD[(Array[Int], DenseMatrix)] = {
    +      features: RDD[(Int, Array[Double])]): RDD[Seq[(Int, Array[Double])]] = {
    --- End diff --
    
    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 `recommendForAllUsers` and `recommendForAllItems` performance).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r114397947
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -276,44 +277,53 @@ 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
    +    /**
    +     * Use dot to replace blas 3 gemm is the key approach to improve efficiency.
    +     * By this change, we can get the topK elements of each block to reduce the GC time.
    +     * Comparing with BLAS.dot, hand-written dot is high efficiency.
    +     */
    +    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) =>
    +        val pq = new BoundedPriorityQueue[(Int, Double)](n)(Ordering.by(_._2))
    --- End diff --
    
    Here we are creating O(blocksize) PQ objects. But we could have only one, and reuse it. We could create it outside the `foreach` calls and call `clear` after using it. Not really sure if it will make a large difference, but could further reduce GC


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r114402475
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -276,44 +277,53 @@ 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
    +    /**
    +     * Use dot to replace blas 3 gemm is the key approach to improve efficiency.
    +     * By this change, we can get the topK elements of each block to reduce the GC time.
    +     * Comparing with BLAS.dot, hand-written dot is high efficiency.
    --- End diff --
    
    "Compared with BLAS.dot, the hand-written version below is more efficient than a call to the native BLAS backend and the same performance as the fallback `F2jBLAS` backend.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/76254/
    Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113428567
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -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))
    --- End diff --
    
    Here we can then use `dstFactor` instead


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113761692
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -277,39 +277,38 @@ 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 (users, items) =>
    -      val m = users.size
    -      val n = math.min(items.size, num)
    +    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
    -      users.foreach (user => {
    +      srcIter.foreach { case (srcId, srcFactor) =>
               def order(a: (Int, Double)) = a._2
               val pq: BoundedPriorityQueue[(Int, Double)] =
    --- End diff --
    
    We could remove the type sig from the `val` definition here to make it fit on one line


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113761881
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -277,39 +277,38 @@ 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 (users, items) =>
    -      val m = users.size
    -      val n = math.min(items.size, num)
    +    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
    -      users.foreach (user => {
    +      srcIter.foreach { case (srcId, srcFactor) =>
               def order(a: (Int, Double)) = a._2
               val pq: BoundedPriorityQueue[(Int, Double)] =
                 new BoundedPriorityQueue[(Int, Double)](n)(Ordering.by(order))
    --- End diff --
    
    I believe you can just do `Ordering.by(_._2)` without needing to define `def order(...` above


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by jtengyp <gi...@git.apache.org>.
Github user jtengyp commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    I did some tests with the PR.
    Here is the cluster configure:
        3 workers, each has 10 cores and 30G memory.
    With the netflix dataset (480,189 users and 17770 movies), the recommendProductsForUsers time reduces from 488.36s to 60.93s, 8x faster than the original method.
    
    With a larger dataset (3.29million users and 0.21 million products), the recommendProductsForUsers time reduces from 48h to 39min, 73x faster than the original method.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113427701
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -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 => {
    --- End diff --
    
    Similarly here: `dstIter.foreach { case (dstId, dstFactor) => `


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r114712062
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -274,46 +275,62 @@ object MatrixFactorizationModel extends Loader[MatrixFactorizationModel] {
           srcFeatures: RDD[(Int, Array[Double])],
           dstFeatures: RDD[(Int, Array[Double])],
           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
    +    val srcBlocks = blockify(srcFeatures)
    +    val dstBlocks = blockify(dstFeatures)
    +    /**
    +     * The previous approach used for computing top-k recommendations aimed to group
    +     * individual factor vectors into blocks, so that Level 3 BLAS operations (gemm) could
    +     * be used for efficiency. However, this causes excessive GC pressure due to the large
    +     * arrays required for intermediate result storage, as well as a high sensitivity to the
    +     * block size used.
    +     * The following approach still groups factors into blocks, but instead computes the
    +     * top-k elements per block, using Level 1 BLAS (dot) and an efficient
    +     * BoundedPriorityQueue. This avoids any large intermediate data structures and results
    +     * in significantly reduced GC pressure as well as shuffle data, which far outweighs
    +     * any cost incurred from not using Level 3 BLAS operations.
    +     */
    +    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
    +      val pq = new BoundedPriorityQueue[(Int, Double)](n)(Ordering.by(_._2))
    +      srcIter.foreach { case (srcId, srcFactor) =>
    +        dstIter.foreach { case (dstId, dstFactor) =>
    +          /**
    +           * Compared with BLAS.dot, the hand-written version below is more efficient than a call
    --- End diff --
    
    Let's match the form of comment from https://github.com/apache/spark/pull/17845/files#diff-be65dd1d6adc53138156641b610fcadaR403


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Thanks @MLnick. Please go ahead for ML API optimization.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-20446][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Interesting - I was working on something very similar - a rough draft of it is in a [branch](https://github.com/mlnick/spark/tree/SPARK-13857-als-parity-v3).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by sethah <gi...@git.apache.org>.
Github user sethah commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r114655727
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -274,46 +275,62 @@ object MatrixFactorizationModel extends Loader[MatrixFactorizationModel] {
           srcFeatures: RDD[(Int, Array[Double])],
           dstFeatures: RDD[(Int, Array[Double])],
           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
    +    val srcBlocks = blockify(srcFeatures)
    +    val dstBlocks = blockify(dstFeatures)
    +    /**
    +     * The previous approach used for computing top-k recommendations aimed to group
    +     * individual factor vectors into blocks, so that Level 3 BLAS operations (gemm) could
    +     * be used for efficiency. However, this causes excessive GC pressure due to the large
    +     * arrays required for intermediate result storage, as well as a high sensitivity to the
    +     * block size used.
    +     * The following approach still groups factors into blocks, but instead computes the
    +     * top-k elements per block, using Level 1 BLAS (dot) and an efficient
    --- End diff --
    
    but it doesn't use level 1 blas, does it?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    I have rewritten recommendForAll with BLAS GEMM, and get about 20%-30% performance improvement.
    https://issues.apache.org/jira/browse/SPARK-21389


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by srowen <gi...@git.apache.org>.
Github user srowen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113860816
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -276,44 +277,53 @@ 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
    +    /**
    +     * Use dot to replace blas 3 gemm is the key approach to improve efficiency.
    +     * By this change, we can get the topK elements of each block to reduce the GC time.
    +     * Comparing with BLAS.dot, hand-written dot is high efficiency.
    +     */
    +    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) =>
    +          val pq = new BoundedPriorityQueue[(Int, Double)](n)(Ordering.by(_._2))
    --- End diff --
    
    Nit: there are several 4-space indents here that should be 2


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/76176/
    Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76258 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76258/testReport)** for PR 17742 at commit [`8eab55b`](https://github.com/apache/spark/commit/8eab55bccd51706d45e0ccb2281114df4310899c).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    ** The most optimized version would be doing a quickselect on each row and select top k.
    ** An easy-to-implement version would be:
    I test both of the methods, the best performance is about 50% this PR.
    Welcome to do more test.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    @mpjlu Could you try not linking with native BLAS or system BLAS in your test? Just let it fallback to f2j BLAS. I can do some tests on my end too.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76254 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76254/testReport)** for PR 17742 at commit [`206a023`](https://github.com/apache/spark/commit/206a023433805e8d55b0cb30eebde130b4245bf9).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r114401026
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -276,44 +277,53 @@ 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
    +    /**
    +     * Use dot to replace blas 3 gemm is the key approach to improve efficiency.
    --- End diff --
    
    I think we should say something like:
    
    > The previous approach used for computing top-k recommendations aimed to group individual factor vectors into blocks, so that Level 3 BLAS operations (gemm) could be used for efficiency. However, this causes excessive GC pressure due to the large arrays required for intermediate result storage, as well as a high sensitivity to the block size used.
    >
    > The following approach still groups factors into blocks, but instead computes the top-k elements per block, using Level 1 BLAS (dot) and an efficient BoundedPriorityQueue. This avoids any large intermediate datastructures and results in significantly reduced GC pressure as well as shuffle data, which far outweighs any cost incurred from not using Level 3 BLAS operations.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    LGTM - I think #17845 and this should go in 2.2. Let me know any objections @srowen @jkbradley 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by viirya <gi...@git.apache.org>.
Github user viirya commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    This LGTM.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    I don't think we should use BLAS 3 here, because no matter use output or not here, you need a big buffer to save the BLAS result. That still cause GC problem. 
    I also want to test: build a handwritten native gemm,  which not returns C (C=A*B), but only return the topK elements of each row.  This maybe better performance than current solution.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r114579054
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -276,44 +277,53 @@ 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
    +    /**
    +     * Use dot to replace blas 3 gemm is the key approach to improve efficiency.
    +     * By this change, we can get the topK elements of each block to reduce the GC time.
    +     * Comparing with BLAS.dot, hand-written dot is high efficiency.
    +     */
    +    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) =>
    +        val pq = new BoundedPriorityQueue[(Int, Double)](n)(Ordering.by(_._2))
    --- End diff --
    
    Thanks, I also think we should move PQ outside foreach, though I didn't find large difference with my test. 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    I think the problem is not BLAS-3 ops, nor the 256MB total memory. The `val output = new Array[(Int, (Int, Double))](m * n)` is not specialized. Each element holds two references. If `m=4096` and `n=4096`, in total we have 33.5 million objects, which caused GC. The implementation in this PR changed `n` to `k`, which significantly reduced the total number of temp objects. But it doesn't mean that we should drop BLAS-3.
    
    @mpjlu Could you test the following?
    
    * change block size to 2048, which reduced the max possible 
    * After `val ratings = srcFactors.transpose.multiply(dstFactors)`, do not construct `output`. There are two options:
    ** The most optimized version would be doing a quickselect on each row and select top k.
    ** An easy-to-implement version would be:
    
    ~~~scala
    Iterator.range(0, m).flatMap { i => 
      Iterator.range(0, n).map { j =>
        (srcIds(i), (dstIds(j), ratings(i, j)))
     }
    }
    ~~~
    
    The second option is just a quick test, scarifying some performance. The temp objects created this way have very short life, and GC should be able to handle it. Then very likely we don't need to do top-k inside ALS, because the `topByKey` implementation is doing the same: https://github.com/apache/spark/blob/master/mllib/src/main/scala/org/apache/spark/mllib/rdd/MLPairRDDFunctions.scala#L42.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    @jkbradley good point. In fact thinking about it again now, it's best to simply use `F2jBLAS` since the performance is the same as the hand-written version. That's actually what we already do internally for MLlib's `BLAS.dot` for `Vector`.
    
    I've created [SPARK-20677](https://issues.apache.org/jira/browse/SPARK-20677) to do that places once they're merged.
    



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    It's true I think my native BLAS is not working will have to check - but yeah 1.5-2x matches what I've seen in my comparisons


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Merged to master/branch-2.2.
    
    Thanks @mpjlu for the awesome performance improvement!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    F2Jblas is faster than MKL blas. The following test is based on F2jBLAS.
    Method 1: BLAS 3 + quickselect on each row and select top k.
    Method 2: this PR
    BLOCK size: 256   512  1024  2048  4096  8192
    Method 1:   48s    41s   36s    88s     NA     NA  
    Method 2:   NA     NA   34s    35s      34s     34s 
    
    3 workers: each worker 40 cores, each worker 120G memory, each worker 1 executor.
    The Data: User 480,000, and Item 17,000 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113427796
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -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)
    --- End diff --
    
    Then we can have `rate += srcFactor(k) * dstFactor(k)`
    
    Also, can we call it `score` or `prediction` rather than `rate`?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Test FAILed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/76260/
    Test FAILed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113428282
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -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 => {
    --- End diff --
    
    Will there be performance benefit to using `while` loop here vs `foreach`?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113427392
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -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) =>
    --- End diff --
    
    Put case statement on previous line: `flatMap { case (... =>`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-20446][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Merged build finished. Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/76261/
    Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76260 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76260/testReport)** for PR 17742 at commit [`a5261b7`](https://github.com/apache/spark/commit/a5261b737ff046ff719c219b94af0f1dff5bf2e2).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    @MLnick , for rank is 200, the F2jblas dot is about the same performance  as handwritten dot. 
    Both of them is much better than native dot.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113428150
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -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())
    --- End diff --
    
    Elsewhere when using e.g. `queue.toArray.sorted` the ordering is reversed. Will this be correct?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Another case
    3 workers\uff0c each 40 cores, each 196G memory, each 1 executor.
    Data Size: user 480,000, item 17,000 
    recommendProductsForUsers with blockSize 4096 is about 34s


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113428386
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -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) {
    --- End diff --
    
    Space here: `while (`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Merged build finished. Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113428354
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -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) {
    --- End diff --
    
    space here: `while (`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by viirya <gi...@git.apache.org>.
Github user viirya commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r114726002
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -274,46 +275,62 @@ object MatrixFactorizationModel extends Loader[MatrixFactorizationModel] {
           srcFeatures: RDD[(Int, Array[Double])],
           dstFeatures: RDD[(Int, Array[Double])],
           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
    +    val srcBlocks = blockify(srcFeatures)
    +    val dstBlocks = blockify(dstFeatures)
    +    /**
    +     * The previous approach used for computing top-k recommendations aimed to group
    +     * individual factor vectors into blocks, so that Level 3 BLAS operations (gemm) could
    +     * be used for efficiency. However, this causes excessive GC pressure due to the large
    +     * arrays required for intermediate result storage, as well as a high sensitivity to the
    +     * block size used.
    +     * The following approach still groups factors into blocks, but instead computes the
    +     * top-k elements per block, using Level 1 BLAS (dot) and an efficient
    +     * BoundedPriorityQueue. This avoids any large intermediate data structures and results
    +     * in significantly reduced GC pressure as well as shuffle data, which far outweighs
    +     * any cost incurred from not using Level 3 BLAS operations.
    +     */
    +    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
    +      val pq = new BoundedPriorityQueue[(Int, Double)](n)(Ordering.by(_._2))
    +      srcIter.foreach { case (srcId, srcFactor) =>
    +        dstIter.foreach { case (dstId, dstFactor) =>
    +          /**
    +           * Compared with BLAS.dot, the hand-written version below is more efficient than a call
    +           * to the native BLAS backend and the same performance as the fallback F2jBLAS backend.
    +           */
    +          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
             }
    -        output.toSeq
    +        j += n
    +        pq.clear()
    +      }
    +      output.toSeq
         }
         ratings.topByKey(num)(Ordering.by(_._2))
       }
     
       /**
    -   * Blockifies features to use Level-3 BLAS.
    +   * Blockifies features to improve the efficiency of cartesian product
        */
       private def blockify(
    -      rank: Int,
    -      features: RDD[(Int, Array[Double])]): RDD[(Array[Int], DenseMatrix)] = {
    +      features: RDD[(Int, Array[Double])]): RDD[Seq[(Int, Array[Double])]] = {
    --- End diff --
    
    With this change, it seems to me that the performance can be less sensitive to `blockSize`.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r114706645
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -274,46 +275,62 @@ object MatrixFactorizationModel extends Loader[MatrixFactorizationModel] {
           srcFeatures: RDD[(Int, Array[Double])],
           dstFeatures: RDD[(Int, Array[Double])],
           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
    +    val srcBlocks = blockify(srcFeatures)
    +    val dstBlocks = blockify(dstFeatures)
    +    /**
    +     * The previous approach used for computing top-k recommendations aimed to group
    +     * individual factor vectors into blocks, so that Level 3 BLAS operations (gemm) could
    +     * be used for efficiency. However, this causes excessive GC pressure due to the large
    +     * arrays required for intermediate result storage, as well as a high sensitivity to the
    +     * block size used.
    +     * The following approach still groups factors into blocks, but instead computes the
    +     * top-k elements per block, using Level 1 BLAS (dot) and an efficient
    --- End diff --
    
    Well it is a "BLAS 1" operation (dot product) - but technically not strictly BLAS itself no. 
    
    How about "... using dot product instead of gemm and an efficient ..."


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76455 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76455/testReport)** for PR 17742 at commit [`17df4cf`](https://github.com/apache/spark/commit/17df4cf8aabd5f4e28551e31d43b0231c1e68676).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113762344
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -277,39 +277,38 @@ 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 (users, items) =>
    -      val m = users.size
    -      val n = math.min(items.size, num)
    +    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
    -      users.foreach (user => {
    +      srcIter.foreach { case (srcId, srcFactor) =>
               def order(a: (Int, Double)) = a._2
               val pq: BoundedPriorityQueue[(Int, Double)] =
                 new BoundedPriorityQueue[(Int, Double)](n)(Ordering.by(order))
    -          items.foreach (item => {
    +          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)
    --- End diff --
    
    We can perhaps say here instead "The below code is equivalent to `val score = blas.ddot(rank, srcFactor, 1, dstFactor, 1)`"


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Hi @MLnick , The new test results are:
    3 worker, each 10 cores, each 30G memory, each 1 executor.
    Data Size: user 3,290,000, item 200,000.
    recommendProductsForUsers with blockSize 4096 is about 37min, 
    



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
       val srcBlocks = blockify(rank, srcFeatures)
        val dstBlocks = blockify(rank, dstFeatures)
        val pq = new BoundedPriorityQueue[(Int, Double)](num)(Ordering.by(_._2))
        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 * num)
          var i = 0
          var j = 0
          while (i < ratings.numRows) {
            var k = 0
            while (k < ratings.numCols) {
              pq += k -> ratings(i, k)
              k += 1
            }
            pq.foreach { case (dstId, score) =>
              output(j) = (srcIds(i), (dstId, score))
              j += 1
            }
            i += 1
            pq.clear
          }
        output.toSeq
        }
    
        val userRec = ratings.topByKey(num)(Ordering.by(_._2))
    Code is like this. Thanks.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76261 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76261/testReport)** for PR 17742 at commit [`a5261b7`](https://github.com/apache/spark/commit/a5261b737ff046ff719c219b94af0f1dff5bf2e2).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76254 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76254/testReport)** for PR 17742 at commit [`206a023`](https://github.com/apache/spark/commit/206a023433805e8d55b0cb30eebde130b4245bf9).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113444550
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -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 => {
    --- End diff --
    
    I will test while here, thanks.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Merged build finished. Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-20446][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/76100/
    Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113762883
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -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) =>
    --- End diff --
    
    I'd like to more detail to the doc string comment for this method to explain the approach used for efficiency.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Merged build finished. Test FAILed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r114711912
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -274,46 +275,62 @@ object MatrixFactorizationModel extends Loader[MatrixFactorizationModel] {
           srcFeatures: RDD[(Int, Array[Double])],
           dstFeatures: RDD[(Int, Array[Double])],
           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
    +    val srcBlocks = blockify(srcFeatures)
    +    val dstBlocks = blockify(dstFeatures)
    +    /**
    +     * The previous approach used for computing top-k recommendations aimed to group
    +     * individual factor vectors into blocks, so that Level 3 BLAS operations (gemm) could
    +     * be used for efficiency. However, this causes excessive GC pressure due to the large
    +     * arrays required for intermediate result storage, as well as a high sensitivity to the
    +     * block size used.
    +     * The following approach still groups factors into blocks, but instead computes the
    +     * top-k elements per block, using Level 1 BLAS (dot) and an efficient
    +     * BoundedPriorityQueue. This avoids any large intermediate data structures and results
    +     * in significantly reduced GC pressure as well as shuffle data, which far outweighs
    +     * any cost incurred from not using Level 3 BLAS operations.
    +     */
    +    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
    +      val pq = new BoundedPriorityQueue[(Int, Double)](n)(Ordering.by(_._2))
    +      srcIter.foreach { case (srcId, srcFactor) =>
    +        dstIter.foreach { case (dstId, dstFactor) =>
    +          /**
    +           * Compared with BLAS.dot, the hand-written version below is more efficient than a call
    +           * to the native BLAS backend and the same performance as the fallback F2jBLAS backend.
    +           */
    +          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
             }
    -        output.toSeq
    +        j += n
    +        pq.clear()
    +      }
    +      output.toSeq
         }
         ratings.topByKey(num)(Ordering.by(_._2))
       }
     
       /**
    -   * Blockifies features to use Level-3 BLAS.
    +   * Blockifies features to improve the efficiency of cartesian product
        */
       private def blockify(
    -      rank: Int,
    -      features: RDD[(Int, Array[Double])]): RDD[(Array[Int], DenseMatrix)] = {
    +      features: RDD[(Int, Array[Double])]): RDD[Seq[(Int, Array[Double])]] = {
    --- End diff --
    
    Let's match what I've done in https://github.com/apache/spark/pull/17845/files#diff-be65dd1d6adc53138156641b610fcadaR440 - i.e. `blockSize` as an argument, with a `TODO: SPARK-20443 - expose blockSize as a param?`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    I changed the commit message to drop the `[ML]` by the way


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113427628
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -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 => {
    --- End diff --
    
    `srcIter.foreach { case (srcId, srcFactor) => `


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Hi @MLnick, I will be on vacation next week.
    If you have time to create an ML optimization follow up PR. I am ok. Otherwise, I will submit the follow up PR after my 1 week vacation.
    Thanks very much.  



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113427565
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -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) =>
    --- End diff --
    
    Prefer `case (srcIter, dstIter)` rather than users / items (as they can be swapped depending on which recommendation method is being called).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113428600
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -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())
    --- End diff --
    
    And here `srcFactor` instead


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-20446][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/76111/
    Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76180 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76180/testReport)** for PR 17742 at commit [`2fd97a0`](https://github.com/apache/spark/commit/2fd97a043c9ca72a04012a4b4cef06ecc023917c).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    @mpjlu I should be able to put something together for the ML API equivalent to this code - in fact I already have a version close to ready to go :)
    
    Thanks for this PR and major performance improvement! I will take a final pass over this shortly.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Thanks @mengxr , glad to meet you here. I am Meng Peng.
    I have tested different blockSize, see https://issues.apache.org/jira/browse/SPARK-20443
    I will test the other methods you mentioned. 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-20446][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76100 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76100/testReport)** for PR 17742 at commit [`14cdbf6`](https://github.com/apache/spark/commit/14cdbf63e79ebcf2d1207c79b0b4ba73e15729b2).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    @mpjlu yeah we can do the ML version in a follow up PR that is ok (I can help if needed).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    BLAS3 with still keeping the output size as `n x m` rather than `n x k` results in massively more shuffle data - I don't think any solution based on exploding the intermediate data so much can be as efficient as this. Since for `k=10` it's ~80k little objects per block vs ~33 million... 
    
    I had a version using BLAS 3 followed by a sort per row (see https://issues.apache.org/jira/browse/SPARK-11968 for branch link and test details). For MLLIB it was slower than this approach by a factor of 1.5x. I just re-tested for ML and it it is 56s vs 16s for this approach, so really significantly slower.
    
    Comparatively, both approaches created the intermediate `output` objects (but only `n x k` size). Certainly that part could perhaps be further optimized. However, the BLAS3 approach still had around 20% GC time vs around 12% from this approach. Each gemm does indeed require a large intermediate array and this seems to cause additional GC time (whether directly or indirectly).
    
    Even without that this approach is a lot faster than `gemm` and sort for the top-k by row. I'm sure the per-row top-k can be made a lot more efficient and that is worth exploring (though frankly I am doubtful it will result in that much more gain over this approach, relative to the code complexity it will introduce). The small object GC can perhaps be improved with the iterator approach and avoiding creating the `output` array (that may be good for another 5% or so perhaps?) - this applies to whatever approach is used.
    



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113445477
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -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())
    --- End diff --
    
    Here, the ordering is not important. This is just one group TopK,  we will get the real TopK in the last.
    I have tested queue.toArray here, no performance benefit.  


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r114712113
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -274,46 +275,62 @@ object MatrixFactorizationModel extends Loader[MatrixFactorizationModel] {
           srcFeatures: RDD[(Int, Array[Double])],
           dstFeatures: RDD[(Int, Array[Double])],
           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
    +    val srcBlocks = blockify(srcFeatures)
    +    val dstBlocks = blockify(dstFeatures)
    +    /**
    +     * The previous approach used for computing top-k recommendations aimed to group
    +     * individual factor vectors into blocks, so that Level 3 BLAS operations (gemm) could
    +     * be used for efficiency. However, this causes excessive GC pressure due to the large
    +     * arrays required for intermediate result storage, as well as a high sensitivity to the
    +     * block size used.
    +     * The following approach still groups factors into blocks, but instead computes the
    +     * top-k elements per block, using Level 1 BLAS (dot) and an efficient
    --- End diff --
    
    In https://github.com/apache/spark/pull/17845/files#diff-be65dd1d6adc53138156641b610fcadaR366 I said `... using a simple dot product (instead of gemm) and an efficient [[BoundedPriorityQueue]].`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r113762745
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -297,23 +319,10 @@ object MatrixFactorizationModel extends Loader[MatrixFactorizationModel] {
        */
    --- End diff --
    
    We should adjust the comment here as we're not using Level-3 BLAS any more.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Merged build finished. Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    I not validate whether this code is right. just test performance.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-20446][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76111 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76111/testReport)** for PR 17742 at commit [`f607e6c`](https://github.com/apache/spark/commit/f607e6c441559c5d48cd4c9dac58960b98c2198a).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-20446][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76100 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76100/testReport)** for PR 17742 at commit [`14cdbf6`](https://github.com/apache/spark/commit/14cdbf63e79ebcf2d1207c79b0b4ba73e15729b2).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    @mpjlu I did just think about the hand-written `dot` for larger `rank` (say 100-200 which is not so common as say 10-50 range for ALS, but still can occur). Did you compare performance for that range of `rank` at all?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r114712002
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -274,46 +275,62 @@ object MatrixFactorizationModel extends Loader[MatrixFactorizationModel] {
           srcFeatures: RDD[(Int, Array[Double])],
           dstFeatures: RDD[(Int, Array[Double])],
           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
    +    val srcBlocks = blockify(srcFeatures)
    +    val dstBlocks = blockify(dstFeatures)
    +    /**
    +     * The previous approach used for computing top-k recommendations aimed to group
    +     * individual factor vectors into blocks, so that Level 3 BLAS operations (gemm) could
    +     * be used for efficiency. However, this causes excessive GC pressure due to the large
    +     * arrays required for intermediate result storage, as well as a high sensitivity to the
    +     * block size used.
    +     * The following approach still groups factors into blocks, but instead computes the
    +     * top-k elements per block, using Level 1 BLAS (dot) and an efficient
    +     * BoundedPriorityQueue. This avoids any large intermediate data structures and results
    +     * in significantly reduced GC pressure as well as shuffle data, which far outweighs
    +     * any cost incurred from not using Level 3 BLAS operations.
    +     */
    +    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
    +      val pq = new BoundedPriorityQueue[(Int, Double)](n)(Ordering.by(_._2))
    +      srcIter.foreach { case (srcId, srcFactor) =>
    +        dstIter.foreach { case (dstId, dstFactor) =>
    +          /**
    +           * Compared with BLAS.dot, the hand-written version below is more efficient than a call
    +           * to the native BLAS backend and the same performance as the fallback F2jBLAS backend.
    +           */
    +          var score: Double = 0
    +          var k = 0
    +          while (k < rank) {
    +            score += srcFactor(k) * dstFactor(k)
    +            k += 1
    +          }
    +          pq += ((dstId, score))
    --- End diff --
    
    `pq += dstId -> score` as per https://github.com/apache/spark/pull/17845#discussion_r114658306


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Ok - I don't expect it would make a substantial difference for dot unless
    the vector size was very large.
    
    Let's check it but we could proceed to merge this in the meantime.
    
    
    On Thu, 4 May 2017 at 15:58, Meng, Peng <no...@github.com> wrote:
    
    > @MLnick <https://github.com/MLnick> , I did not test rank in range 100 to
    > 200. I am on vacation and my colleague is doing this test, I will update
    > this result soon.
    > Thanks.
    >
    > —
    > You are receiving this because you were mentioned.
    > Reply to this email directly, view it on GitHub
    > <https://github.com/apache/spark/pull/17742#issuecomment-299192898>, or mute
    > the thread
    > <https://github.com/notifications/unsubscribe-auth/AA_SB_R-VfrZJtchobPNOAneGs-H613Oks5r2dmVgaJpZM4NF_tV>
    > .
    >



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76418 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76418/testReport)** for PR 17742 at commit [`44a4f74`](https://github.com/apache/spark/commit/44a4f7483daf82f42e1b12c659384c955281068b).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/spark/pull/17742


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76176 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76176/testReport)** for PR 17742 at commit [`ae03124`](https://github.com/apache/spark/commit/ae0312491e3d5a193626d4de1db7d7a101c7482d).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by jkbradley <gi...@git.apache.org>.
Github user jkbradley commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Thanks for doing this!  The logic looks reasonable to me.
    
    One comment: I'm not a fan of custom BLAS implementations scattered throughout MLlib.  I'm OK if people want to merge this now, but could you please follow up by putting the dot as a private API in BLAS.scala and adding unit tests?
    
    @mpjlu You can remove the "[ML]" tag from the PR title since @MLnick is addressing that API separately.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Thanks, I will do some test based on BLAS 3. 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76261 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76261/testReport)** for PR 17742 at commit [`a5261b7`](https://github.com/apache/spark/commit/a5261b737ff046ff719c219b94af0f1dff5bf2e2).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by mpjlu <gi...@git.apache.org>.
Github user mpjlu commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    retest please


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76176 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76176/testReport)** for PR 17742 at commit [`ae03124`](https://github.com/apache/spark/commit/ae0312491e3d5a193626d4de1db7d7a101c7482d).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    **[Test build #76258 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/76258/testReport)** for PR 17742 at commit [`8eab55b`](https://github.com/apache/spark/commit/8eab55bccd51706d45e0ccb2281114df4310899c).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recommendForA...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17742
  
    Merged build finished. Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17742: [Spark-11968][ML][MLLIB]Optimize MLLIB ALS recomm...

Posted by MLnick <gi...@git.apache.org>.
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17742#discussion_r114395288
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala ---
    @@ -276,44 +277,53 @@ 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
    +    /**
    +     * Use dot to replace blas 3 gemm is the key approach to improve efficiency.
    +     * By this change, we can get the topK elements of each block to reduce the GC time.
    +     * Comparing with BLAS.dot, hand-written dot is high efficiency.
    +     */
    +    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) =>
    +        val pq = new BoundedPriorityQueue[(Int, Double)](n)(Ordering.by(_._2))
    +        dstIter.foreach { case (dstId, dstFactor) =>
    +          /**
    +           * The below code is equivalent to
    +           * val score = blas.ddot(rank, srcFactor, 1, dstFactor, 1)
    +           */
    +          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
             }
    -        output.toSeq
    +        j += n
    +      }
    +      output.toSeq
         }
         ratings.topByKey(num)(Ordering.by(_._2))
       }
     
       /**
    -   * Blockifies features to use Level-3 BLAS.
    +   * Blockifies features to improve the efficiency of cartesian product
        */
       private def blockify(
           rank: Int,
    -      features: RDD[(Int, Array[Double])]): RDD[(Array[Int], DenseMatrix)] = {
    +      features: RDD[(Int, Array[Double])]): RDD[Seq[(Int, Array[Double])]] = {
    --- End diff --
    
    We can remove `rank` argument here


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org