Unknown type name '__torch__.torch.classes.metal.Conv2dOpContext' 

kenmbkr: <h2>
<a name="bug-1" class="anchor" href="#bug-1"></a><img src="https://discuss.pytorch.org/images/emoji/apple/bug.png?v=9" title=":bug:" class="emoji" alt=":bug:"> Bug</h2>

<p>Using PyTorch mobile with metal backend, Conv2dOpContext is unknown type name when loading the model in Xcode.</p>
<h2>
<a name="to-reproduce-2" class="anchor" href="#to-reproduce-2"></a>To Reproduce</h2>
<p>Steps to reproduce the behavior:</p>
<ol>
<li>I followed the <a href="https://pytorch.org/tutorials/prototype/ios_gpu_workflow.html" rel="noopener nofollow ugc">tutorial</a> to compile PyTorch from scratch.</li>
<li>I download the HelloWorld <a href="https://github.com/pytorch/ios-demo-app/tree/master/HelloWorld" rel="noopener nofollow ugc">example</a> and change the <code>model.pt</code> to my own model.</li>
<li>My model with CPU backend runs normally but encounters the below error message with Metal backend.</li>
</ol>

<pre><code class="lang-auto">Unknown type name '__torch__.torch.classes.metal.Conv2dOpContext':
Serialized   File "code/__torch__/modules/model_name/___torch_mangle_270.py", line 5
  __buffers__ = []
  __annotations__ = []
  __annotations__["prepack_folding_forward._jit_pass_packed_weight_0"] = __torch__.torch.classes.metal.Conv2dOpContext
                                                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
  __annotations__["prepack_folding_forward._jit_pass_packed_weight_1"] = __torch__.torch.classes.metal.Conv2dOpContext
  __annotations__["prepack_folding_forward._jit_pass_packed_weight_2"] = __torch__.torch.classes.metal.Conv2dOpContext
</code></pre>
<h2>
<a name="expected-behavior-3" class="anchor" href="#expected-behavior-3"></a>Expected behavior</h2>

<h2>
<a name="environment-4" class="anchor" href="#environment-4"></a>Environment</h2>
<p>Please copy and paste the output from our<br>
<a href="https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py" rel="noopener nofollow ugc">environment collection script</a><br>
(or fill out the checklist below manually).</p>
<p>You can get the script and run it with:</p>
<pre><code class="lang-auto">wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py
</code></pre>
<p>PyTorch version: 1.9.0a0+gitd69c22d<br>
Is debug build: False<br>
CUDA used to build PyTorch: Could not collect<br>
ROCM used to build PyTorch: N/A<br>
OS: KDE neon User Edition 5.22 (x86_64)<br>
GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0<br>
Clang version: Could not collect<br>
CMake version: version 3.20.5<br>
Libc version: glibc-2.31<br>
Python version: 3.9.5 | packaged by conda-forge | (default, Jun 19 2021, 00:32:32)  [GCC 9.3.0] (64-bit runtime)<br>
Python platform: Linux-5.11.0-22-generic-x86_64-with-glibc2.31<br>
Is CUDA available: False<br>
CUDA runtime version: Could not collect<br>
GPU models and configuration:<br>
GPU 0: NVIDIA GeForce RTX 2080 Ti<br>
GPU 1: NVIDIA GeForce RTX 2080 Ti<br>
Nvidia driver version: 470.42.01<br>
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.5<br>
HIP runtime version: N/A<br>
MIOpen runtime version: N/A<br>
Versions of relevant libraries:<br>
[pip3] numpy==1.21.0<br>
[pip3] torch==1.10.0a0+git512448a<br>
[conda] magma-cuda113             2.5.2                         1    pytorch<br>
[conda] mkl                       2021.2.0           h06a4308_296<br>
[conda] mkl-include               2021.3.0           h726a3e6_557    conda-forge<br>
[conda] numpy                     1.21.0           py39hdbf815f_0    conda-forge<br>
[conda] torch                     1.10.0a0+git512448a          pypi_0    pypi</p>
<h2>
<a name="additional-context-5" class="anchor" href="#additional-context-5"></a>Additional context</h2>

<p>Operators listed for <code>torch.jit.export_opnames(optimized_model)</code></p>
<pre><code class="lang-auto">['aten::__and__.bool', 'aten::__getitem__.t', 'aten::_set_item.str', 'aten::add.int', 'aten::append.t', 'aten::arange', 'aten::avg_pool2d', 'aten::cat', 'aten::constant_pad_nd', 'aten::dim', 'aten::div.Scalar', 'aten::eq.int', 'aten::floor.float', 'aten::format', 'aten::gt.int', 'aten::le.int', 'aten::len.t', 'aten::lt.int', 'aten::mul.Scalar', 'aten::mul.Tensor', 'aten::ne.int', 'aten::ne.int_float', 'aten::pop.t', 'aten::repeat', 'aten::reshape', 'aten::size', 'aten::slice.t', 'aten::softmax.int', 'aten::sub.Scalar', 'aten::sub.int', 'aten::sum.dim_IntList', 'aten::to.prim_Device', 'aten::unsqueeze', 'aten::upsample_nearest1d.vec', 'aten::upsample_nearest2d.vec', 'aten::upsample_nearest3d.vec', 'aten::view', 'metal_prepack::conv2d_run', 'prim::RaiseException', 'prim::Uninitialized']
</code></pre>
<p>Got this message at the end of the compilation.</p>
<pre><code class="lang-auto">warning: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: archive library: /path/build_ios/install/lib/libtorch.a the table of contents is empty (no object file members in the library define global symbols)
</code></pre>
<p>Running <code>grep</code> shows that <code>Conv2dOpContext</code> is not present in <code>libtorch.a</code></p>
<pre><code class="lang-auto">(pytorch_metal) ➜  pytorch_metal git:(512448a425) ✗ grep "Conv2dOpContext" -r build_ios/install
build_ios/install/include/ATen/native/metal/ops/MetalConvolution.h:Tensor conv2d(const Tensor&amp; input, Conv2dOpContext&amp; context);
build_ios/install/include/ATen/native/metal/MetalPrepackOpContext.h:class Conv2dOpContext : public torch::jit::CustomClassHolder {
build_ios/install/include/ATen/native/metal/MetalPrepackOpContext.h:  Conv2dOpContext() = delete;
build_ios/install/include/ATen/native/metal/MetalPrepackOpContext.h:  Conv2dOpContext(
Binary file build_ios/install/lib/libtorch_cpu.a matches
</code></pre>

kimishpatel: <aside class="quote no-group" data-username="kenmbkr" data-post="1" data-topic="126153">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kenmbkr/48/30980_2.png" class="avatar"> kenmbkr:</div>
<blockquote>
<p>message with Metal backend</p>
</blockquote>
</aside>
<p>Are you running the model on your phone, your mac or linux machine? cc <a class="mention" href="/u/xta0">@xta0</a></p>

xta0: <p>I’ve seen other people complain about this issue. Assigning this to myself.</p>

kenmbkr: <p>I run it on a phone.</p>

edeguine: <p>I am seeing the same error running on ipad with Xcode 14.1 and libtorch 1.13.0</p>