metal : initial implementation
This commit is contained in:
parent
a9681febd6
commit
1173f49c3b
4 changed files with 180 additions and 33 deletions
138
ggml-metal.metal
138
ggml-metal.metal
|
@ -1960,10 +1960,10 @@ kernel void kernel_leaky_relu_f32(
|
|||
}
|
||||
|
||||
kernel void kernel_flash_attn_ext_f16(
|
||||
device const half * q,
|
||||
device const half * k,
|
||||
device const half * v,
|
||||
device const float * mask,
|
||||
device const char * q,
|
||||
device const char * k,
|
||||
device const char * v,
|
||||
device const char * mask,
|
||||
device float * dst,
|
||||
constant int64_t & ne00,
|
||||
constant int64_t & ne01,
|
||||
|
@ -1973,20 +1973,138 @@ kernel void kernel_flash_attn_ext_f16(
|
|||
constant uint64_t & nb01,
|
||||
constant uint64_t & nb02,
|
||||
constant uint64_t & nb03,
|
||||
constant int64_t & ne10,
|
||||
constant int64_t & ne11,
|
||||
constant int64_t & ne12,
|
||||
constant int64_t & ne13,
|
||||
constant uint64_t & nb10,
|
||||
constant uint64_t & nb11,
|
||||
constant uint64_t & nb12,
|
||||
constant uint64_t & nb13,
|
||||
constant int64_t & ne31,
|
||||
constant uint64_t & nb31,
|
||||
constant int64_t & ne0,
|
||||
constant int64_t & ne1,
|
||||
constant int64_t & ne2,
|
||||
constant int64_t & ne3,
|
||||
constant uint64_t & nb0,
|
||||
constant uint64_t & nb1,
|
||||
constant uint64_t & nb2,
|
||||
constant uint64_t & nb3,
|
||||
constant float & scale,
|
||||
threadgroup float * shared [[threadgroup(0)]],
|
||||
uint3 tgpig[[threadgroup_position_in_grid]],
|
||||
uint3 tpitg[[thread_position_in_threadgroup]],
|
||||
uint3 ntg[[threads_per_threadgroup]]) {
|
||||
// TODO: implement
|
||||
uint3 ntg[[threads_per_threadgroup]],
|
||||
uint tiisg[[thread_index_in_simdgroup]],
|
||||
uint sgitg[[simdgroup_index_in_threadgroup]]) {
|
||||
const int64_t iq3 = tgpig[2];
|
||||
const int64_t iq2 = tgpig[1];
|
||||
const int64_t iq1 = tgpig[0]*N_SIMDWIDTH + tiisg;
|
||||
|
||||
if (iq1 >= ne01) {
|
||||
return;
|
||||
}
|
||||
|
||||
const int64_t D = ne00;
|
||||
|
||||
// TODO: can we move this to the stack?
|
||||
threadgroup half * V16 = (threadgroup half *) (shared + (2*sgitg*N_SIMDWIDTH + tiisg)*D);
|
||||
|
||||
// initialize with zeros
|
||||
for (int64_t d = 0; d < D; ++d) {
|
||||
V16[d] = 0.0h;
|
||||
}
|
||||
|
||||
threadgroup half * pq = (threadgroup half *) (shared + (2*sgitg*N_SIMDWIDTH + N_SIMDWIDTH)*D + tiisg*D);
|
||||
|
||||
half S = 0.0h;
|
||||
half M = -INFINITY;
|
||||
|
||||
const int64_t ir = iq3*ne02*ne01 + iq2*ne01 + iq1;
|
||||
|
||||
device const float * mp = mask ? (device const float *) (mask + (ir%ne31)*nb31) : nullptr;
|
||||
|
||||
// assume K and V are same shape
|
||||
const int64_t ne22 = ne12;
|
||||
const int64_t ne23 = ne13;
|
||||
|
||||
const uint64_t nb21 = nb11;
|
||||
const uint64_t nb22 = nb12;
|
||||
const uint64_t nb23 = nb13;
|
||||
|
||||
// broadcast
|
||||
const int64_t rk2 = ne02/ne12;
|
||||
const int64_t rk3 = ne03/ne13;
|
||||
|
||||
const int64_t rv2 = ne02/ne22;
|
||||
const int64_t rv3 = ne03/ne23;
|
||||
|
||||
// k indices
|
||||
const int64_t ik2 = iq2 / rk2;
|
||||
const int64_t ik3 = iq3 / rk3;
|
||||
|
||||
// v indices
|
||||
const int64_t iv2 = iq2 / rv2;
|
||||
const int64_t iv3 = iq3 / rv3;
|
||||
|
||||
// load Q to shared memory
|
||||
for (int64_t d = 0; d < D; ++d) {
|
||||
pq[d] = ((device const half *) ((device const char *) q + (iq1*nb01 + iq2*nb02 + iq3*nb03)))[d];
|
||||
}
|
||||
|
||||
for (int64_t ic = 0; ic < ne11; ++ic) {
|
||||
const half mv = mp ? mp[ic] : 0.0h;
|
||||
if (mv == -INFINITY) {
|
||||
continue;
|
||||
}
|
||||
|
||||
half s = 0.0f;
|
||||
|
||||
//device const half * pq = (device const half *) ((device char *) q + (iq1*nb01 + iq2*nb02 + iq3*nb03));
|
||||
device const half * pk = (device const half *) ((device char *) k + ( ic*nb11 + ik2*nb12 + ik3*nb13));
|
||||
|
||||
for (int64_t d = 0; d < D; ++d) {
|
||||
s += pk[d] * pq[d];
|
||||
}
|
||||
|
||||
s = s*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 < D; ++d) {
|
||||
V16[d] *= ms;
|
||||
}
|
||||
} else {
|
||||
vs = exp(s - M);
|
||||
}
|
||||
|
||||
device const half * pv = (device const half *) ((device char *) v + (ic*nb21 + iv2*nb22 + iv3*nb23));
|
||||
|
||||
// V += v*exp(s - M)
|
||||
for (int64_t d = 0; d < D; ++d) {
|
||||
V16[d] += pv[d] * vs;
|
||||
}
|
||||
|
||||
S = S*ms + vs;
|
||||
}
|
||||
|
||||
for (int64_t d = 0; d < D; ++d) {
|
||||
V16[d] /= S;
|
||||
}
|
||||
|
||||
// dst indices
|
||||
const int64_t i1 = iq1;
|
||||
const int64_t i2 = iq2;
|
||||
const int64_t i3 = iq3;
|
||||
|
||||
for (int64_t d = 0; d < D; ++d) {
|
||||
dst[(i3*ne2*ne1 + i2 + i1*ne1)*D + d] = V16[d];
|
||||
}
|
||||
}
|
||||
|
||||
kernel void kernel_cpy_f16_f16(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue