Torch::jit::load fails with Unknown Type Name Error

hangrymangry: <p>I am trying to implement Fast Neural Style Transfer on iOS application. To do that, I started with an implemented version of Fast Neural Style Transfer from <a href="https://github.com/rrmina/fast-neural-style-pytorch" rel="noopener nofollow ugc">here</a> and attempt to follow <a href="https://pytorch.org/mobile/ios/" rel="noopener nofollow ugc">Pytorch Mobile tutorial</a> by <a class="mention" href="/u/xta0">@xta0</a>  to run the model on my device.</p>
<p>After generating the model.pt file, I tried to load it following the tutorial code using TorchModule. However, I got this error when loading the module.</p>
<blockquote>
<p>Unknown type name ‘<strong>torch</strong>.torch.classes.xnnpack.TransposeConv2dOpContext’: Serialized File “code/<strong>torch</strong>/transformer/___torch_mangle_391.py”, line 18<br>
<strong>annotations</strong>[“prepack_folding._jit_pass_packed_weight_11”] = <strong>torch</strong>.torch.classes.xnnpack.Conv2dOpContext <strong>annotations</strong>[“prepack_folding._jit_pass_packed_weight_12”] = <strong>torch</strong>.torch.classes.xnnpack.Conv2dOpContext <strong>annotations</strong>[“prepack_folding._jit_pass_packed_weight_13”] = <strong>torch</strong>.torch.classes.xnnpack.TransposeConv2dOpContext ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ &lt;— HERE<br>
<strong>annotations</strong>[“prepack_folding._jit_pass_packed_weight_14”] = <strong>torch</strong>.torch.classes.xnnpack.TransposeConv2dOpContext <strong>annotations</strong>[“prepack_folding._jit_pass_packed_weight_15”] = <strong>torch</strong>.torch.classes.xnnpack.Conv2dOpContext (lldb)</p>
</blockquote>
<p>Within Xcode, the code fails here</p>
<pre><code class="lang-auto">    private lazy var module: TorchModule = {
    // Verified that filePath exist.
    if let filePath = Bundle.main.path(forResource: "mobile_model2", ofType: "pt"),
        // The problem is at TorchModule init setup.
        let module = TorchModule(fileAtPath: filePath) {
        return module
    } else {
        fatalError("Can't find the model file!")
    }
}()
</code></pre>
<p>Code fails specifically at torch::jit::load</p>
<pre><code class="lang-auto">- (nullable instancetype)initWithFileAtPath:(NSString*)filePath {
  self = [super init];
  if (self) {
    try {
      // CODE FAILS HERE, when trying to load!
      _impl = torch::jit::load(filePath.UTF8String);
      _impl.eval();
    } catch (const std::exception&amp; exception) {
      NSLog(@"%s", exception.what());
      return nil;
    }
  }
  return self;
}
</code></pre>
<p>This means that the C++ load method call wasn’t able to load my model.pt. What could possibly caused this?</p>
<p>In order to verify that this issue is not solely caused by my model, I tried loading this with another pretrained model resnet18. And as expected, I also receive errors while attempting to load this model using TorchModule, although with a different set of errors.</p>
<pre><code class="lang-auto">**Unknown builtin op: aten::_add_relu_.**

**Could not find any similar ops to aten::_add_relu_. This op may not exist or may not be currently supported in TorchScript.**
</code></pre>

Shuai_Xing: <p>I have the same issue, Pytorch-android 1.8.1 build from source. model  exported by pytorch 1.8.1. have you solved this problem?</p>

kimishpatel: <p>I think the issue is with the builds. Probably ops are not linked in. <a class="mention" href="/u/xta0">@xta0</a> any idea what might be the issue. <a class="mention" href="/u/ivankobzarev">@IvanKobzarev</a> on the android side.</p>

xta0: <p><a class="mention" href="/u/hangrymangry">@hangrymangry</a> what’s your pytorch version? Are you using cocoapods or building from source? <a class="mention" href="/u/kimishpatel">@kimishpatel</a> <code>TransposeConv2dOpContext </code> is from the xnnpack rewrite, right?</p>

kimishpatel: <p>Yes it is from xnnpack but likely the issue with op registration since, as mentioned, even <code>aten::_add_relu_</code> cannot be found.</p>

MeijisIrlnd: <p>Sorry to resurrect this, but is there any update on it? I built LibTorch as a static lib for arm64 Mac, and get the same thing when I build from Xcode. Weirdly, it doesn’t seem to be an issue when using the recommended CMake stuff, but that isn’t really viable for me, and for me it’s erroring on this block :</p>
<pre><code class="lang-auto">m_models = {
    {MODEL_TYPE::BASS, torch::jit::load(modelPath + "/Bass.pt")},
    {MODEL_TYPE::DRUMS, torch::jit::load(modelPath + "/Drum.pt")},
    {MODEL_TYPE::VOCALS, torch::jit::load(modelPath + "/Vocal.pt")},
     {MODEL_TYPE::OTHERS, torch::jit::load(modelPath + "/Other.pt")}
};
</code></pre>
<p>Terminal output is:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/5/a/5a20d648d5c2687772b8040c6bdb2765d949e0a2.png" alt="Screenshot 2022-08-12 at 15.57.12" data-base62-sha1="cRjkaOiHdpvl5Fy9LfxN2q5ZtNE" width="406" height="162"></p>

kimishpatel: <p>Sorry but if did not mention repro steps, can you please do so. I can try to look at it later.</p>

MeijisIrlnd: <p>My bad, I actually managed to fix it by linking with<br>
<code>-Wl,-force_load,/path/to/libtorch.a</code><br>
<code>-Wl,-force_load,/path/to/libtorch_cpu.a</code><br>
after I just started trying to brute force it after googling for 3 hours haha</p>