Skip to content

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

Hugging Face 9 min read How-to mediumAdept

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

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.

Assumes: PyTorch basicstorch.compileML inference concepts

Related posts

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

    Amazon Science mediumAdept

    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.

  • Profiling in PyTorch (Part 1): A Beginner's Guide to torch.profiler

    Hugging Face mediumAdept

    torch.profiler produces two artifacts: a statistical table showing where time goes and a Chrome/Perfetto trace showing when CPU and GPU events happen. A small 64x64 bf16 matmul+add is overhead-bound, with GPU time under 1% of CPU time, while 4096x4096 matrices shift the work to compute-bound GEMM kernels. The trace walkthrough explains a cold-start dead window of about 228 µs before aten::matmul dispatch, a CPU-GPU lane offset, gaps caused by profiler Activity Buffer Requests, and the dispatch chain from Python down to CUDA kernels. The practical advice is to warm up, use profiler schedules, and read Self versus total times carefully before turning to torch.compile.

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

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

  • Host overhead is killing your inference efficiency

    Modal mediumAdept

    GPU idle time while the CPU prepares work (host overhead) shows up as low GPU kernel utilization and directly multiplies inference cost; 50% overhead means GPU spend is 2x what it could be. Gaps in CUDA streams visible in PyTorch Profiler or Nsight Systems traces reveal it. Fixes include avoiding synchronous CPU-GPU transfers by constructing tensors where they are needed (e.g. decode-time position embeddings, unit-size KV page lengths), fusing kernels with the Torch compiler (cutting ~20 launches to 3, saving ~30µs and 20% mean latency), and using CUDA Graphs to amortize remaining launch costs, as SGLang and vLLM already do.

The quest for software engineering knowledge.

Look:
Layout: