Repository: ggerganov/llama.cpp
File: ggml/src/ggml.c (C)

Code before:
        }

        const struct ggml_tensor * node = cgraph->nodes[node_idxs[i]];

        if (node->flags & GGML_TENSOR_FLAG_OUTPUT) {
            return false;
        }

        if (ggml_find_tensor_node_list(cgraph, outputs, num_outputs, node) != -1) {
            continue;
        }

        interior_nodes[interior_nodes_count++] = node_idxs[i];
    }

    for (int i = 0; i < interior_nodes_count; ++i) {
        const int num_uses = ggml_node_get_use_count(cgraph, interior_nodes[i]);

        const struct ggml_tensor * node = cgraph->nodes[interior_nodes[i]];

        // if interior-node has n-uses, ensure that all of them lie within in this subgraph
        int subgraph_uses = 0;
        for (int j = 0; j < count; ++j) {
            const struct ggml_tensor * other_node = cgraph->nodes[node_idxs[j]];
            for (int src_idx = 0; src_idx < GGML_MAX_SRC; src_idx++) {
                if (other_node->src[src_idx] && other_node->src[src_idx] == node) {
                    subgraph_uses++;
                }
            }
        }

        if (subgraph_uses != num_uses) {
            return false;
        }

        // if node is a view, check if the view src is within the subgraph
        if (node->view_src) {
            const struct ggml_tensor * view_src = node->view_src;
            if (ggml_find_tensor_node_list(cgraph, node_idxs, count, view_src) == -1) {
                return false;
            }
        }
    }

    return true;
}

// check if node is part of the graph
static bool ggml_graph_find(const struct ggml_cgraph * cgraph, const struct ggml_tensor * node) {
    if (cgraph == NULL) {
        return true;

Review comment (suggestion):
```suggestion
                if (other_node->src[src_idx] == node) {
```

Code after:
            return false;
        }

        if (ggml_node_list_find_tensor(cgraph, outputs, num_outputs, node) != -1) {
            continue;
        }

        if (node->flags & GGML_TENSOR_FLAG_OUTPUT) {
            return false;
        }

        int subgraph_uses = 0;
        for (int j = i + 1; j < count; ++j) {
            const struct ggml_tensor * other_node = cgraph->nodes[node_idxs[j]];
            for (int src_idx = 0; src_idx < GGML_MAX_SRC; src_idx++) {
                if (other_node->src[src_idx] == node) {
                    subgraph_uses++;
                }
            }
        }

        if (subgraph_uses != ggml_node_get_use_count(cgraph, node_idxs[i])) {
            return false;
        }

        // if node is a view, check if the view_src and all it's parent view_srcs are within the subgraph
        struct ggml_tensor * view_src = node->view_src;
        while (view_src) {
            if (ggml_node_list_find_tensor(cgraph, node_idxs, count, view_src) == -1) {
                return false;
            }
            view_src = view_src->view_src;
        }
    }

    return true;
}

// check if node is part of the graph
static bool ggml_graph_find(const struct ggml_cgraph * cgraph, const struct ggml_tensor * node) {
    if (cgraph == NULL) {
        return true;
    }

    for (int i = 0; i < cgraph->n_nodes; i++) {
        if (cgraph->nodes[i] == node) {
            return true;
        }
    }

    return false;