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

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