Installation¶
From PyPI¶
pip install trnrand
With Neuron hardware support¶
# 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
[torchneuron] pins torch>=2.14, torch-neuronx>=2.14, and nki>=0.6
(torch-neuronx 2.14 requires Python ≥ 3.12). [neuron] is the XLA lineage
(torch-neuronx>=2.9, nki>=0.3.0). These are only needed on
Trainium/Inferentia instances. On CPU or GPU, trnrand falls back to
torch.Generator automatically.
nki and torch-neuronx are not on public PyPI; they ship with the Deep
Learning AMI Neuron or install from https://pip.repos.neuron.amazonaws.com
(see pyproject.toml).
Two Neuron PyTorch lineages¶
torch-neuronx ships in two lineages, and trnrand dispatches its NKI
kernels on whichever one is installed:
| Lineage | Instances | Stack | Kernel dispatch |
|---|---|---|---|
| TorchNeuron (native) | Trn2, Trn3 | Neuron SDK 2.33+, Python ≥ 3.12, torch 2.14, torch-neuronx 2.14.* |
Each kernel is a PyTorch custom op (torch.ops.trnrand.philox4x32, threefry4x32, ...) registered with torch_neuronx.nki_op; tensors live on device="neuron". Works in eager mode and inside torch.compile(backend="neuron"). |
| PyTorch/XLA | Trn1 (also Inf2) | torch-neuronx 2.9 lineage, torch_xla |
Unchanged: kernels run on the torch_xla device. |
Selection is automatic, on the first kernel launch:
- native —
torch_neuronxexportsnki_opand aneurondevice is available (torch_neuronx.device_count() > 0). This path never importstorch_xla. - xla —
torch_xlais installed. - otherwise there is no device path. Without
nki,trnrand.*usestorch.Generator; withnkibut no device, useset_backend("pytorch")orTRNRAND_USE_SIMULATOR=1(unchanged from earlier releases).
The trnrand:: custom ops are registered only when the native path is
selected, so importing trnrand on CPU or on the XLA lineage has no
TorchNeuron side effects. Both lineages publish a package named
torch-neuronx, so pick the extra (or Deep Learning AMI Neuron) that
matches the instance.
To force a path, set TRNSCI_NKI_DISPATCH before the first launch. The
variable is shared by every trnsci library:
| Value | Effect |
|---|---|
auto (default) |
The order above. |
native |
Use TorchNeuron; raise RuntimeError if no neuron device is available. |
xla |
Use torch_xla even if TorchNeuron is present; raise RuntimeError if torch_xla is missing. |
TRNSCI_XLA_DEPRECATION=1 makes the XLA path emit one DeprecationWarning.
It is off by default; the suite turns it on at TorchNeuron GA.
Editable installs and the NKI kernel cache¶
TorchNeuron enables the NKI trace cache by default
(NKI_ENABLE_TRACE_CACHE=1), which persists compiled kernels across
processes. The cache only versions source that belongs to an installed
wheel (a site-packages tree with .dist-info); an editable install is
not tracked and can get a stale kernel after you edit dispatch.py. When
developing with pip install -e, turn the cache off before starting
Python:
export NKI_ENABLE_TRACE_CACHE=0
The test suite keeps the cache on, as users run it, but gives each session
a fresh, empty cache directory (NKI_TRACE_CACHE_URL), so edits never hit
a stale entry.
The cache also ignores constants captured in a kernel's closure
(aws-neuron/aws-neuron-sdk#1454);
trnrand's kernels take every specialising value as an argument
(tests/test_trace_cache_safety.py). Set NKI_ENABLE_TRACE_CACHE=0 if
you write NKI kernels that a factory specialises through a closure.
See the TorchNeuron environment variables reference.
From source¶
git clone https://github.com/trnsci/trnrand
cd trnrand
pip install -e ".[dev]"
pytest tests/ -v
Requirements¶
- Python ≥ 3.10
torch >= 2.1numpy >= 1.24nki >= 0.3.0andtorch-neuronx(optional, for on-hardware NKI kernels; native dispatch needstorch-neuronx2.14.* on Python ≥ 3.12)