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