How can I disable guards generation when using torch.onnx.export(dynamo=True) with dynamic_shapes. Guards generated suggests fix which is wrong and doesn’t work.
In my case, it suggests following fixes which is wrong. Model works with any number of frames in torchscript.
When you use dynamo=True in ONNX export, PyTorch tries to be helpful by suggesting constraints on your dynamic dimensions, but as you’ve discovered, these suggestions can be overly restrictive or simply incorrect.
Try this first
# Reset dynamo state
torch._dynamo.reset()
# Disable ALL shape specialization
torch._dynamo.config.automatic_dynamic_shapes = False
torch._dynamo.config.specialize_int = False
torch._dynamo.config.assume_static_by_default = False
torch._dynamo.config.guard_nn_modules = False
# Now export your model
torch.onnx.export(
model,
example_input,
"model.onnx",
input_names=['input'],
output_names=['output'],
dynamic_axes={'input': {1: 'frames'}}, # Assuming frames is dimension 1
dynamo=True,
export_options=ExportOptions(dynamic_shapes=True)
)
Also you can try a more legacy approach
torch.onnx.export(
model,
example_input,
"model.onnx",
input_names=['input'],
output_names=['output'],
dynamic_axes={'input': {1: 'frames'}},
opset_version=17, # Use a recent opset
# Note: no dynamo=True here
)