NameError: name '_C' is not defined: (Prototype) Use iOS GPU in PyTorch (Metal)

username: HashedViking
cooked: <p>Hello, I wanted to ask a simple question just before I try PyTorch Metal integration.<br>
In <a href="https://pytorch.org/tutorials/prototype/ios_gpu_workflow.html#conclusion" rel="noopener nofollow ugc">tutorial</a> there is a sentence:<br>
"Next we need to make some changes in <code>TorchModule.mm</code>"<br>
Here should be a snippet of code, I guess, that shows how <code>metal()</code> should be used, because next sentence is:<br>
“As you can see, we simply just call <code>.metal()</code> to move our input tensor from CPU to GPU”<br>
So my question is - I should call <code>.metal()</code> on tensor argument just before calling <code>forward</code> method as I call <code>.cuda()</code> for example in Python?</p>

username: cccclai
cooked: <p><a class="mention" href="/u/xta0">@xta0</a> Is it something you know?</p>

username: xta0
cooked: <p>yes, call <code>.metal()</code> on your input tensor and call <code>.cpu()</code> on the output tensor to move it back. Something like</p>
<pre><code class="lang-auto">auto input = torch.rand({1,3,2,2}).metal()
auto output = model.forward({input}).toTensor().cpu();
</code></pre>

username: HashedViking
cooked: <p>Thank you, <a class="mention" href="/u/xta0">@xta0</a>, but I struggle to import Metal PyTorch <strong>torch==1.9.0a0+gite442d5c</strong><br>
(<a href="https://discuss.pytorch.org/t/error-building-pytorch-from-source/115377">after fixing the compilation error</a>)<br>
Error near <code>import torch</code> when I try to run my model:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "/demo.py", line 14, in &lt;module&gt;
    import torch
  File "/opt/anaconda3/envs/FOM_METAL/lib/python3.8/site-packages/torch/__init__.py", line 228, in &lt;module&gt;
    __all__ += [name for name in dir(_C)
NameError: name '_C' is not defined
</code></pre>
<p>Error in python console:</p>
<pre><code class="lang-auto">python
&gt;&gt;&gt; import torch
</code></pre>
<pre><code class="lang-auto">  File "/opt/anaconda3/envs/FOM_METAL/lib/python3.8/site-packages/torch/_tensor.py", line 322, in Tensor
    detach = _C._add_docstr(_C._TensorBase.detach, r"""
RuntimeError: method 'detach' already has a docstring
</code></pre>
<p><strong>torch 1.8.0</strong> works fine</p>
<p>I have no clue how to fix this.</p>

username: HashedViking
cooked: <p>Well I’ve managed to compile it and even import, but now I’m getting some vague error when I try to optimize for Metal:</p>
<pre><code class="lang-auto">traced_fom = torch.jit.trace(fom, (source, driving, initial), strict=False)
optimized_fom = mob_op.optimize_for_mobile(traced_fom, backend='metal') # &lt;&lt;&lt; error here
optimized_fom.save('fom-metal.pt')
</code></pre>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "/demo.py", line 326, in &lt;module&gt;
    predictions = make_animation(source_image, driving_video)
  File "/demo.py", line 91, in make_animation
    optimized_fom = mob_op.optimize_for_mobile(traced_fom, backend='metal')
  File "/opt/anaconda3/envs/fom/lib/python3.8/site-packages/torch/utils/mobile_optimizer.py", line 69, in optimize_for_mobile
    optimized_cpp_module = torch._C._jit_pass_metal_optimize_for_mobile(script_module._c, preserved_methods_str)
RuntimeError: 0 INTERNAL ASSERT FAILED at "../torch/csrc/jit/ir/alias_analysis.cpp":532, please report a bug to PyTorch. We don't have an op for metal_prepack::conv2d_prepack but it isn't a special case.  Argument types: Tensor, None, int[], int[], int[], int, None, None, 
</code></pre>
<p><strong>We don’t have an op for metal_prepack::conv2d_prepack but it isn’t a special case.  Argument types: Tensor, None, int[], int[], int[], int, None, None,</strong></p>
<p>My parameters are:</p>
<pre><code class="lang-auto">conda 4.9.2
Python 3.6.13 :: Anaconda, Inc.

python -m pip freeze

attr==0.3.1
attrs==20.3.0
certifi==2020.12.5
cffi==1.11.5
cloudpickle==0.5.3
coremltools==4.1
cycler==0.10.0
Cython==0.29.22
dask==2021.3.0
dataclasses==0.8
decorator==4.3.0
imageio==2.3.0
imageio-ffmpeg==0.4.3
kiwisolver==1.0.1
matplotlib==2.2.2
mpmath==1.2.1
networkx==2.1
numpy==1.18.1
packaging==20.9
pandas==0.23.4
Pillow==5.2.0
protobuf==3.15.6
pycparser==2.18
pygit==0.1
pyparsing==2.2.0
python-dateutil==2.7.3
pytz==2018.5
PyWavelets==0.5.2
PyYAML==5.1
scikit-image==0.15.0
scikit-learn==0.19.2
scikit-video==1.1.11
scipy==1.1.0
six==1.11.0
sympy==1.7.1
toolz==0.9.0
torch==1.9.0a0+gite442d5c
torchvision==0.9.0
tqdm==4.24.0
typing-extensions==3.7.4.3
</code></pre>
<p>Should I use some specific torch version to make it work?<br>
<a href="https://github.com/pytorch/pytorch/issues/54377" rel="noopener nofollow ugc">Related bug report</a></p>

username: hanton
cooked: <p>Hi <a class="mention" href="/u/hashedviking">@HashedViking</a>, did you update submodules, like running <code>git submodule sync &amp;&amp; git submodule update --init --recursive --jobs 0</code> before running <code>USE_PYTORCH_METAL=ON python setup.py install --cmake</code> to build PyTorch?</p>
<p>I had the same issue and solved it by running the above commands in the <code>main</code> branch(commit <code>2992d92b5acb1e5c712e5e9fded652f1312a81b1</code>).</p>
<p>Let me know if there is anything else I can help.</p>