RuntimeError
torch.onnx.export.RuntimeError
Stack trace
RuntimeError: Exporting the model to ONNX failed because dynamic axes were not specified for inputs or outputs with variable batch size or sequence length.
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
No dynamic_axes argument provided for inputs or outputs with variable batch size or sequence length.
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'}}.
Incorrect keys or dimension indices in the dynamic_axes dictionary that do not match model input/output names or shapes.
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.
Using a model with nested or tuple outputs without specifying dynamic axes for all variable dimensions.
Explicitly specify dynamic axes for each output tensor in the nested structure, matching their names and variable dimensions.
Code: broken vs fixed
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") 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.") 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.