@@ -2774,13 +2774,13 @@ kernel void kernel_flash_attn_ext(
2774
2774
const short NW = N_SIMDWIDTH;
2775
2775
const short SH = (C + Q); // shared memory per simdgroup in (half)
2776
2776
2777
- const short T = D + 2 * nsg*SH; // shared memory size per query in (half)
2778
- const short TF = T/ 2 ; // shared memory size per query in (float)
2777
+ const short T = D + nsg*SH; // shared memory size per query in (half)
2778
+ const short TF = T; // shared memory size per query in (float)
2779
2779
const short T4 = T/4 ; // shared memory size per query in (half4)
2780
2780
2781
- threadgroup half * sq = (threadgroup half *) (shared + 0 *D); // holds the query data
2782
- threadgroup half4 * sq4 = (threadgroup half4 *) (shared + 0 *D); // same as above but in half4
2783
- threadgroup float * ss = (threadgroup float *) (shared + 2 * sgitg*SH + 1 *D); // scratch buffer for attention and diagonal matrix
2781
+ threadgroup half * sq = (threadgroup half *) (shared + 0 *D); // holds the query data
2782
+ threadgroup half4 * sq4 = (threadgroup half4 *) (shared + 0 *D); // same as above but in half4
2783
+ threadgroup half * ss = (threadgroup half *) (shared + sgitg*SH + 1 *D); // scratch buffer for attention and diagonal matrix
2784
2784
2785
2785
threadgroup half * skv = (threadgroup half *) (shared + sgitg*(4 *16 *KV) + Q*T); // scratch buffer to load K and V in shared memory
2786
2786
threadgroup half4x4 * skv4 = (threadgroup half4x4 *) (shared + sgitg*(4 *16 *KV) + Q*T); // same as above but in half4x4
@@ -2809,7 +2809,7 @@ kernel void kernel_flash_attn_ext(
2809
2809
// zero out shared memory SH
2810
2810
for (short j = 0 ; j < Q; ++j) {
2811
2811
for (short i = tiisg; i < SH; i += NW) {
2812
- ss[j*TF + i] = 0 .0f ;
2812
+ ss[j*TF + i] = 0 .0h ;
2813
2813
}
2814
2814
}
2815
2815
@@ -2874,7 +2874,7 @@ kernel void kernel_flash_attn_ext(
2874
2874
// Q*K^T
2875
2875
{
2876
2876
for (short cc = 0 ; cc < C/8 ; ++cc) {
2877
- simdgroup_float8x8 mqk = make_filled_simdgroup_matrix<float , 8 >(0 .h );
2877
+ simdgroup_half8x8 mqk = make_filled_simdgroup_matrix<half , 8 >(0 .h );
2878
2878
2879
2879
if (is_same<block_q, half4x4>::value) {
2880
2880
// we can read directly from global memory
@@ -2944,7 +2944,7 @@ kernel void kernel_flash_attn_ext(
2944
2944
const float m = M[j];
2945
2945
2946
2946
// scale and apply the logitcap / mask
2947
- float s = ss[j*TF + tiisg]*scale;
2947
+ float s = (( float )( ss[j*TF + tiisg])) *scale;
2948
2948
2949
2949
if (logit_softcap != 0 .0f ) {
2950
2950
s = logit_softcap*precise::tanh (s);
@@ -2980,7 +2980,7 @@ kernel void kernel_flash_attn_ext(
2980
2980
2981
2981
// O = diag(ms)*O
2982
2982
{
2983
- simdgroup_float8x8 mm;
2983
+ simdgroup_half8x8 mm;
2984
2984
simdgroup_load (mm, ss + C, TF, 0 , false );
2985
2985
2986
2986
for (short i = 0 ; i < D8; ++i) {
@@ -2991,7 +2991,7 @@ kernel void kernel_flash_attn_ext(
2991
2991
// O = O + (Q*K^T)*V
2992
2992
{
2993
2993
for (short cc = 0 ; cc < C/8 ; ++cc) {
2994
- simdgroup_float8x8 ms;
2994
+ simdgroup_half8x8 ms;
2995
2995
simdgroup_load (ms, ss + 8 *cc, TF, 0 , false );
2996
2996
2997
2997
if (is_same<block_q, half4x4>::value) {
@@ -3103,8 +3103,8 @@ kernel void kernel_flash_attn_ext(
3103
3103
// O_0 = diag(ms0)*O_0 + diag(ms1)*O_1
3104
3104
{
3105
3105
simdgroup_half8x8 t;
3106
- simdgroup_float8x8 ms0;
3107
- simdgroup_float8x8 ms1;
3106
+ simdgroup_half8x8 ms0;
3107
+ simdgroup_half8x8 ms1;
3108
3108
3109
3109
simdgroup_load (ms0, ss + C, TF, 0 , false );
3110
3110
simdgroup_load (ms1, ss + C + sg*SH, TF, 0 , false );
0 commit comments