Dear Altruists,
I want to run my pytorch model in android with gpu.
I have cloned the example app from this github repository suggested by this blog from PyTorch.
When I try to build the app I get the following error.
A problem occurred configuring project ':pytorch_android'.
> Could not create task ':pytorch_android:processDebugManifest'.
> 'org.gradle.api.file.RegularFileProperty org.gradle.api.file.ProjectLayout.fileProperty(org.gradle.api.provider.Provider)'
I have made following changes.
- Put VULKAN traced the model file
mnet.pt
in./android/test_app/app/src/main/assets/
folder - Updated the gradle version from
3.3.2
to3.5.1
in line 28 of./android/build.gradle
file - Updated the
./android/settings.gradle
file keeping only these liines required to run the test_app project
include ':test_app'
project(':test_app').projectDir = file('test_app/app')
- Set the
USE_VULKAN_DEVICE
flag to true adding the following line after line 66 of./android/test_app/app/build.gradle
file
buildConfigField("boolean", "USE_VULKAN_DEVICE", 'true')
With step 1, 2 and 3 the example app builds and runs successfully on my mobile device Samsung A71 but I get BuildConfig.USE_VULKAN_DEVICE = false
in ./android/test_app/app/src/main/java/org/pytorch/testapp/MainActivity.java file.
When I set the flag USE_VULKAN_DEVICE true with step 4, I get the following error.
2021-05-23 17:01:12.001 16511-16896/? E/AndroidRuntime: FATAL EXCEPTION: pytorch-mnet_bg
Process: org.pytorch.testapp.mnet, PID: 16511
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/mobilenetv2.py", line 11, in forward
def forward(self: __torch__.torchvision.models.mobilenetv2.MobileNetV2,
x: Tensor) -> Tensor:
return (self)._forward_impl(x, )
~~~~~~~~~~~~~~~~~~~ <--- HERE
def _forward_impl(self: __torch__.torchvision.models.mobilenetv2.MobileNetV2,
x: Tensor) -> Tensor:
File "code/__torch__/torchvision/models/mobilenetv2.py", line 15, 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 46, 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/mobilenetv2.py", line 32, 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 21, in forward
def forward(self: __torch__.torch.nn.modules.conv.Conv2d,
input: Tensor) -> Tensor:
_0 = (self)._conv_forward(input, self.weight, self.bias, )
~~~~~~~~~~~~~~~~~~~ <--- HERE
return _0
def _conv_forward(self: __torch__.torch.nn.modules.conv.Conv2d,
File "code/__torch__/torch/nn/modules/conv.py", line 27, in _conv_forward
weight: Tensor,
bias: Optional[Tensor]) -> Tensor:
_1 = torch.conv2d(input, weight, bias, [2, 2], [1, 1], [1, 1], 1)
~~~~~~~~~~~~ <--- HERE
return _1
Traceback of TorchScript, original code (most recent call last):
File "/home/tigerit/miniconda3/envs/torch-vulkan/lib/python3.8/site-packages/torchvision/models/mobilenetv2.py", line 198, in forward
def forward(self, x: Tensor) -> Tensor:
return self._forward_impl(x)
~~~~~~~~~~~~~~~~~~ <--- HERE
File "/home/tigerit/miniconda3/envs/torch-vulkan/lib/python3.8/site-packages/torch/nn/modules/container.py", line 119, in forward
def forward(self, input):
for module in self:
input = module(input)
~~~~~~ <--- HERE
return input
File "/home/tigerit/miniconda3/envs/torch-vulkan/lib/python3.8/site-packages/torch/nn/modules/container.py", line 119, in forward
def forward(self, input):
for module in self:
input = module(input)
~~~~~~ <--- HERE
return input
File "/home/tigerit/miniconda3/envs/torch-vulkan/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 399, in forward
def forward(self, input: Tensor) -> Tensor:
return self._conv_forward(input, self.weight, self.bias)
~~~~~~~~~~~~~~~~~~ <--- HERE
File "/home/tigerit/miniconda3/envs/torch-vulkan/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 395, in _conv_forward
weight, bias, self.stride,
_pair(0), self.dilation, self.groups)
return F.conv2d(input, weight, bias, self.stride,
~~~~~~~~ <--- 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 org.pytorch.testapp.MainActivity.doModuleForward(MainActivity.java:142)
2021-05-23 17:01:12.002 16511-16896/? E/AndroidRuntime: at org.pytorch.testapp.MainActivity$1.run(MainActivity.java:44)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.os.HandlerThread.run(HandlerThread.java:67)
2021-05-23 17:01:12.036 16511-16896/? I/Process: Sending signal. PID: 16511 SIG: 9