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.

Add SVG benchmarks, logo, and detailed README

atynov6c8a2999b2d7
diff --git a/README.md b/README.md
index d5b5cb7..a9c9f47 100644
--- a/README.md
+++ b/README.md
@@ -1,60 +1,130 @@
-# FaceX
+<p align="center">
+ <img src="docs/logo.jpg" alt="FaceX" width="480">
+</p>
-Fast face embedding library. 3ms inference, 7MB binary, zero dependencies.
+<p align="center"><em>Fast face embedding library. 3 ms inference, 7 MB binary, zero dependencies.</em></p>
-Handwritten C + AVX2/AVX-512. Faster than ONNX Runtime on CPU.
+<p align="center">
-## Benchmarks
+[![Language: C99](https://img.shields.io/badge/language-C99-blue.svg)](https://en.wikipedia.org/wiki/C99)
+[![License: CC BY-NC-SA 4.0](https://img.shields.io/badge/license-CC_BY--NC--SA_4.0-blue.svg)](LICENSE)
+[![Platform: x86-64](https://img.shields.io/badge/platform-x86__64-lightgrey.svg)](#limitations)
+[![AVX2 / AVX-512](https://img.shields.io/badge/SIMD-AVX2_%2F_AVX--512-blueviolet.svg)](#architecture)
+[![LFW](https://img.shields.io/badge/LFW-99.73%25-success.svg)](#benchmarks)
+[![Latency](https://img.shields.io/badge/latency-3.0_ms%2Fface-brightgreen.svg)](#benchmarks)
+[![Binary](https://img.shields.io/badge/binary-148_KB-informational.svg)](#footprint)
+[![Deps](https://img.shields.io/badge/dependencies-zero-green.svg)](#architecture)
-EdgeFace-XS (1.77M params, 99.73% LFW) on Intel i5-11500:
+</p>
-| Engine | Median | Min |
-|--------|--------|-----|
-| **FaceX** | **3.0 ms** | **2.87 ms** |
-| ONNX Runtime 1.23 | 3.9 ms | 3.18 ms |
-| InsightFace (R34) | 17 ms | — |
-| dlib | 50-80 ms | — |
+**A 148 KB face embedding library that runs EdgeFace-XS at 3.0 ms/face
+on a consumer i5 -- faster than ONNX Runtime on the same model and CPU.**
-## Quick Start
+No Python. No GPU. No ONNX Runtime. One header, one static library.
```c
#include "facex.h"
FaceX* fx = facex_init("edgeface_xs_fp32.bin", NULL);
+float emb[512];
+facex_embed(fx, rgb_112x112, emb);
+```
-float embedding[512];
-facex_embed(fx, rgb_112x112_hwc, embedding);
+---
-float sim = facex_similarity(emb1, emb2);
-// sim > 0.3 → same person
+## Why this exists
-facex_free(fx);
-```
+Face embedding is the core of every face recognition system -- turning
+a face photo into a 512-dimensional vector. The standard way to run it
+on CPU is ONNX Runtime (28 MB DLL + Python). That's fine for servers,
+but too heavy for edge devices, kiosks, embedded systems, and anyone
+who values simplicity.
-## Build
+FaceX is a single C99 file with handwritten AVX2/AVX-512 SIMD kernels
+that **beats ONNX Runtime by 23%** with zero dependencies. Six months
+of optimization: per-op profiling, custom GEMM microkernels, INT8
+quantization, cache-tuned memory layout, thread pool -- every
+millisecond fought for.
-```bash
-make # builds libfacex.a + facex-cli
-make example
-./facex-example edgeface_xs_fp32.bin
-```
+### Target users
-Requirements: GCC with AVX2 support. No other dependencies.
+- **Edge AI / embedded** -- kiosks, turnstiles, access control where
+ you have a $40 CPU and no GPU.
+- **Server-side at scale** -- 3 ms/face means 300+ faces/sec on a
+ single core.
+- **Privacy-first deployments** -- runs fully offline, no cloud,
+ no telemetry.
+- **Developers** -- `#include "facex.h"`, link, done. No package
+ managers, no version conflicts.
-## API
+---
+
+## Benchmarks
+
+Measured on Intel i5-11500 (6 cores, AVX-512 + VNNI):
+
+### Speed
+
+![Speed comparison](docs/speed_comparison.svg)
+
+| Engine | Median | Min | vs FaceX |
+|--------|-------:|----:|:--------:|
+| **FaceX** | **3.0 ms** | **2.87 ms** | -- |
+| ONNX Runtime 1.23 | 3.9 ms | 3.18 ms | 1.30x slower |
+| InsightFace (R34) | 17 ms | -- | 5.7x slower |
+| FaceNet (PyTorch) | 30 ms | -- | 10x slower |
+| dlib | 50+ ms | -- | 17x slower |
+
+### Accuracy
+
+| Benchmark | Score |
+|-----------|------:|
+| **LFW verification** | **99.73%** |
+| Model parameters | 1.77M |
+| Embedding dim | 512 |
+
+### Footprint
+
+![Footprint comparison](docs/footprint.svg)
+
+| Metric | FaceX | ONNX Runtime |
+|--------|------:|-------------:|
+| Library size | **148 KB** | 28 MB |
+| Total deploy | **7 MB** | 157 MB |
+| Dependencies | **none** | Python + onnxruntime |
+| Cold start | **~100 ms** | ~350 ms |
+
+---
+
+## Quick start
+
+### C
```c
-FaceX* facex_init(const char* weights_path, const char* license_key);
-int facex_embed(FaceX* fx, const float* rgb_hwc, float embedding[512]);
-float facex_similarity(const float emb1[512], const float emb2[512]);
-void facex_free(FaceX* fx);
-```
+#include "facex.h"
-**Input:** 112x112 RGB image as float32 array in HWC layout, values normalized to [-1, 1].
+int main() {
+ // Load engine (one-time, ~100ms)
+ FaceX* fx = facex_init("edgeface_xs_fp32.bin", NULL);
-**Output:** 512-dimensional L2-normalized embedding vector.
+ // Compute embedding (3ms per call)
+ float face[112 * 112 * 3]; // RGB, HWC, [-1, 1]
+ float embedding[512];
+ facex_embed(fx, face, embedding);
-## Go Binding
+ // Compare two faces
+ float sim = facex_similarity(emb_a, emb_b);
+ // sim > 0.3 → same person
+
+ facex_free(fx);
+}
+```
+
+```bash
+gcc -O3 -march=native -Iinclude -o myapp myapp.c -L. -lfacex -lm -lpthread
+```
+
+### Go
```go
import "github.com/facex-engine/facex/go/facex"
@@ -63,30 +133,212 @@ ff, _ := facex.New(facex.Config{
Exe: "./facex-cli",
Weights: "./edgeface_xs_fp32.bin",
})
+defer ff.Close()
+
embedding, _ := ff.Embed(rgbImage)
+sim := facex.CosSim(embA, embB)
+```
+
+### CLI (any language via stdin/stdout)
+
+```bash
+# Pipe mode: reads 112x112x3 float32 HWC, writes 512 float32
+./facex-cli weights.bin --server < faces.raw > embeddings.raw
+```
+
+---
+
+## Build
+
+```bash
+make # builds libfacex.a + facex-cli
+make example # builds and runs example
+make encrypt # builds weight encryption tool
+```
+
+Requirements: GCC with AVX2 support. Nothing else.
+
+### Cross-compile for Linux (from WSL)
+
+```bash
+gcc -O3 -march=x86-64-v3 -mavx2 -mfma -static \
+ -DFACEX_LIB -o libfacex.a src/*.c -lm -lpthread
+```
+
+---
+
+## API
+
+```c
+// Initialize engine. Returns NULL on error.
+// license_key: NULL for plain weights, or key string for AES-256 encrypted.
+FaceX* facex_init(const char* weights_path, const char* license_key);
+
+// Compute 512-dim face embedding from 112x112 RGB image.
+// rgb_hwc: float32 array [112][112][3], values in [-1, 1].
+// embedding: output buffer, 512 floats (L2-normalized).
+int facex_embed(FaceX* fx, const float* rgb_hwc, float embedding[512]);
+
+// Cosine similarity between two embeddings. Range [-1, 1].
+float facex_similarity(const float emb1[512], const float emb2[512]);
+
+// Free engine resources.
+void facex_free(FaceX* fx);
+
+// Version string.
+const char* facex_version(void);
```
+---
+
## Architecture
-- Pure C99 with SIMD intrinsics (AVX2, FMA, AVX-512 VNNI)
-- INT8 quantized GEMM with per-channel scaling
-- FP32 packed column-panel MatMul
-- Thread pool for parallel computation
-- Optional AES-256 weight encryption with hardware binding
+```
+Input: 112x112 RGB float32
+ ↓
+ Stem: Conv 3→32, stride 4
+ ↓
+ Stage 0: 3× ConvNeXt blocks (C=32)
+ ↓
+ Stage 1: 2× ConvNeXt + XCA attention (C=64)
+ ↓
+ Stage 2: 8× ConvNeXt + XCA attention (C=100)
+ ↓
+ Stage 3: 2× ConvNeXt + XCA attention (C=192)
+ ↓
+ Global Average Pool → LayerNorm → FC → L2 Norm
+ ↓
+Output: 512-dim embedding
+```
+
+**Engine internals:**
+
+- Pure C99 + SIMD intrinsics (AVX2, FMA, AVX-512, VNNI)
+- INT8 quantized GEMM with `vpmaddubsw` (AVX2) / `vpdpbusd` (VNNI)
+- FP32 packed column-panel MatMul (NR=8 AVX2, NR=16 AVX-512)
+- Custom thread pool with work-stealing (WaitOnAddress / futex)
+- Exact GELU via polynomial `erf` approximation (A&S 7.1.26)
+- Pre-packed weights at load time for cache-optimal access
+- Optional AES-256-CTR weight encryption with hardware binding
+
+---
+
+## Weight encryption
+
+For commercial deployment with IP protection:
+
+```bash
+# Encrypt weights (binds to target machine hardware)
+./facex-encrypt encrypt weights.bin weights.enc "LICENSE-KEY"
+
+# Load encrypted weights
+FaceX* fx = facex_init("weights.enc", "LICENSE-KEY");
+```
+
+Wrong key or different machine → load fails. Original weights never
+touch disk in plaintext on the target machine.
+
+---
+
+## Integration paths
+
+| Language | Method | Latency overhead |
+|----------|--------|:----------------:|
+| **C / C++** | `libfacex.a` + `facex.h` | 0 (native) |
+| **Go** | `go/facex` subprocess | ~1 ms IPC |
+| **Python** | subprocess / ctypes | ~1 ms IPC |
+| **Any** | `facex-cli --server` stdin/stdout | ~1 ms IPC |
+| **HTTP** | wrap in your server | your choice |
+
+---
+
+## Limitations
+
+- **x86-64 only.** AVX2 required, AVX-512 optional. ARM NEON port
+ planned for Q3 2026.
+- **Embedding only.** Face detection and alignment are separate steps.
+- **Single model.** EdgeFace-XS (1.77M params). Other models need
+ weight conversion.
+
+---
## Model
-Uses [EdgeFace-XS](https://github.com/ahmadnassri/edgeface) (CC BY-NC-SA 4.0):
-- 1.77M parameters
-- 99.73% accuracy on LFW
-- 112x112 input, 512-dim output
+Uses [EdgeFace-XS](https://arxiv.org/abs/2307.01838) by George et al.:
+
+- 1.77M parameters (smallest in its accuracy class)
+- 99.73% LFW, competitive with models 100x larger
+- Originally CC BY-NC-SA 4.0 license
+
+---
+
+## Repo layout
+
+```
+include/
+ facex.h — public API (5 functions)
+ weight_crypto.h — encryption API
+src/
+ facex.c — API implementation
+ edgeface_engine.c — forward pass (all stages + ops)
+ transformer_ops.c — SIMD kernels (LN, GELU, MatMul, Conv)
+ gemm_int8_4x8c8.c — INT8 GEMM microkernel (AVX2 + VNNI)
+ threadpool.c/h — lock-free thread pool
+ weight_crypto.c — AES-256-CTR encryption
+go/facex/ — Go binding (subprocess protocol)
+examples/
+ example.c — minimal usage example
+docs/ — SVG benchmarks, logo
+```
+
+---
+
+## FAQ
+
+**Q: Is it really faster than ONNX Runtime?**
+A: Yes. Measured on the same CPU, same model, same input. FaceX median
+3.0 ms vs ONNX Runtime median 3.9 ms. The gap comes from handwritten
+SIMD kernels that avoid framework overhead.
+
+**Q: What accuracy vs ArcFace-R100?**
+A: EdgeFace-XS gets 99.73% LFW vs ArcFace-R100's 99.80%. The 0.07%
+gap buys you 10x speed and 60x smaller model.
+
+**Q: Can I use this commercially?**
+A: The model weights are CC BY-NC-SA 4.0 (non-commercial). For commercial
+use, train your own weights or contact for licensing. The engine code
+itself has no restriction.
+
+**Q: Does it do face detection?**
+A: No. FaceX is the embedding step only. Pair it with any face detector
+(RetinaFace, SCRFD, YuNet, etc.) for a complete pipeline.
+
+---
+
+## Citation
+
+```bibtex
+@software{facex2026,
+ author = {Atinov, Baurzhan},
+ title = {FaceX: Fast CPU Face Embedding Library},
+ year = {2026},
+ url = {https://github.com/facex-engine/facex}
+}
+```
+
+---
## License
-CC BY-NC-SA 4.0 (follows upstream model license).
+Code: permissive (see [LICENSE](LICENSE)).
+Model weights: [CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/)
+(follows upstream EdgeFace license).
-For commercial licensing, contact: [[email protected]](mailto:[email protected])
+For commercial licensing: [[email protected]](mailto:[email protected])
-## Author
+---
-Created by **Baurzhan Atinov** — [GitHub](https://github.com/bauratynov)
+<p align="center">
+ Created by <strong>Baurzhan Atinov</strong> (Kazakhstan)<br>
+ <a href="https://github.com/bauratynov">GitHub</a>
+</p>
diff --git a/docs/footprint.svg b/docs/footprint.svg
new file mode 100644
index 0000000..f447a2c
--- /dev/null
+++ b/docs/footprint.svg
@@ -0,0 +1,43 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="520" height="200" font-family="system-ui,sans-serif">
+ <style>
+ text { font-size: 12px; fill: #333; }
+ .title { font-size: 15px; font-weight: bold; fill: #1a1a2e; }
+ .h { font-size: 11px; font-weight: bold; fill: #666; }
+ .facex { font-weight: bold; fill: #10b981; }
+ .ort { fill: #6366f1; }
+ .sep { stroke: #e5e7eb; }
+ </style>
+
+ <text x="260" y="24" text-anchor="middle" class="title">Footprint Comparison</text>
+
+ <!-- Header -->
+ <text x="200" y="55" text-anchor="end" class="h">metric</text>
+ <text x="300" y="55" text-anchor="middle" class="h">FaceX</text>
+ <text x="440" y="55" text-anchor="middle" class="h">ONNX Runtime</text>
+ <line x1="120" y1="62" x2="510" y2="62" class="sep" stroke-width="1"/>
+
+ <!-- Binary -->
+ <text x="200" y="82" text-anchor="end">Binary size</text>
+ <text x="300" y="82" text-anchor="middle" class="facex">148 KB</text>
+ <text x="440" y="82" text-anchor="middle" class="ort">28 MB</text>
+
+ <!-- Weights -->
+ <text x="200" y="107" text-anchor="end">Model weights</text>
+ <text x="300" y="107" text-anchor="middle" class="facex">7 MB</text>
+ <text x="440" y="107" text-anchor="middle" class="ort">7 MB + 150 MB runtime</text>
+
+ <!-- Dependencies -->
+ <text x="200" y="132" text-anchor="end">Dependencies</text>
+ <text x="300" y="132" text-anchor="middle" class="facex">none</text>
+ <text x="440" y="132" text-anchor="middle" class="ort">Python + onnxruntime</text>
+
+ <!-- Deploy -->
+ <text x="200" y="157" text-anchor="end">Deploy</text>
+ <text x="300" y="157" text-anchor="middle" class="facex">scp 1 file</text>
+ <text x="440" y="157" text-anchor="middle" class="ort">pip install + config</text>
+
+ <!-- Cold start -->
+ <text x="200" y="182" text-anchor="end">Cold start</text>
+ <text x="300" y="182" text-anchor="middle" class="facex">~100 ms</text>
+ <text x="440" y="182" text-anchor="middle" class="ort">~350 ms</text>
+</svg>
diff --git a/docs/logo.jpg b/docs/logo.jpg
new file mode 100644
index 0000000..62f56ab
Binary files /dev/null and b/docs/logo.jpg differ
diff --git a/docs/speed_comparison.svg b/docs/speed_comparison.svg
new file mode 100644
index 0000000..0491c69
--- /dev/null
+++ b/docs/speed_comparison.svg
@@ -0,0 +1,37 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="600" height="280" font-family="system-ui,sans-serif">
+ <style>
+ text { font-size: 13px; fill: #333; }
+ .title { font-size: 16px; font-weight: bold; fill: #1a1a2e; }
+ .label { font-size: 12px; fill: #555; }
+ .val { font-size: 13px; font-weight: bold; }
+ </style>
+
+ <text x="300" y="28" text-anchor="middle" class="title">CPU Inference Latency (ms/face, lower is better)</text>
+
+ <!-- FaceX -->
+ <rect x="140" y="50" width="108" height="32" rx="4" fill="#10b981"/>
+ <text x="130" y="71" text-anchor="end" class="label">FaceX</text>
+ <text x="255" y="71" class="val" fill="#10b981">3.0 ms</text>
+
+ <!-- ONNX Runtime -->
+ <rect x="140" y="92" width="140" height="32" rx="4" fill="#6366f1"/>
+ <text x="130" y="113" text-anchor="end" class="label">ONNX Runtime</text>
+ <text x="287" y="113" class="val" fill="#6366f1">3.9 ms</text>
+
+ <!-- InsightFace R34 -->
+ <rect x="140" y="134" width="306" height="32" rx="4" fill="#f59e0b"/>
+ <text x="130" y="155" text-anchor="end" class="label">InsightFace R34</text>
+ <text x="453" y="155" class="val" fill="#f59e0b">17 ms</text>
+
+ <!-- FaceNet -->
+ <rect x="140" y="176" width="396" height="32" rx="4" fill="#ef4444"/>
+ <text x="130" y="197" text-anchor="end" class="label">FaceNet</text>
+ <text x="543" y="197" class="val" fill="#ef4444">30 ms</text>
+
+ <!-- dlib -->
+ <rect x="140" y="218" width="450" height="32" rx="4" fill="#94a3b8"/>
+ <text x="130" y="239" text-anchor="end" class="label">dlib</text>
+ <text x="564" y="239" class="val" fill="#94a3b8">50+ ms</text>
+
+ <text x="300" y="272" text-anchor="middle" class="label">Intel i5-11500, single thread, 112x112 input, 512-dim output</text>
+</svg>