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

147 lines · 4430 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2
3#ifdef cl_intel_required_subgroup_size
4#pragma OPENCL EXTENSION cl_intel_required_subgroup_size : enable
5#define INTEL_GPU 1
6#define REQD_SUBGROUP_SIZE_16 __attribute__((intel_reqd_sub_group_size(16)))
7#define REQD_SUBGROUP_SIZE_32 __attribute__((intel_reqd_sub_group_size(32)))
8#elif defined(cl_qcom_reqd_sub_group_size)
9#pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable
10#define ADRENO_GPU 1
11#define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half")))
12#define REQD_SUBGROUP_SIZE_128 __attribute__((qcom_reqd_sub_group_size("full")))
13#endif
14
15#define QK_K 256
16#define IQ3_S_N_SCALE (QK_K/64)
17
18typedef struct {
19 half d;
20 uchar qs[QK_K/4];
21 uchar qh[QK_K/32];
22 uchar signs[QK_K/8];
23 uchar scales[IQ3_S_N_SCALE];
24} block_iq3_s;
25
26#undef N_DST
27#undef N_SIMDGROUP
28#undef N_SIMDWIDTH
29
30#ifdef INTEL_GPU
31#define N_DST 4
32#define N_SIMDGROUP 1
33#define N_SIMDWIDTH 16
34#elif defined (ADRENO_GPU)
35#define N_DST 4
36#define N_SIMDGROUP 1
37#define N_SIMDWIDTH 64
38#endif
39
40#undef BLOCK_STRIDE
41// 8 threads cover one super block, one 32 element sub block each
42#define BLOCK_STRIDE (N_SIMDWIDTH/8)
43
44#ifdef INTEL_GPU
45REQD_SUBGROUP_SIZE_16
46#elif defined (ADRENO_GPU)
47REQD_SUBGROUP_SIZE_64
48#endif
49kernel void kernel_mul_mv_iq3_s_f32_flat(
50 global uchar * src0_qs,
51 global uchar * src0_sg,
52 global uchar * src0_qh,
53 global uchar * src0_sc,
54 global half * src0_d,
55 ulong offset0,
56 global char * src1,
57 ulong offset1,
58 global char * dst,
59 ulong offsetd,
60 int ne00,
61 int ne01,
62 ulong nb01,
63 ulong nb02,
64 ulong nb03,
65 int ne12,
66 ulong nb11,
67 ulong nb12,
68 ulong nb13,
69 int ne0,
70 int ne1,
71 int r2,
72 int r3
73) {
74 src1 = src1 + offset1;
75 dst = dst + offsetd;
76
77 int ix = get_sub_group_local_id()/8; // super block index
78 int it = get_sub_group_local_id()%8; // sub block inside the super block
79
80 int nb = ne00/QK_K;
81
82 int r0 = get_group_id(0);
83 int r1 = get_group_id(1);
84 int im = get_group_id(2);
85 int first_row = (r0 * N_SIMDGROUP + get_sub_group_id()) * N_DST;
86
87 int i12 = im%ne12;
88 int i13 = im/ne12;
89
90 ulong offset_src0 = (offset0 + first_row*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03)/sizeof(block_iq3_s);
91 ulong row_stride = nb01/sizeof(block_iq3_s);
92 ulong offset_src1 = r1*nb11 + (i12 )*nb12 + (i13 )*nb13;
93
94 global float * y = (global float *) (src1 + offset_src1);
95
96 float yl[32];
97 float sumf[N_DST] = {0.f};
98 float all_sum;
99
100 global float * y4 = y + ix * QK_K + 32 * it;
101
102 for (int ib = ix; ib < nb; ib += BLOCK_STRIDE) {
103 for (int i = 0; i < 32; ++i) {
104 yl[i] = y4[i];
105 }
106
107 // only ne01 output rows exist; reading past them can pull in an inf scale
108 for (int row = 0; row < N_DST && first_row + row < ne01; row++) {
109 const ulong b = offset_src0 + ib + row*row_stride;
110
111 uchar nib = (it & 1) ? (src0_sc[4*b + (it >> 1)] >> 4) : (src0_sc[4*b + (it >> 1)] & 0xf);
112 float db = (float)src0_d[b] * (float)(1 + 2*nib);
113
114 uchar qhb = src0_qh[8*b + it];
115 global uchar * qsb = src0_qs + 64*b + 8*it;
116 global uchar * sgb = src0_sg + 32*b + 4*it;
117
118 float acc = 0.f;
119 for (int l = 0; l < 4; ++l) {
120 uint i1 = (uint)qsb[2*l+0] | (((uint)qhb << (8-2*l)) & 256);
121 uint i2 = (uint)qsb[2*l+1] | (((uint)qhb << (7-2*l)) & 256);
122 uint g1 = iq3_s_grid[i1];
123 uint g2 = iq3_s_grid[i2];
124 uchar sg = sgb[l];
125 for (int j = 0; j < 4; ++j) {
126 acc += yl[8*l+j+0] * (float)((g1 >> (8*j)) & 0xFF) * ((sg & (1 << j)) ? -1.f : 1.f);
127 acc += yl[8*l+j+4] * (float)((g2 >> (8*j)) & 0xFF) * ((sg & (1 << (j+4))) ? -1.f : 1.f);
128 }
129 }
130
131 sumf[row] += db * acc;
132 }
133
134 y4 += BLOCK_STRIDE * QK_K;
135 }
136
137 global float * dst_f32 = (global float *) dst + im*ne0*ne1 + r1*ne0;
138
139 for (int row = 0; row < N_DST; ++row) {
140 all_sum = sub_group_reduce_add(sumf[row]);
141 if (first_row + row < ne01) {
142 if (get_sub_group_local_id() == 0) {
143 dst_f32[first_row + row] = all_sum;
144 }
145 }
146 }
147}