Skip to content

[android][recipe] Making android native app using torchscript model with custom op #1041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 2, 2020

Conversation

IvanKobzarev
Copy link
Contributor

@IvanKobzarev IvanKobzarev commented Jun 26, 2020

More condensed setup information and result application setup is here:

pytorch/android-demo-app#73

Lyken17 and others added 5 commits June 23, 2020 22:14
Fix allreduce implmentation in tutorial
Signed-off-by: Edward Z. Yang <ezyang@fb.com>
- Don't clean before running html-noplot (if you want to do a clean
  build, just run make clean yourself!)
- Replace filename_pattern with ignore_pattern (with a negative
  lookahead assertion).  This means that specifying a GALLERY_PATTERN
  will also skip parsing all Python files that you don't care about.
  This saves several seconds from the no-op doc build.
- Use distutils to only copy rst files if the mtime is newer than the
  destination; this (probably) helps Sphinx avoid recompiling these
  files when it's useless.

There are some semantic changes in this diff which is why I'm putting
it up for review.

Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Reduce amount of unnecessary recompilation done by sphinx

- OpenCV SDK for Android

Our custom operator will be implemented using the OpenCV library. To use it for android, we need to download OpenCV SDK for Android with prebuilt libraries.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Capitalize "android"

Making Android Application
==========================

After we succeeded in having ``compute.pt``, we want to use this TorchScript model within android application. Using general TorchScript models (without custom operators) on Android, using java API, you can find `here <https://pytorch.org/mobile/android/>`_. We can not use this approach for our case, as our model uses a custom operator(``my_ops.warp_perspective``), default TorchScript execution will fail to find it.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Capitalize "java"


After we succeeded in having ``compute.pt``, we want to use this TorchScript model within android application. Using general TorchScript models (without custom operators) on Android, using java API, you can find `here <https://pytorch.org/mobile/android/>`_. We can not use this approach for our case, as our model uses a custom operator(``my_ops.warp_perspective``), default TorchScript execution will fail to find it.

Registration of ops is not exposed to PyTorch java API, thus we need to build Android Application with native part (C++) and using LibTorch C++ API to implement and register the same custom operator for Android. As our operator uses the OpenCV library - we will use prebuilt OpenCV Android libraries and use the same functions from OpenCV.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Capitalize "java"

Android Application Manifest
============================

Every android application has a manifest.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Capitalize "android"

}
}

``NativePeer#loadAndForwardModel`` is declared as ``native``, it does not have definition in java. Call to this method will beredispatched through jni to C++ method in our ``libpytorch_nativeapp.so``, in ``NativeApp/app/src/main/cpp/pytorch_nativeapp.cpp``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add space: "beredispatched" -> "be re-dispatched"
Capitalize "jni" -> "JNI"




The full setup of this app you can find in `PyTorch Android Demo Application Repository <https://github.com/pytorch/android-demo-app/tree/master/NativeApp>`_.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend to also add this line at the beginning of the recipe for users to download and follow along from the start.

- How to use within this application TorchScript models with custom operators.

Setup
=====
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Recommend not to use the same header level === for sub-headers as is currently used for the title.

@IvanKobzarev IvanKobzarev force-pushed the recipe_aarlink_customop branch from 33e2a77 to fc41a1d Compare June 30, 2020 16:51
@IvanKobzarev IvanKobzarev force-pushed the recipe_aarlink_customop branch from fc41a1d to 8e4e379 Compare June 30, 2020 16:57
@IvanKobzarev IvanKobzarev requested a review from jlin27 July 1, 2020 14:50
@jlin27 jlin27 changed the base branch from release/1.6 to master July 2, 2020 19:33
@netlify
Copy link

netlify bot commented Jul 2, 2020

Deploy preview for pytorch-tutorials-preview ready!

Built with commit a79c227

https://deploy-preview-1041--pytorch-tutorials-preview.netlify.app

@jlin27 jlin27 changed the base branch from master to release/1.6 July 2, 2020 20:11
@jlin27 jlin27 merged commit 7b615c1 into pytorch:release/1.6 Jul 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants