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.
C avx-512avx2biometricsc99computer-visioncpu-inferencedeep-learningedge-aiembedded-aiface-embeddingface-recognitionface-verificationlow-latencyonnx-runtimeproduction-readysimdzero-dependencies
Починил сборку библиотеки и вычисления с упакованными матрицами
Baurzhan Atynov5f84a673d53e
diff --git a/Makefile b/Makefileindex cc30556..1c1b536 100644--- a/Makefile+++ b/Makefile@@ -8,18 +8,11 @@ CC ?= gccCFLAGS = -O3 -march=native -mfma -funroll-loopsLDFLAGS = -lm -lpthread-# Detect AVX-512-AVX512 := $(shell $(CC) -mavx512f -dM -E - < /dev/null 2>/dev/null | grep -c AVX512F)-ifeq ($(AVX512),1)- CFLAGS += -mavx512f -mavx512vnni -mprefer-vector-width=512-endif-ifeq ($(OS),Windows_NT)LDFLAGS += -lsynchronizationEXT = .exeendif-SRCS = src/facex.c src/transformer_ops.c src/gemm_int8_4x8c8.c src/threadpool.c.PHONY: all clean example lib cli encrypt test detect-lib v2-cli@@ -28,14 +21,15 @@ all: lib cli detect-lib v2-cli# Static librarylib: libfacex.a-libfacex.a: $(SRCS)- $(CC) $(CFLAGS) -DFACEX_LIB -c src/facex.c -o facex.o- $(CC) $(CFLAGS) -c src/transformer_ops.c -o transformer_ops.o- $(CC) $(CFLAGS) -c src/gemm_int8_4x8c8.c -o gemm_int8_4x8c8.o- $(CC) $(CFLAGS) -c src/threadpool.c -o threadpool.o- ar rcs $@ facex.o transformer_ops.o gemm_int8_4x8c8.o threadpool.o- @rm -f *.o- @echo "Built libfacex.a"+LIBOBJ = facex.o transformer_ops.o gemm_int8_4x8c8.o threadpool.o detect.o align.o weight_crypto.o++libfacex.a: $(LIBOBJ)+ $(AR) rcs $@ $^++%.o: src/%.c+ $(CC) $(CPPFLAGS) $(CFLAGS) -Iinclude -MMD -MP -c $< -o $@++-include $(LIBOBJ:.o=.d)# Standalone CLI (for Go subprocess / testing)cli: facex-cli$(EXT)@@ -56,8 +50,15 @@ encrypt: facex-encrypt$(EXT)facex-encrypt$(EXT): src/weight_crypto.c$(CC) $(CFLAGS) -DWEIGHT_CRYPTO_MAIN -o $@ $< $(LDFLAGS)+.PHONY: check+check: kernel-test$(EXT)+ ./kernel-test$(EXT)++kernel-test$(EXT): tests/packed_math.c libfacex.a+ $(CC) $(CFLAGS) -Iinclude -o $@ $^ $(LDFLAGS)+# Golden test-test: golden-test$(EXT)+test: check golden-test$(EXT)@echo "Running golden test..."@./golden-test$(EXT) data/edgeface_xs_fp32.bin@@ -89,11 +90,8 @@ example_mfn$(EXT): examples/example_mfn.c src/facex_mfn.c# later sprints — see docs/plan/detector_plan.md).detect-lib: libdetect.a-libdetect.a: src/detect.c include/detect.h- $(CC) $(CFLAGS) -Iinclude -c src/detect.c -o detect.o- ar rcs $@ detect.o- @rm -f detect.o- @echo "Built libdetect.a"+libdetect.a: detect.o+ $(AR) rcs $@ $^clean:- rm -f libfacex.a libdetect.a facex-cli$(EXT) facex-example$(EXT) facex-encrypt$(EXT) golden-test$(EXT) facex-v2$(EXT) facex-mfn$(EXT) *.o+ rm -f libfacex.a libdetect.a facex-cli$(EXT) facex-example$(EXT) facex-encrypt$(EXT) golden-test$(EXT) facex-v2$(EXT) facex-mfn$(EXT) kernel-test$(EXT) *.o $(LIBOBJ:.o=.d)diff --git a/examples/example.c b/examples/example.cindex 591437b..3d5c69a 100644--- a/examples/example.c+++ b/examples/example.c@@ -15,7 +15,7 @@ int main(int argc, char** argv) {printf("FaceX %s\n", facex_version());/* Initialize engine */- FaceX* fx = facex_init(weights, NULL);+ FaceX* fx = facex_init(weights, NULL, NULL);if (!fx) {fprintf(stderr, "Failed to load weights: %s\n", weights);return 1;diff --git a/src/edgeface_engine.c b/src/edgeface_engine.cindex 2255586..2f7be96 100644--- a/src/edgeface_engine.c+++ b/src/edgeface_engine.c@@ -221,7 +221,7 @@ static void xca_dw_conv_nchw(const float* in_nchw, int C, int H, int W,}}-static void xca_block(float* x_hwc, int H, int W, int C,+void xca_block(float* x_hwc, int H, int W, int C,const float* gamma_xca, const float* gamma,/* DW conv for split heads */int n_dw_splits, const int* dw_split_sizes,@@ -461,7 +461,7 @@ void conv2d_hwc_reorder_weights(float* w, int Cout, int Cin, int KK) {free(tmp);}-static void conv2d_hwc(const float* in_hwc, int Cin, int H, int W,+void conv2d_hwc(const float* in_hwc, int Cin, int H, int W,const float* w, /* REORDERED to [Cin*KK, Cout] */const float* b, int Cout, int K, int stride,float* out_hwc) {@@ -618,7 +618,7 @@ static void _mlp_rows(void* ctx_, int start, int end) {/* ============ ConvNeXt Block (HWC in/out, NCHW for DW) ============ *//* x[HW,C] → LN → DW Conv → gamma*x → LN → MLP(4 MatMul) → +residual */-static void convnext_block(float* x_hwc, int H, int W, int C,+void convnext_block(float* x_hwc, int H, int W, int C,const float* gamma,const float* dw_w, const float* dw_b, int K,const float* ln_w, const float* ln_b,diff --git a/src/transformer_ops.c b/src/transformer_ops.cindex 3ead9be..c26d62a 100644--- a/src/transformer_ops.c+++ b/src/transformer_ops.c@@ -534,6 +534,10 @@ void matmul_fp32(const float* A, const float* B, float* C,* Each panel of 8 columns is stored contiguously along K dimension.* This gives sequential 32-byte (1 cache line) loads in the inner K loop. */void pack_b_fp32(const float* B, int K, int N, float* packed) {+#ifndef __AVX2__+ memcpy(packed, B, (size_t)K * N * sizeof(float));+ return;+#endif#ifdef __AVX512F__int NR = 16;#else@@ -554,7 +558,9 @@ void pack_b_fp32(const float* B, int K, int N, float* packed) {}int packed_b_fp32_size(int K, int N) {-#ifdef __AVX512F__+#ifndef __AVX2__+ return K * N;+#elif defined(__AVX512F__)return ((N + 15) / 16) * K * 16;#elsereturn ((N + 7) / 8) * K * 8;@@ -810,6 +816,22 @@ void matmul_fp32_packed(const float* A, const float* B_packed, float* C,* Eliminates separate bias addition pass. */void matmul_fp32_packed_bias(const float* A, const float* B_packed, const float* bias,float* C, int M, int K, int N) {+#ifdef __AVX512F__+ /* AVX512 packs 16 columns per panel; the fused AVX2 kernel expects eight. */+ {+#else+ if (!bias || N % 8 != 0) {+#endif+ matmul_fp32_packed(A, B_packed, C, M, K, N);+ for (int m = 0; m < M; m++) {+ for (int n = 0; n < N; n++) {+ size_t i = (size_t)m * N + n;+ float v = C[i] + (bias ? bias[n] : 0.0f);+ C[i] = v;+ }+ }+ return;+ }#ifdef __AVX2__int NR = 8;int n_panels = (N + NR - 1) / NR;@@ -925,6 +947,22 @@ static inline __m256 _gelu_ymm(__m256 v) {void matmul_bias_gelu_packed(const float* A, const float* B_packed, const float* bias,float* C, int M, int K, int N) {+#ifdef __AVX512F__+ /* AVX512 packs 16 columns per panel; the fused AVX2 kernel expects eight. */+ {+#else+ if (!bias || N % 8 != 0) {+#endif+ matmul_fp32_packed(A, B_packed, C, M, K, N);+ for (int m = 0; m < M; m++) {+ for (int n = 0; n < N; n++) {+ size_t i = (size_t)m * N + n;+ float v = C[i] + (bias ? bias[n] : 0.0f);+ C[i] = 0.5f * v * (1.0f + erff(v * 0.7071067811865476f));+ }+ }+ return;+ }#ifdef __AVX2__int NR = 8;int n_panels = (N + NR - 1) / NR;diff --git a/tests/packed_math.c b/tests/packed_math.cnew file mode 100644index 0000000..6e2fb72--- /dev/null+++ b/tests/packed_math.c@@ -0,0 +1,50 @@+/* Check the public packer and its consumers against direct products.+ * Non-multiple panel widths and short row tiles exercise both tails. */+#include <math.h>+#include <stdint.h>+#include <stdio.h>+#include <stdlib.h>++void pack_b_fp32(const float*, int, int, float*);+int packed_b_fp32_size(int, int);+void matmul_fp32_packed(const float*, const float*, float*, int, int, int);+void matmul_fp32_packed_bias(const float*, const float*, const float*, float*, int, int, int);+void matmul_bias_gelu_packed(const float*, const float*, const float*, float*, int, int, int);++int main(void) {+ const int widths[] = {1, 5, 8, 9, 15, 16, 17, 32, 33};+ const int inner[] = {1, 3, 8, 19};+ unsigned checked = 0, failed = 0;+ for (size_t i = 0; i < sizeof(widths)/sizeof(widths[0]); i++)+ for (size_t j = 0; j < sizeof(inner)/sizeof(inner[0]); j++)+ for (int m = 1; m <= 5; m++) {+ int k = inner[j], n = widths[i];+ float a[5*19], b[19*33], bias[33], c[5*33];+ size_t bytes = (size_t)packed_b_fp32_size(k,n) * sizeof(float);+ void* storage = malloc(bytes + 63);+ if (!storage) return 2;+ float* p = (float*)(((uintptr_t)storage + 63) & ~(uintptr_t)63);+ for (int t=0; t<m*k; t++) a[t]=(t%11-5)*0.125f;+ for (int t=0; t<k*n; t++) b[t]=(t%13-6)*0.25f;+ for (int t=0; t<n; t++) bias[t]=(t%5-2)*0.25f;+ pack_b_fp32(b,k,n,p);+ for (int op=0; op<3; op++) {+ if (op==0) matmul_fp32_packed(a,p,c,m,k,n);+ if (op==1) matmul_fp32_packed_bias(a,p,bias,c,m,k,n);+ if (op==2) matmul_bias_gelu_packed(a,p,bias,c,m,k,n);+ for (int r=0; r<m; r++) for (int col=0; col<n; col++) {+ float ref=op ? bias[col] : 0;+ for (int h=0; h<k; h++) ref+=a[r*k+h]*b[h*n+col];+ if (op==2) ref*=0.5f*(1.0f+erff(ref*0.70710678118f));+ float tol=op==2 ? 0.001f : 0.00001f;+ if (!(fabsf(c[r*n+col]-ref)<=tol)) {+ if (failed++==0) fprintf(stderr,"M=%d K=%d N=%d op=%d got=%g expected=%g\n",m,k,n,op,c[r*n+col],ref);+ }+ checked++;+ }+ }+ free(storage);+ }+ printf("%u packed outputs, %u mismatches\n",checked,failed);+ return failed != 0;+}