Skip to content

Add: Stateful Python random generator API - #379

Open
ternaus wants to merge 1 commit into
ashvardanian:mainfrom
ternaus:agent/rng-generator
Open

Add: Stateful Python random generator API#379
ternaus wants to merge 1 commit into
ashvardanian:mainfrom
ternaus:agent/rng-generator

Conversation

@ternaus

@ternaus ternaus commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

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.

  • Adds nk.Generator(seed=...) with random, uniform, normal, standard_normal, and integers.
  • Supports integer or tuple size=, direct float32/float64 and standard integer dtypes, and writable C-contiguous
    out= buffers.
  • Keeps generator state independent between instances and releases the GIL while filling native output.
  • Exposes the scalar state transition through include/numkong/random.h so ISA-specific fill kernels can be added
    without changing the Python contract.
  • Adds typing stubs, Python documentation, and focused API tests.

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 libm implementation.

The following remain separate follow-ups: ISA-specific fill kernels, choice, shuffle, poisson, laplace, and
beta distributions, 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.
  • Local smoke benchmark after one warmup call, 8 timed iterations, (512, 512, 3) float32 output:
    nk.Generator.random: 0.331 ms median; numpy.random.default_rng().random: 1.297 ms median. This is only a
    local API smoke measurement, not a cross-library benchmark or SIMD performance claim.

Refs #299

@ternaus
ternaus force-pushed the agent/rng-generator branch from 1c3ec01 to 8d61947 Compare August 19, 2026 11:03
@ternaus
ternaus marked this pull request as ready for review August 19, 2026 11:08
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.

1 participant