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_q5_1_f32_l4_lm.cl

175 lines · 6121 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2
3#define LOAD_VEC_A 8
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_q5_1_f32_l4_lm(
13 global uchar4 * src0_qs,
14 global uint * src0_qh,
15 global half * src0_d,
16 global half * src0_m,
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 int ib = idx / 4;
86 int iqs = idx % 4;
87
88 float d = (float)src0_d[ib];
89 float m = (float)src0_m[ib];
90 uint qh_val = src0_qh[ib];
91
92 global uchar4 * qs = src0_qs + ib*4 + iqs;
93 uchar4 q = *qs;
94
95 uint qh_lo = qh_val >> (iqs * 4);
96 uint qh_hi = qh_val >> (iqs * 4 + 16);
97
98 uchar4 b_lo = (uchar4)((uchar)qh_lo, (uchar)(qh_lo >> 1), (uchar)(qh_lo >> 2), (uchar)(qh_lo >> 3)) & (uchar)1;
99 uchar4 b_hi = (uchar4)((uchar)qh_hi, (uchar)(qh_hi >> 1), (uchar)(qh_hi >> 2), (uchar)(qh_hi >> 3)) & (uchar)1;
100
101 float4 v1 = convert_float4((q & (uchar)0x0F) | (b_lo << (uchar)4)) * d + m;
102 float4 v2 = convert_float4((q >> (uchar)4) | (b_hi << (uchar)4)) * d + m;
103
104 buf_a[(loadr_a * 4 + 0) * BM + loadc_a + l] = v1.s0;
105 buf_a[(loadr_a * 4 + 1) * BM + loadc_a + l] = v1.s1;
106 buf_a[(loadr_a * 4 + 2) * BM + loadc_a + l] = v1.s2;
107 buf_a[(loadr_a * 4 + 3) * BM + loadc_a + l] = v1.s3;
108 buf_a[(loadr_a * 4 + 16) * BM + loadc_a + l] = v2.s0;
109 buf_a[(loadr_a * 4 + 17) * BM + loadc_a + l] = v2.s1;
110 buf_a[(loadr_a * 4 + 18) * BM + loadc_a + l] = v2.s2;
111 buf_a[(loadr_a * 4 + 19) * BM + loadc_a + l] = v2.s3;
112 } else {
113 buf_a[(loadr_a * 4 + 0) * BM + loadc_a + l] = 0.0f;
114 buf_a[(loadr_a * 4 + 1) * BM + loadc_a + l] = 0.0f;
115 buf_a[(loadr_a * 4 + 2) * BM + loadc_a + l] = 0.0f;
116 buf_a[(loadr_a * 4 + 3) * BM + loadc_a + l] = 0.0f;
117 buf_a[(loadr_a * 4 + 16) * BM + loadc_a + l] = 0.0f;
118 buf_a[(loadr_a * 4 + 17) * BM + loadc_a + l] = 0.0f;
119 buf_a[(loadr_a * 4 + 18) * BM + loadc_a + l] = 0.0f;
120 buf_a[(loadr_a * 4 + 19) * BM + loadc_a + l] = 0.0f;
121 }
122 }
123
124 for (int l = 0; l < BN; l += loadstride_b) {
125 if (ic*BN + loadc_b + l < ne11) {
126 int idx = pos_b + (loadc_b + l) * stride_b / LOAD_VEC_B + loadr_b;
127 buf_b[(loadr_b * LOAD_VEC_B + 0) * BN + loadc_b + l] = src1[idx].s0;
128 buf_b[(loadr_b * LOAD_VEC_B + 1) * BN + loadc_b + l] = src1[idx].s1;
129 buf_b[(loadr_b * LOAD_VEC_B + 2) * BN + loadc_b + l] = src1[idx].s2;
130 buf_b[(loadr_b * LOAD_VEC_B + 3) * BN + loadc_b + l] = src1[idx].s3;
131 } else {
132 buf_b[(loadr_b * LOAD_VEC_B + 0) * BN + loadc_b + l] = 0.0f;
133 buf_b[(loadr_b * LOAD_VEC_B + 1) * BN + loadc_b + l] = 0.0f;
134 buf_b[(loadr_b * LOAD_VEC_B + 2) * BN + loadc_b + l] = 0.0f;
135 buf_b[(loadr_b * LOAD_VEC_B + 3) * BN + loadc_b + l] = 0.0f;
136 }
137 }
138
139 barrier(CLK_LOCAL_MEM_FENCE);
140
141 pos_a += BK / LOAD_VEC_A;
142 pos_b += BK / LOAD_VEC_B;
143
144 for (int i = 0; i < BK; i++) {
145 for (int j = 0; j < TM; j++) {
146 cache_a[j] = buf_a[(i) * BM + th_r * TM + j];
147 }
148
149 for (int j = 0; j < TN; j++) {
150 cache_b[j] = buf_b[(i) * BN + th_c * TN + j];
151 }
152
153 for (int cc = 0; cc < TN; cc++) {
154 for (int cr = 0; cr < TM; cr++) {
155 const int sums_idx = cc*TM + cr;
156 sums[sums_idx] = mad(cache_a[cr], cache_b[cc], sums[sums_idx]);
157 }
158 }
159 }
160 barrier(CLK_LOCAL_MEM_FENCE);
161 }
162
163 const int dr = ir * BM + th_r * TM;
164 const int dc = ic * BN + th_c * TN;
165
166 const int offsets = batch_idx * batch_stride_d;
167
168 for (int cc = 0; cc < TN; cc++) {
169 for (int cr = 0; cr < TM; cr++) {
170 if (dr + cr < ne01 && dc + cc < ne11) {
171 dst[offsets + (dc + cc) * stride_d + dr + cr] = sums[cc * TM + cr];
172 }
173 }
174 }
175}