Skip to content

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

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

ndgrigorian wants to merge 14 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?

@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-multivariate branch from be00c84 to 742674f Compare November 4, 2026 19:47
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-bit-generators branch 2 times, most recently from 3565820 to e06fd8c Compare November 4, 2026 19:50
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-multivariate branch from 742674f to 9c726f8 Compare November 4, 2026 19:50
@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.0dev3=np2py314h8d9cdd5_107 ran successfully.
Passed: 1491
Failed: 0
Skipped: 6

@coveralls

coveralls commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

Coverage is 84.783% — 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
@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 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 8694043 to eef4522 Compare November 20, 2026 01:26
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-multivariate branch from c91c3c8 to fb583eb Compare November 20, 2026 01:26
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 4ba2f5b to 9ceb8d2 Compare November 23, 2026 20:18
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-bit-generators branch from eb74e82 to 05394c4 Compare November 23, 2026 20:18

@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.

Thanks! I tested it on a UHD 770 (no fp64) with an i7-12700K, and on an Arc B570 and a UHD 730 with an i5-13400, with Level Zero and OpenCL: 05394c4 fixes both state comments, the streams match NumPy and are the same on all 8 devices, and the tests pass.
A few new comments below:

Comment on lines +443 to +448
const auto word = [&state](std::size_t i) {
return static_cast<std::uint32_t>(state.seed[i]);
};
return bitgen_t(
engine_t({word(0), word(1), word(2), word(3), word(4), word(5)},
{std::uint64_t{0}, subsequence << 12, subsequence >> 52}));

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: this constructor runs oneMKL's skip_ahead, whose 910-matrix table is copied into each MRG32k3a kernel. _rng_impl.so grows from 11.4 to 75.8 MB (gzipped: 2.4 to 26.5 MB). A compile-time table of jumps keeps the streams:

The change I built
// The jumps of the components of MRG32k3a by 2**k substreams, k < 64:
// their transition matrices raised to the power 2**(76 + k), computed at
// compile time
namespace mrg32k3a_jump
{
using Mat3 = std::array<std::array<std::uint32_t, 3>, 3>;

inline constexpr std::uint64_t m1 = 4294967087ULL;
inline constexpr std::uint64_t m2 = 4294944443ULL;

// a * b modulo m = 2**32 - c, for a, b < m, without a 64-bit division
template <std::uint64_t m>
constexpr std::uint64_t mulmod(std::uint64_t a, std::uint64_t b)
{
    constexpr std::uint64_t c = (std::uint64_t{1} << 32) - m;
    std::uint64_t x = a * b;
    x = (x >> 32) * c + (x & 0xffffffffULL);
    x = (x >> 32) * c + (x & 0xffffffffULL);
    return x >= m ? x - m : x;
}

template <std::uint64_t m>
constexpr std::uint64_t addmod(std::uint64_t a, std::uint64_t b)
{
    const std::uint64_t x = a + b;
    return x >= m ? x - m : x;
}

template <std::uint64_t m>
constexpr Mat3 matmul(const Mat3 &a, const Mat3 &b)
{
    Mat3 c{};
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            std::uint64_t acc = 0;
            for (int k = 0; k < 3; ++k) {
                acc = addmod<m>(acc, mulmod<m>(a[i][k], b[k][j]));
            }
            c[i][j] = static_cast<std::uint32_t>(acc);
        }
    }
    return c;
}

template <std::uint64_t m>
constexpr std::array<Mat3, 64> table(Mat3 a)
{
    for (int i = 0; i < 76; ++i) {
        a = matmul<m>(a, a);
    }
    std::array<Mat3, 64> t{};
    for (std::size_t k = 0; k < 64; ++k) {
        t[k] = a;
        a = matmul<m>(a, a);
    }
    return t;
}

inline constexpr std::array<Mat3, 64> jumps1 =
    table<m1>(Mat3{{{0, 1, 0},
                    {0, 0, 1},
                    {static_cast<std::uint32_t>(m1 - 810728), 1403580, 0}}});
inline constexpr std::array<Mat3, 64> jumps2 =
    table<m2>(Mat3{{{0, 1, 0},
                    {0, 0, 1},
                    {static_cast<std::uint32_t>(m2 - 1370589), 0, 527612}}});

template <std::uint64_t m>
inline void jump(std::uint32_t s[3], const std::array<Mat3, 64> &t,
                 std::uint64_t w)
{
    for (std::size_t k = 0; w != 0; ++k, w >>= 1) {
        if (w & 1) {
            std::uint32_t r[3];
            for (int i = 0; i < 3; ++i) {
                std::uint64_t acc = 0;
                for (int j = 0; j < 3; ++j) {
                    acc = addmod<m>(acc, mulmod<m>(t[k][i][j], s[j]));
                }
                r[i] = static_cast<std::uint32_t>(acc);
            }
            s[0] = r[0];
            s[1] = r[1];
            s[2] = r[2];
        }
    }
}
} // namespace mrg32k3a_jump

// in MRG32k3a::make (and #include <array>):
        std::uint32_t s1[3] = {word(0), word(1), word(2)};
        std::uint32_t s2[3] = {word(3), word(4), word(5)};
        mrg32k3a_jump::jump<mrg32k3a_jump::m1>(s1, mrg32k3a_jump::jumps1,
                                               subsequence);
        mrg32k3a_jump::jump<mrg32k3a_jump::m2>(s2, mrg32k3a_jump::jumps2,
                                               subsequence);
        return bitgen_t(engine_t({s1[0], s1[1], s1[2], s2[0], s2[1], s2[2]}));

Built on both DUTs: 33.7 MB (9.3 MB gzipped), streams bit-identical on 8 devices, MRG tests pass and run up to 13% faster. Could make jump this way?

Comment on lines +641 to +642
ref = numpy.random.PCG64DXSM(0)
ref.state = 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.

Because NumPy's setter checks bit_generator against "PCG64DXSM", a subclass can no longer set its own state, and jumped() fails on it. NumPy's subclasses work, and this setter accepted them before 05394c4:

import dpnp.random as dr

class MyPCG(dr.PCG64DXSM):
    pass

MyPCG(1).jumped()  # ValueError: state must be for a PCG64DXSM RNG

Could the setter compare the name with self.__class__.__name__ itself, and pass NumPy's setter a copy with "bit_generator": "PCG64DXSM"?

Comment on lines +330 to +331
// The elements a work-item produces in the strided layout
static constexpr std::size_t items_per_work_item = 8;

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: with 8 values per work-item, the affine_power jump of a work-item costs more than its values, so most PCG64DXSM samplers are 2-4x slower than with Philox. 64 values, as in the sequential layout, fixes it (standard_normal f32, ms):

Philox PCG64DXSM with 64
B570, 2**28 14.0 51.2 18.5
UHD 770, 2**25 25.0 68.7 30.5
i7-12700K, 2**24 16.1 20.9 10.5

It changes these streams, so better before a release. Could this be 64?

Comment on lines +534 to +535
# the work-item streams used to be those of jumped(), so that a
# generator made by jumped() repeated almost all of its parent's

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: this comment tells the history of the layout. AGENTS.md asks for one line of why, for example # the streams of jumped() never meet the work-item streams.

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