-
Notifications
You must be signed in to change notification settings - Fork 675
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HuggingFaceTokenizer: add support for Android (#3531)
* tokenizers: add support for building on Android - Build the Rust library for Android using `cargo-ndk` - Disable "http" feature for Rust library when building with Android - Modify to load native library in Android * android: Create tokenizer-native - Copy from pytorch-native and change for tokenizer native modules * Update build.cmd --------- Co-authored-by: Xin Yang <xyang19@gmail.com>
- Loading branch information
1 parent
ba4d837
commit f00f18e
Showing
15 changed files
with
255 additions
and
6 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
rootProject.name='android' | ||
include ':core' | ||
include ':pytorch-native' | ||
include ':tokenizer-native' |
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,3 @@ | ||
# Tokenizer | ||
|
||
This folder contains build of tokenizer-native library for Android. |
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,125 @@ | ||
apply plugin: 'com.android.library' | ||
apply plugin: 'maven-publish' | ||
apply plugin: 'signing' | ||
|
||
android { | ||
compileSdkVersion 34 | ||
namespace "ai.djl.android.tokenizer_native" | ||
|
||
defaultConfig { | ||
minSdkVersion 21 | ||
targetSdkVersion 34 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
consumerProguardFiles 'consumer-rules.pro' | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" | ||
} | ||
} | ||
|
||
sourceSets { | ||
main { | ||
jniLibs.srcDirs = ["jnilib/"] | ||
} | ||
} | ||
publishing { | ||
singleVariant("release") { | ||
withSourcesJar() | ||
} | ||
} | ||
} | ||
|
||
// TODO: Making publish as an individual gradle file | ||
signing { | ||
required(project.hasProperty("staging") || project.hasProperty("snapshot")) | ||
def signingKey = findProperty("signingKey") | ||
def signingPassword = findProperty("signingPassword") | ||
useInMemoryPgpKeys(signingKey, signingPassword) | ||
sign publishing.publications | ||
} | ||
|
||
afterEvaluate { | ||
publishing { | ||
publications { | ||
release(MavenPublication) { | ||
from components.release | ||
pom { | ||
name = "Tokenizer Android native library" | ||
description = "Tokenizer Android native library binary distribution" | ||
url = "http://www.djl.ai/android" | ||
packaging = "aar" | ||
|
||
licenses { | ||
license { | ||
name = "The Apache License, Version 2.0" | ||
url = "https://www.apache.org/licenses/LICENSE-2.0" | ||
} | ||
} | ||
|
||
scm { | ||
connection = "scm:git:git@github.com:deepjavalibrary/djl.git" | ||
developerConnection = "scm:git:git@github.com:deepjavalibrary/djl.git" | ||
url = "/~https://github.com/deepjavalibrary/djl" | ||
tag = "HEAD" | ||
} | ||
|
||
developers { | ||
developer { | ||
name = "DJL.AI Team" | ||
email = "djl-dev@amazon.com" | ||
organization = "Amazon AI" | ||
organizationUrl = "https://amazon.com" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
repositories { | ||
maven { | ||
if (project.hasProperty("snapshot")) { | ||
name = "snapshot" | ||
url = "https://oss.sonatype.org/content/repositories/snapshots/" | ||
credentials { | ||
username = findProperty("ossrhUsername") | ||
password = findProperty("ossrhPassword") | ||
} | ||
} else if (project.hasProperty("staging")) { | ||
name = "staging" | ||
url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" | ||
credentials { | ||
username = findProperty("ossrhUsername") | ||
password = findProperty("ossrhPassword") | ||
} | ||
} else { | ||
name = "local" | ||
url = "build/repo" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
tasks.register('processResources') { | ||
doLast { | ||
def url = "https://publish.djl.ai/tokenizer/jnilib/android" | ||
def abis = ["armeabi-v7a", "arm64-v8a", "x86", "x86_64"] | ||
abis.each { abi -> | ||
def downloadPath = new URL("${url}/${abi}/libdjl_tokenizer.so") | ||
def file = file("${project.projectDir}/jnilib/${abi}/libdjl_tokenizer.so") | ||
if (!file.exists()) { | ||
project.logger.lifecycle("${url}/${abi}/libdjl_tokenizer.so") | ||
file.getParentFile().mkdirs() | ||
downloadPath.withInputStream { i -> file.withOutputStream { it << i } } | ||
} | ||
} | ||
} | ||
} | ||
|
||
assemble.dependsOn processResources |
Empty file.
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 @@ | ||
../gradlew |
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,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
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 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"/> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -ex | ||
|
||
WORK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
export WORK_DIR | ||
|
||
FLAVOR=$1 | ||
|
||
if [ ! -d "build" ]; then | ||
mkdir build | ||
fi | ||
|
||
pushd . | ||
|
||
if [ ! -d "build" ]; then | ||
mkdir build | ||
fi | ||
|
||
rm -rf build/classes | ||
mkdir build/classes | ||
|
||
javac -sourcepath src/main/java/ src/main/java/ai/djl/huggingface/tokenizers/jni/TokenizersLibrary.java -h build/include -d build/classes | ||
javac -sourcepath src/main/java/ src/main/java/ai/djl/engine/rust/RustLibrary.java -h build/include -d build/classes | ||
|
||
cd rust/ | ||
cargo ndk -t $FLAVOR -o $WORK_DIR/build/jnilib --platform=21 build --release | ||
cd .. | ||
popd |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// see /~https://github.com/bbqsrc/cargo-ndk?tab=readme-ov-file#usage | ||
|
||
use std::{env, path::{Path, PathBuf}}; | ||
|
||
fn main() { | ||
if env::var("CARGO_CFG_TARGET_OS").unwrap() == "android" { | ||
android(); | ||
} | ||
} | ||
|
||
fn android() { | ||
println!("cargo:rustc-link-lib=c++_shared"); | ||
|
||
if let Ok(output_path) = env::var("CARGO_NDK_OUTPUT_PATH") { | ||
let sysroot_libs_path = | ||
PathBuf::from(env::var_os("CARGO_NDK_SYSROOT_LIBS_PATH").unwrap()); | ||
let lib_path = sysroot_libs_path.join("libc++_shared.so"); | ||
std::fs::copy( | ||
lib_path, | ||
Path::new(&output_path) | ||
.join(&env::var("CARGO_NDK_ANDROID_TARGET").unwrap()) | ||
.join("libc++_shared.so"), | ||
) | ||
.unwrap(); | ||
} | ||
} |
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