forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a workflow to release Android binaries (pytorch#110976)
This adds 2 jobs to build PyTorch Android with and without lite interpreter: * Keep the list of currently supported ABI armeabi-v7a, arm64-v8a, x86, x86_64 * Pass all the test on emulator * Run an the test app on emulator and my Android phone `arm64-v8a` without any issue ![Screenshot_20231010-114453](https://github.com/pytorch/pytorch/assets/475357/57e12188-1675-44d2-a259-9f9577578590) * Run on AWS https://us-west-2.console.aws.amazon.com/devicefarm/home#/mobile/projects/b531574a-fb82-40ae-b687-8f0b81341ae0/runs/5fce6818-628a-4099-9aab-23e91a212076 Pull Request resolved: pytorch#110976 Approved by: https://github.com/atalman
- Loading branch information
1 parent
5aa96fd
commit 2edc75a
Showing
16 changed files
with
283 additions
and
55 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,17 +41,25 @@ jobs: | |
strategy: | ||
matrix: ${{ fromJSON(needs.filter.outputs.test-matrix) }} | ||
fail-fast: false | ||
# NB: This job can only run on GitHub Linux runner atm. This is an ok thing though | ||
# because that runner is ephemeral and could access upload secrets | ||
runs-on: ${{ matrix.runner }} | ||
env: | ||
# GitHub runner installs Android SDK on this path | ||
ANDROID_ROOT: /usr/local/lib/android | ||
ANDROID_NDK_VERSION: '21.4.7075529' | ||
BUILD_LITE_INTERPRETER: ${{ matrix.use_lite_interpreter }} | ||
# 4 of them are supported atm: armeabi-v7a, arm64-v8a, x86, x86_64 | ||
SUPPORT_ABI: '${{ matrix.support_abi }}' | ||
steps: | ||
# [see note: pytorch repo ref] | ||
- name: Checkout PyTorch | ||
uses: pytorch/pytorch/.github/actions/checkout-pytorch@main | ||
|
||
- name: Setup miniconda | ||
uses: pytorch/test-infra/.github/actions/setup-miniconda@main | ||
with: | ||
python-version: 3.8 | ||
environment-file: .github/requirements/conda-env-${{ runner.os }}-${{ runner.arch }} | ||
environment-file: .github/requirements/conda-env-${{ runner.os }}-${{ runner.arch }}.txt | ||
|
||
- name: Install NDK | ||
uses: nick-fields/[email protected] | ||
|
@@ -60,12 +68,12 @@ jobs: | |
max_attempts: 3 | ||
retry_wait_seconds: 90 | ||
command: | | ||
set -eux | ||
# Install NDK 21 after GitHub update | ||
# https://github.com/actions/virtual-environments/issues/5595 | ||
ANDROID_ROOT="/usr/local/lib/android" | ||
ANDROID_SDK_ROOT="${ANDROID_ROOT}/sdk" | ||
ANDROID_NDK="${ANDROID_SDK_ROOT}/ndk-bundle" | ||
ANDROID_NDK_VERSION="21.4.7075529" | ||
SDKMANAGER="${ANDROID_SDK_ROOT}/cmdline-tools/latest/bin/sdkmanager" | ||
# NB: This step downloads and installs NDK, thus it could be flaky. | ||
|
@@ -86,8 +94,10 @@ jobs: | |
- name: Build PyTorch Android | ||
run: | | ||
set -eux | ||
echo "CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname "$(which conda)")/../"}" >> "${GITHUB_ENV}" | ||
${CONDA_RUN} ./scripts/build_pytorch_android.sh x86 | ||
${CONDA_RUN} ./scripts/build_pytorch_android.sh "${SUPPORT_ABI}" | ||
- name: Run tests | ||
uses: reactivecircus/android-emulator-runner@v2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
name: Build Android binaries | ||
|
||
on: | ||
push: | ||
branches: | ||
- nightly | ||
tags: | ||
# NOTE: Binary build pipelines should only get triggered on release candidate builds | ||
# Release candidate tags look like: v1.11.0-rc1 | ||
- v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+ | ||
paths: | ||
- .github/workflows/build-android-binaries.yml | ||
- .github/workflows/_run_android_tests.yml | ||
- android/** | ||
pull_request: | ||
paths: | ||
- .github/workflows/build-android-binaries.yml | ||
- .github/workflows/_run_android_tests.yml | ||
- android/** | ||
# NB: We can use this workflow dispatch to test and build the binaries manually | ||
workflow_dispatch: | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}-${{ github.event_name == 'workflow_dispatch' }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
android-build-test: | ||
name: android-build-test | ||
uses: ./.github/workflows/_run_android_tests.yml | ||
with: | ||
test-matrix: | | ||
{ include: [ | ||
{ config: 'default', | ||
shard: 1, | ||
num_shards: 1, | ||
runner: 'ubuntu-20.04-16x', | ||
use_lite_interpreter: 1, | ||
support_abi: 'armeabi-v7a,arm64-v8a,x86,x86_64', | ||
}, | ||
{ config: 'default', | ||
shard: 1, | ||
num_shards: 1, | ||
runner: 'ubuntu-20.04-16x', | ||
use_lite_interpreter: 0, | ||
support_abi: 'armeabi-v7a,arm64-v8a,x86,x86_64', | ||
}, | ||
]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
android/pytorch_android/src/main/java/org/pytorch/LitePyTorchAndroid.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.pytorch; | ||
|
||
import android.content.res.AssetManager; | ||
import com.facebook.jni.annotations.DoNotStrip; | ||
import com.facebook.soloader.nativeloader.NativeLoader; | ||
import com.facebook.soloader.nativeloader.SystemDelegate; | ||
|
||
public final class LitePyTorchAndroid { | ||
static { | ||
if (!NativeLoader.isInitialized()) { | ||
NativeLoader.init(new SystemDelegate()); | ||
} | ||
NativeLoader.loadLibrary("pytorch_jni_lite"); | ||
PyTorchCodegenLoader.loadNativeLibs(); | ||
} | ||
|
||
/** | ||
* Attention: This is not recommended way of loading production modules, as prepackaged assets | ||
* increase apk size etc. For production usage consider using loading from file on the disk {@link | ||
* org.pytorch.Module#load(String)}. | ||
* | ||
* <p>This method is meant to use in tests and demos. | ||
*/ | ||
public static Module loadModuleFromAsset( | ||
final AssetManager assetManager, final String assetName, final Device device) { | ||
return new Module(new LiteNativePeer(assetName, assetManager, device)); | ||
} | ||
|
||
public static Module loadModuleFromAsset( | ||
final AssetManager assetManager, final String assetName) { | ||
return new Module(new LiteNativePeer(assetName, assetManager, Device.CPU)); | ||
} | ||
|
||
/** | ||
* Globally sets the number of threads used on native side. Attention: Has global effect, all | ||
* modules use one thread pool with specified number of threads. | ||
* | ||
* @param numThreads number of threads, must be positive number. | ||
*/ | ||
public static void setNumThreads(int numThreads) { | ||
if (numThreads < 1) { | ||
throw new IllegalArgumentException("Number of threads cannot be less than 1"); | ||
} | ||
|
||
nativeSetNumThreads(numThreads); | ||
} | ||
|
||
@DoNotStrip | ||
private static native void nativeSetNumThreads(int numThreads); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.