Skip to content

Commit 1eaea44

Browse files
am17anspiritbuun
authored andcommitted
CUDA: make cuda graphs props check faster (#21472)
* CUDA: compute fast hash instead of expensive props check * use seen node * use memcp
1 parent 466dc2c commit 1eaea44

2 files changed

Lines changed: 6 additions & 128 deletions

File tree

ggml/src/ggml-cuda/common.cuh

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,19 +1157,6 @@ struct ggml_tensor_extra_gpu {
11571157
#define USE_CUDA_GRAPH
11581158
#endif
11591159

1160-
struct ggml_cuda_graph_node_properties {
1161-
void * node_data;
1162-
ggml_op node_op;
1163-
enum ggml_type node_type;
1164-
int32_t flags;
1165-
int64_t ne[GGML_MAX_DIMS];
1166-
size_t nb[GGML_MAX_DIMS];
1167-
void * src_data[GGML_MAX_SRC];
1168-
int32_t op_params[GGML_MAX_OP_PARAMS / sizeof(int32_t)];
1169-
};
1170-
1171-
static_assert(std::is_trivial<ggml_cuda_graph_node_properties>::value, "ggml_cuda_graph_node_properties must be trivial");
1172-
11731160
struct ggml_cuda_graph {
11741161
#ifdef USE_CUDA_GRAPH
11751162
~ggml_cuda_graph() {
@@ -1186,13 +1173,7 @@ struct ggml_cuda_graph {
11861173
std::vector<cudaGraphNode_t> nodes;
11871174
bool disable_due_to_gpu_arch = false;
11881175
bool warmup_complete = false;
1189-
std::vector<ggml_cuda_graph_node_properties> props;
1190-
1191-
// these are extra tensors (inputs) that participate in the ggml graph but are not nodes
1192-
// they properties also have to match in order to be able to safely reuse a CUDA graph
1193-
// ref: https://github.com/ggml-org/llama.cpp/pull/18583
1194-
// ref: https://github.com/ggml-org/llama.cpp/pull/19165
1195-
std::vector<ggml_cuda_graph_node_properties> extra;
1176+
std::vector<ggml_tensor> nodes_copy;
11961177

11971178
bool is_enabled() const {
11981179
static const bool disable_cuda_graphs_due_to_env = (getenv("GGML_CUDA_DISABLE_GRAPHS") != nullptr);

ggml/src/ggml-cuda/ggml-cuda.cu

Lines changed: 5 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
#include <cstdlib>
8484
#include <string>
8585
#include <vector>
86-
#include <unordered_set>
8786

8887
static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
8988

@@ -2973,74 +2972,6 @@ static bool ggml_cuda_graph_check_compability(ggml_cgraph * cgraph) {
29732972
return use_cuda_graph;
29742973
}
29752974

2976-
static void ggml_cuda_graph_node_set_properties(ggml_cuda_graph_node_properties * props, ggml_tensor * node) {
2977-
memset(props, 0, sizeof(ggml_cuda_graph_node_properties));
2978-
props->node_data = node->data;
2979-
props->node_op = node->op;
2980-
props->node_type = node->type;
2981-
props->flags = node->flags;
2982-
for (int i = 0; i < GGML_MAX_DIMS; i++) {
2983-
props->ne[i] = node->ne[i];
2984-
props->nb[i] = node->nb[i];
2985-
}
2986-
for (int i = 0; i < GGML_MAX_SRC; i++) {
2987-
if (!node->src[i]) {
2988-
continue;
2989-
}
2990-
2991-
props->src_data[i] = node->src[i]->data;
2992-
}
2993-
memcpy(props->op_params, node->op_params, GGML_MAX_OP_PARAMS);
2994-
}
2995-
2996-
static bool ggml_cuda_graph_node_properties_match(ggml_tensor * node, ggml_cuda_graph_node_properties * props) {
2997-
if (node->data != props->node_data && node->op != GGML_OP_VIEW) {
2998-
return false;
2999-
}
3000-
3001-
if (node->op != props->node_op) {
3002-
return false;
3003-
}
3004-
3005-
if (node->type != props->node_type) {
3006-
return false;
3007-
}
3008-
3009-
for (int i = 0; i < GGML_MAX_DIMS; i++) {
3010-
if (node->ne[i] != props->ne[i]) {
3011-
return false;
3012-
}
3013-
if (node->nb[i] != props->nb[i]) {
3014-
return false;
3015-
}
3016-
}
3017-
3018-
if (node->op != GGML_OP_VIEW) {
3019-
for (int i = 0; i < GGML_MAX_SRC; i++) {
3020-
if (!node->src[i]) {
3021-
if (props->src_data[i] != nullptr) {
3022-
return false;
3023-
}
3024-
continue;
3025-
}
3026-
3027-
if (node->src[i]->data != props->src_data[i]) {
3028-
return false;
3029-
}
3030-
}
3031-
}
3032-
3033-
if (memcmp(props->op_params, node->op_params, GGML_MAX_OP_PARAMS) != 0) {
3034-
return false;
3035-
}
3036-
3037-
if ((node->flags & GGML_TENSOR_FLAG_COMPUTE) != (props->flags & GGML_TENSOR_FLAG_COMPUTE)) {
3038-
return false;
3039-
}
3040-
3041-
return true;
3042-
}
3043-
30442975
static const void * ggml_cuda_graph_get_key(ggml_cgraph * cgraph) {
30452976
return cgraph->nodes[0];
30462977
}
@@ -3052,52 +2983,18 @@ static bool ggml_cuda_graph_update_required(ggml_backend_cuda_context * cuda_ctx
30522983
ggml_cuda_graph * graph = cuda_ctx->cuda_graph(graph_key);
30532984

30542985
// Check if the graph size has changed
3055-
if (graph->props.size() != (size_t)cgraph->n_nodes) {
2986+
if ((int)graph->nodes_copy.size() != cgraph->n_nodes) {
30562987
res = true;
3057-
graph->props.resize(cgraph->n_nodes);
2988+
graph->nodes_copy.resize(cgraph->n_nodes);
30582989
}
30592990

3060-
// Loop over nodes in GGML graph to determine if CUDA graph update is required
3061-
// and store properties to allow this comparison for the next token
3062-
std::unordered_set<ggml_tensor *> seen_node;
3063-
std::vector<ggml_tensor *> srcs_extra;
30642991
for (int i = 0; i < cgraph->n_nodes; i++) {
3065-
bool props_match = true;
3066-
3067-
seen_node.insert(cgraph->nodes[i]);
3068-
30692992
if (!res) {
3070-
props_match = ggml_cuda_graph_node_properties_match(cgraph->nodes[i], &graph->props[i]);
3071-
}
3072-
if (!props_match) {
3073-
res = true;
3074-
}
3075-
ggml_cuda_graph_node_set_properties(&graph->props[i], cgraph->nodes[i]);
3076-
3077-
for (int src_idx = 0; src_idx < GGML_MAX_SRC; ++src_idx) {
3078-
ggml_tensor * src = cgraph->nodes[i]->src[src_idx];
3079-
if (src && seen_node.find(src) == seen_node.end()) {
3080-
srcs_extra.push_back(src);
2993+
if (memcmp(&graph->nodes_copy[i], cgraph->nodes[i], sizeof(ggml_tensor)) != 0) {
2994+
res = true;
30812995
}
30822996
}
3083-
}
3084-
3085-
if (graph->extra.size() != (size_t) srcs_extra.size()) {
3086-
res = true;
3087-
graph->extra.resize(srcs_extra.size());
3088-
}
3089-
3090-
for (size_t i = 0; i < srcs_extra.size(); ++i) {
3091-
bool props_match = true;
3092-
3093-
if (!res) {
3094-
props_match = ggml_cuda_graph_node_properties_match(srcs_extra[i], &graph->extra[i]);
3095-
}
3096-
3097-
if (!props_match) {
3098-
res = true;
3099-
}
3100-
ggml_cuda_graph_node_set_properties(&graph->extra[i], srcs_extra[i]);
2997+
memcpy(&graph->nodes_copy[i], cgraph->nodes[i], sizeof(ggml_tensor));
31012998
}
31022999

31033000
return res;

0 commit comments

Comments
 (0)