pytorch/android
Ivan Kobzarev a71aefe857 [android][test_app] cleanup (#40136)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/40136

Test Plan: Imported from OSS

Differential Revision: D22084170

Pulled By: IvanKobzarev

fbshipit-source-id: f8d2d0494b3ac4f7fe2118238d621155d697d2c4
2020-06-17 11:07:44 -07:00
..
gradle [android] Maven publishing license fix (#32474) 2020-03-20 12:27:08 -07:00
libs [android] Remove android fbjni subproject (#39691) 2020-06-15 15:58:18 -07:00
pytorch_android [vulkan] jni build support USE_VULKAN (#39188) 2020-05-28 15:39:02 -07:00
pytorch_android_torchvision [codemod][lint][fbcode] Apply google-java-format 2020-02-13 12:14:14 -08:00
test_app [android][test_app] cleanup (#40136) 2020-06-17 11:07:44 -07:00
.gitignore
build.gradle Tensor prep from image in native (#31426) 2020-01-15 17:10:00 -08:00
build_test_app.sh [pytorch] consolidate android gradle build scripts (#39999) 2020-06-15 23:55:21 -07:00
common.sh [pytorch] consolidate android gradle build scripts (#39999) 2020-06-15 23:55:21 -07:00
gradle.properties Bump base version to 1.6.0a0 (#35495) 2020-03-27 12:14:49 -07:00
README.md [android][fbjni] Test_app and Readme update with the recent fbjni dep state (#40058) 2020-06-16 18:42:56 -07:00
run_tests.sh [pytorch] consolidate android gradle build scripts (#39999) 2020-06-15 23:55:21 -07:00
settings.gradle [android] Remove android fbjni subproject (#39691) 2020-06-15 15:58:18 -07:00

Android

Demo applications and tutorials

Demo applications with code walk-through can be find in this github repo.

Publishing

Release

Release artifacts are published to jcenter:

repositories {
    jcenter()
}

dependencies {
    implementation 'org.pytorch:pytorch_android:1.5.0'
    implementation 'org.pytorch:pytorch_android_torchvision:1.5.0'
}
Nightly

Nightly(snapshots) builds are published every night from master branch to nexus sonatype snapshots repository

To use them repository must be specified explicitly:

repositories {
    maven {
        url "https://oss.sonatype.org/content/repositories/snapshots"
    }
}

dependencies {
    ...
    implementation 'org.pytorch:pytorch_android:1.6.0-SNAPSHOT'
    implementation 'org.pytorch:pytorch_android_torchvision:1.6.0-SNAPSHOT'
    ...
}

The current nightly(snapshots) version is the value of VERSION_NAME in gradle.properties in current folder, at this moment it is 1.6.0-SNAPSHOT.

Building PyTorch Android from Source

In some cases you might want to use a local build of pytorch android, for example you may build custom libtorch binary with another set of operators or to make local changes.

For this you can use ./scripts/build_pytorch_android.sh script.

git clone https://github.com/pytorch/pytorch.git
cd pytorch
git submodule update --init --recursive
sh ./scripts/build_pytorch_android.sh

The workflow contains several steps:

1. Build libtorch for android for all 4 android abis (armeabi-v7a, arm64-v8a, x86, x86_64)

2. Create symbolic links to the results of those builds: android/pytorch_android/src/main/jniLibs/${abi} to the directory with output libraries android/pytorch_android/src/main/cpp/libtorch_include/${abi} to the directory with headers. These directories are used to build libpytorch.so library that will be loaded on android device.

3. And finally run gradle in android/pytorch_android directory with task assembleRelease

Script requires that Android SDK, Android NDK and gradle are installed. They are specified as environment variables:

ANDROID_HOME - path to Android SDK

ANDROID_NDK - path to Android NDK

GRADLE_HOME - path to gradle

After successful build you should see the result as aar file:

$ find pytorch_android/build/ -type f -name *aar
pytorch_android/build/outputs/aar/pytorch_android.aar
pytorch_android_torchvision/build/outputs/aar/pytorch_android.aar

It can be used directly in android projects, as a gradle dependency:

allprojects {
    repositories {
        flatDir {
            dirs 'libs'
        }
    }
}

dependencies {
    implementation(name:'pytorch_android', ext:'aar')
    implementation(name:'pytorch_android_torchvision', ext:'aar')
    ...
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.facebook.soloader:nativeloader:0.8.0'
    implementation 'com.facebook.fbjni:fbjni-java-only:0.0.3'
}

We also have to add all transitive dependencies of our aars. As pytorch_android depends on 'com.android.support:appcompat-v7:28.0.0', 'com.facebook.soloader:nativeloader:0.8.0' and 'com.facebook.fbjni:fbjni-java-only:0.0.3', we need to add them. (In case of using maven dependencies they are added automatically from pom.xml).

You can check out test app example that uses aars directly.

More Details

You can find more details about the PyTorch Android API in the Javadoc.