TorchNeuron: running HF models on AWS Trainium is easier than ever
TorchNeuron is a PyTorch plugin that exposes AWS Trainium as a native `neuron` device with eager execution, `torch.compile`, and native distributed APIs (DTensor, FSDP2, TP), replacing the PyTorch XLA–based path that required lazy tensors and static graphs. As a result, Transformers, Diffusers, Accelerate, PEFT and TRL run directly on Trainium, and Hugging Face now recommends them over Optimum Neuron; 89% of the top 100 text-generation architectures work out of the box versus 46% before. Examples cover tensor-parallel generation with a static KV cache, TRL SFT with a TP×FSDP grid via `DistributedConfig`, and TP for Diffusers image pipelines. Practical caveats include keeping shapes static (padding to multiples of 128, disabling Dynamo automatic dynamic shapes) and disabling async weight loading; it enters public beta Oct. 20, 2026.
Related posts
- The science behind TorchNeuron, Amazon’s new PyTorch backend for AWS Trainium chips
Amazon Science mediumAdeptTorchNeuron is an open-source PyTorch backend that runs existing PyTorch code on AWS Trainium with little or no change. It adds an opt-in adaptive eager mode that batches dispatched ops into small subgraphs and compiles them in parallel. The subgraphs are sized so compile time overlaps with execution and stays hidden, while source-line correlation is preserved for debugging. Compiled NEFF executables are cached in memory, on disk, and through a per-machine shared compilation service, which cut time-to-first-step by about a third for a 64-process LLM run. Validation used AI agents to mine Hugging Face down to 841 representative models plus a registry of tens of thousands of tensor shapes, feeding a staged pipeline of fast unit tests, then representative models, then the full model set.
- Does Your Model Run on AWS Trainium? Look It Up
Hugging Face mediumAdeptA Hugging Face Space reports whether popular Transformers models run unmodified on AWS Trainium via the TorchNeuron PyTorch backend. For each model it shows the result, the number of Trainium cores used, wall time including compilation, peak device memory, and the run.py script to reproduce it. Each test shards the model with tensor or expert parallelism over the fewest trn2 cores that hold it, generates 32 tokens, and checks them against a CPU run or with a judge model. A PASS says nothing about throughput or long generations. Results are published as a queryable Parquet dataset, and precompiled NEFF graphs can be synced from a public cache to skip compilation.
- Accelerating Hugging Face Transformers with AWS Inferentia2
Hugging Face easyNoviceHugging Face benchmarked popular Transformer models on AWS Inferentia2 (inf2.xlarge) against Inferentia1 (inf1.2xlarge) and an NVIDIA A10G GPU (g5.2xlarge), running 144 p95-latency experiments at batch size 1 across six architectures and sequence lengths from 8 to 512. On average Inferentia2 delivered 4.5x lower latency than the A10G and 4x lower than Inferentia1, with BERT-base about 6x faster up to sequence length 256 and ViT-base about 2x faster than the GPU. The GPU runs were unoptimized fp32. Models compile for Inferentia2 through Optimum Neuron and the AWS Neuron SDK, and instances start at $0.76/hour.
- Profiling TorchNeuron (Part 1): System-level profiling on AWS Trainium.
Hugging Face mediumAdeptA beginner-friendly walkthrough of system-level profiling for PyTorch on AWS Trainium using TorchNeuron. It covers configuring torch.profiler with NeuronConfig and NeuronProfiler, reading the key_averages() table, and tracing a single operation through framework, runtime and hardware lanes in Neuron Explorer. Three RMSNorm implementations are profiled. Naive eager ops take 2,355 µs of device time across 6 dispatches. torch.rms_norm lowers to one fused kernel at 403 µs, and torch.compile discovers the same fusion at 376 µs, roughly one-sixth of the naive cost because gaps between dispatches and intermediate tensors are eliminated.
- Hugging Face ❤️ TorchTPU: TPUs, Natively
Hugging Face mediumAdeptTorchTPU is a native PyTorch backend that makes TPU an ordinary device, replacing the lazy-tensor torch_xla path and making Optimum TPU unnecessary for running Hugging Face models. Transformers loads models with device_map="tpu", and Diffusers pipelines run with pipe.to("tpu"), optionally with torch.compile and tensor parallelism across chips. Pallas kernels from the Kernels Hub, such as a Tokamax ragged-dot MoE kernel, speed up batch-64 decoding by 21.5× on Qwen3-30B-A3B and 12.8× on Gemma 4 26B-A4B. Of the Transformers tests that run on TPU, 96.5% pass, and 34 TorchTPU issues have been reported so far, 19 of them fixed.