This document is relevant for: Trn2, Trn3
TorchNeuron environment variables#
This reference lists the environment variables available to configure
torch-neuronx behavior. Use this page to look up variables that control
the neuron backend for torch.compile, compilation caching, eager
execution, distributed collectives, metrics, profiling, and the Neuron memory
allocator.
Applies to#
torch-neuronxTrn2 and Trn3 Trainium instances
neuron backend for torch.compile#
Variable |
Type |
Default |
Description |
|---|---|---|---|
|
str |
|
Directory where compilation artifacts (FX graphs, StableHLO IR, NEFF files) are saved. |
|
bool |
|
When |
|
bool |
|
When |
|
bool |
PyTorch >= 2.13: |
Enables framework decomposition of complex operations before lowering. When disabled, verified native complex operations use compiler lowering and other operations follow the existing fallback policy. |
|
bool |
|
When |
|
bool |
|
When |
|
str |
(query live instance) |
Target platform (for example |
Artifact directory structure#
Both torch.compile and eager execution use the same unified job-based
artifact hierarchy. The working directory used by all neuronx-cc calls is
preserved under TORCH_NEURONX_DEBUG_DIR.
{TORCH_NEURONX_DEBUG_DIR}/torch_neuronx/rank{rank}/{job_id}/
├── fx_graphs/{model}_{ts}.fx.txt
├── transformed_fx/{model}_{ts}.transformed.fx.txt
├── raw/{model}_{ts}.torch_raw.mlir
├── stablehlo/{model}_{ts}.stablehlo.mlir
├── torch_mlir_error/{model}_{ts}.mlir # Populated when torch-mlir fails
├── inputs/ # Only when TORCH_NEURONX_DYNAMO_TENSOR_DUMPS is set
└── outputs/ # Only when TORCH_NEURONX_DYNAMO_TENSOR_DUMPS is set
{TORCH_NEURONX_DEBUG_DIR}/neuronx-cc/{hash}/
├── log-neuron-cc.txt
├── module.mlir
├── module.neff
├── metadata.txt
└── compiler_args.txt
When TORCH_NEURONX_PRESERVE_COMPILATION_ARTIFACTS=1, all directories and
their contents are preserved for debugging and analysis.
Compiler#
See Neuron Compiler CLI reference for available compiler flags.
Persistent NEFF / HLO cache#
Compiled NEFFs and lowered HLO entries are cached on disk to avoid recompilation across runs. The cache is content-addressed; entries from one run can be reused by another as long as the inputs (HLO bytes, compiler config, compiler version) are unchanged.
Variable |
Type |
Default |
Description |
|---|---|---|---|
|
path |
|
Directory for persistent NEFF cache. Point at a shared mount (for example, FSx) to reuse compiled NEFFs across runs and instances. |
|
path |
|
Directory for persistent HLO cache, shared by the eager and |
|
bool |
|
When |
|
path |
|
Local directory for filelocks and scratch. Must stay on a local POSIX filesystem (NFS/Lustre lock semantics are unreliable). |
|
bool |
|
When |
|
int |
|
Seconds to wait for the per-key compile lock before giving up. |
|
bool |
|
When |
|
bool |
|
When |
|
bool |
|
When |
|
path |
|
Directory for the torch inductor cache. This is a PyTorch variable and affects the location of other caches as well. Refer to the PyTorch documentation for details on other caches. |
General (eager execution)#
These variables affect eager execution (non-compiled operations).
Variable |
Type |
Default |
Description |
|---|---|---|---|
|
bool |
|
When |
|
bool |
|
When |
|
bool |
|
When |
|
int |
|
Log verbosity: |
|
bool |
|
(Experimental) When |
Neuron Runtime#
See Neuron Runtime configuration for all runtime variables.
See Logical NeuronCore Configuration for LNC details.
Metrics#
Variable |
Type |
Default |
Description |
|---|---|---|---|
|
str |
|
Comma-separated metric tables to enable for CSV export. |
|
str |
|
Directory for metric CSV files. |
Profiling#
These variables set the options of the default Neuron profiling session. A
NeuronConfig overrides them. CPU-only profiling does not start a session,
and a run without the profiler is not affected.
torch-neuronx starts the default session when torch.profiler.profile
gets ProfilerActivity.PrivateUse1 and no NeuronConfig in
experimental_config.
Variable |
Type |
Default |
Description |
|---|---|---|---|
|
bool |
|
Set to |
|
str |
|
Comma-separated profile modes. Add |
|
str |
|
Comma-separated global ranks that capture a profile. The rank comes from the |
|
int |
|
Maximum number of system trace events for each NeuronCore. |
|
str |
|
NeuronCore indices or ranges to capture, for example |
|
path |
|
Directory for the profile session output. |
With the device mode, the session copies the NEFFs from
TORCH_NEURONX_NEFF_CACHE_DIR (default /tmp/neff_cache) to the neffs
directory in TORCH_NEURONX_PROFILE_OUTPUT_DIR. The device profile needs
these NEFFs.
Distributed#
Variable |
Type |
Default |
Description |
|---|---|---|---|
|
bool |
|
When |
|
bool |
|
When |
|
bool |
on (default stream) |
Enables split-based allgather for better memory efficiency. When unset, the default depends on the stream; when set, only |
|
bool |
on (default stream) |
Enables split-based reduce_scatter. When unset, the default depends on the stream; when set, only |
|
bool |
|
Enables native NKI |
|
int |
|
Maximum bucket size (MB) for collective operations. |
Neuron memory allocator configuration#
Variable |
Type |
Default |
Description |
|---|---|---|---|
|
bool |
|
When |
The Neuron caching allocator’s segment-pool tuning knobs are exposed through a single environment variable:
TORCH_NEURONX_SEGMENT_ALLOCATOR_CONFIG=key1=value1,key2=value2,...
The value is a comma-separated list of key=value pairs. The first call to
the allocator parses the variable, validates the result, and snapshots it into
the per-device NeuronSegmentPool at construction. The snapshot is fixed for
the pool’s lifetime. To change tuning, set the variable and restart the
process.
Format
Sizes: raw bytes as a non-negative decimal integer. No size suffixes;
1048576is valid,1MBis rejected with a warning.Plain integers (only
kRoundupPower2Divisionstoday): same syntax, bare digits.Unknown keys are ignored with a warning.
Invalid values are ignored with a warning; the field keeps its default.
Default values are tuned from training-workload experiments. Override them if you run into out-of-memory (OOM) errors resulting from fragmentation.
Warning
Changing these values can have drastic effects on memory allocation in training workloads and can potentially induce OOMs.
Key |
Type |
Default (bytes) |
Definition |
|---|---|---|---|
|
bytes |
|
Allocations below this go to the small pool, at or above to the large pool. The two pools have separate free sets so small allocations cannot fragment large-pool segments and vice versa. |
|
bytes |
|
Segment size NRT-allocates for small-pool requests. |
|
bytes |
|
Segment size NRT-allocates for large-pool requests in |
|
bytes |
|
Above this, each request gets its own per-request segment sized to the request rounded up to |
|
bytes |
|
Alignment for per-request segments above |
|
bytes |
|
Floor for |
|
int |
|
|
|
bytes |
|
Small-pool minimum split remainder. If carving a chosen free block would leave a remainder smaller than this, the block is handed out whole instead. |
|
bytes |
|
Large-pool minimum split remainder. Same idea as above for the large pool. |
|
bytes |
|
Cap above which a free block cannot be split. Default unlimited (all blocks splittable). Setting a finite cap keeps large blocks intact for future large requests, at the risk of cache misses for small requests when only above-cap blocks are free. Pairs with |
|
bytes |
|
When a free block is non-splittable ( |
Examples
Cap large-block splitting at 512 MB; allow up to 64 MB rounding waste on non-split reuse:
TORCH_NEURONX_SEGMENT_ALLOCATOR_CONFIG="kMaxSplitSizeBytes=536870912,kMaxNonSplitRoundingBytes=67108864"
Reduce internal fragmentation in the medium-size range by widening rounding divisions to 8:
TORCH_NEURONX_SEGMENT_ALLOCATOR_CONFIG="kRoundupPower2Divisions=8"