ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-opencl / kernels / mul_mm_q3_k_f32_l4_lm.cl
| 1 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 2 | |
| 3 | #ifdef cl_intel_required_subgroup_size |
| 4 | #define INTEL_GPU 1 |
| 5 | #endif |
| 6 | |
| 7 | #define QK_K 256 |
| 8 | |
| 9 | struct block_q3_K { |
| 10 | uchar hmask[QK_K/8]; |
| 11 | uchar qs[QK_K/4]; |
| 12 | uchar scales[12]; |
| 13 | half d; |
| 14 | }; |
| 15 | |
| 16 | #define LOAD_VEC_A 4 |
| 17 | #define LOAD_VEC_B 4 |
| 18 | |
| 19 | #define BM 64 |
| 20 | #define BN 64 |
| 21 | #ifndef BK |
| 22 | #define BK 32 |
| 23 | #endif |
| 24 | #ifdef INTEL_GPU |
| 25 | #define TM 8 |
| 26 | #else |
| 27 | #define TM 4 |
| 28 | #endif |
| 29 | #define TN 8 |
| 30 | |
| 31 | #ifndef LM_HALF |
| 32 | #define LM_HALF 0 |
| 33 | #endif |
| 34 | #if LM_HALF |
| 35 | typedef half lm_st; |
| 36 | #define LM_LD4(p, i) convert_float4(vload4((i), (p))) |
| 37 | #else |
| 38 | typedef float lm_st; |
| 39 | #define LM_LD4(p, i) vload4((i), (p)) |
| 40 | #endif |
| 41 | |
| 42 | kernel void kernel_mul_mm_q3_k_f32_l4_lm( |
| 43 | global uchar * src0_qs, |
| 44 | global uchar * src0_hm, |
| 45 | global uchar * src0_sc, |
| 46 | global half * src0_d, |
| 47 | ulong offset0, |
| 48 | global float4 * src1, |
| 49 | ulong offset1, |
| 50 | global float * dst, |
| 51 | ulong offsetd, |
| 52 | |
| 53 | int ne00, |
| 54 | int ne01, |
| 55 | int ne02, |
| 56 | int ne11, |
| 57 | int ne12, |
| 58 | |
| 59 | int stride_a, |
| 60 | int stride_b, |
| 61 | int stride_d, |
| 62 | |
| 63 | int batch_stride_a, |
| 64 | int batch_stride_b, |
| 65 | int batch_stride_d, |
| 66 | |
| 67 | int r2, |
| 68 | int r3 |
| 69 | ) { |
| 70 | src1 = (global float4*)((global char*)src1 + offset1); |
| 71 | dst = (global float *)((global char*)dst + offsetd); |
| 72 | |
| 73 | local lm_st buf_a[BM * BK]; |
| 74 | local lm_st buf_b[BN * BK]; |
| 75 | |
| 76 | const int batch_idx = get_global_id(2); |
| 77 | |
| 78 | const int i13 = batch_idx / ne12; |
| 79 | const int i12 = batch_idx % ne12; |
| 80 | |
| 81 | const int i03 = i13 / r3; |
| 82 | const int i02 = i12 / r2; |
| 83 | |
| 84 | const int batch_idx_a = i03 * ne02 + i02; |
| 85 | |
| 86 | const int ir = get_group_id(0); |
| 87 | const int ic = get_group_id(1); |
| 88 | |
| 89 | const int tid = get_local_id(0); |
| 90 | const int th_r = tid % (BM / TM); |
| 91 | const int th_c = tid / (BM / TM); |
| 92 | |
| 93 | const int loadr_a = get_local_id(0) % (BK / LOAD_VEC_A); |
| 94 | const int loadc_a = get_local_id(0) / (BK / LOAD_VEC_A); |
| 95 | const int loadr_b = get_local_id(0) % (BK / LOAD_VEC_B); |
| 96 | const int loadc_b = get_local_id(0) / (BK / LOAD_VEC_B); |
| 97 | |
| 98 | const int loadstride_a = get_local_size(0) * LOAD_VEC_A / BK; |
| 99 | const int loadstride_b = get_local_size(0) * LOAD_VEC_B / BK; |
| 100 | |
| 101 | // pos_a counts elements, not blocks - one q3_K super block holds QK_K of them |
| 102 | int pos_a = batch_idx_a * batch_stride_a + ir * BM * stride_a; |
| 103 | int pos_b = (batch_idx * batch_stride_b + ic * BN * stride_b) / LOAD_VEC_B; |
| 104 | |
| 105 | float4 sums4[(TM/4) * TN]; |
| 106 | float4 cache_a4[TM/4]; |
| 107 | |
| 108 | for (int i = 0; i < (TM/4) * TN; i++) { |
| 109 | sums4[i] = (float4)(0.0f); |
| 110 | } |
| 111 | |
| 112 | for (int block = 0; block < ne00; block += BK) { |
| 113 | for (int l = 0; l < BM; l += loadstride_a) { |
| 114 | if (ir*BM + loadc_a + l < ne01) { |
| 115 | int idx = pos_a + (loadc_a + l) * stride_a + loadr_a * LOAD_VEC_A; |
| 116 | int ib = idx / QK_K; |
| 117 | ib += offset0/sizeof(struct block_q3_K); |
| 118 | int e = idx % QK_K; |
| 119 | |
| 120 | // Only qs advances in the second half of a super-block. |
| 121 | int n = e >> 7; |
| 122 | int rem = e & 127; |
| 123 | int j = rem >> 5; |
| 124 | int s = (rem >> 4) & 1; |
| 125 | int l16 = rem & 15; |
| 126 | int is = 8*n + 2*j + s; |
| 127 | int shift = 2*j; |
| 128 | uchar m = (uchar)(1u << (4*n + j)); |
| 129 | |
| 130 | global ushort * a = (global ushort *)(src0_sc + 12*ib); |
| 131 | uint a0 = (uint)a[0] | ((uint)a[1] << 16); |
| 132 | uint a1 = (uint)a[2] | ((uint)a[3] << 16); |
| 133 | uint tmp = (uint)a[4] | ((uint)a[5] << 16); |
| 134 | |
| 135 | uint g = is >> 2; |
| 136 | uint w = (g & 1u) ? a1 : a0; |
| 137 | w = ((w >> ((g >> 1)*4)) & 0x0f0f0f0fu) | (((tmp >> (2*g)) & 0x03030303u) << 4); |
| 138 | |
| 139 | uint sc6 = (w >> (8*(is & 3))) & 0xFFu; |
| 140 | float dl = (float)src0_d[ib] * ((float)sc6 - 32.0f); |
| 141 | |
| 142 | uchar4 q = vload4(0, src0_qs + 64*ib + 32*n + 16*s + l16); |
| 143 | uchar4 hb = vload4(0, src0_hm + 32*ib + 16*s + l16); |
| 144 | |
| 145 | float4 qv = convert_float4((uchar4)((q.s0 >> shift) & 3, |
| 146 | (q.s1 >> shift) & 3, |
| 147 | (q.s2 >> shift) & 3, |
| 148 | (q.s3 >> shift) & 3)); |
| 149 | float4 hv = (float4)((hb.s0 & m) ? 0.f : 4.f, |
| 150 | (hb.s1 & m) ? 0.f : 4.f, |
| 151 | (hb.s2 & m) ? 0.f : 4.f, |
| 152 | (hb.s3 & m) ? 0.f : 4.f); |
| 153 | float4 v1 = (qv - hv) * dl; |
| 154 | |
| 155 | buf_a[(loadr_a * LOAD_VEC_A + 0) * BM + loadc_a + l] = v1.s0; |
| 156 | buf_a[(loadr_a * LOAD_VEC_A + 1) * BM + loadc_a + l] = v1.s1; |
| 157 | buf_a[(loadr_a * LOAD_VEC_A + 2) * BM + loadc_a + l] = v1.s2; |
| 158 | buf_a[(loadr_a * LOAD_VEC_A + 3) * BM + loadc_a + l] = v1.s3; |
| 159 | } else { |
| 160 | buf_a[(loadr_a * LOAD_VEC_A + 0) * BM + loadc_a + l] = 0.0f; |
| 161 | buf_a[(loadr_a * LOAD_VEC_A + 1) * BM + loadc_a + l] = 0.0f; |
| 162 | buf_a[(loadr_a * LOAD_VEC_A + 2) * BM + loadc_a + l] = 0.0f; |
| 163 | buf_a[(loadr_a * LOAD_VEC_A + 3) * BM + loadc_a + l] = 0.0f; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | for (int l = 0; l < BN; l += loadstride_b) { |
| 168 | if (ic*BN + loadc_b + l < ne11) { |
| 169 | int idx = pos_b + (loadc_b + l) * stride_b / LOAD_VEC_B + loadr_b; |
| 170 | buf_b[(loadr_b * LOAD_VEC_B + 0) * BN + loadc_b + l] = src1[idx].s0; |
| 171 | buf_b[(loadr_b * LOAD_VEC_B + 1) * BN + loadc_b + l] = src1[idx].s1; |
| 172 | buf_b[(loadr_b * LOAD_VEC_B + 2) * BN + loadc_b + l] = src1[idx].s2; |
| 173 | buf_b[(loadr_b * LOAD_VEC_B + 3) * BN + loadc_b + l] = src1[idx].s3; |
| 174 | } else { |
| 175 | buf_b[(loadr_b * LOAD_VEC_B + 0) * BN + loadc_b + l] = 0.0f; |
| 176 | buf_b[(loadr_b * LOAD_VEC_B + 1) * BN + loadc_b + l] = 0.0f; |
| 177 | buf_b[(loadr_b * LOAD_VEC_B + 2) * BN + loadc_b + l] = 0.0f; |
| 178 | buf_b[(loadr_b * LOAD_VEC_B + 3) * BN + loadc_b + l] = 0.0f; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | barrier(CLK_LOCAL_MEM_FENCE); |
| 183 | |
| 184 | pos_a += BK; |
| 185 | pos_b += BK / LOAD_VEC_B; |
| 186 | |
| 187 | for (int i = 0; i < BK; i++) { |
| 188 | for (int a = 0; a < TM/4; a++) { |
| 189 | cache_a4[a] = LM_LD4(buf_a + (i) * BM + th_r * TM, a); |
| 190 | } |
| 191 | |
| 192 | for (int cc = 0; cc < TN; cc++) { |
| 193 | const float cache_b = buf_b[(i) * BN + th_c * TN + cc]; |
| 194 | for (int a = 0; a < TM/4; a++) { |
| 195 | const int sums_idx = cc*(TM/4) + a; |
| 196 | sums4[sums_idx] = mad(cache_a4[a], (float4)cache_b, sums4[sums_idx]); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | barrier(CLK_LOCAL_MEM_FENCE); |
| 201 | } |
| 202 | |
| 203 | const int dr = ir * BM + th_r * TM; |
| 204 | const int dc = ic * BN + th_c * TN; |
| 205 | |
| 206 | const ulong offsets = batch_idx * batch_stride_d; |
| 207 | |
| 208 | for (int cc = 0; cc < TN; cc++) { |
| 209 | for (int a = 0; a < TM/4; a++) { |
| 210 | const float4 v = sums4[cc * (TM/4) + a]; |
| 211 | const float vs[4] = { v.s0, v.s1, v.s2, v.s3 }; |
| 212 | for (int k = 0; k < 4; k++) { |
| 213 | if (dr + 4*a + k < ne01 && dc + cc < ne11) { |
| 214 | dst[offsets + (dc + cc) * stride_d + dr + 4*a + k] = vs[k]; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |