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

147 lines · 4416 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(
49 global char * src0,
50 ulong offset0,
51 global char * src1,
52 ulong offset1,
53 global char * dst,
54 ulong offsetd,
55 int ne00,
56 int ne01,
57 ulong nb01,
58 ulong nb02,
59 ulong nb03,
60 int ne12,
61 ulong nb11,
62 ulong nb12,
63 ulong nb13,
64 int ne0,
65 int ne1,
66 int r2,
67 int r3
68) {
69 src0 = src0 + offset0;
70 src1 = src1 + offset1;
71 dst = dst + offsetd;
72
73 int ix = get_sub_group_local_id()/8; // super block index
74 int it = get_sub_group_local_id()%8; // sub block inside the super block
75
76 int nb = ne00/QK_K;
77
78 int r0 = get_group_id(0);
79 int r1 = get_group_id(1);
80 int im = get_group_id(2);
81 int first_row = (r0 * N_SIMDGROUP + get_sub_group_id()) * N_DST;
82
83 int i12 = im%ne12;
84 int i13 = im/ne12;
85
86 ulong offset_src0 = first_row*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03;
87 ulong offset_src1 = r1*nb11 + (i12 )*nb12 + (i13 )*nb13;
88
89 global block_iq2_s * x = (global block_iq2_s *) (src0 + offset_src0);
90 global float * y = (global float *) (src1 + offset_src1);
91
92 float yl[32];
93 float sumf[N_DST] = {0.f};
94 float all_sum;
95
96 global float * y4 = y + ix * QK_K + 32 * it;
97
98 for (int ib = ix; ib < nb; ib += BLOCK_STRIDE) {
99 for (int i = 0; i < 32; ++i) {
100 yl[i] = y4[i];
101 }
102
103 global char * xrow = (global char *)(x + ib);
104
105 // only ne01 output rows exist; reading past them can pull in an inf scale
106 for (int row = 0; row < N_DST && first_row + row < ne01; row++) {
107 global block_iq2_s * xb = (global block_iq2_s *)(xrow + row*nb01);
108
109 uchar scb = xb->scales[it];
110 float dlo = (float)xb->d * (0.5f + (float)(scb & 0xf)) * 0.25f;
111 float dhi = (float)xb->d * (0.5f + (float)(scb >> 4)) * 0.25f;
112
113 uchar qhb = xb->qh[it];
114 global uchar * qsb = xb->qs + 4*it;
115 global uchar * sgb = xb->qs + 32 + 4*it;
116
117 float alo = 0.f, ahi = 0.f;
118 for (int l = 0; l < 4; ++l) {
119 uint gi = (uint)qsb[l] | (((uint)qhb << (8-2*l)) & 0x300);
120 uchar sg = sgb[l];
121 uint lo = iq2_s_grid[2*gi+0];
122 uint hi = iq2_s_grid[2*gi+1];
123 float a = 0.f;
124 for (int j = 0; j < 4; ++j) {
125 a += yl[8*l+j+0] * (float)((lo >> (8*j)) & 0xFF) * ((sg & (1 << j)) ? -1.f : 1.f);
126 a += yl[8*l+j+4] * (float)((hi >> (8*j)) & 0xFF) * ((sg & (1 << (j+4))) ? -1.f : 1.f);
127 }
128 if (l < 2) alo += a; else ahi += a;
129 }
130
131 sumf[row] += dlo * alo + dhi * ahi;
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}