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 / mul_mv_iq4_nl_f32.cl

164 lines · 4780 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2
3#ifdef cl_intel_subgroups
4#pragma OPENCL EXTENSION cl_intel_subgroups : enable
5#else
6#pragma OPENCL EXTENSION cl_khr_subgroups : enable
7#endif
8
9#ifdef cl_intel_required_subgroup_size
10#pragma OPENCL EXTENSION cl_intel_required_subgroup_size : enable
11#define INTEL_GPU 1
12#define REQD_SUBGROUP_SIZE_16 __attribute__((intel_reqd_sub_group_size(16)))
13#define REQD_SUBGROUP_SIZE_32 __attribute__((intel_reqd_sub_group_size(32)))
14#elif defined(cl_qcom_reqd_sub_group_size)
15#pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable
16#define ADRENO_GPU 1
17#define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half")))
18#define REQD_SUBGROUP_SIZE_128 __attribute__((qcom_reqd_sub_group_size("full")))
19#endif
20
21#define QK4_NL 32
22
23typedef char int8_t;
24typedef uchar uint8_t;
25typedef short int16_t;
26typedef ushort uint16_t;
27typedef int int32_t;
28typedef uint uint32_t;
29
30constant float kvalues_iq4nl[16] = {
31 -127.f, -104.f, -83.f, -65.f, -49.f, -35.f, -22.f, -10.f,
32 1.f, 13.f, 25.f, 38.f, 53.f, 69.f, 89.f, 113.f
33};
34
35//------------------------------------------------------------------------------
36// block_iq4_nl
37//------------------------------------------------------------------------------
38struct block_iq4_nl
39{
40 half d;
41 uint8_t qs[QK4_NL / 2];
42};
43
44//------------------------------------------------------------------------------
45// mul_vec_q_n_f32
46//------------------------------------------------------------------------------
47// Compute inner product between half a block of iq4_nl and 16 floats (yl).
48// il indicates where the quants begin (0 or 8).
49inline float block_iq4_nl_dot_y(
50 global struct block_iq4_nl * qb_curr,
51 private float * yl,
52 int il
53) {
54 float d = qb_curr->d;
55 float acc = 0.f;
56 global uchar * qs = qb_curr->qs + il;
57 for (int i = 0; i < 8; ++i) {
58 acc += yl[i] * kvalues_iq4nl[qs[i] & 0x0F];
59 acc += yl[i+8] * kvalues_iq4nl[qs[i] >> 4];
60 }
61 return d * acc;
62}
63
64#ifdef INTEL_GPU
65#define N_DST 4 // each subgroup group works on 4 rows
66#define N_SUBGROUP 1 // number of subgroups in a thread group
67#define N_SUBGROUP_SIZE 16 // assuming subgroup size is 16
68#elif defined (ADRENO_GPU)
69#define N_DST 4
70#define N_SUBGROUP 1
71#define N_SUBGROUP_SIZE 64
72#endif
73
74inline void mul_vec_q_n_f32(
75 global void * src0,
76 global float * src1,
77 global float * dst,
78 int ne00,
79 int ne01,
80 int ne02,
81 int ne10,
82 int ne12,
83 int ne0,
84 int ne1,
85 int r2,
86 int r3
87) {
88
89 const ulong nb = ne00/QK4_NL;
90
91 int r0 = get_group_id(0);
92 int r1 = get_group_id(1);
93 int im = get_group_id(2);
94
95 int first_row = (r0 * N_SUBGROUP + get_sub_group_id()) * N_DST;
96
97 int i12 = im%ne12;
98 int i13 = im/ne12;
99
100 ulong offset0 = first_row * nb + (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
101
102 global struct block_iq4_nl * x = (global struct block_iq4_nl *) src0 + offset0;
103 global float * y = (global float *) src1 + r1*ne10 + im*ne00*ne1;
104
105 float yl[16]; // src1 vector cache
106 float sumf[N_DST]={0.f};
107
108 int ix = get_sub_group_local_id()/2;
109 int il = 8*(get_sub_group_local_id()%2);
110
111 global float * yb = y + ix * QK4_NL + il;
112
113 // each thread in a SIMD group deals with half a block.
114 for (int ib = ix; ib < nb; ib += N_SUBGROUP_SIZE/2) {
115 for (int i = 0; i < 8; ++i) {
116 yl[i] = yb[i];
117 yl[i+8] = yb[i+16];
118 }
119
120 for (int row = 0; row < N_DST; row++) {
121 sumf[row] += block_iq4_nl_dot_y(x+ib+row*nb, yl, il);
122 }
123
124 yb += QK4_NL * (N_SUBGROUP_SIZE/2);
125 }
126
127 float tot[N_DST] = {
128 sub_group_reduce_add(sumf[0]), sub_group_reduce_add(sumf[1]),
129 sub_group_reduce_add(sumf[2]), sub_group_reduce_add(sumf[3])};
130 for (int row = 0; row < N_DST; ++row) {
131 if (get_sub_group_local_id() == 0 && first_row + row < ne01) {
132 dst[r1*ne0 + im*ne0*ne1 + first_row + row] = tot[row];
133 }
134 }
135}
136
137#ifdef INTEL_GPU
138REQD_SUBGROUP_SIZE_16
139#elif defined (ADRENO_GPU)
140REQD_SUBGROUP_SIZE_64
141#endif
142kernel void kernel_mul_mv_iq4_nl_f32(
143 global void * src0,
144 ulong offset0,
145 global float * src1,
146 ulong offset1,
147 global float * dst,
148 ulong offsetd,
149 int ne00,
150 int ne01,
151 int ne02,
152 int ne10,
153 int ne12,
154 int ne0,
155 int ne1,
156 int r2,
157 int r3
158) {
159 src0 = (global void*)((global char*)src0 + offset0);
160 src1 = (global float*)((global char*)src1 + offset1);
161 dst = (global float*)((global char*)dst + offsetd);
162
163 mul_vec_q_n_f32(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3);
164}