Repository: pytorch
Issue: Android API to run model on Vulkan backend
Labels: oncall: mobile

## According to the tutorial on the official website blog, I successfully compiled the pytorch library with vulkan and generated a model with vulkan, but the following error occurred when calling it on the Android side：
```
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.pytorch_c, PID: 18814
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pytorch_c/com.example.pytorch_c.MainActivity}: java.lang.RuntimeException: The following operation failed in the TorchScript interpreter.
    Traceback of TorchScript, serialized code (most recent call last):
      File "code/__torch__/torchvision/models/mobilenet.py", line 10, in forward
      def forward(self: __torch__.torchvision.models.mobilenet.MobileNetV2,
        x: Tensor) -> Tensor:
        return (self)._forward_impl(x, )
                ~~~~~~~~~~~~~~~~~~~ <--- HERE
      def _forward_impl(self: __torch__.torchvision.models.mobilenet.MobileNetV2,
        x: Tensor) -> Tensor:
      File "code/__torch__/torchvision/models/mobilenet.py", line 14, in _forward_impl
        x: Tensor) -> Tensor:
        _0 = __torch__.torch.nn.functional.adaptive_avg_pool2d
        x0 = (self.features).forward(x, )
              ~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
        x1 = torch.reshape(_0(x0, [1, 1], ), [(torch.size(x0))[0], -1])
        return (self.classifier).forward(x1, )
      File "code/__torch__/torch/nn/modules/container/___torch_mangle_80.py", line 45, in forward
        _17 = getattr(self, "17")
        _18 = getattr(self, "18")
        input0 = (_0).forward(input, )
                  ~~~~~~~~~~~ <--- HERE
        input1 = (_1).forward(input0, )
        input2 = (_2).forward(input1, )
      File "code/__torch__/torchvision/models/mobilenet.py", line 29, in forward
        _2 = getattr(self, "1")
        _3 = getattr(self, "2")
        input0 = (_1).forward(input, )
                  ~~~~~~~~~~~ <--- HERE
        input1 = (_2).forward(input0, )
        return (_3).forward(input1, )
      File "code/__torch__/torch/nn/modules/conv.py", line 20, in forward
      def forward(self: __torch__.torch.nn.modules.conv.Conv2d,
        input: Tensor) -> Tensor:
        _0 = (self)._conv_forward(input, self.weight, )
              ~~~~~~~~~~~~~~~~~~~ <--- HERE
        return _0
      def _conv_forward(self: __torch__.torch.nn.modules.conv.Conv2d,
      File "code/__torch__/torch/nn/modules/conv.py", line 25, in _conv_forward
        input: Tensor,
        weight: Tensor) -> Tensor:
        _1 = torch.conv2d(input, weight, self.bias, [2, 2], [1, 1], [1, 1], 1)
             ~~~~~~~~~~~~ <--- HERE
        return _1
    
    Traceback of TorchScript, original code (most recent call last):
      File "D:\Anaconda3\envs\torch17\lib\site-packages\torchvision\models\mobilenet.py", line 171, in forward
        def forward(self, x):
            return self._forward_impl(x)
                   ~~~~~~~~~~~~~~~~~~ <--- HERE
      File "D:\Anaconda3\envs\torch17\lib\site-packages\torch\nn\modules\container.py", line 117, in forward
        def forward(self, input):
            for module in self:
                input = module(input)
                        ~~~~~~ <--- HERE
            return input
      File "D:\Anaconda3\envs\torch17\lib\site-packages\torch\nn\modules\container.py", line 117, in forward
        def forward(self, input):
            for module in self:
                input = module(input)
                        ~~~~~~ <--- HERE
            return input
      File "D:\Anaconda3\envs\torch17\lib\site-packages\torch\nn\modules\conv.py", line 423, in forward
        def forward(self, input: Tensor) -> Tensor:
            return self._conv_forward(input, self.weight)
                   ~~~~~~~~~~~~~~~~~~ <--- HERE
      File "D:\Anaconda3\envs\torch17\lib\site-packages\torch\nn\modules\conv.py", line 419, in _conv_forward
                                weight, self.bias, self.stride,
                                _pair(0), self.dilation, self.groups)
            return F.conv2d(input, weight, self.bias, self.stride,
                   ~~~~~~~~ <--- HERE
                            self.padding, self.dilation, self.groups)
    RuntimeError: expected scalar type Float but found UNKNOWN_SCALAR
    
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3898)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4081)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2462)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:219)
        at android.app.ActivityThread.main(ActivityThread.java:8393)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
     Caused by: java.lang.RuntimeException: The following operation failed in the TorchScript interpreter.
    Traceback of TorchScript, serialized code (most recent call last):
      File "code/__torch__/torchvision/models/mobilenet.py", line 10, in forward
      def forward(self: __torch__.torchvision.models.mobilenet.MobileNetV2,
        x: Tensor) -> Tensor:
        return (self)._forward_impl(x, )
                ~~~~~~~~~~~~~~~~~~~ <--- HERE
      def _forward_impl(self: __torch__.torchvision.models.mobilenet.MobileNetV2,
        x: Tensor) -> Tensor:
      File "code/__torch__/torchvision/models/mobilenet.py", line 14, in _forward_impl
        x: Tensor) -> Tensor:
        _0 = __torch__.torch.nn.functional.adaptive_avg_pool2d
        x0 = (self.features).forward(x, )
              ~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
        x1 = torch.reshape(_0(x0, [1, 1], ), [(torch.size(x0))[0], -1])
        return (self.classifier).forward(x1, )
      File "code/__torch__/torch/nn/modules/container/___torch_mangle_80.py", line 45, in forward
        _17 = getattr(self, "17")
        _18 = getattr(self, "18")
        input0 = (_0).forward(input, )
                  ~~~~~~~~~~~ <--- HERE
        input1 = (_1).forward(input0, )
        input2 = (_2).forward(input1, )
      File "code/__torch__/torchvision/models/mobilenet.py", line 29, in forward
        _2 = getattr(self, "1")
        _3 = getattr(self, "2")
        input0 = (_1).forward(input, )
                  ~~~~~~~~~~~ <--- HERE
        input1 = (_2).forward(input0, )
        return (_3).forward(input1, )
      File "code/__torch__/torch/nn/modules/conv.py", line 20, in forward
      def forward(self: __torch__.torch.nn.modules.conv.Conv2d,
        input: Tensor) -> Tensor:
        _0 = (self)._conv_forward(input, self.weight, )
              ~~~~~~~~~~~~~~~~~~~ <--- HERE
        return _0
      def _conv_forward(self: __torch__.torch.nn.modules.conv.Conv2d,
      File "code/__torch__/torch/nn/modules/conv.py", line 25, in _conv_forward
        input: Tensor,
        weight: Tensor) -> Tensor:
        _1 = torch.conv2d(input, weight, self.bias, [2, 2], [1, 1], [1, 1], 1)
             ~~~~~~~~~~~~ <--- HERE
        return _1
    
    Traceback of TorchScript, original code (most recent call last):
      File "D:\Anaconda3\envs\torch17\lib\site-packages\torchvision\models\mobilenet.py", line 171, in forward
        def forward(self, x):
            return self._forward_impl(x)
                   ~~~~~~~~~~~~~~~~~~ <--- HERE
      File "D:\Anaconda3\envs\torch17\lib\site-packages\torch\nn\modules\container.py", line 117, in forward
        def forward(self, input):
            for module in self:
                input = module(input)
                        ~~~~~~ <--- HERE
            return input
      File "D:\Anaconda3\envs\torch17\lib\site-packages\torch\nn\modules\container.py", line 117, in forward
        def forward(self, input):
            for module in self:
                input = module(input)
                        ~~~~~~ <--- HERE
            return input
      File "D:\Anaconda3\envs\torch17\lib\site-packages\torch\nn\modules\conv.py", line 423, in forward
        def forward(self, input: Tensor) -> Tensor:
            return self._conv_forward(input, self.weight)
                   ~~~~~~~~~~~~~~~~~~ <--- HERE
      File "D:\Anaconda3\envs\torch17\lib\site-packages\torch\nn\modules\conv.py", line 419, in _conv_forward
                                weight, self.bias, self.stride,
                                _pair(0), self.dilation, self.groups)
            return F.conv2d(input, weight, self.bias, self.stride,
E/AndroidRuntime:                ~~~~~~~~ <--- HERE
                            self.padding, self.dilation, self.groups)
    RuntimeError: expected scalar type Float but found UNKNOWN_SCALAR
    
        at org.pytorch.NativePeer.forward(Native Method)
        at org.pytorch.Module.forward(Module.java:49)
        at com.example.pytorch_c.MainActivity.onCreate(MainActivity.java:56)
        at android.app.Activity.performCreate(Activity.java:8121)
        at android.app.Activity.performCreate(Activity.java:8109)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3871)
        	... 11 more
I/Process: Sending signal. PID: 18814 SIG: 9

```
The calling method on the Android side is as follows:

```
mModule = Module.load(assetFilePath(this, "mobilenet2-vulkan.pt"),Device.VULKAN);
Bitmap bitmap=ReadAssetsImage("image.jpg");
bitmap = Bitmap.createScaledBitmap(bitmap,224,224,false);
Tensor tesor=TensorImageUtils.bitmapToFloat32Tensor(bitmap,
        TORCHVISION_NORM_MEAN_RGB, TORCHVISION_NORM_STD_RGB);
Tensor outputTensor = mModule.forward(IValue.from(tesor)).toTensor();
```
or
```
mModule = Module.load(assetFilePath(this, "mobilenet2-vulkan.pt"),Device.VULKAN);
FloatBuffer buffer = Tensor.allocateFloatBuffer(1 * 3 * 224 * 224);
Tensor mInputTensor = Tensor.fromBlob(buffer, new long[]{1, 3, 224, 224});
Tensor outputTensor = mModule.forward(IValue.from(mInputTensor)).toTensor();
```
Both methods will cause the above-mentioned error.I don’t know where there is a problem in the process, if you know the solution, please tell me, thank you！