Repository navigation
Reimplement dpnp.random.RandomState on the device RNG - #3162
ndgrigorian wants to merge 15 commits into
Conversation
b5f1504 to
3565820
Compare
315889e to
142a3d8
Compare
3565820 to
e06fd8c
Compare
142a3d8 to
160719a
Compare
|
Array API standard conformance tests for dpnp=0.22.0dev2=np2py314h8d9cdd5_78 ran successfully. |
|
View rendered docs @ https://intelpython.github.io/dpnp/pull/3162/index.html |
e06fd8c to
c5182f4
Compare
edee62b to
843a343
Compare
acc7ffe to
25ebfd8
Compare
25ebfd8 to
ed4d347
Compare
843a343 to
0232b4c
Compare
ed4d347 to
a59e76d
Compare
0232b4c to
804d1ed
Compare
a59e76d to
a04ffb8
Compare
804d1ed to
f2ab866
Compare
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.
seed() with a SeedSequence failed after reseeding a non-Philox bit generator, as the seed was passed to SeedSequence again. The new states now spawn from the bit generator's own seed sequence, so a child it spawns also differs from them.
a04ffb8 to
8694043
Compare
f2ab866 to
46d8cc7
Compare
vlad-perevezentsev
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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: 0BitGenerator.spawn races with itself too: 32 threads x spawn(4) give 56-108 duplicate children of 128. Could both spawns take bit_generator.lock?
| # a device parameter without a constraint needs no conversion | ||
| if not device or cons == _CONS_NONE: |
There was a problem hiding this comment.
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.
| * 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) |
There was a problem hiding this comment.
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).
| * :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. |
There was a problem hiding this comment.
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 PR moves
dpnp.random.RandomStateand thedpnp.randommodule functions onto the device RNG, as part of the series splitting #3147. It is stacked on #3161RandomStatenow delegates to theGeneratorsamplers, with NumPy's legacy checks and error messages. UnlikeGenerator, it also checks parameters on the device, as CuPy does, which waits for the device. The module functions keep oneRandomStateper queue: the queue given bydevice/sycl_queue, or else that of the firstdpnp.ndarrayparameter, or else the default queueAdds
get_state,set_state,get_bit_generatorandset_bit_generator(per queue;seedreseeds in place)RandomStateRandomStatealso receives changesdevice,usm_typeandsycl_queueare keyword-onlyrandnno longer takesd0=uniformno longer takes an integer dtyperandint(dtype=int)returns int64shufflerequires adpnp.ndarrayAlso enables the CuPy
random_teststhe 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 inskipped_tests_cuda.tblare dropped too, but haven't been run on CUDAThe legacy Cython module is still built but no longer used; #3163 removes it