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

139 lines · 4038 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#define IQ1_S_DELTA 0.125f
17
18typedef struct {
19 half d;
20 uchar qs[QK_K/8];
21 ushort qh[QK_K/32];
22} block_iq1_s;
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_iq1_s_f32_flat(
48 global uchar * src0_qs,
49 global ushort * src0_qh,
50 global half * src0_d,
51 ulong offset0,
52 global char * src1,
53 ulong offset1,
54 global char * dst,
55 ulong offsetd,
56 int ne00,
57 int ne01,
58 ulong nb01,
59 ulong nb02,
60 ulong nb03,
61 int ne12,
62 ulong nb11,
63 ulong nb12,
64 ulong nb13,
65 int ne0,
66 int ne1,
67 int r2,
68 int r3
69) {
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 = (offset0 + first_row*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03)/sizeof(block_iq1_s);
87 ulong row_stride = nb01/sizeof(block_iq1_s);
88 ulong offset_src1 = r1*nb11 + (i12 )*nb12 + (i13 )*nb13;
89
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 // only ne01 output rows exist; reading past them can pull in an inf scale
104 for (int row = 0; row < N_DST && first_row + row < ne01; row++) {
105 const ulong b = offset_src0 + ib + row*row_stride;
106
107 ushort qhb = src0_qh[8*b + it];
108 float dl = (float)src0_d[b] * (float)(2*((qhb >> 12) & 7) + 1);
109 float dlt = (qhb & 0x8000) ? (-1.f - IQ1_S_DELTA) : (-1.f + IQ1_S_DELTA);
110
111 global uchar * qsb = src0_qs + 32*b + 4*it;
112
113 float acc = 0.f;
114 for (int l = 0; l < 4; ++l) {
115 uint gi = (uint)qsb[l] | ((((uint)qhb >> (3*l)) & 7) << 8);
116 uint g = iq1_s_grid[gi];
117 for (int j = 0; j < 4; ++j) {
118 acc += yl[8*l+j+0] * ((float)((g >> (8*j+0)) & 0xF) + dlt);
119 acc += yl[8*l+j+4] * ((float)((g >> (8*j+4)) & 0xF) + dlt);
120 }
121 }
122
123 sumf[row] += dl * acc;
124 }
125
126 y4 += BLOCK_STRIDE * QK_K;
127 }
128
129 global float * dst_f32 = (global float *) dst + im*ne0*ne1 + r1*ne0;
130
131 for (int row = 0; row < N_DST; ++row) {
132 all_sum = sub_group_reduce_add(sumf[row]);
133 if (first_row + row < ne01) {
134 if (get_sub_group_local_id() == 0) {
135 dst_f32[first_row + row] = all_sum;
136 }
137 }
138 }
139}