Getting error to build android app with VULKAN Device

username: Amit_Roy1
cooked: <p>Dear Altruists,<br>
I want to run my pytorch model in android with gpu.</p>
<p>I have cloned the example app from this <a href="https://github.com/pytorch/pytorch/tree/master/android" rel="noopener nofollow ugc">github repository</a> suggested by this <a href="https://pytorch.org/tutorials/prototype/vulkan_workflow.html" rel="noopener nofollow ugc">blog</a> from PyTorch.</p>
<p>When I try to build the app I get the following error.</p>
<pre><code class="lang-auto">A problem occurred configuring project ':pytorch_android'.
&gt; Could not create task ':pytorch_android:processDebugManifest'.
   &gt; 'org.gradle.api.file.RegularFileProperty org.gradle.api.file.ProjectLayout.fileProperty(org.gradle.api.provider.Provider)'
</code></pre>
<p>I have made following changes.</p>
<ol>
<li>Put VULKAN traced the model file <code>mnet.pt</code> in <code>./android/test_app/app/src/main/assets/</code> folder</li>
<li>Updated the gradle version from <code>3.3.2</code> to <code>3.5.1</code> in line 28 of <code>./android/build.gradle</code> file</li>
<li>Updated the <code>./android/settings.gradle</code> file keeping only these liines required to run the test_app project</li>
</ol>
<pre><code class="lang-auto">include ':test_app'
project(':test_app').projectDir = file('test_app/app')
</code></pre>
<ol start="4">
<li>Set the <code>USE_VULKAN_DEVICE</code> flag to true adding the following line after line 66 of <code>./android/test_app/app/build.gradle</code> file<br>
<code>buildConfigField("boolean", "USE_VULKAN_DEVICE", 'true')</code>
</li>
</ol>
<p>With step 1, 2 and 3 the example app builds and runs successfully on my mobile device Samsung A71 but I get <code>BuildConfig.USE_VULKAN_DEVICE = false</code>  in ./android/test_app/app/src/main/java/org/pytorch/testapp/MainActivity.java file.</p>
<p>When I set the flag USE_VULKAN_DEVICE true with step 4, I get the following error.</p>
<pre><code class="lang-auto">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) -&gt; Tensor:
        return (self)._forward_impl(x, )
                ~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
      def _forward_impl(self: __torch__.torchvision.models.mobilenetv2.MobileNetV2,
        x: Tensor) -&gt; Tensor:
      File "code/__torch__/torchvision/models/mobilenetv2.py", line 15, in _forward_impl
        x: Tensor) -&gt; Tensor:
        _0 = __torch__.torch.nn.functional.adaptive_avg_pool2d
        x0 = (self.features).forward(x, )
              ~~~~~~~~~~~~~~~~~~~~~~ &lt;--- 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, )
                  ~~~~~~~~~~~ &lt;--- 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, )
                  ~~~~~~~~~~~ &lt;--- 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) -&gt; Tensor:
        _0 = (self)._conv_forward(input, self.weight, self.bias, )
              ~~~~~~~~~~~~~~~~~~~ &lt;--- 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]) -&gt; Tensor:
        _1 = torch.conv2d(input, weight, bias, [2, 2], [1, 1], [1, 1], 1)
             ~~~~~~~~~~~~ &lt;--- 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) -&gt; Tensor:
            return self._forward_impl(x)
                   ~~~~~~~~~~~~~~~~~~ &lt;--- 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)
                        ~~~~~~ &lt;--- 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)
                        ~~~~~~ &lt;--- 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) -&gt; Tensor:
            return self._conv_forward(input, self.weight, self.bias)
                   ~~~~~~~~~~~~~~~~~~ &lt;--- 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,
                   ~~~~~~~~ &lt;--- 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
</code></pre>

username: IvanKobzarev
cooked: <p><a class="mention" href="/u/amit_roy1">@Amit_Roy1</a><br>
What version of gradle do you use? (6+ version should be fine)</p>
<aside class="onebox githubblob">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/blob/master/.circleci/docker/build.sh#L174" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/master/.circleci/docker/build.sh#L174" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/master/.circleci/docker/build.sh#L174</a></h4>



  <pre class="onebox">    <code class="lang-sh">
      <ol class="start lines" start="164" style="counter-reset: li-counter 163 ;">
          <li>  DB=yes</li>
          <li>  VISION=yes</li>
          <li>  ;;</li>
          <li>pytorch-linux-xenial-py3-clang5-android-ndk-r19c)</li>
          <li>  ANACONDA_PYTHON_VERSION=3.6</li>
          <li>  CLANG_VERSION=5.0</li>
          <li>  LLVMDEV=yes</li>
          <li>  PROTOBUF=yes</li>
          <li>  ANDROID=yes</li>
          <li>  ANDROID_NDK_VERSION=r19c</li>
          <li class="selected">  GRADLE_VERSION=6.8.3</li>
          <li>  CMAKE_VERSION=3.7.0</li>
          <li>  NINJA_VERSION=1.9.0</li>
          <li>  ;;</li>
          <li>pytorch-linux-xenial-py3.6-clang7)</li>
          <li>  ANACONDA_PYTHON_VERSION=3.6</li>
          <li>  CLANG_VERSION=7</li>
          <li>  PROTOBUF=yes</li>
          <li>  DB=yes</li>
          <li>  VISION=yes</li>
          <li>  ;;</li>
      </ol>
    </code>
  </pre>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


username: IvanKobzarev
cooked: <aside class="quote no-group" data-username="Amit_Roy1" data-post="1" data-topic="122113">
<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/amit_roy1/48/38635_2.png" class="avatar"> Amit_Roy1:</div>
<blockquote>
<p>BuildConfig.USE_VULKAN_DEVICE = false</p>
</blockquote>
</aside>
<p>Sometimes gradle does not regenerate BuildConfig.java file. If you use Android Studio, could you try Build → Clean first or <code>gradle clean</code> to force regeneration.</p>