openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / workloads / quant_int8 / kernel.cu
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | #include <cuda_runtime.h> |
| 3 | |
| 4 | // One thread per row: absmax, scale = absmax / 127, q = clamp(rint(x / scale), -127, 127). |
| 5 | __global__ void quant_int8(const float *x, signed char *q, float *s, int R, int C) |
| 6 | { |
| 7 | int r = blockIdx.x * blockDim.x + threadIdx.x; |
| 8 | if (r >= R) |
| 9 | return; |
| 10 | float amax = 0.0f; |
| 11 | for (int c = 0; c < C; c++) |
| 12 | amax = fmaxf(amax, fabsf(x[r * C + c])); |
| 13 | float sc = amax / 127.0f; |
| 14 | s[r] = sc; |
| 15 | for (int c = 0; c < C; c++) { |
| 16 | float y = sc > 0.0f ? x[r * C + c] / sc : 0.0f; |
| 17 | float v = fminf(fmaxf(rintf(y), -127.0f), 127.0f); |
| 18 | q[r * C + c] = (signed char)(int)v; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | void oal_launch(const long long *p, void *const *buf) |
| 23 | { |
| 24 | int R = (int)p[0]; |
| 25 | int C = (int)p[1]; |
| 26 | quant_int8<<<(R + 63) / 64, 64>>>((const float *)buf[0], (signed char *)buf[1], (float *)buf[2], R, C); |
| 27 | } |