Skip to content

armgit / facex

Public Imported from GitHub
0

Full face stack that runs entirely in the browser. Detection, 576-point 3D mesh, recognition, anti-spoof, smile — all WebAssembly, zero server. Apache 2.0.

7.0 KB
Raw Blame
/*
 * gemm_int8.c — INT8 GEMM with AVX-512 VNNI.
 *
 * Uses VPDPBUSD: u8 × s8 → s32 accumulation, no saturation.
 * Tile: MR=4, NR=16 (4 rows × 16 cols, 4 ZMM accumulators).
 *
 * Weight layout: packed c4 format (4 k-values per group for VNNI alignment).
 * Activation: s8 converted to u8 via +128 offset (XOR trick).
 * Compensation: pre-computed 128 * col_sum per output channel.
 *
 * Fused epilogue: acc → float → (- compensation) * w_scale → + bias → SiLU → * out_scale → clamp → s8
 */

#include "nn2_internal.h"
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <immintrin.h>

/* ============ Weight packing for VNNI: [Cout_group][K_blocks][16×4 bytes] ============ */

/* Pack int8 weights from [Cout, K] row-major into VNNI-friendly format.
 * NR=16 columns per group, K padded to multiple of 4.
 * Layout per group: [K/4 blocks of 64 bytes: 16 cols × 4 k-values] */
void nn2_int8_pack_weights(
    const int8_t* weights,  /* [Cout, K] */
    int K, int Cout,
    void* packed_out,       /* output buffer */
    int32_t* col_sums)      /* [Cout]: 128 * sum(w) per channel */
{
    int K4 = (K + 3) & ~3;
    uint8_t* out = (uint8_t*)packed_out;

    for (int co = 0; co < Cout; co += 16) {
        int nr = (co + 16 <= Cout) ? 16 : Cout - co;

        /* Compute col_sums */
        for (int j = 0; j < nr; j++) {
            int32_t sum = 0;
            for (int k = 0; k < K; k++)
                sum += (int32_t)weights[(co + j) * K + k];
            col_sums[co + j] = 128 * sum;
        }

        /* Pack: [K4/4 blocks][16 cols × 4 k-values = 64 bytes] */
        for (int k = 0; k < K4; k += 4) {
            for (int j = 0; j < 16; j++) {
                for (int kk = 0; kk < 4; kk++) {
                    if (j < nr && (k + kk) < K)
                        *out++ = (uint8_t)weights[(co + j) * K + (k + kk)];
                    else
                        *out++ = 0;
                }
            }
        }
    }
}

int nn2_int8_packed_size(int K, int Cout)
{
    int K4 = (K + 3) & ~3;
    int groups = (Cout + 15) / 16;
    return groups * K4 * 16; /* bytes */
}

/* ============ VNNI microkernel: 4 rows × 16 cols ============ */

#ifdef __AVX512VNNI__

static void vnni_4x16_ukernel(
    int K,                           /* must be padded to multiple of 4 */
    const int8_t* A, int a_stride,   /* [mr, K] s8 activation */
    const uint8_t* W,                /* packed weights [K/4][16×4] */
    int32_t* C,                      /* [mr, 16] output accumulators */
    int mr)
{
    __m512i acc0 = _mm512_setzero_si512();
    __m512i acc1 = _mm512_setzero_si512();
    __m512i acc2 = _mm512_setzero_si512();
    __m512i acc3 = _mm512_setzero_si512();

    const int8_t* a0 = A;
    const int8_t* a1 = (mr > 1) ? a0 + a_stride : a0;
    const int8_t* a2 = (mr > 2) ? a0 + 2 * a_stride : a0;
    const int8_t* a3 = (mr > 3) ? a0 + 3 * a_stride : a0;

    const uint32_t xor32 = 0x80808080u;

    for (int k = 0; k < K; k += 4) {
        /* Load packed B: 16 columns × 4 k-values = 64 bytes = 1 ZMM */
        __m512i vb = _mm512_loadu_si512((const __m512i*)W);
        W += 64;

        /* Broadcast 4 A bytes (one row), XOR to unsigned */
        uint32_t a0_val, a1_val, a2_val, a3_val;
        memcpy(&a0_val, a0 + k, 4); a0_val ^= xor32;
        memcpy(&a1_val, a1 + k, 4); a1_val ^= xor32;
        memcpy(&a2_val, a2 + k, 4); a2_val ^= xor32;
        memcpy(&a3_val, a3 + k, 4); a3_val ^= xor32;

        __m512i va0 = _mm512_set1_epi32(a0_val);
        __m512i va1 = _mm512_set1_epi32(a1_val);
        __m512i va2 = _mm512_set1_epi32(a2_val);
        __m512i va3 = _mm512_set1_epi32(a3_val);

        /* VPDPBUSD: acc += u8(A) · s8(B) per 32-bit lane, no saturation */
        acc0 = _mm512_dpbusd_epi32(acc0, va0, vb);
        acc1 = _mm512_dpbusd_epi32(acc1, va1, vb);
        acc2 = _mm512_dpbusd_epi32(acc2, va2, vb);
        acc3 = _mm512_dpbusd_epi32(acc3, va3, vb);
    }

    _mm512_storeu_si512((__m512i*)(C),      acc0);
    if (mr > 1) _mm512_storeu_si512((__m512i*)(C + 16), acc1);
    if (mr > 2) _mm512_storeu_si512((__m512i*)(C + 32), acc2);
    if (mr > 3) _mm512_storeu_si512((__m512i*)(C + 48), acc3);
}

#endif /* __AVX512VNNI__ */

/* ============ Full INT8 GEMM with fused epilogue ============ */

void nn2_int8_gemm_fused(
    const int8_t* A, int M, int K, int N,  /* A[M,K] s8 activations */
    const void* B_packed,                    /* packed weights */
    const int32_t* col_sums,                /* [N] compensation */
    const float* w_scales,                   /* [N] per-channel weight scale */
    const float* bias,                       /* [N] or NULL */
    float act_scale,                         /* input activation scale (per-tensor) */
    float out_scale,                         /* output activation scale (per-tensor) */
    int act_type,                            /* 0=none, 1=SiLU, 2=ReLU */
    float* output)                           /* [M, N] float output */
{
    int K4 = (K + 3) & ~3;
    /* Pad A to K4 */
    int8_t* A_pad = NULL;
    const int8_t* A_eff = A;
    if (K != K4) {
        A_pad = (int8_t*)calloc(M * K4, 1);
        for (int m = 0; m < M; m++)
            memcpy(A_pad + m * K4, A + m * K, K);
        A_eff = A_pad;
    }

#ifdef __AVX512VNNI__
    int32_t acc_buf[4 * 16];

    for (int m = 0; m < M; m += 4) {
        int mr = (m + 4 <= M) ? 4 : M - m;
        const uint8_t* w_ptr = (const uint8_t*)B_packed;

        for (int n = 0; n < N; n += 16) {
            int nr = (n + 16 <= N) ? 16 : N - n;

            memset(acc_buf, 0, sizeof(acc_buf));
            vnni_4x16_ukernel(K4, A_eff + m * K4, K4, w_ptr, acc_buf, mr);

            /* Fused epilogue: dequant + bias + activation */
            float inv_out = (out_scale > 0) ? 1.0f / out_scale : 1.0f;
            for (int i = 0; i < mr; i++) {
                float* orow = output + (m + i) * N + n;
                for (int j = 0; j < nr; j++) {
                    /* Dequantize: (acc - compensation) * act_scale * w_scale */
                    float val = (float)(acc_buf[i * 16 + j] - col_sums[n + j])
                                * act_scale * w_scales[n + j];
                    if (bias) val += bias[n + j];

                    /* Activation */
                    if (act_type == 1) { /* SiLU */
                        float sig = 1.0f / (1.0f + expf(-val));
                        val = val * sig;
                    } else if (act_type == 2) { /* ReLU */
                        if (val < 0) val = 0;
                    }

                    orow[j] = val;
                }
            }

            w_ptr += K4 * 16;
        }
    }
#else
    /* Scalar fallback */
    for (int m = 0; m < M; m++) {
        for (int n = 0; n < N; n++) {
            int32_t acc = 0;
            for (int k = 0; k < K; k++)
                acc += (int32_t)((uint8_t)((int)A_eff[m * K + k] + 128)) * (int32_t)((const int8_t*)B_packed)[/* ... */0];
            /* ... simplified, real impl needs proper unpacking */
            output[m * N + n] = (float)acc;
        }
    }
#endif

    free(A_pad);
}