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

149 lines · 4525 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_M_DELTA 0.125f
17
18// no d field: the super block scale is 4 nibbles spread over scales[]
19typedef struct {
20 uchar qs[QK_K/8];
21 uchar qh[QK_K/16];
22 uchar scales[QK_K/32];
23} block_iq1_m;
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_iq1_m_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_iq1_m * x = (global block_iq1_m *) (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_iq1_m * xb = (global block_iq1_m *)(xrow + row*nb01);
108
109 global ushort * sc = (global ushort *)xb->scales;
110 ushort u16 = (ushort)(((uint)sc[0] >> 12) | (((uint)sc[1] >> 8) & 0x00f0)
111 | (((uint)sc[2] >> 4) & 0x0f00) | ((uint)sc[3] & 0xf000));
112 float dsuper = (float)as_half(u16);
113
114 float acc = 0.f;
115 for (int il = 0; il < 4; ++il) {
116 int ib16 = 2*it + il/2;
117 float dl = dsuper * (float)(2*(((uint)sc[ib16/4] >> (3*(ib16%4))) & 7) + 1);
118
119 uchar qhb = xb->qh[2*it + il/2];
120 float dlt = (qhb & (0x08 << (4*(il%2)))) ? (-1.f - IQ1_M_DELTA) : (-1.f + IQ1_M_DELTA);
121
122 uint gi = (uint)xb->qs[4*it+il] | ((((uint)qhb >> (4*(il%2))) & 7) << 8);
123 uint g = iq1_s_grid[gi];
124
125 float a = 0.f;
126 for (int j = 0; j < 4; ++j) {
127 a += yl[8*il+j+0] * ((float)((g >> (8*j+0)) & 0xF) + dlt);
128 a += yl[8*il+j+4] * ((float)((g >> (8*j+4)) & 0xF) + dlt);
129 }
130 acc += dl * a;
131 }
132
133 sumf[row] += acc;
134 }
135
136 y4 += BLOCK_STRIDE * QK_K;
137 }
138
139 global float * dst_f32 = (global float *) dst + im*ne0*ne1 + r1*ne0;
140
141 for (int row = 0; row < N_DST; ++row) {
142 all_sum = sub_group_reduce_add(sumf[row]);
143 if (first_row + row < ne01) {
144 if (get_sub_group_local_id() == 0) {
145 dst_f32[first_row + row] = all_sum;
146 }
147 }
148 }
149}