Repository navigation
Add PCG64DXSM and MRG32k3a bit generators to dpnp.random - #3161
ndgrigorian wants to merge 12 commits into
Conversation
be00c84 to
742674f
Compare
3565820 to
e06fd8c
Compare
742674f to
9c726f8
Compare
|
View rendered docs @ https://intelpython.github.io/dpnp/pull/3161/index.html |
|
Array API standard conformance tests for dpnp=0.22.0dev2=np2py314h8d9cdd5_63 ran successfully. |
9c726f8 to
2c9b5f0
Compare
e06fd8c to
c5182f4
Compare
bf0b546 to
9dab4d3
Compare
acc7ffe to
25ebfd8
Compare
25ebfd8 to
ed4d347
Compare
9dab4d3 to
ebeb60b
Compare
ed4d347 to
a59e76d
Compare
9b8329f to
f8e50e8
Compare
a59e76d to
a04ffb8
Compare
PCG64DXSM is a custom engine bit-identical with NumPy's for raw values, random, uniform and bytes, drawn in a sequential layout for the samplers taking a fixed number of draws. MRG32k3a uses oneMKL's device engine, with work-item w on substream w. sycl::mul_hi is replaced with a limb-based umul_hi, as DPC++ 2026.2 miscompiles it on Intel GPUs.
Use the dpnp.empty text for their device and sycl_queue, add the reference line to PCG64DXSM, list its advance and jumped methods in the docs, and make the thread of their lock test a daemon.
NumPy's uint32_t and int conversions go through a C long, so the overflow messages differ on Windows, where it is 32 bits; has_uint32 outside the int range is now rejected too.
f8e50e8 to
c91c3c8
Compare
a04ffb8 to
8694043
Compare
vlad-perevezentsev
left a comment
There was a problem hiding this comment.
I tested the new changes on GPU and CPU here, on an Alder Lake iGPU without fp64, on Windows with an Arc GPU, and with free-threaded Python 3.14t: the streams match everywhere, threads are safe, and the earlier comments are fixed. But the new test_state_errors fails on Linux with Python 3.13 and newer, see below.
| def _as_c_int(value, c_type, bits, signed): | ||
| """`value` as Cython converts it to the C type `c_type`, with its errors.""" | ||
|
|
||
| if not hasattr(type(value), "__int__"): | ||
| raise TypeError("an integer is required") | ||
| value = int(value) | ||
| if signed: | ||
| long_lim, lim = 1 << (_LONG_BITS - 1), 1 << (bits - 1) | ||
| if not -long_lim <= value < long_lim: | ||
| raise OverflowError("Python int too large to convert to C long") | ||
| if not -lim <= value < lim: | ||
| raise OverflowError(f"value too large to convert to {c_type}") | ||
| else: | ||
| if value < 0: | ||
| raise OverflowError(f"can't convert negative value to {c_type}") | ||
| if value >> _LONG_BITS: | ||
| raise OverflowError( | ||
| "Python int too large to convert to C unsigned long" | ||
| ) | ||
| if value >> bits: | ||
| raise OverflowError(f"value too large to convert to {c_type}") | ||
| return value |
There was a problem hiding this comment.
Blocking: test_state_errors fails on Linux with Python 3.13 and newer, in CI (linux / Test (3.14), the 3.13 and 3.15 jobs; 3.12 passes) and here on GPU and CPU. For has_uint32=2**63, NumPy says C int there, and _as_c_int says C long:
import numpy, dpnp.random as dr
st = dict(numpy.random.PCG64DXSM(1).state, has_uint32=2**63)
for bg in (numpy.random.PCG64DXSM(), dr.PCG64DXSM()):
try:
bg.state = st
except OverflowError as e:
print(e)
# numpy: Python int too large to convert to C int
# dpnp: Python int too large to convert to C longThe text depends on the Python and Cython versions too. Could the setter check value with a NumPy PCG64DXSM's own setter and read the fields back?
| state = int(value["state"]["state"]) & _MASK128 | ||
| inc = int(value["state"]["inc"]) & _MASK128 |
There was a problem hiding this comment.
Missed in my earlier rounds: the setter masks state and inc with & _MASK128 and takes strings, so a bad state passes silently where NumPy raises (also for state=-1 and state="1"):
import numpy, dpnp.random as dr
st = numpy.random.PCG64DXSM(1).state
bad = dict(st, state={"state": 2**128, "inc": st["state"]["inc"]})
for bg in (numpy.random.PCG64DXSM(), dr.PCG64DXSM()):
try:
bg.state = bad
print(bg.state["state"]["state"])
except OverflowError as e:
print(e)
# numpy: Python int too large to convert to C long; dpnp: 0The ValueError for a wrong bit_generator also says PRNG, where NumPy says RNG. NumPy's own setter, as in the other comment, would fix both.
This PR adds two more bit generators to
dpnp.random, as part of the series splitting #3147. It is stacked on #3160PCG64DXSM, a custom engine (oneMKL'spcg64_dxsmhas a hard-coded increment) that is bit-identical with NumPy's forrandom_raw,random(f32/f64),uniformandbytes, and exchanges states withnumpy.random.PCG64DXSM. The samplers taking a fixed number of draws per element use a new sequential layout, which reproduces NumPy's stream order; the others give work-itemwthe stream of NumPy'sjumped(w)MRG32k3a, on oneMKL's devicemrg32k3a, where work-itemwdraws from substreamw. Each work-item produces 128 elements, since moving to a substream costs a matrix product per digit, and 8 elements per work-item were 2x (GPU) to 10x (CPU) slower than Philoxitems_per_work_itemis now a per-engine trait, exposed to Python as_rng_impl.items_per_work_item