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

143 lines · 4129 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 QK8_0 32
22typedef struct {
23 half d; // delta
24 char qs[QK8_0]; // quants
25} block_q8_0;
26
27#define NB_Q8_0 8
28
29#ifdef INTEL_GPU
30#define N_R0_Q8_0 4 // number of rows each subgroup works on
31#define N_SG_Q8_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_Q8_0 4
35#define N_SG_Q8_0 2
36#define N_SIMDWIDTH 64
37#endif
38
39#ifdef INTEL_GPU
40REQD_SUBGROUP_SIZE_16
41#elif defined (ADRENO_GPU)
42REQD_SUBGROUP_SIZE_64
43#endif
44kernel void kernel_mul_mv_id_q8_0_f32(
45 global char * src0,
46 ulong offset0,
47 global char * src1,
48 ulong offset1,
49 global char * src2,
50 ulong offset2,
51 global char * dst,
52 ulong offsetd,
53 int ne00,
54 int ne01,
55 ulong nb01,
56 ulong nb02,
57 int ne11,
58 int ne12,
59 ulong nb11,
60 ulong nb12,
61 int ne20,
62 int ne21,
63 ulong nb21,
64 int ne0,
65 int ne1
66) {
67 src0 = (global char *)((global char *)src0 + offset0);
68 src1 = (global char *)((global char *)src1 + offset1);
69 src2 = (global char *)((global char *)src2 + offset2);
70 dst = (global char *)((global char *)dst + offsetd);
71
72 int iid1 = get_group_id(2)/ne20;
73 int idx = get_group_id(2)%ne20;
74
75 int i02 = ((global int *) (src2 + iid1*nb21))[idx];
76 if (i02 == -1) { // skipped slot, the dst row is zeroed by kernel_moe_zero_dst
77 return;
78 }
79
80 int i11_ = idx % ne11;
81 int i12_ = iid1;
82
83 int i1 = idx;
84 int i2 = i12_;
85
86 global char * src0_cur = src0 + i02*nb02;
87 global char * src1_cur = src1 + i11_*nb11 + i12_*nb12;
88
89 global char * dst_cur = dst + (i1*ne0 + i2*ne1*ne0)*sizeof(float);
90
91 int nb = ne00/QK8_0;
92
93 int r0 = get_group_id(0);
94 int r1 = get_group_id(1);
95
96 int first_row = (r0*N_SG_Q8_0 + get_sub_group_id()) * N_R0_Q8_0;
97
98 ulong offset_src1 = r1*nb11;
99 global float * y = (global float *) (src1_cur + offset_src1);
100
101 // pointers to src0 rows
102 global block_q8_0 * ax[N_R0_Q8_0];
103 for (int row = 0; row < N_R0_Q8_0; ++row) {
104 ulong offset_src0 = (first_row + row)*nb01;
105 ax[row] = (global block_q8_0 *) ((global char *) src0_cur + offset_src0);
106 }
107
108 float yl[NB_Q8_0];
109 float sumf[N_R0_Q8_0] = { 0.f };
110
111 const short ix = get_sub_group_local_id()/4;
112 const short il = get_sub_group_local_id()%4;
113
114 global float * yb = y + ix*QK8_0 + il*NB_Q8_0;
115
116 // each thread handles NB_Q8_0 quants at a time
117 for (int ib = ix; ib < nb; ib += N_SIMDWIDTH/4) {
118 for (short i = 0; i < NB_Q8_0; ++i) {
119 yl[i] = yb[i];
120 }
121
122 for (short row = 0; row < N_R0_Q8_0; row++) {
123 global char * qs = ax[row][ib].qs + il*NB_Q8_0;
124 float sumq = 0.f;
125 for (short iq = 0; iq < NB_Q8_0; ++iq) {
126 sumq += qs[iq] * yl[iq];
127 }
128 sumf[row] += sumq*ax[row][ib].d;
129 }
130
131 yb += N_SIMDWIDTH*NB_Q8_0;
132 }
133
134 global float * dst_f32 = (global float *) dst_cur + (ulong)r1*ne0;
135
136 for (int row = 0; row < N_R0_Q8_0; ++row) {
137 float tot = sub_group_reduce_add(sumf[row]);
138
139 if (get_sub_group_local_id() == 0 && first_row + row < ne01) {
140 dst_f32[first_row + row] = tot;
141 }
142 }
143}