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

Hi, I am super excited to see 1.9 release so I give a try, however I can’t get the hello world working.
if I simple change dependencies from

    implementation 'org.pytorch:pytorch_android:1.8.0-SNAPSHOT'
    implementation 'org.pytorch:pytorch_android_torchvision:1.8.0-SNAPSHOT'

to

    implementation 'org.pytorch:pytorch_android:1.9.0'
    implementation 'org.pytorch:pytorch_android_torchvision:1.9.0'

I would get lost of similar compile error of duplicate class. like those

  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)

If I use

    implementation 'org.pytorch:pytorch_android_lite:1.9.0'
    implementation 'org.pytorch:pytorch_android_torchvision:1.9.0'

The code will build but at run time, it crash with couldn’t find "libpytorch_jni.so"

I inspected the APK and there is no libpytorch_jni.so in lib folder. there are only
libc++_shared.so libfbjni.so libpytorch_jni_lite.so libpytorch_vision_jni.so

It seems like the dependencies or packaging is not setup straight forward, I wonder what is a working configuration. Thanks

@IvanKobzarev any ideas here?

Try use pytorch_android_lite,

Look the project (android-demo-app/ObjectDetection at master · pytorch/android-demo-app · GitHub). The commit with update version.

Important points:

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 ?

@yangcheng We started publishing 2 gradle artifacts:
implementation ‘org.pytorch:pytorch_android:1.9.0’
implementation ‘org.pytorch:pytorch_android_lite:1.9.0’ (contains mobile interpreter that uses different java entry point - LiteModuleLoader)

The java classes of both packages are the same, that’s why there is an class duplication error.

Without workaround it is not possible to have both of them in the same android artifact.

Hi there

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:

implementation ‘org.pytorch:pytorch_android:1.9.0’
implementation ‘org.pytorch:pytorch_android_lite:1.9.0’

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:

    implementation 'org.pytorch:pytorch_android:1.9.0'
    implementation 'org.pytorch:pytorch_android_torchvision:1.9.0'

Any suggestions/help would be greatly appreciated

Try using this:

    implementation 'org.pytorch:pytorch_android_lite:1.9.0'
    implementation 'org.pytorch:pytorch_android_torchvision:1.9.0'
    implementation 'org.pytorch:pytorch_android_lite:1.9.0'
    implementation 'org.pytorch:pytorch_android_torchvision:1.9.0'

works for me on the ImageSegmentation app. It should just fetch libpytorch_jni_lite.so and I’m not sure why the error message shows that it still looks for libpytorch_jni.so. It looks like some cache issue to me.

The problem is android and android_torchvision do not work well together.

implementation ‘org.pytorch:pytorch_android:1.9.0’
implementation 'org.pytorch:pytorch_android_torchvision:1.9.0'

It will error out with duplicate class.

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.

https://search.maven.org/artifact/org.pytorch/pytorch_android_torchvision/1.9.0/aar

Screen Shot 2021-06-24 at 10.32.14

the output of ./gradlew app:dependencies on ObjectDetection 's latest master 87c0ac738e48cab8b4afeacf45316671b2370bc9 confirms torch_vision has a dependency on android_lite.

+--- 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 -> 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 -> 1.2.0 (*)
|    +--- com.facebook.soloader:nativeloader:0.8.0
|    \--- androidx.appcompat:appcompat:1.0.0 -> 1.2.0 (*)
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

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

If the app use ‘org.pytorch:pytorch_android:1.9.0’, Module.load is the correct api to load.

If the app use ‘org.pytorch:pytorch_android_lite:1.9.0’, LiteModuleLoader.load is the correct api to load.

Is it possible for you to use LiteModuleLoader.load in Java Api + pytorch_android_lite in build.gradle?

Hi there. Thanks for your reply. So unfortunately I’m unable to use LiteModuleLoader.load because to use that requires that the model._save_for_lite_interpreter() be used to generate a .ptl 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:

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)).

However, when converting the model to a .pt model with model.save() it throws no errors on the Python side. However, the .pt seems to only work with Module.load and org.pytorch:pytorch_android:1.9.0 in build.gradle

Does this mean that my particular model is simply not supported by the API?

Thank you so much for your help by the way!

A .ptl version of the model (generated by model._save_for_lite_interpreter()) is need for LiteModuleLoader.load.

Based on the error message, is it possible to regenerate the .ptl model by replacing the class Foo() with class Foo(torch.nn.Module), and resaving the model with model._save_for_lite_interpreter()?

If the pytorch_android:1.9 is still preferred (not recommended because of increased app size and performance regression), one workaround is use the nightly torchvision build and use it as

    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'org.pytorch:pytorch_android:1.9.0'
    implementation(name:'pytorch_android_torchvision-1.9.0, ext:'aar')

yes, I got my app running now, thanks a lot!
Maybe LiteModuleLoader package can remove Module.load method to avoid the confusion. that can be really helpful for developers migrate their code

1 Like