Skip to content
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

added example on how to avoid map partitions #14166

Open
wants to merge 2 commits into
base: release/530-release-candidate
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import com.johnsnowlabs.nlp.util.io.ResourceHelper
import com.johnsnowlabs.tags.{FastTest, SlowTest}
import com.johnsnowlabs.util.{Benchmark, FileHelper}
import org.apache.spark.ml.Pipeline
import org.apache.spark.sql.{SparkSession, SparkSessionExtensions}
import org.apache.spark.sql.catalyst.expressions.{Alias, Expression, NamedExpression}
import org.apache.spark.sql.connector.expressions.Expression
import org.scalatest.flatspec.AnyFlatSpec

import scala.io.Source
Expand Down Expand Up @@ -252,6 +255,73 @@ class NerDLSpec extends AnyFlatSpec {

}

"NerDLModel" should "avoid mappartition" taggedAs FastTest in {

import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.rules.Rule

// Define a custom optimization rule
case class OptimizeMapPartitions(sparkSession: SparkSession) extends Rule[LogicalPlan] {
override def apply(plan: LogicalPlan): LogicalPlan = {

plan transformUp {
case mapPartitions@MapPartitions(func, objAttr, child) =>

// Does the plan look for the NER?
val callLambda = shouldCallLambda(mapPartitions, plan)
if (callLambda)
mapPartitions
else
child // we do nothing here!
case other => other // No change for other operations
}
}

private def shouldCallLambda(mapPartitions: MapPartitions, plan: LogicalPlan): Boolean = {
// things you have to do for money :)
var condition = false

plan transformUp {
// IMPORTANT:: here we determine whether the plan will require the NER or not!!
// child is the input to the projection, not used
case projection@Project(projectList, child) =>
// need to find a projection for which this mapPartition is present
// and the projection must include an NER column
if (projection.containsChild.head.children.contains(mapPartitions) && // must contain mapPartitions
projectList.exists(_.name.startsWith("ner")))
condition = true
projection
}
condition
}
}

// Register your Catalyst plugin
ResourceHelper.spark.experimental.extraOptimizations = Seq(OptimizeMapPartitions(ResourceHelper.spark))

val nerModel = NerDLModel
.pretrained()
.setInputCols("sentence", "token", "embeddings")
.setOutputCol("ner")

val conll = CoNLL(explodeSentences = false)
val training_data =
conll.readDataset(ResourceHelper.spark, "src/test/resources/conll2003/eng.train")

val embeddings = WordEmbeddingsModel.pretrained()

val pipeline = new Pipeline()
.setStages(Array(embeddings, nerModel))

//map Partitions not called
pipeline.fit(training_data).transform(training_data).select("token").collect()

//map Partitions called
pipeline.fit(training_data).transform(training_data).select("ner").collect()


}

"NerDLApproach" should "benchmark test" taggedAs SlowTest in {

val conll = CoNLL(explodeSentences = false)
Expand Down
Loading