Skip to content

Commit 0710d5f

Browse files
committed
ggml : add GGML_SCHED_NO_REALLOC option to disable reallocations in ggml_backend_sched
Enabled in ggml-ci for testing.
1 parent cb623de commit 0710d5f

File tree

6 files changed

+20
-5
lines changed

6 files changed

+20
-5
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_SCHED_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_SCHED_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_SCHED_NO_REALLOC)
225+
target_compile_definitions(ggml-base PUBLIC GGML_SCHED_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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -921,10 +921,15 @@ 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+
}
926932
#endif
927-
928933
ggml_vbuffer_free(galloc->buffers[i]);
929934
galloc->buffers[i] = ggml_vbuffer_alloc(galloc->bufts[i], galloc->buf_tallocs[i], GGML_BACKEND_BUFFER_USAGE_COMPUTE);
930935
if (galloc->buffers[i] == NULL) {

ggml/src/ggml-backend.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,9 +1400,14 @@ static bool ggml_backend_sched_alloc_splits(ggml_backend_sched_t sched) {
14001400
for (int i = 0; i < sched->n_backends; i++) {
14011401
ggml_backend_synchronize(sched->backends[i]);
14021402
}
1403+
#ifdef GGML_SCHED_NO_REALLOC
1404+
GGML_ABORT("%s: failured to allocate graph, but graph re-allocation is disabled by GGML_SCHED_NO_REALLOC\n", __func__);
1405+
#endif
1406+
14031407
#ifndef NDEBUG
14041408
GGML_LOG_DEBUG("%s: failed to allocate graph, reserving (backend_ids_changed = %d)\n", __func__, backend_ids_changed);
14051409
#endif
1410+
14061411
ggml_gallocr_reserve_n(sched->galloc, &sched->graph, sched->node_backend_ids, sched->leaf_backend_ids);
14071412
if (!ggml_gallocr_alloc_graph(sched->galloc, &sched->graph)) {
14081413
GGML_LOG_ERROR("%s: failed to allocate graph\n", __func__);

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_SCHED_NO_REALLOC)
200200
# TODO: repair known memory leaks
201201
llama_build_and_test(test-opt.cpp)
202202
endif()

0 commit comments

Comments
 (0)