Skip to content

Commit

Permalink
build qwen-1.8b apk.
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzhaode committed Dec 6, 2023
1 parent 1dee908 commit 84a52f4
Show file tree
Hide file tree
Showing 18 changed files with 155 additions and 183 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ llm模型导出onnx模型请使用[llm-export](https://github.com/wangzhaode/llm

其他版本:
- Qwen-1_8B-Chat-int8:[![Download][download-qwen-1.8b-mnn-int8]][release-qwen-1.8b-mnn-int8]
- Android APK: [![Download][download-qwen-1.8b-apk]][release-qwen-1.8b-apk]

[download-chatglm-6b-onnx]: https://img.shields.io/github/downloads/wangzhaode/llm-export/chatglm-6b-onnx/total
[download-chatglm2-6b-onnx]: https://img.shields.io/github/downloads/wangzhaode/llm-export/chatglm2-6b-onnx/total
Expand All @@ -51,6 +52,8 @@ llm模型导出onnx模型请使用[llm-export](https://github.com/wangzhaode/llm
[download-llama2-7b-chat-mnn]: https://img.shields.io/github/downloads/wangzhaode/mnn-llm/llama2-7b-chat-mnn/total
[download-qwen-1.8b-mnn]: https://img.shields.io/github/downloads/wangzhaode/mnn-llm/qwen-1.8b-mnn/total
[download-qwen-1.8b-mnn-int8]: https://img.shields.io/github/downloads/wangzhaode/mnn-llm/qwen-1.8b-mnn-int8/total
[download-qwen-1.8b-apk]: https://img.shields.io/github/downloads/wangzhaode/mnn-llm/qwen-1.8b-apk/total

[release-chatglm-6b-mnn]: https://github.com/wangzhaode/mnn-llm/releases/tag/chatglm-6b-mnn
[release-chatglm2-6b-mnn]: https://github.com/wangzhaode/mnn-llm/releases/tag/chatglm2-6b-mnn
[release-chatglm3-6b-mnn]: https://github.com/wangzhaode/mnn-llm/releases/tag/chatglm3-6b-mnn
Expand All @@ -60,6 +63,8 @@ llm模型导出onnx模型请使用[llm-export](https://github.com/wangzhaode/llm
[release-llama2-7b-chat-mnn]: https://github.com/wangzhaode/mnn-llm/releases/tag/llama2-7b-chat-mnn
[release-qwen-1.8b-mnn]: https://github.com/wangzhaode/mnn-llm/releases/tag/qwen-1.8b-mnn
[release-qwen-1.8b-mnn-int8]: https://github.com/wangzhaode/mnn-llm/releases/tag/qwen-1.8b-mnn-int8
[release-qwen-1.8b-apk]: https://github.com/wangzhaode/mnn-llm/releases/tag/qwen-1.8b-apk


### 速度

Expand Down Expand Up @@ -157,12 +162,13 @@ adb shell "cd /data/local/tmp && export LD_LIBRARY_PATH=. && ./cli_demo -m model


## Reference
- [chatglm-6b](https://huggingface.co/THUDM/chatglm-6b)
- [chatglm2-6b](https://huggingface.co/THUDM/chatglm2-6b)
- [codegeex2-6b](https://huggingface.co/THUDM/codegeex2-6b)
- [Baichuan2-7B-Chat](https://huggingface.co/baichuan-inc/Baichuan2-7B-Chat)
- [Qwen-7B-Chat](https://huggingface.co/tangger/Qwen-7B-Chat)
- [chatglm-6b](https://modelscope.cn/models/ZhipuAI/chatglm-6b/summary)
- [chatglm2-6b](https://modelscope.cn/models/ZhipuAI/chatglm2-6b/summary)
- [chatglm3-6b](https://modelscope.cn/models/ZhipuAI/chatglm3-6b/summary)
- [codegeex2-6b](https://modelscope.cn/models/ZhipuAI/codegeex2-6b/summary)
- [Baichuan2-7B-Chat](https://modelscope.cn/models/baichuan-inc/baichuan-7B/summary)
- [Qwen-7B-Chat](https://modelscope.cn/models/qwen/Qwen-7B-Chat/summary)
- [Qwen-1.8B-Chat](https://modelscope.cn/models/qwen/Qwen-1_8B-Chat/summary)
- [cpp-httplib](https://github.com/yhirose/cpp-httplib)
- [chatgpt-web](https://github.com/xqdoo00o/chatgpt-web)
- [cppjieba](https://github.com/yanyiwu/cppjieba)
- [ChatViewDemo](https://github.com/BrettFX/ChatViewDemo)
2 changes: 1 addition & 1 deletion android/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion android/app/src/main/assets/tokenizer

This file was deleted.

9 changes: 7 additions & 2 deletions android/app/src/main/java/com/mnn/llm/Conversation.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Conversation extends BaseActivity {
private Button send;
private DateFormat mDateFormat;
private Chat mChat;
private boolean mHistory = false;
private boolean mHistory = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -138,12 +138,16 @@ public boolean onCreateOptionsMenu(Menu menu) {

@Override
public boolean onOptionsItemSelected(MenuItem item) {
/*
if (mHistory) {
Toast.makeText(getBaseContext(), "关闭上下文", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(), "打开上下文", Toast.LENGTH_SHORT).show();
}
mHistory = !mHistory;
*/
Toast.makeText(getBaseContext(), "清空记忆", Toast.LENGTH_SHORT).show();
mChat.Reset();
return true;
}
}
Expand All @@ -168,7 +172,7 @@ public void run() {
System.out.println("[MNN_DEBUG] start response\n");
while (!last_response.contains("<eop>")) {
try {
Thread.sleep(200);
Thread.sleep(50);
} catch (Exception e) {}
String response = new String(mChat.Response());
if (response.equals(last_response)) {
Expand All @@ -177,6 +181,7 @@ public void run() {
last_response = response;
}
Message msg = new Message();
System.out.println("[MNN_DEBUG] " + response);
msg.obj = response.replaceFirst("<eop>", "");
mHandler.sendMessage(msg);
}
Expand Down
6 changes: 1 addition & 5 deletions android/app/src/main/java/com/mnn/llm/DownloadModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

Expand All @@ -29,8 +26,7 @@ protected void onCreate(Bundle savedInstanceState) {
mDownloadAll = (Button)findViewById(R.id.download_all);
// init Data
String[] modelArray = this.getResources().getStringArray(R.array.model_list);
int[] modelSize = this.getResources().getIntArray(R.array.model_size);
mAdapter = new DownloadRecyclerView(this, modelArray, modelSize);
mAdapter = new DownloadRecyclerView(this, modelArray);
mRecyclerView.setAdapter(mAdapter);
}
public void downloadAll(View view) {
Expand Down
56 changes: 29 additions & 27 deletions android/app/src/main/java/com/mnn/llm/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.io.File;
import java.io.IOException;


public class MainActivity extends AppCompatActivity {
private Chat mChat;
Expand All @@ -28,8 +28,9 @@ public class MainActivity extends AppCompatActivity {
private TextView mProcessName;
private TextView mProcessPercent;
// resource files
private String mModelDir = "/data/local/tmp/model";
private boolean mModelNeedDownload = true;
private String mModelName = "qwen-1.8b-int4";
private String mModelDir = "/data/local/tmp/chat/" + mModelName; // default dir
private boolean mModelReady = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -41,7 +42,8 @@ protected void onCreate(Bundle savedInstanceState) {
mProcessBar = (ProgressBar)findViewById(R.id.process_bar);
mProcessName = (TextView)findViewById(R.id.process_name);
mProcessPercent = (TextView)findViewById(R.id.process_percent);
mModelDir = this.getCacheDir().toString() + "/model";
// using assert file
mModelDir = this.getCacheDir() + "/" + mModelName;
mProcessHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Expand All @@ -58,55 +60,56 @@ public void handleMessage(Message msg) {
}
}
};
/*
File model = new File(mModelDir, "glm_block_0.mnn");
if (model.exists()) {
model.delete();
}
*/
onCheckModels();
}

@Override
protected void onResume() {
super.onResume();
onCheckModels();
}

public void onCheckModels() {
mModelNeedDownload = checkModelsNeedDownload();
if (mModelNeedDownload) {
mModelReady = checkModelsReady();
// try copy from assert file
if (!mModelReady) {
try {
mModelDir = Common.copyAssetResource2File(this, mModelName);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
mModelReady = checkModelsReady();
}
// download models
if (!mModelReady) {
mModelInfo.setVisibility(View.VISIBLE);
mModelInfo.setText("使用前请先下载模型!");
mModelInfo.setText("请下载模型文件");
mLoadButton.setText("下载模型");
} else {
mModelInfo.setVisibility(View.VISIBLE);
mModelInfo.setText("模型下载完毕,请加载模型!");
mModelInfo.setText(mModelName + "模型文件就绪,模型加载中");
mLoadButton.setText("加载模型");
}
}
public boolean checkModelsNeedDownload() {
public boolean checkModelsReady() {
System.out.println("### Check Models!");
File dir = new File(mModelDir);
if (!dir.exists()) {
return true;
return false;
}
String[] modelArray = this.getResources().getStringArray(R.array.model_list);
int[] modelSize = this.getResources().getIntArray(R.array.model_size);
for (int i = 0; i < modelArray.length; i++) {
File model = new File(mModelDir, modelArray[i]);
if (!model.exists()) {
return true;
}
if (model.length() != modelSize[i]) {
return true;
return false;
}
}
return false;
return true;
}

public void loadModel(View view) {
if (mModelNeedDownload) {
onCheckModels();
if (!mModelReady) {
startActivity(new Intent(this, DownloadModel.class));
return;
}
Expand All @@ -115,7 +118,6 @@ public void loadModel(View view) {
mLoadButton.setText("模型加载中 ...");
mProcessView.setVisibility(View.VISIBLE);
mChat = new Chat();
System.out.println("[MNN_DEBUG] is chat Ready: " + mChat.Ready());
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class DownloadRecyclerView extends RecyclerView.Adapter<RecyclerView.View
private Handler mHandler;
private Button mButton;

public DownloadRecyclerView(Context context, String[] models, int[] modelSize) {
public DownloadRecyclerView(Context context, String[] models) {
this.mContext = context;
this.mItems = new ArrayList<DownloadData>();
final String modelDir = context.getCacheDir().toString() + "/model";
Expand All @@ -48,7 +48,7 @@ public void handleMessage(Message msg) {
}
};
for (int i = 0; i < models.length; i++) {
this.mItems.add(new DownloadData(mHandler, modelDir, models[i], i, modelSize[i]));
this.mItems.add(new DownloadData(mHandler, modelDir, models[i], i, 25751300));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ private void configureViewHolder3(HolderMe vh1, int position) {
}

private void configureViewHolder2(HolderYou vh1, int position) {
vh1.getTime().setText(items.get(position).getTime());
vh1.getChatText().setText(items.get(position).getText());
}
private void configureViewHolder1(HolderDate vh1, int position) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,13 @@

public class HolderYou extends RecyclerView.ViewHolder {

private TextView time, chatText;
private TextView chatText;

public HolderYou(View v) {
super(v);
time = (TextView) v.findViewById(R.id.tv_time);
chatText = (TextView) v.findViewById(R.id.tv_chat_text);
}

public TextView getTime() {
return time;
}

public void setTime(TextView time) {
this.time = time;
}

public TextView getChatText() {
return chatText;
}
Expand Down
1 change: 1 addition & 0 deletions android/app/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ cmake_minimum_required(VERSION 3.10)
include_directories(${CMAKE_CURRENT_LIST_DIR}/../../../../../include/)
link_directories(${CMAKE_CURRENT_LIST_DIR}/libs/arm64-v8a)

add_definitions(-DUSING_DISK_EMBED)
FILE(GLOB SRCS ../../../../../src/*.cpp)
add_library(llm_mnn SHARED llm_mnn_jni.cpp ${SRCS})

Expand Down
15 changes: 8 additions & 7 deletions android/app/src/main/jni/llm_mnn_jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include "llm.hpp"

static Llm* llm;
static std::unique_ptr<Llm> llm(nullptr);
static std::stringstream response_buffer;

extern "C" {
Expand All @@ -25,32 +25,33 @@ JNIEXPORT void JNI_OnUnload(JavaVM* vm, void* reserved) {
}

JNIEXPORT jboolean JNICALL Java_com_mnn_llm_Chat_Init(JNIEnv* env, jobject thiz, jstring modelDir) {
if (llm->load_progress() < 100) {
const char* model_dir = env->GetStringUTFChars(modelDir, 0);
llm = Llm::createLLM(model_dir);
const char* model_dir = env->GetStringUTFChars(modelDir, 0);
if (!llm.get()) {
llm.reset(Llm::createLLM(model_dir));
llm->load(model_dir);
}
return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL Java_com_mnn_llm_Chat_Ready(JNIEnv* env, jobject thiz) {
if (llm->load_progress() >= 100) {
if (llm.get() && llm->load_progress() >= 100) {
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());
}

JNIEXPORT jstring JNICALL Java_com_mnn_llm_Chat_Submit(JNIEnv* env, jobject thiz, jstring inputStr) {
if (llm->load_progress() < 100) {
if (!llm.get()) {
return env->NewStringUTF("Failed, Chat is not ready!");
}
const char* input_str = env->GetStringUTFChars(inputStr, 0);
auto chat = [&](std::string str) {
llm->response(str, &response_buffer);
llm->response(str, &response_buffer, "<eop>");
};
std::thread chat_thread(chat, input_str);
chat_thread.detach();
Expand Down
2 changes: 1 addition & 1 deletion android/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="8pt"
android:text="ChatGLM-MNN" />
android:text="mnn-llm" />
</LinearLayout>

<LinearLayout
Expand Down
16 changes: 1 addition & 15 deletions android/app/src/main/res/layout/layout_holder_you.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
android:gravity="center_vertical">
android:orientation="horizontal">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
Expand All @@ -19,7 +18,6 @@
<TextView
android:id="@+id/tv_chat_text"
android:layout_toLeftOf="@id/tv_time"
android:maxWidth="@dimen/maxwidth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/space10"
Expand All @@ -28,17 +26,5 @@
android:textSize="@dimen/h3"
android:textAppearance="@style/Light"
android:background="@drawable/bg_chat_you"/>
<TextView
android:id="@+id/tv_time"
android:layout_marginLeft="@dimen/space10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="@dimen/space5"
android:drawableLeft="@drawable/ic_clock"
android:text="5:04pm"
android:textColor="@color/colorTextInfo"
android:textSize="@dimen/h4"
android:textAppearance="@style/ExtraLight"
android:layout_centerVertical="true"/>
</LinearLayout>
</RelativeLayout>
Loading

0 comments on commit 84a52f4

Please sign in to comment.