From 28a528816791a2e1e79509b26a8875a4a657184e Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Sat, 27 May 2023 16:15:10 +0300 Subject: [PATCH] ggml : add ggml_tensor_overhead() --- examples/common-ggml.cpp | 4 ++-- include/ggml/ggml.h | 6 +++--- src/ggml.c | 4 ++++ 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/examples/common-ggml.cpp b/examples/common-ggml.cpp index 544c0b6447..0db2c034b3 100644 --- a/examples/common-ggml.cpp +++ b/examples/common-ggml.cpp @@ -509,7 +509,7 @@ ggml_cgraph ggml_graph_import(const char * fname, struct ggml_context ** ctx_dat // create the data context { - const size_t overhead = 1*GGML_TENSOR_OVERHEAD; + const size_t overhead = 1*ggml_tensor_overhead(); struct ggml_init_params params = { .mem_size = fsize + overhead, @@ -559,7 +559,7 @@ ggml_cgraph ggml_graph_import(const char * fname, struct ggml_context ** ctx_dat // create the data context { - const size_t overhead = (leafs + nodes)*GGML_TENSOR_OVERHEAD; + const size_t overhead = (leafs + nodes)*ggml_tensor_overhead(); struct ggml_init_params params = { .mem_size = size_eval + overhead, diff --git a/include/ggml/ggml.h b/include/ggml/ggml.h index 0c90f50641..5581382801 100644 --- a/include/ggml/ggml.h +++ b/include/ggml/ggml.h @@ -380,9 +380,6 @@ extern "C" { static const size_t GGML_TENSOR_SIZE = sizeof(struct ggml_tensor); - // use this to compute the memory overhead of a tensor - static const size_t GGML_TENSOR_OVERHEAD = (GGML_OBJECT_SIZE + GGML_TENSOR_SIZE + 16); - // computation graph struct ggml_cgraph { int n_nodes; @@ -444,6 +441,9 @@ extern "C" { // TODO: temporary until model loading of ggml examples is refactored GGML_API enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype); + // use this to compute the memory overhead of a tensor + GGML_API size_t ggml_tensor_overhead(void); + // main GGML_API struct ggml_context * ggml_init(struct ggml_init_params params); diff --git a/src/ggml.c b/src/ggml.c index febad650e6..14972464b5 100644 --- a/src/ggml.c +++ b/src/ggml.c @@ -3808,6 +3808,10 @@ enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype) { return wtype; } +size_t ggml_tensor_overhead(void) { + return GGML_OBJECT_SIZE + GGML_TENSOR_SIZE + 16; +} + static inline bool ggml_is_transposed(const struct ggml_tensor * tensor) { return tensor->nb[0] > tensor->nb[1]; }