interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-opencl / kernels / mul_mm_q6_k_f32_l4_lm.cl

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