From 72dc7bff4dee9f262edee7ec5bfa99e42ba1d63b Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Fri, 17 Jan 2025 15:18:24 +0200 Subject: [PATCH 1/7] cmake : add sanitizer flags for llama.cpp ggml-ci --- common/CMakeLists.txt | 17 +++++++++++++++++ examples/CMakeLists.txt | 17 +++++++++++++++++ src/CMakeLists.txt | 17 +++++++++++++++++ tests/CMakeLists.txt | 19 +++++++++++++++++++ 4 files changed, 70 insertions(+) diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index df1cdf9a5..374746c76 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -4,6 +4,23 @@ find_package(Threads REQUIRED) llama_add_compile_flags() +if (NOT MSVC) + if (LLAMA_SANITIZE_THREAD) + add_compile_options(-fsanitize=thread) + link_libraries (-fsanitize=thread) + endif() + + if (LLAMA_SANITIZE_ADDRESS) + add_compile_options(-fsanitize=address -fno-omit-frame-pointer) + link_libraries (-fsanitize=address) + endif() + + if (LLAMA_SANITIZE_UNDEFINED) + add_compile_options(-fsanitize=undefined) + link_libraries (-fsanitize=undefined) + endif() +endif() + # Build info header # diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 66cfab2c3..43507ea71 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -10,6 +10,23 @@ find_package(Threads REQUIRED) llama_add_compile_flags() +if (NOT MSVC) + if (LLAMA_SANITIZE_THREAD) + add_compile_options(-fsanitize=thread) + link_libraries (-fsanitize=thread) + endif() + + if (LLAMA_SANITIZE_ADDRESS) + add_compile_options(-fsanitize=address -fno-omit-frame-pointer) + link_libraries (-fsanitize=address) + endif() + + if (LLAMA_SANITIZE_UNDEFINED) + add_compile_options(-fsanitize=undefined) + link_libraries (-fsanitize=undefined) + endif() +endif() + # examples include_directories(${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index aeb75bf3e..5f9c8e9d7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,22 @@ llama_add_compile_flags() +if (NOT MSVC) + if (LLAMA_SANITIZE_THREAD) + add_compile_options(-fsanitize=thread) + link_libraries (-fsanitize=thread) + endif() + + if (LLAMA_SANITIZE_ADDRESS) + add_compile_options(-fsanitize=address -fno-omit-frame-pointer) + link_libraries (-fsanitize=address) + endif() + + if (LLAMA_SANITIZE_UNDEFINED) + add_compile_options(-fsanitize=undefined) + link_libraries (-fsanitize=undefined) + endif() +endif() + # # libraries # diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2b5e5fd4a..0c9ebf580 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,3 +1,22 @@ +llama_add_compile_flags() + +if (NOT MSVC) + if (LLAMA_SANITIZE_THREAD) + add_compile_options(-fsanitize=thread) + link_libraries (-fsanitize=thread) + endif() + + if (LLAMA_SANITIZE_ADDRESS) + add_compile_options(-fsanitize=address -fno-omit-frame-pointer) + link_libraries (-fsanitize=address) + endif() + + if (LLAMA_SANITIZE_UNDEFINED) + add_compile_options(-fsanitize=undefined) + link_libraries (-fsanitize=undefined) + endif() +endif() + function(llama_test target) include(CMakeParseArguments) set(options) From ce293d837c4c54bf646f55a9e9d625d6ea9cb4cc Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Fri, 17 Jan 2025 15:22:36 +0200 Subject: [PATCH 2/7] tests : fix compile warnings ggml-ci --- tests/test-gguf.cpp | 20 +++++++++----------- tests/test-sampling.cpp | 1 - 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/tests/test-gguf.cpp b/tests/test-gguf.cpp index 611957ac0..7ee888b3b 100644 --- a/tests/test-gguf.cpp +++ b/tests/test-gguf.cpp @@ -48,7 +48,7 @@ enum handcrafted_file_type { HANDCRAFTED_DATA_CUSTOM_ALIGN = 810 + offset_has_data, }; -std::string handcrafted_file_type_name(const enum handcrafted_file_type hft) { +static std::string handcrafted_file_type_name(const enum handcrafted_file_type hft) { switch (hft) { case HANDCRAFTED_HEADER_BAD_MAGIC: return "HEADER_BAD_MAGIC"; case HANDCRAFTED_HEADER_BAD_VERSION_1: return "HEADER_BAD_VERSION_1"; @@ -99,7 +99,7 @@ static bool expect_context_not_null(const enum handcrafted_file_type hft) { typedef std::pair> tensor_config_t; -std::vector get_tensor_configs(std::mt19937 & rng) { +static std::vector get_tensor_configs(std::mt19937 & rng) { std::vector tensor_configs; tensor_configs.reserve(100); @@ -122,7 +122,7 @@ std::vector get_tensor_configs(std::mt19937 & rng) { return tensor_configs; } -std::vector> get_kv_types(std::mt19937 rng) { +static std::vector> get_kv_types(std::mt19937 rng) { std::vector> kv_types; kv_types.reserve(100); @@ -626,8 +626,6 @@ static bool handcrafted_check_tensor_data(const gguf_context * gguf_ctx, const u bool ok = true; - const uint32_t alignment = GGUF_DEFAULT_ALIGNMENT; - for (int i = 0; i < int(tensor_configs.size()); ++i) { const ggml_type type = tensor_configs[i].first; const std::array shape = tensor_configs[i].second; @@ -866,13 +864,13 @@ static struct random_gguf_context_result get_random_gguf_context(ggml_backend_t case GGUF_TYPE_COUNT: default: { GGML_ABORT("fatal error"); - } break; + } } } break; case GGUF_TYPE_COUNT: default: { GGML_ABORT("fatal error"); - } break; + } } } @@ -938,7 +936,7 @@ static bool all_kv_in_other(const gguf_context * ctx, const gguf_context * other } if (type == GGUF_TYPE_ARRAY) { - const int arr_n = gguf_get_arr_n(ctx, id); + const size_t arr_n = gguf_get_arr_n(ctx, id); if (arr_n != gguf_get_arr_n(other, idx_other)) { ok = false; continue; @@ -953,7 +951,7 @@ static bool all_kv_in_other(const gguf_context * ctx, const gguf_context * other if (type_arr == GGUF_TYPE_BOOL) { const int8_t * data = reinterpret_cast(gguf_get_arr_data(ctx, id)); const int8_t * data_other = reinterpret_cast(gguf_get_arr_data(other, idx_other)); - for (int arr_i = 0; arr_i < arr_n; ++arr_i) { + for (size_t arr_i = 0; arr_i < arr_n; ++arr_i) { if (bool(data[arr_i]) != bool(data_other[arr_i])) { ok = false; } @@ -962,7 +960,7 @@ static bool all_kv_in_other(const gguf_context * ctx, const gguf_context * other } if (type_arr == GGUF_TYPE_STRING) { - for (int arr_i = 0; arr_i < arr_n; ++arr_i) { + for (size_t arr_i = 0; arr_i < arr_n; ++arr_i) { const std::string str = gguf_get_arr_str(ctx, id, arr_i); const std::string str_other = gguf_get_arr_str(other, idx_other, arr_i); if (str != str_other) { @@ -1057,7 +1055,7 @@ static bool same_tensor_data(const struct ggml_context * orig, const struct ggml ok = false; } - return true; + return ok; } static std::pair test_roundtrip(ggml_backend_dev_t dev, const unsigned int seed, const bool only_meta) { diff --git a/tests/test-sampling.cpp b/tests/test-sampling.cpp index c0dcb4848..61bd67850 100644 --- a/tests/test-sampling.cpp +++ b/tests/test-sampling.cpp @@ -144,7 +144,6 @@ static void test_penalties( sampler_tester tester(probs, probs_expected); - const size_t n_vocab = probs.size(); auto * sampler = llama_sampler_init_penalties(last_tokens.size(), repeat_penalty, alpha_frequency, alpha_presence); for (size_t i = 0; i < last_tokens.size(); i++) { From 9a03bc811fb6062c2118ccc220885e6012d0a4fb Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Fri, 17 Jan 2025 15:33:36 +0200 Subject: [PATCH 3/7] cmake : move sanitizer flags to llama_add_compile_flags ggml-ci --- cmake/common.cmake | 17 +++++++++++++++++ common/CMakeLists.txt | 17 ----------------- examples/CMakeLists.txt | 17 ----------------- src/CMakeLists.txt | 17 ----------------- tests/CMakeLists.txt | 17 ----------------- 5 files changed, 17 insertions(+), 68 deletions(-) diff --git a/cmake/common.cmake b/cmake/common.cmake index 0f54871e4..b408c8e8a 100644 --- a/cmake/common.cmake +++ b/cmake/common.cmake @@ -30,4 +30,21 @@ function(llama_add_compile_flags) set(CXX_FLAGS "" PARENT_SCOPE) endif() endif() + + if (NOT MSVC) + if (LLAMA_SANITIZE_THREAD) + add_compile_options(-fsanitize=thread) + link_libraries (-fsanitize=thread) + endif() + + if (LLAMA_SANITIZE_ADDRESS) + add_compile_options(-fsanitize=address -fno-omit-frame-pointer) + link_libraries (-fsanitize=address) + endif() + + if (LLAMA_SANITIZE_UNDEFINED) + add_compile_options(-fsanitize=undefined) + link_libraries (-fsanitize=undefined) + endif() + endif() endfunction() diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 374746c76..df1cdf9a5 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -4,23 +4,6 @@ find_package(Threads REQUIRED) llama_add_compile_flags() -if (NOT MSVC) - if (LLAMA_SANITIZE_THREAD) - add_compile_options(-fsanitize=thread) - link_libraries (-fsanitize=thread) - endif() - - if (LLAMA_SANITIZE_ADDRESS) - add_compile_options(-fsanitize=address -fno-omit-frame-pointer) - link_libraries (-fsanitize=address) - endif() - - if (LLAMA_SANITIZE_UNDEFINED) - add_compile_options(-fsanitize=undefined) - link_libraries (-fsanitize=undefined) - endif() -endif() - # Build info header # diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 43507ea71..66cfab2c3 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -10,23 +10,6 @@ find_package(Threads REQUIRED) llama_add_compile_flags() -if (NOT MSVC) - if (LLAMA_SANITIZE_THREAD) - add_compile_options(-fsanitize=thread) - link_libraries (-fsanitize=thread) - endif() - - if (LLAMA_SANITIZE_ADDRESS) - add_compile_options(-fsanitize=address -fno-omit-frame-pointer) - link_libraries (-fsanitize=address) - endif() - - if (LLAMA_SANITIZE_UNDEFINED) - add_compile_options(-fsanitize=undefined) - link_libraries (-fsanitize=undefined) - endif() -endif() - # examples include_directories(${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5f9c8e9d7..aeb75bf3e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,22 +1,5 @@ llama_add_compile_flags() -if (NOT MSVC) - if (LLAMA_SANITIZE_THREAD) - add_compile_options(-fsanitize=thread) - link_libraries (-fsanitize=thread) - endif() - - if (LLAMA_SANITIZE_ADDRESS) - add_compile_options(-fsanitize=address -fno-omit-frame-pointer) - link_libraries (-fsanitize=address) - endif() - - if (LLAMA_SANITIZE_UNDEFINED) - add_compile_options(-fsanitize=undefined) - link_libraries (-fsanitize=undefined) - endif() -endif() - # # libraries # diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0c9ebf580..3fa43c295 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,22 +1,5 @@ llama_add_compile_flags() -if (NOT MSVC) - if (LLAMA_SANITIZE_THREAD) - add_compile_options(-fsanitize=thread) - link_libraries (-fsanitize=thread) - endif() - - if (LLAMA_SANITIZE_ADDRESS) - add_compile_options(-fsanitize=address -fno-omit-frame-pointer) - link_libraries (-fsanitize=address) - endif() - - if (LLAMA_SANITIZE_UNDEFINED) - add_compile_options(-fsanitize=undefined) - link_libraries (-fsanitize=undefined) - endif() -endif() - function(llama_test target) include(CMakeParseArguments) set(options) From 9d1b20ad1aca65e55408ee6f90104e9c2e8e7ada Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Fri, 17 Jan 2025 15:40:03 +0200 Subject: [PATCH 4/7] cmake : move llama.cpp compile flags to top level lists ggml-ci --- CMakeLists.txt | 45 ++++++++++++++++++++++++----------------- common/CMakeLists.txt | 2 -- examples/CMakeLists.txt | 2 +- src/CMakeLists.txt | 2 -- tests/CMakeLists.txt | 2 -- 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a717a508f..622958dde 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -118,15 +118,40 @@ llama_option_depr(WARNING LLAMA_SYCL_F16 GGML_SYCL_F16) llama_option_depr(WARNING LLAMA_CANN GGML_CANN) # -# build the library +# 3rd party # if (NOT TARGET ggml) add_subdirectory(ggml) # ... otherwise assume ggml is added by a parent CMakeLists.txt endif() + +# +# build the library +# + +llama_add_compile_flags() # llama-only flags + add_subdirectory(src) +# +# utils, programs, examples and tests +# + +if (LLAMA_BUILD_COMMON) + add_subdirectory(common) +endif() + +if (LLAMA_BUILD_COMMON AND LLAMA_BUILD_TESTS AND NOT CMAKE_JS_VERSION) + include(CTest) + add_subdirectory(tests) +endif() + +if (LLAMA_BUILD_COMMON AND LLAMA_BUILD_EXAMPLES) + add_subdirectory(examples) + add_subdirectory(pocs) +endif() + # # install # @@ -200,21 +225,3 @@ configure_file(cmake/llama.pc.in install(FILES "${CMAKE_CURRENT_BINARY_DIR}/llama.pc" DESTINATION lib/pkgconfig) - -# -# utils, programs, examples and tests -# - -if (LLAMA_BUILD_COMMON) - add_subdirectory(common) -endif() - -if (LLAMA_BUILD_COMMON AND LLAMA_BUILD_TESTS AND NOT CMAKE_JS_VERSION) - include(CTest) - add_subdirectory(tests) -endif() - -if (LLAMA_BUILD_COMMON AND LLAMA_BUILD_EXAMPLES) - add_subdirectory(examples) - add_subdirectory(pocs) -endif() diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index df1cdf9a5..01c00e277 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -2,8 +2,6 @@ find_package(Threads REQUIRED) -llama_add_compile_flags() - # Build info header # diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 66cfab2c3..f8395b59f 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -8,7 +8,7 @@ find_package(Threads REQUIRED) # flags -llama_add_compile_flags() +# ... # examples diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index aeb75bf3e..ea9fbd870 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,3 @@ -llama_add_compile_flags() - # # libraries # diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3fa43c295..2b5e5fd4a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,5 +1,3 @@ -llama_add_compile_flags() - function(llama_test target) include(CMakeParseArguments) set(options) From e872097c35db57806f0fe94c82fbe8a8a34a07f6 Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Fri, 17 Jan 2025 15:48:39 +0200 Subject: [PATCH 5/7] cmake : apply only sanitizer flags at top level ggml-ci --- CMakeLists.txt | 26 +++++++++++++++++++------- cmake/common.cmake | 17 ----------------- common/CMakeLists.txt | 2 ++ examples/CMakeLists.txt | 2 +- src/CMakeLists.txt | 2 ++ tests/CMakeLists.txt | 2 ++ 6 files changed, 26 insertions(+), 25 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 622958dde..0e5780f4a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,11 +83,8 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/build-info.cmake) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/common.cmake) # override ggml options -set(GGML_SANITIZE_THREAD ${LLAMA_SANITIZE_THREAD}) -set(GGML_SANITIZE_ADDRESS ${LLAMA_SANITIZE_ADDRESS}) -set(GGML_SANITIZE_UNDEFINED ${LLAMA_SANITIZE_UNDEFINED}) -set(GGML_ALL_WARNINGS ${LLAMA_ALL_WARNINGS}) -set(GGML_FATAL_WARNINGS ${LLAMA_FATAL_WARNINGS}) +set(GGML_ALL_WARNINGS ${LLAMA_ALL_WARNINGS}) +set(GGML_FATAL_WARNINGS ${LLAMA_FATAL_WARNINGS}) # change the default for these ggml options if (NOT DEFINED GGML_LLAMAFILE) @@ -117,6 +114,23 @@ llama_option_depr(WARNING LLAMA_SYCL GGML_SYCL) llama_option_depr(WARNING LLAMA_SYCL_F16 GGML_SYCL_F16) llama_option_depr(WARNING LLAMA_CANN GGML_CANN) +if (NOT MSVC) + if (LLAMA_SANITIZE_THREAD) + add_compile_options(-fsanitize=thread) + link_libraries (-fsanitize=thread) + endif() + + if (LLAMA_SANITIZE_ADDRESS) + add_compile_options(-fsanitize=address -fno-omit-frame-pointer) + link_libraries (-fsanitize=address) + endif() + + if (LLAMA_SANITIZE_UNDEFINED) + add_compile_options(-fsanitize=undefined) + link_libraries (-fsanitize=undefined) + endif() +endif() + # # 3rd party # @@ -130,8 +144,6 @@ endif() # build the library # -llama_add_compile_flags() # llama-only flags - add_subdirectory(src) # diff --git a/cmake/common.cmake b/cmake/common.cmake index b408c8e8a..0f54871e4 100644 --- a/cmake/common.cmake +++ b/cmake/common.cmake @@ -30,21 +30,4 @@ function(llama_add_compile_flags) set(CXX_FLAGS "" PARENT_SCOPE) endif() endif() - - if (NOT MSVC) - if (LLAMA_SANITIZE_THREAD) - add_compile_options(-fsanitize=thread) - link_libraries (-fsanitize=thread) - endif() - - if (LLAMA_SANITIZE_ADDRESS) - add_compile_options(-fsanitize=address -fno-omit-frame-pointer) - link_libraries (-fsanitize=address) - endif() - - if (LLAMA_SANITIZE_UNDEFINED) - add_compile_options(-fsanitize=undefined) - link_libraries (-fsanitize=undefined) - endif() - endif() endfunction() diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 01c00e277..df1cdf9a5 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -2,6 +2,8 @@ find_package(Threads REQUIRED) +llama_add_compile_flags() + # Build info header # diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index f8395b59f..66cfab2c3 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -8,7 +8,7 @@ find_package(Threads REQUIRED) # flags -# ... +llama_add_compile_flags() # examples diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ea9fbd870..aeb75bf3e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,5 @@ +llama_add_compile_flags() + # # libraries # diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2b5e5fd4a..3fa43c295 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,3 +1,5 @@ +llama_add_compile_flags() + function(llama_test target) include(CMakeParseArguments) set(options) From 7000623c00f345e46ab4a57d1d657ca1628fbb70 Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Fri, 17 Jan 2025 16:26:12 +0200 Subject: [PATCH 6/7] tests : fix gguf context use in same_tensor_data --- tests/test-gguf.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-gguf.cpp b/tests/test-gguf.cpp index 7ee888b3b..2e39b5c1a 100644 --- a/tests/test-gguf.cpp +++ b/tests/test-gguf.cpp @@ -1049,7 +1049,7 @@ static bool same_tensor_data(const struct ggml_context * orig, const struct ggml } t_orig = ggml_get_next_tensor(orig, t_orig); - t_read = ggml_get_next_tensor(orig, t_read); + t_read = ggml_get_next_tensor(read, t_read); } if (t_read) { ok = false; From ba421dd04ea4a878d0085ad093222f996afc1a27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20G=C3=A4=C3=9Fler?= Date: Sat, 18 Jan 2025 09:49:47 +0100 Subject: [PATCH 7/7] gguf-test: tensor data comparison --- ggml/src/gguf.cpp | 4 ++++ tests/test-gguf.cpp | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/ggml/src/gguf.cpp b/ggml/src/gguf.cpp index 655ed600a..ab13669c5 100644 --- a/ggml/src/gguf.cpp +++ b/ggml/src/gguf.cpp @@ -648,6 +648,10 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par ok = ok && data != nullptr; + if (ok) { + ggml_set_name(data, "GGUF tensor data binary blob"); + } + // read the binary blob with the tensor data ok = ok && gr.read(data->data, ctx->size); diff --git a/tests/test-gguf.cpp b/tests/test-gguf.cpp index 2e39b5c1a..6ed696328 100644 --- a/tests/test-gguf.cpp +++ b/tests/test-gguf.cpp @@ -1031,6 +1031,12 @@ static bool same_tensor_data(const struct ggml_context * orig, const struct ggml struct ggml_tensor * t_orig = ggml_get_first_tensor(orig); struct ggml_tensor * t_read = ggml_get_first_tensor(read); + + if (std::string(t_read->name) != "GGUF tensor data binary blob") { + return false; + } + t_read = ggml_get_next_tensor(read, t_read); + while (t_orig) { if (!t_read) { ok = false;