-
Notifications
You must be signed in to change notification settings - Fork 614
[GLUTEN-12187][VL] Port AttachDistributedSequenceExec to Velox backend #12188
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
Open
baibaichen
wants to merge
5
commits into
apache:main
Choose a base branch
from
baibaichen:snt/attach-dist-seq
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+427
−2
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
37fd192
[GLUTEN-12187][VL] Port AttachDistributedSequenceExec to Velox backend
baibaichen 5cf4d9a
[GLUTEN-12187][VL] Regenerate Configuration.md for new attachDistribu…
baibaichen 9e91b6e
[GLUTEN-12187][VL] Drop columnar cache: use two-pass child execution
baibaichen c5045d8
[GLUTEN-12187][VL] Copy input columns to keep uniform refCnt on outpu…
baibaichen d0eff70
[GLUTEN-12187][VL] Gate AttachDistributedSequence offload on backend …
baibaichen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
176 changes: 176 additions & 0 deletions
176
...ox/src/main/scala/org/apache/gluten/execution/ColumnarAttachDistributedSequenceExec.scala
This file contains hidden or 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,176 @@ | ||
| /* | ||
| * 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.backendsapi.arrow.ArrowBatchTypes.ArrowJavaBatchType | ||
| import org.apache.gluten.columnarbatch.ColumnarBatches | ||
| import org.apache.gluten.extension.columnar.transition.{Convention, ConventionReq} | ||
| import org.apache.gluten.iterator.Iterators | ||
| import org.apache.gluten.vectorized.ArrowWritableColumnVector | ||
|
|
||
| import org.apache.spark.rdd.RDD | ||
| import org.apache.spark.sql.catalyst.expressions.Attribute | ||
| import org.apache.spark.sql.execution.{ColumnarAttachDistributedSequenceBaseExec, SparkPlan} | ||
| import org.apache.spark.sql.types.{LongType, StructField, StructType} | ||
| import org.apache.spark.sql.vectorized.{ColumnarBatch, ColumnVector} | ||
|
|
||
| /** | ||
| * Velox implementation of [[ColumnarAttachDistributedSequenceBaseExec]] that prepends a contiguous, | ||
| * globally increasing `Long` id column to its child output while keeping the columnar pipeline | ||
| * intact. | ||
| * | ||
| * Mirrors Spark's `AttachDistributedSequenceExec` semantics with two passes over the child: | ||
| * 1. A first pass executes the child plan over partitions `[0, numPartitions - 1)` and sums the | ||
| * `numRows` of every produced batch -- the last partition's count is not needed for the | ||
| * prefix-sum. The batches are closed immediately; no native data is materialized for the count | ||
| * pass beyond what the child operator naturally produces. | ||
| * 2. The per-partition prefix-sum is broadcast and a second pass executes the child plan again, | ||
| * prepending the new id column. Each output column (id + copies of the input columns) is a | ||
| * freshly allocated [[ArrowWritableColumnVector]] so the output batch has a uniform reference | ||
| * count -- required by the downstream `OffloadArrowDataExec`'s `getRefCntHeavy` check. Input | ||
| * values are copied via Arrow's `ValueVector.copyFromSafe`; the upstream input batch is left | ||
| * untouched and closed by the upstream iterator. | ||
| * | ||
| * Why no cache? The natural choice would be to wrap the child output in | ||
| * [[org.apache.spark.sql.execution.ColumnarCachedBatchSerializer]] and `persist` once, so the child | ||
| * plan is computed only once. That works for ordinary columnar batches but fails for zero-column | ||
| * batches that can result from column pruning when only the new id column is selected | ||
| * (`df.select("id")` projects away every input column): the cache serializer's | ||
| * `ensureVeloxBatch -> isVeloxBatch -> getIndicatorVector` path throws on zero-column input. The | ||
| * two-pass approach trades one extra child execution for robustness across all valid plans, and | ||
| * matches vanilla Spark's behavior when the pandas-on-Spark cache option is `NONE`. | ||
| * | ||
| * For the trivial single-partition case the count pass is skipped and the assignment runs directly | ||
| * with `startOffset = 0`. | ||
| */ | ||
| case class ColumnarAttachDistributedSequenceExec( | ||
| sequenceAttr: Attribute, | ||
| override val child: SparkPlan) | ||
| extends ColumnarAttachDistributedSequenceBaseExec(sequenceAttr, child) { | ||
|
|
||
| override def batchType(): Convention.BatchType = ArrowJavaBatchType | ||
|
|
||
| override def requiredChildConvention(): Seq[ConventionReq] = Seq( | ||
| ConventionReq.ofBatch(ConventionReq.BatchType.Is(ArrowJavaBatchType))) | ||
|
|
||
| private val outputSchema: StructType = | ||
| StructType( | ||
| StructField(sequenceAttr.name, LongType, nullable = false) +: | ||
| child.output.map(a => StructField(a.name, a.dataType, a.nullable))) | ||
|
|
||
| override protected def doExecuteColumnar(): RDD[ColumnarBatch] = { | ||
| val childRdd = child.executeColumnar() | ||
| val numPartitions = childRdd.getNumPartitions | ||
|
|
||
| if (numPartitions <= 1) { | ||
| // Fast path: at most one partition, no need to count. | ||
| return childRdd.mapPartitions(it => assignIds(it, startOffset = 0L)) | ||
| } | ||
|
|
||
| // First pass: execute the child plan and count rows per partition for partitions | ||
| // [0, numPartitions - 1). The last partition's count is unused for the prefix-sum. | ||
| // Each batch is closed immediately after reading numRows so off-heap buffers are released. | ||
| val frontCounts: Array[Long] = sparkContext.runJob( | ||
| childRdd, | ||
| (it: Iterator[ColumnarBatch]) => { | ||
| var sum = 0L | ||
| while (it.hasNext) { | ||
| val cb = it.next() | ||
| sum += cb.numRows().toLong | ||
| cb.close() | ||
| } | ||
| sum | ||
| }, | ||
| 0 until (numPartitions - 1) | ||
| ) | ||
| val offsets = frontCounts.scanLeft(0L)(_ + _) | ||
| val bcOffsets = sparkContext.broadcast(offsets) | ||
|
|
||
| // Second pass: re-execute the child plan and prepend the id column. | ||
| childRdd.mapPartitionsWithIndex { | ||
| (pid, it) => assignIds(it, bcOffsets.value(pid)) | ||
| } | ||
| } | ||
|
|
||
| override protected def withNewChildInternal( | ||
| newChild: SparkPlan): ColumnarAttachDistributedSequenceExec = | ||
| copy(child = newChild) | ||
|
|
||
| /** | ||
| * Prepends a `Long` id column to each input batch starting from `startOffset` and incrementing by | ||
| * row index. The output is a fresh heavy [[ColumnarBatch]] whose columns are all freshly | ||
| * allocated [[ArrowWritableColumnVector]]s with reference count 1. Input column values are copied | ||
| * via Arrow's `ValueVector.copyFromSafe` rather than retained zero-copy so the resulting batch | ||
| * satisfies the uniform-reference-count invariant required by downstream | ||
| * `ColumnarBatches.offload` / `getRefCntHeavy`. The original input batch is left untouched and is | ||
| * closed by the upstream iterator's recycling logic. | ||
| */ | ||
| private def assignIds( | ||
| batches: Iterator[ColumnarBatch], | ||
| startOffset: Long): Iterator[ColumnarBatch] = { | ||
| val attached = new Iterator[ColumnarBatch] { | ||
| private var running: Long = startOffset | ||
|
|
||
| override def hasNext: Boolean = batches.hasNext | ||
|
|
||
| override def next(): ColumnarBatch = { | ||
| val inputCb = batches.next() | ||
| ColumnarBatches.checkLoaded(inputCb) | ||
| val numRows = inputCb.numRows() | ||
| val outCols = ArrowWritableColumnVector.allocateColumns(numRows, outputSchema) | ||
| try { | ||
| val idVec = outCols(0) | ||
| var i = 0 | ||
| while (i < numRows) { | ||
| idVec.putLong(i, running + i) | ||
| i += 1 | ||
| } | ||
| idVec.setValueCount(numRows) | ||
|
|
||
| // Copy each input column into its corresponding freshly-allocated output column. Using | ||
| // Arrow's per-row `copyFromSafe` keeps the implementation type-agnostic and ensures every | ||
| // output column has reference count 1, matching the id column. This avoids the uniform | ||
| // ref-count check enforced by `ColumnarBatches.getRefCntHeavy` when the planner inserts | ||
| // an `OffloadArrowDataExec` between us and the next Velox consumer. | ||
| var j = 0 | ||
| while (j < inputCb.numCols()) { | ||
| val src = inputCb.column(j).asInstanceOf[ArrowWritableColumnVector].getValueVector | ||
| val dst = outCols(j + 1).getValueVector | ||
| var r = 0 | ||
| while (r < numRows) { | ||
| dst.copyFromSafe(r, r, src) | ||
| r += 1 | ||
| } | ||
| dst.setValueCount(numRows) | ||
| j += 1 | ||
| } | ||
|
|
||
| running += numRows | ||
| new ColumnarBatch(outCols.asInstanceOf[Array[ColumnVector]], numRows) | ||
| } catch { | ||
| case t: Throwable => | ||
| outCols.foreach(_.close()) | ||
| throw t | ||
| } | ||
| } | ||
| } | ||
| Iterators | ||
| .wrap(attached) | ||
| .recyclePayload(_.close()) | ||
| .create() | ||
| } | ||
| } |
127 changes: 127 additions & 0 deletions
127
.../src/test/scala/org/apache/gluten/execution/VeloxAttachDistributedSequenceExecSuite.scala
This file contains hidden or 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,127 @@ | ||
| /* | ||
| * 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.config.GlutenConfig | ||
|
|
||
| import org.apache.spark.SparkConf | ||
| import org.apache.spark.sql.DataFrame | ||
| import org.apache.spark.sql.catalyst.expressions.AttributeReference | ||
| import org.apache.spark.sql.catalyst.plans.logical.AttachDistributedSequence | ||
| import org.apache.spark.sql.classic.ClassicDataset | ||
| import org.apache.spark.sql.execution.python.AttachDistributedSequenceExec | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.types.LongType | ||
|
|
||
| class VeloxAttachDistributedSequenceExecSuite extends VeloxWholeStageTransformerSuite { | ||
|
|
||
| override protected val resourcePath: String = "/tpch-data-parquet" | ||
| override protected val fileFormat: String = "parquet" | ||
|
|
||
| override def sparkConf: SparkConf = { | ||
| super.sparkConf | ||
| .set("spark.sql.shuffle.partitions", "3") | ||
| .set("spark.default.parallelism", "3") | ||
| .set(SQLConf.ANSI_ENABLED.key, "false") | ||
| } | ||
|
|
||
| /** | ||
| * Build a DataFrame that prepends a distributed-sequence id column using a directly constructed | ||
| * [[AttachDistributedSequence]] logical node. This avoids depending on pandas-on-Spark / PySpark | ||
| * in JVM tests. | ||
| */ | ||
| private def attachSequence(df: DataFrame, name: String = "id"): DataFrame = { | ||
| val attr = AttributeReference(name, LongType, nullable = false)() | ||
| ClassicDataset.ofRows(spark, AttachDistributedSequence(attr, df.queryExecution.analyzed)) | ||
| } | ||
|
|
||
| test("contiguous ids for a single partition") { | ||
| val df = attachSequence(spark.range(0, 7, 1, 1).toDF("v")) | ||
| val ids = df.select("id").collect().map(_.getLong(0)).toSeq | ||
| assert(ids == Seq(0L, 1L, 2L, 3L, 4L, 5L, 6L)) | ||
| } | ||
|
|
||
| test("contiguous ids across multiple partitions of equal size") { | ||
| val df = attachSequence(spark.range(0, 12, 1, 4).toDF("v")) | ||
| val ids = df.select("id").collect().map(_.getLong(0)).toSeq.sorted | ||
| assert(ids == (0L until 12L)) | ||
| // Check the offload happened. | ||
| val plan = df.queryExecution.executedPlan | ||
| val matched = plan.collectFirst { | ||
| case e: ColumnarAttachDistributedSequenceExec => e | ||
| } | ||
| assert(matched.isDefined, s"Expected ColumnarAttachDistributedSequenceExec in:\n$plan") | ||
| } | ||
|
|
||
| test("contiguous ids across multiple partitions of unequal size") { | ||
| val base = spark.range(0, 100, 1, 8).toDF("v").filter("v % 3 = 0") | ||
| val df = attachSequence(base) | ||
| val rows = df.collect() | ||
| val ids = rows.map(_.getAs[Long]("id")).toSeq.sorted | ||
| assert(ids == (0L until rows.length)) | ||
| } | ||
|
|
||
| test("empty input produces empty output") { | ||
| val df = attachSequence(spark.range(0, 0, 1, 4).toDF("v")) | ||
| assert(df.collect().isEmpty) | ||
| } | ||
|
|
||
| test("id is paired with the correct row payload") { | ||
| val df = attachSequence(spark.range(0, 5, 1, 1).toDF("v")) | ||
| val rows = df.select("id", "v").collect().map(r => (r.getLong(0), r.getLong(1))).toSeq | ||
| assert(rows == Seq((0L, 0L), (1L, 1L), (2L, 2L), (3L, 3L), (4L, 4L))) | ||
| } | ||
|
|
||
| test("output survives a downstream Velox shuffle (offload path)") { | ||
| // Repartition after attach forces ArrowJava -> ArrowNative -> VeloxBatch via | ||
| // OffloadArrowDataExec, which calls ColumnarBatches.getRefCntHeavy and | ||
| // requires the uniform-refCnt invariant on the output batch. This mirrors | ||
| // the vanilla SPARK-36338 inherited test that exposed the bug in CI. | ||
| val df = attachSequence(spark.range(0, 20, 1, 4).toDF("v")).repartition(3) | ||
| val rows = df.select("id", "v").collect().map(r => (r.getLong(0), r.getLong(1))).toSeq | ||
| val ids = rows.map(_._1).sorted | ||
| val vs = rows.map(_._2).sorted | ||
| assert(ids == (0L until 20L)) | ||
| assert(vs == (0L until 20L)) | ||
| } | ||
|
|
||
| test("falls back to vanilla exec when columnar attach-distributed-sequence is disabled") { | ||
| withSQLConf( | ||
| "spark.gluten.sql.columnar.attachDistributedSequence" -> "false" | ||
| ) { | ||
| val df = attachSequence(spark.range(0, 4, 1, 2).toDF("v")) | ||
| val plan = df.queryExecution.executedPlan | ||
| assert( | ||
| plan.find(_.isInstanceOf[ColumnarAttachDistributedSequenceExec]).isEmpty, | ||
| s"Expected no ColumnarAttachDistributedSequenceExec in:\n$plan") | ||
| val ids = df.select("id").collect().map(_.getLong(0)).toSeq.sorted | ||
| assert(ids == Seq(0L, 1L, 2L, 3L)) | ||
| } | ||
| } | ||
|
|
||
| test("GlutenConfig getter returns default true") { | ||
| assert(GlutenConfig.get.enableColumnarAttachDistributedSequence) | ||
| } | ||
|
|
||
| test("vanilla exec construction does not break offload pattern") { | ||
| // Sanity: confirm vanilla exec class is available and constructible (used in offload). | ||
| val attr = AttributeReference("id", LongType, nullable = false)() | ||
| val child = spark.range(0, 1, 1, 1).queryExecution.executedPlan | ||
| val vanilla = AttachDistributedSequenceExec(attr, child) | ||
| assert(vanilla.output.head.name == "id") | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we also set Spark master "local[3]" explicitly?
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.
It seems that gluten columnar shuffle is not set. Do we need to set it in the base class WholeStageTransformerSuite?