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_q1_0_f32_l4_lm.cl
| 1 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 2 | |
| 3 | // LOAD_VEC_A is 8 because one q1_0 quant byte expands to 8 weights along K. |
| 4 | #define LOAD_VEC_A 8 |
| 5 | #define LOAD_VEC_B 4 |
| 6 | |
| 7 | #define BM 64 |
| 8 | #define BN 64 |
| 9 | #define BK 32 |
| 10 | #define TM 4 |
| 11 | #define TN 8 |
| 12 | |
| 13 | kernel void kernel_mul_mm_q1_0_f32_l4_lm( |
| 14 | global uchar * src0_q, |
| 15 | global half * src0_d, |
| 16 | global float4 * src1, |
| 17 | ulong offset1, |
| 18 | global float * dst, |
| 19 | ulong offsetd, |
| 20 | |
| 21 | int ne00, |
| 22 | int ne01, |
| 23 | int ne02, |
| 24 | int ne11, |
| 25 | int ne12, |
| 26 | |
| 27 | int stride_a, |
| 28 | int stride_b, |
| 29 | int stride_d, |
| 30 | |
| 31 | int batch_stride_a, |
| 32 | int batch_stride_b, |
| 33 | int batch_stride_d, |
| 34 | |
| 35 | int r2, |
| 36 | int r3 |
| 37 | ) { |
| 38 | src1 = (global float4*)((global char*)src1 + offset1); |
| 39 | dst = (global float *)((global char*)dst + offsetd); |
| 40 | |
| 41 | local float buf_a[BM * BK]; |
| 42 | local float buf_b[BN * BK]; |
| 43 | |
| 44 | const int batch_idx = get_global_id(2); |
| 45 | |
| 46 | const int i13 = batch_idx / ne12; |
| 47 | const int i12 = batch_idx % ne12; |
| 48 | |
| 49 | const int i03 = i13 / r3; |
| 50 | const int i02 = i12 / r2; |
| 51 | |
| 52 | const int batch_idx_a = i03 * ne02 + i02; |
| 53 | |
| 54 | const int ir = get_group_id(0); |
| 55 | const int ic = get_group_id(1); |
| 56 | |
| 57 | const int tid = get_local_id(0); |
| 58 | const int th_r = tid % (BM / TM); |
| 59 | const int th_c = tid / (BM / TM); |
| 60 | |
| 61 | const int loadr_a = get_local_id(0) % (BK / LOAD_VEC_A); |
| 62 | const int loadc_a = get_local_id(0) / (BK / LOAD_VEC_A); |
| 63 | const int loadr_b = get_local_id(0) % (BK / LOAD_VEC_B); |
| 64 | const int loadc_b = get_local_id(0) / (BK / LOAD_VEC_B); |
| 65 | |
| 66 | const int loadstride_a = get_local_size(0) * LOAD_VEC_A / BK; |
| 67 | const int loadstride_b = get_local_size(0) * LOAD_VEC_B / BK; |
| 68 | |
| 69 | int pos_a = (batch_idx_a * batch_stride_a + ir * BM * stride_a) / LOAD_VEC_A; |
| 70 | int pos_b = (batch_idx * batch_stride_b + ic * BN * stride_b) / LOAD_VEC_B; |
| 71 | |
| 72 | float sums[TM * TN]; |
| 73 | float cache_a[TM]; |
| 74 | float cache_b[TN]; |
| 75 | |
| 76 | for (int i = 0; i < TM * TN; i++) { |
| 77 | sums[i] = 0.0f; |
| 78 | } |
| 79 | |
| 80 | for (int block = 0; block < ne00; block += BK) { |
| 81 | for (int l = 0; l < BM; l += loadstride_a) { |
| 82 | if (ir*BM + loadc_a + l < ne01) { |
| 83 | int idx = pos_a + (loadc_a + l) * stride_a / LOAD_VEC_A + loadr_a; |
| 84 | int ib = idx / 16; // 16 quant bytes per q1_0 block |
| 85 | |
| 86 | float d = (float)src0_d[ib]; |
| 87 | uint bits = src0_q[idx]; |
| 88 | |
| 89 | // use float to avoid unsigned underflow of (2*0 - 1). |
| 90 | buf_a[(loadr_a * LOAD_VEC_A + 0) * BM + loadc_a + l] = d * (2.0f*(float)((bits >> 0) & 1) - 1.0f); |
| 91 | buf_a[(loadr_a * LOAD_VEC_A + 1) * BM + loadc_a + l] = d * (2.0f*(float)((bits >> 1) & 1) - 1.0f); |
| 92 | buf_a[(loadr_a * LOAD_VEC_A + 2) * BM + loadc_a + l] = d * (2.0f*(float)((bits >> 2) & 1) - 1.0f); |
| 93 | buf_a[(loadr_a * LOAD_VEC_A + 3) * BM + loadc_a + l] = d * (2.0f*(float)((bits >> 3) & 1) - 1.0f); |
| 94 | buf_a[(loadr_a * LOAD_VEC_A + 4) * BM + loadc_a + l] = d * (2.0f*(float)((bits >> 4) & 1) - 1.0f); |
| 95 | buf_a[(loadr_a * LOAD_VEC_A + 5) * BM + loadc_a + l] = d * (2.0f*(float)((bits >> 5) & 1) - 1.0f); |
| 96 | buf_a[(loadr_a * LOAD_VEC_A + 6) * BM + loadc_a + l] = d * (2.0f*(float)((bits >> 6) & 1) - 1.0f); |
| 97 | buf_a[(loadr_a * LOAD_VEC_A + 7) * BM + loadc_a + l] = d * (2.0f*(float)((bits >> 7) & 1) - 1.0f); |
| 98 | } else { |
| 99 | for (int b = 0; b < LOAD_VEC_A; ++b) { |
| 100 | buf_a[(loadr_a * LOAD_VEC_A + b) * BM + loadc_a + l] = 0.0f; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | for (int l = 0; l < BN; l += loadstride_b) { |
| 106 | if (ic*BN + loadc_b + l < ne11) { |
| 107 | int idx = pos_b + (loadc_b + l) * stride_b / LOAD_VEC_B + loadr_b; |
| 108 | buf_b[(loadr_b * LOAD_VEC_B + 0) * BN + loadc_b + l] = src1[idx].s0; |
| 109 | buf_b[(loadr_b * LOAD_VEC_B + 1) * BN + loadc_b + l] = src1[idx].s1; |
| 110 | buf_b[(loadr_b * LOAD_VEC_B + 2) * BN + loadc_b + l] = src1[idx].s2; |
| 111 | buf_b[(loadr_b * LOAD_VEC_B + 3) * BN + loadc_b + l] = src1[idx].s3; |
| 112 | } else { |
| 113 | buf_b[(loadr_b * LOAD_VEC_B + 0) * BN + loadc_b + l] = 0.0f; |
| 114 | buf_b[(loadr_b * LOAD_VEC_B + 1) * BN + loadc_b + l] = 0.0f; |
| 115 | buf_b[(loadr_b * LOAD_VEC_B + 2) * BN + loadc_b + l] = 0.0f; |
| 116 | buf_b[(loadr_b * LOAD_VEC_B + 3) * BN + loadc_b + l] = 0.0f; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | barrier(CLK_LOCAL_MEM_FENCE); |
| 121 | |
| 122 | pos_a += BK / LOAD_VEC_A; |
| 123 | pos_b += BK / LOAD_VEC_B; |
| 124 | |
| 125 | for (int i = 0; i < BK; i++) { |
| 126 | for (int j = 0; j < TM; j++) { |
| 127 | cache_a[j] = buf_a[(i) * BM + th_r * TM + j]; |
| 128 | } |
| 129 | |
| 130 | for (int j = 0; j < TN; j++) { |
| 131 | cache_b[j] = buf_b[(i) * BN + th_c * TN + j]; |
| 132 | } |
| 133 | |
| 134 | for (int cc = 0; cc < TN; cc++) { |
| 135 | for (int cr = 0; cr < TM; cr++) { |
| 136 | const int sums_idx = cc*TM + cr; |
| 137 | sums[sums_idx] = mad(cache_a[cr], cache_b[cc], sums[sums_idx]); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | barrier(CLK_LOCAL_MEM_FENCE); |
| 142 | } |
| 143 | |
| 144 | const int dr = ir * BM + th_r * TM; |
| 145 | const int dc = ic * BN + th_c * TN; |
| 146 | |
| 147 | const int offsets = batch_idx * batch_stride_d; |
| 148 | |
| 149 | for (int cc = 0; cc < TN; cc++) { |
| 150 | for (int cr = 0; cr < TM; cr++) { |
| 151 | if (dr + cr < ne01 && dc + cc < ne11) { |
| 152 | dst[offsets + (dc + cc) * stride_d + dr + cr] = sums[cc * TM + cr]; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |