Skip to content

Commit 748833a

Browse files
committed
metal : use F16 math in mul_mat kernels
ggml-ci
1 parent 6423c65 commit 748833a

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

ggml/src/ggml-metal.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1973,7 +1973,7 @@ static void ggml_metal_encode_node(
19731973
[encoder setBytes:&ne1 length:sizeof(ne1) atIndex:14];
19741974
[encoder setBytes:&r2 length:sizeof(r2) atIndex:15];
19751975
[encoder setBytes:&r3 length:sizeof(r3) atIndex:16];
1976-
[encoder setThreadgroupMemoryLength:8192 atIndex:0];
1976+
[encoder setThreadgroupMemoryLength:(4096 + 2048) atIndex:0];
19771977
[encoder dispatchThreadgroups:MTLSizeMake( (ne11 + 31)/32, (ne01 + 63)/64, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(128, 1, 1)];
19781978
} else {
19791979
int nth0 = 32;

ggml/src/ggml-metal.metal

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6310,8 +6310,8 @@ kernel void kernel_mul_mm(device const uchar * src0,
63106310
uint tiitg[[thread_index_in_threadgroup]],
63116311
uint sgitg[[simdgroup_index_in_threadgroup]]) {
63126312

6313-
threadgroup T * sa = (threadgroup T *)(shared_memory);
6314-
threadgroup float * sb = (threadgroup float *)(shared_memory + 4096);
6313+
threadgroup T * sa = (threadgroup T *)(shared_memory);
6314+
threadgroup half * sb = (threadgroup half *)(shared_memory + 4096);
63156315

63166316
const uint r0 = tgpig.y;
63176317
const uint r1 = tgpig.x;
@@ -6325,12 +6325,12 @@ kernel void kernel_mul_mm(device const uchar * src0,
63256325
short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
63266326
short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
63276327

6328-
simdgroup_T8x8 ma[4];
6329-
simdgroup_float8x8 mb[2];
6330-
simdgroup_float8x8 mc[8];
6328+
simdgroup_T8x8 ma[4];
6329+
simdgroup_half8x8 mb[2];
6330+
simdgroup_half8x8 mc[8];
63316331

63326332
for (short i = 0; i < 8; i++){
6333-
mc[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
6333+
mc[i] = make_filled_simdgroup_matrix<half, 8>(0.h);
63346334
}
63356335

63366336
short il = (tiitg % THREAD_PER_ROW);
@@ -6361,17 +6361,17 @@ kernel void kernel_mul_mm(device const uchar * src0,
63616361
+ (tiitg/THREAD_PER_ROW)%8 + (i&7)*8) = temp_a[i/4][i%4];
63626362
}
63636363

6364-
*(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL)*8*32 + 8*(tiitg/THREAD_PER_COL)) = *((device float2x4 *) y);
6364+
*(threadgroup half2x4 *)(sb + (tiitg%THREAD_PER_COL)*8*32 + 8*(tiitg/THREAD_PER_COL)) = (half2x4)(*((device float2x4 *)y));
63656365

63666366
il = (il + 2 < nl) ? il + 2 : il % 2;
6367-
x = (il < 2) ? x + (2+nl-1)/nl : x;
6367+
x = (il < 2) ? x + (2 + nl - 1)/nl : x;
63686368
y += BLOCK_SIZE_K;
63696369

63706370
threadgroup_barrier(mem_flags::mem_threadgroup);
63716371

63726372
// load matrices from threadgroup memory and conduct outer products
6373-
threadgroup T * lsma = (sa + THREAD_MAT_M*SG_MAT_SIZE*(sgitg%2));
6374-
threadgroup float * lsmb = (sb + THREAD_MAT_N*SG_MAT_SIZE*(sgitg/2));
6373+
threadgroup T * lsma = (sa + THREAD_MAT_M*SG_MAT_SIZE*(sgitg%2));
6374+
threadgroup half * lsmb = (sb + THREAD_MAT_N*SG_MAT_SIZE*(sgitg/2));
63756375

63766376
#pragma unroll(4)
63776377
for (short ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
@@ -6399,15 +6399,22 @@ kernel void kernel_mul_mm(device const uchar * src0,
63996399
device float * C = dst + (BLOCK_SIZE_M * r0 + 32 * (sgitg & 1)) \
64006400
+ (BLOCK_SIZE_N * r1 + 16 * (sgitg >> 1)) * ne0 + im*ne1*ne0;
64016401
for (short i = 0; i < 8; i++) {
6402-
simdgroup_store(mc[i], C + 8 * (i%4) + 8 * ne0 * (i/4), ne0);
6402+
// cast to f32
6403+
simdgroup_float8x8 mc_f32(1.0f);
6404+
simdgroup_multiply(mc_f32, mc[i], mc_f32);
6405+
simdgroup_store(mc_f32, C + 8 * (i%4) + 8 * ne0 * (i/4), ne0);
6406+
//simdgroup_store(mc[i], C + 8 * (i%4) + 8 * ne0 * (i/4), ne0);
64036407
}
64046408
} else {
64056409
// block is smaller than 64x32, we should avoid writing data outside of the matrix
64066410
threadgroup_barrier(mem_flags::mem_threadgroup);
64076411
threadgroup float * temp_str = ((threadgroup float *) shared_memory) \
6408-
+ 32 * (sgitg&1) + (16 * (sgitg>>1))*BLOCK_SIZE_M;
6412+
+ 32*(sgitg&1) + (16*(sgitg >> 1))*BLOCK_SIZE_M;
64096413
for (short i = 0; i < 8; i++) {
6410-
simdgroup_store(mc[i], temp_str + 8*(i%4) + 8*BLOCK_SIZE_M*(i/4), BLOCK_SIZE_M);
6414+
simdgroup_float8x8 mc_f32(1.0f);
6415+
simdgroup_multiply(mc_f32, mc[i], mc_f32);
6416+
simdgroup_store(mc_f32, temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
6417+
//simdgroup_store(mc[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
64116418
}
64126419

64136420
threadgroup_barrier(mem_flags::mem_threadgroup);

0 commit comments

Comments
 (0)