Skip to content

Add: Stateful Python random generator API - #379

Open
ternaus wants to merge 2 commits into
ashvardanian:main-devfrom
ternaus:agent/rng-generator
Open

ternaus wants to merge 2 commits into
ashvardanian:main-devfrom
ternaus:agent/rng-generator

Conversation

@ternaus

@ternaus ternaus commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Introduce a seeded Generator with independent state, floating distributions, bounded integers, and caller-owned output buffers. State and fills are serialized per instance. Generation is scalar, and normal sampling uses host libm.

This introduces a new public API and remains a separate, lower-priority review.

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
Copilot AI balanced review requested due to automatic review settings October 17, 2026 10:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

6 open issues
What changed in this PR

Adds a stateful nk.Generator(seed=...) API to provide independent, reproducible random generation from Python (with optional out= support and dtype/shape controls), backed by new native RNG utilities.

Changes:

  • Introduces native RNG core (include/numkong/random.h) and Python bindings (python/random.c/.h) for Generator methods (random, uniform, normal, standard_normal, integers) and state.
  • Exposes the new Generator type from the extension module and adds typing stubs + Python documentation.
  • Adds a dedicated pytest suite covering determinism, state restore, dtype/shape/bounds, and out= behavior.
File Description
test/​test_random.py Adds tests for determinism, state independence/restore, out= validation, and argument errors.
setup.py Adds python/random.c to the extension build sources.
python/​random.h Declares the new RandomGeneratorType for module registration.
python/​random.c Implements numkong.Generator Python C-extension type and distribution methods (GIL-releasing fills).
python/​numkong/​__init__.pyi Adds typing stubs for the new Generator API.
python/​numkong.c Registers the Generator type with the extension module.
python/​README.md Documents nk.Generator usage and out= semantics.
include/​numkong/​random.h Adds the native RNG generator struct and RNG/distribution functions.
include/​numkong/​numkong.h Re-exports the new RNG header from the umbrella include.

Review effort: Lite

Comment thread include/numkong/random.h Outdated
Comment thread python/random.c
Comment thread python/README.md Outdated
Comment thread python/numkong/__init__.pyi
Comment thread python/random.c Outdated
Comment thread python/random.c Outdated
Expose seeded generation through native state and a Python Generator wrapper.

Refs ashvardanian#299.
@ashvardanian
ashvardanian changed the base branch from main to main-dev October 18, 2026 22:58

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.

2 participants