Skip to content

Commit 99874e5

Browse files
committed
support minicpmv
1 parent 5cf5e7d commit 99874e5

18 files changed

+3754
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ models-mnt
5959
/libllama.so
6060
/llama-bench
6161
/llava-cli
62+
/minicpmv-cli
6263
/lookahead
6364
/lookup
6465
/lookup-create

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Define the default target now so that it is always the first target
22
BUILD_TARGETS = \
33
main quantize quantize-stats perplexity imatrix embedding vdot q8dot train-text-from-scratch convert-llama2c-to-ggml \
4-
simple batched batched-bench save-load-state server gguf gguf-split eval-callback llama-bench libllava.a llava-cli baby-llama beam-search \
4+
simple batched batched-bench save-load-state server gguf gguf-split eval-callback llama-bench libllava.a llava-cli minicpmv-cli baby-llama beam-search \
55
retrieval speculative infill tokenize benchmark-matmult parallel finetune export-lora lookahead lookup passkey gritlm tests/test-c.o
66

77
# Binaries only useful for tests
@@ -846,6 +846,12 @@ llava-cli: examples/llava/llava-cli.cpp examples/llava/clip.h examples/llava/cli
846846
$(CXX) $(CXXFLAGS) -c examples/llava/llava.cpp -o $(call GET_OBJ_FILE, examples/llava/llava.cpp)
847847
$(CXX) $(CXXFLAGS) $(filter-out %.h $< examples/llava/clip.cpp examples/llava/llava.cpp,$^) $(call GET_OBJ_FILE, $<) $(call GET_OBJ_FILE, examples/llava/clip.cpp) $(call GET_OBJ_FILE, examples/llava/llava.cpp) -o $@ $(LDFLAGS)
848848

849+
minicpmv-cli: examples/minicpmv/minicpmv-cli.cpp examples/minicpmv/clip.h examples/minicpmv/clip.cpp examples/minicpmv/minicpmv.h examples/minicpmv/minicpmv.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
850+
$(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
851+
$(CXX) $(CXXFLAGS) -c examples/minicpmv/clip.cpp -o $(call GET_OBJ_FILE, examples/minicpmv/clip.cpp) -Wno-cast-qual
852+
$(CXX) $(CXXFLAGS) -c examples/minicpmv/minicpmv.cpp -o $(call GET_OBJ_FILE, examples/minicpmv/minicpmv.cpp)
853+
$(CXX) $(CXXFLAGS) $(filter-out %.h $< examples/minicpmv/clip.cpp examples/minicpmv/minicpmv.cpp,$^) $(call GET_OBJ_FILE, $<) $(call GET_OBJ_FILE, examples/minicpmv/clip.cpp) $(call GET_OBJ_FILE, examples/minicpmv/minicpmv.cpp) -o $@ $(LDFLAGS)
854+
849855
baby-llama: examples/baby-llama/baby-llama.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
850856
$(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
851857
$(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)

convert-hf-to-gguf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,8 @@ def set_gguf_parameters(self):
16381638
self.gguf_writer.add_head_count_kv(self.hparams["num_key_value_heads"])
16391639
self.gguf_writer.add_layer_norm_rms_eps(self.hparams["rms_norm_eps"])
16401640
self.gguf_writer.add_file_type(self.ftype)
1641+
if "tie_word_embeddings" in self.hparams:
1642+
self.gguf_writer.add_tie_lm_head(self.hparams["tie_word_embeddings"])
16411643

16421644
def set_vocab(self):
16431645
self._set_vocab_llama_hf()

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ else()
2626
add_subdirectory(infill)
2727
add_subdirectory(llama-bench)
2828
add_subdirectory(llava)
29+
add_subdirectory(minicpmv)
2930
if (LLAMA_SYCL)
3031
add_subdirectory(sycl)
3132
endif()

examples/minicpmv/CMakeLists.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
add_library(minicpmv OBJECT
2+
minicpmv.cpp
3+
minicpmv.h
4+
clip.cpp
5+
clip.h
6+
)
7+
8+
target_link_libraries(minicpmv PRIVATE ggml llama ${CMAKE_THREAD_LIBS_INIT})
9+
10+
target_include_directories(minicpmv PUBLIC .)
11+
target_include_directories(minicpmv PUBLIC ../..)
12+
target_include_directories(minicpmv PUBLIC ../../common)
13+
14+
target_compile_features(minicpmv PRIVATE cxx_std_11)
15+
16+
add_library(minicpmv_static STATIC $<TARGET_OBJECTS:minicpmv>)
17+
if (BUILD_SHARED_LIBS)
18+
set_target_properties(minicpmv PROPERTIES POSITION_INDEPENDENT_CODE ON)
19+
target_compile_definitions(minicpmv PRIVATE LLAMA_SHARED LLAMA_BUILD)
20+
add_library(minicpmv_shared SHARED $<TARGET_OBJECTS:minicpmv>)
21+
target_link_libraries(minicpmv_shared PRIVATE ggml llama ${CMAKE_THREAD_LIBS_INIT})
22+
install(TARGETS minicpmv_shared LIBRARY)
23+
endif()
24+
25+
if (NOT MSVC)
26+
target_compile_options(minicpmv PRIVATE -Wno-cast-qual) # stb_image.h
27+
endif()
28+
29+
if(TARGET BUILD_INFO)
30+
add_dependencies(minicpmv BUILD_INFO)
31+
endif()
32+
33+
set(TARGET minicpmv-cli)
34+
add_executable(minicpmv-cli minicpmv-cli.cpp)
35+
install(TARGETS minicpmv-cli RUNTIME)
36+
target_link_libraries(minicpmv-cli PRIVATE common minicpmv ${CMAKE_THREAD_LIBS_INIT})
37+
target_compile_features(minicpmv PRIVATE cxx_std_11)

examples/minicpmv/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 所有命令在 llama.cpp 根目录执行,模型位于根目录上级目录处
2+
# All command should be executed under the root path of llama.cpp repo. We assume the MiniCPM-V-2 model are put in its parent folder.
3+
4+
```bash
5+
make
6+
make minicpmv-cli
7+
8+
python ./examples/minicpmv/minicpm-surgery.py -m ../MiniCPM-V-2
9+
python ./examples/minicpmv/convert-image-encoder-to-gguf.py -m ../MiniCPM-V-2 --llava-projector ../MiniCPM-V-2/llava.projector --output-dir ../MiniCPM-V-2 --image-mean 0.5 0.5 0.5 --image-std 0.5 0.5 0.5
10+
python ./convert-hf-to-gguf.py ../MiniCPM-V-2/MiniCPM
11+
./minicpmv-cli -m ../MiniCPM-V-2/MiniCPM/ggml-model-f16.gguf --mmproj ../MiniCPM-V-2/mmproj-model-f16.gguf -c 4096 --temp 0.6 --top-p 0.8 --top-k 100 --repeat-penalty 1.0 --image ../test.jpg -p "这张图里有什么?"
12+
13+
# or run quantize int4 version
14+
./quantize ../MiniCPM-V-2/MiniCPM/ggml-model-f16.gguf ../MiniCPM-V-2/MiniCPM/ggml-model-Q4_K_M.gguf Q4_K_M
15+
./minicpmv-cli -m ../MiniCPM-V-2/MiniCPM/ggml-model-Q4_K_M.gguf --mmproj ../MiniCPM-V-2/mmproj-model-f16.gguf -c 4096 --temp 0.6 --top-p 0.8 --top-k 100 --repeat-penalty 1.0 --image ../test.jpg -p "这张图里有什么?"
16+
17+
# or run in interactive mode
18+
./minicpmv-cli -m ../MiniCPM-V-2/MiniCPM/ggml-model-Q4_K_M.gguf --mmproj ../MiniCPM-V-2/mmproj-model-f16.gguf -c 4096 --temp 0.6 --top-p 0.8 --top-k 100 --repeat-penalty 1.0 --image ../test.jpg -i
19+
```

examples/minicpmv/android/adb_run.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
3+
model_dir="/Users/cxt/model/llm/mobileVLM/MobileVLM-1.7B_processed"
4+
projector_name="mmproj-model-f16.gguf"
5+
llama_name="ggml-model-q4_k.gguf"
6+
img_dir="/Users/cxt/model/llm"
7+
img_name="demo.jpg"
8+
prompt="A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. USER: <image>\nWho is the author of this book? \nAnswer the question using a single word or phrase. ASSISTANT:"
9+
# img_name="cat.jpeg"
10+
# prompt="A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. USER: <image>\nWhat is in the image? ASSISTANT:"
11+
12+
program_dir="build_64/bin"
13+
binName="minicpmv-cli"
14+
n_threads=4
15+
16+
17+
deviceDir="/data/local/tmp"
18+
saveDir="output"
19+
if [ ! -d ${saveDir} ]; then
20+
mkdir ${saveDir}
21+
fi
22+
23+
24+
function android_run() {
25+
# # copy resource into device
26+
# adb push ${model_dir}/${projector_name} ${deviceDir}/${projector_name}
27+
# adb push ${model_dir}/${llama_name} ${deviceDir}/${llama_name}
28+
adb push ${img_dir}/${img_name} ${deviceDir}/${img_name}
29+
# copy program into device
30+
adb push ${program_dir}/${binName} ${deviceDir}/${binName}
31+
adb shell "chmod 0777 ${deviceDir}/${binName}"
32+
33+
# run
34+
adb shell "echo cd ${deviceDir} ${deviceDir}/${binName} \
35+
-m ${deviceDir}/${llama_name} \
36+
--mmproj ${deviceDir}/${projector_name} \
37+
-t ${n_threads} \
38+
--image ${deviceDir}/${img_name} \
39+
-p \"${prompt}\" \
40+
> ${deviceDir}/${modelName}_${projector_name}_${n_threads}_${img_name}.txt"
41+
adb shell "cd ${deviceDir}; pwd; ${deviceDir}/${binName} \
42+
-m ${deviceDir}/${llama_name} \
43+
--mmproj ${deviceDir}/${projector_name} \
44+
-t ${n_threads} \
45+
--image ${deviceDir}/${img_name} \
46+
-p \"${prompt}\" \
47+
>> ${deviceDir}/${modelName}_${projector_name}_${n_threads}_${img_name}.txt 2>&1"
48+
adb pull ${deviceDir}/${modelName}_${projector_name}_${n_threads}_${img_name}.txt ${saveDir}
49+
}
50+
51+
android_run
52+
53+
echo "android_run is Done!"

0 commit comments

Comments
 (0)