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

150 lines · 5717 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
13inline uint iq2_xs_pack(uint gv, uint sg, uint base) {
14 int v0 = (int)((gv >> 0) & 0xFF); if (sg & (1u << (base + 0))) { v0 = -v0; }
15 int v1 = (int)((gv >> 8) & 0xFF); if (sg & (1u << (base + 1))) { v1 = -v1; }
16 int v2 = (int)((gv >> 16) & 0xFF); if (sg & (1u << (base + 2))) { v2 = -v2; }
17 int v3 = (int)((gv >> 24) & 0xFF); if (sg & (1u << (base + 3))) { v3 = -v3; }
18 return ((uint)v0 & 0xFFu) | (((uint)v1 & 0xFFu) << 8)
19 | (((uint)v2 & 0xFFu) << 16) | (((uint)v3 & 0xFFu) << 24);
20}
21
22// Pass staged operands by value; local dp4a operands miscompile on X2.
23inline int dot4_q8a_v(uint4 qw, uint4 a) {
24 int r = 0;
25 r = dot_acc_sat_4x8packed_ss_int(qw.s0, a.x, r);
26 r = dot_acc_sat_4x8packed_ss_int(qw.s1, a.y, r);
27 r = dot_acc_sat_4x8packed_ss_int(qw.s2, a.z, r);
28 r = dot_acc_sat_4x8packed_ss_int(qw.s3, a.w, r);
29 return r;
30}
31
32__attribute__((qcom_wave_pair_mode(1)))
33kernel void kernel_gemm_noshuffle_iq2_xs_q8_1_dp4a(
34 __global const ushort * src0_qs,
35 __global const uchar * src0_sc,
36 __global const half * src0_d,
37 __global const uint * src1_qa,
38 __global const half * src1_da,
39 __global float * dst,
40 ulong offsetd,
41 int m,
42 int n_no_padding,
43 int k
44) {
45 dst = (global float *)((global char *)dst + offsetd);
46
47 const uint lid = get_local_id(0);
48 const uint block_id_m = get_global_id(1);
49 const uint block_id_n = get_global_id(2);
50
51 const uint row = block_id_m * 64 + lid;
52 const uint col_base = block_id_n * TILESIZE_N;
53 const bool row_valid = row < (uint)m;
54 const uint rrow = row_valid ? row : 0;
55
56 const uint k_u = (uint)k >> 2;
57 const uint k_b = (uint)k >> 5;
58
59 __local uint4 sh_qa4[TILESIZE_N][2];
60 __local half sh_d[TILESIZE_N];
61
62#define IQ2_XS_GRID(i) iq2_xs_grid[(i)]
63
64#define NGROUPS (TILESIZE_N / 4)
65 float4 acc[NGROUPS];
66 #pragma unroll
67 for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f);
68
69 for (uint step = 0; step < (uint)k; step += 32) {
70 const uint sub = step >> 5;
71 const uint ib = sub >> 3;
72
73 const uint scv = (uint)src0_sc[rrow + sub * (uint)m];
74 const float dv = (float)src0_d[rrow + ib * (uint)m];
75 const float dl0 = dv * (0.5f + (float)( scv & 0xFu)) * 0.25f;
76 const float dl1 = dv * (0.5f + (float)( scv >> 4) ) * 0.25f;
77
78 const uint gb = rrow + (step >> 3) * (uint)m;
79
80 uint4 qlo, qhi;
81 {
82 const uint q0 = (uint)src0_qs[gb + 0u * (uint)m];
83 const uint q1 = (uint)src0_qs[gb + 1u * (uint)m];
84 const uint q2 = (uint)src0_qs[gb + 2u * (uint)m];
85 const uint q3 = (uint)src0_qs[gb + 3u * (uint)m];
86 const uint gi0 = (q0 & 511u) << 1;
87 const uint gi1 = (q1 & 511u) << 1;
88 const uint gi2 = (q2 & 511u) << 1;
89 const uint gi3 = (q3 & 511u) << 1;
90 const uint s0 = iq2_xs_signs(q0 >> 9);
91 const uint s1 = iq2_xs_signs(q1 >> 9);
92 const uint s2 = iq2_xs_signs(q2 >> 9);
93 const uint s3 = iq2_xs_signs(q3 >> 9);
94 qlo.s0 = iq2_xs_pack(IQ2_XS_GRID(gi0 + 0u), s0, 0u);
95 qlo.s1 = iq2_xs_pack(IQ2_XS_GRID(gi0 + 1u), s0, 4u);
96 qlo.s2 = iq2_xs_pack(IQ2_XS_GRID(gi1 + 0u), s1, 0u);
97 qlo.s3 = iq2_xs_pack(IQ2_XS_GRID(gi1 + 1u), s1, 4u);
98 qhi.s0 = iq2_xs_pack(IQ2_XS_GRID(gi2 + 0u), s2, 0u);
99 qhi.s1 = iq2_xs_pack(IQ2_XS_GRID(gi2 + 1u), s2, 4u);
100 qhi.s2 = iq2_xs_pack(IQ2_XS_GRID(gi3 + 0u), s3, 0u);
101 qhi.s3 = iq2_xs_pack(IQ2_XS_GRID(gi3 + 1u), s3, 4u);
102 }
103
104 for (uint idx = lid; idx < TILESIZE_N * 2; idx += 64) {
105 const uint t = idx >> 1;
106 const uint v = idx & 1;
107 const uint c = col_base + t;
108 sh_qa4[t][v] = (c < (uint)n_no_padding)
109 ? vload4(0, src1_qa + c * k_u + (step >> 2) + (v << 2))
110 : (uint4)(0u);
111 }
112 if (lid < TILESIZE_N) {
113 const uint c = col_base + lid;
114 sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0;
115 }
116 barrier(CLK_LOCAL_MEM_FENCE);
117
118#define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3]))
119#define IQ2_XS_COL(b) (dl0 * (float)dot4_q8a_v(qlo, (uint4)(sh_qa4[b][0])) \
120 + dl1 * (float)dot4_q8a_v(qhi, (uint4)(sh_qa4[b][1])))
121 #pragma unroll
122 for (int g = 0; g < NGROUPS; ++g) {
123 const int b = g * 4;
124 float4 rf;
125 rf.s0 = IQ2_XS_COL(b+0); rf.s1 = IQ2_XS_COL(b+1);
126 rf.s2 = IQ2_XS_COL(b+2); rf.s3 = IQ2_XS_COL(b+3);
127 acc[g] += LD4(sh_d, b) * rf;
128 }
129#undef IQ2_XS_COL
130#undef LD4
131 barrier(CLK_LOCAL_MEM_FENCE);
132 }
133
134 if (!row_valid) {
135 return;
136 }
137
138 #pragma unroll
139 for (int g = 0; g < NGROUPS; ++g) {
140 const uint b = (uint)(g * 4);
141 const float4 a = acc[g];
142 const uint c0 = col_base + b;
143 if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0;
144 if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1;
145 if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2;
146 if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3;
147 }
148#undef NGROUPS
149#undef IQ2_XS_GRID
150}