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

141 lines · 4343 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 QK1_0 128
22typedef struct {
23 half d;
24 uchar qs[QK1_0/8];
25} block_q1_0;
26
27#define NB_Q1_0 16
28
29#ifdef INTEL_GPU
30#define N_R0_Q1_0 4 // number of rows each subgroup works on
31#define N_SG_Q1_0 2 // number of subgroups in a work group
32#define N_SIMDWIDTH 16 // subgroup size
33#elif defined (ADRENO_GPU)
34#define N_R0_Q1_0 4
35#define N_SG_Q1_0 2
36#define N_SIMDWIDTH 64
37#endif
38
39inline float block_q_1_0_dot_y(global block_q1_0 * qb, float sumy, float yl[NB_Q1_0], short il) {
40 global uchar * qs = qb->qs + il*2;
41 uint b0 = qs[0];
42 uint b1 = qs[1];
43
44 float acc = 0.f;
45 acc += yl[ 0]*(float)((b0 >> 0) & 1) + yl[ 1]*(float)((b0 >> 1) & 1);
46 acc += yl[ 2]*(float)((b0 >> 2) & 1) + yl[ 3]*(float)((b0 >> 3) & 1);
47 acc += yl[ 4]*(float)((b0 >> 4) & 1) + yl[ 5]*(float)((b0 >> 5) & 1);
48 acc += yl[ 6]*(float)((b0 >> 6) & 1) + yl[ 7]*(float)((b0 >> 7) & 1);
49
50 acc += yl[ 8]*(float)((b1 >> 0) & 1) + yl[ 9]*(float)((b1 >> 1) & 1);
51 acc += yl[10]*(float)((b1 >> 2) & 1) + yl[11]*(float)((b1 >> 3) & 1);
52 acc += yl[12]*(float)((b1 >> 4) & 1) + yl[13]*(float)((b1 >> 5) & 1);
53 acc += yl[14]*(float)((b1 >> 6) & 1) + yl[15]*(float)((b1 >> 7) & 1);
54
55 return qb->d * (2.0f*acc - sumy);
56}
57
58#ifdef INTEL_GPU
59REQD_SUBGROUP_SIZE_16
60#elif defined (ADRENO_GPU)
61REQD_SUBGROUP_SIZE_64
62#endif
63kernel void kernel_mul_mv_q1_0_f32(
64 global char * src0,
65 ulong offset0,
66 global char * src1,
67 ulong offset1,
68 global char * dst,
69 ulong offsetd,
70 int ne00,
71 int ne01,
72 ulong nb01,
73 ulong nb02,
74 ulong nb03,
75 int ne12,
76 ulong nb11,
77 ulong nb12,
78 ulong nb13,
79 int ne0,
80 int ne1,
81 int r2,
82 int r3
83) {
84 src0 = (global char*)((global char*)src0 + offset0);
85 src1 = (global char*)((global char*)src1 + offset1);
86 dst = (global char*)((global char*)dst + offsetd);
87
88 int nb = ne00/QK1_0;
89
90 int r0 = get_group_id(0);
91 int r1 = get_group_id(1);
92 int im = get_group_id(2);
93
94 int first_row = (r0*N_SG_Q1_0 + get_sub_group_id()) * N_R0_Q1_0;
95
96 uint i12 = im%ne12;
97 uint i13 = im/ne12;
98
99 ulong offset_src1 = r1*nb11 + i12*nb12 + i13*nb13;
100 global float * y = (global float *) (src1 + offset_src1);
101
102 // pointers to src0 rows
103 global block_q1_0 * ax[N_R0_Q1_0];
104 for (int row = 0; row < N_R0_Q1_0; ++row) {
105 ulong offset_src0 = (first_row + row)*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03;
106 ax[row] = (global block_q1_0 *) ((global char *) src0 + offset_src0);
107 }
108
109 float yl[NB_Q1_0];
110 float sumf[N_R0_Q1_0] = { 0.f };
111
112 const short ix = get_sub_group_local_id()/8;
113 const short il = get_sub_group_local_id()%8;
114
115 global float * yb = y + ix*QK1_0 + il*NB_Q1_0;
116
117 // each thread handles NB_Q1_0 quants at a time
118 for (int ib = ix; ib < nb; ib += N_SIMDWIDTH/8) {
119 float sumy = 0.f;
120 for (short i = 0; i < NB_Q1_0; ++i) {
121 yl[i] = yb[i];
122 sumy += yb[i];
123 }
124
125 for (short row = 0; row < N_R0_Q1_0; row++) {
126 sumf[row] += block_q_1_0_dot_y(ax[row] + ib, sumy, yl, il);
127 }
128
129 yb += N_SIMDWIDTH*NB_Q1_0;
130 }
131
132 global float * dst_f32 = (global float *) dst + (ulong)im*ne0*ne1 + (ulong)r1*ne0;
133
134 for (int row = 0; row < N_R0_Q1_0; ++row) {
135 float tot = sub_group_reduce_add(sumf[row]);
136
137 if (get_sub_group_local_id() == 0 && first_row + row < ne01) {
138 dst_f32[first_row + row] = tot;
139 }
140 }
141}