TorchNeuron: Amazon opens Trainium to PyTorch with a single configuration change
Imagen generada con gpt-image-2.5-sunburst para InferamaSource ↗
01

The end of mandatory rewriting

Amazon Web Services has introduced TorchNeuron, an open-source library under the Apache 2.0 license that allows PyTorch code to run directly on its Trainium2 and Trainium3 accelerators. The primary goal is to eliminate the technical barrier that has historically prevented developers from migrating their models to alternative hardware: the need to rewrite entire applications to adapt to a new architecture.

For over a decade, the NVIDIA ecosystem has been consolidated thanks to CUDA, software that allows PyTorch applications to function natively on its GPUs. TorchNeuron directly attacks this competitive advantage by integrating into PyTorch's official extension points, specifically through the PrivateUse1 backend. This means developers can maintain their usual workflow and simply change the target device via a single line of configuration.

02

Technical architecture and execution modes

TorchNeuron is not a simple wrapper, but a native backend that integrates deeply with the PyTorch compiler. It supports two main execution modes: eager mode, ideal for research and debugging, and torch.compile mode, optimized for production performance. In eager mode, each operation is individually compiled into a NEFF (Neuron Executable File Format) file and executed on the device.

For high-intensity workloads, using torch.compile allows capturing the model as a graph that the Neuron compiler optimizes globally. This process uses Torch-MLIR to convert the graph into StableHLO before generating the final code for the hardware. Additionally, the library supports standard distributed APIs such as FSDP, DDP, and DTensor, facilitating large-scale model training without modifying parallelism logic.

Execution modes in TorchNeuron

Comparison of available execution modes and their recommended use cases.

ModePrimary useFeatures
EagerResearch and debuggingImmediate execution, support for breakpoints and dynamic control flow.
torch.compileProduction and high performanceFull graph optimization, use of NKI for custom kernels.
03

Technical limitations of the initial version

Although the launch is significant, the public beta version of TorchNeuron presents specific limitations that teams must consider before migrating critical workloads. According to technical documentation, the first version skips approximately 22,000 tests from the PyTorch suite, primarily related to operators that do not yet have a native implementation in Neuron.

Among the features not supported in this first release are dynamic input shapes, sparse tensors, and certain forward-mode autograd operations. These omissions mean that not all PyTorch code will be immediately compatible, and developers must validate their specific models to ensure they do not rely on these excluded functionalities.

04

Strategic impact and competition with NVIDIA

Amazon's move is not just technical, but strategic. By opening TorchNeuron and contributing to the main PyTorch project to make the framework more hardware-agnostic, AWS seeks to erode NVIDIA's software advantage. The idea is that developers choose hardware based on price, performance, and availability, rather than forced compatibility by code.

Colin Brace, VP of Technology Software at AWS Annapurna Labs, stated that the goal is to eliminate the 'lock-in' that prevented developers from having a real choice. With more than 30 initial customers testing the technology, including voice AI companies like Deepgram and Cartesia, Amazon demonstrates that it is possible to achieve cost-efficiency improvements without sacrificing model quality.

05

How to get started with TorchNeuron

For developers interested in trying TorchNeuron, the installation process is straightforward. The library is available on GitHub under the aws-neuron/torch-neuronx repository. Once installed, the switch to Trainium is made by modifying the model's target device.

It is important to note that TorchNeuron requires EC2 Trn2 or Trn3 instances. Teams already using PyTorch can start with small models to validate compatibility before scaling to large-scale training or inference workloads. Integration with Hugging Face Transformers also allows loading pre-trained models directly onto the Neuron device.

Example migration to Trainium · python

Example code showing how to change the device of a PyTorch model to Trainium using TorchNeuron.

import torch

# Define a simple model
model = torch.nn.Linear(10, 1)

# Migrate to Trainium (one-line change)
model = model.to('neuron')

# Run an inference
input_data = torch.randn(1, 10, device='neuron')
output = model(input_data)

print(output)

Open questions

  • Independent benchmarks confirming Amazon's reported performance improvements for all model types have not been published.
  • The timeline for closing compatibility gaps (the 22,000 skipped tests) is not detailed in primary sources.
  • It is not specified which specific models from each initial customer (such as the Lieber Institute) are being run or their concrete results.
06

Keep exploring

06

Sources consulted

03

Corrections and transparency

If you spot incorrect or outdated information, send us a correction with the page and source we should review.

Submit a correction