Skip to content

Commit 46c338f

Browse files
committed
metal : remove mask padding requirement
1 parent 50d2b21 commit 46c338f

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

ggml/src/ggml-metal/ggml-metal-device.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,19 +994,23 @@ ggml_metal_pipeline_t ggml_metal_library_get_pipeline_flash_attn_ext(
994994
const int32_t ns10 = op->src[1]->nb[1]/op->src[1]->nb[0];
995995
const int32_t ns20 = op->src[2]->nb[1]/op->src[2]->nb[0];
996996

997+
// do bounds checks for the mask?
998+
const bool bc_mask = op->src[3] && (op->src[3]->ne[1] % 8 != 0);
999+
9971000
snprintf(base, 256, "kernel_%s_%s_dk%d_dv%d",
9981001
"flash_attn_ext",
9991002
ggml_type_name(op->src[1]->type),
10001003
dk,
10011004
dv);
10021005

1003-
snprintf(name, 256, "%s_mask=%d_sinks=%d_bias=%d_scap=%d_kvpad=%d_ns10=%d_ns20=%d_nsg=%d",
1006+
snprintf(name, 256, "%s_mask=%d_sinks=%d_bias=%d_scap=%d_kvpad=%d_bcm=%d_ns10=%d_ns20=%d_nsg=%d",
10041007
base,
10051008
has_mask,
10061009
has_sinks,
10071010
has_bias,
10081011
has_scap,
10091012
has_kvpad,
1013+
bc_mask,
10101014
ns10,
10111015
ns20,
10121016
nsg);
@@ -1024,6 +1028,8 @@ ggml_metal_pipeline_t ggml_metal_library_get_pipeline_flash_attn_ext(
10241028
ggml_metal_cv_set_bool(cv, has_scap, FC_FLASH_ATTN_EXT + 3);
10251029
ggml_metal_cv_set_bool(cv, has_kvpad, FC_FLASH_ATTN_EXT + 4);
10261030

1031+
ggml_metal_cv_set_bool(cv, bc_mask, FC_FLASH_ATTN_EXT + 10);
1032+
10271033
ggml_metal_cv_set_int32(cv, ns10, FC_FLASH_ATTN_EXT + 20);
10281034
ggml_metal_cv_set_int32(cv, ns20, FC_FLASH_ATTN_EXT + 21);
10291035
ggml_metal_cv_set_int32(cv, nsg, FC_FLASH_ATTN_EXT + 22);

ggml/src/ggml-metal/ggml-metal-ops.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,8 +1979,8 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
19791979
GGML_ASSERT(ne12 == ne22);
19801980

19811981
GGML_ASSERT(!op->src[3] || op->src[3]->type == GGML_TYPE_F16);
1982-
GGML_ASSERT(!op->src[3] || op->src[3]->ne[1] >= GGML_PAD(op->src[0]->ne[1], 8) &&
1983-
"the Flash-Attention Metal kernel requires the mask to be padded to 8 and at least n_queries big");
1982+
GGML_ASSERT(!op->src[3] || op->src[3]->ne[1] >= op->src[0]->ne[1] &&
1983+
"the Flash-Attention Metal kernel requires the mask to be at least n_queries big");
19841984

19851985
float scale;
19861986
float max_bias;

ggml/src/ggml-metal/ggml-metal.metal

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4492,6 +4492,8 @@ constant bool FC_flash_attn_ext_has_bias [[function_constant(FC_FLASH_ATTN_EXT
44924492
constant bool FC_flash_attn_ext_has_scap [[function_constant(FC_FLASH_ATTN_EXT + 3)]];
44934493
constant bool FC_flash_attn_ext_has_kvpad [[function_constant(FC_FLASH_ATTN_EXT + 4)]];
44944494

4495+
constant bool FC_flash_attn_ext_bc_mask [[function_constant(FC_FLASH_ATTN_EXT + 10)]];
4496+
44954497
//constant float FC_flash_attn_ext_scale [[function_constant(FC_FLASH_ATTN_EXT + 10)]];
44964498
//constant float FC_flash_attn_ext_max_bias [[function_constant(FC_FLASH_ATTN_EXT + 11)]];
44974499
//constant float FC_flash_attn_ext_logit_softcap [[function_constant(FC_FLASH_ATTN_EXT + 12)]];
@@ -4678,7 +4680,7 @@ void kernel_flash_attn_ext_impl(
46784680
v += (ikv2 + ikv3*args.ne_12_2)*args.nb21*C;
46794681

46804682
if (!FC_flash_attn_ext_has_mask) {
4681-
threadgroup half * sm = (threadgroup half *) (sm2);
4683+
threadgroup half * sm = (threadgroup half *) (sm2);
46824684

46834685
FOR_UNROLL (short jj = 0; jj < NQ; ++jj) {
46844686
const short j = jj*NSG + sgitg;
@@ -4708,7 +4710,12 @@ void kernel_flash_attn_ext_impl(
47084710
FOR_UNROLL (short jj = 0; jj < NQ; ++jj) {
47094711
const short j = jj*NSG + sgitg;
47104712

4711-
sm2[j*SH + tiisg] = pm2[jj][tiisg];
4713+
if (FC_flash_attn_ext_bc_mask) {
4714+
sm2[j*SH + tiisg] = (iq1 + j) < args.ne31 ? pm2[jj][tiisg] : half2(-MAXHALF, -MAXHALF);
4715+
} else {
4716+
sm2[j*SH + tiisg] = pm2[jj][tiisg];
4717+
}
4718+
47124719
pm2[jj] += NW;
47134720
}
47144721

0 commit comments

Comments
 (0)