MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / ggml_recompute_graph_node

Function ggml_recompute_graph_node

ggml.c:15253–15320  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15251// gradient checkpointing
15252
15253static struct ggml_tensor * ggml_recompute_graph_node(
15254 struct ggml_context * ctx,
15255 struct ggml_cgraph * graph,
15256 struct hash_map * replacements,
15257 struct ggml_tensor * node) {
15258
15259 if (node == NULL) {
15260 return NULL;
15261 }
15262
15263 if (node->is_param) {
15264 return node;
15265 }
15266
15267 if (!ggml_hash_contains(graph->visited_hash_table, node)) {
15268 return node;
15269 }
15270
15271 int count_children = 0;
15272 for (int k = 0; k < GGML_MAX_SRC; ++k) {
15273 if (node->src[k]) {
15274 ++count_children;
15275 }
15276 }
15277
15278 if (count_children == 0) {
15279 return node;
15280 }
15281
15282 size_t i = ggml_hash_find(replacements->set, node);
15283 GGML_ASSERT(i != GGML_HASHTABLE_FULL); // assert that not full
15284 if (replacements->set.keys[i] == node) {
15285 return replacements->vals[i];
15286 }
15287
15288 struct ggml_tensor * clone = ggml_new_tensor(ctx, node->type, node->n_dims, node->ne);
15289
15290 // insert clone into replacements
15291 GGML_ASSERT(replacements->set.keys[i] == NULL); // assert that we don't overwrite
15292 replacements->set.keys[i] = node;
15293 replacements->vals[i] = clone;
15294
15295 clone->op = node->op;
15296 clone->grad = node->grad;
15297 clone->is_param = node->is_param;
15298 clone->extra = node->extra;
15299 for (int k = 0; k < GGML_MAX_DIMS; ++k) {
15300 clone->nb[k] = node->nb[k];
15301 }
15302 for (int k = 0; k < GGML_MAX_SRC; ++k) {
15303 clone->src[k] = ggml_recompute_graph_node(ctx, graph, replacements, node->src[k]);
15304 }
15305 if (node->view_src != NULL) {
15306 clone->data = (node->view_src->data == NULL)
15307 ? NULL // view_src not yet allocated
15308 : (char *) node->view_src->data // view_src already allocated
15309 + node->view_offs;
15310 clone->view_src = node->view_src;

Calls 5

ggml_hash_containsFunction · 0.70
ggml_hash_findFunction · 0.70
ggml_new_tensorFunction · 0.70
ggml_format_nameFunction · 0.70
ggml_get_nameFunction · 0.70

Tested by

no test coverage detected