This document is relevant for: Trn2, Trn3
torch.neuron and torch.accelerator reference#
This topic lists the functions of the torch.neuron device module and the
torch.accelerator functions that TorchNeuron supports. Use it to look up a
function’s signature, what it returns, and the Neuron-specific behavior behind
it.
Applies to#
torch-neuronx(TorchNeuron) on Trn2 and Trn3 Trainium instances.Eager mode and
torch.compile(backend="neuron").
How the module is registered#
When torch_neuronx loads, it registers Trainium with PyTorch as the
PrivateUse1 backend under the name neuron:
torch.neuronbecomes the device module for theneurondevice, with the functions listed on this page. It plays the role thattorch.cudaplays for CUDA devices.torch.acceleratorresolves to theneurondevice when no other accelerator is present, so code written againsttorch.acceleratorselects Trainium without a change.Tensors, modules, and storages get
.neuron()and.is_neuronmethods, generated by PyTorch from the backend name.
PyTorch loads torch_neuronx automatically when you import torch, so you
do not need to import it yourself. An explicit import torch_neuronx is
needed only when TORCH_DEVICE_BACKEND_AUTOLOAD=0 is set. If another
PrivateUse1 backend was imported first, torch_neuronx leaves the device
registration to that backend and torch.neuron is not created.
import torch
assert torch.neuron.is_available()
print(torch.neuron.device_count(), torch.accelerator.current_accelerator())
torch.accelerator support#
The torch.accelerator namespace is PyTorch’s device-agnostic API. Device,
stream, and synchronization functions reach TorchNeuron through the device
guard that torch-neuronx registers, and work the same way as their
torch.neuron equivalents. The memory functions report the same Neuron
caching allocator statistics as the torch.neuron memory functions listed
below; the table notes the differences.
|
Status |
|
|---|---|---|
|
Supported |
|
|
Supported |
Returns |
|
Supported |
|
|
Supported |
|
|
Supported |
|
|
Supported |
|
|
Supported |
|
|
Supported |
|
|
Supported |
|
|
Supported |
Reports the dtypes the device supports: |
|
Supported |
Same statistics and resets as the |
|
Supported |
Same as |
|
Supported |
|
|
Not available |
Accelerator graph capture is not supported on Neuron. Use |
torch.neuron functions#
Every function below is available as torch.neuron.<name>. Where a function
takes a device argument, it accepts an int index, a str such as
"neuron:1", or a torch.device; None means the current Neuron device.
set_device and get_device_name are the exceptions: they do not accept a
str.
Device#
Function |
Returns |
Description |
|---|---|---|
|
|
|
|
|
Whether the Neuron runtime is initialized. The runtime initializes lazily on first device use. |
|
|
Initializes the Neuron runtime and applies the pending device seed. Raises |
|
|
Number of Neuron device indices exposed to PyTorch: the configured local world size when distributed state is initialized, otherwise the number of visible NeuronCores (virtual cores, as configured by logical NeuronCore configuration). |
|
|
Current device index: the thread-local selection, the process default, or |
|
|
Selects the current device from an |
|
context manager |
Selects a device for the duration of a |
|
|
|
|
properties object |
|
|
|
Dtypes supported for automatic mixed precision: |
|
|
Always |
Random number generation#
Each Neuron device has its own generator. The generator state is a seed and a
counter offset; the seed functions below reset the offset to zero. The set of
accessible generators is fixed the first time it is queried, so a later change
in device_count() is not reflected in it. For how the on-device RNG works,
reproducibility guarantees, and which random operations run on the device, see
Random number generation.
Function |
Returns |
Description |
|---|---|---|
|
|
Sets the current device generator’s seed and resets its offset. When the runtime is initialized and NKI RNG is available, also queues a GpSimd RNG-state update on the current device. |
|
|
Sets the seed of every accessible generator and resets each offset. Same NKI RNG update as |
|
|
Assigns a nondeterministic seed to the current generator and resets its offset. |
|
|
Draws one nondeterministic seed from the first generator and copies it to every accessible generator, resetting each offset. |
|
|
Initial seed of the current device generator. |
|
|
A 16-element CPU |
|
|
Replaces the generator state from an 8-byte (seed only; offset reset to zero) or 16-byte (seed and offset) CPU |
Streams and events#
Neuron execution is asynchronous: an operation returns to Python once it is enqueued on a stream. Use these functions to create streams, order work between them, and wait for completion. For a worked example that overlaps communication with compute, see Overlap communication with compute.
Function |
Returns |
Description |
|---|---|---|
|
|
Creates a stream on |
|
context manager |
Makes |
|
|
The context manager that |
|
|
The currently selected stream on a device. |
|
|
The default stream of a device. |
|
|
Makes |
|
|
Blocks until all streams on the device have completed. Call it before you stop a timer or read a memory statistic. |
|
|
Makes future work on the current stream wait for the work already submitted to |
|
|
Makes future work on the current stream wait for |
|
|
Records an event after the work already submitted to the current stream. With |
|
|
|
|
|
Creates an event. With |
Memory#
These functions read the Neuron caching allocator. Because execution is
asynchronous, call torch.neuron.synchronize() before you read a statistic.
For how to interpret the numbers and reduce memory use, see
How to profile memory usage in TorchNeuron and
Guidelines for memory management.
Function |
Returns |
Description |
|---|---|---|
|
|
Bytes occupied by allocated blocks ( |
|
|
Peak allocated bytes since the last reset ( |
|
|
Bytes currently reserved by the caching allocator ( |
|
|
Peak reserved bytes since the last reset ( |
|
|
Byte values by category, pool, and state; allocator, deferred-allocation, and scalar-cache counters; scalar cache size; and per-category runtime usage bytes when available. Per-pool values are measured only for |
|
|
Human-readable table of allocated, reserved, and active bytes, plus allocation, free, out-of-memory, retry, and deferred-allocation counters. |
|
|
The allocator snapshot captured at the allocation peak: |
|
|
Resets peak byte values to their current values. Does not clear the |
|
|
Resets event counters and the historical allocated and freed totals. Current and peak byte values are unchanged. |
|
|
Releases cached allocator blocks to the Neuron runtime. With lazy allocation, synchronizes every visible device first. Memory held by live tensors is not released. |
|
|
|
|
|
Fragmentation and efficiency metrics for the segment pool: |
|
|
Formatted table of the |
Compilation metrics#
These functions mirror the torch_neuronx.neuron_dynamo_backend helpers
documented in get_dynamo_metrics().
Function |
Returns |
Description |
|---|---|---|
|
|
Per-graph compilation metrics for the |
|
|
Clears the process-wide metrics records and the |
Example#
import torch
torch.neuron.manual_seed(0)
dev = torch.device("neuron", torch.neuron.current_device())
x = torch.randn(4096, 4096, device=dev)
y = x @ x
torch.neuron.synchronize()
print(torch.neuron.memory_summary())