Skip to content

Commit bdbda1b

Browse files
committed
ggml : sync ggml core (minor additions, e.g. ggml_get_tensor_by_name())
1 parent 66874d4 commit bdbda1b

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

ggml.c

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3494,7 +3494,7 @@ static bool GGML_IS_QUANTIZED[GGML_TYPE_COUNT] = {
34943494
};
34953495
static_assert(GGML_TYPE_COUNT == 13, "GGML_IS_QUANTIZED is outdated");
34963496

3497-
static const char * GGML_OP_LABEL[GGML_OP_COUNT] = {
3497+
static const char * GGML_OP_NAME[GGML_OP_COUNT] = {
34983498
"NONE",
34993499

35003500
"DUP",
@@ -3749,6 +3749,9 @@ const char * ggml_type_name(enum ggml_type type) {
37493749
return GGML_TYPE_NAME[type];
37503750
}
37513751

3752+
const char * ggml_op_name(enum ggml_op op) {
3753+
return GGML_OP_NAME[op];
3754+
}
37523755

37533756
size_t ggml_element_size(const struct ggml_tensor * tensor) {
37543757
return GGML_TYPE_SIZE[tensor->type];
@@ -4017,6 +4020,10 @@ size_t ggml_set_scratch(struct ggml_context * ctx, struct ggml_scratch scratch)
40174020
return result;
40184021
}
40194022

4023+
void ggml_set_no_alloc(struct ggml_context * ctx, bool no_alloc) {
4024+
ctx->no_alloc = no_alloc;
4025+
}
4026+
40204027
// IMPORTANT:
40214028
// when creating "opt" tensors, always save and load the scratch buffer
40224029
// this is an error prone process, but it is necessary to support inplace
@@ -4061,7 +4068,7 @@ struct ggml_tensor * ggml_new_tensor_impl(
40614068
struct ggml_object * const obj_new = (struct ggml_object *)(mem_buffer + cur_end);
40624069

40634070
if (ctx->scratch.data == NULL || data != NULL) {
4064-
size_needed += sizeof(struct ggml_tensor);
4071+
size_needed += GGML_TENSOR_SIZE;
40654072

40664073
if (cur_end + size_needed + GGML_OBJECT_SIZE > ctx->mem_size) {
40674074
GGML_PRINT("%s: not enough space in the context's memory pool (needed %zu, available %zu)\n",
@@ -4077,14 +4084,15 @@ struct ggml_tensor * ggml_new_tensor_impl(
40774084
};
40784085
} else {
40794086
if (ctx->scratch.offs + size_needed > ctx->scratch.size) {
4080-
GGML_PRINT("%s: not enough space in the scratch memory\n", __func__);
4087+
GGML_PRINT("%s: not enough space in the scratch memory pool (needed %zu, available %zu)\n",
4088+
__func__, ctx->scratch.offs + size_needed, ctx->scratch.size);
40814089
assert(false);
40824090
return NULL;
40834091
}
40844092

4085-
if (cur_end + sizeof(struct ggml_tensor) + GGML_OBJECT_SIZE > ctx->mem_size) {
4093+
if (cur_end + GGML_TENSOR_SIZE + GGML_OBJECT_SIZE > ctx->mem_size) {
40864094
GGML_PRINT("%s: not enough space in the context's memory pool (needed %zu, available %zu)\n",
4087-
__func__, cur_end + sizeof(struct ggml_tensor) + GGML_OBJECT_SIZE, ctx->mem_size);
4095+
__func__, cur_end + GGML_TENSOR_SIZE + GGML_OBJECT_SIZE, ctx->mem_size);
40884096
assert(false);
40894097
return NULL;
40904098
}
@@ -4093,7 +4101,7 @@ struct ggml_tensor * ggml_new_tensor_impl(
40934101

40944102
*obj_new = (struct ggml_object) {
40954103
.offs = cur_end + GGML_OBJECT_SIZE,
4096-
.size = sizeof(struct ggml_tensor),
4104+
.size = GGML_TENSOR_SIZE,
40974105
.next = NULL,
40984106
};
40994107

@@ -13792,11 +13800,19 @@ static void ggml_visit_parents(struct ggml_cgraph * cgraph, struct ggml_tensor *
1379213800
// reached a leaf node, not part of the gradient graph (e.g. a constant)
1379313801
GGML_ASSERT(cgraph->n_leafs < GGML_MAX_NODES);
1379413802

13803+
if (strlen(node->name) == 0) {
13804+
snprintf(node->name, sizeof(node->name), "leaf_%d", cgraph->n_leafs);
13805+
}
13806+
1379513807
cgraph->leafs[cgraph->n_leafs] = node;
1379613808
cgraph->n_leafs++;
1379713809
} else {
1379813810
GGML_ASSERT(cgraph->n_nodes < GGML_MAX_NODES);
1379913811

13812+
if (strlen(node->name) == 0) {
13813+
snprintf(node->name, sizeof(node->name), "node_%d", cgraph->n_nodes);
13814+
}
13815+
1380013816
cgraph->nodes[cgraph->n_nodes] = node;
1380113817
cgraph->grads[cgraph->n_nodes] = node->grad;
1380213818
cgraph->n_nodes++;
@@ -14510,6 +14526,18 @@ void ggml_graph_reset(struct ggml_cgraph * cgraph) {
1451014526
}
1451114527
}
1451214528

14529+
struct ggml_tensor * ggml_get_tensor_by_name(struct ggml_cgraph * cgraph, const char * name) {
14530+
for (int i = 0; i < cgraph->n_nodes; i++) {
14531+
struct ggml_tensor * node = cgraph->nodes[i];
14532+
14533+
if (strcmp(node->name, name) == 0) {
14534+
return node;
14535+
}
14536+
}
14537+
14538+
return NULL;
14539+
}
14540+
1451314541
void ggml_graph_print(const struct ggml_cgraph * cgraph) {
1451414542
int64_t perf_total_per_op_us[GGML_OP_COUNT] = {0};
1451514543

@@ -14527,7 +14555,7 @@ void ggml_graph_print(const struct ggml_cgraph * cgraph) {
1452714555
GGML_PRINT(" - %3d: [ %5" PRId64 ", %5" PRId64 ", %5" PRId64 "] %16s %s (%3d) cpu = %7.3f / %7.3f ms, wall = %7.3f / %7.3f ms\n",
1452814556
i,
1452914557
node->ne[0], node->ne[1], node->ne[2],
14530-
GGML_OP_LABEL[node->op], node->is_param ? "x" : node->grad ? "g" : " ", node->perf_runs,
14558+
GGML_OP_NAME[node->op], node->is_param ? "x" : node->grad ? "g" : " ", node->perf_runs,
1453114559
(double) node->perf_cycles / (double) ggml_cycles_per_ms(),
1453214560
(double) node->perf_cycles / (double) ggml_cycles_per_ms() / (double) node->perf_runs,
1453314561
(double) node->perf_time_us / 1000.0,
@@ -14541,15 +14569,15 @@ void ggml_graph_print(const struct ggml_cgraph * cgraph) {
1454114569
GGML_PRINT(" - %3d: [ %5" PRId64 ", %5" PRId64 "] %8s\n",
1454214570
i,
1454314571
node->ne[0], node->ne[1],
14544-
GGML_OP_LABEL[node->op]);
14572+
GGML_OP_NAME[node->op]);
1454514573
}
1454614574

1454714575
for (int i = 0; i < GGML_OP_COUNT; i++) {
1454814576
if (perf_total_per_op_us[i] == 0) {
1454914577
continue;
1455014578
}
1455114579

14552-
GGML_PRINT("perf_total_per_op_us[%16s] = %7.3f ms\n", GGML_OP_LABEL[i], (double) perf_total_per_op_us[i] / 1000.0);
14580+
GGML_PRINT("perf_total_per_op_us[%16s] = %7.3f ms\n", GGML_OP_NAME[i], (double) perf_total_per_op_us[i] / 1000.0);
1455314581
}
1455414582

1455514583
GGML_PRINT("========================================\n");

ggml.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
#define GGML_MAX_PARAMS 256
199199
#define GGML_MAX_CONTEXTS 64
200200
#define GGML_MAX_OPT 4
201+
#define GGML_MAX_NAME 32
201202
#define GGML_DEFAULT_N_THREADS 4
202203

203204
#define GGML_ASSERT(x) \
@@ -372,11 +373,16 @@ extern "C" {
372373

373374
void * data;
374375

375-
char name[32];
376+
char name[GGML_MAX_NAME];
376377

377378
char padding[16];
378379
};
379380

381+
static const size_t GGML_TENSOR_SIZE = sizeof(struct ggml_tensor);
382+
383+
// use this to compute the memory overhead of a tensor
384+
static const size_t GGML_TENSOR_OVERHEAD = (GGML_OBJECT_SIZE + GGML_TENSOR_SIZE + 16);
385+
380386
// computation graph
381387
struct ggml_cgraph {
382388
int n_nodes;
@@ -429,6 +435,7 @@ extern "C" {
429435
GGML_API float ggml_type_sizef(enum ggml_type type); // ggml_type_size()/ggml_blck_size() as float
430436

431437
GGML_API const char * ggml_type_name(enum ggml_type type);
438+
GGML_API const char * ggml_op_name (enum ggml_op op);
432439

433440
GGML_API size_t ggml_element_size(const struct ggml_tensor * tensor);
434441

@@ -445,6 +452,7 @@ extern "C" {
445452
GGML_API size_t ggml_used_mem(const struct ggml_context * ctx);
446453

447454
GGML_API size_t ggml_set_scratch(struct ggml_context * ctx, struct ggml_scratch scratch);
455+
GGML_API void ggml_set_no_alloc(struct ggml_context * ctx, bool no_alloc);
448456

449457
GGML_API struct ggml_tensor * ggml_new_tensor(
450458
struct ggml_context * ctx,
@@ -970,6 +978,8 @@ extern "C" {
970978
GGML_API void ggml_graph_compute(struct ggml_context * ctx, struct ggml_cgraph * cgraph);
971979
GGML_API void ggml_graph_reset (struct ggml_cgraph * cgraph);
972980

981+
GGML_API struct ggml_tensor * ggml_get_tensor_by_name(struct ggml_cgraph * cgraph, const char * name);
982+
973983
// print info and performance information for the graph
974984
GGML_API void ggml_graph_print(const struct ggml_cgraph * cgraph);
975985

0 commit comments

Comments
 (0)