mirror of
				https://github.com/ggml-org/llama.cpp.git
				synced 2025-10-31 08:51:55 +00:00 
			
		
		
		
	vulkan: use vec dot for matrix matrix multiplications (#16056)
* vulkan: Change the mul_mm shared memory and register caching system to use vec2 instead of scalars, to enable using dot2 instructions * use fma instead of dot to fix Nvidia and Apple performance issues
This commit is contained in:
		| @@ -31,10 +31,10 @@ | ||||
| #include "types.comp" | ||||
|  | ||||
| #ifndef LOAD_VEC_A | ||||
| #define LOAD_VEC_A 1 | ||||
| #define LOAD_VEC_A 2 | ||||
| #endif | ||||
| #ifndef LOAD_VEC_B | ||||
| #define LOAD_VEC_B 1 | ||||
| #define LOAD_VEC_B 2 | ||||
| #endif | ||||
|  | ||||
| #if !defined(TO_FLOAT_TYPE) | ||||
| @@ -98,13 +98,13 @@ layout (constant_id = 9) const uint TK = 1;  // Only needed for coopmat | ||||
| layout (constant_id = 10) const uint WARP = 32; | ||||
|  | ||||
| #ifdef COOPMAT | ||||
| #define SHMEM_STRIDE (BK + 8) | ||||
| #define SHMEM_STRIDE (BK / 2 + 4) | ||||
| #else | ||||
| #define SHMEM_STRIDE (BK + 1) | ||||
| #define SHMEM_STRIDE (BK / 2 + 1) | ||||
| #endif | ||||
|  | ||||
| shared FLOAT_TYPE buf_a[BM * SHMEM_STRIDE]; | ||||
| shared FLOAT_TYPE buf_b[BN * SHMEM_STRIDE]; | ||||
| shared FLOAT_TYPE_VEC2 buf_a[BM * SHMEM_STRIDE]; | ||||
| shared FLOAT_TYPE_VEC2 buf_b[BN * SHMEM_STRIDE]; | ||||
|  | ||||
| #define NUM_WARPS (BLOCK_SIZE / WARP) | ||||
|  | ||||
| @@ -302,8 +302,8 @@ void main() { | ||||
|     } | ||||
| #else | ||||
|     ACC_TYPE sums[WMITER * TM * WNITER * TN]; | ||||
|     FLOAT_TYPE cache_a[WMITER * TM]; | ||||
|     FLOAT_TYPE cache_b[TN]; | ||||
|     FLOAT_TYPE_VEC2 cache_a[WMITER * TM]; | ||||
|     FLOAT_TYPE_VEC2 cache_b[TN]; | ||||
|  | ||||
|     [[unroll]] for (uint i = 0; i < WMITER*TM*WNITER*TN; i++) { | ||||
|         sums[i] = ACC_TYPE(0.0f); | ||||
| @@ -312,13 +312,13 @@ void main() { | ||||
|  | ||||
|     for (uint block = start_k; block < end_k; block += BK) { | ||||
|         [[unroll]] for (uint l = 0; l < BM; l += loadstride_a) { | ||||
|             load_a_to_shmem(pos_a, loadr_a, loadc_a + l, ir * BM + loadc_a + l, block + loadr_a, end_k); | ||||
|             load_a_to_shmem(pos_a, loadr_a, loadc_a + l, ir * BM + loadc_a + l, block, end_k); | ||||
|         } | ||||
|         [[unroll]] for (uint l = 0; l < BN; l += loadstride_b) { | ||||
| #if !defined(MUL_MAT_ID) | ||||
|             load_b_to_shmem(pos_b, loadr_b, loadc_b + l, ic * BN + loadc_b + l, block + loadr_b, end_k); | ||||
|             load_b_to_shmem(pos_b, loadr_b, loadc_b + l, ic * BN + loadc_b + l, block, end_k); | ||||
| #else | ||||
|             load_b_to_shmem(pos_b, loadr_b, loadc_b + l, ic, _ne1, block + loadr_b, end_k); | ||||
|             load_b_to_shmem(pos_b, loadr_b, loadc_b + l, ic, _ne1, block, end_k); | ||||
| #endif | ||||
|         } | ||||
|  | ||||
| @@ -331,17 +331,17 @@ void main() { | ||||
|         [[unroll]] for (uint i = 0; i < BK; i += TK) { | ||||
|             [[unroll]] for (uint cm_row = 0; cm_row < cms_per_row; cm_row++) { | ||||
|                 // Load from shared into cache | ||||
|                 coopMatLoad(cache_a, buf_a, (warp_r * WM + cm_row * TM) * SHMEM_STRIDE + i, SHMEM_STRIDE, gl_CooperativeMatrixLayoutRowMajor); | ||||
|                 coopMatLoad(cache_a, buf_a, (warp_r * WM + cm_row * TM) * SHMEM_STRIDE + i / 2, SHMEM_STRIDE, gl_CooperativeMatrixLayoutRowMajor); | ||||
|  | ||||
|                 [[unroll]] for (uint cm_col = 0; cm_col < cms_per_col; cm_col++) { | ||||
|                     coopMatLoad(cache_b, buf_b, (warp_c * WN + cm_col * TN) * SHMEM_STRIDE + i, SHMEM_STRIDE, gl_CooperativeMatrixLayoutColumnMajor); | ||||
|                     coopMatLoad(cache_b, buf_b, (warp_c * WN + cm_col * TN) * SHMEM_STRIDE + i / 2, SHMEM_STRIDE, gl_CooperativeMatrixLayoutColumnMajor); | ||||
|  | ||||
|                     sums[cm_col * cms_per_row + cm_row] = coopMatMulAdd(cache_a, cache_b, sums[cm_col * cms_per_row + cm_row]); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| #else | ||||
|         [[unroll]] for (uint i = 0; i < BK; i++) { | ||||
|         [[unroll]] for (uint i = 0; i < BK / 2; i++) { | ||||
|             // Load from shared into cache | ||||
|             [[unroll]] for (uint wsir = 0; wsir < WMITER; wsir++) { | ||||
|                 [[unroll]] for (uint j = 0; j < TM; j++) { | ||||
| @@ -357,7 +357,7 @@ void main() { | ||||
|                     [[unroll]] for (uint cc = 0; cc < TN; cc++) { | ||||
|                         [[unroll]] for (uint cr = 0; cr < TM; cr++) { | ||||
|                             const uint sums_idx = (wsic * TN + cc) * (WMITER * TM) + wsir * TM + cr; | ||||
|                             sums[sums_idx] = fma(ACC_TYPE(cache_a[wsir * TM + cr]), ACC_TYPE(cache_b[cc]), sums[sums_idx]); | ||||
|                             sums[sums_idx] = fma(ACC_TYPE(cache_a[wsir * TM + cr].x), ACC_TYPE(cache_b[cc].x), fma(ACC_TYPE(cache_a[wsir * TM + cr].y), ACC_TYPE(cache_b[cc].y), sums[sums_idx])); | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|   | ||||
| @@ -1,51 +1,53 @@ | ||||
| void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uint idx_m, const uint idx_k, const uint end_k) { | ||||
| void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uint idx_m, const uint block, const uint end_k) { | ||||
| #if defined(DATA_A_F32) || defined(DATA_A_F16) | ||||
| #if LOAD_VEC_A == 8 | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A / 2; | ||||
|             FLOAT_TYPE_VEC8 aa = FLOAT_TYPE_VEC8(data_a[idx]); | ||||
|             buf_a[buf_idx    ] = aa[0].x; | ||||
|             buf_a[buf_idx + 1] = aa[0].y; | ||||
|             buf_a[buf_idx + 2] = aa[0].z; | ||||
|             buf_a[buf_idx + 3] = aa[0].w; | ||||
|             buf_a[buf_idx + 4] = aa[1].x; | ||||
|             buf_a[buf_idx + 5] = aa[1].y; | ||||
|             buf_a[buf_idx + 6] = aa[1].z; | ||||
|             buf_a[buf_idx + 7] = aa[1].w; | ||||
|             buf_a[buf_idx    ] = aa[0].xy; | ||||
|             buf_a[buf_idx + 1] = aa[0].zw; | ||||
|             buf_a[buf_idx + 2] = aa[1].xy; | ||||
|             buf_a[buf_idx + 3] = aa[1].zw; | ||||
| #elif LOAD_VEC_A == 4 | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A / 2; | ||||
|             FLOAT_TYPE_VEC4 aa = FLOAT_TYPE_VEC4(data_a[idx]); | ||||
|             buf_a[buf_idx    ] = aa.x; | ||||
|             buf_a[buf_idx + 1] = aa.y; | ||||
|             buf_a[buf_idx + 2] = aa.z; | ||||
|             buf_a[buf_idx + 3] = aa.w; | ||||
| #else | ||||
|             if (idx_m < p.M && idx_k < end_k) { | ||||
|                 buf_a[col * SHMEM_STRIDE + row] = FLOAT_TYPE(data_a[pos_a + col * p.stride_a + row]); | ||||
|             buf_a[buf_idx    ] = aa.xy; | ||||
|             buf_a[buf_idx + 1] = aa.zw; | ||||
| #else // LOAD_VEC_A == 2 | ||||
|             const uint idx = pos_a * 2 + col * p.stride_a + row * 2; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row; | ||||
|             if (idx_m < p.M && block + row * 2 + 1 < end_k) { | ||||
|                 buf_a[buf_idx] = FLOAT_TYPE_VEC2(data_a[idx], | ||||
|                                                  data_a[idx + 1]); | ||||
|             } else if (idx_m < p.M && block + row * 2 < end_k) { | ||||
|                 buf_a[buf_idx] = FLOAT_TYPE_VEC2(data_a[idx], 0.0f); | ||||
|             } else { | ||||
|                 buf_a[col * SHMEM_STRIDE + row] = FLOAT_TYPE(0.0f); | ||||
|                 buf_a[buf_idx] = FLOAT_TYPE_VEC2(0.0f); | ||||
|             } | ||||
| #endif | ||||
| #elif defined(DATA_A_BF16) | ||||
| #if LOAD_VEC_A == 4 | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A / 2; | ||||
|             FLOAT_TYPE_VEC4 aa = FLOAT_TYPE_VEC4(TO_FLOAT_TYPE(data_a[idx])); | ||||
|             buf_a[buf_idx    ] = aa.x; | ||||
|             buf_a[buf_idx + 1] = aa.y; | ||||
|             buf_a[buf_idx + 2] = aa.z; | ||||
|             buf_a[buf_idx + 3] = aa.w; | ||||
| #else | ||||
|             if (idx_m < p.M && idx_k < end_k) { | ||||
|                 buf_a[col * SHMEM_STRIDE + row] = TO_FLOAT_TYPE(data_a[pos_a + col * p.stride_a + row]); | ||||
|             buf_a[buf_idx    ] = aa.xy; | ||||
|             buf_a[buf_idx + 1] = aa.zw; | ||||
| #else // LOAD_VEC_A == 2 | ||||
|             const uint idx = pos_a * 2 + col * p.stride_a + row * 2; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row; | ||||
|             if (idx_m < p.M && block + row * 2 + 1 < end_k) { | ||||
|                 buf_a[buf_idx] = FLOAT_TYPE_VEC2(TO_FLOAT_TYPE(data_a[idx]), | ||||
|                                                  TO_FLOAT_TYPE(data_a[idx + 1])); | ||||
|             } else if (idx_m < p.M && block + row * 2 < end_k) { | ||||
|                 buf_a[buf_idx] = FLOAT_TYPE_VEC2(TO_FLOAT_TYPE(data_a[idx]), 0.0f); | ||||
|             } else { | ||||
|                 buf_a[col * SHMEM_STRIDE + row] = TO_FLOAT_TYPE(uint16_t(0)); | ||||
|                 buf_a[buf_idx] = FLOAT_TYPE_VEC2(0.0f); | ||||
|             } | ||||
| #endif | ||||
| #elif defined(DATA_A_Q4_0) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + 4 * row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + 2 * row; | ||||
|  | ||||
|             const uint ib = idx / 4; | ||||
|             const uint iqs = idx & 0x03; | ||||
| @@ -55,17 +57,13 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|             const vec4 v0 = (vec4(unpack8(vui & 0x0F0F0F0F)) - 8.0f) * d; | ||||
|             const vec4 v1 = (vec4(unpack8((vui >> 4) & 0x0F0F0F0F)) - 8.0f) * d; | ||||
|  | ||||
|             buf_a[buf_idx     ] = FLOAT_TYPE(v0.x); | ||||
|             buf_a[buf_idx + 1 ] = FLOAT_TYPE(v0.y); | ||||
|             buf_a[buf_idx + 2 ] = FLOAT_TYPE(v0.z); | ||||
|             buf_a[buf_idx + 3 ] = FLOAT_TYPE(v0.w); | ||||
|             buf_a[buf_idx + 16] = FLOAT_TYPE(v1.x); | ||||
|             buf_a[buf_idx + 17] = FLOAT_TYPE(v1.y); | ||||
|             buf_a[buf_idx + 18] = FLOAT_TYPE(v1.z); | ||||
|             buf_a[buf_idx + 19] = FLOAT_TYPE(v1.w); | ||||
|             buf_a[buf_idx    ] = FLOAT_TYPE_VEC2(v0.xy); | ||||
|             buf_a[buf_idx + 1] = FLOAT_TYPE_VEC2(v0.zw); | ||||
|             buf_a[buf_idx + 8] = FLOAT_TYPE_VEC2(v1.xy); | ||||
|             buf_a[buf_idx + 9] = FLOAT_TYPE_VEC2(v1.zw); | ||||
| #elif defined(DATA_A_Q4_1) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + 4 * row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + 2 * row; | ||||
|  | ||||
|             const uint ib = idx / 4; | ||||
|             const uint iqs = idx & 0x03; | ||||
| @@ -76,17 +74,13 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|             const vec4 v0 = vec4(unpack8(vui & 0x0F0F0F0F)) * d + m; | ||||
|             const vec4 v1 = vec4(unpack8((vui >> 4) & 0x0F0F0F0F)) * d + m; | ||||
|  | ||||
|             buf_a[buf_idx     ] = FLOAT_TYPE(v0.x); | ||||
|             buf_a[buf_idx + 1 ] = FLOAT_TYPE(v0.y); | ||||
|             buf_a[buf_idx + 2 ] = FLOAT_TYPE(v0.z); | ||||
|             buf_a[buf_idx + 3 ] = FLOAT_TYPE(v0.w); | ||||
|             buf_a[buf_idx + 16] = FLOAT_TYPE(v1.x); | ||||
|             buf_a[buf_idx + 17] = FLOAT_TYPE(v1.y); | ||||
|             buf_a[buf_idx + 18] = FLOAT_TYPE(v1.z); | ||||
|             buf_a[buf_idx + 19] = FLOAT_TYPE(v1.w); | ||||
|             buf_a[buf_idx     ] = FLOAT_TYPE_VEC2(v0.xy); | ||||
|             buf_a[buf_idx + 1 ] = FLOAT_TYPE_VEC2(v0.zw); | ||||
|             buf_a[buf_idx + 8 ] = FLOAT_TYPE_VEC2(v1.xy); | ||||
|             buf_a[buf_idx + 9 ] = FLOAT_TYPE_VEC2(v1.zw); | ||||
| #elif defined(DATA_A_Q5_0) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + 2 * row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row; | ||||
|  | ||||
|             const uint ib = idx / 8; | ||||
|             const uint iqs = idx & 0x07; | ||||
| @@ -99,13 +93,11 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|             const uint vui = uint(data_a_packed16[ib].qs[iqs]); | ||||
|             const vec4 v = (vec4((vui & 0xF) | qh0.x, ((vui >> 4) & 0xF) | qh0.y, ((vui >> 8) & 0xF) | qh1.x, (vui >> 12) | qh1.y) - 16.0f) * d; | ||||
|  | ||||
|             buf_a[buf_idx     ] = FLOAT_TYPE(v.x); | ||||
|             buf_a[buf_idx + 1 ] = FLOAT_TYPE(v.z); | ||||
|             buf_a[buf_idx + 16] = FLOAT_TYPE(v.y); | ||||
|             buf_a[buf_idx + 17] = FLOAT_TYPE(v.w); | ||||
|             buf_a[buf_idx    ] = FLOAT_TYPE_VEC2(v.xz); | ||||
|             buf_a[buf_idx + 8] = FLOAT_TYPE_VEC2(v.yw); | ||||
| #elif defined(DATA_A_Q5_1) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + 2 * row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row; | ||||
|  | ||||
|             const uint ib = idx / 8; | ||||
|             const uint iqs = idx & 0x07; | ||||
| @@ -119,13 +111,11 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|             const uint vui = uint(data_a_packed16[ib].qs[iqs]); | ||||
|             const vec4 v = vec4((vui & 0xF) | qh0.x, ((vui >> 4) & 0xF) | qh0.y, ((vui >> 8) & 0xF) | qh1.x, (vui >> 12) | qh1.y) * d + m; | ||||
|  | ||||
|             buf_a[buf_idx     ] = FLOAT_TYPE(v.x); | ||||
|             buf_a[buf_idx + 1 ] = FLOAT_TYPE(v.z); | ||||
|             buf_a[buf_idx + 16] = FLOAT_TYPE(v.y); | ||||
|             buf_a[buf_idx + 17] = FLOAT_TYPE(v.w); | ||||
|             buf_a[buf_idx    ] = FLOAT_TYPE_VEC2(v.xz); | ||||
|             buf_a[buf_idx + 8] = FLOAT_TYPE_VEC2(v.yw); | ||||
| #elif defined(DATA_A_Q8_0) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A / 2; | ||||
|  | ||||
|             const uint ib = idx / 8; | ||||
|             const uint iqs = idx & 0x07; | ||||
| @@ -135,13 +125,11 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|             const i8vec2 v1 = unpack8(int32_t(data_a_packed16[ib].qs[2*iqs + 1])).xy; | ||||
|             const vec4 v = vec4(v0.x, v0.y, v1.x, v1.y) * d; | ||||
|  | ||||
|             buf_a[buf_idx    ] = FLOAT_TYPE(v.x); | ||||
|             buf_a[buf_idx + 1] = FLOAT_TYPE(v.y); | ||||
|             buf_a[buf_idx + 2] = FLOAT_TYPE(v.z); | ||||
|             buf_a[buf_idx + 3] = FLOAT_TYPE(v.w); | ||||
|             buf_a[buf_idx    ] = FLOAT_TYPE_VEC2(v.xy); | ||||
|             buf_a[buf_idx + 1] = FLOAT_TYPE_VEC2(v.zw); | ||||
| #elif defined(DATA_A_Q2_K) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A / 2; | ||||
|  | ||||
|             const uint ib = idx / 128;                         // 2 values per idx | ||||
|             const uint iqs = idx % 128;                        // 0..127 | ||||
| @@ -156,11 +144,10 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|  | ||||
|             const vec2 v = d.x * float(scales & 0xF) * vec2((qs >> qsshift) & 3) - d.y * float(scales >> 4); | ||||
|  | ||||
|             buf_a[buf_idx    ] = FLOAT_TYPE(v.x); | ||||
|             buf_a[buf_idx + 1] = FLOAT_TYPE(v.y); | ||||
|             buf_a[buf_idx] = FLOAT_TYPE_VEC2(v.xy); | ||||
| #elif defined(DATA_A_Q3_K) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A / 2; | ||||
|  | ||||
|             const uint ib = idx / 128;                   // 2 values per idx | ||||
|             const uint iqs = idx % 128;                  // 0..127 | ||||
| @@ -178,11 +165,11 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|                                   | (((data_a[ib].scales[8 + (is % 4)] >> (2 * int(is / 4))) & 3) << 4)); | ||||
|             const float dl = float(data_a[ib].d) * float(us - 32); | ||||
|  | ||||
|             buf_a[buf_idx    ] = FLOAT_TYPE(dl * float(int8_t((data_a[ib].qs[qsi    ] >> qsshift) & 3) - (((data_a[ib].hmask[hmi    ] & m) != 0) ? 0 : 4))); | ||||
|             buf_a[buf_idx + 1] = FLOAT_TYPE(dl * float(int8_t((data_a[ib].qs[qsi + 1] >> qsshift) & 3) - (((data_a[ib].hmask[hmi + 1] & m) != 0) ? 0 : 4))); | ||||
|             buf_a[buf_idx] = FLOAT_TYPE_VEC2(dl * float(int8_t((data_a[ib].qs[qsi    ] >> qsshift) & 3) - (((data_a[ib].hmask[hmi    ] & m) != 0) ? 0 : 4)), | ||||
|                                              dl * float(int8_t((data_a[ib].qs[qsi + 1] >> qsshift) & 3) - (((data_a[ib].hmask[hmi + 1] & m) != 0) ? 0 : 4))); | ||||
| #elif defined(DATA_A_Q4_K) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A / 2; | ||||
|  | ||||
|             const uint ib = idx / 128;                 // 2 values per idx | ||||
|             const uint iqs = idx % 128;                // 0..127 | ||||
| @@ -211,11 +198,11 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|             const float d = loadd.x * sc; | ||||
|             const float m = -loadd.y * mbyte; | ||||
|  | ||||
|             buf_a[buf_idx    ] = FLOAT_TYPE(fma(d, float((data_a[ib].qs[qsi    ] >> (b * 4)) & 0xF), m)); | ||||
|             buf_a[buf_idx + 1] = FLOAT_TYPE(fma(d, float((data_a[ib].qs[qsi + 1] >> (b * 4)) & 0xF), m)); | ||||
|             buf_a[buf_idx] = FLOAT_TYPE_VEC2(fma(d, float((data_a[ib].qs[qsi    ] >> (b * 4)) & 0xF), m), | ||||
|                                              fma(d, float((data_a[ib].qs[qsi + 1] >> (b * 4)) & 0xF), m)); | ||||
| #elif defined(DATA_A_Q5_K) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A / 2; | ||||
|  | ||||
|             const uint ib = idx / 128;                 // 2 values per idx | ||||
|             const uint iqs = idx % 128;                // 0..127 | ||||
| @@ -247,11 +234,11 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|             const float d = loadd.x * sc; | ||||
|             const float m = -loadd.y * mbyte; | ||||
|  | ||||
|             buf_a[buf_idx    ] = FLOAT_TYPE(fma(d, float((data_a[ib].qs[qsi    ] >> (b * 4)) & 0xF) + float((data_a[ib].qh[qhi    ] & hm) != 0 ? 16 : 0), m)); | ||||
|             buf_a[buf_idx + 1] = FLOAT_TYPE(fma(d, float((data_a[ib].qs[qsi + 1] >> (b * 4)) & 0xF) + float((data_a[ib].qh[qhi + 1] & hm) != 0 ? 16 : 0), m)); | ||||
|             buf_a[buf_idx] = FLOAT_TYPE_VEC2(fma(d, float((data_a[ib].qs[qsi    ] >> (b * 4)) & 0xF) + float((data_a[ib].qh[qhi    ] & hm) != 0 ? 16 : 0), m), | ||||
|                                              fma(d, float((data_a[ib].qs[qsi + 1] >> (b * 4)) & 0xF) + float((data_a[ib].qh[qhi + 1] & hm) != 0 ? 16 : 0), m)); | ||||
| #elif defined(DATA_A_Q6_K) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A / 2; | ||||
|  | ||||
|             const uint ib = idx / 128;                  // 2 values per idx | ||||
|             const uint iqs = idx % 128;                 // 0..127 | ||||
| @@ -266,11 +253,11 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|  | ||||
|             const float dscale = float(data_a[ib].d) * float(data_a[ib].scales[is]); | ||||
|  | ||||
|             buf_a[buf_idx    ] = FLOAT_TYPE(dscale * float(int8_t(((data_a[ib].ql[qsi    ] >> (b * 4)) & 0xF) | (((data_a[ib].qh[qhi    ] >> qhshift) & 3) << 4)) - 32)); | ||||
|             buf_a[buf_idx + 1] = FLOAT_TYPE(dscale * float(int8_t(((data_a[ib].ql[qsi + 1] >> (b * 4)) & 0xF) | (((data_a[ib].qh[qhi + 1] >> qhshift) & 3) << 4)) - 32)); | ||||
|             buf_a[buf_idx] = FLOAT_TYPE_VEC2(dscale * float(int8_t(((data_a[ib].ql[qsi    ] >> (b * 4)) & 0xF) | (((data_a[ib].qh[qhi    ] >> qhshift) & 3) << 4)) - 32), | ||||
|                                              dscale * float(int8_t(((data_a[ib].ql[qsi + 1] >> (b * 4)) & 0xF) | (((data_a[ib].qh[qhi + 1] >> qhshift) & 3) << 4)) - 32)); | ||||
| #elif defined(DATA_A_IQ1_S) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A / 2; | ||||
|  | ||||
|             const uint ib = idx / 32;                  // 8 values per idx | ||||
|             const uint ib32 = (idx % 32) / 4;         // 0..7 | ||||
| @@ -283,12 +270,13 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|             const float delta = ((qh & 0x8000) != 0) ? -IQ1S_DELTA : IQ1S_DELTA; | ||||
|             const int16_t grid = int16_t(iq1s_grid[qs | (bitfieldExtract(qh, 3 * int(ib8 & 3), 3) << 8)]); | ||||
|  | ||||
|             [[unroll]] for (int k = 0; k < 8; ++k) { | ||||
|                 buf_a[buf_idx + k] = FLOAT_TYPE(dl * (bitfieldExtract(grid, 2 * k, 2) + delta)); | ||||
|             [[unroll]] for (int k = 0; k < 4; ++k) { | ||||
|                 buf_a[buf_idx + k] = FLOAT_TYPE_VEC2(dl * (bitfieldExtract(grid, 4 * k    , 2) + delta), | ||||
|                                                      dl * (bitfieldExtract(grid, 4 * k + 2, 2) + delta)); | ||||
|             } | ||||
| #elif defined(DATA_A_IQ1_M) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A / 2; | ||||
|  | ||||
|             const uint ib = idx / 32;  // 8 values per idx | ||||
|             const uint ib8 = idx % 32; | ||||
| @@ -304,12 +292,13 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|             const float delta = ((qh & 8) != 0) ? -IQ1M_DELTA : IQ1M_DELTA; | ||||
|             const int16_t grid = int16_t(iq1s_grid[qs | ((qh & 7) << 8)]); | ||||
|  | ||||
|             [[unroll]] for (int k = 0; k < 8; ++k) { | ||||
|                 buf_a[buf_idx + k] = FLOAT_TYPE(dl * (bitfieldExtract(grid, 2 * k, 2) + delta)); | ||||
|             [[unroll]] for (int k = 0; k < 4; ++k) { | ||||
|                 buf_a[buf_idx + k] = FLOAT_TYPE_VEC2(dl * (bitfieldExtract(grid, 4 * k    , 2) + delta), | ||||
|                                                      dl * (bitfieldExtract(grid, 4 * k + 2, 2) + delta)); | ||||
|             } | ||||
| #elif defined(DATA_A_IQ2_XXS) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A / 2; | ||||
|  | ||||
|             const uint ib = idx / 32;                 // 8 values per idx | ||||
|             const uint ib32 = (idx % 32) / 4;         // 0..7 | ||||
| @@ -330,17 +319,17 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|             const vec4 grid0 = vec4(unpack8(grid.x)); | ||||
|             const vec4 grid1 = vec4(unpack8(grid.y)); | ||||
|  | ||||
|             buf_a[buf_idx    ] = db * FLOAT_TYPE((sign &   1) != 0 ? -grid0.x : grid0.x); | ||||
|             buf_a[buf_idx + 1] = db * FLOAT_TYPE((sign &   2) != 0 ? -grid0.y : grid0.y); | ||||
|             buf_a[buf_idx + 2] = db * FLOAT_TYPE((sign &   4) != 0 ? -grid0.z : grid0.z); | ||||
|             buf_a[buf_idx + 3] = db * FLOAT_TYPE((sign &   8) != 0 ? -grid0.w : grid0.w); | ||||
|             buf_a[buf_idx + 4] = db * FLOAT_TYPE((sign &  16) != 0 ? -grid1.x : grid1.x); | ||||
|             buf_a[buf_idx + 5] = db * FLOAT_TYPE((sign &  32) != 0 ? -grid1.y : grid1.y); | ||||
|             buf_a[buf_idx + 6] = db * FLOAT_TYPE((sign &  64) != 0 ? -grid1.z : grid1.z); | ||||
|             buf_a[buf_idx + 7] = db * FLOAT_TYPE((sign & 128) != 0 ? -grid1.w : grid1.w); | ||||
|             buf_a[buf_idx    ] = db * FLOAT_TYPE_VEC2((sign &   1) != 0 ? -grid0.x : grid0.x, | ||||
|                                                       (sign &   2) != 0 ? -grid0.y : grid0.y); | ||||
|             buf_a[buf_idx + 1] = db * FLOAT_TYPE_VEC2((sign &   4) != 0 ? -grid0.z : grid0.z, | ||||
|                                                       (sign &   8) != 0 ? -grid0.w : grid0.w); | ||||
|             buf_a[buf_idx + 2] = db * FLOAT_TYPE_VEC2((sign &  16) != 0 ? -grid1.x : grid1.x, | ||||
|                                                       (sign &  32) != 0 ? -grid1.y : grid1.y); | ||||
|             buf_a[buf_idx + 3] = db * FLOAT_TYPE_VEC2((sign &  64) != 0 ? -grid1.z : grid1.z, | ||||
|                                                       (sign & 128) != 0 ? -grid1.w : grid1.w); | ||||
| #elif defined(DATA_A_IQ2_XS) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A / 2; | ||||
|  | ||||
|             const uint ib = idx / 32;            // 8 values per idx | ||||
|             const uint ib32 = (idx % 32) / 4;    // 0..7 | ||||
| @@ -356,17 +345,17 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|             const vec4 grid0 = vec4(unpack8(grid.x)); | ||||
|             const vec4 grid1 = vec4(unpack8(grid.y)); | ||||
|  | ||||
|             buf_a[buf_idx    ] = db * FLOAT_TYPE((sign &   1) != 0 ? -grid0.x : grid0.x); | ||||
|             buf_a[buf_idx + 1] = db * FLOAT_TYPE((sign &   2) != 0 ? -grid0.y : grid0.y); | ||||
|             buf_a[buf_idx + 2] = db * FLOAT_TYPE((sign &   4) != 0 ? -grid0.z : grid0.z); | ||||
|             buf_a[buf_idx + 3] = db * FLOAT_TYPE((sign &   8) != 0 ? -grid0.w : grid0.w); | ||||
|             buf_a[buf_idx + 4] = db * FLOAT_TYPE((sign &  16) != 0 ? -grid1.x : grid1.x); | ||||
|             buf_a[buf_idx + 5] = db * FLOAT_TYPE((sign &  32) != 0 ? -grid1.y : grid1.y); | ||||
|             buf_a[buf_idx + 6] = db * FLOAT_TYPE((sign &  64) != 0 ? -grid1.z : grid1.z); | ||||
|             buf_a[buf_idx + 7] = db * FLOAT_TYPE((sign & 128) != 0 ? -grid1.w : grid1.w); | ||||
|             buf_a[buf_idx    ] = db * FLOAT_TYPE_VEC2((sign &   1) != 0 ? -grid0.x : grid0.x, | ||||
|                                                       (sign &   2) != 0 ? -grid0.y : grid0.y); | ||||
|             buf_a[buf_idx + 1] = db * FLOAT_TYPE_VEC2((sign &   4) != 0 ? -grid0.z : grid0.z, | ||||
|                                                       (sign &   8) != 0 ? -grid0.w : grid0.w); | ||||
|             buf_a[buf_idx + 2] = db * FLOAT_TYPE_VEC2((sign &  16) != 0 ? -grid1.x : grid1.x, | ||||
|                                                       (sign &  32) != 0 ? -grid1.y : grid1.y); | ||||
|             buf_a[buf_idx + 3] = db * FLOAT_TYPE_VEC2((sign &  64) != 0 ? -grid1.z : grid1.z, | ||||
|                                                       (sign & 128) != 0 ? -grid1.w : grid1.w); | ||||
| #elif defined(DATA_A_IQ2_S) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A / 2; | ||||
|  | ||||
|             const uint ib = idx / 32;  // 8 values per idx | ||||
|             const uint ib8 = idx % 32; // 0..31 | ||||
| @@ -384,17 +373,17 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|             const vec4 grid0 = vec4(unpack8(grid.x)); | ||||
|             const vec4 grid1 = vec4(unpack8(grid.y)); | ||||
|  | ||||
|             buf_a[buf_idx    ] = db * FLOAT_TYPE((sign &   1) != 0 ? -grid0.x : grid0.x); | ||||
|             buf_a[buf_idx + 1] = db * FLOAT_TYPE((sign &   2) != 0 ? -grid0.y : grid0.y); | ||||
|             buf_a[buf_idx + 2] = db * FLOAT_TYPE((sign &   4) != 0 ? -grid0.z : grid0.z); | ||||
|             buf_a[buf_idx + 3] = db * FLOAT_TYPE((sign &   8) != 0 ? -grid0.w : grid0.w); | ||||
|             buf_a[buf_idx + 4] = db * FLOAT_TYPE((sign &  16) != 0 ? -grid1.x : grid1.x); | ||||
|             buf_a[buf_idx + 5] = db * FLOAT_TYPE((sign &  32) != 0 ? -grid1.y : grid1.y); | ||||
|             buf_a[buf_idx + 6] = db * FLOAT_TYPE((sign &  64) != 0 ? -grid1.z : grid1.z); | ||||
|             buf_a[buf_idx + 7] = db * FLOAT_TYPE((sign & 128) != 0 ? -grid1.w : grid1.w); | ||||
|             buf_a[buf_idx    ] = db * FLOAT_TYPE_VEC2((sign &   1) != 0 ? -grid0.x : grid0.x, | ||||
|                                                       (sign &   2) != 0 ? -grid0.y : grid0.y); | ||||
|             buf_a[buf_idx + 1] = db * FLOAT_TYPE_VEC2((sign &   4) != 0 ? -grid0.z : grid0.z, | ||||
|                                                       (sign &   8) != 0 ? -grid0.w : grid0.w); | ||||
|             buf_a[buf_idx + 2] = db * FLOAT_TYPE_VEC2((sign &  16) != 0 ? -grid1.x : grid1.x, | ||||
|                                                       (sign &  32) != 0 ? -grid1.y : grid1.y); | ||||
|             buf_a[buf_idx + 3] = db * FLOAT_TYPE_VEC2((sign &  64) != 0 ? -grid1.z : grid1.z, | ||||
|                                                       (sign & 128) != 0 ? -grid1.w : grid1.w); | ||||
| #elif defined(DATA_A_IQ3_XXS) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A / 2; | ||||
|  | ||||
|             const uint ib = idx / 64;            // 4 values per idx | ||||
|             const uint iqs = idx % 64;           // 0..63 | ||||
| @@ -414,13 +403,13 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|             const uint grid = iq3xxs_grid[qs]; | ||||
|             const vec4 v = db * vec4(unpack8(grid)); | ||||
|  | ||||
|             buf_a[buf_idx    ] = FLOAT_TYPE((sign &   1) != 0 ? -v.x : v.x); | ||||
|             buf_a[buf_idx + 1] = FLOAT_TYPE((sign &   2) != 0 ? -v.y : v.y); | ||||
|             buf_a[buf_idx + 2] = FLOAT_TYPE((sign &   4) != 0 ? -v.z : v.z); | ||||
|             buf_a[buf_idx + 3] = FLOAT_TYPE((sign &   8) != 0 ? -v.w : v.w); | ||||
|             buf_a[buf_idx    ] = FLOAT_TYPE_VEC2((sign &   1) != 0 ? -v.x : v.x, | ||||
|                                                  (sign &   2) != 0 ? -v.y : v.y); | ||||
|             buf_a[buf_idx + 1] = FLOAT_TYPE_VEC2((sign &   4) != 0 ? -v.z : v.z, | ||||
|                                                  (sign &   8) != 0 ? -v.w : v.w); | ||||
| #elif defined(DATA_A_IQ3_S) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A / 2; | ||||
|  | ||||
|             const uint ib = idx / 64;            // 4 values per idx | ||||
|             const uint iqs = idx % 64;           // 0..63 | ||||
| @@ -436,13 +425,13 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|             const uint32_t grid = iq3s_grid[qs | ((qh << (8 - (iqs % 8))) & 256)]; | ||||
|             const vec4 v = db * vec4(unpack8(grid)); | ||||
|  | ||||
|             buf_a[buf_idx    ] = FLOAT_TYPE((sign &   1) != 0 ? -v.x : v.x); | ||||
|             buf_a[buf_idx + 1] = FLOAT_TYPE((sign &   2) != 0 ? -v.y : v.y); | ||||
|             buf_a[buf_idx + 2] = FLOAT_TYPE((sign &   4) != 0 ? -v.z : v.z); | ||||
|             buf_a[buf_idx + 3] = FLOAT_TYPE((sign &   8) != 0 ? -v.w : v.w); | ||||
|             buf_a[buf_idx    ] = FLOAT_TYPE_VEC2((sign &   1) != 0 ? -v.x : v.x, | ||||
|                                                  (sign &   2) != 0 ? -v.y : v.y); | ||||
|             buf_a[buf_idx + 1] = FLOAT_TYPE_VEC2((sign &   4) != 0 ? -v.z : v.z, | ||||
|                                                  (sign &   8) != 0 ? -v.w : v.w); | ||||
| #elif defined(DATA_A_IQ4_XS) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_A / 2; | ||||
|  | ||||
|             const uint ib = idx / 128;                  // 2 values per idx | ||||
|             const uint ib32 = (idx % 128) / 16;         // 0..7 | ||||
| @@ -457,11 +446,10 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|             const float d = float(data_a[ib].d); | ||||
|             const vec2 v = d * float(int(sl | (sh << 4)) - 32) * vec2(kvalues_iq4nl[qs.x], kvalues_iq4nl[qs.y]); | ||||
|  | ||||
|             buf_a[buf_idx    ] = FLOAT_TYPE(v.x); | ||||
|             buf_a[buf_idx + 1] = FLOAT_TYPE(v.y); | ||||
|             buf_a[buf_idx    ] = FLOAT_TYPE_VEC2(v.xy); | ||||
| #elif defined(DATA_A_IQ4_NL) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + 2 * row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row; | ||||
|  | ||||
|             const uint ib = idx / 8; | ||||
|             const uint iqs = idx & 0x07; | ||||
| @@ -469,13 +457,13 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|             const FLOAT_TYPE d = FLOAT_TYPE(data_a_packed16[ib].d); | ||||
|             const uint vui = uint(data_a_packed16[ib].qs[iqs]); | ||||
|  | ||||
|             buf_a[buf_idx     ] = FLOAT_TYPE(kvalues_iq4nl[vui & 0xF]) * d; | ||||
|             buf_a[buf_idx + 1 ] = FLOAT_TYPE(kvalues_iq4nl[bitfieldExtract(vui, 8, 4)]) * d; | ||||
|             buf_a[buf_idx + 16] = FLOAT_TYPE(kvalues_iq4nl[bitfieldExtract(vui, 4, 4)]) * d; | ||||
|             buf_a[buf_idx + 17] = FLOAT_TYPE(kvalues_iq4nl[vui >> 12]) * d; | ||||
|             buf_a[buf_idx    ] = d * FLOAT_TYPE_VEC2(kvalues_iq4nl[vui & 0xF], | ||||
|                                                       kvalues_iq4nl[bitfieldExtract(vui, 8, 4)]); | ||||
|             buf_a[buf_idx + 8] = d * FLOAT_TYPE_VEC2(kvalues_iq4nl[bitfieldExtract(vui, 4, 4)], | ||||
|                                                      kvalues_iq4nl[vui >> 12]); | ||||
| #elif defined(DATA_A_MXFP4) | ||||
|             const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + 2 * row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row; | ||||
|  | ||||
|             const uint ib = idx / 8; | ||||
|             const uint iqs = (idx & 0x07) * 2; | ||||
| @@ -484,84 +472,84 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin | ||||
|             const uint vui = uint(data_a[ib].qs[iqs]); | ||||
|             const uint vui2 = uint(data_a[ib].qs[iqs+1]); | ||||
|  | ||||
|             buf_a[buf_idx     ] = FLOAT_TYPE(kvalues_mxfp4[vui & 0xF] * d); | ||||
|             buf_a[buf_idx + 16] = FLOAT_TYPE(kvalues_mxfp4[vui >>  4] * d); | ||||
|             buf_a[buf_idx +  1] = FLOAT_TYPE(kvalues_mxfp4[vui2 & 0xF] * d); | ||||
|             buf_a[buf_idx + 17] = FLOAT_TYPE(kvalues_mxfp4[vui2 >>  4] * d); | ||||
|             buf_a[buf_idx    ] = FLOAT_TYPE_VEC2(kvalues_mxfp4[vui  & 0xF] * d, | ||||
|                                                  kvalues_mxfp4[vui2 & 0xF] * d); | ||||
|             buf_a[buf_idx + 8] = FLOAT_TYPE_VEC2(kvalues_mxfp4[vui  >>  4] * d, | ||||
|                                                  kvalues_mxfp4[vui2 >>  4] * d); | ||||
| #endif | ||||
| } | ||||
|  | ||||
| #if !defined(MUL_MAT_ID) | ||||
| void load_b_to_shmem(const uint pos_b, const uint row, const uint col, const uint idx_n, const uint idx_k, const uint end_k) { | ||||
| void load_b_to_shmem(const uint pos_b, const uint row, const uint col, const uint idx_n, const uint block, const uint end_k) { | ||||
| #if LOAD_VEC_B == 8 | ||||
|             // Not supported for b_type bf16 because bf16mat2x4 does not exist | ||||
|             const uint idx = pos_b + col * p.stride_b / LOAD_VEC_B + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_B; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_B / 2; | ||||
|             FLOAT_TYPE_VEC8 bb = FLOAT_TYPE_VEC8(data_b[idx]); | ||||
|             buf_b[buf_idx + 0] = bb[0].x; | ||||
|             buf_b[buf_idx + 1] = bb[0].y; | ||||
|             buf_b[buf_idx + 2] = bb[0].z; | ||||
|             buf_b[buf_idx + 3] = bb[0].w; | ||||
|             buf_b[buf_idx + 4] = bb[1].x; | ||||
|             buf_b[buf_idx + 5] = bb[1].y; | ||||
|             buf_b[buf_idx + 6] = bb[1].z; | ||||
|             buf_b[buf_idx + 7] = bb[1].w; | ||||
|             buf_b[buf_idx + 0] = bb[0].xy; | ||||
|             buf_b[buf_idx + 1] = bb[0].zw; | ||||
|             buf_b[buf_idx + 2] = bb[1].xy; | ||||
|             buf_b[buf_idx + 3] = bb[1].zw; | ||||
| #elif LOAD_VEC_B == 4 | ||||
|             const uint idx = pos_b + col * p.stride_b / LOAD_VEC_B + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_B; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_B / 2; | ||||
| #if defined(DATA_B_BF16) | ||||
|             FLOAT_TYPE_VEC4 bb = FLOAT_TYPE_VEC4(TO_FLOAT_TYPE(data_b[idx])); | ||||
| #else | ||||
|             FLOAT_TYPE_VEC4 bb = FLOAT_TYPE_VEC4(data_b[idx]); | ||||
| #endif | ||||
|             buf_b[buf_idx + 0] = bb.x; | ||||
|             buf_b[buf_idx + 1] = bb.y; | ||||
|             buf_b[buf_idx + 2] = bb.z; | ||||
|             buf_b[buf_idx + 3] = bb.w; | ||||
| #else // LOAD_VEC_B == 1 | ||||
|             if (idx_n < p.N && idx_k < end_k) { | ||||
|                 buf_b[col * SHMEM_STRIDE + row] = TO_FLOAT_TYPE(data_b[pos_b + col * p.stride_b + row]); | ||||
|             buf_b[buf_idx + 0] = bb.xy; | ||||
|             buf_b[buf_idx + 1] = bb.zw; | ||||
| #else // LOAD_VEC_B == 2 | ||||
|             const uint idx = pos_b * 2 + col * p.stride_b + row * 2; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row; | ||||
|             if (idx_n < p.N && block + row * 2 + 1 < end_k) { | ||||
|                 buf_b[buf_idx] = FLOAT_TYPE_VEC2(TO_FLOAT_TYPE(data_b[idx]), | ||||
|                                                  TO_FLOAT_TYPE(data_b[idx + 1])); | ||||
|             } else if (idx_n < p.N && block + row * 2 < end_k) { | ||||
|                 buf_b[buf_idx] = FLOAT_TYPE_VEC2(TO_FLOAT_TYPE(data_b[idx]), 0.0f); | ||||
|             } else { | ||||
|                 buf_b[col * SHMEM_STRIDE + row] = FLOAT_TYPE(0.0f); | ||||
|                 buf_b[buf_idx] = FLOAT_TYPE_VEC2(0.0f); | ||||
|             } | ||||
| #endif | ||||
| } | ||||
| #else | ||||
| void load_b_to_shmem(const uint pos_b, const uint row, const uint col, const uint ic, const uint _ne1, const uint idx_k, const uint end_k) { | ||||
| void load_b_to_shmem(const uint pos_b, const uint row, const uint col, const uint ic, const uint _ne1, const uint block, const uint end_k) { | ||||
| #if LOAD_VEC_B == 8 | ||||
|             // Not supported for b_type bf16 because bf16mat2x4 does not exist | ||||
|             const u16vec2 row_idx = row_ids[col]; | ||||
|             const uint idx = pos_b + row_idx.y * p.batch_stride_b / LOAD_VEC_B + (row_idx.x % p.ne11) * p.stride_b / LOAD_VEC_B + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_B; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_B / 2; | ||||
|             FLOAT_TYPE_VEC8 bb = FLOAT_TYPE_VEC8(data_b[idx]); | ||||
|             buf_b[buf_idx + 0] = bb[0].x; | ||||
|             buf_b[buf_idx + 1] = bb[0].y; | ||||
|             buf_b[buf_idx + 2] = bb[0].z; | ||||
|             buf_b[buf_idx + 3] = bb[0].w; | ||||
|             buf_b[buf_idx + 4] = bb[1].x; | ||||
|             buf_b[buf_idx + 5] = bb[1].y; | ||||
|             buf_b[buf_idx + 6] = bb[1].z; | ||||
|             buf_b[buf_idx + 7] = bb[1].w; | ||||
|             buf_b[buf_idx + 0] = bb[0].xy; | ||||
|             buf_b[buf_idx + 1] = bb[0].zw; | ||||
|             buf_b[buf_idx + 2] = bb[1].xy; | ||||
|             buf_b[buf_idx + 3] = bb[1].zw; | ||||
| #elif LOAD_VEC_B == 4 | ||||
|             const u16vec2 row_idx = row_ids[col]; | ||||
|             const uint idx = pos_b + row_idx.y * p.batch_stride_b / LOAD_VEC_B + (row_idx.x % p.ne11) * p.stride_b / LOAD_VEC_B + row; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_B; | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_B / 2; | ||||
| #if defined(DATA_B_BF16) | ||||
|             FLOAT_TYPE_VEC4 bb = FLOAT_TYPE_VEC4(TO_FLOAT_TYPE(data_b[idx])); | ||||
| #else | ||||
|             FLOAT_TYPE_VEC4 bb = FLOAT_TYPE_VEC4(data_b[idx]); | ||||
| #endif | ||||
|             buf_b[buf_idx + 0] = bb.x; | ||||
|             buf_b[buf_idx + 1] = bb.y; | ||||
|             buf_b[buf_idx + 2] = bb.z; | ||||
|             buf_b[buf_idx + 3] = bb.w; | ||||
| #else // LOAD_VEC_B == 1 | ||||
|             buf_b[buf_idx + 0] = bb.xy; | ||||
|             buf_b[buf_idx + 1] = bb.zw; | ||||
| #else // LOAD_VEC_B == 2 | ||||
|             const uint row_i = ic * BN + col; | ||||
|             if (row_i < _ne1 && idx_k < end_k) { | ||||
|             const uint buf_idx = col * SHMEM_STRIDE + row; | ||||
|             if (row_i < _ne1 && block + row * 2 + 1 < end_k) { | ||||
|                 const u16vec2 row_idx = row_ids[col]; | ||||
|                 buf_b[col * SHMEM_STRIDE + row] = TO_FLOAT_TYPE(data_b[pos_b + row_idx.y * p.batch_stride_b + (row_idx.x % p.ne11) * p.stride_b + row]); | ||||
|                 const uint idx = pos_b * 2 + row_idx.y * p.batch_stride_b + (row_idx.x % p.ne11) * p.stride_b + row * 2; | ||||
|                 buf_b[buf_idx] = FLOAT_TYPE_VEC2(TO_FLOAT_TYPE(data_b[idx]), | ||||
|                                                  TO_FLOAT_TYPE(data_b[idx + 1])); | ||||
|             } else if (row_i < _ne1 && block + row * 2 < end_k) { | ||||
|                 const u16vec2 row_idx = row_ids[col]; | ||||
|                 const uint idx = pos_b * 2 + row_idx.y * p.batch_stride_b + (row_idx.x % p.ne11) * p.stride_b + row * 2; | ||||
|                 buf_b[buf_idx] = FLOAT_TYPE_VEC2(TO_FLOAT_TYPE(data_b[idx]), 0.0f); | ||||
|             } else { | ||||
|                 buf_b[col * SHMEM_STRIDE + row] = FLOAT_TYPE(0.0f); | ||||
|                 buf_b[buf_idx] = FLOAT_TYPE_VEC2(0.0f); | ||||
|             } | ||||
| #endif | ||||
| } | ||||
|   | ||||
| @@ -11,12 +11,12 @@ | ||||
| #define QUANT_K 1 | ||||
| #define QUANT_R 1 | ||||
|  | ||||
| #if !defined(LOAD_VEC_A) || LOAD_VEC_A == 1 | ||||
| #define A_TYPE float | ||||
| #elif LOAD_VEC_A == 4 | ||||
| #if LOAD_VEC_A == 4 | ||||
| #define A_TYPE vec4 | ||||
| #elif LOAD_VEC_A == 8 | ||||
| #define A_TYPE mat2x4 | ||||
| #else | ||||
| #define A_TYPE float | ||||
| #endif | ||||
| #endif | ||||
|  | ||||
| @@ -24,12 +24,12 @@ | ||||
| #define QUANT_K 1 | ||||
| #define QUANT_R 1 | ||||
|  | ||||
| #if !defined(LOAD_VEC_A) || LOAD_VEC_A == 1 | ||||
| #define A_TYPE float16_t | ||||
| #elif LOAD_VEC_A == 4 | ||||
| #if LOAD_VEC_A == 4 | ||||
| #define A_TYPE f16vec4 | ||||
| #elif LOAD_VEC_A == 8 | ||||
| #define A_TYPE f16mat2x4 | ||||
| #else | ||||
| #define A_TYPE float16_t | ||||
| #endif | ||||
| #endif | ||||
|  | ||||
| @@ -37,12 +37,12 @@ | ||||
| #define QUANT_K 1 | ||||
| #define QUANT_R 1 | ||||
|  | ||||
| #if !defined(LOAD_VEC_A) || LOAD_VEC_A == 1 | ||||
| #define A_TYPE uint16_t | ||||
| #elif LOAD_VEC_A == 4 | ||||
| #if LOAD_VEC_A == 4 | ||||
| #define A_TYPE u16vec4 | ||||
| #elif LOAD_VEC_A == 8 | ||||
| #error unsupported | ||||
| #else | ||||
| #define A_TYPE uint16_t | ||||
| #endif | ||||
| #endif | ||||
|  | ||||
|   | ||||
| @@ -336,7 +336,8 @@ void matmul_shaders(bool fp16, MatMulIdType matmul_id_type, bool coopmat, bool c | ||||
|         base_dict["FLOAT16"] = "1"; | ||||
|     } | ||||
|  | ||||
|     base_dict["ACC_TYPE"] = f16acc ? "float16_t" : "float"; | ||||
|     base_dict["ACC_TYPE"     ] = f16acc ? "float16_t" : "float"; | ||||
|     base_dict["ACC_TYPE_VEC2"] = f16acc ? "f16vec2"   : "vec2"; | ||||
|     if (f16acc) { | ||||
|         base_dict["ACC_TYPE_MAX"] = "\"float16_t(65504.0)\""; | ||||
|     } | ||||
| @@ -418,7 +419,6 @@ void matmul_shaders(bool fp16, MatMulIdType matmul_id_type, bool coopmat, bool c | ||||
|  | ||||
|     // bf16 | ||||
|     { | ||||
|         std::string load_vec_a_unaligned = "1"; | ||||
|         // For aligned matmul loads | ||||
|         std::string load_vec_a = coopmat2 ? "1" : "4"; | ||||
|  | ||||
| @@ -436,8 +436,8 @@ void matmul_shaders(bool fp16, MatMulIdType matmul_id_type, bool coopmat, bool c | ||||
|         if (!(coopmat || coopmat2)) | ||||
| #endif | ||||
|         { | ||||
|             string_to_spv(shader_name + "_bf16_aligned", source_name, merge_maps(merge_maps(base_dict, float_type_dict_bf16), {{"TO_FLOAT_TYPE", to_float_type}, {"DATA_A_BF16", "1"}, {"LOAD_VEC_A", load_vec_a},           {"LOAD_VEC_B", "4"}, {"B_TYPE", coopmat2 ? "bfloat16_t" : "u16vec4"},  {"D_TYPE", "float"}, {"B_IS_FLOAT", "1"}, {"DATA_B_BF16", "1"}, {"ALIGNED", "1"}}), fp16, coopmat, coopmat2, f16acc); | ||||
|             string_to_spv(shader_name + "_bf16",         source_name, merge_maps(merge_maps(base_dict, float_type_dict_bf16), {{"TO_FLOAT_TYPE", to_float_type}, {"DATA_A_BF16", "1"}, {"LOAD_VEC_A", load_vec_a_unaligned},                      {"B_TYPE", coopmat2 ? "bfloat16_t" : "uint16_t"}, {"D_TYPE", "float"}, {"B_IS_FLOAT", "1"}, {"DATA_B_BF16", "1"}}),                   fp16, coopmat, coopmat2, f16acc); | ||||
|             string_to_spv(shader_name + "_bf16",         source_name, merge_maps(merge_maps(base_dict, float_type_dict_bf16), {{"TO_FLOAT_TYPE", to_float_type}, {"DATA_A_BF16", "1"},                             {"B_TYPE", coopmat2 ? "bfloat16_t" : "uint16_t"}, {"D_TYPE", "float"}, {"B_IS_FLOAT", "1"}, {"DATA_B_BF16", "1"}}),                   fp16, coopmat, coopmat2, f16acc); | ||||
|             string_to_spv(shader_name + "_bf16_aligned", source_name, merge_maps(merge_maps(base_dict, float_type_dict_bf16), {{"TO_FLOAT_TYPE", to_float_type}, {"DATA_A_BF16", "1"}, {"LOAD_VEC_A", load_vec_a}, {"LOAD_VEC_B", "4"}, {"B_TYPE", coopmat2 ? "bfloat16_t" : "u16vec4"},  {"D_TYPE", "float"}, {"B_IS_FLOAT", "1"}, {"DATA_B_BF16", "1"}, {"ALIGNED", "1"}}), fp16, coopmat, coopmat2, f16acc); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @@ -454,7 +454,7 @@ void matmul_shaders(bool fp16, MatMulIdType matmul_id_type, bool coopmat, bool c | ||||
|  | ||||
|         std::string data_a_key = "DATA_A_" + to_uppercase(tname); | ||||
|         // For unaligned, load one at a time for f32/f16, or two at a time for quants | ||||
|         std::string load_vec_a_unaligned = (coopmat2 || tname == "f32" || tname == "f16" || tname == "bf16") ? "1" : load_vec_quant; | ||||
|         std::string load_vec_a_unaligned = coopmat2 ? "1" : (tname == "f32" || tname == "f16" || tname == "bf16") ? "2" : load_vec_quant; | ||||
|         // For aligned matmul loads | ||||
|         std::string load_vec_a = (coopmat2 || tname == "f32" || tname == "f16" || tname == "bf16") ? load_vec : load_vec_quant; | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Ruben Ortlam
					Ruben Ortlam