This document is relevant for: Inf1
, Inf2
, Trn1
, Trn2
Introducing PyTorch 2.7 Support#
What are we introducing?#
Starting with the Neuron 2.24 release, customers can now upgrade to PyTorch NeuronX (torch-neuronx
) with specific support for PyTorch version 2.7.
PyTorch Neuron (torch-neuronx) Setup is updated to include installation instructions for PyTorch NeuronX 2.7 for Amazon Linux 2023 and Ubuntu 22.04. Note that PyTorch NeuronX 2.7 is supported on Python 3.9, 3.10, and 3.11.
Review migration guide for possible changes to training scripts. No code changes are required for inference scripts.
How is PyTorch NeuronX 2.7 different compared to PyTorch NeuronX 2.5?#
PyTorch NeuronX 2.7 uses Torch-XLA v2.7 and PyTorch v2.7 which have C++11 ABI enabled by default.
Additionally, Torch-XLA v2.7 includes a fix for the training performance issue pytorch/xla#9037.
See Torch-XLA 2.7 release for a full list.
See Migrate your application to PyTorch 2.7 for changes needed to use PyTorch NeuronX 2.7.
Note
GSPMD and Torch Dynamo (torch.compile) support in Neuron will be available in a future release.
How can I install PyTorch NeuronX 2.7?#
To install PyTorch NeuronX 2.7, follow the PyTorch Neuron (torch-neuronx) Setup guides for Amazon Linux 2023 and Ubuntu 22.04 AMI. Refer to the Neuron Multi-Framework DLAMI setup guide for Ubuntu 22.04 with a pre-installed virtual environment for PyTorch NeuronX 2.7 that you can use to get started. PyTorch NeuronX 2.7 can be installed using the following:
python -m pip install --upgrade neuronx-cc==2.* torch-neuronx==2.7.* torchvision
Note
PyTorch NeuronX 2.7 is currently available for Python 3.9, 3.10, 3.11.
Migrate your application to PyTorch 2.7#
First, install the PyTorch NeuronX 2.7 as described above in installation guide
Migrating training scripts#
To migrate the training scripts from PyTorch NeuronX 2.5/2.6 to PyTorch NeuronX 2.7, implement the following changes:
Note
xm
below refers to torch_xla.core.xla_model
, xr
refers to torch_xla.runtime
, and xmp
refers to torch_xla.distributed.xla_multiprocessing
The environment variables
XLA_DOWNCAST_BF16
andXLA_USE_BF16
are deprecated (warnings are shown when used) and will be removed in an upcoming release. Switch to automatic mixed-precision or usemodel.to(torch.bfloat16)
command to convert model to BF16 format. (see Migration From XLA_USE_BF16/XLA_DOWNCAST_BF16)The functions
xm.xrt_world_size()
,xm.get_ordinal()
, andxm.get_local_ordinal()
are deprecated and removed so there are errors when used. Switch toxr.world_size()
,xr.global_ordinal()
, andxr.local_ordinal()
respectively as replacements.The default behavior of
torch.load
parameterweights_only
is changed fromFalse
toTrue
. Settingweights_only
toTrue
may cause issues with pickling custom objects.If using
xmp.spawn
, thenprocs
argument is limited to 1 or None since v2.1. Previously, passing a value > 1 would result in a warning. In torch-xla 2.6+, passing a value > 1 will result in an error with an actionable message to useNEURON_NUM_DEVICES
to set the number of NeuronCores to use.
See v2.6 migration guide for additional changes needed if you are migrating from PyTorch NeuronX 2.5. See v2.5 migration guide for additional changes needed if you are migrating from PyTorch NeuronX 2.1.
Migrating inference scripts#
There are no code changes required in the inference scripts.
Troubleshooting and Known Issues#
Using the latest torch-xla v2.7 may result in increase in host memory usage#
Using the latest torch-xla v2.7 may result in an increase in host memory usage compared to torch-xla v2.6. In one example, LLama2 pretraining with ZeRO1 and sequence length 16k could see an increase of 1.6% in host memory usage.
TypeError: AdamW.__init__() got an unexpected keyword argument ‘decoupled_weight_decay’#
AdamW now has an additional argument “decoupled_weight_decay” which defaults to False. If you get “TypeError: AdamW.__init__() got an unexpected keyword argument ‘decoupled_weight_decay’” with NeuronX Distributed, update to the latest version.
Tensor split on second dimension of 2D array not working#
Currently, when using the tensor split operation on a 2D array in the second dimension, the resulting tensors do not contain the expected data (pytorch/xla#8640). The workaround is to set XLA_DISABLE_FUNCTIONALIZATION=0
. Another workaround is to use torch.tensor_split
.
Lower BERT pretraining performance when switch to using model.to(torch.bfloat16)
#
Currently, BERT pretraining performance is approximately 11% lower when switching to using model.to(torch.bfloat16)
as part of migration away from the deprecated environment variable XLA_DOWNCAST_BF16
due to pytorch/xla#8545. As a workaround to recover the performance, you can set XLA_DOWNCAST_BF16=1
, which will still work in torch-neuronx 2.5 and 2.7 although there will be end-of-support warnings (as noted below).
Warning “XLA_DOWNCAST_BF16 will be deprecated after the 2.6 release, please downcast your model directly”#
Environment variables XLA_DOWNCAST_BF16
and XLA_USE_BF16
are deprecated (warnings are shown when used). Switch to automatic mixed-precision or use model.to(torch.bfloat16)
command to cast model to BF16. (see Migration From XLA_USE_BF16/XLA_DOWNCAST_BF16)
AttributeError: <module ‘torch_xla.core.xla_model’ … does not have the attribute ‘xrt_world_size’#
This is an error that torch_xla.core.xla_model.xrt_world_size()
is removed in torch-xla version 2.7. Switch to using torch_xla.runtime.world_size()
instead. If using Hugging Face transformers/accelerate libraries, use transformers==4.53.* and accelerate==1.7.*.
AttributeError: <module ‘torch_xla.core.xla_model’ … does not have the attribute ‘get_ordinal’#
This is an error that torch_xla.core.xla_model.get_ordinal()
is removed in torch-xla version 2.7. Switch to using torch_xla.runtime.global_ordinal()
instead. If using Hugging Face transformers/accelerate libraries, use transformers==4.53.* and accelerate==1.7.*.
AttributeError: <module ‘torch_xla.core.xla_model’ … does not have the attribute ‘get_local_ordinal’#
This is an error that torch_xla.core.xla_model.get_local_ordinal()
is removed in torch-xla version 2.7. Switch to using torch_xla.runtime.local_ordinal()
instead. If using Hugging Face transformers/accelerate libraries, use transformers==4.53.* and accelerate==1.7.*.
Socket Error: Socket failed to bind#
In PyTorch 2.7, there must be a socket available for both torchrun and the init_process_group
to bind. By default, both
will be set to use unused sockets. If you plan to use a MASTER_PORT
environment variable then this error may occur if the port you set it to
is already in use.
[W socket.cpp:426] [c10d] The server socket has failed to bind to [::]:2.700 (errno: 98 - Address already in use).
[W socket.cpp:426] [c10d] The server socket has failed to bind to ?UNKNOWN? (errno: 98 - Address already in use).
[E socket.cpp:462] [c10d] The server socket has failed to listen on any local network address.
RuntimeError: The server socket has failed to listen on any local network address.
The server socket has failed to bind to ?UNKNOWN? (errno: 98 - Address already in use).
To resolve the issue, if you are setting MASTER_PORT
, ensure that the port you’re setting it to is not used anywhere else in your scripts. Otherwise,
you can leave MASTER_PORT
unset and torchrun will set the default port for you.
AttributeError: module 'torch' has no attribute 'xla'
Failure#
In PyTorch 2.7, training scripts might fail during activation checkpointing with the error shown below.
AttributeError: module 'torch' has no attribute 'xla'
The solution is to use torch_xla.utils.checkpoint.checkpoint
instead of torch.utils.checkpoint.checkpoint
as the checkpoint function while wrapping pytorch modules for activation checkpointing.
Refer to the pytorch/xla discussion regarding this issue.
Also set use_reentrant=True
while calling the torch_xla checkpoint function. Failure to do so will lead to XLA currently does not support use_reentrant==False
error.
For more details on checkpointing, refer the documentation.
Error Attempted to access the data pointer on an invalid python storage
when using HF Trainer API#
While using HuggingFace Transformers Trainer API to train (i.e. HuggingFace Trainer API fine-tuning tutorial), you may see the error “Attempted to access the data pointer on an invalid python storage”. This is a known issue and has been fixed in the version 4.37.3
of HuggingFace Transformers.
FileNotFoundError: [Errno 2] No such file or directory: 'libneuronpjrt-path'
Failure#
In PyTorch 2.7, users might face the error shown below due to incompatible libneuronxla
and torch-neuronx
versions being installed.
FileNotFoundError: [Errno 2] No such file or directory: 'libneuronpjrt-path'
Check that the version of libneuronxla
that supports PyTorch NeuronX 2.7 is 2.2.*
. If not, then uninstall libneuronxla
using pip uninstall libneuronxla
and then reinstall the packages following the installation guide installation guide
Input dimension should be either 1 or equal to the output dimension it is broadcasting into
or IndexError: index out of range
error during Neuron Parallel Compile#
When running Neuron Parallel Compile with HF Trainer API, you may see the errors Status: INVALID_ARGUMENT: Input dimension should be either 1 or equal to the output dimension it is broadcasting into
or IndexError: index out of range
in Accelerator’s pad_across_processes
function. This is due to data-dependent operations in evaluation metrics computation. Data-dependent operations would result in undefined behavior with Neuron Parallel Compile trial execution (execute empty graphs with zero outputs). To work around this error, disable compute_metrics when NEURON_EXTRACT_GRAPHS_ONLY is set to 1:
compute_metrics=None if os.environ.get("NEURON_EXTRACT_GRAPHS_ONLY") else compute_metrics
Compiler assertion error when running Stable Diffusion training#
With PyTorch 2.7 (torch-neuronx), you may encounter the following compiler assertion error with Stable Diffusion training when gradient accumulation is enabled. This will be fixed in an upcoming release. For now, if you want to run Stable Diffusion training, disable gradient accumulation in torch-neuronx 2.7 by keeping the default gradient accumulation steps of 1.
ERROR 222163 [NeuronAssert]: Assertion failure in usr/lib/python3.9/concurrent/futures/process.py at line 239 with exception:
too many partition dims! {{0,+,960}[10],+,10560}[10]
Frequently Asked Questions (FAQ)#
Do I need to recompile my models with PyTorch 2.7?#
Yes.
Do I need to update my scripts for PyTorch 2.7?#
See the migration guide
What environment variables will be changed with PyTorch NeuronX 2.7 ?#
The environment variables XLA_DOWNCAST_BF16
and XLA_USE_BF16
are deprecated (warnings are shown when used). Switch to automatic mixed-precision or use model.to(torch.bfloat16)
command to cast model to BF16. (see Migration From XLA_USE_BF16/XLA_DOWNCAST_BF16)
What features will be missing with PyTorch NeuronX 2.7?#
PyTorch NeuronX 2.7 has all of the supported features in PyTorch NeuronX 2.6, with known issues listed above, and unsupported features as listed in PyTorch Neuron (torch-neuronx) release notes.
Can I use Neuron Distributed and Transformers Neuron libraries with PyTorch NeuronX 2.7?#
Yes, NeuronX Distributed and Transformers NeuronX are supported by PyTorch NeuronX 2.7. AWS Neuron Reference for NeMo Megatron has reached end-of-support in release 2.23.
Can I still use PyTorch 2.6 version?#
PyTorch 2.6 is supported since release 2.23.
Can I still use PyTorch 2.5 version?#
PyTorch 2.5 is supported for releases 2.21 to 2.24 and will reach end-of-life in a future release. Additionally, the CVE CVE-2025-32434 affects PyTorch version 2.5. We recommend upgrading to the new version of Torch-NeuronX by following PyTorch Neuron (torch-neuronx) Setup.
Can I still use PyTorch 2.1 version?#
PyTorch 2.1 is supported for release 2.21 and has reached end-of-life in release 2.22. Additionally, the CVEs CVE-2024-31583 and CVE-2024-31580 affect PyTorch versions 2.1 and earlier. We recommend upgrading to the new version of Torch-NeuronX by following PyTorch Neuron (torch-neuronx) Setup.
This document is relevant for: Inf1
, Inf2
, Trn1
, Trn2