Skip to content

Commit

Permalink
[Android][docs][iOS] Add model lib check and update docs (#675)
Browse files Browse the repository at this point in the history
This PR add a checker for both Android and iOS app, to make sure that the added model have supported model libs. Otherwise, the app will pop the information.

Meanwhile, this PR updates the doc for Android ADB, due to the outdated model path.
  • Loading branch information
cyx-6 authored Aug 6, 2023
1 parent bdeab17 commit f4587df
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
5 changes: 0 additions & 5 deletions android/MLCChat/app/src/main/assets/app-config.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
{
"model_libs": [
"vicuna-v1-7b-q4f16_1",
"RedPajama-INCITE-Chat-3B-v1-q4f16_0"
],
"model_list": [
{
"model_url": "https://huggingface.co/mlc-ai/demo-vicuna-v1-7b-int4/",
"local_id": "vicuna-v1-7b-q4f16_1"
},
{
"model_url": "https://huggingface.co/mlc-ai/mlc-chat-RedPajama-INCITE-Chat-3B-v1-q4f16_0/",
"local_id": "RedPajama-INCITE-Chat-3B-v1-q4f16_0"
Expand Down
24 changes: 22 additions & 2 deletions android/MLCChat/app/src/main/java/ai/mlc/mlcchat/AppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
val modelConfig = gson.fromJson(modelConfigString, ModelConfig::class.java)
addModelConfig(modelConfig, modelRecord.modelUrl, true)
} else {
downloadModelConfig(modelRecord.modelUrl, modelRecord.localId, true)
downloadModelConfig(
if (modelRecord.modelUrl.endsWith("/")) modelRecord.modelUrl else "$modelRecord.modelUrl/",
modelRecord.localId,
true
)
}
}
modelSampleList += appConfig.modelSamples
Expand Down Expand Up @@ -126,6 +130,18 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
}
}

private fun isModelConfigAllowed(modelConfig: ModelConfig): Boolean {
if (appConfig.modelLibs.contains(modelConfig.modelLib)) return true;
viewModelScope.launch {
Toast.makeText(
application,
"Model lib ${modelConfig.modelLib} is not supported.",
Toast.LENGTH_SHORT
).show()
}
return false
}


private fun downloadModelConfig(modelUrl: String, localId: String?, isBuiltin: Boolean) {
thread(start = true) {
Expand Down Expand Up @@ -160,6 +176,10 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
).show()
return@launch
}
if (!isModelConfigAllowed(modelConfig)) {
tempFile.delete()
return@launch
}
val modelDirFile = File(appDirFile, modelConfig.localId)
val modelConfigFile = File(modelDirFile, ModelConfigFilename)
tempFile.copyTo(modelConfigFile, overwrite = true)
Expand All @@ -180,7 +200,7 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
viewModelScope.launch {
Toast.makeText(
application,
"Add model failed: ${e.localizedMessage}",
"Download model config failed: ${e.localizedMessage}",
Toast.LENGTH_SHORT
).show()
}
Expand Down
9 changes: 4 additions & 5 deletions docs/deploy/android.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,14 @@ weights you build, please follow the steps below.
please uninstall the existing app and try ``adb install`` again.

- Step 13. Push the tokenizer and model weights to your phone through
- Step 13. Push the model dir to your phone through
ADB.

.. code:: bash
adb push dist/models/vicuna-v1-7b/tokenizer.model /data/local/tmp/vicuna-v1-7b/tokenizer.model
adb push dist/vicuna-v1-7b/float16/params /data/local/tmp/vicuna-v1-7b/params
adb shell "mkdir -p /storage/emulated/0/Android/data/ai.mlc.mlcchat/files/Download/"
adb shell "mv /data/local/tmp/vicuna-v1-7b /storage/emulated/0/Android/data/ai.mlc.mlcchat/files/Download/vicuna-v1-7b"
adb push dist/models/vicuna-v1-7b/ /data/local/tmp/vicuna-v1-7b/
adb shell "mkdir -p /storage/emulated/0/Android/data/ai.mlc.mlcchat/files/"
adb shell "mv /data/local/tmp/vicuna-v1-7b /storage/emulated/0/Android/data/ai.mlc.mlcchat/files/vicuna-v1-7b"
- Step 14. Everything is ready. Launch the MLCChat on your phone and
you will be able to use the app with your own weights. You will find
Expand Down
16 changes: 14 additions & 2 deletions ios/MLCChat/StartState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class StartState : ObservableObject {
static let ParamsConfigFileName = "ndarray-cache.json"

init() {
loadPrebuiltModels()
loadAppConfig()
loadPrebuiltModels()
}

private func loadPrebuiltModels() {
Expand Down Expand Up @@ -101,7 +101,11 @@ class StartState : ObservableObject {
assert(fileManager.fileExists(atPath: modelConfigUrl.path()))
let fileHandle = try FileHandle(forReadingFrom: modelConfigUrl)
let data = fileHandle.readDataToEndOfFile()
return try decoder.decode(ModelConfig.self, from: data)
let modelConfig = try decoder.decode(ModelConfig.self, from: data)
if !isModelConfigAllowed(modelConfig: modelConfig) {
return nil
}
return modelConfig
} catch {
showAlert(message: "Failed to resolve model config: \(error.localizedDescription)")
}
Expand Down Expand Up @@ -139,6 +143,14 @@ class StartState : ObservableObject {
}
}

private func isModelConfigAllowed(modelConfig: ModelConfig) -> Bool {
if appConfig.model_libs.contains(modelConfig.model_lib) {
return true
}
showAlert(message: "Model lib \(modelConfig.model_lib) is not supported")
return false
}


private func downloadConfig(modelUrl: URL, localId: String?, isBuiltin: Bool) {
let downloadTask = URLSession.shared.downloadTask(with: modelUrl.appending(path: "resolve").appending(path: "main").appending(path: StartState.ModelConfigFileName)) {
Expand Down

0 comments on commit f4587df

Please sign in to comment.