Is this model convertable to mobile?

username: Matt_Davis
cooked: <p>Getting an error saying the metalprepack to Conv2dprepack is not supported. Seems like it is in the code, so is it maybe the usage of it in the model below? I added the following check for unsupported things:</p>
<p>optimization_blocklist = [<br>
MobileOptimizerType.CONV_BN_FUSION,<br>
MobileOptimizerType.INSERT_FOLD_PREPACK_OPS,<br>
MobileOptimizerType.REMOVE_DROPOUT,<br>
MobileOptimizerType.FUSE_ADD_RELU,<br>
MobileOptimizerType.HOIST_CONV_PACKED_PARAMS,<br>
]</p>
<p>OcclusionAwareGenerator(<br>
(dense_motion_network): DenseMotionNetwork(<br>
(hourglass): Hourglass(<br>
(encoder): Encoder(<br>
(down_blocks): ModuleList(<br>
(0): DownBlock2d(<br>
(conv): Conv2d(44, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm): SynchronizedBatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(pool): AvgPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0)<br>
)<br>
(1): DownBlock2d(<br>
(conv): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm): SynchronizedBatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(pool): AvgPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0)<br>
)<br>
(2): DownBlock2d(<br>
(conv): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm): SynchronizedBatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(pool): AvgPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0)<br>
)<br>
(3): DownBlock2d(<br>
(conv): Conv2d(512, 1024, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm): SynchronizedBatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(pool): AvgPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0)<br>
)<br>
(4): DownBlock2d(<br>
(conv): Conv2d(1024, 1024, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm): SynchronizedBatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(pool): AvgPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0)<br>
)<br>
)<br>
)<br>
(decoder): Decoder(<br>
(up_blocks): ModuleList(<br>
(0): UpBlock2d(<br>
(conv): Conv2d(1024, 1024, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm): SynchronizedBatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(1): UpBlock2d(<br>
(conv): Conv2d(2048, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm): SynchronizedBatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(2): UpBlock2d(<br>
(conv): Conv2d(1024, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm): SynchronizedBatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(3): UpBlock2d(<br>
(conv): Conv2d(512, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm): SynchronizedBatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(4): UpBlock2d(<br>
(conv): Conv2d(256, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm): SynchronizedBatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
)<br>
)<br>
(mask): Conv2d(108, 11, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))<br>
(occlusion): Conv2d(108, 1, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))<br>
(down): AntiAliasInterpolation2d()<br>
)<br>
(first): SameBlock2d(<br>
(conv): Conv2d(3, 64, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))<br>
(norm): SynchronizedBatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(down_blocks): ModuleList(<br>
(0): DownBlock2d(<br>
(conv): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm): SynchronizedBatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(pool): AvgPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0)<br>
)<br>
(1): DownBlock2d(<br>
(conv): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm): SynchronizedBatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(pool): AvgPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0)<br>
)<br>
)<br>
(up_blocks): ModuleList(<br>
(0): UpBlock2d(<br>
(conv): Conv2d(256, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm): SynchronizedBatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(1): UpBlock2d(<br>
(conv): Conv2d(128, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm): SynchronizedBatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(bottleneck): Sequential(<br>
(r0): ResBlock2d(<br>
(conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm1): SynchronizedBatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(norm2): SynchronizedBatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(r1): ResBlock2d(<br>
(conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm1): SynchronizedBatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(norm2): SynchronizedBatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(r2): ResBlock2d(<br>
(conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm1): SynchronizedBatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(norm2): SynchronizedBatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(r3): ResBlock2d(<br>
(conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm1): SynchronizedBatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(norm2): SynchronizedBatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(r4): ResBlock2d(<br>
(conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm1): SynchronizedBatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(norm2): SynchronizedBatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(r5): ResBlock2d(<br>
(conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(norm1): SynchronizedBatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(norm2): SynchronizedBatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(final): Conv2d(64, 3, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))<br>
)</p>
<p>converting model to optimize for mobile gets the error:<br>
RuntimeError                              Traceback (most recent call last)<br>
 in ()<br>
17 traced_detector = torch.jit.trace(generator, (image_tensor, kp_driving, kp_driving), strict=False)<br>
18 <span class="hashtag">#optimized_traced_detector</span> = optimize_for_mobile(traced_detector, backend=‘metal’)<br>
—&gt; 19 optimized_traced_detector = optimize_for_mobile(traced_detector, optimization_blocklist, backend=‘metal’)<br>
20 <span class="hashtag">#print</span>(torch.jit.export_opnames(optimized_traced_detector))<br>
21 optimized_traced_detector._save_for_lite_interpreter("/content/generator_metal.pt")</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/utils/mobile_optimizer.py in optimize_for_mobile(script_module, optimization_blocklist, preserved_methods, backend)<br>
67         optimized_cpp_module = torch._C._jit_pass_vulkan_optimize_for_mobile(script_module._c, preserved_methods_str)<br>
68     elif backend == ‘metal’:<br>
—&gt; 69         optimized_cpp_module = torch._C._jit_pass_metal_optimize_for_mobile(script_module._c, preserved_methods_str)<br>
70     else:<br>
71         raise TypeError(“Unknown backend, must be one of ‘CPU’, ‘Vulkan’ or ‘Metal’”)</p>
<p>RuntimeError: 0 INTERNAL ASSERT FAILED at “/pytorch/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, Tensor, int[], int[], int[], int, None, None,</p>
<p>CPU Model Works perfectly, Model Performs great on GPU. Just not able to get it onto phone quite yet.</p>

username: ptrblck
cooked: <p>It seems you are hitting <a href="https://github.com/pytorch/pytorch/issues/69609">this issue</a>. Could you comment on this bug with your use case (and if possible a code snippet to reproduce it)?</p>

username: kimishpatel
cooked: <aside class="quote no-group" data-username="Matt_Davis" data-post="1" data-topic="139460">
<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/matt_davis/48/43394_2.png" class="avatar"> Matt_Davis:</div>
<blockquote>
<p>have an op for metal_prepack</p>
</blockquote>
</aside>
<p><a class="mention" href="/u/xta0">@xta0</a> for metal related stuff</p>