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

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