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

135 lines · 4825 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 iq4_xs_pack(ushort q) {
14 return iq4_xs_code(q & 15)
15 | (iq4_xs_code((q >> 4) & 15) << 8)
16 | (iq4_xs_code((q >> 8) & 15) << 16)
17 | (iq4_xs_code(q >> 12) << 24);
18}
19
20// Pass staged operands by value; local dp4a operands miscompile on X2.
21inline int dot8_q8a_v(uint8 qw, uint4 a0, uint4 a1) {
22 int r = 0;
23 r = dot_acc_sat_4x8packed_ss_int(qw.s0, a0.x, r);
24 r = dot_acc_sat_4x8packed_ss_int(qw.s1, a0.y, r);
25 r = dot_acc_sat_4x8packed_ss_int(qw.s2, a0.z, r);
26 r = dot_acc_sat_4x8packed_ss_int(qw.s3, a0.w, r);
27 r = dot_acc_sat_4x8packed_ss_int(qw.s4, a1.x, r);
28 r = dot_acc_sat_4x8packed_ss_int(qw.s5, a1.y, r);
29 r = dot_acc_sat_4x8packed_ss_int(qw.s6, a1.z, r);
30 r = dot_acc_sat_4x8packed_ss_int(qw.s7, a1.w, r);
31 return r;
32}
33
34__attribute__((qcom_wave_pair_mode(1)))
35kernel void kernel_gemm_noshuffle_iq4_xs_q8_1_dp4a(
36 __global const ushort * src0_qs,
37 __global const uint * src0_sl,
38 __global const ushort * src0_sh,
39 __global const half * src0_d,
40 __global const uint * src1_qa,
41 __global const half * src1_da,
42 __global float * dst,
43 ulong offsetd,
44 int m,
45 int n_no_padding,
46 int k
47) {
48 dst = (global float *)((global char *)dst + offsetd);
49
50 const uint lid = get_local_id(0);
51 const uint block_id_m = get_global_id(1);
52 const uint block_id_n = get_global_id(2);
53
54 const uint row = block_id_m * 64 + lid;
55 const uint col_base = block_id_n * TILESIZE_N;
56 const bool row_valid = row < (uint)m;
57 const uint rrow = row_valid ? row : 0;
58
59 const uint k_u = (uint)k >> 2;
60 const uint k_b = (uint)k >> 5;
61
62 __local uint4 sh_qa4[TILESIZE_N][2];
63 __local half sh_d[TILESIZE_N];
64
65#define NGROUPS (TILESIZE_N / 4)
66 float4 acc[NGROUPS];
67 #pragma unroll
68 for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f);
69
70 for (uint step = 0; step < (uint)k; step += 32) {
71 const uint sub = step >> 5;
72
73 const uint ib = sub >> 3;
74 const uint sb = sub & 7u;
75 const uint sl = src0_sl[rrow + ib*(uint)m];
76 const uint sh = src0_sh[rrow + ib*(uint)m];
77 const int ls = (int)(((sl >> (4*sb)) & 15u) | (((sh >> (2*sb)) & 3u) << 4)) - 32;
78 const float d_w = (float)src0_d[rrow + ib*(uint)m] * (float)ls;
79 const uint qsb = rrow + sub*8u*(uint)m;
80
81 uint8 qw;
82 qw.s0 = iq4_xs_pack(src0_qs[qsb + 0u*(uint)m]);
83 qw.s1 = iq4_xs_pack(src0_qs[qsb + 1u*(uint)m]);
84 qw.s2 = iq4_xs_pack(src0_qs[qsb + 2u*(uint)m]);
85 qw.s3 = iq4_xs_pack(src0_qs[qsb + 3u*(uint)m]);
86 qw.s4 = iq4_xs_pack(src0_qs[qsb + 4u*(uint)m]);
87 qw.s5 = iq4_xs_pack(src0_qs[qsb + 5u*(uint)m]);
88 qw.s6 = iq4_xs_pack(src0_qs[qsb + 6u*(uint)m]);
89 qw.s7 = iq4_xs_pack(src0_qs[qsb + 7u*(uint)m]);
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 sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0;
102 }
103 barrier(CLK_LOCAL_MEM_FENCE);
104
105#define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3]))
106 #pragma unroll
107 for (int g = 0; g < NGROUPS; ++g) {
108 const int b = g * 4;
109 float4 rf;
110#define TDOT4(T) dot8_q8a_v(qw, (uint4)(sh_qa4[T][0]), (uint4)(sh_qa4[T][1]))
111 rf.s0 = (float)TDOT4(b+0); rf.s1 = (float)TDOT4(b+1);
112 rf.s2 = (float)TDOT4(b+2); rf.s3 = (float)TDOT4(b+3);
113 acc[g] += d_w * LD4(sh_d, b) * rf;
114 }
115#undef TDOT4
116#undef LD4
117 barrier(CLK_LOCAL_MEM_FENCE);
118 }
119
120 if (!row_valid) {
121 return;
122 }
123
124 #pragma unroll
125 for (int g = 0; g < NGROUPS; ++g) {
126 const uint b = (uint)(g * 4);
127 const float4 a = acc[g];
128 const uint c0 = col_base + b;
129 if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0;
130 if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1;
131 if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2;
132 if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3;
133 }
134#undef NGROUPS
135}