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 / gemv_noshuffle_q6_k_f32_32b_trans.cl

128 lines · 5283 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2#pragma OPENCL EXTENSION cl_khr_subgroups : enable
3#pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable
4
5#define QK_K 256
6#define N_SIMDGROUP 8
7#define SIMDGROUP_WIDTH 64
8
9static inline float8 q6_k_to_fp32_packed8(ushort2 ql8, ushort qh8, float d_scale) {
10 float8 fp32x8;
11 fp32x8.s0 = ((float)(( ql8.s0 & 0x000F) | ((uint)((qh8 ) & 0x3) << 4)) - 32.f) * d_scale;
12 fp32x8.s1 = ((float)((( ql8.s0 >> 4) & 0x000F) | ((uint)((qh8 >> 2) & 0x3) << 4)) - 32.f) * d_scale;
13 fp32x8.s2 = ((float)((( ql8.s0 >> 8) & 0x000F) | ((uint)((qh8 >> 4) & 0x3) << 4)) - 32.f) * d_scale;
14 fp32x8.s3 = ((float)((( ql8.s0 >> 12)& 0x000F) | ((uint)((qh8 >> 6) & 0x3) << 4)) - 32.f) * d_scale;
15 fp32x8.s4 = ((float)(( ql8.s1 & 0x000F) | ((uint)((qh8 >> 8) & 0x3) << 4)) - 32.f) * d_scale;
16 fp32x8.s5 = ((float)((( ql8.s1 >> 4) & 0x000F) | ((uint)((qh8 >>10) & 0x3) << 4)) - 32.f) * d_scale;
17 fp32x8.s6 = ((float)((( ql8.s1 >> 8) & 0x000F) | ((uint)((qh8 >>12) & 0x3) << 4)) - 32.f) * d_scale;
18 fp32x8.s7 = ((float)((( ql8.s1 >> 12)& 0x000F) | ((uint)((qh8 >>14) & 0x3) << 4)) - 32.f) * d_scale;
19 return fp32x8;
20}
21
22__attribute__((qcom_reqd_sub_group_size("half")))
23__kernel void kernel_gemv_noshuffle_q6_k_f32_32b_trans(
24 __read_only image1d_buffer_t src0_ql,
25 __read_only image1d_buffer_t src0_qh,
26 __global char * src0_s,
27 __global half * src0_d,
28 __read_only image1d_buffer_t src1,
29 __global float * dst,
30 ulong offsetd,
31 int ne00,
32 int ne01
33) {
34 uint i01 = get_global_id(0);
35 uint sgid = get_local_id(1);
36 uint slid = get_sub_group_local_id();
37
38 int num_superblocks = ne00 / QK_K;
39 int num_subblocks = ne00 / 32; // 2 sub-blocks of 16 processed per iter below
40 int scales_per_row = num_superblocks * 16;
41
42 __private float sum = 0.0f;
43
44 // Loop over 32-element groups (2 sub-blocks of 16 each), N_SIMDGROUP groups per iter.
45 for (uint ib = sgid; ib < num_subblocks; ib += N_SIMDGROUP) {
46 uint sb = ib / 8; // super-block index
47 uint j = ib % 8; // 32-element group within super-block (0..7)
48
49 // Load d for this super-block.
50 half d_val = src0_d[sb * ne01 + i01];
51
52 // Load 2 sub-block scales (int8), one per 16 elements.
53 global const char * sc = src0_s + i01 * scales_per_row + sb * 16;
54 float scale0 = (float)d_val * (float)sc[j * 2];
55 float scale1 = (float)d_val * (float)sc[j * 2 + 1];
56
57 // Load 4 uints of ql (32 elements, 4-bit each = 128 bits), column-major stride ne01.
58 uint ql_base = (ib * 4) * ne01 + i01;
59 uint4 regQL;
60 regQL.s0 = read_imageui(src0_ql, ql_base).x;
61 regQL.s1 = read_imageui(src0_ql, ql_base + ne01).x;
62 regQL.s2 = read_imageui(src0_ql, ql_base + ne01 * 2).x;
63 regQL.s3 = read_imageui(src0_ql, ql_base + ne01 * 3).x;
64
65 // Load 2 uints of qh (32 elements, 2-bit each = 64 bits), column-major stride ne01.
66 uint qh_base = (ib * 2) * ne01 + i01;
67 uint2 regQH;
68 regQH.s0 = read_imageui(src0_qh, qh_base).x;
69 regQH.s1 = read_imageui(src0_qh, qh_base + ne01).x;
70
71 // Load activations: 32 floats = 8 float4s.
72 uint y_offset = ib * 8;
73
74 float4 y_local = (slid < 8) ? read_imagef(src1, (y_offset + slid)) : (float4)0.0f;
75 float4 y0 = sub_group_broadcast(y_local, 0);
76 float4 y1 = sub_group_broadcast(y_local, 1);
77 float4 y2 = sub_group_broadcast(y_local, 2);
78 float4 y3 = sub_group_broadcast(y_local, 3);
79 float4 y4v = sub_group_broadcast(y_local, 4);
80 float4 y5 = sub_group_broadcast(y_local, 5);
81 float4 y6 = sub_group_broadcast(y_local, 6);
82 float4 y7 = sub_group_broadcast(y_local, 7);
83
84 // Dequantize elements 0..7 (scale0).
85 float8 fp32x8 = q6_k_to_fp32_packed8(as_ushort2(regQL.s0), (ushort)(regQH.s0 & 0xFFFF), scale0);
86
87 float4 acc = y0 * fp32x8.lo;
88 acc += y1 * fp32x8.hi;
89
90 // Dequantize elements 8..15 (scale0).
91 fp32x8 = q6_k_to_fp32_packed8(as_ushort2(regQL.s1), (ushort)(regQH.s0 >> 16), scale0);
92
93 acc += y2 * fp32x8.lo;
94 acc += y3 * fp32x8.hi;
95
96 // Dequantize elements 16..23 (scale1).
97 fp32x8 = q6_k_to_fp32_packed8(as_ushort2(regQL.s2), (ushort)(regQH.s1 & 0xFFFF), scale1);
98
99 acc += y4v * fp32x8.lo;
100 acc += y5 * fp32x8.hi;
101
102 // Dequantize elements 24..31 (scale1).
103 fp32x8 = q6_k_to_fp32_packed8(as_ushort2(regQL.s3), (ushort)(regQH.s1 >> 16), scale1);
104
105 acc += y6 * fp32x8.lo;
106 acc += y7 * fp32x8.hi;
107
108 sum += ((acc.s0 + acc.s1) + (acc.s2 + acc.s3));
109 }
110
111 // reduction in local memory, assumes #subgroups=4
112 __local float reduceLM[SIMDGROUP_WIDTH * (N_SIMDGROUP - 1)];
113 if (sgid > 0) {
114 reduceLM[SIMDGROUP_WIDTH * (sgid - 1) + slid] = sum;
115 }
116 barrier(CLK_LOCAL_MEM_FENCE);
117 if (sgid == 0) {
118 for (uint i = 0; i < N_SIMDGROUP - 1; ++i) {
119 sum += reduceLM[SIMDGROUP_WIDTH * i + slid];
120 }
121 }
122
123 // 1 output per thread in subgroup 0
124 if (sgid == 0) {
125 dst = dst + (offsetd >> 2);
126 dst[i01] = sum;
127 }
128}