Skip to content

Commit

Permalink
[refactor] update android project.
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzhaode committed Jun 17, 2024
1 parent 7af455d commit a5f0963
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 93 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: 'recursive'
- name: build
run: ./script/android_build.sh

android_app_build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: 'recursive'
- name: build
run: ./script/android_app_build.sh

12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.5)
project(mnn-llm)

option(BUILD_FOR_ANDROID "Build for android whith mini memory mode." OFF)
option(USING_VISUAL_MODEL "Using visual model will need dpes: MNNOpenCV and httplib." OFF)
option(DUMP_PROFILE_INFO "Dump profile info when chat." OFF)
option(BUILD_JNI "Build JNI for android app." OFF)

if (USING_VISUAL_MODEL)
add_definitions(-DUSING_VISUAL_MODEL)
Expand Down Expand Up @@ -38,6 +39,11 @@ include_directories(${CMAKE_CURRENT_LIST_DIR}/include/
# source files
FILE(GLOB SRCS ${CMAKE_CURRENT_LIST_DIR}/src/*.cpp)

# jni file
if (BUILD_JNI)
list(APPEND SRCS "${CMAKE_CURRENT_LIST_DIR}/android/app/src/main/jni/llm_mnn_jni.cpp")
endif()

if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/source-charset:utf-8>")
Expand All @@ -57,6 +63,8 @@ else()
endif()
endif()

if (NOT (BUILD_JNI))

add_executable(cli_demo ${CMAKE_CURRENT_LIST_DIR}/demo/cli_demo.cpp)
add_executable(tokenizer_demo ${CMAKE_CURRENT_LIST_DIR}/demo/tokenizer_demo.cpp)
add_executable(embedding_demo ${CMAKE_CURRENT_LIST_DIR}/demo/embedding_demo.cpp)
Expand Down Expand Up @@ -110,3 +118,5 @@ else()
target_link_libraries(web_demo llm pthread)
endif()
endif()

endif()
10 changes: 6 additions & 4 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk {
abiFilters 'arm64-v8a'
externalNativeBuild {
cmake {
arguments "-DBUILD_JNI=TRUE", "-DANDROID_STL=c++_shared"
abiFilters 'arm64-v8a'
}
}
}
buildTypes {
Expand All @@ -22,8 +25,7 @@ android {
}
externalNativeBuild {
cmake {
version "3.10.2"
path file('src/main/jni/CMakeLists.txt')
path file('../../CMakeLists.txt')
}
}
sourceSets {
Expand Down
2 changes: 1 addition & 1 deletion android/app/src/main/java/com/mnn/llm/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public class Chat implements Serializable {
public native void Reset();

static {
System.loadLibrary("llm_mnn");
System.loadLibrary("llm");
}
}
3 changes: 2 additions & 1 deletion android/app/src/main/java/com/mnn/llm/DownloadModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ protected void onCreate(Bundle savedInstanceState) {
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mDownloadAll = (Button)findViewById(R.id.download_all);
// init Data
String[] modelArray = this.getResources().getStringArray(R.array.model_list);
// String[] modelArray = this.getResources().getStringArray(R.array.model_list);
String[] modelArray = {"config.json"};
mAdapter = new DownloadRecyclerView(this, modelArray);
mRecyclerView.setAdapter(mAdapter);
}
Expand Down
9 changes: 1 addition & 8 deletions android/app/src/main/java/com/mnn/llm/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
mModelName = (String) parent.getItemAtPosition(position);
mModelInfo.setText("选择模型:" + mModelName);
mModelInfo.setVisibility(View.VISIBLE);
mModelDir = mSearchPath + mModelName;
mModelDir = mSearchPath + mModelName + "/config.json";
}
}
@Override
Expand Down Expand Up @@ -120,13 +120,6 @@ public boolean checkModelsReady() {
if (!dir.exists()) {
return false;
}
String[] modelArray = this.getResources().getStringArray(R.array.model_list);
for (int i = 0; i < modelArray.length; i++) {
File model = new File(mModelDir, modelArray[i]);
if (!model.exists()) {
return false;
}
}
return true;
}

Expand Down
14 changes: 0 additions & 14 deletions android/app/src/main/jni/CMakeLists.txt

This file was deleted.

Empty file.
15 changes: 4 additions & 11 deletions android/app/src/main/jni/llm_mnn_jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ JNIEXPORT jboolean JNICALL Java_com_mnn_llm_Chat_Init(JNIEnv* env, jobject thiz,
const char* model_dir = env->GetStringUTFChars(modelDir, 0);
if (!llm.get()) {
llm.reset(Llm::createLLM(model_dir));
llm->load(model_dir);
llm->load();
}
return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL Java_com_mnn_llm_Chat_Ready(JNIEnv* env, jobject thiz) {
if (llm.get() && llm->load_progress() >= 100) {
if (llm.get()) {
return JNI_TRUE;
}
return JNI_FALSE;
}

JNIEXPORT jfloat JNICALL Java_com_mnn_llm_Chat_Progress(JNIEnv* env, jobject thiz) {
if (!llm.get()) return jfloat(0);
return jfloat(llm->load_progress());
return jfloat(llm->load_progress_);
}

JNIEXPORT jstring JNICALL Java_com_mnn_llm_Chat_Submit(JNIEnv* env, jobject thiz, jstring inputStr) {
Expand All @@ -59,13 +59,6 @@ JNIEXPORT jstring JNICALL Java_com_mnn_llm_Chat_Submit(JNIEnv* env, jobject thiz
return result;
}

/*
JNIEXPORT jstring JNICALL Java_com_mnn_chatglm_Chat_Response(JNIEnv* env, jobject thiz) {
jstring result = env->NewStringUTF(response_buffer.str().c_str());
return result;
}
*/

JNIEXPORT jbyteArray JNICALL Java_com_mnn_llm_Chat_Response(JNIEnv* env, jobject thiz) {
auto len = response_buffer.str().size();
jbyteArray res = env->NewByteArray(len);
Expand All @@ -78,7 +71,7 @@ JNIEXPORT void JNICALL Java_com_mnn_llm_Chat_Done(JNIEnv* env, jobject thiz) {
}

JNIEXPORT void JNICALL Java_com_mnn_llm_Chat_Reset(JNIEnv* env, jobject thiz) {
llm->reset();
// llm->reset();
}

} // extern "C"
32 changes: 0 additions & 32 deletions android/app/src/main/res/values/models.xml

This file was deleted.

8 changes: 4 additions & 4 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ buildscript {
repositories {
google()
jcenter()

}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath "com.android.tools.build:gradle:7.1.3"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand All @@ -18,7 +18,7 @@ allprojects {
repositories {
google()
jcenter()

}
}

Expand Down
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
3 changes: 2 additions & 1 deletion include/llm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ class Llm {
// time
int64_t prefill_us_ = 0;
int64_t decode_us_ = 0;
float load_progress_ = 0.f;
bool is_single_ = true;
bool is_disk_embedding_ = true;
std::shared_ptr<LlmConfig> config_;
Expand Down Expand Up @@ -276,7 +277,7 @@ class Lvlm : public Llm {
img_pad_ = config->llm_config_.value("img_pad", img_pad_);
}
~Lvlm() { visual_module_.reset(); }
virtual void load();
virtual void load() override;
private:
int img_size_ = 448, imgpad_len_ = 256, img_start_ = 151857, img_end_ = 151858, img_pad_ = 151859;
std::shared_ptr<Module> visual_module_;
Expand Down
16 changes: 1 addition & 15 deletions script/android_app_build.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# 1. clone MNN
git clone https://github.com/alibaba/MNN.git --depth=1

# 2. build MNN
cd MNN/project/android
mkdir build
cd build
../build_64.sh
cd ../../../..

# 3. copy headers and libs
cp -r MNN/include/MNN include
cp MNN/project/android/build/libMNN.so MNN/project/android/build/libMNN_Express.so android/app/src/main/jni/libs/arm64-v8a

# 4. build mnn-llm android apk
# 1. build mnn-llm android apk
cd android
./gradlew assembleDebug
1 change: 1 addition & 0 deletions src/llm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ void Llm::load() {
MNN_PRINT("Done!\n");
}
}
load_progress_ = 100.f;
}

VARP Llm::forward(const std::vector<int>& input_ids) {
Expand Down

0 comments on commit a5f0963

Please sign in to comment.