fix(rocrand, hiprand): Document stream switching and fix a set_stream data race #16981
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
amd-hsong
Motivation
<!-- Explain the purpose of this PR and the goals it aims to achieve. -->
This PR addresses two issues:
1.
rocrand_set_stream(andhiprandSetStream) 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_streamwritesm_is_host_func_blockingwithout 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: therocrand_set_stream/hiprandSetStreamcomments state the rule.rocrand.hpp/hiprand.hpp: the C++ wrapperstream()comment points to the C function. The other engines inherit it through\copydoc.Poisson
m_is_host_func_blockingdata racepoisson_distribution_manager::update_discrete_distributionruns as a host function queued withhipLaunchHostFunc, on a HIP runtime thread. It readmanager->m_is_host_func_blockingto decide whether to takem_mutex, whileset_streamwrites that member on every call without a lock. Ifset_streamruns 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_arggets ause_lockfield, set from!m_is_host_func_blockingwhen the update is enqueued. The host function readsarg->use_lockinstead of the member.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. -->
poisson_distribution_manager_tests.set_stream_keeps_locking_of_pending_updateinprojects/rocrand/test/internal/test_poisson_distribution.cpp. It instantiates the manager with a fakeSystemthat 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 whileget_distributionholds 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.poisson.hppchange to confirm it fails before the fix and passes after.Test Result
<!-- Briefly summarize test outcomes. -->
The new test passes.
Submission Checklist