metal : skip loading all-zero mask (#19337)
* metal : skip loading all-zero mask * cont : minor
This commit is contained in:
@@ -5285,6 +5285,7 @@ constant int32_t FC_flash_attn_ext_blk_ncpsg [[function_constant(FC_FLASH_ATTN_E
|
|||||||
// scan the blocks of the mask that are not masked
|
// scan the blocks of the mask that are not masked
|
||||||
// 0 - masked (i.e. full of -INF, skip)
|
// 0 - masked (i.e. full of -INF, skip)
|
||||||
// 1 - not masked (i.e. at least one element of the mask is not -INF)
|
// 1 - not masked (i.e. at least one element of the mask is not -INF)
|
||||||
|
// 2 - all zero
|
||||||
kernel void kernel_flash_attn_ext_blk(
|
kernel void kernel_flash_attn_ext_blk(
|
||||||
constant ggml_metal_kargs_flash_attn_ext_blk & args,
|
constant ggml_metal_kargs_flash_attn_ext_blk & args,
|
||||||
device const char * mask,
|
device const char * mask,
|
||||||
@@ -5306,29 +5307,31 @@ kernel void kernel_flash_attn_ext_blk(
|
|||||||
|
|
||||||
device const half * mask_src = (device const half *) (mask + (i1*Q)*args.nb31 + i2*args.nb32 + i3*args.nb33) + i0*C + tiisg;
|
device const half * mask_src = (device const half *) (mask + (i1*Q)*args.nb31 + i2*args.nb32 + i3*args.nb33) + i0*C + tiisg;
|
||||||
|
|
||||||
// fast route
|
|
||||||
if (res == 0) {
|
|
||||||
if (simd_max(*mask_src) > -MAXHALF/2) {
|
|
||||||
res = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// detailed check of the elements of the block
|
// detailed check of the elements of the block
|
||||||
if ((C > NW || Q > 1) && res == 0) {
|
if ((C > NW || Q > 1) && res == 0) {
|
||||||
half m = -MAXHALF;
|
half mmin = MAXHALF;
|
||||||
|
half mmax = -MAXHALF;
|
||||||
|
|
||||||
FOR_UNROLL (short j = 0; j < Q; ++j) {
|
FOR_UNROLL (short j = 0; j < Q; ++j) {
|
||||||
FOR_UNROLL (short ii = 0; ii < C/NW; ++ii) {
|
FOR_UNROLL (short ii = 0; ii < C/NW; ++ii) {
|
||||||
m = max(m, mask_src[ii*NW]);
|
mmin = min(mmin, mask_src[ii*NW]);
|
||||||
|
mmax = max(mmax, mask_src[ii*NW]);
|
||||||
}
|
}
|
||||||
|
|
||||||
mask_src += args.nb31/2;
|
mask_src += args.nb31/2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (simd_max(m) > -MAXHALF/2) {
|
mmin = simd_min(mmin);
|
||||||
|
mmax = simd_max(mmax);
|
||||||
|
|
||||||
|
if (mmax > -MAXHALF) {
|
||||||
|
if (mmin == 0.0 && mmax == 0.0) {
|
||||||
|
res = 2;
|
||||||
|
} else {
|
||||||
res = 1;
|
res = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const int32_t nblk1 = ((args.ne01 + Q - 1)/Q);
|
const int32_t nblk1 = ((args.ne01 + Q - 1)/Q);
|
||||||
const int32_t nblk0 = ((args.ne30 + C - 1)/C);
|
const int32_t nblk0 = ((args.ne30 + C - 1)/C);
|
||||||
@@ -5568,9 +5571,13 @@ void kernel_flash_attn_ext_impl(
|
|||||||
ic = 0;
|
ic = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char blk_cur = 1;
|
||||||
|
|
||||||
// read the mask into shared mem
|
// read the mask into shared mem
|
||||||
if (FC_flash_attn_ext_has_mask) {
|
if (FC_flash_attn_ext_has_mask) {
|
||||||
if (blk[ic0] == 0) {
|
blk_cur = blk[ic0];
|
||||||
|
|
||||||
|
if (blk_cur == 0) {
|
||||||
FOR_UNROLL (short jj = 0; jj < NQ; ++jj) {
|
FOR_UNROLL (short jj = 0; jj < NQ; ++jj) {
|
||||||
pm2[jj] += NW;
|
pm2[jj] += NW;
|
||||||
}
|
}
|
||||||
@@ -5578,6 +5585,7 @@ void kernel_flash_attn_ext_impl(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (blk_cur == 1) {
|
||||||
FOR_UNROLL (short jj = 0; jj < NQ; ++jj) {
|
FOR_UNROLL (short jj = 0; jj < NQ; ++jj) {
|
||||||
const short j = jj*NSG + sgitg;
|
const short j = jj*NSG + sgitg;
|
||||||
|
|
||||||
@@ -5589,6 +5597,11 @@ void kernel_flash_attn_ext_impl(
|
|||||||
|
|
||||||
pm2[jj] += NW;
|
pm2[jj] += NW;
|
||||||
}
|
}
|
||||||
|
} else if (blk_cur == 2) {
|
||||||
|
FOR_UNROLL (short jj = 0; jj < NQ; ++jj) {
|
||||||
|
pm2[jj] += NW;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
// note: old -INF block optimization - obsoleted by pre-computing non-masked blocks
|
// note: old -INF block optimization - obsoleted by pre-computing non-masked blocks
|
||||||
@@ -5752,11 +5765,13 @@ void kernel_flash_attn_ext_impl(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// mqk = mqk + slope*mask
|
// mqk = mqk + slope*mask
|
||||||
|
if (blk_cur != 2) {
|
||||||
if (FC_flash_attn_ext_has_bias) {
|
if (FC_flash_attn_ext_has_bias) {
|
||||||
s2 += s2_t(sm2[j*SH + tiisg])*slope;
|
s2 += s2_t(sm2[j*SH + tiisg])*slope;
|
||||||
} else {
|
} else {
|
||||||
s2 += s2_t(sm2[j*SH + tiisg]);
|
s2 += s2_t(sm2[j*SH + tiisg]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
M[jj] = simd_max(max(M[jj], max(s2[0], s2[1])));
|
M[jj] = simd_max(max(M[jj], max(s2[0], s2[1])));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user