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

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