Skip to content

Commit 7a49a6f

Browse files
committed
init
1 parent 9b82476 commit 7a49a6f

19 files changed

+3926
-291
lines changed

.gitignore

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

Makefile

Lines changed: 8 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
@@ -859,6 +859,13 @@ llava-cli: examples/llava/llava-cli.cpp examples/llava/clip.h examples/llava/cli
859859
$(CXX) $(CXXFLAGS) -c examples/llava/llava.cpp -o $(call GET_OBJ_FILE, examples/llava/llava.cpp)
860860
$(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)
861861

862+
minicpmv-cli: examples/minicpmv/minicpmv-cli.cpp examples/minicpmv/clip.h examples/minicpmv/clip.cpp examples/minicpmv/minicpmv.h examples/minicpmv/minicpmv.cpp examples/minicpmv/minicpmv_io.h examples/minicpmv/minicpmv_io.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
863+
$(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
864+
$(CXX) $(CXXFLAGS) -c examples/minicpmv/clip.cpp -o $(call GET_OBJ_FILE, examples/minicpmv/clip.cpp) -Wno-cast-qual
865+
$(CXX) $(CXXFLAGS) -c examples/minicpmv/minicpmv.cpp -o $(call GET_OBJ_FILE, examples/minicpmv/minicpmv.cpp)
866+
$(CXX) $(CXXFLAGS) -c examples/minicpmv/minicpmv_io.cpp -o $(call GET_OBJ_FILE, examples/minicpmv/minicpmv_io.cpp)
867+
$(CXX) $(CXXFLAGS) $(filter-out %.h $< examples/minicpmv/clip.cpp examples/minicpmv/minicpmv.cpp examples/minicpmv/minicpmv_io.cpp,$^) $(call GET_OBJ_FILE, $<) $(call GET_OBJ_FILE, examples/minicpmv/clip.cpp) $(call GET_OBJ_FILE, examples/minicpmv/minicpmv.cpp) $(call GET_OBJ_FILE, examples/minicpmv/minicpmv_io.cpp) -o $@ $(LDFLAGS)
868+
862869
baby-llama: examples/baby-llama/baby-llama.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
863870
$(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
864871
$(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)

convert-hf-to-gguf.py

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -673,44 +673,6 @@ def set_gguf_parameters(self):
673673
self.gguf_writer.add_parallel_residual(self.hparams.get("use_parallel_residual", True))
674674
self.gguf_writer.add_layer_norm_eps(self.hparams["layer_norm_eps"])
675675

676-
def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
677-
del bid # unused
678-
679-
n_head = self.hparams.get("n_head", self.hparams.get("num_attention_heads"))
680-
n_embed = self.hparams.get("hidden_size", self.hparams.get("n_embed"))
681-
682-
tensors: list[tuple[str, Tensor]] = []
683-
684-
if re.match(r"gpt_neox\.layers\.\d+\.attention\.query_key_value\.weight", name):
685-
# Map bloom-style qkv_linear to gpt-style qkv_linear
686-
# bloom: https://github.com/huggingface/transformers/blob/main/src/transformers/models/bloom/modeling_bloom.py#L238-L252 # noqa
687-
# gpt-2: https://github.com/huggingface/transformers/blob/main/src/transformers/models/gpt2/modeling_gpt2.py#L312 # noqa
688-
qkv_weights = data_torch.reshape((n_head, 3, n_embed // n_head, n_embed))
689-
data_torch = torch.cat(
690-
(
691-
qkv_weights[:, 0, :, :].reshape((-1, n_embed)),
692-
qkv_weights[:, 1, :, :].reshape((-1, n_embed)),
693-
qkv_weights[:, 2, :, :].reshape((-1, n_embed)),
694-
),
695-
dim=0,
696-
)
697-
logger.info("re-format attention.linear_qkv.weight")
698-
elif re.match(r"gpt_neox\.layers\.\d+\.attention\.query_key_value\.bias", name):
699-
qkv_bias = data_torch.reshape((n_head, 3, n_embed // n_head))
700-
data_torch = torch.cat(
701-
(
702-
qkv_bias[:, 0, :].reshape((n_embed,)),
703-
qkv_bias[:, 1, :].reshape((n_embed,)),
704-
qkv_bias[:, 2, :].reshape((n_embed,)),
705-
),
706-
dim=0,
707-
)
708-
logger.info("re-format attention.linear_qkv.bias")
709-
710-
tensors.append((self.map_tensor_name(name), data_torch))
711-
712-
return tensors
713-
714676

715677
@Model.register("BloomForCausalLM")
716678
class BloomModel(Model):

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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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)
38+
39+
add_library(minicpmv_io OBJECT
40+
minicpmv_io.cpp
41+
)
42+
target_link_libraries(minicpmv_io PRIVATE llava ${CMAKE_THREAD_LIBS_INIT})

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.5 model are put in its parent folder.
3+
4+
```bash
5+
make
6+
make minicpmv-cli
7+
8+
python ./examples/minicpmv/minicpmv-surgery.py -m ../MiniCPM-V-2_5
9+
python ./examples/minicpmv/minicpmv-convert-image-encoder-to-gguf.py -m ../MiniCPM-V-2_5 --llava-projector ../MiniCPM-V-2_5/llava.projector --output-dir ../MiniCPM-V-2_5/ --image-mean 0.5 0.5 0.5 --image-std 0.5 0.5 0.5
10+
python ./convert.py ../MiniCPM-V-2_5/model --outtype f16 --vocab-type bpe
11+
./minicpmv-cli -m ../MiniCPM-V-2_5/model/ggml-model-f16.gguf --mmproj ../MiniCPM-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?"
12+
13+
# or run quantize int4 version
14+
./quantize ../MiniCPM-V-2_5/model/ggml-model-f16.gguf ../MiniCPM-V-2_5/model/ggml-model-Q4_K_M.gguf Q4_K_M
15+
./minicpmv-cli -m ../MiniCPM-V-2_5/model/ggml-model-Q4_K_M.gguf --mmproj ../MiniCPM-V-2_5/mmproj-model-f16.gguf -c 4096 --temp 0.6 --top-p 0.8 --top-k 100 --repeat-penalty 1.0 --image xx.jpg -p "What is in the image?"
16+
17+
# or run in interactive mode
18+
./minicpmv-cli -m ../MiniCPM-V-2_5/model/ggml-model-Q4_K_M.gguf --mmproj ../MiniCPM-V-2_5/mmproj-model-f16.gguf -c 4096 --temp 0.6 --top-p 0.8 --top-k 100 --repeat-penalty 1.0 --image xx.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)