Skip to content

Reimplement dpnp.random.RandomState on the device RNG - #3162

Open
ndgrigorian wants to merge 15 commits into
feature/device-rng-bit-generatorsfrom
feature/device-rng-random-state
Open

ndgrigorian wants to merge 15 commits into
feature/device-rng-bit-generatorsfrom
feature/device-rng-random-state

Conversation

@ndgrigorian

@ndgrigorian ndgrigorian commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

This PR moves dpnp.random.RandomState and the dpnp.random module functions onto the device RNG, as part of the series splitting #3147. It is stacked on #3161

RandomState now delegates to the Generator samplers, with NumPy's legacy checks and error messages. Unlike Generator, it also checks parameters on the device, as CuPy does, which waits for the device. The module functions keep one RandomState per queue: the queue given by device/sycl_queue, or else that of the first dpnp.ndarray parameter, or else the default queue

Adds

  • get_state, set_state, get_bit_generator and set_bit_generator (per queue; seed reseeds in place)
  • A bit generator as the seed of RandomState

RandomState also receives changes

  • Values now match across devices, and differ from the legacy implementation's
  • device, usm_type and sycl_queue are keyword-only
  • randn no longer takes d0=
  • uniform no longer takes an integer dtype
  • randint(dtype=int) returns int64
  • shuffle requires a dpnp.ndarray

Also enables the CuPy random_tests the device RNG supports, with per-test skips for CuPy internals and the bit generators we don't provide, and drops the random entries of the skip lists. The ones in skipped_tests_cuda.tbl are dropped too, but haven't been run on CUDA

The legacy Cython module is still built but no longer used; #3163 removes it

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-bit-generators branch from b5f1504 to 3565820 Compare November 4, 2026 19:47
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-random-state branch from 315889e to 142a3d8 Compare November 4, 2026 19:47
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-bit-generators branch from 3565820 to e06fd8c Compare November 4, 2026 19:50
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-random-state branch from 142a3d8 to 160719a Compare November 4, 2026 19:50
@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.22.0dev2=np2py314h8d9cdd5_78 ran successfully.
Passed: 1491
Failed: 0
Skipped: 6

@coveralls

coveralls commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

Coverage is 81.102% — feature/device-rng-random-state into feature/device-rng-bit-generators. No base build found for feature/device-rng-bit-generators.

@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

View rendered docs @ https://intelpython.github.io/dpnp/pull/3162/index.html

@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-bit-generators branch from e06fd8c to c5182f4 Compare November 6, 2026 17:55
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-random-state branch 2 times, most recently from edee62b to 843a343 Compare November 8, 2026 21:54
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-bit-generators branch from acc7ffe to 25ebfd8 Compare November 8, 2026 21:54
Comment thread dpnp/random/dpnp_iface_random.py Resolved
Comment thread dpnp/random/dpnp_iface_random.py Resolved
Comment thread dpnp/random/dpnp_random_state.py Resolved
Comment thread dpnp/random/dpnp_random_state.py Resolved
Comment thread CHANGELOG.md Resolved
Comment thread dpnp/random/_generator.py Resolved
Comment thread dpnp/random/dpnp_random_state.py Resolved
Comment thread dpnp/random/_generator.py Resolved
Comment thread dpnp/random/_generator.py Resolved
Comment thread dpnp/random/dpnp_iface_random.py Resolved
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-bit-generators branch from 25ebfd8 to ed4d347 Compare November 13, 2026 20:06
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-random-state branch from 843a343 to 0232b4c Compare November 13, 2026 20:06
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-bit-generators branch from ed4d347 to a59e76d Compare November 13, 2026 22:41
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-random-state branch from 0232b4c to 804d1ed Compare November 13, 2026 22:41
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-bit-generators branch from a59e76d to a04ffb8 Compare November 16, 2026 15:57
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-random-state branch from 804d1ed to f2ab866 Compare November 16, 2026 15:57
Comment thread dpnp/random/dpnp_iface_random.py Resolved
Comment thread CHANGELOG.md Resolved
RandomState and the module functions now delegate to the Generator
samplers, keeping NumPy's legacy checks and messages, also for
parameters on the device. The module keeps one RandomState per queue,
and adds get_state, set_state, get_bit_generator and set_bit_generator.

Enable the CuPy random tests the device RNG supports.
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-bit-generators branch from a04ffb8 to 8694043 Compare November 17, 2026 21:13
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-random-state branch from f2ab866 to 46d8cc7 Compare November 17, 2026 21:13

@vlad-perevezentsev vlad-perevezentsev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the new changes on a GPU Max and its CPU, on Python 3.14t, on an Iris Xe without fp64 and on Windows (Arc B390): both fixes work, the streams match across devices, and the tests pass. The red Linux jobs come from test_state_errors of #3161. A few comments below, the free-threading one first.

f"returned by RandmoState {rs.get_sycl_queue()}"
)
return _dpnp_random_states[sycl_queue]
(child,) = _seed_source.spawn(1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On free-threaded Python, a new queue's state can get the same stream as a child of get_bit_generator().spawn(). Since ff5edaa both spawn the bit generator's seed_seq, but _get_random_state holds the module lock and BitGenerator.spawn holds none:

import threading
import dpctl, dpnp

dpnp.random.seed(0)
bg = dpnp.random.get_bit_generator()
queues = [dpctl.SyclQueue() for _ in range(16)]
states, kids = [], []
barrier = threading.Barrier(17)

def new_queue(q):  # the first draw on a queue spawns its state from the seed
    barrier.wait()
    dpnp.random.rand(2, sycl_queue=q)
    states.append(dpnp.random.get_bit_generator(sycl_queue=q).seed_seq.spawn_key)

def spawner():
    barrier.wait()
    kids.extend(bg.spawn(1)[0].seed_seq.spawn_key for _ in range(16))

ts = [threading.Thread(target=new_queue, args=(q,)) for q in queues] + [threading.Thread(target=spawner)]
for t in ts:
    t.start()
for t in ts:
    t.join()
print("streams shared:", len(set(states) & set(kids)))  # 3.14t: 1-6 per run; with the GIL: 0

BitGenerator.spawn races with itself too: 32 threads x spawn(4) give 56-108 duplicate children of 128. Could both spawns take bit_generator.lock?

Comment thread dpnp/random/_generator.py
Comment on lines +345 to +346
# a device parameter without a constraint needs no conversion
if not device or cons == _CONS_NONE:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Missed in my earlier rounds: parameters without a constraint now skip _on_device, so a complex loc on the device gets the sampler's cast error, with 'same_kind' instead of NumPy's 'safe':

import dpnp

dpnp.random.RandomState(0).normal(dpnp.asarray([1 + 1j]), 1.0)
# TypeError: ... according to the rule 'same_kind'; numpy: ... according to the rule 'safe'

Checking for a complex dtype before the _CONS_NONE return would keep NumPy's text.

Comment thread CHANGELOG.md
* Added the multivariate distributions, `permutation`, `shuffle`, `permuted`, `choice` and `bytes` to `dpnp.random.Generator` [#3160](https://github.com/IntelPython/dpnp/pull/3160)
* Added the `dpnp.random.PCG64DXSM` bit generator, which matches `numpy.random.PCG64DXSM` for raw values, `random`, `uniform` and `bytes` [#3161](https://github.com/IntelPython/dpnp/pull/3161)
* Added the `dpnp.random.MRG32k3a` bit generator [#3161](https://github.com/IntelPython/dpnp/pull/3161)
* Added `dpnp.random.get_state`, `dpnp.random.set_state`, `dpnp.random.get_bit_generator` and `dpnp.random.set_bit_generator`, and support for a bit generator as the seed of `dpnp.random.RandomState` [#3162](https://github.com/IntelPython/dpnp/pull/3162)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Missed in my earlier rounds: on master RandomState had only normal, rand, randint, randn, random_sample, standard_normal and uniform. So the two Fixed lines should name only the dpnp.random functions, and this Added line could say that RandomState now has the other methods of NumPy's (beta to zipf, choice, permutation, shuffle, bytes, tomaxint).

Comment thread doc/reference/random.rst
Comment on lines +77 to +81
* :meth:`RandomState.randint` and :func:`randint` return ``int64`` values by
default, as do :meth:`RandomState.random_integers`,
:func:`random_integers` and :meth:`RandomState.tomaxint`, whose values
reach the largest ``int64``. NumPy returns a C ``long`` instead, which is
``int32`` on Windows.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: On Windows, NumPy's legacy RandomState also returns C long (int32) from choice, permutation of an integer and the discrete distributions (binomial, poisson, multinomial, ...), and dpnp returns int64 there too. This note names only randint, random_integers and tomaxint: could it say "the integer results of RandomState"?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants