xi-db opened a new pull request, #50507:
URL: https://github.com/apache/spark/pull/50507

   ### What changes were proposed in this pull request?
   
   In SparkML with Spark Connect, the parallelFitTasks fails when running 
CrossValidator fitting, as the active remote spark session is not properly 
propagated to the new threads.
   
   Before the PR, this code will fail in the line `cvModel = cv.fit(data)`:
   
   ```
   from pyspark.ml.classification import RandomForestClassifier
   from pyspark.ml.evaluation import BinaryClassificationEvaluator
   from pyspark.ml.tuning import ParamGridBuilder, CrossValidator
   from pyspark.ml.linalg import Vectors
   
   data = spark.createDataFrame([
       (Vectors.dense(1.0, 2.0), 0),
       (Vectors.dense(2.0, 3.0), 1),
       (Vectors.dense(1.5, 2.5), 0),
       (Vectors.dense(3.0, 4.0), 1),
       (Vectors.dense(1.1, 2.1), 0),
       (Vectors.dense(2.5, 3.5), 1),
   ], ["features", "label"])
   
   rf = RandomForestClassifier(labelCol="label", featuresCol="features")
   evaluator = BinaryClassificationEvaluator(labelCol="label")
   paramGrid = (ParamGridBuilder()
                .addGrid(rf.maxDepth, [2])
                .addGrid(rf.numTrees, [5, 10])
                .build())
   
   cv = CrossValidator(estimator=rf,
                       estimatorParamMaps=paramGrid,
                       evaluator=evaluator,
                       numFolds=3)
   
   cvModel = cv.fit(data)
   
   bestModel = cvModel.bestModel
   print(f"Best maxDepth: {bestModel.getMaxDepth()}")
   print(f"Best maxBins: {bestModel.getMaxBins()}")
   print(f"Best numTrees: {bestModel.getNumTrees}")
   ``` 
   
   It fails because on that thread the active remote spark session is not 
properly set:
   ```
     File "python/pyspark/ml/util.py", line 250, in wrapped
       assert session is not None
              ^^^^^^^^^^^^^^^^^^^
   AssertionError
   ```
   
   With this fix, the above code snippet works correctly.
   
   ### Why are the changes needed?
   
   It fixes a bug with CrossValidator fitting.
   
   ### Does this PR introduce _any_ user-facing change?
   No.
   
   ### How was this patch tested?
   New test.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   No.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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

Reply via email to