← Back to Pull requests

fix(rocrand, hiprand): Document stream switching and fix a set_stream data race #16981

Open
unknown opened this pull request with 471 lines diff across 5 files
View on GitHub
unknown unknown commented on Nov 30, 2026 20:27 UTC
Author

Motivation

<!-- Explain the purpose of this PR and the goals it aims to achieve. -->
This PR addresses two issues:

1. rocrand_set_stream (and hiprandSetStream) only stores the new stream. It does not order the new stream after work the generator has already queued on the old one, and the documentation said nothing about it. All of a generator's work reads and writes the same internal state (the engine states, and for Poisson the distribution tables), so if the old stream is not synchronized, work on the two streams can run at the same time and silently give wrong results, even when each stream writes to its own output buffer.

2. A related data race in the Poisson distribution: set_stream writes m_is_host_func_blocking without a lock while a table update host function still pending on the old stream reads it.

Jira ID: AIPRIMS-284

Technical Details

<!-- Explain the changes along with any relevant GitHub links. -->

Documentation of the stream-switching requirement

The requirement is documented in:

  • projects/rocrand/docs/conceptual/programmers-guide.rst: new section "Changing a generator's stream".

  • projects/hiprand/docs/how-to/use-hiprand-interfaces.rst: new subsection "Changing a generator's stream" under "Host API". It notes that the rule also applies with the cuRAND backend.

  • rocrand.h / hiprand.h: the rocrand_set_stream / hiprandSetStream comments state the rule.

  • rocrand.hpp / hiprand.hpp: the C++ wrapper stream() comment points to the C function. The other engines inherit it through \copydoc.
  • Poisson m_is_host_func_blocking data race

    poisson_distribution_manager::update_discrete_distribution runs as a host function queued with hipLaunchHostFunc, on a HIP runtime thread. It read manager->m_is_host_func_blocking to decide whether to take m_mutex, while set_stream writes that member on every call without a lock. If set_stream runs while an update is still pending on the old stream, this is a data race, and the update locks according to the new stream instead of the one it was queued on. The fix is:

  • update_discrete_distribution_arg gets a use_lock field, set from !m_is_host_func_blocking when the update is enqueued. The host function reads arg->use_lock instead of the member.

  • The private members are regrouped by whether the host function may use them (those that don't change after init()) or only the thread that calls the rocRAND API, with comments stating the rule.
  • This is the only host function that reads manager state. The capture check (is_stream_capturing, m_is_cache_disabled) is only used on the API thread and needs no change.

    Test Plan

    <!-- Explain any relevant testing done to verify this PR. -->

  • New test poisson_distribution_manager_tests.set_stream_keeps_locking_of_pending_update in projects/rocrand/test/internal/test_poisson_distribution.cpp. It instantiates the manager with a fake System that has one blocking and one non-blocking stream and holds back queued host functions. It queues an update on the blocking stream, switches to the non-blocking stream, and runs the held-back update on another thread while get_distribution holds the lock. Without the fix the update waits for the lock and the test fails after a 5 second timeout; with the fix it skips the lock and passes.

  • Run the new test with and without the poisson.hpp change to confirm it fails before the fix and passes after.

  • Standard CI test suite for rocRAND and hipRAND.

  • Docs build for rocRAND and hipRAND.
  • Test Result

    <!-- Briefly summarize test outcomes. -->
    The new test passes.

    Submission Checklist

  • Look over the contributing guidelines at https://github.com/ROCm/TheRock/blob/main/GOVERNANCE.md#pull-requests.
  • Diff: 471 lines (+353 / -118) across 5 files Reviewer: amd-hsong Review duration: 5632m
    All Vouch review confidence checks passed
    Residual risk 0.24 = Change Risk (0.46) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.46 · 471 lines diff across 5 files
    0.46
    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.368
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #16981"
    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: 45.7% (MEDIUM) · Higher = larger blast radius
    45.7%
    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: 24.3% · Safe to merge
    24.3%