High severity intermediate · Fix: 5-10 min

RuntimeError

torch.onnx.export.RuntimeError

What this error means
This error occurs when exporting a PyTorch model to ONNX format without properly specifying dynamic axes for inputs or outputs that have variable dimensions.

Stack trace

traceback
RuntimeError: Exporting the model to ONNX failed because dynamic axes were not specified for inputs or outputs with variable batch size or sequence length.
QUICK FIX
Add a correct dynamic_axes parameter to torch.onnx.export mapping variable dimensions to names for all inputs and outputs.

Why it happens

PyTorch's ONNX exporter requires explicit dynamic_axes mapping for any input or output tensor dimension that can vary, such as batch size or sequence length. Without this, the exporter assumes fixed sizes and fails when encountering variable dimensions during export.

Detection

Check for RuntimeError during torch.onnx.export calls mentioning dynamic axes or shape mismatch; log the export parameters and input tensor shapes to identify missing dynamic axes.

Causes & fixes

1

No dynamic_axes argument provided for inputs or outputs with variable batch size or sequence length.

✓ Fix

Add a dynamic_axes dictionary to torch.onnx.export specifying which input/output dimensions are dynamic, e.g., {'input': {0: 'batch_size'}, 'output': {0: 'batch_size'}}.

2

Incorrect keys or dimension indices in the dynamic_axes dictionary that do not match model input/output names or shapes.

✓ Fix

Verify that the keys in dynamic_axes exactly match the input/output names used in the model and that dimension indices correspond to the correct axes.

3

Using a model with nested or tuple outputs without specifying dynamic axes for all variable dimensions.

✓ Fix

Explicitly specify dynamic axes for each output tensor in the nested structure, matching their names and variable dimensions.

Code: broken vs fixed

Broken - triggers the error
python
import torch

model = MyModel()
input_tensor = torch.randn(4, 3, 224, 224)

# This line triggers the dynamic axes export error
torch.onnx.export(model, input_tensor, "model.onnx")
Fixed - works correctly
python
import os
import torch

os.environ['TORCH_ONNX_DEBUG'] = '1'  # Enable debug info for export

model = MyModel()
input_tensor = torch.randn(4, 3, 224, 224)
dynamic_axes = {'input': {0: 'batch_size'}, 'output': {0: 'batch_size'}}

# Fixed: specify dynamic axes for variable batch dimension
torch.onnx.export(model, input_tensor, "model.onnx", dynamic_axes=dynamic_axes)
print("ONNX export succeeded with dynamic axes specified.")
Added the dynamic_axes argument to torch.onnx.export to specify which input and output dimensions are dynamic, allowing variable batch sizes during export.

Workaround

Wrap the export call in try/except RuntimeError, and if the error relates to dynamic axes, manually set fixed input sizes or export multiple fixed-size models as a temporary measure.

Prevention

Always define dynamic_axes for any input or output tensor dimension that can vary before exporting to ONNX, and validate input/output names and shapes to avoid export failures.

Python 3.7+ · torch >=1.7.0 · tested on 2.0.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.