-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-22707][ML] Optimize CrossValidator memory occupation by models in fitting #19904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| package org.apache.spark.ml.tuning | ||
|
|
||
| import java.util.{List => JList, Locale} | ||
| import java.util.concurrent.atomic.AtomicInteger | ||
|
|
||
| import scala.collection.JavaConverters._ | ||
| import scala.concurrent.Future | ||
|
|
@@ -146,25 +147,18 @@ class CrossValidator @Since("1.2.0") (@Since("1.4.0") override val uid: String) | |
| val validationDataset = sparkSession.createDataFrame(validation, schema).cache() | ||
| logDebug(s"Train split $splitIndex with multiple sets of parameters.") | ||
|
|
||
| val completeFitCount = new AtomicInteger(0) | ||
|
||
| // Fit models in a Future for training in parallel | ||
| val modelFutures = epm.zipWithIndex.map { case (paramMap, paramIndex) => | ||
| Future[Model[_]] { | ||
| val foldMetricFutures = epm.zipWithIndex.map { case (paramMap, paramIndex) => | ||
| Future[Double] { | ||
| val model = est.fit(trainingDataset, paramMap).asInstanceOf[Model[_]] | ||
| if (completeFitCount.incrementAndGet() == epm.length) { | ||
| trainingDataset.unpersist() | ||
| } | ||
|
|
||
| if (collectSubModelsParam) { | ||
| subModels.get(splitIndex)(paramIndex) = model | ||
| } | ||
| model | ||
| } (executionContext) | ||
| } | ||
|
|
||
| // Unpersist training data only when all models have trained | ||
| Future.sequence[Model[_], Iterable](modelFutures)(implicitly, executionContext) | ||
| .onComplete { _ => trainingDataset.unpersist() } (executionContext) | ||
|
|
||
| // Evaluate models in a Future that will calulate a metric and allow model to be cleaned up | ||
| val foldMetricFutures = modelFutures.zip(epm).map { case (modelFuture, paramMap) => | ||
| modelFuture.map { model => | ||
| // TODO: duplicate evaluator to take extra params from input | ||
| val metric = eval.evaluate(model.transform(validationDataset, paramMap)) | ||
| logDebug(s"Got metric $metric for model trained with $paramMap.") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not needed anymore