Repository navigation
Overview
This release enables free-threaded (GIL-disabled) CPython builds and adds support for Python 3.15. randint and randint_untyped now accept array-like low/high bounds, as NumPy's RandomState.randint does, and sampling with array-valued distribution parameters and several randint paths is much faster. It also fixes a group of correctness bugs where mkl_random returned uninitialized memory as random values: for out-of-range brng values, for integer generation with WH, MCG31, R250 and MRG32K3A, for degenerate distribution parameters, and for very large requests. geometric now follows the NumPy convention. Several seeded streams change in this release, see Behavior Changes below. Building from source now requires a newer Cython.
Highlights
🧵 Free-Threaded CPython
- The Cython extension is compiled with
freethreading_compatible=True, so importingmkl_randomno longer re-enables the GIL. The free-threaded configuration is built and tested in CI on 3.14t - Every
MKLRandomStateserializes access to its MKL stream under its own lock, so one generator can be shared between threads. The how-to guide has a new thread-safety section - Removed the
python-gilconstraint from the conda recipes that pinnedmkl_randomto GIL-enabled Python 3.14 builds
🐍 Python 3.15 Support
- Added support for Python 3.15. The free-threaded 3.15t build is not provided yet, since conda-forge has no NumPy build for it
🎲 Array-Like Bounds in randint
randintaccepts broadcastablelow/higharrays, asnumpy.random.RandomState.randintdoes, drawing each element with Lemire's methodrandint_untypedreuses therandintimplementation, so it gains the same array-like bounds and no longer raisesOverflowErrorfor bounds outside the Clongrange (e.g.2**40on Windows)
⚡ Performance
normal,uniform,exponential,laplace,gumbel,logistic,rayleighandlognormalwith array-valued parameters fill the whole request with one MKL call instead of one call per element- Faster
randintfor power-of-two ranges at or aboveINT_MAX, forbool,int8,uint8,int16anduint16, and for array-like bounds shuffleuses itsmemcpyfast path for multi-dimensional arrays whose first-axis items are contiguous- Integer generation with
WH,MCG31,R250andMRG32K3Auses less memory and is faster
🐛 Correctness Fixes
- An out-of-range integer
brngandMKLRandomState(seed, brng=None)no longer read past the generator table and fall back toMT19937;brng=0now selectsMT19937instead of leaving the state unseeded randintwith scalar bounds aboveINT_MAX,tomaxintandbytesreturn random data forWH,MCG31,R250andMRG32K3A, which lackviRngUniformBitssupportpoissonwithlam == 0andnegative_binomialwithp == 1return zeros, andgeometricrejectsNaN, instead of returning the untouched output buffer- Very large outputs are now filled completely: integer fills larger than two
MKL_INT_MAXchunks,multinomial,multinormal_choleskywhen the number of samples times the dimension exceedsMKL_INT_MAX, and 64-bit integers withPHILOX4X32X10andARS5 randintno longer returnshighfor 64-bit dtypes when the range is at or aboveINT_MAX- Fixed memory leaks in
set_stateandlogseries
🆕 Other Additions
- The sampling method ids
ICDF,BOXMULLER,BOXMULLER2,POISNORMandPTPEare exported frommkl_random, as themethoddocstrings already documented - Added an ASV benchmark suite under
benchmarks/
- Seeded streams change for array-valued parameters of the eight distributions listed under Performance (scalar-parameter paths are unchanged), for power-of-two
randintranges at or aboveINT_MAX, forgeometric, and for integer-likemethodids such asnp.int64(2), which now select the method they name instead ofBoxMuller geometricnow counts trials, as its docstring and NumPy do: the support starts at1and the mean is1 / p. Samples that fit inint32are one larger than before, andgeometric(p) - 1gives the old values. The samples are stillint32, unlike NumPy'sint64- Unsupported
methodvalues now emit aUserWarninglisting the supported methods before falling back to the default method - New
ValueErrors:negative_binomialwithp == 0;NaNparameters topoisson,negative_binomial,geometricandlogseries; array-valued parameters of the eight distributions above that do not broadcast tosizewithout adding dimensions;multinormal_choleskywith an emptymean, which used to raiseZeroDivisionError uniformwith scalar bounds andsize=Nonereturns a Pythonfloatinstead of a 0-d array; with array-valued bounds it may returnhighdue to floating-point rounding
📦 Build Requirement Changes
- Minimum build-time
Cythonraised to3.1.0(first release providing thefreethreading_compatibledirective)
Python Version Support
- ✅ Python 3.10, 3.11, 3.12, 3.13, 3.14, 3.15 (plus free-threaded 3.14t)
Detailed Changes
Added
- Added support for
array_like(broadcastable)low/highbounds inrandintgh-168 - Added support for free-threaded (GIL-disabled) CPython builds: the Cython extension is compiled with
freethreading_compatible=True, so importingmkl_randomno longer re-enables the GIL gh-159 - Added a thread-safety section to the how-to guide for free-threaded Python gh-159
- Added an ASV benchmark suite under
benchmarks/and abenchmarkoptional dependency group gh-184 - Enabled support of Python 3.15 gh-188
- Exported the sampling method ids
ICDF,BOXMULLER,BOXMULLER2,POISNORMandPTPEfrommkl_random, as themethoddocstrings already documented gh-189
Changed
- Unsupported
methodvalues (e.g. a misspelled name, a method the function does not implement,None,bool,float) now emit aUserWarninglisting the supported methods before falling back to the function's default method, instead of falling back silently gh-189 - Sped up
normal,uniform,exponential,laplace,gumbel,logistic,rayleighandlognormalfor array-valued parameters. Seeded results change for these array paths; scalar paths are unchanged gh-171 - Array parameters for these distributions must broadcast to the requested
sizewithout adding dimensions; previously accepted mismatches now raiseValueErrorgh-171 uniformwith array-valued bounds may returnhighdue to floating-point rounding gh-171- Pinned Cython in the Coverity Scan workflow so generated code stays stable between scans, and added
coverity/README.mddocumenting the known Cython-boilerplate false positives and the scan review checklist gh-164 - Sped up
randintfor power-of-two ranges at or aboveINT_MAXgh-172 - The random streams for power-of-two
randintranges at or aboveINT_MAXhave changed: with a fixed seed these now produce different (but equally valid) samples gh-172 - Sped up
randintforbool,uint8,int8,uint16andint16; generated values are unchanged gh-172 - Raised the minimum build-time
Cythonrequirement to3.1.0, the first release providing thefreethreading_compatibledirective gh-159 - Extended the
memcpy-based fast path ofshuffleto multi-dimensionalndarrayinputs whose first-axis items are contiguous, which is also much faster than the previous buffered path gh-159 - Updated the
randint_untypedimplementation, which now supportsarray_like(broadcastable)low/highbounds gh-176 - Sped up
randintwitharray_likebounds by generating words in cache-sized chunks and by picking the Lemire rejection test by range width gh-173 - Reduced the memory use and sped up integer generation with
WH,MCG31,R250andMRG32K3A, which lackviRngUniformBitssupport gh-178 - Replaced the
mklrand_py_helper.hshim withcpython.bytescimports and dropped the no-op__future__import, since the project requires Python 3.10+ gh-192 negative_binomialnow rejectsp == 0, which MKL does not support, with aValueError; the documented range ofpis(0, 1]gh-195poisson,negative_binomial,geometricandlogseriesnow rejectNaNparameters with aValueError, as NumPy does, instead of passing them to MKL gh-195geometricsamples are now one larger than before for everypwhose result fits inint32, so results for a fixed seed change;geometric(p) - 1reproduces the old values for them gh-196
Fixed
- Fixed
poissonwith a scalarlam == 0returning uninitialized memory as random values, since MKL requireslam > 0. The same applied to array-valuedlamwithmethod="PTPE". Both now return zeros, as the default array path and NumPy already did gh-195 - Fixed
negative_binomialwithp == 1returning uninitialized memory as random values, since MKL requiresp < 1; it now returns zeros, as NumPy does gh-195 - Fixed
geometricwithpbeingNaNreturning uninitialized memory as random values gh-195 - Fixed
logseriesrejectingp == 0, which NumPy documents as valid; it now returns ones, which its sampler already produced without a special case gh-195 - Fixed
multinormal_choleskyand the other functions takingmethodrouting integer-like ids such asnp.int64(0),np.int64(2)orFalsetoBoxMuller; NumPy integers now select the method they name gh-189 - Fixed
multinormal_choleskyraisingZeroDivisionErrorfor an emptymean; it now raisesValueErrorgh-189 - Fixed an out-of-range integer
brngindexingbrng_listpast its end, which returned uninitialized memory as random values; it now warns and falls back toMT19937gh-177 - Fixed
brng=0being treated as unset, which left the state unseeded gh-177 - Fixed
MKLRandomState(seed, brng=None)reading pastbrng_listand returning uninitialized memory; it now usesMT19937gh-177 - Fixed
uniformto return a Pythonfloatfor scalar bounds withsize=Noneinstead of a 0-d array gh-167 - Fixed
randintreturninghighforint64,uint64and the defaultintdtype when the range is at or aboveINT_MAXgh-172 - Fixed the integer fills silently under-filling requests larger than two
MKL_INT_MAXchunks gh-172 - Fixed
multinomialunder-filling large outputs by decrementing its chunk counter by elements instead of draws gh-172 - Fixed a memory leak in
set_state, which leaked the previous MKL stream on every call gh-159 - Fixed a memory leak in
logseries, which leaked a temporary buffer on every call gh-159 - Fixed
set_statereplacing the generator state before raisingValueErrorwhen the buffer does not match the algorithm name; the state is now left unchanged gh-159 - Fixed
get_statewriting past its buffer when another thread reseeded the generator with a differentbrngat the same time, and a deadlock when aseedvalue or ashuffleelement called back into the same generator gh-159 - Fixed
randintwith scalar bounds aboveINT_MAX,tomaxintandbytesreturning uninitialized data forWH,MCG31,R250andMRG32K3A, which lackviRngUniformBitssupport gh-175 - Fixed 64-bit integer generation returning zeros or crashing with
PHILOX4X32X10andARS5for requests of2**30elements or more gh-175 - Fixed
randint_untypedraisingOverflowErrorfor bounds outside the Clongrange, e.g.2**40on Windows gh-176 - Fixed
multinormal_choleskysilently returning wrong output, e.g. every sample equal tomean, when the number of samples times the dimension exceedsMKL_INT_MAXgh-187 - Fixed the
bytesdocstring, which documented astrreturn type instead ofbytesgh-192 - Fixed the
tomaxintexample, which compared against the Python 2sys.maxintgh-192 - Fixed the remaining Python 2 references in the
random_sample,random_integersandrandintdocstrings and in thezipfimplementation comment: the<type '...'>reprs,np.intandsys.maxintgh-192 - Fixed the
random_integersdocstring, which described the result asnumpy.int_rather than a Clong, documented the wrong scalar return type, and passed a floatsizethat no longer works gh-192 - Fixed
geometricreturning the number of failures before the first success instead of the number of trials, which contradicted its own docstring and NumPy. The support now starts at1,P(1)ispand the mean is1 / p. The samples are stillint32, unlike NumPy'sint64, and overflow for very smallpgh-196
Removed
- Removed the
python-gilconstraint from the conda recipes, which pinnedmkl_randomto GIL-enabled Python 3.14 builds gh-159
Contributors
- Anton Volkov (@antonwolfy)
- Nikita Grigorian (@ndgrigorian)
- Jordan Harlow (@jharlow-intel)
- Vladislav Perevezentsev (@vlad-perevezentsev)
New Contributors
- Venkata Sai Gireesh Chamarthi (@vchamarthi)
Full Changelog: 1.5.0...1.6.0