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)
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
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.
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:
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:
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.
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.
the output of ./gradlew app:dependencies on ObjectDetection 's latest master 87c0ac738e48cab8b4afeacf45316671b2370bc9 confirms torch_vision has a dependency on android_lite.
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
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?
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
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