Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Amazon Science 5 min read Deep dive mediumAdept

The science behind TorchNeuron, Amazon’s new PyTorch backend for AWS Trainium chips

TorchNeuron 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.

Assumes: PyTorch eager vs compiled executionML acceleratorsoperator fusion

Related posts

  • TorchNeuron: running HF models on AWS Trainium is easier than ever

    Hugging Face mediumAdept

    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.

  • Profiling TorchNeuron (Part 1): System-level profiling on AWS Trainium.

    Hugging Face mediumAdept

    A 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.

  • Does Your Model Run on AWS Trainium? Look It Up

    Hugging Face mediumAdept

    A 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.

  • Make your ZeroGPU Spaces go brrr with ahead-of-time compilation

    Hugging Face mediumAdept

    PyTorch ahead-of-time compilation (torch.export plus AOTInductor) suits ZeroGPU Spaces, where each GPU task runs in a short-lived forked process and torch.compile's just-in-time caching costs tens of seconds to minutes per task. The workflow captures example inputs with spaces.aoti_capture, exports and compiles the transformer inside a @spaces.GPU function, then patches it into the pipeline with spaces.aoti_apply, giving 1.3–1.8× speedups on Flux, Wan and LTX (1.7× on FLUX.1-dev). Further gains come from FP8 dynamic quantization via torchao (about 1.2× more on H200), dynamic shapes via torch.export.Dim, per-resolution multi-compile with shared weights, and prebuilt FlashAttention-3 kernels from the kernels library.

  • Together Compiler: Continual Compilation for AI inference

    Together AI hardMaster

    Together Compiler (TKCC) is an agent-driven continual optimization loop that recompiles inference engines as model, hardware and traffic shape drift. It avoids repeated engine restarts by using persistent engines that hot-reload candidate forward passes, cutting time-to-serving from 467s to 9s and raising experiment throughput 252x. A two-tier setup pairs fast few-layer engines for low-variance timing with slower many-layer engines that check results on real data distributions, which matters for MoE and sparse attention. A cost model combining hardware specs, microbenchmarks and measured forward passes picks the parallelism strategy, and the system reports up to 2.6x higher TPS on InferenceX AgentX, 1.7x over open-source megakernels on Llama 1B at batch size 1, and gains on non-LLM workloads.

The quest for software engineering knowledge.

Look:
Layout: