This document is relevant for: Trn2, Trn3
TorchNeuron: Native PyTorch on AWS Neuron#
Beta Release
TorchNeuron is in public beta for the Neuron 2.33 release. It is supported on Trainium 2 (trn2) and Trainium 3 (trn3) instances. We welcome your feedback.
TorchNeuron is an open-source native PyTorch backend for AWS Trainium. It integrates through
PyTorch’s standard PrivateUse1 device backend mechanism, providing eager mode execution, torch.compile
for JIT compilation, and standard distributed APIs including FSDP, DTensor, DDP, and Tensor Parallelism.
TorchNeuron uses standard PyTorch APIs. To run existing PyTorch code on Trainium, change the device
placement from cuda to neuron.
What changes are needed?#
For eager mode:
Device placement: Change
.to('cuda')to.to('neuron')If your code uses
torch.accelerator, no changes are needed (automatic device detection)Mixed precision: Use
torch.autocast(device_type="neuron")Distributed training:
torch.distributedworks natively through theneuronbackend and supports the PyTorch parallelism APIs: DDP, FSDP2, tensor and sequence parallelism with DTensor, pipeline parallelism, and context parallelism. Configure sharding based on NeuronCores rather than GPUs
For torch.compile (on top of the eager mode changes):
Specify
backend="neuron"(e.g.,@torch.compile(backend="neuron"))Remove CUDA-specific parameters like
mode="max-autotune-no-cudagraphs"
No import torch_neuronx is needed: PyTorch autoloads TorchNeuron on import torch through
the torch.backends entry point that the torch-neuronx package registers. This includes
@nki.jit kernels, which run without the import. Import torch_neuronx only to call its own
APIs, such as nki_op or wrap_nki (see How to work with NKI kernels from torch-neuronx).
This document is relevant for: Trn2, Trn3