Skip to content

Commit

Permalink
Add a workflow to release Android binaries (pytorch#110976)
Browse files Browse the repository at this point in the history
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
huydhn authored and pytorchmergebot committed Oct 11, 2023
1 parent 5aa96fd commit 2edc75a
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 55 deletions.
File renamed without changes.
20 changes: 15 additions & 5 deletions .github/workflows/_run_android_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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.
Expand All @@ -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
Expand Down
48 changes: 48 additions & 0 deletions .github/workflows/build-android-binaries.yml
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',
},
]}
9 changes: 8 additions & 1 deletion .github/workflows/periodic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,14 @@ jobs:
with:
test-matrix: |
{ include: [
{ config: "default", shard: 1, num_shards: 1, runner: "ubuntu-20.04-16x" },
{ config: "default",
shard: 1,
num_shards: 1,
runner: "ubuntu-20.04-16x"
use_lite_interpreter: 1,
# Just set x86 for testing here
support_abi: x86,
},
]}
linux-vulkan-focal-py3_11-clang10-build:
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,7 @@ venv/
# Log files
*.log
sweep/

# Android build artifacts
android/pytorch_android/.cxx
android/pytorch_android_torchvision/.cxx
1 change: 1 addition & 0 deletions android/pytorch_android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ android {
println 'Build pytorch_jni'
exclude 'org/pytorch/LiteModuleLoader.java'
exclude 'org/pytorch/LiteNativePeer.java'
exclude 'org/pytorch/LitePyTorchAndroid.java'
} else {
println 'Build pytorch_jni_lite'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.Ignore;

public abstract class PytorchTestBase {
private static final String TEST_MODULE_ASSET_NAME = "android_api_module.ptl";
Expand Down Expand Up @@ -413,7 +414,10 @@ public void testOtherMathOps() throws IOException {
}

@Test
@Ignore
public void testSpectralOps() throws IOException {
// NB: This model fails without lite interpreter. The error is as follows:
// RuntimeError: stft requires the return_complex parameter be given for real inputs
runModel("spectral_ops");
}

Expand Down
34 changes: 0 additions & 34 deletions android/pytorch_android/src/main/cpp/pytorch_jni_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
#include <fbjni/fbjni.h>

#include "pytorch_jni_common.h"
#if defined(__ANDROID__)
#ifndef USE_PTHREADPOOL
#define USE_PTHREADPOOL
#endif /* USE_PTHREADPOOL */
#include <caffe2/utils/threadpool/pthreadpool-cpp.h>
#endif

namespace pytorch_jni {

Expand Down Expand Up @@ -666,32 +660,4 @@ at::IValue JIValue::JIValueToAtIValue(
typeCode);
}

#if defined(__ANDROID__)
class PyTorchAndroidJni : public facebook::jni::JavaClass<PyTorchAndroidJni> {
public:
constexpr static auto kJavaDescriptor = "Lorg/pytorch/PyTorchAndroid;";

static void registerNatives() {
javaClassStatic()->registerNatives({
makeNativeMethod(
"nativeSetNumThreads", PyTorchAndroidJni::setNumThreads),
});
}

static void setNumThreads(facebook::jni::alias_ref<jclass>, jint numThreads) {
caffe2::pthreadpool()->set_thread_count(numThreads);
}
};
#endif

void common_registerNatives() {
static const int once = []() {
#if defined(__ANDROID__)
pytorch_jni::PyTorchAndroidJni::registerNatives();
#endif
return 0;
}();
((void)once);
}

} // namespace pytorch_jni
33 changes: 33 additions & 0 deletions android/pytorch_android/src/main/cpp/pytorch_jni_jit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
#include <android/log.h>

#ifndef USE_PTHREADPOOL
#define USE_PTHREADPOOL
#endif /* USE_PTHREADPOOL */
#include <caffe2/utils/threadpool/pthreadpool-cpp.h>
#endif

namespace pytorch_jni {
Expand Down Expand Up @@ -235,6 +240,34 @@ class PytorchJni : public facebook::jni::HybridClass<PytorchJni> {
}
};

#if defined(__ANDROID__)
class PyTorchAndroidJni : public facebook::jni::JavaClass<PyTorchAndroidJni> {
public:
constexpr static auto kJavaDescriptor = "Lorg/pytorch/PyTorchAndroid;";

static void registerNatives() {
javaClassStatic()->registerNatives({
makeNativeMethod(
"nativeSetNumThreads", PyTorchAndroidJni::setNumThreads),
});
}

static void setNumThreads(facebook::jni::alias_ref<jclass>, jint numThreads) {
caffe2::pthreadpool()->set_thread_count(numThreads);
}
};
#endif

void common_registerNatives() {
static const int once = []() {
#if defined(__ANDROID__)
pytorch_jni::PyTorchAndroidJni::registerNatives();
#endif
return 0;
}();
((void)once);
}

} // namespace pytorch_jni

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
Expand Down
33 changes: 33 additions & 0 deletions android/pytorch_android/src/main/cpp/pytorch_jni_lite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
#include <android/log.h>

#ifndef USE_PTHREADPOOL
#define USE_PTHREADPOOL
#endif /* USE_PTHREADPOOL */
#include <caffe2/utils/threadpool/pthreadpool-cpp.h>
#endif

namespace pytorch_jni {
Expand Down Expand Up @@ -199,6 +204,34 @@ class PytorchJni : public facebook::jni::HybridClass<PytorchJni> {
}
};

#if defined(__ANDROID__)
class PyTorchAndroidJni : public facebook::jni::JavaClass<PyTorchAndroidJni> {
public:
constexpr static auto kJavaDescriptor = "Lorg/pytorch/LitePyTorchAndroid;";

static void registerNatives() {
javaClassStatic()->registerNatives({
makeNativeMethod(
"nativeSetNumThreads", PyTorchAndroidJni::setNumThreads),
});
}

static void setNumThreads(facebook::jni::alias_ref<jclass>, jint numThreads) {
caffe2::pthreadpool()->set_thread_count(numThreads);
}
};
#endif

void common_registerNatives() {
static const int once = []() {
#if defined(__ANDROID__)
pytorch_jni::PyTorchAndroidJni::registerNatives();
#endif
return 0;
}();
((void)once);
}

} // namespace pytorch_jni

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
Expand Down
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public final class PyTorchAndroid {
if (!NativeLoader.isInitialized()) {
NativeLoader.init(new SystemDelegate());
}
NativeLoader.loadLibrary("pytorch_jni_lite");
NativeLoader.loadLibrary("pytorch_jni");
PyTorchCodegenLoader.loadNativeLibs();
}

Expand Down
Loading

0 comments on commit 2edc75a

Please sign in to comment.