RuntimeError
torch.onnx.errors.RuntimeError
Stack trace
RuntimeError: Exporting the operator 'my_custom_op' to ONNX is not supported. Please register a symbolic function for this operator or disable export of this operator.
Why it happens
PyTorch's ONNX exporter requires all operators used in the model to have corresponding symbolic functions that define how to translate them into ONNX graph nodes. Custom operators without registered symbolic functions cause the exporter to fail because it cannot map them to ONNX operators.
Detection
Monitor ONNX export logs for RuntimeError mentioning unsupported operators. Add validation to check if all custom ops have symbolic registrations before export.
Causes & fixes
Custom PyTorch operator lacks a registered ONNX symbolic function.
Implement and register a symbolic function for the custom operator using torch.onnx.register_custom_op_symbolic.
Using an outdated PyTorch version that does not support the custom operator export.
Upgrade PyTorch to a version that supports custom operator symbolic registration or includes the operator.
Attempting to export a model with dynamic control flow or unsupported ops without fallback.
Refactor the model to avoid unsupported ops or implement symbolic functions; alternatively, use torch.onnx.export with opset_version supporting the ops.
Code: broken vs fixed
import torch
class MyModel(torch.nn.Module):
def forward(self, x):
return torch.ops.my_namespace.my_custom_op(x)
model = MyModel()
torch.onnx.export(model, torch.randn(1,3,224,224), "model.onnx") # This line raises RuntimeError import os
import torch
# Register symbolic function for the custom op
from torch.onnx import register_custom_op_symbolic
def symbolic_my_custom_op(g, x):
return g.op("MyNamespace::MyCustomOp", x)
register_custom_op_symbolic("my_namespace::my_custom_op", symbolic_my_custom_op, 9)
class MyModel(torch.nn.Module):
def forward(self, x):
return torch.ops.my_namespace.my_custom_op(x)
model = MyModel()
torch.onnx.export(model, torch.randn(1,3,224,224), "model.onnx") # Fixed: symbolic registered Workaround
Wrap the custom operator call in a torch.jit.script function and export the scripted model, or replace the custom op with supported PyTorch ops temporarily.
Prevention
Design models using only operators with existing ONNX symbolic support or always register symbolic functions for custom ops before export.