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

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