|
| 1 | +## MiniCPM-Llama3-V 2.5 |
| 2 | + |
| 3 | +### Prepare models and code |
| 4 | + |
| 5 | +Download [MiniCPM-Llama3-V-2_5](https://huggingface.co/openbmb/MiniCPM-Llama3-V-2_5) PyTorch model from huggingface to "MiniCPM-Llama3-V-2_5" folder. |
| 6 | + |
| 7 | +Clone llama.cpp: |
| 8 | +```bash |
| 9 | +git clone https://github.com/ggerganov/llama.cpp |
| 10 | +cd llama.cpp |
| 11 | +``` |
| 12 | + |
| 13 | +### Usage |
| 14 | + |
| 15 | +Convert PyTorch model to gguf files (You can also download the converted [gguf](https://huggingface.co/openbmb/MiniCPM-Llama3-V-2_5-gguf) by us) |
| 16 | + |
| 17 | +```bash |
| 18 | +python ./examples/minicpmv/minicpmv-surgery.py -m ../MiniCPM-Llama3-V-2_5 |
| 19 | +python ./examples/minicpmv/minicpmv-convert-image-encoder-to-gguf.py -m ../MiniCPM-Llama3-V-2_5 --minicpmv-projector ../MiniCPM-Llama3-V-2_5/minicpmv.projector --output-dir ../MiniCPM-Llama3-V-2_5/ --image-mean 0.5 0.5 0.5 --image-std 0.5 0.5 0.5 |
| 20 | +python ./convert-hf-to-gguf.py ../MiniCPM-Llama3-V-2_5/model |
| 21 | + |
| 22 | +# quantize int4 version |
| 23 | +./llama-quantize ../MiniCPM-Llama3-V-2_5/model/model-8B-F16.gguf ../MiniCPM-Llama3-V-2_5/model/ggml-model-Q4_K_M.gguf Q4_K_M |
| 24 | +``` |
| 25 | + |
| 26 | +Build for Linux or Mac |
| 27 | + |
| 28 | +```bash |
| 29 | +make |
| 30 | +make llama-minicpmv-cli |
| 31 | +``` |
| 32 | + |
| 33 | +Inference on Linux or Mac |
| 34 | +``` |
| 35 | +# run f16 version |
| 36 | +./llama-minicpmv-cli -m ../MiniCPM-Llama3-V-2_5/model/model-8B-F16.gguf --mmproj ../MiniCPM-Llama3-V-2_5/mmproj-model-f16.gguf -c 4096 --temp 0.7 --top-p 0.8 --top-k 100 --repeat-penalty 1.05 --image xx.jpg -p "What is in the image?" |
| 37 | +
|
| 38 | +# run quantized int4 version |
| 39 | +./llama-minicpmv-cli -m ../MiniCPM-Llama3-V-2_5/model/ggml-model-Q4_K_M.gguf --mmproj ../MiniCPM-Llama3-V-2_5/mmproj-model-f16.gguf -c 4096 --temp 0.7 --top-p 0.8 --top-k 100 --repeat-penalty 1.05 --image xx.jpg -p "What is in the image?" |
| 40 | +
|
| 41 | +# or run in interactive mode |
| 42 | +./llama-minicpmv-cli -m ../MiniCPM-Llama3-V-2_5/model/ggml-model-Q4_K_M.gguf --mmproj ../MiniCPM-Llama3-V-2_5/mmproj-model-f16.gguf -c 4096 --temp 0.7 --top-p 0.8 --top-k 100 --repeat-penalty 1.05 --image xx.jpg -i |
| 43 | +``` |
| 44 | + |
| 45 | +### Android |
| 46 | + |
| 47 | +#### Build on Android device using Termux |
| 48 | +We found that build on Android device would bring better runtime performance, so we recommend to build on device. |
| 49 | + |
| 50 | +[Termux](https://github.com/termux/termux-app#installation) is a terminal app on Android device (no root required). |
| 51 | + |
| 52 | +Install tools in Termux: |
| 53 | +``` |
| 54 | +apt update && apt upgrade -y |
| 55 | +apt install git make cmake |
| 56 | +``` |
| 57 | + |
| 58 | +It's recommended to move your model inside the `~/` directory for best performance: |
| 59 | +``` |
| 60 | +cd storage/downloads |
| 61 | +mv model.gguf ~/ |
| 62 | +``` |
| 63 | + |
| 64 | +#### Building the Project using Android NDK |
| 65 | +Obtain the [Android NDK](https://developer.android.com/ndk) and then build with CMake. |
| 66 | + |
| 67 | +Execute the following commands on your computer to avoid downloading the NDK to your mobile. Alternatively, you can also do this in Termux: |
| 68 | + |
| 69 | +```bash |
| 70 | +mkdir build-android |
| 71 | +cd build-android |
| 72 | +export NDK=/your_ndk_path |
| 73 | +cmake -DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=android-23 -DCMAKE_C_FLAGS=-march=armv8.4a+dotprod .. |
| 74 | +make |
| 75 | +``` |
| 76 | + |
| 77 | +Install [termux](https://github.com/termux/termux-app#installation) on your device and run `termux-setup-storage` to get access to your SD card (if Android 11+ then run the command twice). |
| 78 | + |
| 79 | +Finally, copy these built `llama` binaries and the model file to your device storage. Because the file permissions in the Android sdcard cannot be changed, you can copy the executable files to the `/data/data/com.termux/files/home/bin` path, and then execute the following commands in Termux to add executable permission: |
| 80 | + |
| 81 | +(Assumed that you have pushed the built executable files to the /sdcard/llama.cpp/bin path using `adb push`) |
| 82 | +``` |
| 83 | +$cp -r /sdcard/llama.cpp/bin /data/data/com.termux/files/home/ |
| 84 | +$cd /data/data/com.termux/files/home/bin |
| 85 | +$chmod +x ./* |
| 86 | +``` |
| 87 | + |
| 88 | +Download models and push them to `/sdcard/llama.cpp/`, then move it to `/data/data/com.termux/files/home/model/` |
| 89 | + |
| 90 | +``` |
| 91 | +$mv /sdcard/llama.cpp/ggml-model-Q4_K_M.gguf /data/data/com.termux/files/home/model/ |
| 92 | +$mv /sdcard/llama.cpp/mmproj-model-f16.gguf /data/data/com.termux/files/home/model/ |
| 93 | +``` |
| 94 | + |
| 95 | +Now, you can start chatting: |
| 96 | +``` |
| 97 | +$cd /data/data/com.termux/files/home/bin |
| 98 | +$./llama-minicpmv-cli -m ../model/ggml-model-Q4_K_M.gguf --mmproj ../model/mmproj-model-f16.gguf -c 4096 --temp 0.7 --top-p 0.8 --top-k 100 --repeat-penalty 1.05 --image xx.jpg -p "What is in the image?" |
| 99 | +``` |
0 commit comments