| 16206 | } |
| 16207 | |
| 16208 | static void ggml_visit_parents(struct ggml_cgraph * cgraph, struct ggml_tensor * node) { |
| 16209 | if (node->grad == NULL) { |
| 16210 | // this usually happens when we generate intermediate nodes from constants in the backward pass |
| 16211 | // it can also happen during forward pass, if the user performs computations with constants |
| 16212 | if (node->op != GGML_OP_NONE) { |
| 16213 | //GGML_PRINT_DEBUG("%s: warning: node %p has no grad, but op %d\n", __func__, (void *) node, node->op); |
| 16214 | } |
| 16215 | } |
| 16216 | |
| 16217 | // check if already visited |
| 16218 | if (ggml_hash_insert(cgraph->visited_hash_table, node) == GGML_HASHTABLE_ALREADY_EXISTS) { |
| 16219 | return; |
| 16220 | } |
| 16221 | |
| 16222 | for (int i = 0; i < GGML_MAX_SRC; ++i) { |
| 16223 | const int k = |
| 16224 | (cgraph->order == GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT) ? i : |
| 16225 | (cgraph->order == GGML_CGRAPH_EVAL_ORDER_RIGHT_TO_LEFT) ? (GGML_MAX_SRC-1-i) : |
| 16226 | /* unknown order, just fall back to using i*/ i; |
| 16227 | if (node->src[k]) { |
| 16228 | ggml_visit_parents(cgraph, node->src[k]); |
| 16229 | } |
| 16230 | } |
| 16231 | |
| 16232 | if (node->op == GGML_OP_NONE && node->grad == NULL) { |
| 16233 | // reached a leaf node, not part of the gradient graph (e.g. a constant) |
| 16234 | GGML_ASSERT(cgraph->n_leafs < cgraph->size); |
| 16235 | |
| 16236 | if (strlen(node->name) == 0) { |
| 16237 | ggml_format_name(node, "leaf_%d", cgraph->n_leafs); |
| 16238 | } |
| 16239 | |
| 16240 | cgraph->leafs[cgraph->n_leafs] = node; |
| 16241 | cgraph->n_leafs++; |
| 16242 | atomic_store(&(node->is_finish), 1); |
| 16243 | } else { |
| 16244 | GGML_ASSERT(cgraph->n_nodes < cgraph->size); |
| 16245 | |
| 16246 | if (strlen(node->name) == 0) { |
| 16247 | ggml_format_name(node, "node_%d", cgraph->n_nodes); |
| 16248 | } |
| 16249 | |
| 16250 | cgraph->nodes[cgraph->n_nodes] = node; |
| 16251 | if (cgraph->grads) { |
| 16252 | cgraph->grads[cgraph->n_nodes] = node->grad; |
| 16253 | } |
| 16254 | cgraph->n_nodes++; |
| 16255 | } |
| 16256 | } |
| 16257 | |
| 16258 | static void ggml_build_forward_impl(struct ggml_cgraph * cgraph, struct ggml_tensor * tensor, bool expand) { |
| 16259 | if (!expand) { |
no test coverage detected