|
| 1 | +#include "ggml-profile.h" |
| 2 | + |
| 3 | +#include <stdint.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <stdio.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +#include <string> |
| 9 | +#include <chrono> |
| 10 | + |
| 11 | +#ifdef GGML_GRAPH_PROFILER |
| 12 | + |
| 13 | +struct ggml_profile_output { |
| 14 | + const char * prefix; |
| 15 | + FILE * stream; |
| 16 | +}; |
| 17 | + |
| 18 | +extern "C" void ggml_graph_profile_init(struct ggml_cgraph *cg, int n_threads) |
| 19 | +{ |
| 20 | + // TODO: make this a param |
| 21 | + const char *env = getenv("GGML_GRAPH_PROFILE"); |
| 22 | + if (!env) { return; } |
| 23 | + |
| 24 | + // The number of threads may change between passes (pp vs tg). |
| 25 | + // Allocate for max_n_threads for simplicity for now. |
| 26 | + // TODO: use aligned allocator |
| 27 | + |
| 28 | + size_t node_size = sizeof(struct ggml_profile_timing) * GGML_MAX_N_THREADS; |
| 29 | + size_t pvec_size = sizeof(std::intptr_t) * cg->n_nodes; |
| 30 | + size_t time_size = node_size * cg->n_nodes; |
| 31 | + size_t t_size = pvec_size + time_size + sizeof(ggml_profile_output) + sizeof(ggml_profile_data); |
| 32 | + |
| 33 | + uint8_t * ptr = (uint8_t *) malloc(t_size); |
| 34 | + if (!ptr) { |
| 35 | + fprintf(stderr, "ggml-profile: failed to allocate profiling data : n_threads %d n_nodes %d\n", n_threads, cg->n_nodes); |
| 36 | + return; |
| 37 | + } |
| 38 | + memset(ptr, 0, t_size); |
| 39 | + |
| 40 | + // init all pointers |
| 41 | + cg->prof = (ggml_profile_data *) ptr; ptr += sizeof(ggml_profile_data); |
| 42 | + cg->prof->output = (ggml_profile_output *) ptr; ptr += sizeof(ggml_profile_output); |
| 43 | + cg->prof->timing = (ggml_profile_timing **) ptr; ptr += pvec_size; |
| 44 | + for (int i=0; i < cg->n_nodes; i++) { |
| 45 | + cg->prof->timing[i] = (struct ggml_profile_timing *) ptr; ptr += node_size; |
| 46 | + } |
| 47 | + |
| 48 | + // init the output |
| 49 | + ggml_profile_output *out = cg->prof->output; |
| 50 | + if (!strcmp("stderr", env) || !strcmp("1", env)) { |
| 51 | + out->prefix = "ggml-profile:"; |
| 52 | + out->stream = stderr; |
| 53 | + } else { |
| 54 | + out->prefix = ""; |
| 55 | + out->stream = fopen(env, "w"); |
| 56 | + } |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +extern "C" void ggml_graph_profile_start(struct ggml_cgraph *cg, int n_threads) |
| 61 | +{ |
| 62 | + if (!cg->prof) { ggml_graph_profile_init(cg, n_threads); } |
| 63 | + if (!cg->prof) { return; } |
| 64 | +} |
| 65 | + |
| 66 | +static inline int ggml_profile_format_tensor_dims(char *str, struct ggml_tensor *t) |
| 67 | +{ |
| 68 | + return sprintf(str, "%d:%d:%d:%d", |
| 69 | + (int) t->ne[0], (int) t->ne[1], (int) t->ne[3], (int) t->ne[3]); |
| 70 | +} |
| 71 | + |
| 72 | +static inline void ggml_profile_format_op_dims(char *str, struct ggml_tensor *t) |
| 73 | +{ |
| 74 | + char *p = str; |
| 75 | + |
| 76 | + // append src0 and src1 (if any) |
| 77 | + if (t->src[0]) { |
| 78 | + p += ggml_profile_format_tensor_dims(p, t->src[0]); |
| 79 | + |
| 80 | + for (int i = 1; i < GGML_MAX_SRC && t->src[i]; i++) { |
| 81 | + p += sprintf(p, " x "); |
| 82 | + p += ggml_profile_format_tensor_dims(p, t->src[i]); |
| 83 | + } |
| 84 | + |
| 85 | + p += sprintf(p, " -> "); |
| 86 | + } |
| 87 | + |
| 88 | + // format self dims separately for better visual alignment |
| 89 | + char self[64]; |
| 90 | + ggml_profile_format_tensor_dims(self, t); |
| 91 | + |
| 92 | + p += sprintf(p, "%12s", self); |
| 93 | +} |
| 94 | + |
| 95 | +static inline void ggml_profile_format_op_types(char *str, struct ggml_tensor *t) |
| 96 | +{ |
| 97 | + char *p = str; |
| 98 | + |
| 99 | + // append src0 and src1 (if any) |
| 100 | + if (t->src[0]) { |
| 101 | + p += sprintf(p, "%s", ggml_type_name(t->src[0]->type)); |
| 102 | + |
| 103 | + for (int i = 1; i < GGML_MAX_SRC && t->src[i]; i++) { |
| 104 | + p += sprintf(p, " x "); |
| 105 | + p += sprintf(p, "%s", ggml_type_name(t->src[i]->type)); |
| 106 | + } |
| 107 | + |
| 108 | + p += sprintf(p, " -> "); |
| 109 | + } |
| 110 | + |
| 111 | + p += sprintf(p, "%3s", ggml_type_name(t->type)); |
| 112 | +} |
| 113 | + |
| 114 | +extern "C" void ggml_graph_profile_finish(struct ggml_cgraph *cg, int n_threads) |
| 115 | +{ |
| 116 | + if (!cg->prof) { return; } |
| 117 | + |
| 118 | + ggml_profile_output *out = cg->prof->output; |
| 119 | + |
| 120 | + fprintf(out->stream, "%s| node idx | op name | proc (nsec) | sync (nsec) | total (nsec) | op dims | op types | tensor name |\n", out->prefix); |
| 121 | + fprintf(out->stream, "%s| -------: | :------ | ----------: | ----------: | -----------: | ------: | -------: | ----------: |\n", out->prefix); |
| 122 | + |
| 123 | + char dims[64 * GGML_MAX_SRC]; |
| 124 | + char types[16 * GGML_MAX_SRC]; |
| 125 | + |
| 126 | + for (int i = 0; i < cg->n_nodes; i++) { |
| 127 | + uint64_t p_nsec = 0; |
| 128 | + uint64_t s_nsec = 0; |
| 129 | + uint64_t t_nsec = 0; |
| 130 | + |
| 131 | + // add up per thread counters and reset them |
| 132 | + for (int t=0; t < n_threads; t++) { |
| 133 | + ggml_profile_timing &timing = cg->prof->timing[i][t]; |
| 134 | + |
| 135 | + p_nsec += timing.nsec[GGML_PROF_OP_SYNC] - timing.nsec[GGML_PROF_OP_START]; |
| 136 | + s_nsec += timing.nsec[GGML_PROF_OP_END] - timing.nsec[GGML_PROF_OP_SYNC]; |
| 137 | + t_nsec += timing.nsec[GGML_PROF_OP_END] - timing.nsec[GGML_PROF_OP_START]; |
| 138 | + |
| 139 | + timing.nsec[GGML_PROF_OP_START] = 0; |
| 140 | + timing.nsec[GGML_PROF_OP_SYNC] = 0; |
| 141 | + timing.nsec[GGML_PROF_OP_END] = 0; |
| 142 | + } |
| 143 | + |
| 144 | + ggml_profile_format_op_dims(dims, cg->nodes[i]); |
| 145 | + ggml_profile_format_op_types(types, cg->nodes[i]); |
| 146 | + |
| 147 | + fprintf(out->stream, "%s| %04d | %10s | %10lu | %10lu | %10lu | %46s | %22s | %20s |\n", out->prefix, |
| 148 | + i, ggml_op_name(cg->nodes[i]->op), |
| 149 | + (unsigned long) p_nsec, (unsigned long) s_nsec, (unsigned long) t_nsec, |
| 150 | + dims, types, cg->nodes[i]->name); |
| 151 | + } |
| 152 | + fprintf(out->stream, "%s \n", out->prefix); // empty line to split tables |
| 153 | +} |
| 154 | + |
| 155 | +extern "C" void ggml_graph_profile_free(struct ggml_cgraph *cg) |
| 156 | +{ |
| 157 | + if (!cg->prof) { return; } |
| 158 | + |
| 159 | + ggml_profile_output *out = cg->prof->output; |
| 160 | + if (out->stream != stderr) { |
| 161 | + fclose(out->stream); |
| 162 | + } |
| 163 | + |
| 164 | + free(cg->prof); cg->prof = nullptr; |
| 165 | +} |
| 166 | + |
| 167 | +extern "C" void ggml_graph_profile_event(const struct ggml_cgraph *cg, enum ggml_profile_event e, int node_n, int ith) |
| 168 | +{ |
| 169 | + if (!cg->prof) { return; } |
| 170 | + |
| 171 | + using clock = std::chrono::high_resolution_clock; |
| 172 | + |
| 173 | + ggml_profile_timing &timing = cg->prof->timing[node_n][ith]; |
| 174 | + timing.nsec[e] = std::chrono::nanoseconds(clock::now().time_since_epoch()).count(); |
| 175 | +} |
| 176 | + |
| 177 | +#endif // GGML_GRAPH_PROFILER |
0 commit comments