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

40 lines · 1186 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2
3// Quantize a contiguous K-major [N, K] f32 activation buffer (K contiguous) into 32-value Q8_1 SoA blocks.
4// Store int8 quants, per-block scale d, and scaled sum s = d * sum(qs); one work-item handles each block.
5__kernel void kernel_quant_a_q8_1(
6 __global const float * src, // [N * K]
7 __global char * qa, // [N * K]
8 __global half * da, // [N * (K/32)]
9 __global half * sa, // [N * (K/32)]
10 int total_blocks // N * (K/32)
11) {
12 const int blk = get_global_id(0);
13 if (blk >= total_blocks) {
14 return;
15 }
16
17 const int base = blk * 32;
18
19 float v[32];
20 float amax = 0.0f;
21 #pragma unroll
22 for (int i = 0; i < 32; ++i) {
23 v[i] = src[base + i];
24 amax = fmax(amax, fabs(v[i]));
25 }
26
27 const float d = amax / 127.0f;
28 const float id = (amax > 0.0f) ? (127.0f / amax) : 0.0f;
29
30 int sum = 0;
31 #pragma unroll
32 for (int i = 0; i < 32; ++i) {
33 const int q = (int)rint(v[i] * id);
34 qa[base + i] = (char)q;
35 sum += q;
36 }
37
38 da[blk] = (half)d;
39 sa[blk] = (half)(d * (float)sum);
40}