TorchScript Error Unknown builtin op

username: DTSED
cooked: <p>Hi there,</p>
<p>I am trying to use a torch script exported module traced on iOS device but I am getting this error:</p>
<pre><code class="lang-auto">ibc++abi.dylib: terminating with uncaught exception of type torch::jit::ErrorReport: 
Unknown builtin op: aten::_batch_norm_impl_index_backward.
Could not find any similar ops to aten::_batch_norm_impl_index_backward. This op may not exist or may not be currently supported in TorchScript.
:
  File "&lt;string&gt;", line 19

            def backward(grad_output):
                dinput, dweight, dbias = torch._batch_norm_impl_index_backward(
                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
                    impl_idx, input, grad_output, weight, running_mean, running_var,
                    save1, save2, training, eps, [True, has_weight, has_bias], reserve)

</code></pre>
<p>This only occurs when loading the traced script on mobile. Using a desktop platform runs ok.<br>
I have read a related issue <a href="https://github.com/pytorch/pytorch/pull/30067" rel="nofollow noopener">pytorch repository issue</a> but it seems that it does not fix the problem in this case.<br>
Could anyone help me with this?</p>
<p>Thanks</p>

username: mark_jimenez
cooked: <p>Hey <a class="mention" href="/u/dtsed">@DTSED</a> were you able to figure this out? I’m encountering the same issue, not sure what in my torchscript model is triggering it.</p>

username: mark_jimenez
cooked: <p><a class="mention" href="/u/xta0">@xta0</a>, I can replicate the error on iOS using the code below. Not sure if this is a regression because I don’t see how <code>backward</code> is called anywhere.</p>
<p>Python code:</p>
<pre><code class="lang-auto">class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(784, 392)
        self.fc2 = nn.Linear(392, 10)

    def forward(self, x):
        x = self.fc1(x)
        x = nn.functional.relu(x)
        x = self.fc2(x)
        return x

n = Net()
X = th.randn(3, 28 * 28)


simple_net = th.jit.trace(n, X)
</code></pre>
<p>iOS C++ Code</p>
<pre><code class="lang-auto">auto qengines = at::globalContext().supportedQEngines();
if (std::find(qengines.begin(), qengines.end(), at::QEngine::QNNPACK) != qengines.end()) {
    at::globalContext().setQEngine(at::QEngine::QNNPACK);
}
_impl = torch::jit::load(filePath.UTF8String);
_impl.eval();

std::vector&lt;torch::jit::IValue&gt; modelArgs;

at::Tensor tensor = torch::from_blob(data, trainingDataVectorShape, at::kFloat);
modelArgs.push_back(tensor);

auto result = _impl.forward(modelArgs);

</code></pre>
<p>Resulting error:</p>
<pre><code class="lang-auto">libc++abi.dylib: terminating with uncaught exception of type torch::jit::ErrorReport: 
Unknown builtin op: aten::_batch_norm_impl_index_backward.
Could not find any similar ops to aten::_batch_norm_impl_index_backward. This op may not exist or may not be currently supported in TorchScript.
:
  File "&lt;string&gt;", line 19

            def backward(grad_output):
                dinput, dweight, dbias = torch._batch_norm_impl_index_backward(
                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
                    impl_idx, input, grad_output, weight, running_mean, running_var,
                    save1, save2, training, eps, [True, has_weight, has_bias], reserve)
</code></pre>

username: xta0
cooked: <p><a class="mention" href="/u/dtsed">@DTSED</a> <a class="mention" href="/u/mark_jimenez">@mark_jimenez</a> can you guys try adding <code>torch::autograd::AutoGradMode guard(false);</code> before calling <code>torch.jit.load</code> and <code>forward</code>?</p>
<pre><code class="lang-auto">    at::AutoNonVariableTypeMode nonVarTypeModeGuard(true);
    torch::autograd::AutoGradMode guard(false);
    auto model = torch::jit::load(path.UTF8String);
    auto input = torch::randn({3,784});
    auto output = model.forward({input});
    std::cout&lt;&lt;output.toTensor().sizes()&lt;&lt;std::endl;
</code></pre>
<p>Autograd features have been disabled on mobile, the current workaround is to use the RAII guard above. <code>torch::autograd::AutoGradMode guard(false);</code> is similar to <code>with torch.no_grad():</code> in python.</p>

username: mark_jimenez
cooked: <p>Works great, thank you again <span class="mention">@xtao</span> !</p>

username: DTSED
cooked: <p>It works for me too, sorry for the delay responding.<br>
Thanks</p>