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