Skip to content

Add PCG64DXSM and MRG32k3a bit generators to dpnp.random - #3161

Open
ndgrigorian wants to merge 12 commits into
feature/device-rng-multivariatefrom
feature/device-rng-bit-generators
Open

ndgrigorian wants to merge 12 commits into
feature/device-rng-multivariatefrom
feature/device-rng-bit-generators

Conversation

@ndgrigorian

@ndgrigorian ndgrigorian commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

This PR adds two more bit generators to dpnp.random, as part of the series splitting #3147. It is stacked on #3160

  • PCG64DXSM, a custom engine (oneMKL's pcg64_dxsm has a hard-coded increment) that is bit-identical with NumPy's for random_raw, random (f32/f64), uniform and bytes, and exchanges states with numpy.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-item w the stream of NumPy's jumped(w)
  • MRG32k3a, on oneMKL's device mrg32k3a, where work-item w draws from substream w. 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 Philox

items_per_work_item is now a per-engine trait, exposed to Python as _rng_impl.items_per_work_item

  • 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?

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

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

@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_63 ran successfully.
Passed: 1491
Failed: 0
Skipped: 6

@coveralls

coveralls commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

Coverage is 84.784% — feature/device-rng-bit-generators into feature/device-rng-multivariate. No base build found for feature/device-rng-multivariate.

@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-multivariate branch from 9c726f8 to 2c9b5f0 Compare November 6, 2026 17:55
@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-multivariate branch from bf0b546 to 9dab4d3 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/_bit_generator.py Resolved
Comment thread dpnp/random/_bit_generator.py Resolved
Comment thread dpnp/random/_bit_generator.py Resolved
Comment thread dpnp/random/_bit_generator.py Resolved
Comment thread dpnp/random/_bit_generator.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-multivariate branch from 9dab4d3 to ebeb60b 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-multivariate branch 2 times, most recently from 9b8329f to f8e50e8 Compare November 16, 2026 15:57
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-bit-generators branch from a59e76d to a04ffb8 Compare November 16, 2026 15:57
Comment thread dpnp/random/_bit_generator.py Resolved
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.
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-multivariate branch from f8e50e8 to c91c3c8 Compare November 17, 2026 21:13
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-bit-generators branch from a04ffb8 to 8694043 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 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.

Comment on lines +523 to +544
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

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.

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 long

The 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?

Comment on lines +666 to +667
state = int(value["state"]["state"]) & _MASK128
inc = int(value["state"]["inc"]) & _MASK128

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.

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: 0

The 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 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