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

154 lines · 5954 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 iq3_s_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#define IQ3_S_GEMM_GRID(i) iq3_s_grid[(i)]
23
24inline uint iq3_s_load(__global const uchar * qs, uint qsb, uint qhb, uint m,
25 uint u, uint sgv, uint base) {
26 const uint gidx = (uint)qs[qsb + u * m] | (((qhb >> u) & 1u) << 8);
27 return iq3_s_pack(IQ3_S_GEMM_GRID(gidx), sgv, base);
28}
29
30// Pass staged operands by value; local dp4a operands miscompile on X2.
31inline int dot8_q8a_v(uint8 qw, uint4 a0, uint4 a1) {
32 int r = 0;
33 r = dot_acc_sat_4x8packed_ss_int(qw.s0, a0.x, r);
34 r = dot_acc_sat_4x8packed_ss_int(qw.s1, a0.y, r);
35 r = dot_acc_sat_4x8packed_ss_int(qw.s2, a0.z, r);
36 r = dot_acc_sat_4x8packed_ss_int(qw.s3, a0.w, r);
37 r = dot_acc_sat_4x8packed_ss_int(qw.s4, a1.x, r);
38 r = dot_acc_sat_4x8packed_ss_int(qw.s5, a1.y, r);
39 r = dot_acc_sat_4x8packed_ss_int(qw.s6, a1.z, r);
40 r = dot_acc_sat_4x8packed_ss_int(qw.s7, a1.w, r);
41 return r;
42}
43
44__attribute__((qcom_wave_pair_mode(1)))
45kernel void kernel_gemm_noshuffle_iq3_s_q8_1_dp4a(
46 __global const uchar * src0_qs,
47 __global const uchar * src0_sg,
48 __global const uchar * src0_qh,
49 __global const uchar * src0_sc,
50 __global const half * src0_d,
51 __global const uint * src1_qa,
52 __global const half * src1_da,
53 __global float * dst,
54 ulong offsetd,
55 int m,
56 int n_no_padding,
57 int k
58) {
59 dst = (global float *)((global char *)dst + offsetd);
60
61 const uint lid = get_local_id(0);
62 const uint block_id_m = get_global_id(1);
63 const uint block_id_n = get_global_id(2);
64
65 const uint row = block_id_m * 64 + lid;
66 const uint col_base = block_id_n * TILESIZE_N;
67 const bool row_valid = row < (uint)m;
68 const uint rrow = row_valid ? row : 0;
69
70 const uint k_u = (uint)k >> 2;
71 const uint k_b = (uint)k >> 5;
72
73 __local uint4 sh_qa4[TILESIZE_N][2];
74 __local half sh_d[TILESIZE_N];
75
76#define NGROUPS (TILESIZE_N / 4)
77 float4 acc[NGROUPS];
78 #pragma unroll
79 for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f);
80
81 for (uint step = 0; step < (uint)k; step += 32) {
82 const uint sub = step >> 5;
83
84 const uint ib = sub >> 3;
85 const uint sb = sub & 7u;
86
87 const uint scb = (uint)src0_sc[rrow + (ib*(QK_K/64) + (sb >> 1)) * (uint)m];
88 const uint nib = (sb & 1u) ? (scb >> 4) : (scb & 0xFu);
89 const float d_w = (float)src0_d[rrow + ib * (uint)m] * (float)(1 + 2*nib);
90
91 const uint qhb = (uint)src0_qh[rrow + (ib*(QK_K/32) + sb) * (uint)m];
92 const uint qsb = rrow + (ib*(QK_K/4) + 8*sb) * (uint)m;
93 const uint sgb = rrow + (ib*(QK_K/8) + 4*sb) * (uint)m;
94
95 const uint sg0 = (uint)src0_sg[sgb + 0 * (uint)m];
96 const uint sg1 = (uint)src0_sg[sgb + 1 * (uint)m];
97 const uint sg2 = (uint)src0_sg[sgb + 2 * (uint)m];
98 const uint sg3 = (uint)src0_sg[sgb + 3 * (uint)m];
99
100 uint8 qw;
101 qw.s0 = iq3_s_load(src0_qs, qsb, qhb, (uint)m, 0u, sg0, 0u);
102 qw.s1 = iq3_s_load(src0_qs, qsb, qhb, (uint)m, 1u, sg0, 4u);
103 qw.s2 = iq3_s_load(src0_qs, qsb, qhb, (uint)m, 2u, sg1, 0u);
104 qw.s3 = iq3_s_load(src0_qs, qsb, qhb, (uint)m, 3u, sg1, 4u);
105 qw.s4 = iq3_s_load(src0_qs, qsb, qhb, (uint)m, 4u, sg2, 0u);
106 qw.s5 = iq3_s_load(src0_qs, qsb, qhb, (uint)m, 5u, sg2, 4u);
107 qw.s6 = iq3_s_load(src0_qs, qsb, qhb, (uint)m, 6u, sg3, 0u);
108 qw.s7 = iq3_s_load(src0_qs, qsb, qhb, (uint)m, 7u, sg3, 4u);
109
110 for (uint idx = lid; idx < TILESIZE_N * 2; idx += 64) {
111 const uint t = idx >> 1;
112 const uint v = idx & 1;
113 const uint c = col_base + t;
114 sh_qa4[t][v] = (c < (uint)n_no_padding)
115 ? vload4(0, src1_qa + c * k_u + (step >> 2) + (v << 2))
116 : (uint4)(0u);
117 }
118 if (lid < TILESIZE_N) {
119 const uint c = col_base + lid;
120 sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0;
121 }
122 barrier(CLK_LOCAL_MEM_FENCE);
123
124#define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3]))
125 #pragma unroll
126 for (int g = 0; g < NGROUPS; ++g) {
127 const int b = g * 4;
128 float4 rf;
129#define TDOT4(T) dot8_q8a_v(qw, (uint4)(sh_qa4[T][0]), (uint4)(sh_qa4[T][1]))
130 rf.s0 = (float)TDOT4(b+0); rf.s1 = (float)TDOT4(b+1);
131 rf.s2 = (float)TDOT4(b+2); rf.s3 = (float)TDOT4(b+3);
132 acc[g] += d_w * LD4(sh_d, b) * rf;
133 }
134#undef TDOT4
135#undef LD4
136 barrier(CLK_LOCAL_MEM_FENCE);
137 }
138
139 if (!row_valid) {
140 return;
141 }
142
143 #pragma unroll
144 for (int g = 0; g < NGROUPS; ++g) {
145 const uint b = (uint)(g * 4);
146 const float4 a = acc[g];
147 const uint c0 = col_base + b;
148 if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0;
149 if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1;
150 if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2;
151 if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3;
152 }
153#undef NGROUPS
154}