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

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