Unable to get pytorch 1.9 working on Android, couldn't find "libpytorch_jni.so"

username: yangcheng
cooked: <p>Hi, I am super excited to see 1.9 release so I give a try, however I can’t get the hello world working.<br>
if I simple change dependencies from</p>
<pre><code class="lang-auto">    implementation 'org.pytorch:pytorch_android:1.8.0-SNAPSHOT'
    implementation 'org.pytorch:pytorch_android_torchvision:1.8.0-SNAPSHOT'
</code></pre>
<p>to</p>
<pre><code class="lang-auto">    implementation 'org.pytorch:pytorch_android:1.9.0'
    implementation 'org.pytorch:pytorch_android_torchvision:1.9.0'
</code></pre>
<p>I would get lost of similar compile error of duplicate class. like those</p>
<pre><code class="lang-auto">  java.lang.RuntimeException: Duplicate class org.pytorch.BuildConfig found in modules classes.jar (org.pytorch:pytorch_android:1.9.0) and classes.jar (org.pytorch:pytorch_android_lite:1.9.0)
  Duplicate class org.pytorch.DType found in modules classes.jar (org.pytorch:pytorch_android:1.9.0) and classes.jar (org.pytorch:pytorch_android_lite:1.9.0)
  Duplicate class org.pytorch.Device found in modules classes.jar (org.pytorch:pytorch_android:1.9.0) and classes.jar (org.pytorch:pytorch_android_lite:1.9.0)
  Duplicate class org.pytorch.INativePeer found in modules classes.jar (org.pytorch:pytorch_android:1.9.0) and classes.jar (org.pytorch:pytorch_android_lite:1.9.0)
  Duplicate class org.pytorch.IValue found in modules classes.jar (org.pytorch:pytorch_android:1.9.0) and classes.jar (org.pytorch:pytorch_android_lite:1.9.0)
  Duplicate class org.pytorch.MemoryFormat found in modules classes.jar (org.pytorch:pytorch_android:1.9.0) and classes.jar (org.pytorch:pytorch_android_lite:1.9.0)
  Duplicate class org.pytorch.Module found in modules classes.jar (org.pytorch:pytorch_android:1.9.0) and classes.jar (org.pytorch:pytorch_android_lite:1.9.0)
  Duplicate class org.pytorch.NativePeer found in modules classes.jar (org.pytorch:pytorch_android:1.9.0) and classes.jar (org.pytorch:pytorch_android_lite:1.9.0)
  Duplicate class org.pytorch.PyTorchAndroid found in modules classes.jar (org.pytorch:pytorch_android:1.9.0) and classes.jar (org.pytorch:pytorch_android_lite:1.9.0)
  Duplicate class org.pytorch.PyTorchCodegenLoader found in modules classes.jar (org.pytorch:pytorch_android:1.9.0) and classes.jar (org.pytorch:pytorch_android_lite:1.9.0)
  Duplicate class org.pytorch.Tensor found in modules classes.jar (org.pytorch:pytorch_android:1.9.0) and classes.jar (org.pytorch:pytorch_android_lite:1.9.0)
  Duplicate class org.pytorch.Tensor$1 found in modules classes.jar (org.pytorch:pytorch_android:1.9.0) and classes.jar (org.pytorch:pytorch_android_lite:1.9.0)
  Duplicate class org.pytorch.Tensor$Tensor_float32 found in modules classes.jar (org.pytorch:pytorch_android:1.9.0) and classes.jar (org.pytorch:pytorch_android_lite:1.9.0)
  Duplicate class org.pytorch.Tensor$Tensor_float64 found in modules classes.jar (org.pytorch:pytorch_android:1.9.0) and classes.jar (org.pytorch:pytorch_android_lite:1.9.0)
  Duplicate class org.pytorch.Tensor$Tensor_int32 found in modules classes.jar (org.pytorch:pytorch_android:1.9.0) and classes.jar (org.pytorch:pytorch_android_lite:1.9.0)
  Duplicate class org.pytorch.Tensor$Tensor_int64 found in modules classes.jar (org.pytorch:pytorch_android:1.9.0) and classes.jar (org.pytorch:pytorch_android_lite:1.9.0)

</code></pre>
<p>If I use</p>
<pre><code class="lang-auto">    implementation 'org.pytorch:pytorch_android_lite:1.9.0'
    implementation 'org.pytorch:pytorch_android_torchvision:1.9.0'
</code></pre>
<p>The code will build but at run time, it crash with  <strong>couldn’t find "libpytorch_jni.so"</strong></p>
<p>I inspected the APK and there is no libpytorch_jni.so in lib folder. there are only<br>
<code>libc++_shared.so         libfbjni.so              libpytorch_jni_lite.so   libpytorch_vision_jni.so</code></p>
<p>It seems like the dependencies or packaging is not setup straight forward, I wonder what is a working configuration. Thanks</p>

username: kimishpatel
cooked: <p><a class="mention" href="/u/ivankobzarev">@IvanKobzarev</a> any ideas here?</p>

username: Marcio_Albuquerque
cooked: <p>Try use pytorch_android_lite,</p>
<p>Look the project (<a href="https://github.com/pytorch/android-demo-app/tree/master/ObjectDetection" class="inline-onebox" rel="noopener nofollow ugc">android-demo-app/ObjectDetection at master · pytorch/android-demo-app · GitHub</a>). The commit with update version.</p>
<aside class="onebox githubcommit">
  <header class="source">

      <a href="https://github.com/pytorch/android-demo-app/commit/3d31a030d92524cc1fd780562577cd6f638489f6" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/android-demo-app</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Commit">
    <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M10.86 7c-.45-1.72-2-3-3.86-3-1.86 0-3.41 1.28-3.86 3H0v2h3.14c.45 1.72 2 3 3.86 3 1.86 0 3.41-1.28 3.86-3H14V7h-3.14zM7 10.2c-1.22 0-2.2-.98-2.2-2.2 0-1.22.98-2.2 2.2-2.2 1.22 0 2.2.98 2.2 2.2 0 1.22-.98 2.2-2.2 2.2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/android-demo-app/commit/3d31a030d92524cc1fd780562577cd6f638489f6" target="_blank" rel="noopener nofollow ugc">Demo app updates to use PyTorch 1.9 org.pytorch:pytorch_android_lite:1.9.0 (#151)</a>
    </h4>

    <div class="github-info">
      <div class="date">
        committed <span class="discourse-local-date" data-format="ll" data-date="2021-06-17" data-time="18:47:55" data-timezone="UTC">06:47PM - 17 Jun 21 UTC</span>
      </div>

      <div class="user">
        <a href="https://github.com/jeffxtang" target="_blank" rel="noopener nofollow ugc">
          <img alt="jeffxtang" src="https://avatars.githubusercontent.com/u/535090?v=4" class="onebox-avatar-inline" width="20" height="20">
          jeffxtang
        </a>
      </div>

      <div class="lines" title="changed 18 files with 134 additions and 80 deletions">
        <a href="https://github.com/pytorch/android-demo-app/commit/3d31a030d92524cc1fd780562577cd6f638489f6" target="_blank" rel="noopener nofollow ugc">
          <span class="added">+134</span>
          <span class="removed">-80</span>
        </a>
      </div>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">* initial commit

* Revert "initial commit"

This reverts commit 5a65775315a<span class="show-more-container"><a href="https://github.com/pytorch/android-demo-app/commit/3d31a030d92524cc1fd780562577cd6f638489f6" target="_blank" rel="noopener nofollow ugc" class="show-more">…</a></span><span class="excerpt hidden">d581637837a408f80607fa5e3fed7.

* main readme and helloworld/demo app readme updates

* build.gradle, README and code update for PT1.9 for HelloWorld and Object Detection using pytorch_android_lite:1.9.0

* build.gradle, README and code update for PT1.9 for Question Answering using pytorch_android_lite:1.9.0

* build.gradle, README and code update for PT1.9 for TorchVideo using pytorch_android_lite:1.9.0

* HelloWorld script fix

* README update for TorchVideo</span></p>
  </div>

  </article>

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

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

<p>Important points:</p>
<ul>
<li>
<p><a href="https://github.com/pytorch/android-demo-app/commit/3d31a030d92524cc1fd780562577cd6f638489f6#diff-848fcd652009786a97515cee8d66df2082da0e76f269a34104d2b0521aa98b4f" rel="noopener nofollow ugc">trace_model.py</a></p>
</li>
<li>
<p><a href="https://github.com/pytorch/android-demo-app/commit/3d31a030d92524cc1fd780562577cd6f638489f6#diff-036d411fe06c646b9a9e2303de5219f489c536b110f1935190eb6eb16aef3840" rel="noopener nofollow ugc">MainActivity.java</a></p>
</li>
<li>
<p><a href="https://github.com/pytorch/android-demo-app/commit/3d31a030d92524cc1fd780562577cd6f638489f6#diff-70f975f1309162f59feaf315383937cd1af80dadb31ad603deee3c51945db230" rel="noopener nofollow ugc">build.gradle</a></p>
</li>
</ul>

username: yangcheng
cooked: <p>I see, it worked if I only use LiteModuleLoader and remove all Module reference. I wanted to do a comparison between Module.load and LiteModuleLoader , I guess that’s not possible now ?</p>

username: IvanKobzarev
cooked: <p><a class="mention" href="/u/yangcheng">@yangcheng</a> We started publishing 2 gradle artifacts:<br>
implementation ‘org.pytorch:pytorch_android:1.9.0’<br>
implementation ‘org.pytorch:pytorch_android_lite:1.9.0’ (contains mobile interpreter that uses different java entry point - LiteModuleLoader)</p>
<p>The java classes of both packages are the same, that’s why there is an class duplication error.</p>
<p>Without workaround it is not possible to have both of them in the same android artifact.</p>

username: zaeemasvatisazi
cooked: <p>Hi there</p>
<p>So I’m trying to run a Mask-RCNN object detection model in Android Studio using PyTorch Mobile, and I’m getting the same issue as mentioned. You mentioned that it is not possible to have both:</p>
<pre><code class="lang-auto">implementation ‘org.pytorch:pytorch_android:1.9.0’
implementation ‘org.pytorch:pytorch_android_lite:1.9.0’
</code></pre>
<p>in the same android artifact, but when I try having just the original version (and not the lite version), I still get the duplicate class error. This is how my the relevant dependencies in my build.gradle file looks:</p>
<pre><code class="lang-auto">    implementation 'org.pytorch:pytorch_android:1.9.0'
    implementation 'org.pytorch:pytorch_android_torchvision:1.9.0'
</code></pre>
<p>Any suggestions/help would be greatly appreciated</p>

username: cccclai
cooked: <p>Try using this:</p>
<pre><code class="lang-auto">    implementation 'org.pytorch:pytorch_android_lite:1.9.0'
    implementation 'org.pytorch:pytorch_android_torchvision:1.9.0'
</code></pre>

username: cccclai
cooked: <pre><code class="lang-auto">    implementation 'org.pytorch:pytorch_android_lite:1.9.0'
    implementation 'org.pytorch:pytorch_android_torchvision:1.9.0'
</code></pre>
<p>works for me on the <a href="https://github.com/pytorch/android-demo-app/tree/master/ImageSegmentation" rel="noopener nofollow ugc">ImageSegmentation app</a>. It should just fetch <code>libpytorch_jni_lite.so</code> and I’m not sure why the error message shows that it still looks for <code>libpytorch_jni.so</code>. It looks like some cache issue to me.</p>

username: yangcheng
cooked: <p>The problem is android and android_torchvision do not work well together.</p>
<pre><code class="lang-auto">implementation ‘org.pytorch:pytorch_android:1.9.0’
implementation 'org.pytorch:pytorch_android_torchvision:1.9.0'
</code></pre>
<p>It will error out with duplicate class.</p>
<p>I inspect the aar and it seems like android_torchvision has a dependency on android_lite. I am no expert on gradle or packaging, just want to provide more information that maybe related.</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://search.maven.org/artifact/org.pytorch/pytorch_android_torchvision/1.9.0/aar">
  <header class="source">
      <img src="https://search.maven.org/artifact/org.pytorch/pytorch_android_torchvision/1.9.0/favicon.ico?v=3" class="site-icon" width="" height="">

      <a href="https://search.maven.org/artifact/org.pytorch/pytorch_android_torchvision/1.9.0/aar" target="_blank" rel="noopener nofollow ugc">search.maven.org</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://search.maven.org/artifact/org.pytorch/pytorch_android_torchvision/1.9.0/aar" target="_blank" rel="noopener nofollow ugc">Maven Central Repository Search</a></h3>

  <p>Official search by the maintainers of Maven Central Repository</p>


  </article>

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

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

<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/d/8/d8bffa15fade600f59b44996527ca093d1b86154.png" alt="Screen Shot 2021-06-24 at 10.32.14" data-base62-sha1="uVsrpUje2sNSmfpOQocyaw07SxS" width="592" height="237"></p>
<p>the output of <code>./gradlew app:dependencies</code> on <code>ObjectDetection</code> 's latest master 87c0ac738e48cab8b4afeacf45316671b2370bc9 confirms torch_vision has a dependency on android_lite.</p>
<pre><code class="lang-auto">+--- org.pytorch:pytorch_android:1.9.0
|    +--- com.facebook.fbjni:fbjni-java-only:0.0.3
|    |    \--- com.facebook.soloader:nativeloader:0.8.0
|    +--- com.facebook.soloader:nativeloader:0.8.0
|    \--- androidx.appcompat:appcompat:1.0.0 -&gt; 1.2.0 (*)
+--- org.pytorch:pytorch_android_torchvision:1.9.0
|    +--- org.pytorch:pytorch_android_lite:1.9.0
|    |    +--- com.facebook.fbjni:fbjni-java-only:0.0.3 (*)
|    |    +--- com.facebook.soloader:nativeloader:0.8.0
|    |    \--- androidx.appcompat:appcompat:1.0.0 -&gt; 1.2.0 (*)
|    +--- com.facebook.soloader:nativeloader:0.8.0
|    \--- androidx.appcompat:appcompat:1.0.0 -&gt; 1.2.0 (*)
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

</code></pre>

username: yangcheng
cooked: <p>I figured out. it’s not caching issue. <code>Module.load</code> is still available in Java API even I only use android_lite in gradle. And in this case, if I use <code>Module.load</code>  instead of <code>LiteModuleLoader.load,</code> I would get the <code>libpytorch_jni.so</code> error</p>

username: cccclai
cooked: <p>If the app use ‘org.pytorch:pytorch_android:1.9.0’, <code>Module.load</code> is the correct api to load.</p>
<p>If the app use ‘org.pytorch:pytorch_android_lite:1.9.0’, <code>LiteModuleLoader.load</code> is the correct api to load.</p>
<p>Is it possible for you to use <code>LiteModuleLoader.load</code> in Java Api + <code>pytorch_android_lite</code> in <code>build.gradle</code>?</p>

username: zaeemasvatisazi
cooked: <p>Hi there. Thanks for your reply. So unfortunately I’m unable to use <code>LiteModuleLoader.load</code> because to use that requires that the <code>model._save_for_lite_interpreter()</code> be used to generate a <code>.ptl</code> version of the model, but it throws an error (on the Python side) when I try calling that function on my Faster-RCNN + Mask-RCNN predictor model. This is the error it throws:</p>
<pre><code class="lang-auto">RuntimeError: CREATE_OBJECT is not supported in mobile module. Workaround: instead of using arbitrary class type (class Foo()), define a pytorch class (class Foo(torch.nn.Module)).
</code></pre>
<p>However, when converting the model to a <code>.pt</code> model with <code>model.save()</code> it throws no errors on the Python side. However, the <code>.pt</code> seems to only work with <code>Module.load</code> and <code>org.pytorch:pytorch_android:1.9.0</code> in <code>build.gradle</code></p>
<p>Does this mean that my particular model is simply not supported by the API?</p>
<p>Thank you so much for your help by the way!</p>

username: cccclai
cooked: <p>A <code>.ptl</code> version of the model (generated by <code>model._save_for_lite_interpreter()</code>)  is need for <code>LiteModuleLoader.load</code>.</p>
<p>Based on the error message, is it possible to regenerate the <code>.ptl</code> model by replacing the <code>class Foo()</code> with <code>class Foo(torch.nn.Module)</code>, and resaving the model with <code>model._save_for_lite_interpreter()</code>?</p>
<p>If the <code>pytorch_android:1.9</code> is still preferred (not recommended because of increased app size and performance regression), one workaround is use the <a href="https://oss.sonatype.org/#nexus-search;quick~torchvision_android" rel="noopener nofollow ugc">nightly <code>torchvision</code> build</a> and use it as</p>
<pre><code class="lang-auto">    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'org.pytorch:pytorch_android:1.9.0'
    implementation(name:'pytorch_android_torchvision-1.9.0, ext:'aar')
</code></pre>

username: yangcheng
cooked: <p>yes, I got my app running now, thanks a lot!<br>
Maybe LiteModuleLoader package can remove Module.load method to avoid the confusion. that can be really helpful for developers migrate their code</p>