This document is relevant for: Trn3
How to set up TorchAO Neuron#
Task overview#
This topic shows how to install TorchAO Neuron on an AWS Trainium instance and verify that the MXFP8 linear layer runs with TorchNeuron. It covers the supported install path and a verification step you can use before working through the MXFP8 training tutorials.
Prerequisites#
Trainium instance: Access to a Trn3 instance.
Neuron SDK >= 2.34
torch-neuronx >= 2.14.3.1: Run Quickstart: Install and verify torch-neuronx and confirm the verification script passes before continuing. In particular, confirm
device='neuron'resolves in a plain PyTorch script.TorchTitan Neuron installed if you plan to follow the MXFP8 training tutorials: How to set up TorchTitan Neuron.
Instructions#
1: Install TorchAO Neuron on the Trainium instance
TorchAO Neuron ships as the torchao-neuron wheel on the Neuron pip
repository, the same repository that serves torch-neuronx. Install
it into the environment where you verified torch-neuronx:
python -m pip install torchao-neuron \
--extra-index-url https://pip.repos.neuron.amazonaws.com
The wheel installs the torchao Python package. Do not install the
upstream torchao wheel into the same environment, because both
provide the same package name.
2: Verify the installation
First confirm the package imports and that the Neuron MXFP8 ops registered.
python -c "import torch, torchao; import torchao.prototype.mx_formats.kernels; \
print(torchao.__version__, hasattr(torch.ops.neuron, 'fused_quantize_mx_scaled_mm_nki'))"
Then run the fused linear once against a bf16 reference. Create
verify_torchao_neuron.py:
import torch
import torch.nn.functional as F
from torchao.prototype.moe_training.mxfp8_linear import MXFP8Linear
torch.manual_seed(0)
x_cpu = torch.randn(128, 256, dtype=torch.bfloat16)
linear = MXFP8Linear(256, 512, bias=False).to("neuron", torch.bfloat16)
w_cpu = linear.weight.detach().cpu()
x = x_cpu.to("neuron").requires_grad_(True)
out = linear(x)
out.sum().backward()
ref = F.linear(x_cpu.float(), w_cpu.float())
rel = (torch.linalg.norm(out.cpu().float() - ref) / torch.linalg.norm(ref)).item()
print(f"shape={tuple(out.shape)} finite={torch.isfinite(out.cpu()).all().item()} rel_error={rel:.4f}")
Run it:
python verify_torchao_neuron.py
With expected output:
shape=(128, 512) finite=True rel_error=0.0353
As a second option, verify the fused MXFP8 linear, which runs quantization and
the scaled matmul as a single Neuron operation and accepts kernel
configuration arguments. Create verify_torchao_neuron_fused.py:
import torch
import torch.nn.functional as F
from torchao.prototype.mx_formats.neuron_mxfp8 import (
NeuronLowering,
NeuronMXFP8BackendOptions,
NkiKernelOptions,
QuantScheme,
neuron_fused_mx_linear,
)
torch.manual_seed(0)
x_cpu = torch.randn(128, 256, dtype=torch.bfloat16)
w_cpu = torch.randn(512, 256, dtype=torch.bfloat16)
# Optional. Omit for the defaults: NKI lowering, 1x32, fused backward=False
opts = NeuronMXFP8BackendOptions(
lowering=NeuronLowering.NKI,
nki=NkiKernelOptions(quant_scheme=QuantScheme.ONE_X32, fused_backward=True),
)
x = x_cpu.to("neuron").requires_grad_(True)
w = w_cpu.to("neuron").requires_grad_(True)
out = neuron_fused_mx_linear(x, w, None, opts)
out.sum().backward()
ref = F.linear(x_cpu.float(), w_cpu.float())
rel = (torch.linalg.norm(out.cpu().float() - ref) / torch.linalg.norm(ref)).item()
print(f"shape={tuple(out.shape)} finite={torch.isfinite(out.cpu()).all().item()} rel_error={rel:.4f}")
Run it:
python verify_torchao_neuron_fused.py
Confirm your work#
To confirm TorchAO Neuron is installed and configured correctly:
The one-line import check prints the version and
True.verify_torchao_neuron.pyprintsshape=(128, 512) finite=Trueand arel_errorwell under0.10
Common issues#
import torchao fails
Possible solution: Confirm you are in the same Python environment where
torch-neuronxis installed, and thatpip install torchao-neuroncompleted.pip show torchao-neuronlists the installed version.
The import check prints False
Possible solution: The Neuron MXFP8 ops register only on Trn3. Check the instance type; on Trn2 the package imports but MXFP8 is unavailable, and TorchTitan Neuron reports
Neuron MXFP8 is only supported on trn3 or later.
See Troubleshooting: TorchAO Neuron for MXFP8 training failures.