When converting from .pth to onnx using the torch.onnx.dynamo_export, I encountered the following errors.
/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/onnx/_internal/exporter.py:130: UserWarning: torch.onnx.dynamo_export only implements opset version 18 for now. If you need to use a different opset version, please register them with register_custom_op.
warnings.warn(
[2023-12-21 10:51:58,910] [0/0] torch._dynamo.variables.higher_order_ops: [WARNING] speculate_subgraph: while introspecting the user-defined autograd.Function, we were unable to trace function `trampoline_autograd_fwd` into a single graph. This means that Dynamo was unable to prove safety for this API and will fall back to eager-mode PyTorch, which could lead to a slowdown.
[2023-12-21 10:51:58,910] [0/0] torch._dynamo.variables.higher_order_ops: [ERROR] call_method UserDefinedObjectVariable(fused_bias_act) __call__ [TensorVariable(), TensorVariable(), TensorVariable(), ConstantVariable(int), ConstantVariable(int), ConstantVariable(float), ConstantVariable(float)] {}
Traceback (most recent call last):
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/higher_order_ops.py", line 152, in speculate_subgraph
output = f.call_function(tx, args, sub_kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/functions.py", line 261, in call_function
return super().call_function(tx, args, kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/functions.py", line 90, in call_function
return tx.inline_user_function_return(
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 598, in inline_user_function_return
result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 2179, in inline_call
return cls.inline_call_(parent, func, args, kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 2286, in inline_call_
tracer.run()
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 724, in run
and self.step()
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 688, in step
getattr(self, inst.opname)(inst)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 392, in wrapper
return inner_fn(self, inst)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 1155, in CALL_FUNCTION_EX
self.call_function(fn, argsvars.items, kwargsvars.items)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 562, in call_function
self.push(fn.call_function(self, args, kwargs))
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/misc.py", line 594, in call_function
return self.obj.call_method(tx, self.name, args, kwargs).add_options(self)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/misc.py", line 413, in call_method
return tx.inline_call(tx, forward, args, kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 2179, in inline_call
return cls.inline_call_(parent, func, args, kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 2286, in inline_call_
tracer.run()
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 724, in run
and self.step()
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 688, in step
getattr(self, inst.opname)(inst)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 392, in wrapper
return inner_fn(self, inst)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 1115, in CALL_FUNCTION
self.call_function(fn, args, {})
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 562, in call_function
self.push(fn.call_function(self, args, kwargs))
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/user_defined.py", line 383, in call_function
return self.call_method(tx, "__call__", args, kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/user_defined.py", line 303, in call_method
return super().call_method(tx, name, args, kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/base.py", line 329, in call_method
raise unimplemented(f"call_method {self} {name} {args} {kwargs}")
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/exc.py", line 172, in unimplemented
raise Unsupported(msg)
torch._dynamo.exc.Unsupported: call_method UserDefinedObjectVariable(fused_bias_act) __call__ [TensorVariable(), TensorVariable(), TensorVariable(), ConstantVariable(int), ConstantVariable(int), ConstantVariable(float), ConstantVariable(float)] {}
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/onnx/_internal/exporter.py", line 1195, in dynamo_export
).export()
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/onnx/_internal/exporter.py", line 941, in export
graph_module = self.options.fx_tracer.generate_fx(
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/onnx/_internal/fx/dynamo_graph_extractor.py", line 199, in generate_fx
graph_module, graph_guard = torch._dynamo.export(
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py", line 1140, in inner
result_traced = opt_f(*args, **kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py", line 328, in _fn
return fn(*args, **kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/onnx/_internal/fx/dynamo_graph_extractor.py", line 154, in wrapped
return output_adapter.apply(model_func(*args, **kwargs))
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py", line 490, in catch_errors
return callback(frame, cache_entry, hooks, frame_state)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 133, in _fn
return fn(*args, **kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 389, in _convert_frame_assert
return _compile(
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 569, in _compile
guarded_code = compile_inner(code, one_graph, hooks, transform)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/utils.py", line 189, in time_wrapper
r = func(*args, **kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 491, in compile_inner
out_code = transform_code_object(code, transform)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/bytecode_transformation.py", line 1028, in transform_code_object
transformations(instructions, code_options)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 458, in transform
tracer.run()
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 2074, in run
super().run()
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 724, in run
and self.step()
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 688, in step
getattr(self, inst.opname)(inst)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 392, in wrapper
return inner_fn(self, inst)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 1115, in CALL_FUNCTION
self.call_function(fn, args, {})
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 562, in call_function
self.push(fn.call_function(self, args, kwargs))
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/nn_module.py", line 266, in call_function
tx.call_function(
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 562, in call_function
self.push(fn.call_function(self, args, kwargs))
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/nn_module.py", line 331, in call_function
return tx.inline_user_function_return(
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 598, in inline_user_function_return
result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 2179, in inline_call
return cls.inline_call_(parent, func, args, kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 2286, in inline_call_
tracer.run()
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 724, in run
and self.step()
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 688, in step
getattr(self, inst.opname)(inst)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 392, in wrapper
return inner_fn(self, inst)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 1155, in CALL_FUNCTION_EX
self.call_function(fn, argsvars.items, kwargsvars.items)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 562, in call_function
self.push(fn.call_function(self, args, kwargs))
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/functions.py", line 307, in call_function
return super().call_function(tx, args, kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/functions.py", line 261, in call_function
return super().call_function(tx, args, kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/functions.py", line 90, in call_function
return tx.inline_user_function_return(
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 598, in inline_user_function_return
result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 2179, in inline_call
return cls.inline_call_(parent, func, args, kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 2286, in inline_call_
tracer.run()
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 724, in run
and self.step()
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 688, in step
getattr(self, inst.opname)(inst)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 392, in wrapper
return inner_fn(self, inst)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 1115, in CALL_FUNCTION
self.call_function(fn, args, {})
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 562, in call_function
self.push(fn.call_function(self, args, kwargs))
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/functions.py", line 261, in call_function
return super().call_function(tx, args, kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/functions.py", line 90, in call_function
return tx.inline_user_function_return(
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 598, in inline_user_function_return
result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 2179, in inline_call
return cls.inline_call_(parent, func, args, kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 2286, in inline_call_
tracer.run()
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 724, in run
and self.step()
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 688, in step
getattr(self, inst.opname)(inst)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 392, in wrapper
return inner_fn(self, inst)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 1115, in CALL_FUNCTION
self.call_function(fn, args, {})
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 562, in call_function
self.push(fn.call_function(self, args, kwargs))
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/misc.py", line 583, in call_function
return self.obj.call_apply(tx, args, kwargs).add_options(self)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/misc.py", line 333, in call_apply
speculated_fwd_result = higher_order_autograd_fn.call_function(
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/higher_order_ops.py", line 966, in call_function
) = speculate_subgraph(
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/_dynamo/variables/higher_order_ops.py", line 211, in speculate_subgraph
raise Unsupported(
torch._dynamo.exc.Unsupported: speculate_subgraph: while introspecting the user-defined autograd.Function, we were unable to trace function `trampoline_autograd_fwd` into a single graph. This means that Dynamo was unable to prove safety for this API and will fall back to eager-mode PyTorch, which could lead to a slowdown. Scroll up for the stack trace of the initial exception. The reason was: call_method UserDefinedObjectVariable(fused_bias_act) __call__ [TensorVariable(), TensorVariable(), TensorVariable(), ConstantVariable(int), ConstantVariable(int), ConstantVariable(float), ConstantVariable(float)] {}
from user code:
File "/home/batman/GFPGAN-Training-Models-To-Onnx/gfpgan/archs/gfpganv1_arch.py", line 367, in forward
feat = self.conv_body_first(x)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/basicsr/ops/fused_act/fused_act.py", line 91, in forward
return fused_leaky_relu(input, self.bias, self.negative_slope, self.scale)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/basicsr/ops/fused_act/fused_act.py", line 95, in fused_leaky_relu
return FusedLeakyReLUFunction.apply(input, bias, negative_slope, scale)
Set TORCH_LOGS="+dynamo" and TORCHDYNAMO_VERBOSE=1 for more information
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/batman/GFPGAN-Training-Models-To-Onnx/new_from_github.py", line 59, in <module>
torch.onnx.dynamo_export(inference_model, cropped_face_t)
File "/home/batman/GFPGAN-Training-Models-To-Onnx/onnxVenv/lib/python3.10/site-packages/torch/onnx/_internal/exporter.py", line 1206, in dynamo_export
raise OnnxExporterError(
torch.onnx.OnnxExporterError: Failed to export the model to ONNX. Generating SARIF report at 'report_dynamo_export.sarif'. SARIF is a standard format for the output of static analysis tools. SARIF logs can be loaded in VS Code SARIF viewer extension, or SARIF web viewer (https://microsoft.github.io/sarif-web-component/). Please report a bug on PyTorch Github: https://github.com/pytorch/pytorch/issues
Code to convert .pth to onnx
import cv2
from basicsr.utils import img2tensor
from torchvision.transforms.functional import normalize
import torch
from gfpgan.archs.gfpganv1_arch import GFPGANv1
model_path = "./GFPGANv1.pth"
onnx_path = "./experiments/GFPGAN_v1.onnx"
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print("-------------------", device)
inference_model = GFPGANv1(
out_size=512,
num_style_feat=512,
channel_multiplier=1,
decoder_load_path=None,
fix_decoder=False,
num_mlp=8,
input_is_latent=True,
different_w=True,
narrow=1,
sft_half=True).to(device)
loadnet = torch.load(model_path)
if 'params_ema' in loadnet:
keyname = 'params_ema'
else:
keyname = 'params'
inference_model.load_state_dict(loadnet[keyname], strict=False)
inference_model = inference_model.eval()
img_path = './1.png'
input_img = cv2.imread(img_path, cv2.IMREAD_COLOR)
img = cv2.resize(input_img, (512, 512))
cropped_face_t = img2tensor(img / 255., bgr2rgb=True, float32=True)
normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
cropped_face_t = cropped_face_t.unsqueeze(0).to(device)
mat1 = torch.randn(3, 512, 512).to(device) # moving the tensor to cpu
mat1 = mat1.unsqueeze(0).to(device)
#torch_out = torch.jit.trace(inference_model, cropped_face_t)
torch.onnx.dynamo_export(inference_model, cropped_face_t)
print("export GFPGANv1 onnx done.")
report_dynamo_export.sarif File
{
"runs":[
{
"tool":{
"driver":{
"name":"torch.onnx.dynamo_export",
"contents":[
"localizedData",
"nonLocalizedData"
],
"language":"en-US",
"rules":[],
"version":"2.1.0+cu118"
}
},
"language":"en-US",
"newlineSequences":[
"\r\n",
"\n"
],
"results":[]
}
],
"version":"2.1.0",
"schemaUri":"https://docs.oasis-open.org/sarif/sarif/v2.1.0/cs01/schemas/sarif-schema-2.1.0.json"
}