Skip to content

Commit

Permalink
metal : reduce branches
Browse files Browse the repository at this point in the history
  • Loading branch information
ggerganov committed Jan 21, 2024
1 parent 528da75 commit e0ba0da
Showing 1 changed file with 8 additions and 22 deletions.
30 changes: 8 additions & 22 deletions ggml-metal.metal
Original file line number Diff line number Diff line change
Expand Up @@ -2056,40 +2056,26 @@ kernel void kernel_flash_attn_ext_f16(
continue;
}

half4 s4 = 0.0f;
device const half4 * pk4 = (device const half4 *) ((device char *) k + (ic*nb11 + ik2*nb12 + ik3*nb13));
device const half4 * pv4 = (device const half4 *) ((device char *) v + (ic*nb21 + iv2*nb22 + iv3*nb23));

device const half4 * pk4 = (device const half4 *) ((device char *) k + ( ic*nb11 + ik2*nb12 + ik3*nb13));
half4 s4 = 0.0h;

for (int64_t d = 0; d < D4; ++d) {
s4 += pk4[d] * pq4[d];
}

half s = s4.x + s4.y + s4.z + s4.w;

s = s*scale + mv;
half s = (s4.x + s4.y + s4.z + s4.w)*scale + mv;

const half Mold = M;

half ms = 1.0f;
half vs = 1.0f;

if (s > M) {
M = s;
ms = exp(Mold - M);

// V = V*exp(Mold - M)
for (int64_t d = 0; d < D4; ++d) {
V16[d] *= ms;
}
} else {
vs = exp(s - M);
}
M = max(M, s);

device const half4 * pv4 = (device const half4 *) ((device char *) v + (ic*nb21 + iv2*nb22 + iv3*nb23));
const half ms = (Mold - M);
const half vs = (s - M);

// V += v*exp(s - M)
for (int64_t d = 0; d < D4; ++d) {
V16[d] += pv4[d] * vs;
V16[d] = V16[d]*ms + pv4[d]*vs;
}

S = S*ms + vs;
Expand Down

0 comments on commit e0ba0da

Please sign in to comment.