forked from apache/incubator-gluten
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[VL] RAS: Add a new built-in cost model that avoids offloading trivia…
…l projects if its neighbor nodes fell back (apache#6756)
- Loading branch information
Showing
7 changed files
with
218 additions
and
64 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
backends-velox/src/test/scala/org/apache/gluten/execution/VeloxRoughCostModelSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.gluten.execution | ||
|
||
import org.apache.gluten.GlutenConfig | ||
|
||
import org.apache.spark.SparkConf | ||
import org.apache.spark.sql.execution.ProjectExec | ||
|
||
class VeloxRoughCostModelSuite extends VeloxWholeStageTransformerSuite { | ||
override protected val resourcePath: String = "/tpch-data-parquet-velox" | ||
override protected val fileFormat: String = "parquet" | ||
|
||
override def beforeAll(): Unit = { | ||
super.beforeAll() | ||
spark | ||
.range(100) | ||
.selectExpr("cast(id % 3 as int) as c1", "id as c2") | ||
.write | ||
.format("parquet") | ||
.saveAsTable("tmp1") | ||
} | ||
|
||
override protected def afterAll(): Unit = { | ||
spark.sql("drop table tmp1") | ||
super.afterAll() | ||
} | ||
|
||
override protected def sparkConf: SparkConf = super.sparkConf | ||
.set(GlutenConfig.RAS_ENABLED.key, "true") | ||
.set(GlutenConfig.RAS_COST_MODEL.key, "rough") | ||
|
||
test("fallback trivial project if its neighbor nodes fell back") { | ||
withSQLConf(GlutenConfig.COLUMNAR_FILESCAN_ENABLED.key -> "false") { | ||
runQueryAndCompare("select c1 as c3 from tmp1") { | ||
checkSparkOperatorMatch[ProjectExec] | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
gluten-core/src/main/scala/org/apache/gluten/planner/cost/LegacyCostModel.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.gluten.planner.cost | ||
|
||
import org.apache.gluten.extension.columnar.enumerated.RemoveFilter | ||
import org.apache.gluten.extension.columnar.transition.{ColumnarToRowLike, RowToColumnarLike} | ||
import org.apache.gluten.utils.PlanUtil | ||
|
||
import org.apache.spark.sql.execution.{ColumnarToRowExec, RowToColumnarExec, SparkPlan} | ||
|
||
class LegacyCostModel extends LongCostModel { | ||
|
||
// A very rough estimation as of now. The cost model basically considers any | ||
// fallen back ops as having extreme high cost so offloads computations as | ||
// much as possible. | ||
override def selfLongCostOf(node: SparkPlan): Long = { | ||
node match { | ||
case _: RemoveFilter.NoopFilter => | ||
// To make planner choose the tree that has applied rule PushFilterToScan. | ||
0L | ||
case ColumnarToRowExec(_) => 10L | ||
case RowToColumnarExec(_) => 10L | ||
case ColumnarToRowLike(_) => 10L | ||
case RowToColumnarLike(_) => 10L | ||
case p if PlanUtil.isGlutenColumnarOp(p) => 10L | ||
case p if PlanUtil.isVanillaColumnarOp(p) => 1000L | ||
// Other row ops. Usually a vanilla row op. | ||
case _ => 1000L | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
gluten-core/src/main/scala/org/apache/gluten/planner/cost/LongCostModel.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.gluten.planner.cost | ||
|
||
import org.apache.gluten.planner.plan.GlutenPlanModel.GroupLeafExec | ||
import org.apache.gluten.ras.{Cost, CostModel} | ||
|
||
import org.apache.spark.sql.execution.SparkPlan | ||
|
||
abstract class LongCostModel extends CostModel[SparkPlan] { | ||
private val infLongCost = Long.MaxValue | ||
|
||
override def costOf(node: SparkPlan): GlutenCost = node match { | ||
case _: GroupLeafExec => throw new IllegalStateException() | ||
case _ => GlutenCost(longCostOf(node)) | ||
} | ||
|
||
private def longCostOf(node: SparkPlan): Long = node match { | ||
case n => | ||
val selfCost = selfLongCostOf(n) | ||
|
||
// Sum with ceil to avoid overflow. | ||
def safeSum(a: Long, b: Long): Long = { | ||
assert(a >= 0) | ||
assert(b >= 0) | ||
val sum = a + b | ||
if (sum < a || sum < b) Long.MaxValue else sum | ||
} | ||
|
||
(n.children.map(longCostOf).toList :+ selfCost).reduce(safeSum) | ||
} | ||
|
||
def selfLongCostOf(node: SparkPlan): Long | ||
|
||
override def costComparator(): Ordering[Cost] = Ordering.Long.on { | ||
case GlutenCost(value) => value | ||
case _ => throw new IllegalStateException("Unexpected cost type") | ||
} | ||
|
||
override def makeInfCost(): Cost = GlutenCost(infLongCost) | ||
} |
53 changes: 53 additions & 0 deletions
53
gluten-core/src/main/scala/org/apache/gluten/planner/cost/RoughCostModel.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.gluten.planner.cost | ||
|
||
import org.apache.gluten.extension.columnar.enumerated.RemoveFilter | ||
import org.apache.gluten.extension.columnar.transition.{ColumnarToRowLike, RowToColumnarLike} | ||
import org.apache.gluten.utils.PlanUtil | ||
|
||
import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, NamedExpression} | ||
import org.apache.spark.sql.execution.{ColumnarToRowExec, ProjectExec, RowToColumnarExec, SparkPlan} | ||
|
||
class RoughCostModel extends LongCostModel { | ||
|
||
override def selfLongCostOf(node: SparkPlan): Long = { | ||
node match { | ||
case _: RemoveFilter.NoopFilter => | ||
// To make planner choose the tree that has applied rule PushFilterToScan. | ||
0L | ||
case ProjectExec(projectList, _) if projectList.forall(isCheapExpression) => | ||
// Make trivial ProjectExec has the same cost as ProjectExecTransform to reduce unnecessary | ||
// c2r and r2c. | ||
10L | ||
case ColumnarToRowExec(_) => 10L | ||
case RowToColumnarExec(_) => 10L | ||
case ColumnarToRowLike(_) => 10L | ||
case RowToColumnarLike(_) => 10L | ||
case p if PlanUtil.isGlutenColumnarOp(p) => 10L | ||
case p if PlanUtil.isVanillaColumnarOp(p) => 1000L | ||
// Other row ops. Usually a vanilla row op. | ||
case _ => 1000L | ||
} | ||
} | ||
|
||
private def isCheapExpression(ne: NamedExpression): Boolean = ne match { | ||
case Alias(_: Attribute, _) => true | ||
case _: Attribute => true | ||
case _ => false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters