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 / gemm_noshuffle_iq1_s_q8_1_dp4a.cl

138 lines · 5199 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2#pragma OPENCL EXTENSION cl_khr_subgroups : enable
3#ifdef cl_khr_integer_dot_product
4#pragma OPENCL EXTENSION cl_khr_integer_dot_product : enable
5#endif
6
7#define QK_K 256
8
9#ifndef TILESIZE_N
10#define TILESIZE_N 32
11#endif
12
13// Pass staged operands by value; local dp4a operands miscompile on X2.
14inline int dot8_q8a_v(uint8 qw, uint4 a0, uint4 a1) {
15 int r = 0;
16 r = dot_acc_sat_4x8packed_ss_int(qw.s0, a0.x, r);
17 r = dot_acc_sat_4x8packed_ss_int(qw.s1, a0.y, r);
18 r = dot_acc_sat_4x8packed_ss_int(qw.s2, a0.z, r);
19 r = dot_acc_sat_4x8packed_ss_int(qw.s3, a0.w, r);
20 r = dot_acc_sat_4x8packed_ss_int(qw.s4, a1.x, r);
21 r = dot_acc_sat_4x8packed_ss_int(qw.s5, a1.y, r);
22 r = dot_acc_sat_4x8packed_ss_int(qw.s6, a1.z, r);
23 r = dot_acc_sat_4x8packed_ss_int(qw.s7, a1.w, r);
24 return r;
25}
26
27__attribute__((qcom_wave_pair_mode(1)))
28kernel void kernel_gemm_noshuffle_iq1_s_q8_1_dp4a(
29 __global const uchar * src0_qs,
30 __global const ushort * src0_qh,
31 __global const half * src0_d,
32 __global const uint * src1_qa,
33 __global const half * src1_da,
34 __global const half * src1_sa,
35 __global float * dst,
36 ulong offsetd,
37 int m,
38 int n_no_padding,
39 int k
40) {
41 dst = (global float *)((global char *)dst + offsetd);
42
43 const uint lid = get_local_id(0);
44 const uint block_id_m = get_global_id(1);
45 const uint block_id_n = get_global_id(2);
46
47 const uint row = block_id_m * 64 + lid;
48 const uint col_base = block_id_n * TILESIZE_N;
49 const bool row_valid = row < (uint)m;
50 const uint rrow = row_valid ? row : 0;
51
52 const uint k_u = (uint)k >> 2;
53 const uint k_b = (uint)k >> 5;
54
55 __local uint4 sh_qa4[TILESIZE_N][2];
56 __local half sh_da[TILESIZE_N];
57 __local half sh_sa[TILESIZE_N];
58
59#define IQ1_S_GRID(i) iq1_s_grid[(i)]
60
61#define NGROUPS (TILESIZE_N / 4)
62 float4 acc[NGROUPS];
63 #pragma unroll
64 for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f);
65
66 for (uint step = 0; step < (uint)k; step += 32) {
67 const uint sub = step >> 5;
68 const uint ib = sub >> 3;
69
70 const uint qhv = (uint)src0_qh[rrow + sub * (uint)m];
71 const float dl = (float)src0_d[rrow + ib * (uint)m]
72 * (float)(2u * ((qhv >> 12) & 7u) + 1u);
73
74 // The codebook stores grid + 1; sa includes the Q8_1 activation scale.
75 const float dlt = ((qhv & 0x8000u) ? -0.125f : 0.125f) - 1.0f;
76
77 const uint gb = rrow + (step >> 3) * (uint)m;
78
79 uint8 qw;
80 {
81 const uint g0 = IQ1_S_GRID((uint)src0_qs[gb + 0u * (uint)m] | ((( qhv & 7u) << 8)));
82 const uint g1 = IQ1_S_GRID((uint)src0_qs[gb + 1u * (uint)m] | ((((qhv >> 3) & 7u) << 8)));
83 const uint g2 = IQ1_S_GRID((uint)src0_qs[gb + 2u * (uint)m] | ((((qhv >> 6) & 7u) << 8)));
84 const uint g3 = IQ1_S_GRID((uint)src0_qs[gb + 3u * (uint)m] | ((((qhv >> 9) & 7u) << 8)));
85 qw.s0 = g0 & 0x0F0F0F0Fu; qw.s1 = (g0 >> 4) & 0x0F0F0F0Fu;
86 qw.s2 = g1 & 0x0F0F0F0Fu; qw.s3 = (g1 >> 4) & 0x0F0F0F0Fu;
87 qw.s4 = g2 & 0x0F0F0F0Fu; qw.s5 = (g2 >> 4) & 0x0F0F0F0Fu;
88 qw.s6 = g3 & 0x0F0F0F0Fu; qw.s7 = (g3 >> 4) & 0x0F0F0F0Fu;
89 }
90
91 for (uint idx = lid; idx < TILESIZE_N * 2; idx += 64) {
92 const uint t = idx >> 1;
93 const uint v = idx & 1;
94 const uint c = col_base + t;
95 sh_qa4[t][v] = (c < (uint)n_no_padding)
96 ? vload4(0, src1_qa + c * k_u + (step >> 2) + (v << 2))
97 : (uint4)(0u);
98 }
99 if (lid < TILESIZE_N) {
100 const uint c = col_base + lid;
101 const bool ok = c < (uint)n_no_padding;
102 sh_da[lid] = ok ? src1_da[c * k_b + sub] : (half)0;
103 sh_sa[lid] = ok ? src1_sa[c * k_b + sub] : (half)0;
104 }
105 barrier(CLK_LOCAL_MEM_FENCE);
106
107#define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3]))
108 #pragma unroll
109 for (int g = 0; g < NGROUPS; ++g) {
110 const int b = g * 4;
111 float4 rf;
112#define TDOT4(T) dot8_q8a_v(qw, (uint4)(sh_qa4[T][0]), (uint4)(sh_qa4[T][1]))
113 rf.s0 = (float)TDOT4(b+0); rf.s1 = (float)TDOT4(b+1);
114 rf.s2 = (float)TDOT4(b+2); rf.s3 = (float)TDOT4(b+3);
115 acc[g] += dl * (LD4(sh_da, b) * rf) + (dl * dlt) * LD4(sh_sa, b);
116 }
117#undef TDOT4
118#undef LD4
119 barrier(CLK_LOCAL_MEM_FENCE);
120 }
121
122 if (!row_valid) {
123 return;
124 }
125
126 #pragma unroll
127 for (int g = 0; g < NGROUPS; ++g) {
128 const uint b = (uint)(g * 4);
129 const float4 a = acc[g];
130 const uint c0 = col_base + b;
131 if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0;
132 if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1;
133 if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2;
134 if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3;
135 }
136#undef NGROUPS
137#undef IQ1_S_GRID
138}