How to debug torch.nn.export()


I’m trying to convert a Pytorch model to CoreML format. As suggested, I’m aproaching this issue in two steps:

  • Pytorch to ONNX
  • ONNX to CoreML format

However, while converting a simple UNET with ResNet backend to ONNX, I get the following error:
RuntimeError: Failed to export an ONNX attribute, since it’s not constant, please try to make things (e.g., kernel size) static if possible

I took a look at torch.onnx code, but it is not easy to find exactly what line of the code (model) is provoking this error.

So, my question is: how to debug this conversion? There is a way to find what is causing this error? (turning verbose to True do not help)


You can put prints everywhere you want to debug. The ONNX serializer traces the model, so it will execute it once, take a look at the documentation for more details.

Here is a snippet showing how you could debug:

import torch
class Conv(torch.nn.Module):
	def __init__(self):
		super(Conv, self).__init__()
		self.conv = torch.nn.Conv2d(3, 7, 5, 1, 2)
	def forward(self, x):
		print("-" * 40)
		print("DEBUG forward BEFORE convolution")
		print("-" * 40)
		x = self.conv(x)
		print("-" * 40)
		print("DEBUG forward AFTER convolution")
		print("-" * 40)
		return x

model = Conv()
dummy_input = torch.rand(16, 3, 100, 100)
torch.onnx.export(model, dummy_input, "debug.onnx", verbose=True)

Thanks @LeviViana,

I did not know that it would work. Looking at the call stack the execution was not anywhere near my model (it was inside torch.onnx in many calls). I took a look at the onnx code and found out where I could print the name of the current operation (even if symbolic), so I did that using gdb as I usually do.

In the end I found out a bug that is already registered at github.