pytorch/android
Jiakai Liu 9a5e9d8cec [pytorch][mobile] change mobile build scripts to build PyTorch by default (#34203)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/34203

Currently cmake and mobile build scripts still build libcaffe2 by
default. To build pytorch mobile users have to set environment variable
BUILD_PYTORCH_MOBILE=1 or set cmake option BUILD_CAFFE2_MOBILE=OFF.

PyTorch mobile has been released for a while. It's about time to change
CMake and build scripts to build libtorch by default.

Changed caffe2 CI job to build libcaffe2 by setting BUILD_CAFFE2_MOBILE=1
environment variable. Only found android CI for libcaffe2 - do we ever
have iOS CI for libcaffe2?

Test Plan: Imported from OSS

Differential Revision: D20267274

Pulled By: ljk53

fbshipit-source-id: 9d997032a599c874d62fbcfc4f5d4fbf8323a12e
2020-03-05 23:40:47 -08:00
..
gradle Pickup proxy parameters for publishing (#27389) 2019-10-04 16:21:31 -07:00
libs fbjni gradle obey ABI_FILTERS parameter 2019-11-19 20:09:48 -08:00
pytorch_android [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
pytorch_android_torchvision [codemod][lint][fbcode] Apply google-java-format 2020-02-13 12:14:14 -08:00
test_app [codemod][lint][fbcode] Apply google-java-format 2020-02-13 12:14:14 -08:00
.gitignore Test application for profiling, CMake params for debug symbols (#28406) 2019-11-08 14:19:04 -08:00
build_test_app.sh [pytorch][mobile] change mobile build scripts to build PyTorch by default (#34203) 2020-03-05 23:40:47 -08:00
build.gradle Tensor prep from image in native (#31426) 2020-01-15 17:10:00 -08:00
gradle.properties Update all instances of 1.4.0 -> 1.5.0 (#31785) 2020-01-07 08:00:17 -08:00
README.md Update Docs for building PyTorch for Android. 2020-01-30 17:12:03 -08:00
run_tests.sh Script for full android build to aars; script to run android tests (#26833) 2019-09-30 14:25:00 -07:00
settings.gradle Test application for profiling, CMake params for debug symbols (#28406) 2019-11-08 14:19:04 -08: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.3.0'
    implementation 'org.pytorch:pytorch_android_torchvision:1.3.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.5.0-SNAPSHOT'
    implementation 'org.pytorch:pytorch_android_torchvision:1.5.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.5.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
libs/fbjni_local/build/outputs/aar/pytorch_android_fbjni.aar

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

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

android {
    ...
    packagingOptions {
        pickFirst "**/libfbjni.so"
    }
    ...
}

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

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

At the moment for the case of using aar files directly we need additional configuration due to packaging specific (libfbjni.so is packaged in both pytorch_android_fbjni.aar and pytorch_android.aar).

packagingOptions {
    pickFirst "**/libfbjni.so"
}

More Details

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