trnrand
Random number generation for AWS Trainium via NKI.
Seeded pseudo-random distributions, quasi-random sequences for quasi-Monte Carlo, and on-device Philox RNG targeting the GpSimd engine.
Part of the trnsci scientific computing suite (github.com/trnsci).
Current phase
trnrand follows the trnsci 5-phase roadmap. Active work is tracked in phase-labeled GitHub issues:
- Phase 1 — correctness (active): Philox 4×32 and Box-Muller NKI kernels scaffolded with CPU-reference oracles; awaiting hardware validation on trn1 / trn2.
- Phase 3 — perf: batched-tile RNG streaming, NEFF cache reuse.
- Phase 4 — multi-chip: stream-partitioned RNG across NeuronCores.
- Phase 5 — generation: trn2 PSUM sizing for larger batched tile generation.
(No Phase 2 for trnrand — the precision story is inherited where relevant.)
Suite-wide tracker: trnsci/trnsci#1.
Install
pip install trnrand
# Trn2/Trn3 with TorchNeuron (native PyTorch, Neuron SDK 2.33+)
pip install "trnrand[torchneuron]" --extra-index-url https://pip.repos.neuron.amazonaws.com
# Trn1 / PyTorch-XLA lineage
pip install "trnrand[neuron]" --extra-index-url https://pip.repos.neuron.amazonaws.com
Kernels dispatch on whichever Neuron PyTorch lineage is installed:
- TorchNeuron (native) — Trn2/Trn3, Neuron SDK 2.33+, Python ≥ 3.12,
torch2.14,torch-neuronx2.14.*. Each kernel is a PyTorch custom op (torch.ops.trnrand.*) on theneurondevice; notorch_xla. - PyTorch/XLA — Trn1 (and any host on the
torch-neuronx2.9 lineage). Kernels dispatch throughtorch_xla, unchanged.
trnrand picks the native path when the native torch_neuronx and a neuron
device are present, and the XLA path when torch_xla is installed; without
nki it uses torch.Generator. Set TRNSCI_NKI_DISPATCH=native or xla
(shared by all trnsci libraries) to force a path. With an editable install
(pip install -e), set NKI_ENABLE_TRACE_CACHE=0: the NKI kernel cache only
versions installed wheels and can serve stale kernels. See
installation.
Usage
import trnrand
# Seeded, reproducible generation
g = trnrand.Generator(seed=42)
# Standard distributions
x = trnrand.normal(1000, mean=0.0, std=1.0, generator=g)
u = trnrand.uniform(1000, low=-1.0, high=1.0, generator=g)
e = trnrand.exponential(1000, rate=2.0, generator=g)
# Quasi-random sequences (better convergence for MC integration)
sobol_pts = trnrand.sobol(1024, n_dims=5, seed=42)
halton_pts = trnrand.halton(1024, n_dims=3)
lhs_pts = trnrand.latin_hypercube(100, n_dims=4)
# Module-level seeding
trnrand.manual_seed(42)
x = trnrand.standard_normal(256)
Operations
| Category | Function | Description |
|---|---|---|
| Distributions | uniform |
U[low, high) |
normal |
N(μ, σ²) | |
standard_normal |
N(0, 1) | |
exponential |
Exp(λ) | |
bernoulli |
Bernoulli(p) | |
randint |
Uniform integers [low, high) | |
randperm |
Random permutation | |
truncated_normal |
Bounded normal (rejection sampling) | |
| Quasi-random | sobol |
Sobol sequence (scrambled) |
halton |
Halton sequence | |
latin_hypercube |
Latin Hypercube Sampling |
MC vs QMC Example
python examples/mc_integration.py
Compares pseudo-random vs Sobol quasi-random for estimating the volume of a 5-D hypersphere. QMC converges O(1/N) vs O(1/√N).
Status
- Seeded Generator with state management
- Standard distributions (uniform, normal, exponential, Bernoulli, etc.)
- Sobol, Halton, Latin Hypercube sequences
- MC vs QMC integration example
- NKI Philox kernel on GpSimd
- On-device Box-Muller (uniform → normal)
- Benchmarks vs cuRAND
Related Projects
| Project | What |
|---|---|
| trnfft | FFT + complex ops |
| trnblas | BLAS operations |
| trnsolver | Linear solvers |
License
Apache 2.0 — Copyright 2026 Scott Friedman
Disclaimer
trnsci is an independent open-source project. It is not sponsored by, endorsed by, or affiliated with Amazon.com, Inc., Amazon Web Services, Inc., or Annapurna Labs Ltd.
"AWS", "Amazon", "Trainium", "Inferentia", "NeuronCore", "Neuron SDK", and related identifiers are trademarks of their respective owners and are used here solely for descriptive and interoperability purposes. Use does not imply endorsement, partnership, or any other relationship.
All work, opinions, analyses, benchmark results, architectural commentary, and editorial judgments in this repository and on trnsci.dev are those of the project's contributors. They do not represent the views, positions, or commitments of Amazon, AWS, or Annapurna Labs.
Feedback directed at the Neuron SDK or Trainium hardware is good-faith ecosystem commentary from independent users. It is not privileged information, is not pre-reviewed by AWS, and should not be read as authoritative about product roadmap, behavior, or quality.
For official AWS guidance, see aws-neuron documentation and the AWS Trainium product page.
Metadata
Release files for trnrand 0.11.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| trnrand-0.11.0.tar.gz | 785.4 kB | With provenance | Details for trnrand-0.11.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| trnrand-0.11.0-py3-none-any.whl | Python 3 | none | any | Details for trnrand-0.11.0-py3-none-any.whl |
Total release size: 853.3 kB
Release files / trnrand-0.11.0.tar.gz
| Download URL | trnrand-0.11.0.tar.gz |
|---|---|
| Size | 785.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d052cfcabc4031c8bc06e275c0929a93d4ecc4ad9fb37e1586a3070e04b3e0d6
|
|
BLAKE2b-256 checksum How to use checksums |
6897c03a52859a6c510e5311480b514539b35ff3eb256925844a811cd9e391b9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.
Transparency logRelease files / trnrand-0.11.0-py3-none-any.whl
| Download URL | trnrand-0.11.0-py3-none-any.whl |
|---|---|
| Size | 67.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
43ef9821b473c0a294dbc680147547d14e179d138abf4e86290605750f9fc6b8
|
|
BLAKE2b-256 checksum How to use checksums |
b5ac7de28dd06c742cd56ecf3848f37a671493ebe710f2f289d3068f4764d36e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.
Transparency logSecurity for trnrand 0.11.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for trnrand-0.11.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.