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

137 lines · 5382 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_xxs_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 dot8_q8a_v(uint8 qw, uint4 a0, uint4 a1) {
24 int r = 0;
25 r = dot_acc_sat_4x8packed_ss_int(qw.s0, a0.x, r);
26 r = dot_acc_sat_4x8packed_ss_int(qw.s1, a0.y, r);
27 r = dot_acc_sat_4x8packed_ss_int(qw.s2, a0.z, r);
28 r = dot_acc_sat_4x8packed_ss_int(qw.s3, a0.w, r);
29 r = dot_acc_sat_4x8packed_ss_int(qw.s4, a1.x, r);
30 r = dot_acc_sat_4x8packed_ss_int(qw.s5, a1.y, r);
31 r = dot_acc_sat_4x8packed_ss_int(qw.s6, a1.z, r);
32 r = dot_acc_sat_4x8packed_ss_int(qw.s7, a1.w, r);
33 return r;
34}
35
36__attribute__((qcom_wave_pair_mode(1)))
37kernel void kernel_gemm_noshuffle_iq3_xxs_q8_1_dp4a(
38 __global const uchar * src0_qs,
39 __global const uint * src0_sas,
40 __global const half * src0_d,
41 __global const uint * src1_qa,
42 __global const half * src1_da,
43 __global float * dst,
44 ulong offsetd,
45 int m,
46 int n_no_padding,
47 int k
48) {
49 dst = (global float *)((global char *)dst + offsetd);
50
51 const uint lid = get_local_id(0);
52 const uint block_id_m = get_global_id(1);
53 const uint block_id_n = get_global_id(2);
54
55 const uint row = block_id_m * 64 + lid;
56 const uint col_base = block_id_n * TILESIZE_N;
57 const bool row_valid = row < (uint)m;
58 const uint rrow = row_valid ? row : 0;
59
60 const uint k_u = (uint)k >> 2;
61 const uint k_b = (uint)k >> 5;
62
63 __local uint4 sh_qa4[TILESIZE_N][2];
64 __local half sh_d[TILESIZE_N];
65
66#define NGROUPS (TILESIZE_N / 4)
67 float4 acc[NGROUPS];
68 #pragma unroll
69 for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f);
70
71 for (uint step = 0; step < (uint)k; step += 32) {
72 const uint sub = step >> 5;
73
74 const uint ib = sub >> 3;
75 const uint aux = src0_sas[rrow + sub*(uint)m];
76 const float d_w = (float)src0_d[rrow + ib*(uint)m] * (0.5f + (float)(aux >> 28)) * 0.5f;
77 const uint sg0 = iq3_xxs_signs( aux & 127u);
78 const uint sg1 = iq3_xxs_signs((aux >> 7) & 127u);
79 const uint sg2 = iq3_xxs_signs((aux >> 14) & 127u);
80 const uint sg3 = iq3_xxs_signs((aux >> 21) & 127u);
81 const uint qsb = rrow + sub*8u*(uint)m;
82
83 uint8 qw;
84 qw.s0 = iq3_xxs_pack(iq3_xxs_grid[src0_qs[qsb + 0u*(uint)m]], sg0, 0u);
85 qw.s1 = iq3_xxs_pack(iq3_xxs_grid[src0_qs[qsb + 1u*(uint)m]], sg0, 4u);
86 qw.s2 = iq3_xxs_pack(iq3_xxs_grid[src0_qs[qsb + 2u*(uint)m]], sg1, 0u);
87 qw.s3 = iq3_xxs_pack(iq3_xxs_grid[src0_qs[qsb + 3u*(uint)m]], sg1, 4u);
88 qw.s4 = iq3_xxs_pack(iq3_xxs_grid[src0_qs[qsb + 4u*(uint)m]], sg2, 0u);
89 qw.s5 = iq3_xxs_pack(iq3_xxs_grid[src0_qs[qsb + 5u*(uint)m]], sg2, 4u);
90 qw.s6 = iq3_xxs_pack(iq3_xxs_grid[src0_qs[qsb + 6u*(uint)m]], sg3, 0u);
91 qw.s7 = iq3_xxs_pack(iq3_xxs_grid[src0_qs[qsb + 7u*(uint)m]], sg3, 4u);
92
93 for (uint idx = lid; idx < TILESIZE_N * 2; idx += 64) {
94 const uint t = idx >> 1;
95 const uint v = idx & 1;
96 const uint c = col_base + t;
97 sh_qa4[t][v] = (c < (uint)n_no_padding)
98 ? vload4(0, src1_qa + c * k_u + (step >> 2) + (v << 2))
99 : (uint4)(0u);
100 }
101 if (lid < TILESIZE_N) {
102 const uint c = col_base + lid;
103 sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[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] += d_w * LD4(sh_d, b) * rf;
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}