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.

fix: large images

atynovcd6df63e5d75
diff --git a/src/detect.c b/src/detect.c
index fe540c3..f90de8c 100644
--- a/src/detect.c
+++ b/src/detect.c
@@ -73,6 +73,7 @@ Detect* detect_init(const char* weights_path) {
det->tensors[i] = (float*)(det->raw + off); off += sz;
}
+ /* Initial work buffer — will be grown in detect_run if needed */
det->work_size = 4 * 1024 * 1024;
det->work = (float*)calloc(det->work_size, sizeof(float));
if (!det->work) { free(det->raw); free(det); return NULL; }
@@ -237,6 +238,16 @@ int detect_run(Detect* det, const uint8_t* rgb_hwc, int width, int height,
int H = height, W = width;
int h2=H/2, w2=W/2, h4=h2/2, w4=w2/2, h8=h4/2, w8=w4/2, h16=h8/2, w16=w8/2, h32=h16/2, w32=w16/2;
+ /* Calculate required workspace */
+ size_t need = (size_t)3*H*W + 3*(size_t)64*h2*w2 + (size_t)64*h8*w8
+ + (size_t)64*h16*w16 + (size_t)64*h32*w32 + (size_t)16*h8*w8 + 4096;
+ if (need > det->work_size) {
+ free(det->work);
+ det->work_size = need;
+ det->work = (float*)calloc(det->work_size, sizeof(float));
+ if (!det->work) return -1;
+ }
+
/* Workspace layout: dedicated buffers */
float* wp = det->work;
float* inp = wp; wp += 3*H*W;
diff --git a/src/edgeface_engine.c b/src/edgeface_engine.c
index 1b19781..e09b6a0 100644
--- a/src/edgeface_engine.c
+++ b/src/edgeface_engine.c
@@ -1062,9 +1062,13 @@ static int engine_init(const char* weights_path, Weights* weights) {
}
int pw_size = packed_weights_size_4x8c8(K, N);
- void* packed = malloc(pw_size);
- int32_t* col_sums = (int32_t*)calloc(((N+7)&~7), sizeof(int32_t));
- pack_weights_4x8c8(w_int8, NULL, K, N, packed, col_sums);
+ void* packed = NULL;
+ int32_t* col_sums = NULL;
+ if (pw_size > 0) {
+ packed = malloc(pw_size);
+ col_sums = (int32_t*)calloc(((N+7)&~7), sizeof(int32_t));
+ pack_weights_4x8c8(w_int8, NULL, K, N, packed, col_sums);
+ }
weights->mm[idx].packed = packed;
weights->mm[idx].col_sums = col_sums;
diff --git a/src/gemm_stub.c b/src/gemm_stub.c
index 9caff0b..631515f 100644
--- a/src/gemm_stub.c
+++ b/src/gemm_stub.c
@@ -14,7 +14,7 @@ void pack_weights_4x8c8(const int8_t* w, const float* scales, int K, int N,
int packed_weights_size_4x8c8(int K, int N) {
(void)K; (void)N;
- return 64; /* minimum allocation */
+ return 0; /* return 0 so engine skips INT8 path and uses FP32 */
}
void int8_gemm_4x8c8(int M, int N, int K,
diff --git a/src/transformer_ops.c b/src/transformer_ops.c
index 3ead9be..007aaa5 100644
--- a/src/transformer_ops.c
+++ b/src/transformer_ops.c
@@ -534,11 +534,13 @@ 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) {
-#ifdef __AVX512F__
+#if defined(__AVX512F__)
int NR = 16;
-#else
+#elif defined(__AVX2__)
int NR = 8;
#endif
+
+#if defined(__AVX2__) || defined(__AVX512F__)
int n_panels = (N + NR - 1) / NR;
for (int p = 0; p < n_panels; p++) {
int n_start = p * NR;
@@ -551,13 +553,19 @@ void pack_b_fp32(const float* B, int K, int N, float* packed) {
for (; j < NR; j++) dst[k * NR + j] = 0; /* zero-pad last panel */
}
}
+#else
+ /* Scalar: just copy — matmul_fp32 expects standard [K,N] layout */
+ memcpy(packed, B, (size_t)K * N * sizeof(float));
+#endif
}
int packed_b_fp32_size(int K, int N) {
#ifdef __AVX512F__
return ((N + 15) / 16) * K * 16;
-#else
+#elif defined(__AVX2__)
return ((N + 7) / 8) * K * 8;
+#else
+ return K * N;
#endif
}