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

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