I have modified and crafted some code to run LLM in adb shell or linux shell via MLC-LLM (btw. great appreciate to authors and contributors) as a binary executable program.
I'm not an expert in C++, so the code isn't perfect(actually it is tinkered and glued outputs of ChatGPT, Claude and my dog), but I think it's easy to read, understand and run.
How to setup:
setup MLC-LLM and virtualenv (install dependencies, TVM, etc. etc.)
Create build directory, e.g. build-aarch64-opencl. Run all following commands from this dir.
Build: make -j 8. Now you should have libmlc_llm_module.so, tvm/libtvm.so and llm_benchmark.
Do normal steps which you do for building mlc llm model, e.g. (I have used GPT-2, because its unmodified version match RAM of my phone. You may need to modify paths.):
5. Build model library (I was getting error about missing compiler without $CC; I think android version don't matter that much; You may want to somehow modify predefined device - this is story for new books):
```sh
CC=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android31-clang++ \
TVM_NDK_CC=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android31-clang++ \
mlc_llm compile ./gpt2-medium-q0f16/mlc-chat-config.json \
--device android:adreno-so \
--host aarch64-linux-android \
-o gpt2-medium-q0f16-opencl-aarch64.so
Let's rock. Inside adb shell run following commands:
cd /data/local/tmp/mlc/gpt2-medium-aarch64-opencl/
LD_LIBRARY_PATH=. ./llm_benchmark \
./gpt2-medium-q0f16 \
./gpt2-medium-q0f16-opencl-aarch64.so \
"local" 4 60 250 \
"Give me short answer who you are?" 3
Arguments: 1st - folder with weights; 2nd - file with model library; 3rd - execution mode (server or interactive as alternatives); 4th 4 - means OpenCL (alternatives described in sourcecode); 5th - timeout in seconds of executation; 6th - max tokens; 7th - prompt; 8th - number of executions (in case of 1, it will print generated text).
If you would like to run on local computer, you should remove in cmake directives for cross compilation. And modify elements to suits your setup.
I'm afraid this cannot be merged, because it modifies some important files, like openai_format.cc.
Hello,
I have modified and crafted some code to run LLM in adb shell or linux shell via MLC-LLM (btw. great appreciate to authors and contributors) as a binary executable program.
I'm not an expert in C++, so the code isn't perfect(actually it is tinkered and glued outputs of ChatGPT, Claude and my dog), but I think it's easy to read, understand and run.
How to setup:
build-aarch64-opencl
. Run all following commands from this dir.make -j 8
. Now you should havelibmlc_llm_module.so
,tvm/libtvm.so
andllm_benchmark
.mlc_llm gen_config \ --quantization q0f16 \ --max-batch-size 1 \ --conv-template gpt2 \ -o ./gpt2-medium-q0f16 \ gpt2-medium/
adb shell
run following commands:4
- means OpenCL (alternatives described in sourcecode); 5th - timeout in seconds of executation; 6th - max tokens; 7th - prompt; 8th - number of executions (in case of 1, it will print generated text).