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