Repository navigation
Add PCG64DXSM and MRG32k3a bit generators to dpnp.random - #3161
ndgrigorian wants to merge 14 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.0dev3=np2py314h8d9cdd5_107 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
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.
8694043 to
eef4522
Compare
c91c3c8 to
fb583eb
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.
4ba2f5b to
9ceb8d2
Compare
eb74e82 to
05394c4
Compare
vlad-perevezentsev
left a comment
There was a problem hiding this comment.
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:
| 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})); |
There was a problem hiding this comment.
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?
| ref = numpy.random.PCG64DXSM(0) | ||
| ref.state = value |
There was a problem hiding this comment.
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 RNGCould the setter compare the name with self.__class__.__name__ itself, and pass NumPy's setter a copy with "bit_generator": "PCG64DXSM"?
| // The elements a work-item produces in the strided layout | ||
| static constexpr std::size_t items_per_work_item = 8; |
There was a problem hiding this comment.
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?
| # the work-item streams used to be those of jumped(), so that a | ||
| # generator made by jumped() repeated almost all of its parent's |
There was a problem hiding this comment.
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 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