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

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