Skip to content

Commit 80be908

Browse files
committed
ggml : sync llama.cpp (tensor names)
1 parent 0a63fc0 commit 80be908

File tree

2 files changed

+98
-40
lines changed

2 files changed

+98
-40
lines changed

include/ggml/ggml.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ extern "C" {
563563

564564
GGML_API const char * ggml_get_name(const struct ggml_tensor * tensor);
565565
GGML_API struct ggml_tensor * ggml_set_name(struct ggml_tensor * tensor, const char * name);
566+
GGML_API struct ggml_tensor * ggml_format_name(struct ggml_tensor * tensor, const char * fmt, ...);
566567

567568
//
568569
// operations on tensors with backpropagation

src/ggml.c

Lines changed: 97 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <stdio.h>
2525
#include <float.h>
2626
#include <limits.h>
27+
#include <stdarg.h>
2728

2829
#ifdef GGML_USE_METAL
2930
#include <unistd.h>
@@ -4734,10 +4735,19 @@ struct ggml_tensor * ggml_set_name(struct ggml_tensor * tensor, const char * nam
47344735
return tensor;
47354736
}
47364737

4738+
struct ggml_tensor * ggml_format_name(struct ggml_tensor * tensor, const char * fmt, ...) {
4739+
va_list args;
4740+
va_start(args, fmt);
4741+
vsnprintf(tensor->name, sizeof(tensor->name), fmt, args);
4742+
va_end(args);
4743+
return tensor;
4744+
}
4745+
47374746
struct ggml_tensor * ggml_view_tensor(
47384747
struct ggml_context * ctx,
47394748
const struct ggml_tensor * src) {
47404749
struct ggml_tensor * result = ggml_new_tensor_impl(ctx, src->type, src->n_dims, src->ne, src->data);
4750+
ggml_format_name(result, "%s (view)", src->name);
47414751

47424752
result->nb[0] = src->nb[0];
47434753
result->nb[1] = src->nb[1];
@@ -5899,6 +5909,11 @@ struct ggml_tensor * ggml_cpy_impl(
58995909

59005910
// make a view of the destination
59015911
struct ggml_tensor * result = ggml_view_tensor(ctx, b);
5912+
if (strlen(b->name) > 0) {
5913+
ggml_format_name(result, "%s (copy of %s)", b->name, a->name);
5914+
} else {
5915+
ggml_format_name(result, "%s (copy)", a->name);
5916+
}
59025917

59035918
result->op = GGML_OP_CPY;
59045919
result->grad = is_node ? ggml_dup_tensor(ctx, result) : NULL;
@@ -5935,6 +5950,7 @@ struct ggml_tensor * ggml_cont_impl(
59355950
}
59365951

59375952
struct ggml_tensor * result = inplace ? ggml_view_tensor(ctx, a) : ggml_dup_tensor(ctx, a);
5953+
ggml_format_name(result, "%s (cont)", a->name);
59385954

59395955
result->op = GGML_OP_CONT;
59405956
result->grad = is_node ? ggml_dup_tensor(ctx, result) : NULL;
@@ -5978,6 +5994,7 @@ struct ggml_tensor * ggml_reshape(
59785994
}
59795995

59805996
struct ggml_tensor * result = ggml_new_tensor_impl(ctx, a->type, b->n_dims, b->ne, a->data);
5997+
ggml_format_name(result, "%s (reshaped)", a->name);
59815998

59825999
result->op = GGML_OP_RESHAPE;
59836000
result->grad = is_node ? ggml_dup_tensor(ctx, result) : NULL;
@@ -6002,6 +6019,7 @@ struct ggml_tensor * ggml_reshape_1d(
60026019

60036020
const int64_t ne[1] = { ne0 };
60046021
struct ggml_tensor * result = ggml_new_tensor_impl(ctx, a->type, 1, ne, a->data);
6022+
ggml_format_name(result, "%s (reshaped)", a->name);
60056023

60066024
result->op = GGML_OP_RESHAPE;
60076025
result->grad = is_node ? ggml_dup_tensor(ctx, result) : NULL;
@@ -6027,6 +6045,7 @@ struct ggml_tensor * ggml_reshape_2d(
60276045

60286046
const int64_t ne[2] = { ne0, ne1 };
60296047
struct ggml_tensor * result = ggml_new_tensor_impl(ctx, a->type, 2, ne, a->data);
6048+
ggml_format_name(result, "%s (reshaped)", a->name);
60306049

60316050
result->op = GGML_OP_RESHAPE;
60326051
result->grad = is_node ? ggml_dup_tensor(ctx, result) : NULL;
@@ -6053,6 +6072,7 @@ struct ggml_tensor * ggml_reshape_3d(
60536072

60546073
const int64_t ne[3] = { ne0, ne1, ne2 };
60556074
struct ggml_tensor * result = ggml_new_tensor_impl(ctx, a->type, 3, ne, a->data);
6075+
ggml_format_name(result, "%s (reshaped)", a->name);
60566076

60576077
result->op = GGML_OP_RESHAPE;
60586078
result->grad = is_node ? ggml_dup_tensor(ctx, result) : NULL;
@@ -6081,6 +6101,7 @@ struct ggml_tensor * ggml_reshape_4d(
60816101

60826102
const int64_t ne[4] = { ne0, ne1, ne2, ne3 };
60836103
struct ggml_tensor * result = ggml_new_tensor_impl(ctx, a->type, 4, ne, a->data);
6104+
ggml_format_name(result, "%s (reshaped)", a->name);
60846105

60856106
result->op = GGML_OP_RESHAPE;
60866107
result->grad = is_node ? ggml_dup_tensor(ctx, result) : NULL;
@@ -6105,10 +6126,12 @@ struct ggml_tensor * ggml_view_1d(
61056126
}
61066127

61076128
struct ggml_tensor * result = ggml_new_tensor_impl(ctx, a->type, 1, &ne0, (char *) a->data + offset);
6129+
ggml_format_name(result, "%s (view)", a->name);
61086130

61096131
ggml_scratch_save(ctx);
61106132

61116133
struct ggml_tensor * offs = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 2);
6134+
ggml_set_name(offs, "offset");
61126135
memcpy(offs->data, &offset, 2*sizeof(int32_t));
61136136

61146137
ggml_scratch_load(ctx);
@@ -6141,10 +6164,12 @@ struct ggml_tensor * ggml_view_2d(
61416164
const int64_t ne[GGML_MAX_DIMS] = { ne0, ne1, 1, 1 };
61426165

61436166
struct ggml_tensor * result = ggml_new_tensor_impl(ctx, a->type, 2, ne, (char *) a->data + offset);
6167+
ggml_format_name(result, "%s (view)", a->name);
61446168

61456169
ggml_scratch_save(ctx);
61466170

61476171
struct ggml_tensor * offs = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 2);
6172+
ggml_set_name(offs, "offset");
61486173
memcpy(offs->data, &offset, 2*sizeof(int32_t));
61496174

61506175
ggml_scratch_load(ctx);
@@ -6183,10 +6208,12 @@ struct ggml_tensor * ggml_view_3d(
61836208
const int64_t ne[GGML_MAX_DIMS] = { ne0, ne1, ne2, 1 };
61846209

61856210
struct ggml_tensor * result = ggml_new_tensor_impl(ctx, a->type, 3, ne, (char *) a->data + offset);
6211+
ggml_format_name(result, "%s (view)", a->name);
61866212

61876213
ggml_scratch_save(ctx);
61886214

61896215
struct ggml_tensor * offs = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 2);
6216+
ggml_set_name(offs, "offset");
61906217
memcpy(offs->data, &offset, 2*sizeof(int32_t));
61916218

61926219
ggml_scratch_load(ctx);
@@ -6227,10 +6254,12 @@ struct ggml_tensor * ggml_view_4d(
62276254
const int64_t ne[GGML_MAX_DIMS] = { ne0, ne1, ne2, ne3 };
62286255

62296256
struct ggml_tensor * result = ggml_new_tensor_impl(ctx, a->type, 4, ne, (char *) a->data + offset);
6257+
ggml_format_name(result, "%s (view)", a->name);
62306258

62316259
ggml_scratch_save(ctx);
62326260

62336261
struct ggml_tensor * offs = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 2);
6262+
ggml_set_name(offs, "offset");
62346263
memcpy(offs->data, &offset, 2*sizeof(int32_t));
62356264

62366265
ggml_scratch_load(ctx);
@@ -6276,6 +6305,7 @@ struct ggml_tensor * ggml_permute(
62766305
}
62776306

62786307
struct ggml_tensor * result = ggml_view_tensor(ctx, a);
6308+
ggml_format_name(result, "%s (permuted)", a->name);
62796309

62806310
int ne[GGML_MAX_DIMS];
62816311
int nb[GGML_MAX_DIMS];
@@ -6335,6 +6365,7 @@ struct ggml_tensor * ggml_transpose(
63356365
}
63366366

63376367
struct ggml_tensor * result = ggml_view_tensor(ctx, a);
6368+
ggml_format_name(result, "%s (transposed)", a->name);
63386369

63396370
result->ne[0] = a->ne[1];
63406371
result->ne[1] = a->ne[0];
@@ -16004,7 +16035,7 @@ static void ggml_visit_parents(struct ggml_cgraph * cgraph, struct ggml_tensor *
1600416035
GGML_ASSERT(cgraph->n_leafs < GGML_MAX_NODES);
1600516036

1600616037
if (strlen(node->name) == 0) {
16007-
snprintf(node->name, sizeof(node->name), "leaf_%d", cgraph->n_leafs);
16038+
ggml_format_name(node, "leaf_%d", cgraph->n_leafs);
1600816039
}
1600916040

1601016041
cgraph->leafs[cgraph->n_leafs] = node;
@@ -16013,7 +16044,7 @@ static void ggml_visit_parents(struct ggml_cgraph * cgraph, struct ggml_tensor *
1601316044
GGML_ASSERT(cgraph->n_nodes < GGML_MAX_NODES);
1601416045

1601516046
if (strlen(node->name) == 0) {
16016-
snprintf(node->name, sizeof(node->name), "node_%d", cgraph->n_nodes);
16047+
ggml_format_name(node, "node_%d", cgraph->n_nodes);
1601716048
}
1601816049

1601916050
cgraph->nodes[cgraph->n_nodes] = node;
@@ -17397,6 +17428,26 @@ static struct ggml_tensor * ggml_graph_get_parent(const struct ggml_cgraph * cgr
1739717428
return NULL;
1739817429
}
1739917430

17431+
static void ggml_graph_dump_dot_node_edge(FILE * fp, const struct ggml_cgraph * gb, struct ggml_tensor * node, struct ggml_tensor * parent, const char * label) {
17432+
struct ggml_tensor * gparent = ggml_graph_get_parent(gb, node);
17433+
struct ggml_tensor * gparent0 = ggml_graph_get_parent(gb, parent);
17434+
fprintf(fp, " \"%p\":%s -> \"%p\":%s [ arrowhead = %s; style = %s; label = \"%s\"; ]\n",
17435+
gparent0 ? (void *) gparent0 : (void *) parent,
17436+
gparent0 ? "g" : "x",
17437+
gparent ? (void *) gparent : (void *) node,
17438+
gparent ? "g" : "x",
17439+
gparent ? "empty" : "vee",
17440+
gparent ? "dashed" : "solid",
17441+
label);
17442+
}
17443+
17444+
static void ggml_graph_dump_dot_leaf_edge(FILE * fp, struct ggml_tensor * node, struct ggml_tensor * parent, const char * label) {
17445+
fprintf(fp, " \"%p\":%s -> \"%p\":%s [ label = \"%s\"; ]\n",
17446+
(void *) parent, "x",
17447+
(void *) node, "x",
17448+
label);
17449+
}
17450+
1740017451
void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * gf, const char * filename) {
1740117452
char color[16];
1740217453

@@ -17432,7 +17483,9 @@ void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph
1743217483
(void *) node, color);
1743317484

1743417485
if (strlen(node->name) > 0) {
17435-
fprintf(fp, "%s |", node->name);
17486+
fprintf(fp, "%s (%s)|", node->name, ggml_type_name(node->type));
17487+
} else {
17488+
fprintf(fp, "(%s)|", ggml_type_name(node->type));
1743617489
}
1743717490

1743817491
if (node->n_dims == 2) {
@@ -17441,7 +17494,6 @@ void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph
1744117494
fprintf(fp, "%d [%" PRId64 ", %" PRId64 ", %" PRId64 "] | <x>%s", i, node->ne[0], node->ne[1], node->ne[2], GGML_OP_SYMBOL[node->op]);
1744217495
}
1744317496

17444-
1744517497
if (node->grad) {
1744617498
fprintf(fp, " | <g>%s\"; ]\n", GGML_OP_SYMBOL[node->grad->op]);
1744717499
} else {
@@ -17460,65 +17512,70 @@ void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph
1746017512
(void *) node, color);
1746117513

1746217514
if (strlen(node->name) > 0) {
17463-
fprintf(fp, "%s | ", node->name);
17515+
fprintf(fp, "%s (%s)|", node->name, ggml_type_name(node->type));
17516+
} else {
17517+
fprintf(fp, "(%s)|", ggml_type_name(node->type));
1746417518
}
17465-
if (ggml_nelements(node) == 1) {
17466-
if (node->type == GGML_TYPE_I8 || node->type == GGML_TYPE_I16 || node->type == GGML_TYPE_I32) {
17467-
fprintf(fp, "%d", ggml_get_i32_1d(node, 0));
17468-
}
17469-
else {
17470-
fprintf(fp, "%.1e", (double)ggml_get_f32_1d(node, 0));
17519+
17520+
fprintf(fp, "CONST %d [%" PRId64 ", %" PRId64 "]", i, node->ne[0], node->ne[1]);
17521+
if (ggml_nelements(node) < 5) {
17522+
fprintf(fp, " | (");
17523+
for (int j = 0; j < ggml_nelements(node); j++) {
17524+
if (node->type == GGML_TYPE_I8 || node->type == GGML_TYPE_I16 || node->type == GGML_TYPE_I32) {
17525+
fprintf(fp, "%d", ggml_get_i32_1d(node, j));
17526+
}
17527+
else if (node->type == GGML_TYPE_F32 || node->type == GGML_TYPE_F16) {
17528+
fprintf(fp, "%.1e", (double)ggml_get_f32_1d(node, j));
17529+
}
17530+
else {
17531+
fprintf(fp, "#");
17532+
}
17533+
if (j < ggml_nelements(node) - 1) {
17534+
fprintf(fp, ", ");
17535+
}
1747117536
}
17472-
}
17473-
else {
17474-
fprintf(fp, "CONST %d [%" PRId64 ", %" PRId64 "]", i, node->ne[0], node->ne[1]);
17537+
fprintf(fp, ")");
1747517538
}
1747617539
fprintf(fp, "\"; ]\n");
1747717540
}
1747817541

1747917542
for (int i = 0; i < gb->n_nodes; i++) {
1748017543
struct ggml_tensor * node = gb->nodes[i];
1748117544

17482-
struct ggml_tensor * parent = ggml_graph_get_parent(gb, node);
17483-
1748417545
if (node->src0) {
17485-
struct ggml_tensor * parent0 = ggml_graph_get_parent(gb, node->src0);
17486-
17487-
fprintf(fp, " \"%p\":%s -> \"%p\":%s [ arrowhead = %s; style = %s; label = \"x\"; ]\n",
17488-
parent0 ? (void *) parent0 : (void *) node->src0,
17489-
parent0 ? "g" : "x",
17490-
parent ? (void *) parent : (void *) node,
17491-
parent ? "g" : "x",
17492-
parent ? "empty" : "vee",
17493-
parent ? "dashed" : "solid");
17546+
ggml_graph_dump_dot_node_edge(fp, gb, node, node->src0, "x");
1749417547
}
1749517548

1749617549
if (node->src1) {
17497-
struct ggml_tensor * parent1 = ggml_graph_get_parent(gb, node->src1);
17498-
17499-
fprintf(fp, " \"%p\":%s -> \"%p\":%s [ arrowhead = %s; style = %s; label = \"y\"; ]\n",
17500-
parent1 ? (void *) parent1 : (void *) node->src1,
17501-
parent1 ? "g" : "x",
17502-
parent ? (void *) parent : (void *) node,
17503-
parent ? "g" : "x",
17504-
parent ? "empty" : "vee",
17505-
parent ? "dashed" : "solid");
17550+
ggml_graph_dump_dot_node_edge(fp, gb, node, node->src1, "y");
17551+
}
17552+
17553+
for (int j = 0; j < GGML_MAX_OPT; j++) {
17554+
if (node->opt[j]) {
17555+
char label[16];
17556+
snprintf(label, sizeof(label), "opt %d", j);
17557+
ggml_graph_dump_dot_node_edge(fp, gb, node, node->opt[j], label);
17558+
}
1750617559
}
1750717560
}
1750817561

1750917562
for (int i = 0; i < gb->n_leafs; i++) {
1751017563
struct ggml_tensor * node = gb->leafs[i];
1751117564

1751217565
if (node->src0) {
17513-
fprintf(fp, " \"%p\":%s -> \"%p\":%s [ label = \"x\"; ]\n",
17514-
(void *) node->src0, "x",
17515-
(void *) node, "x");
17566+
ggml_graph_dump_dot_leaf_edge(fp, node, node->src0, "x");
1751617567
}
1751717568

1751817569
if (node->src1) {
17519-
fprintf(fp, " \"%p\":%s -> \"%p\":%s [ label = \"y\"; ]\n",
17520-
(void *) node->src1, "x",
17521-
(void *) node, "x");
17570+
ggml_graph_dump_dot_leaf_edge(fp, node, node->src1, "y");
17571+
}
17572+
17573+
for (int j = 0; j < GGML_MAX_OPT; j++) {
17574+
if (node->opt[j]) {
17575+
char label[16];
17576+
snprintf(label, sizeof(label), "opt %d", j);
17577+
ggml_graph_dump_dot_leaf_edge(fp, node, node->opt[j], label);
17578+
}
1752217579
}
1752317580
}
1752417581

0 commit comments

Comments
 (0)