| 6727 | } |
| 6728 | |
| 6729 | static size_t ggml_visit_parents_graph(struct ggml_cgraph * cgraph, struct ggml_tensor * node, bool compute) { |
| 6730 | if (node->op != GGML_OP_NONE && compute) { |
| 6731 | node->flags |= GGML_TENSOR_FLAG_COMPUTE; |
| 6732 | } |
| 6733 | |
| 6734 | const size_t node_hash_pos = ggml_hash_find(&cgraph->visited_hash_set, node); |
| 6735 | GGML_ASSERT(node_hash_pos != GGML_HASHSET_FULL); |
| 6736 | |
| 6737 | if (ggml_bitset_get(cgraph->visited_hash_set.used, node_hash_pos)) { |
| 6738 | // already visited |
| 6739 | |
| 6740 | if (compute) { |
| 6741 | // update the compute flag regardless |
| 6742 | for (int i = 0; i < GGML_MAX_SRC; ++i) { |
| 6743 | struct ggml_tensor * src = node->src[i]; |
| 6744 | if (src && ((src->flags & GGML_TENSOR_FLAG_COMPUTE) == 0)) { |
| 6745 | ggml_visit_parents_graph(cgraph, src, true); |
| 6746 | } |
| 6747 | } |
| 6748 | } |
| 6749 | |
| 6750 | return node_hash_pos; |
| 6751 | } |
| 6752 | |
| 6753 | // This is the first time we see this node in the current graph. |
| 6754 | cgraph->visited_hash_set.keys[node_hash_pos] = node; |
| 6755 | ggml_bitset_set(cgraph->visited_hash_set.used, node_hash_pos); |
| 6756 | cgraph->use_counts[node_hash_pos] = 0; |
| 6757 | |
| 6758 | for (int i = 0; i < GGML_MAX_SRC; ++i) { |
| 6759 | const int k = |
| 6760 | (cgraph->order == GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT) ? i : |
| 6761 | (cgraph->order == GGML_CGRAPH_EVAL_ORDER_RIGHT_TO_LEFT) ? (GGML_MAX_SRC-1-i) : |
| 6762 | /* unknown order, just fall back to using i */ i; |
| 6763 | |
| 6764 | struct ggml_tensor * src = node->src[k]; |
| 6765 | if (src) { |
| 6766 | const size_t src_hash_pos = ggml_visit_parents_graph(cgraph, src, compute); |
| 6767 | |
| 6768 | // Update the use count for this operand. |
| 6769 | cgraph->use_counts[src_hash_pos]++; |
| 6770 | } |
| 6771 | } |
| 6772 | |
| 6773 | if (node->op == GGML_OP_NONE && !(node->flags & GGML_TENSOR_FLAG_PARAM)) { |
| 6774 | // reached a leaf node, not part of the gradient graph (e.g. a constant) |
| 6775 | GGML_ASSERT(cgraph->n_leafs < cgraph->size); |
| 6776 | |
| 6777 | if (strlen(node->name) == 0) { |
| 6778 | ggml_format_name(node, "leaf_%d", cgraph->n_leafs); |
| 6779 | } |
| 6780 | |
| 6781 | cgraph->leafs[cgraph->n_leafs] = node; |
| 6782 | cgraph->n_leafs++; |
| 6783 | } else { |
| 6784 | GGML_ASSERT(cgraph->n_nodes < cgraph->size); |
| 6785 | |
| 6786 | if (strlen(node->name) == 0) { |
no test coverage detected