python-for-android icon indicating copy to clipboard operation
python-for-android copied to clipboard

for pytorch

Open chenyang918 opened this issue 5 years ago • 8 comments

When is pytorch supported? If you support pytorch like that you can simply apply AI to mobile. If you don't have that idea, I hope I've said it and considered it. If you're trying, go for it. We look forward to your performance.

chenyang918 avatar Nov 14 '20 03:11 chenyang918

For performance Pytorch depends on NVIDIA hardware, but this does not exist on an Android device. If performance is not a concern, there are a few ways you could try to implement Pytorch in this environment.

The simplest would probably be to use the Java interface, though you may find a better way. https://pytorch.org/mobile/android/#quickstart-with-a-helloworld-example and pyjnius https://pyjnius.readthedocs.io/en/stable/

Please share the details when you have a working example.

RobertFlatt avatar Nov 18 '20 20:11 RobertFlatt

Pytorch CPU is confirmed working in Termux (https://github.com/xuancong84/install-PyTorch-on-Android)

there are cross-compile patches available from OpenEmbedded (https://github.com/renesas-rz/meta-renesas-ai/tree/master/meta-pytorch/recipes-mathematics/pytorch/files) that can be a starting point to writing a p4a build recipe.

mzakharo avatar Dec 08 '20 17:12 mzakharo

Pytorch support would be insanely useful. Check this out, it's a way to use packaged Pytorch as a module in a Java android app. I'm thinking this could be combined with p4a in a recipe. Maybe someone more experienced with recipes can give it a shot? https://pytorch.org/tutorials/recipes/android_native_app_with_custom_op.html

neddongo avatar Jun 12 '22 08:06 neddongo

If you just want predictions (and not full pytorch/training), the recommended path is to convert your pytorch model to tflite and use pyjnius to call android tflite API's.

mzakharo avatar Jun 12 '22 14:06 mzakharo

Yes I just want predictions on a model I trained elsewhere. Thanks for the tip, I'll try it out.

On Jun 12 2022, at 3:27 pm, Mikhail Zakharov @.***> wrote:

If you just want predictions (and not full pytorch/training), the recommended path is to convert your pytorch model to tflite and use pyjnius to call android tflite API's. — Reply to this email directly, view it on GitHub (https://github.com/kivy/python-for-android/issues/2353#issuecomment-1153187992), or unsubscribe (https://github.com/notifications/unsubscribe-auth/AS5QKPAPAXG3J5YPPJQMU2TVOXXUZANCNFSM4TVIX5VA). You are receiving this because you commented.


This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus

neddongo avatar Jun 12 '22 14:06 neddongo

No need for Pyjnius. P4a has a recipe for tflite-runtime, so you can use tflite-runtime on a desktop and Android. And an example https://github.com/Android-for-Python/c4k_tflite_example

RobertFlatt avatar Jun 12 '22 17:06 RobertFlatt

Does tflite-runtime support GPU on android?

On Sun, Jun 12, 2022, 1:20 PM RobertF @.***> wrote:

No need for Pyjnius. P4a has a recipe for tflite-runtime, so you can use tflite-runtime on a desktop and Android. And an example https://github.com/Android-for-Python/c4k_tflite_example

— Reply to this email directly, view it on GitHub https://github.com/kivy/python-for-android/issues/2353#issuecomment-1153238715, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEKUOYWWEYTRLUAQK6QGQ3VOYL4FANCNFSM4TVIX5VA . You are receiving this because you commented.Message ID: @.***>

mzakharo avatar Jun 12 '22 20:06 mzakharo

tflite-runtime performance varies widely by platform, see the table here https://github.com/Android-for-Python/c4k_tflite_example , because it uses different delegates. A delegate will use the some combination available resources (instructions/processor/memory model).

As, for example, you can see with the Windows, and Windows + Coral line items in the table.

It is Google code, I have not tried to determine the internal operation of these delegates. Though I do happen to know that arm7 performance depends on the NEON instructions.

The delegate used depends on Android version too. The best delegate is an api introduced with Android 11. As shown by 25fps on object classification. The Java api is compiled from the same code base so I expect its performance will be somewhat similar, but I don't have any benchmark data.

tflite-runtime is documented https://www.tensorflow.org/lite/guide/python and has plenty of Python (in the raspberry_pi subdirectories) and Kotlin (in android subdirectory) examples https://github.com/tensorflow/examples/tree/master/lite/examples , so comparisons would be possible.

RobertFlatt avatar Jun 12 '22 22:06 RobertFlatt

👋 We use the issue tracker exclusively for bug reports and feature requests. However, this issue appears to be a support request. Please use our support channels to get help with the project.

If you're having trouble installing or using python-for-android, maybe you could be interested in our quickstart guide.

Let us know if this comment was made in error, and we'll be happy to reopen the issue.

github-actions[bot] avatar Nov 20 '23 01:11 github-actions[bot]