Skip to content

Commit

Permalink
ggml : add ggml_tensor_overhead()
Browse files Browse the repository at this point in the history
  • Loading branch information
ggerganov committed May 27, 2023
1 parent cbe87bd commit 28a5288
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 2 additions & 2 deletions examples/common-ggml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions include/ggml/ggml.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/ggml.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down

0 comments on commit 28a5288

Please sign in to comment.