Skip to content

Commit 6d90fe9

Browse files
committed
ggml : add GGML_NO_REALLOC option to disable reallocations in ggml-alloc
Enabled in ggml-ci for testing.
1 parent 9b17d74 commit 6d90fe9

File tree

6 files changed

+26
-4
lines changed

6 files changed

+26
-4
lines changed

ci/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ sd=`dirname $0`
4545
cd $sd/../
4646
SRC=`pwd`
4747

48-
CMAKE_EXTRA="-DLLAMA_FATAL_WARNINGS=ON -DLLAMA_CURL=ON"
48+
CMAKE_EXTRA="-DLLAMA_FATAL_WARNINGS=ON -DLLAMA_CURL=ON -DGGML_NO_REALLOC=ON"
4949

5050
if [ ! -z ${GG_BUILD_METAL} ]; then
5151
CMAKE_EXTRA="${CMAKE_EXTRA} -DGGML_METAL=ON"

ggml/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ endif()
182182
# ggml core
183183
set(GGML_SCHED_MAX_COPIES "4" CACHE STRING "ggml: max input copies for pipeline parallelism")
184184
option(GGML_CPU "ggml: enable CPU backend" ON)
185+
option(GGML_NO_REALLOC "ggml: disallow reallocations in ggml-alloc (for debugging)" OFF)
185186

186187
# 3rd party libs / backends
187188
option(GGML_ACCELERATE "ggml: enable Accelerate framework" ON)

ggml/src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ if (GGML_BACKEND_DL)
221221
target_compile_definitions(ggml-base PUBLIC GGML_BACKEND_DL)
222222
endif()
223223

224+
if (GGML_NO_REALLOC)
225+
target_compile_definitions(ggml-base PUBLIC GGML_NO_REALLOC)
226+
endif()
227+
224228
add_library(ggml
225229
ggml-backend-reg.cpp)
226230
add_library(ggml::ggml ALIAS ggml)

ggml/src/ggml-alloc.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,8 +921,23 @@ bool ggml_gallocr_reserve_n(ggml_gallocr_t galloc, struct ggml_cgraph * graph, c
921921
}
922922
if (realloc) {
923923
#ifndef NDEBUG
924-
size_t cur_size = galloc->buffers[i] ? ggml_vbuffer_size(galloc->buffers[i]) : 0;
925-
GGML_LOG_DEBUG("%s: reallocating %s buffer from size %.02f MiB to %.02f MiB\n", __func__, ggml_backend_buft_name(galloc->bufts[i]), cur_size / 1024.0 / 1024.0, new_size / 1024.0 / 1024.0);
924+
{
925+
size_t cur_size = galloc->buffers[i] ? ggml_vbuffer_size(galloc->buffers[i]) : 0;
926+
if (cur_size > 0) {
927+
GGML_LOG_DEBUG("%s: reallocating %s buffer from size %.02f MiB to %.02f MiB\n",
928+
__func__, ggml_backend_buft_name(galloc->bufts[i]),
929+
cur_size / 1024.0 / 1024.0, new_size / 1024.0 / 1024.0);
930+
}
931+
}
932+
#endif
933+
934+
#ifdef GGML_NO_REALLOC
935+
if (galloc->buffers[i] != NULL) {
936+
size_t cur_size = galloc->buffers[i] ? ggml_vbuffer_size(galloc->buffers[i]) : 0;
937+
GGML_ABORT("%s: reallocating %s buffer from size %.02f MiB to %.02f MiB not allowed with GGML_NO_REALLOC\n",
938+
__func__, ggml_backend_buft_name(galloc->bufts[i]),
939+
cur_size / 1024.0 / 1024.0, new_size / 1024.0 / 1024.0);
940+
}
926941
#endif
927942

928943
ggml_vbuffer_free(galloc->buffers[i]);

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ if (NOT WIN32)
196196
llama_build_and_test(test-arg-parser.cpp)
197197
endif()
198198

199-
if (NOT LLAMA_SANITIZE_ADDRESS)
199+
if (NOT LLAMA_SANITIZE_ADDRESS AND NOT GGML_NO_REALLOC)
200200
# TODO: repair known memory leaks
201201
llama_build_and_test(test-opt.cpp)
202202
endif()

tests/test-alloc.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,8 @@ int main() {
603603
run("test_prefer_already_allocated_memory", test_prefer_already_allocated_memory);
604604
run("test_multiple_buffer_types", test_multiple_buffer_types);
605605
run("test_buffer_size_zero", test_buffer_size_zero);
606+
#ifndef GGML_NO_REALLOC
606607
run("test_reallocation", test_reallocation);
608+
#endif
607609
return 0;
608610
}

0 commit comments

Comments
 (0)