Error when Convert Pytorch model to TorchScript: " RuntimeError: undefined value super:"

Duong_Pham_Anh: <p>I got this error while trying to convert nn.module to torchscript.</p>
<pre><code class="lang-auto">RuntimeError:
undefined value super:
  File "D:\project\Kikai project\DIFRINT\models\correlation\correlation.py", line 147
        def __init__(self):
                super(FunctionCorrelation, self).__init__()
  ~~~~~ &lt;--- HERE
'FunctionCorrelation.__init__' is being compiled since it was called from '__torch__.models.correlation.correlation.FunctionCorrelation'
  File "D:\project\Kikai project\DIFRINT\models\correlation\correlation.py", line 238
        def forward(self, tensorFirst, tensorSecond):
                return FunctionCorrelation.apply(tensorFirst, tensorSecond)
         ~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
'__torch__.models.correlation.correlation.FunctionCorrelation' is being compiled since it was called from 'ModuleCorrelation.forward'
  File "D:\project\Kikai project\DIFRINT\models\correlation\correlation.py", line 238
        def forward(self, tensorFirst, tensorSecond):
                return FunctionCorrelation.apply(tensorFirst, tensorSecond)
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
</code></pre>
<p>This is my code</p>
<pre><code class="lang-auto">class FunctionCorrelation(torch.autograd.Function):
	def __init__(self):
		super(FunctionCorrelation, self).__init__()
		# super().__init__()
	# end

	@staticmethod
	def forward(self, first, second):
		self.save_for_backward(first, second)
</code></pre>
<p>and</p>
<pre><code class="lang-auto">class ModuleCorrelation(torch.nn.Module):
	def __init__(self):
		super(ModuleCorrelation, self).__init__()
		# super().__init__()
	# end

	def forward(self, tensorFirst, tensorSecond):
		return FunctionCorrelation.apply(tensorFirst, tensorSecond)
	# end
</code></pre>
<p>I used torch==1.6.0, torchvision==0.7.0,  cupy-cuda102==7.8.0, Pillow==6.1.0<br>
I think may be get problem when i use <em>torch.autograd.Function</em>  this cause i have to use <span class="mention">@staticmethod</span> in torch==1.6.0<br>
Someone can help me<br>
thank</p>

tom: <p>At this point, you cannot use autograd.Functions with the JIT.</p>

Duong_Pham_Anh: <p><a class="mention" href="/u/tom">@tom</a><br>
so have some thing can replace <em>torch.autograd.Function</em>  that TorchScript support</p>

tom: <p>Well, so if you push your autograd function to C++ you can wrap the function with a JIT operator. You cannot currently do that in Python, though.</p>
<p>I once made a prototype of a scripteable function interface, but I never pushed it to the point where it was a mergeable PR.</p>

Duong_Pham_Anh: <p><a class="mention" href="/u/tom">@tom</a><br>
in my case. I have trained model in pytorch and i want convert it to C++<br>
so i dont need train or calculate gradient with autograd function just convert trained model<br>
may be have way to replate autograd function to normal function to Script it???</p>

tom: <p>If you trace the model, you can just use <code>torch.jit.is_tracing()</code> to check whether you want the JIT-compatible bit and use the function otherwise.<br>
There also is <code>torch.jit.is_scripting()</code> for the scripting equivalent.</p>
<p>Best regards</p>
<p>Thomas</p>

Duong_Pham_Anh: <p><a class="mention" href="/u/tom">@tom</a><br>
thank you very much</p>

fermat97: <p>Hi, I got the same error:</p>
<blockquote>
<p>RuntimeError: undefined value super:</p>
</blockquote>
<p>while trying to TorchScript  <code>torch.nn.BCEWithLogitsLoss</code>. Do you have any idea?</p>

fermat97: <p>It seems in my case the reason is due to the fact that only <code>instances</code> of <code>nn.Modules</code> can be scripted. Therefore, it is necessary to first produce an instance out of <code>torch.nn.BCEWithLogitsLoss</code> and then continue with <code>TorchScripting</code> that instance.</p>