llama.cpp 去掉打印,只显示推理结果
1 llama.cpp/common/log.h
#define LOG_INF(...) LOG_TMPL(GGML_LOG_LEVEL_INFO, 0, __VA_ARGS__)
#define LOG_WRN(...) LOG_TMPL(GGML_LOG_LEVEL_WARN, 0, __VA_ARGS__)
#define LOG_ERR(...) LOG_TMPL(GGML_LOG_LEVEL_ERROR, 0, __VA_ARGS__)
#define LOG_DBG(...) LOG_TMPL(GGML_LOG_LEVEL_DEBUG, LOG_DEFAULT_DEBUG, __VA_ARGS__)
#define LOG_CNT(...) LOG_TMPL(GGML_LOG_LEVEL_CONT, 0, __VA_ARGS__)
#define LOG_INFV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_INFO, verbosity, __VA_ARGS__)
#define LOG_WRNV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_WARN, verbosity, __VA_ARGS__)
#define LOG_ERRV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_ERROR, verbosity, __VA_ARGS__)
#define LOG_DBGV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_DEBUG, verbosity, __VA_ARGS__)
#define LOG_CNTV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_CONT, verbosity, __VA_ARGS__)
修改为:
#ifndef NDEBUG // 如果没有定义NDEBUG(即处于调试模式)
#define LOG_INF(...) LOG_TMPL(GGML_LOG_LEVEL_INFO, 0, __VA_ARGS__)
#define LOG_WRN(...) LOG_TMPL(GGML_LOG_LEVEL_WARN, 0, __VA_ARGS__)
#define LOG_ERR(...) LOG_TMPL(GGML_LOG_LEVEL_ERROR, 0, __VA_ARGS__)
#define LOG_DBG(...) LOG_TMPL(GGML_LOG_LEVEL_DEBUG, LOG_DEFAULT_DEBUG, __VA_ARGS__)
#define LOG_CNT(...) LOG_TMPL(GGML_LOG_LEVEL_CONT, 0, __VA_ARGS__)
#define LOG_INFV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_INFO, verbosity, __VA_ARGS__)
#define LOG_WRNV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_WARN, verbosity, __VA_ARGS__)
#define LOG_ERRV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_ERROR, verbosity, __VA_ARGS__)
#define LOG_DBGV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_DEBUG, verbosity, __VA_ARGS__)
#define LOG_CNTV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_CONT, verbosity, __VA_ARGS__)
#else // 如果定义了NDEBUG(即处于发布模式)
#define LOG_INF(...)
#define LOG_WRN(...)
#define LOG_ERR(...)
#define LOG_DBG(...)
#define LOG_CNT(...)
#define LOG_INFV(verbosity, ...)
#define LOG_WRNV(verbosity, ...)
#define LOG_ERRV(verbosity, ...)
#define LOG_DBGV(verbosity, ...)
#define LOG_CNTV(verbosity, ...)
#endif
2 语言模型部分,已经存在参数
语言模型部分 ,命令行参数:
--log-disable
禁止所有打印
3 视觉模型部分,以 llava-cli 为例
1 修改代码
llama.cpp/examples/llava/llava.cpp :
llama.cpp/examples/llava/clip.cpp :
#define LOG_INF(...) do { fprintf(stdout, __VA_ARGS__); } while (0)
#define LOG_WRN(...) do { fprintf(stderr, __VA_ARGS__); } while (0)
#define LOG_ERR(...) do { fprintf(stderr, __VA_ARGS__); } while (0)
#define LOG_DBG(...) do { fprintf(stderr, __VA_ARGS__); } while (0)
改为如下:
#ifndef NDEBUG // 如果没有定义NDEBUG(即处于调试模式)
#define LOG_INF(...) do { fprintf(stdout, __VA_ARGS__); } while (0)
#define LOG_WRN(...) do { fprintf(stderr, __VA_ARGS__); } while (0)
#define LOG_ERR(...) do { fprintf(stderr, __VA_ARGS__); } while (0)
#define LOG_DBG(...) do { fprintf(stderr, __VA_ARGS__); } while (0)
#else // 如果定义了NDEBUG(即处于发布模式)
#define LOG_INF(...)
#define LOG_WRN(...)
#define LOG_ERR(...)
#define LOG_DBG(...)
#define LOG_CNT(...)
#endif
2 examples/llava/llava-cli.cpp :
process_prompt()函数内部:
LOG("%s", tmp);
改为
std::cout<<tmp;
3 examples/llava/CMakeLists.txt
添加宏定义 -DNDEBUG
set(TARGET llama-llava-cli)
add_executable(${TARGET} llava-cli.cpp)
set_target_properties(${TARGET} PROPERTIES OUTPUT_NAME llama-llava-cli)
install(TARGETS ${TARGET} RUNTIME)
target_link_libraries(${TARGET} PRIVATE common llava ${CMAKE_THREAD_LIBS_INIT})
target_compile_features(${TARGET} PRIVATE cxx_std_11)
# 为特定目标添加 NDEBUG 定义
target_compile_definitions(${TARGET} PRIVATE -DNDEBUG)
编译之后,推理测试:
1.png
model_dir=/huggingface_cache/Bunny-v1_0-4B-gguf/ggml-model-f16.gguf
mmproj_dir=/huggingface_cache/Bunny-v1_0-4B-gguf/mmproj-model-f16.gguf
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>\nWhy is the image funny? ASSISTANT:"
image=/media/wmx/soft1/huggingface_cache/1.png
/media/llama.cpp/build/bin/llama-llava-cli \
-m $model_dir \
--mmproj $mmproj_dir \
--image ${image} \
-p $prompt
只输出:
在海滩上,一位女士和她的狗正在进行一个友好的互动。狗伸出前爪,
似乎在与女士进行握手,这是人类和动物之间常见的问候方式。
女士坐在沙滩上,面带微笑,似乎在享受和狗狗的时光。背景是晴朗的天空和远处的海洋,