-
Notifications
You must be signed in to change notification settings - Fork 283
Add split-and-retry path to GpuProjectExec #14724
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
thirtiseven
wants to merge
5
commits into
NVIDIA:main
Choose a base branch
from
thirtiseven:project_split_retry
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.
+303
−10
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
197 changes: 197 additions & 0 deletions
197
tests/src/test/scala/com/nvidia/spark/rapids/ProjectSplitRetrySuite.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,197 @@ | ||
| /* | ||
| * Copyright (c) 2026, NVIDIA CORPORATION. | ||
| * | ||
| * Licensed 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 com.nvidia.spark.rapids | ||
|
|
||
| import ai.rapids.cudf.ColumnVector | ||
| import com.nvidia.spark.rapids.Arm.withResource | ||
| import com.nvidia.spark.rapids.RapidsPluginImplicits.AutoCloseableProducingSeq | ||
| import com.nvidia.spark.rapids.jni.{GpuSplitAndRetryOOM, RmmSpark} | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.{AttributeReference, ExprId, NamedExpression} | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.rapids.GpuAdd | ||
| import org.apache.spark.sql.rapids.catalyst.expressions.GpuRand | ||
| import org.apache.spark.sql.types.IntegerType | ||
| import org.apache.spark.sql.vectorized.ColumnarBatch | ||
|
|
||
| class ProjectSplitRetrySuite extends RmmSparkRetrySuiteBase { | ||
| private val NUM_ROWS = 500 | ||
| private val RAND_SEED = 10 | ||
| private val intAttr = AttributeReference("int", IntegerType)(ExprId(10)) | ||
| private val batchAttrs = Seq(intAttr) | ||
|
|
||
| // Reset retry counters so a leaked count from one test cannot mask a | ||
| // missed injection in the next. | ||
| override def afterEach(): Unit = { | ||
| RmmSpark.getAndResetNumRetryThrow(/*taskId*/ 1) | ||
| RmmSpark.getAndResetNumSplitRetryThrow(/*taskId*/ 1) | ||
| super.afterEach() | ||
| } | ||
|
|
||
| private def buildBatch(): ColumnarBatch = { | ||
| val ints = 0 until NUM_ROWS | ||
| new ColumnarBatch( | ||
| Array(GpuColumnVector.from(ColumnVector.fromInts(ints: _*), IntegerType)), | ||
| ints.length) | ||
| } | ||
|
|
||
| private def newSpillable(): SpillableColumnarBatch = | ||
| SpillableColumnarBatch(buildBatch(), SpillPriorities.ACTIVE_ON_DECK_PRIORITY) | ||
|
|
||
| // GpuAdd(int, 1) — pure, deterministic, retryable. | ||
| private def addOneExprs(): Seq[GpuExpression] = Seq( | ||
| GpuAlias(GpuAdd( | ||
| GpuBoundReference(0, IntegerType, true)(NamedExpression.newExprId, "int"), | ||
| GpuLiteral(1, IntegerType), | ||
| failOnError = false)(), | ||
| "plus_one")()) | ||
|
|
||
| private def collectInts(cb: ColumnarBatch, col: Int): Array[Int] = { | ||
| val gcv = cb.column(col).asInstanceOf[GpuColumnVector] | ||
| withResource(gcv.copyToHost()) { hcv => | ||
| (0 until cb.numRows()).map(hcv.getInt).toArray | ||
| } | ||
| } | ||
|
|
||
| private def collectDoubles(cb: ColumnarBatch, col: Int): Array[Double] = { | ||
| val gcv = cb.column(col).asInstanceOf[GpuColumnVector] | ||
| withResource(gcv.copyToHost()) { hcv => | ||
| (0 until cb.numRows()).map(hcv.getDouble).toArray | ||
| } | ||
| } | ||
|
|
||
| // Helper: build the SpillableColumnarBatch BEFORE injecting the OOM, so | ||
| // that the alloc inside ColumnVector.fromInts doesn't accidentally absorb | ||
| // the injection. Only the projection itself should trip the OOM. | ||
| private def withInjectedOOM[T](inject: => Unit)(body: SpillableColumnarBatch => T): T = { | ||
| val sb = newSpillable() | ||
| inject | ||
| body(sb) | ||
| } | ||
|
|
||
| test("split-retry produces same output as a single-batch projection") { | ||
| val out = withInjectedOOM { | ||
| RmmSpark.forceSplitAndRetryOOM(RmmSpark.getCurrentThreadId, 1, | ||
| RmmSpark.OomInjectionType.GPU.ordinal, 0) | ||
| } { sb => | ||
| GpuProjectExec.projectAndCloseWithRetrySingleBatch(sb, addOneExprs()) | ||
| } | ||
| withResource(out) { cb => | ||
| assertResult(NUM_ROWS)(cb.numRows()) | ||
| assertResult(1)(cb.numCols()) | ||
| val got = collectInts(cb, 0) | ||
| (0 until NUM_ROWS).foreach { i => | ||
| assertResult(i + 1)(got(i)) | ||
| } | ||
| } | ||
| assert(RmmSpark.getAndResetNumSplitRetryThrow(/*taskId*/ 1) > 0, | ||
| "expected at least one SplitAndRetryOOM to have been observed") | ||
| } | ||
|
|
||
| test("conf=false routes split-retry OOM to legacy path which fails") { | ||
| val sqlConf = new SQLConf() | ||
| sqlConf.setConfString(RapidsConf.PROJECT_SPLIT_RETRY_ENABLED.key, "false") | ||
| SQLConf.withExistingConf(sqlConf) { | ||
| val sb = newSpillable() | ||
| RmmSpark.forceSplitAndRetryOOM(RmmSpark.getCurrentThreadId, 1, | ||
| RmmSpark.OomInjectionType.GPU.ordinal, 0) | ||
| assertThrows[GpuSplitAndRetryOOM] { | ||
| GpuProjectExec.projectAndCloseWithRetrySingleBatch(sb, addOneExprs()).close() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("tiered project split-retry produces correct output") { | ||
| val tier = GpuBindReferences.bindGpuReferencesTiered( | ||
| addOneExprs(), batchAttrs, new SQLConf(), Map.empty) | ||
| assert(tier.areAllRetryable && tier.areAllDeterministic) | ||
| val out = withInjectedOOM { | ||
| RmmSpark.forceSplitAndRetryOOM(RmmSpark.getCurrentThreadId, 1, | ||
| RmmSpark.OomInjectionType.GPU.ordinal, 0) | ||
| } { sb => | ||
| tier.projectAndCloseWithRetrySingleBatch(sb) | ||
| } | ||
| withResource(out) { cb => | ||
| assertResult(NUM_ROWS)(cb.numRows()) | ||
| val got = collectInts(cb, 0) | ||
| (0 until NUM_ROWS).foreach { i => | ||
| assertResult(i + 1)(got(i)) | ||
| } | ||
| } | ||
| assert(RmmSpark.getAndResetNumSplitRetryThrow(/*taskId*/ 1) > 0) | ||
| } | ||
|
|
||
| // A non-deterministic projection (containing GpuRand) must NOT take the | ||
| // split path even when the conf is on, because row-splitting would | ||
| // change rand state across the halves and break the row-aligned stitch | ||
| // between deterministic and rand columns. forceRetryOOM (plain, not | ||
| // split) verifies the legacy withRetryNoSplit path is selected and | ||
| // checkpoint/restore reproduces the rand sequence on retry. | ||
| test("mixed deterministic + GpuRand falls back to legacy retry path") { | ||
| def projection(): Seq[GpuExpression] = Seq( | ||
| GpuAlias(GpuAdd( | ||
| GpuBoundReference(0, IntegerType, true)(NamedExpression.newExprId, "int"), | ||
| GpuLiteral(1, IntegerType), | ||
| failOnError = false)(), "plus_one")(), | ||
| GpuAlias(GpuRand(GpuLiteral(RAND_SEED, IntegerType), false), "rnd")()) | ||
|
|
||
| val batches = Seq(true, false).safeMap { forceRetry => | ||
| val tier = GpuBindReferences.bindGpuReferencesTiered( | ||
| projection(), batchAttrs, new SQLConf(), Map.empty) | ||
| assert(tier.areAllRetryable && !tier.areAllDeterministic) | ||
| val sb = newSpillable() | ||
| if (forceRetry) { | ||
| RmmSpark.forceRetryOOM(RmmSpark.getCurrentThreadId, 1, | ||
| RmmSpark.OomInjectionType.GPU.ordinal, 0) | ||
| } | ||
| tier.projectAndCloseWithRetrySingleBatch(sb) | ||
| } | ||
| withResource(batches) { case Seq(retried, ref) => | ||
| assertResult(ref.numRows())(retried.numRows()) | ||
| assertResult(ref.numCols())(retried.numCols()) | ||
| val refPlus = collectInts(ref, 0) | ||
| val retPlus = collectInts(retried, 0) | ||
| val refRand = collectDoubles(ref, 1) | ||
| val retRand = collectDoubles(retried, 1) | ||
| (0 until NUM_ROWS).foreach { i => | ||
| assertResult(refPlus(i))(retPlus(i)) | ||
| assertResult(refRand(i))(retRand(i)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // A plain GpuRetryOOM under the new path is resolved before the splitter | ||
| // is invoked, so the result comes back as a single piece — exercising the | ||
| // single-piece path through ConcatAndConsumeAll.buildNonEmptyBatchFromTypes. | ||
| test("plain GpuRetryOOM under split-retry path returns a single piece") { | ||
| val out = withInjectedOOM { | ||
| RmmSpark.forceRetryOOM(RmmSpark.getCurrentThreadId, 1, | ||
| RmmSpark.OomInjectionType.GPU.ordinal, 0) | ||
| } { sb => | ||
| GpuProjectExec.projectAndCloseWithRetrySingleBatch(sb, addOneExprs()) | ||
| } | ||
| withResource(out) { cb => | ||
| assertResult(NUM_ROWS)(cb.numRows()) | ||
| val got = collectInts(cb, 0) | ||
| (0 until NUM_ROWS).foreach { i => | ||
| assertResult(i + 1)(got(i)) | ||
| } | ||
| } | ||
| assertResult(0)(RmmSpark.getAndResetNumSplitRetryThrow(/*taskId*/ 1)) | ||
| assert(RmmSpark.getAndResetNumRetryThrow(/*taskId*/ 1) > 0) | ||
| } | ||
| } |
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.
pieceswhenbuildNonEmptyBatchFromTypesthrows in the multi-batch path.ConcatAndConsumeAll.buildNonEmptyBatchFromTypescloses all input batches in itsfinallyblock (arrayOfBatches.foreach(_.close())). If it throws (e.g., OOM duringTable.concatenate), both thatfinallyand thecloseOnExcept(pieces)handler call close on the sameGpuColumnVectorobjects, driving the cuDF native ref-count negative. Narrow thecloseOnExceptscope to cover only the collection loop and letbuildNonEmptyBatchFromTypestake exclusive ownership for the concat step.