Add: Stateful Python random generator API - #379
Open
ternaus wants to merge 1 commit into
Open
Conversation
ternaus
force-pushed
the
agent/rng-generator
branch
from
August 19, 2026 11:03
1c3ec01 to
8d61947
Compare
ternaus
marked this pull request as ready for review
August 19, 2026 11:08
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements the stateful Python API baseline proposed in #299.
It establishes an independent generator with explicit state before we add ISA-specific kernels and wider distribution
coverage.
nk.Generator(seed=...)withrandom,uniform,normal,standard_normal, andintegers.size=, directfloat32/float64and standard integer dtypes, and writable C-contiguousout=buffers.include/numkong/random.hso ISA-specific fill kernels can be addedwithout changing the Python contract.
Scope
This PR fixes the smallest useful public contract requested in the issue discussion. It deliberately does not claim
SIMD acceleration yet. The current fill path is a deterministic scalar baseline; normal floating-point samples still
follow the host
libmimplementation.The following remain separate follow-ups: ISA-specific fill kernels,
choice,shuffle,poisson,laplace, andbetadistributions, plus wider statistical validation.Benchmark plan
After the API shape is reviewed, we will add a representative benchmark to #299
against NumPy and OpenCV. It will document the workload, dtype, shape, allocation or
out=route, warmup, iterations,hardware, and library versions. The comparison will measure the matching available API paths; it will not present
different random algorithms as if they produced identical sequences.
Validation
python3 setup.py build_ext --inplace— passed on macOS arm64, Python 3.9.6, Apple Clang.PYTHONPATH=/private/tmp/numkong-test-deps:python python3 -m pytest test/test_random.py -q— 14 passed.ruff check python/numkong/__init__.pyi test/test_random.py— passed.clang++ -std=c++20 -Iinclude -Itest -fsyntax-only test/test.cpp— passed.(512, 512, 3)float32output:nk.Generator.random: 0.331 ms median;numpy.random.default_rng().random: 1.297 ms median. This is only alocal API smoke measurement, not a cross-library benchmark or SIMD performance claim.
Refs #299