.dot file of ggml_graph can not be generated to .png file (original) (raw)

Hi, I want to generate a picture of the grapj. And I uncommented this 2 lines in "llama.cpp", so that to run the function ggml_graph_dump_dot()

    //if (n_past%100 == 0) {
        ggml_graph_print   (&gf);
        ggml_graph_dump_dot(&gf, NULL, "gpt-2.dot");
    //}

And I got a file named gpt-2.dot
But when I run command in python:

from graphviz import Digraph
import sys
sys.setrecursionlimit(300000) 

import pydot
import os
(graph,) = pydot.graph_from_dot_file("D:\\PIQ\\llama.cpp\\build\\examples\\main\\gpt-2.dot")
graph.write_png("gpt-2.png")

I get the error message: Expect '{' but got '['
So I modifid the function ggml_graph_dump_dot() in ggml.c like this:

void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * gf, const char * filename) {
    char color[16];

    FILE * fp = fopen(filename, "w");
    GGML_ASSERT(fp);

    fprintf(fp, "digraph G {\n");
    fprintf(fp, "  newrank = true;\n");
    fprintf(fp, "  rankdir = LR;\n");

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

        if (ggml_graph_get_parent(gb, node) != NULL) {
            continue;
        }

        if (node->is_param) {
            snprintf(color, sizeof(color), "yellow");
        } else if (node->grad) {
            if (ggml_graph_find(gf, node)) {
                snprintf(color, sizeof(color), "green");
            } else {
                snprintf(color, sizeof(color), "lightblue");
            }
        } else {
            snprintf(color, sizeof(color), "white");
        }

        fprintf(fp, "  \"%p\" { \
style = filled; fillcolor = %s; shape = record; \
label=\"%d [%d, %d] | <x>%s",
                (void *) node, color,
                i, node->ne[0], node->ne[1],
                GGML_OP_SYMBOL[node->op]);

        if (node->grad) {
            fprintf(fp, " | <g>%s\"; }\n", GGML_OP_SYMBOL[node->grad->op]);
        } else {
            fprintf(fp, "\"; }\n");
        }
    }

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

        snprintf(color, sizeof(color), "pink");

        if (ggml_nelements(node) == 1) {
            fprintf(fp, "  \"%p\" { \
style = filled; fillcolor = %s; shape = record; \
label=\"<x>%.1e\"; }\n",
                    (void *) node, color, ggml_get_f32_1d(node, 0));
        } else {
            fprintf(fp, "  \"%p\" { \
style = filled; fillcolor = %s; shape = record; \
label=\"<x>CONST %d [%d, %d]\"; }\n",
                    (void *) node, color,
                    i, node->ne[0], node->ne[1]);
        }
    }

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

        struct ggml_tensor * parent = ggml_graph_get_parent(gb, node);

        if (node->src0) {
            struct ggml_tensor * parent0 = ggml_graph_get_parent(gb, node->src0);

            fprintf(fp, "  \"%p\":%s -> \"%p\":%s { arrowhead = %s; style = %s; label = \"x\"; }\n",
                    parent0 ? (void *) parent0 : (void *) node->src0,
                    parent0 ? "g" : "x",
                    parent ? (void *) parent : (void *) node,
                    parent ? "g" : "x",
                    parent ? "empty" : "vee",
                    parent ? "dashed" : "solid");
        }

        if (node->src1) {
            struct ggml_tensor * parent1 = ggml_graph_get_parent(gb, node->src1);

            fprintf(fp, "  \"%p\":%s -> \"%p\":%s { arrowhead = %s; style = %s; label = \"y\"; }\n",
                    parent1 ? (void *) parent1 : (void *) node->src1,
                    parent1 ? "g" : "x",
                    parent ? (void *) parent : (void *) node,
                    parent ? "g" : "x",
                    parent ? "empty" : "vee",
                    parent ? "dashed" : "solid");
        }
    }

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

        if (node->src0) {
            fprintf(fp, "  \"%p\":%s -> \"%p\":%s [ label = \"x\"; ]\n",
                    (void *) node->src0, "x",
                    (void *) node, "x");
        }

        if (node->src1) {
            fprintf(fp, "  \"%p\":%s -> \"%p\":%s [ label = \"y\"; ]\n",
                    (void *) node->src1, "x",
                    (void *) node, "x");
        }
    }

    fprintf(fp, "}\n");

    fclose(fp);

    GGML_PRINT("%s: dot -Tpng %s -o %s.png && open %s.png\n", __func__, filename, filename, filename);
}

To replace the '[' and ] to '{' and '}'
Then it doesn't have new error , but the process blocked, can not stop.Blocked in here:
image

And I also run command in windows powershell, but it still blocked like this:
image

same in wsl:

image

So can anybody know what can I do? Thanks

Attach is the .dot file generated by me, you can modify the suffix from gpt-2.txt to gpt-2.dot. The model size is 7B-version.

gpt-2.txt