You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by ru...@apache.org on 2025/03/06 11:15:21 UTC

(spark) branch master updated: [SPARK-51379][ML] Move treeAggregate's final aggregation from driver to executor

This is an automated email from the ASF dual-hosted git repository.

ruifengz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new 8d2bf0bb0c53 [SPARK-51379][ML] Move treeAggregate's final aggregation from driver to executor
8d2bf0bb0c53 is described below

commit 8d2bf0bb0c5302d042e23648a7744d3db4bba0c0
Author: Ruifeng Zheng <ru...@apache.org>
AuthorDate: Thu Mar 6 19:15:03 2025 +0800

    [SPARK-51379][ML] Move treeAggregate's final aggregation from driver to executor
    
    ### What changes were proposed in this pull request?
    Move treeAggregate's final aggregation from driver to executor.
    
    https://github.com/apache/spark/commit/ee20fbb3dc8594e2c70758c0e2d140f50108a6c5 introduced an optimization that:
    
    > Move final iteration of aggregation of RDD.treeAggregate to an executor with one partition and fetch that result to the driver
    
    This PR tries to apply this optimization so that less memory is required for ML's treeAggregate.
    
    ### Why are the changes needed?
    to optimize driver peak memory usage
    
    ### Does this PR introduce _any_ user-facing change?
    no
    
    ### How was this patch tested?
    ci and manually check
    
    preparing data:
    ```
    df = spark.read.format("libsvm").load("data/mllib/sample_libsvm_data.txt")
    for i in range(10):
        df = df.union(df)
    
    df.count()
    df.repartition(1024).write.mode("overwrite").parquet("/tmp/test_data")
    ```
    
    training a lr
    ```
    from pyspark.ml.classification import *
    df = spark.read.parquet("/tmp/test_data")
    lr = LogisticRegression()
    model = lr.fit(df)
    ```
    
    before:
    ![image](https://github.com/user-attachments/assets/84e72882-667b-40fd-9f12-ea4d62cc8978)
    
    after:
    ![image](https://github.com/user-attachments/assets/917e9634-2a90-4ca8-9621-02aac561b3e1)
    
    In each iteration, the data sent to driver is reduced from 136.1 KiB to 21.3 KiB
    
    </body></html>
    
    ### Was this patch authored or co-authored using generative AI tooling?
    no
    
    Closes #50142 from zhengruifeng/ml_final_agg.
    
    Authored-by: Ruifeng Zheng <ru...@apache.org>
    Signed-off-by: Ruifeng Zheng <ru...@apache.org>
---
 .../spark/ml/optim/WeightedLeastSquares.scala      |  7 +++-
 .../spark/ml/optim/loss/RDDLossFunction.scala      |  2 +-
 .../org/apache/spark/ml/stat/Summarizer.scala      | 12 +++---
 .../spark/mllib/clustering/GaussianMixture.scala   |  7 +++-
 .../spark/mllib/clustering/LDAOptimizer.scala      |  4 +-
 .../spark/mllib/evaluation/MultilabelMetrics.scala | 12 +++---
 .../scala/org/apache/spark/mllib/feature/IDF.scala | 14 ++++---
 .../spark/mllib/linalg/distributed/RowMatrix.scala | 46 +++++++++++++++-------
 .../spark/mllib/optimization/GradientDescent.scala | 11 ++++--
 .../apache/spark/mllib/optimization/LBFGS.scala    |  3 +-
 .../org/apache/spark/mllib/stat/Statistics.scala   | 11 ++++--
 11 files changed, 85 insertions(+), 44 deletions(-)

diff --git a/mllib/src/main/scala/org/apache/spark/ml/optim/WeightedLeastSquares.scala b/mllib/src/main/scala/org/apache/spark/ml/optim/WeightedLeastSquares.scala
index eff100cc3ae3..31db7b18efb3 100644
--- a/mllib/src/main/scala/org/apache/spark/ml/optim/WeightedLeastSquares.scala
+++ b/mllib/src/main/scala/org/apache/spark/ml/optim/WeightedLeastSquares.scala
@@ -107,7 +107,12 @@ private[ml] class WeightedLeastSquares(
       instr.logWarning("regParam is zero, which might cause numerical instability and overfitting.")
     }
 
-    val summary = instances.treeAggregate(new Aggregator)(_.add(_), _.merge(_), depth)
+    val summary = instances.treeAggregate[Aggregator](
+      zeroValue = new Aggregator,
+      seqOp = (agg: Aggregator, x: Instance) => agg.add(x),
+      combOp = (agg1: Aggregator, agg2: Aggregator) => agg1.merge(agg2),
+      depth = depth,
+      finalAggregateOnExecutor = true)
     summary.validate()
     instr.logInfo(log"Number of instances: ${MDC(COUNT, summary.count)}.")
     val k = if (fitIntercept) summary.k + 1 else summary.k
diff --git a/mllib/src/main/scala/org/apache/spark/ml/optim/loss/RDDLossFunction.scala b/mllib/src/main/scala/org/apache/spark/ml/optim/loss/RDDLossFunction.scala
index 84977a6916f0..7800e9807090 100644
--- a/mllib/src/main/scala/org/apache/spark/ml/optim/loss/RDDLossFunction.scala
+++ b/mllib/src/main/scala/org/apache/spark/ml/optim/loss/RDDLossFunction.scala
@@ -58,7 +58,7 @@ private[ml] class RDDLossFunction[
     val thisAgg = getAggregator(bcCoefficients)
     val seqOp = (agg: Agg, x: T) => agg.add(x)
     val combOp = (agg1: Agg, agg2: Agg) => agg1.merge(agg2)
-    val newAgg = instances.treeAggregate(thisAgg)(seqOp, combOp, aggregationDepth)
+    val newAgg = instances.treeAggregate(thisAgg, seqOp, combOp, aggregationDepth, true)
     val gradient = newAgg.gradient
     val regLoss = regularization.map { regFun =>
       val (regLoss, regGradient) = regFun.calculate(Vectors.fromBreeze(coefficients))
diff --git a/mllib/src/main/scala/org/apache/spark/ml/stat/Summarizer.scala b/mllib/src/main/scala/org/apache/spark/ml/stat/Summarizer.scala
index f4718b203431..5a136b7578f3 100644
--- a/mllib/src/main/scala/org/apache/spark/ml/stat/Summarizer.scala
+++ b/mllib/src/main/scala/org/apache/spark/ml/stat/Summarizer.scala
@@ -217,15 +217,16 @@ object Summarizer extends Logging {
       aggregationDepth: Int = 2,
       requested: Seq[String] = Seq("mean", "std", "count")) = {
     instances.treeAggregate(
-      (Summarizer.createSummarizerBuffer(requested: _*),
-        Summarizer.createSummarizerBuffer("mean", "std", "count")))(
+      zeroValue = (Summarizer.createSummarizerBuffer(requested: _*),
+        Summarizer.createSummarizerBuffer("mean", "std", "count")),
       seqOp = (c: (SummarizerBuffer, SummarizerBuffer), instance: Instance) =>
         (c._1.add(instance.features, instance.weight),
           c._2.add(Vectors.dense(instance.label), instance.weight)),
       combOp = (c1: (SummarizerBuffer, SummarizerBuffer),
                 c2: (SummarizerBuffer, SummarizerBuffer)) =>
         (c1._1.merge(c2._1), c1._2.merge(c2._2)),
-      depth = aggregationDepth
+      depth = aggregationDepth,
+      finalAggregateOnExecutor = true
     )
   }
 
@@ -235,13 +236,14 @@ object Summarizer extends Logging {
       aggregationDepth: Int = 2,
       requested: Seq[String] = Seq("mean", "std", "count")) = {
     instances.treeAggregate(
-      (Summarizer.createSummarizerBuffer(requested: _*), new MultiClassSummarizer))(
+      zeroValue = (Summarizer.createSummarizerBuffer(requested: _*), new MultiClassSummarizer),
       seqOp = (c: (SummarizerBuffer, MultiClassSummarizer), instance: Instance) =>
         (c._1.add(instance.features, instance.weight), c._2.add(instance.label, instance.weight)),
       combOp = (c1: (SummarizerBuffer, MultiClassSummarizer),
                 c2: (SummarizerBuffer, MultiClassSummarizer)) =>
         (c1._1.merge(c2._1), c1._2.merge(c2._2)),
-      depth = aggregationDepth
+      depth = aggregationDepth,
+      finalAggregateOnExecutor = true
     )
   }
 }
diff --git a/mllib/src/main/scala/org/apache/spark/mllib/clustering/GaussianMixture.scala b/mllib/src/main/scala/org/apache/spark/mllib/clustering/GaussianMixture.scala
index eacac07e8260..562e5b3995cf 100644
--- a/mllib/src/main/scala/org/apache/spark/mllib/clustering/GaussianMixture.scala
+++ b/mllib/src/main/scala/org/apache/spark/mllib/clustering/GaussianMixture.scala
@@ -201,7 +201,12 @@ class GaussianMixture private (
       val compute = sc.broadcast(ExpectationSum.add(weights, gaussians)_)
 
       // aggregate the cluster contribution for all sample points
-      val sums = breezeData.treeAggregate(ExpectationSum.zero(k, d))(compute.value, _ += _)
+      val sums = breezeData.treeAggregate[ExpectationSum](
+        zeroValue = ExpectationSum.zero(k, d),
+        seqOp = (agg: ExpectationSum, v: BV[Double]) => compute.value(agg, v),
+        combOp = (agg1: ExpectationSum, agg2: ExpectationSum) => agg1 += agg2,
+        depth = 2,
+        finalAggregateOnExecutor = true)
 
       // Create new distributions based on the partial assignments
       // (often referred to as the "M" step in literature)
diff --git a/mllib/src/main/scala/org/apache/spark/mllib/clustering/LDAOptimizer.scala b/mllib/src/main/scala/org/apache/spark/mllib/clustering/LDAOptimizer.scala
index 234ecbc46063..e4dd53ec9cce 100644
--- a/mllib/src/main/scala/org/apache/spark/mllib/clustering/LDAOptimizer.scala
+++ b/mllib/src/main/scala/org/apache/spark/mllib/clustering/LDAOptimizer.scala
@@ -507,8 +507,8 @@ final class OnlineLDAOptimizer extends LDAOptimizer with Logging {
     }
 
     val (statsSum: BDM[Double], logphatOption: Option[BDV[Double]], nonEmptyDocsN: Long) = stats
-      .treeAggregate((null.asInstanceOf[BDM[Double]], logphatPartOptionBase(), 0L))(
-        elementWiseSum, elementWiseSum
+      .treeAggregate((null.asInstanceOf[BDM[Double]], logphatPartOptionBase(), 0L),
+        elementWiseSum, elementWiseSum, 2, true
       )
 
     expElogbetaBc.destroy()
diff --git a/mllib/src/main/scala/org/apache/spark/mllib/evaluation/MultilabelMetrics.scala b/mllib/src/main/scala/org/apache/spark/mllib/evaluation/MultilabelMetrics.scala
index 7caf36e85c79..79dcafe33123 100644
--- a/mllib/src/main/scala/org/apache/spark/mllib/evaluation/MultilabelMetrics.scala
+++ b/mllib/src/main/scala/org/apache/spark/mllib/evaluation/MultilabelMetrics.scala
@@ -45,11 +45,13 @@ class MultilabelMetrics @Since("1.2.0") (predictionAndLabels: RDD[(Array[Double]
    * and labels on one pass.
    */
   private val summary: MultilabelSummarizer = {
-    predictionAndLabels
-      .treeAggregate(new MultilabelSummarizer)(
-        (summary, sample) => summary.add(sample._1, sample._2),
-        (sum1, sum2) => sum1.merge(sum2)
-      )
+    predictionAndLabels.treeAggregate[MultilabelSummarizer](
+      zeroValue = new MultilabelSummarizer,
+      seqOp = (summary: MultilabelSummarizer,
+               sample: (Array[Double], Array[Double])) => summary.add(sample._1, sample._2),
+      combOp = (sum1: MultilabelSummarizer, sum2: MultilabelSummarizer) => sum1.merge(sum2),
+      depth = 2,
+      finalAggregateOnExecutor = true)
   }
 
 
diff --git a/mllib/src/main/scala/org/apache/spark/mllib/feature/IDF.scala b/mllib/src/main/scala/org/apache/spark/mllib/feature/IDF.scala
index f1517f976835..5634f0e1d9b7 100644
--- a/mllib/src/main/scala/org/apache/spark/mllib/feature/IDF.scala
+++ b/mllib/src/main/scala/org/apache/spark/mllib/feature/IDF.scala
@@ -51,11 +51,15 @@ class IDF @Since("1.2.0") (@Since("1.2.0") val minDocFreq: Int) {
    */
   @Since("1.1.0")
   def fit(dataset: RDD[Vector]): IDFModel = {
-    val (idf: Vector, docFreq: Array[Long], numDocs: Long) = dataset.treeAggregate(
-      new IDF.DocumentFrequencyAggregator(minDocFreq = minDocFreq))(
-      seqOp = (df, v) => df.add(v),
-      combOp = (df1, df2) => df1.merge(df2)
-    ).idf()
+    val (idf: Vector, docFreq: Array[Long], numDocs: Long) = dataset
+      .treeAggregate[IDF.DocumentFrequencyAggregator](
+        zeroValue = new IDF.DocumentFrequencyAggregator(minDocFreq = minDocFreq),
+        seqOp = (df: IDF.DocumentFrequencyAggregator, v: Vector) => df.add(v),
+        combOp = (df1: IDF.DocumentFrequencyAggregator,
+                  df2: IDF.DocumentFrequencyAggregator) => df1.merge(df2),
+        depth = 2,
+        finalAggregateOnExecutor = true
+      ).idf()
     new IDFModel(idf, docFreq, numDocs)
   }
 
diff --git a/mllib/src/main/scala/org/apache/spark/mllib/linalg/distributed/RowMatrix.scala b/mllib/src/main/scala/org/apache/spark/mllib/linalg/distributed/RowMatrix.scala
index 63cd41439054..7621489bb1f0 100644
--- a/mllib/src/main/scala/org/apache/spark/mllib/linalg/distributed/RowMatrix.scala
+++ b/mllib/src/main/scala/org/apache/spark/mllib/linalg/distributed/RowMatrix.scala
@@ -91,8 +91,9 @@ class RowMatrix @Since("1.0.0") (
   private[mllib] def multiplyGramianMatrixBy(v: BDV[Double]): BDV[Double] = {
     val n = numCols().toInt
     val vbr = rows.context.broadcast(v)
-    rows.treeAggregate(null.asInstanceOf[BDV[Double]])(
-      seqOp = (U, r) => {
+    rows.treeAggregate[BDV[Double]](
+      zeroValue = null.asInstanceOf[BDV[Double]],
+      seqOp = (U: BDV[Double], r: Vector) => {
         val rBrz = r.asBreeze
         val a = rBrz.dot(vbr.value)
         val theU =
@@ -109,7 +110,8 @@ class RowMatrix @Since("1.0.0") (
             s"Do not support vector operation from type ${rBrz.getClass.getName}.")
         }
         theU
-      }, combOp = (U1, U2) => {
+      },
+      combOp = (U1: BDV[Double], U2: BDV[Double]) => {
         if (U1 == null) {
           U2
         } else if (U2 == null) {
@@ -118,7 +120,10 @@ class RowMatrix @Since("1.0.0") (
           U1 += U2
           U1
         }
-      })
+      },
+      depth = 2,
+      finalAggregateOnExecutor = true
+    )
   }
 
   /**
@@ -136,8 +141,9 @@ class RowMatrix @Since("1.0.0") (
     val gramianSizeInBytes = nt * 8L
 
     // Compute the upper triangular part of the gram matrix.
-    val GU = rows.treeAggregate(null.asInstanceOf[BDV[Double]])(
-      seqOp = (maybeU, v) => {
+    val GU = rows.treeAggregate[BDV[Double]](
+      zeroValue = null.asInstanceOf[BDV[Double]],
+      seqOp = (maybeU: BDV[Double], v: Vector) => {
         val U =
           if (maybeU == null) {
             new BDV[Double](nt)
@@ -146,7 +152,8 @@ class RowMatrix @Since("1.0.0") (
           }
         BLAS.spr(1.0, v, U.data)
         U
-      }, combOp = (U1, U2) =>
+      },
+      combOp = (U1: BDV[Double], U2: BDV[Double]) =>
         if (U1 == null) {
           U2
         } else if (U2 == null) {
@@ -154,7 +161,8 @@ class RowMatrix @Since("1.0.0") (
         } else {
           U1 += U2
         },
-      depth = getTreeAggregateIdealDepth(gramianSizeInBytes)
+      depth = getTreeAggregateIdealDepth(gramianSizeInBytes),
+      finalAggregateOnExecutor = true
     )
 
     RowMatrix.triuToFull(n, GU.data)
@@ -168,8 +176,9 @@ class RowMatrix @Since("1.0.0") (
     // This succeeds when n <= 65535, which is checked above
     val nt = if (n % 2 == 0) ((n / 2) * (n + 1)) else (n * ((n + 1) / 2))
 
-    val MU = rows.treeAggregate(null.asInstanceOf[BDV[Double]])(
-      seqOp = (maybeU, v) => {
+    val MU = rows.treeAggregate[BDV[Double]](
+      zeroValue = null.asInstanceOf[BDV[Double]],
+      seqOp = (maybeU: BDV[Double], v: Vector) => {
         val U =
           if (maybeU == null) {
             new BDV[Double](nt)
@@ -188,14 +197,17 @@ class RowMatrix @Since("1.0.0") (
 
         BLAS.spr(1.0, new DenseVector(na), U.data)
         U
-      }, combOp = (U1, U2) =>
+      },
+      combOp = (U1: BDV[Double], U2: BDV[Double]) =>
         if (U1 == null) {
           U2
         } else if (U2 == null) {
           U1
         } else {
           U1 += U2
-        }
+        },
+      depth = 2,
+      finalAggregateOnExecutor = true
     )
 
     bc.destroy()
@@ -533,9 +545,13 @@ class RowMatrix @Since("1.0.0") (
    */
   @Since("1.0.0")
   def computeColumnSummaryStatistics(): MultivariateStatisticalSummary = {
-    val summary = rows.treeAggregate(new MultivariateOnlineSummarizer)(
-      (aggregator, data) => aggregator.add(data),
-      (aggregator1, aggregator2) => aggregator1.merge(aggregator2))
+    val summary = rows.treeAggregate[MultivariateOnlineSummarizer](
+      zeroValue = new MultivariateOnlineSummarizer,
+      seqOp = (aggregator: MultivariateOnlineSummarizer, data: Vector) => aggregator.add(data),
+      combOp = (aggregator1: MultivariateOnlineSummarizer,
+                aggregator2: MultivariateOnlineSummarizer) => aggregator1.merge(aggregator2),
+      depth = 2,
+      finalAggregateOnExecutor = true)
     updateNumRows(summary.count)
     summary
   }
diff --git a/mllib/src/main/scala/org/apache/spark/mllib/optimization/GradientDescent.scala b/mllib/src/main/scala/org/apache/spark/mllib/optimization/GradientDescent.scala
index 8da2b0e27fb8..d4ff30c6add0 100644
--- a/mllib/src/main/scala/org/apache/spark/mllib/optimization/GradientDescent.scala
+++ b/mllib/src/main/scala/org/apache/spark/mllib/optimization/GradientDescent.scala
@@ -244,8 +244,9 @@ object GradientDescent extends Logging {
       // Sample a subset (fraction miniBatchFraction) of the total data
       // compute and sum up the subgradients on this subset (this is one map-reduce)
       val (gradientSum, lossSum, miniBatchSize) = data.sample(false, miniBatchFraction, 42 + i)
-        .treeAggregate((null.asInstanceOf[BDV[Double]], 0.0, 0L))(
-          seqOp = (c, v) => {
+        .treeAggregate[(BDV[Double], Double, Long)](
+          zeroValue = (null.asInstanceOf[BDV[Double]], 0.0, 0L),
+          seqOp = (c: (BDV[Double], Double, Long), v: (Double, Vector)) => {
             // c: (grad, loss, count), v: (label, features)
             val vec =
               if (c._1 == null) {
@@ -256,7 +257,7 @@ object GradientDescent extends Logging {
             val l = gradient.compute(v._2, v._1, bcWeights.value, Vectors.fromBreeze(vec))
             (vec, c._2 + l, c._3 + 1)
           },
-          combOp = (c1, c2) => {
+          combOp = (c1: (BDV[Double], Double, Long), c2: (BDV[Double], Double, Long)) => {
             // c: (grad, loss, count)
             val vec =
               if (c1._1 == null) {
@@ -268,7 +269,9 @@ object GradientDescent extends Logging {
                 c1._1
               }
             (vec, c1._2 + c2._2, c1._3 + c2._3)
-          })
+          },
+          depth = 2,
+          finalAggregateOnExecutor = true)
       bcWeights.destroy()
 
       if (miniBatchSize > 0) {
diff --git a/mllib/src/main/scala/org/apache/spark/mllib/optimization/LBFGS.scala b/mllib/src/main/scala/org/apache/spark/mllib/optimization/LBFGS.scala
index 28c997f5301c..3a8cde3ca1e8 100644
--- a/mllib/src/main/scala/org/apache/spark/mllib/optimization/LBFGS.scala
+++ b/mllib/src/main/scala/org/apache/spark/mllib/optimization/LBFGS.scala
@@ -258,7 +258,8 @@ object LBFGS extends Logging {
        }
 
       val zeroSparseVector = Vectors.sparse(n, Seq.empty)
-      val (gradientSum, lossSum) = data.treeAggregate((zeroSparseVector, 0.0))(seqOp, combOp)
+      val (gradientSum, lossSum) = data.treeAggregate(
+        (zeroSparseVector, 0.0), seqOp, combOp, 2, true)
 
       // broadcasted model is not needed anymore
       bcW.destroy()
diff --git a/mllib/src/main/scala/org/apache/spark/mllib/stat/Statistics.scala b/mllib/src/main/scala/org/apache/spark/mllib/stat/Statistics.scala
index d5f34c2630a4..d223e6eb335e 100644
--- a/mllib/src/main/scala/org/apache/spark/mllib/stat/Statistics.scala
+++ b/mllib/src/main/scala/org/apache/spark/mllib/stat/Statistics.scala
@@ -55,10 +55,13 @@ object Statistics {
    * @return [[SummarizerBuffer]] object containing column-wise summary statistics.
    */
   private[mllib] def colStats(X: RDD[(Vector, Double)], requested: Seq[String]) = {
-    X.treeAggregate(Summarizer.createSummarizerBuffer(requested: _*))(
-      seqOp = { case (c, (v, w)) => c.add(v.nonZeroIterator, v.size, w) },
-      combOp = { case (c1, c2) => c1.merge(c2) },
-      depth = 2
+    X.treeAggregate[SummarizerBuffer](
+      zeroValue = Summarizer.createSummarizerBuffer(requested: _*),
+      seqOp = (c: SummarizerBuffer,
+               vw: (Vector, Double)) => c.add(vw._1.nonZeroIterator, vw._1.size, vw._2),
+      combOp = (c1: SummarizerBuffer, c2: SummarizerBuffer) => c1.merge(c2),
+      depth = 2,
+      finalAggregateOnExecutor = true
     )
   }
 


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