![[Report] I Attended the Code Talk on Training nanochat with PyTorch and AWS Trainium #AWSreInvent #AIM339](https://devio2024-media.developers.io/image/upload/f_auto,q_auto/c_limit,w_1200/v1796065673/eyecatch/xw5jr5b9agc6qjxlknmg.png)
[Report] I Attended the Code Talk on Training nanochat with PyTorch and AWS Trainium #AWSreInvent #AIM339
This page has been translated by machine translation. View original
Introduction
Hello, this is watabo from Classmethod Operations.
At AWS re:Invent 2026, I attended a Code Talk titled "Build nanochat models with PyTorch and Trainium-Easier than you think".
A code talk is a code-centered session format led by experts through live coding.
The speaker writes code in real time as the session progresses, and attendees learn by watching.
To be honest, I couldn't understand the finer details of the code, but I could still tell that "there don't seem to be many places to change from the original code."
In this article, I'll organize what I heard while checking it afterward against the slides and the public code.
Session Overview
Can you train an LLM on AWS Trainium, our purpose-built AI chip for better price performance, using PyTorch with minimal code changes?
The question being posed is: "Can you train an LLM using PyTorch code with minimal changes on Trainium, AWS's AI-dedicated chip that emphasizes cost performance?"
What Is nanochat?

nanochat is code created by Andrej Karpathy that lets you build a ChatGPT-like conversational model from start to finish.
The slides introduced that about 6,000 lines cover the following flow.

Starting with preparation that splits text into word-like units, it goes through pretraining, additional training, and evaluation, and finally you can chat with the model.
It was originally built for NVIDIA GPUs (a single server with 8 H100s).
What's the Benefit?
Most AI training code is written on the assumption of NVIDIA GPUs.
Running it on a different chip would seem to require major rewrites, but what this session showed was that "you can train on Trainium with only a few changes."
Trainium is a training chip developed by AWS.
The session overview describes it as "offering excellent cost performance."
If you can move over while staying with the familiar PyTorch, that means more options for the chip used for training.
How Is It Achieved?
The Basic Form of Writing PyTorch

There are three things to look at.
- With
import torch_neuronx, you can use Trainium from PyTorch under the nameneuron - Specify
neuronas the location for the model and data - Specify
neuronas the backend fortorch.compile
torch.compile is a PyTorch feature that converts the computation steps to suit the chip.
The part that repeats training stays as ordinary PyTorch code.
What Was Changed in nanochat
The Trainium version of nanochat is published on GitHub.
Comparing this repository's minimal branch with the original nanochat, the changes were only 3 files, with 34 lines added and 6 lines deleted.
The content falls into four broad areas.
| Change | Details |
|---|---|
| Adding a usable chip | Added neuron to the original options (NVIDIA GPU, Mac, CPU) |
| Switching the backend | Specified the torch.compile backend for Trainium |
| Splitting up compilation | Compiles per block (components with the same shape) rather than the whole model |
| Fine-tuning | Adjusted the inter-chip communication method, numeric format, and how processing time is measured to suit Trainium |
The basic-form slide compiled the whole model, but in nanochat it is done per block.
According to a comment in the code, this is because the whole model is too large for Trainium's compiler.
gpt.py, which defines the model's internals, is unchanged in the minimal branch.
Going Even Faster with Handwritten Components
The session also introduced a branch called riv-kernels.
Computation components (kernels) are normally generated automatically by the compiler.
This branch replaces two time-consuming places with handwritten components.
For the handwriting, it uses NKI, a way of writing code for Trainium.
According to the explanatory document in the branch, processing time per training step is 17–25% shorter for the small model.
Larger models and multi-node configurations have not been measured.
The order is: if you just want to get it running, leave it to automatic compilation, and when you want more speed, add handwritten components.
Training Process and Results

In the session, they showed us a conversation with the model alongside the scrolling training log.
To the question "Why is the sky blue?", it answered by citing Rayleigh scattering.
The results presented are also listed in the README of the repository's riv branch.
| Model | Training time | CORE score |
|---|---|---|
| OpenAI GPT-2 (1.6B, 2019) | 168 hours | 0.2565 |
| nanochat d20 (897M), one trn2.48xlarge | 2.42 hours | 0.2319 |
CORE is nanochat's evaluation metric that aggregates multiple benchmarks.
1.6B and 897M are the models' parameter counts, and d20 refers to a 20-layer model.
The GPT-2 row is a value from 2019, so this is not a table comparing chip speeds under the same conditions.
A Tool for Finding Slow Spots

An analysis tool called Neuron Explorer was also introduced.
You can check how much time each process took on a timeline like the one on screen.
Trying It Yourself
At the end of the session, a tutorial page was introduced.
Please refer to it for the steps.
If you want to reproduce the nanochat results, the instance confirmed to work in the README is trn2.48xlarge.
It's a large instance, so it seems difficult to try on an individual basis.
If you run it, please check your quotas (Service Quotas) and pricing in advance.
Conclusion
Reading through the code was difficult, but even as a non-developer I could grasp the form of "writing PyTorch almost as is, and just changing the destination to neuron."
I hope this entry helps someone.
References
