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

142 lines · 4169 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// d, then 64 grid index bytes, then 8 uint32 of packed scale+signs
18typedef struct {
19 half d;
20 uchar qs[3*QK_K/8];
21} block_iq3_xxs;
22
23#undef N_DST
24#undef N_SIMDGROUP
25#undef N_SIMDWIDTH
26
27#ifdef INTEL_GPU
28#define N_DST 4
29#define N_SIMDGROUP 1
30#define N_SIMDWIDTH 16
31#elif defined (ADRENO_GPU)
32#define N_DST 4
33#define N_SIMDGROUP 1
34#define N_SIMDWIDTH 64
35#endif
36
37#undef BLOCK_STRIDE
38// 8 threads cover one super block, one 32 element sub block each
39#define BLOCK_STRIDE (N_SIMDWIDTH/8)
40
41#ifdef INTEL_GPU
42REQD_SUBGROUP_SIZE_16
43#elif defined (ADRENO_GPU)
44REQD_SUBGROUP_SIZE_64
45#endif
46kernel void kernel_mul_mv_iq3_xxs_f32_flat(
47 global uchar * src0_qs,
48 global uint * src0_sas,
49 global half * src0_d,
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 src1 = src1 + offset1;
70 dst = dst + offsetd;
71
72 int ix = get_sub_group_local_id()/8; // super block index
73 int it = get_sub_group_local_id()%8; // sub block inside the super block
74
75 int nb = ne00/QK_K;
76
77 int r0 = get_group_id(0);
78 int r1 = get_group_id(1);
79 int im = get_group_id(2);
80 int first_row = (r0 * N_SIMDGROUP + get_sub_group_id()) * N_DST;
81
82 int i12 = im%ne12;
83 int i13 = im/ne12;
84
85 ulong offset_src0 = (offset0 + first_row*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03)/sizeof(block_iq3_xxs);
86 ulong row_stride = nb01/sizeof(block_iq3_xxs);
87 ulong offset_src1 = r1*nb11 + (i12 )*nb12 + (i13 )*nb13;
88
89 global float * y = (global float *) (src1 + offset_src1);
90
91 float yl[32];
92 float sumf[N_DST] = {0.f};
93 float all_sum;
94
95 global float * y4 = y + ix * QK_K + 32 * it;
96
97 for (int ib = ix; ib < nb; ib += BLOCK_STRIDE) {
98 for (int i = 0; i < 32; ++i) {
99 yl[i] = y4[i];
100 }
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 const ulong b = offset_src0 + ib + row*row_stride;
105
106 global uchar * qs = src0_qs + 64*b + 8*it;
107
108 uint aux32 = src0_sas[8*b + it];
109
110 float db = (float)src0_d[b] * (0.5f + (float)(aux32 >> 28)) * 0.5f;
111
112 float acc = 0.f;
113 for (int l = 0; l < 4; ++l) {
114 uchar signs = iq3_xxs_signs((aux32 >> (7*l)) & 127u);
115 uint g1 = iq3_xxs_grid[qs[2*l+0]];
116 uint g2 = iq3_xxs_grid[qs[2*l+1]];
117
118 for (int j = 0; j < 4; ++j) {
119 float s1 = (signs & (1 << j)) ? -1.f : 1.f;
120 float s2 = (signs & (1 << (j+4))) ? -1.f : 1.f;
121 acc += yl[8*l+j+0] * (float)((g1 >> (8*j)) & 0xFF) * s1;
122 acc += yl[8*l+j+4] * (float)((g2 >> (8*j)) & 0xFF) * s2;
123 }
124 }
125
126 sumf[row] += db * acc;
127 }
128
129 y4 += BLOCK_STRIDE * QK_K;
130 }
131
132 global float * dst_f32 = (global float *) dst + im*ne0*ne1 + r1*ne0;
133
134 for (int row = 0; row < N_DST; ++row) {
135 all_sum = sub_group_reduce_add(sumf[row]);
136 if (first_row + row < ne01) {
137 if (get_sub_group_local_id() == 0) {
138 dst_f32[first_row + row] = all_sum;
139 }
140 }
141 }
142}