Skip to content

Commit f900252

Browse files
Green-Skystduhpf
authored andcommitted
fix mask with flash attn
1 parent 6b5dc9d commit f900252

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

ggml_extend.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,18 @@ __STATIC_INLINE__ struct ggml_tensor* ggml_nn_attention_ext(struct ggml_context*
944944
v = ggml_reshape_3d(ctx, v, d_head, L_k, n_head * N); // [N * n_head, L_k, d_head]
945945
v = ggml_cast(ctx, v, GGML_TYPE_F16);
946946

947+
if (mask != nullptr) {
948+
mask = ggml_transpose(ctx, mask);
949+
950+
if (mask->ne[1] < GGML_PAD(q->ne[1], GGML_KQ_MASK_PAD)) {
951+
LOG_DEBUG("mask dims %ld, %ld, %ld, %ld\n", mask->ne[0], mask->ne[1], mask->ne[2], mask->ne[3]);
952+
LOG_DEBUG("needs padding, padding from %ld to %ld\n", mask->ne[1], GGML_PAD(q->ne[1], GGML_KQ_MASK_PAD));
953+
mask = ggml_pad(ctx, mask, 0, GGML_PAD(q->ne[1], GGML_KQ_MASK_PAD) - mask->ne[1], 0, 0);
954+
}
955+
956+
mask = ggml_cast(ctx, mask, GGML_TYPE_F16);
957+
}
958+
947959
kqv = ggml_flash_attn_ext(ctx, q, k, v, mask, scale, 0, 0);
948960
ggml_flash_attn_ext_set_prec(kqv, GGML_PREC_F32);
949961

0 commit comments

Comments
 (0)