]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
CANN: Disable acl_graph for prefill stage (#15933)
authorhipudding <redacted>
Thu, 11 Sep 2025 07:59:37 +0000 (15:59 +0800)
committerGitHub <redacted>
Thu, 11 Sep 2025 07:59:37 +0000 (15:59 +0800)
Since the prefill length is not fixed, graphs constructed for the
prefill stage cannot be reused. For this reason, ACL graph
execution is disabled by default during prefill.

docs/backend/CANN.md
ggml/src/ggml-cann/ggml-cann.cpp

index 35b189bb9558f89992c3f99b8fa6904cdfbd56fc..e45fc7dd28f388de975147212295313c9697c226 100755 (executable)
@@ -318,3 +318,7 @@ Operators are executed using ACL graph execution, rather than in op-by-op (eager
 ### GGML_CANN_GRAPH_CACHE_CAPACITY
 
 Maximum number of compiled CANN graphs kept in the LRU cache, default is 12. When the number of cached graphs exceeds this capacity, the least recently used graph will be evicted.
+
+### GGML_CANN_PREFILL_USE_GRAPH
+
+Enable ACL graph execution during the prefill stage, default is false. This option is only effective when FA is enabled.
index d148174f1e84f253b8b3625493110789ca88eeb5..19a18a281dfcb827f8aa95289017f929df268fd3 100755 (executable)
@@ -2360,6 +2360,21 @@ static enum ggml_status ggml_backend_cann_graph_compute(
     bool use_cann_graph = true;
     bool cann_graph_update_required = false;
 
+    static bool prefill_use_graph = parse_bool(get_env("GGML_CANN_PREFILL_USE_GRAPH").value_or(""));
+    if (!prefill_use_graph) {
+        // Do not use acl_graph for prefill.
+        for (int i = 0; i < cgraph->n_nodes; i++) {
+            ggml_tensor * node = cgraph->nodes[i];
+            // TODO: Optimize here. Currently, we can only
+            // get seq_len by FA's input.
+            if (node->op == GGML_OP_FLASH_ATTN_EXT) {
+                // Q -> src[0], shape: [B, S, N, D]
+                use_cann_graph = (node->src[0]->ne[1] == 1);
+                break;
+            }
+        }
+    }
+
     if (!cann_ctx->acl_graph_mode) {
         use_cann_graph = false;
     }