Skip to content

Commit 36daf24

Browse files
zhaixuejun1993wine99
authored andcommitted
OpenVINO backend: fix error for attention size compute in llm param
1 parent d69be5f commit 36daf24

3 files changed

Lines changed: 15 additions & 4 deletions

File tree

ggml/src/ggml-openvino/ggml-decoder.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ std::pair<ModelParams, ComputeParams> GgmlOvDecoder::compute_llm_params(ggml_cgr
286286
for (int i = 0; i < cgraph->n_nodes; i++) {
287287
auto * node = cgraph->nodes[i];
288288
std::string name = std::string(node->name);
289-
if (node->op == GGML_OP_FLASH_ATTN_EXT || (node->op == GGML_OP_SOFT_MAX && node->src[1] != nullptr)) {
289+
if (node->op == GGML_OP_FLASH_ATTN_EXT || (node->op == GGML_OP_SOFT_MAX && node->src[1] != nullptr && node->src[0]->src[1] != nullptr)) {
290290
compute_params.input_len = node->src[0]->ne[1];
291291

292292
auto * q_perm = node->src[0];
@@ -342,6 +342,15 @@ std::pair<ModelParams, ComputeParams> GgmlOvDecoder::compute_llm_params(ggml_cgr
342342
compute_params.token_len_per_seq = 1;
343343
}
344344
}
345+
346+
if (node->op == GGML_OP_MUL_MAT && node->src[0]->op == GGML_OP_PERMUTE &&
347+
node->src[0]->src[0]->op == GGML_OP_VIEW && is_kvcache(node->src[0]->view_src, node->view_src)) {
348+
if (node->src[1]->op == GGML_OP_PERMUTE && node->src[1]->src[0]->op == GGML_OP_VIEW &&
349+
node->src[1]->src[0]->src[0]->op == GGML_OP_ROPE) {
350+
compute_params.attention_size = node->ne[0];
351+
}
352+
}
353+
345354
// if the node op is TRANSPOSE and its input is PERMUTE and the source of the PERMUTE is VIEW, then get the attention size with the TRANSPOSE node ne[0] (in case no GGML_OP_FLASH_ATTN_EXT)
346355
if (node->op == GGML_OP_TRANSPOSE && node->src[0]->op == GGML_OP_PERMUTE &&
347356
node->src[0]->src[0]->op == GGML_OP_VIEW) {

ggml/src/ggml-openvino/ggml-decoder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ class GgmlOvDecoder : public ov::frontend::ggml::GgmlDecoder {
248248
}
249249

250250
inline static bool is_kvcache(const ggml_tensor * tensor, const ggml_tensor * op) {
251-
return (op->op == GGML_OP_SET_ROWS && op->src[2] == tensor) ||
252-
tensor->buffer->usage == GGML_BACKEND_BUFFER_USAGE_ANY;
251+
return tensor->buffer->usage == GGML_BACKEND_BUFFER_USAGE_ANY ||
252+
(op->op == GGML_OP_SET_ROWS && op->src[2] == tensor);
253253
}
254254

255255
inline static bool is_kv_idx(const ggml_tensor * tensor, const ggml_tensor * op) {

ggml/src/ggml-openvino/openvino/translate_session.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ void add_rope_sin_cos(TensorMap & tensor_map, GgmlDecoder & ggml_model_decoder)
146146

147147
// Create common patterns
148148
void preprocess(TensorMap & tensor_map, GgmlDecoder & ggml_model_decoder) {
149-
add_sliced_mask(tensor_map, ggml_model_decoder);
149+
if (ggml_model_decoder.is_stateful()) {
150+
add_sliced_mask(tensor_map, ggml_model_decoder);
151+
}
150152
add_rope_sin_cos(tensor_map, ggml_model_decoder);
151153
}
152154

0 commit comments

Comments
 (0)