← Back to Pull requests

Avoid GPU waits when the MemoryStore evicts a GPU broadcast [databricks] #16490

Open
unknown opened this pull request with 630 lines diff across 7 files
View on GitHub
unknown unknown commented on Oct 21, 2026 22:39 UTC
Author

Fixes #16460.

Description

When Spark's MemoryStore evicts a broadcast that the plugin has loaded onto the GPU, it serializes it while holding the memory manager lock. The old code waited for the GPU semaphore at that point, which deadlocks with GPU tasks that need the same lock. The full analysis is in https://github.com/NVIDIA/cudf-spark/issues/16460#issuecomment-6328248089.

Related: #16491 models the wait order of eviction and batch from an earlier revision of this PR. #15230 tracks the broader lock-ordering work.

Changes:

  • doWriteObject writes a GPU-materialized broadcast as packed table bytes plus cudf unpack metadata, without the GPU semaphore or a device allocation:

  • - Device data is copied to host through the spill bounce buffer. cudf's JNI selects the RMM device for each copy, so the evicting thread need not have acquired the semaphore.
    - Spilled data is streamed from its host or disk copy.
    - New helpers: SpillableColumnarBatchFromBufferHandle.writePackedTo and SpillableHostBufferHandle.writeTo.
    - Eviction may still wait for a spill bounce buffer and its copy. Holders of those never need the memory manager lock.
  • Zero-row and row-count-only batches are written on the host.

  • The stream now starts with a one-byte format tag. The driver writes the existing JCudf format after the tag.

  • A reloaded packed broadcast stays on the host until a task calls batch, which copies it to the device.

  • batch returns an already-loaded batch under the object's monitor alone, as before. Otherwise it builds the GPU batch (buildGpuBatch) under a separate materializeLock and takes the monitor only to publish the result.

  • hostBatch reads device data under materializeLock and inside withRetryNoSplit, not under the object's monitor. In local mode the driver shares the executor's MemoryStore, so hostBatch on a reloaded packed broadcast copies through the GPU, as it already does for a loaded batch.

  • doWriteObject takes the monitor only to snapshot its state and reference the data it writes.

  • The packed writer fails the eviction if it writes a different number of bytes than its header declares.
  • Tests:

  • New BroadcastEvictionSuite. Each eviction runs on a task thread under the SparkEnv memory manager lock and asserts that it allocates no device memory.

  • - Eviction while another task holds the only GPU permit:
    - with the GPU batch on the device, in host memory, and on disk, asserting where the data is, re-evicting the reloaded broadcast, and evicting a GPU batch rebuilt from the packed bytes;
    - for zero-row and row-count-only broadcasts. The row-count-only case is coverage: the old code also passed it.
    - Eviction completes while another task waits for the GPU inside hostBatch.
    - Eviction completes while another task is paused inside buildGpuBatch, for both the original and the packed format.
    - Reading an unknown format tag or a truncated packed stream fails.
  • Each regression was reintroduced to confirm the tests fail:

  • - a semaphore wait in eviction;
    - a device allocation in the spilled write path;
    - building the batch or running hostBatch under the object's monitor.
  • Existing SerializationSuite broadcast tests pass.
  • AI assistance: drafted with Claude Code and reviewed by the author.

    Checklists

    Documentation

  • Updated for new or modified user-facing features or behaviors

  • No user-facing change
  • Testing

  • Added or modified tests to cover new code paths

  • Covered by existing tests

  • (Please provide the names of the existing tests in the PR description.)
  • Not required
  • Performance

  • Tests ran and results are added in the PR description

  • Issue filed with a link in the PR description

  • Not required
  • Microbenchmark on one Quadro RTX 6000 with Spark 3.5.0. The setup:

  • The broadcast has 4M rows (long, double and string columns); the packed data is 107 MB.

  • Eviction writes to a stream that discards the bytes, so output I/O is excluded. The "batch on disk" rows still read the spill file, likely from the page cache.

  • Each result is the median of 10 runs after 3 warm-up runs.

  • "Before" runs the pre-fix eviction code (getColumnarBatch plus JCudf serialization) on the same build, with an uncontended semaphore. The harness was temporary and is not part of this PR.
  • | Operation | Before | After |
    |---|---|---|
    | Evict, batch on device | 16.3 ms | 16.4 ms |
    | Evict, batch in host memory | 27.2 ms | 5.6 ms |
    | Evict, batch on disk | 120.1 ms | 88.5 ms |
    | Reload evicted broadcast and call batch | 128.1 ms | 73.6 ms |

  • Eviction of spilled data is faster because the old path first copied it back to the device.

  • Reload is faster because it skips the JCudf parse and an extra host copy.

  • Calling batch on a loaded broadcast takes one uncontended monitor, as before: about 20 ns per call.

  • The packed stream is 134 bytes larger than the JCudf stream (107,411,299 vs 107,411,165 bytes).
  • 🤖 Generated with Claude Code

    Diff: 630 lines (+472 / -158) across 7 files Reviewer: gerashegalov Review duration: 15274m
    All Vouch review confidence checks passed
    Residual risk 0.27 = Change Risk (0.50) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 630 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.386
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #16490"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.4% (MEDIUM) · Higher = larger blast radius
    50.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 26.8% · Safe to merge
    26.8%