#[path = "arcface/mod.rs"]
mod common;
use std::time::Instant;
use coremlit::{
ComputeUnits,
embeddings::face::{AlignedFace, FaceEmbedder, FaceEmbedderOptions, arcface},
};
const ARMS: [ComputeUnits; 4] = [
ComputeUnits::All,
ComputeUnits::CpuAndGpu,
ComputeUnits::CpuOnly,
ComputeUnits::CpuAndNeuralEngine,
];
struct Arm {
compute: ComputeUnits,
cold_load_ms: f64,
corpus_ms: f64,
worst_cos: f64,
worst_id: String,
}
impl Arm {
fn faces_per_second(&self, faces: usize) -> f64 {
#[expect(clippy::cast_precision_loss, reason = "18 faces is exact in f64")]
let n = faces as f64;
n * 1000.0 / self.corpus_ms
}
}
fn measure(compute: ComputeUnits, reference: &common::Reference, faces: &[AlignedFace]) -> Arm {
let started = Instant::now();
let embedder = FaceEmbedder::load(
common::model_path(),
arcface::MODEL,
FaceEmbedderOptions::new().with_compute(compute),
)
.unwrap_or_else(|e| panic!("load under {compute:?}: {e}"));
let cold_load_ms = started.elapsed().as_secs_f64() * 1000.0;
let _warm = embedder
.embed(faces)
.unwrap_or_else(|e| panic!("warm-up under {compute:?}: {e}"));
let started = Instant::now();
let embeddings = embedder
.embed(faces)
.unwrap_or_else(|e| panic!("embed under {compute:?}: {e}"));
let corpus_ms = started.elapsed().as_secs_f64() * 1000.0;
let mut worst = (1.0f64, String::new());
for (face, embedding) in reference.faces.iter().zip(&embeddings) {
assert!(
embedding.as_slice().iter().all(|v| v.is_finite()),
"{compute:?} produced a non-finite component for {}. A NaN here is not a precision \
finding — it is a vector whose cosine against anything is NaN, which every downstream \
threshold silently fails open on.",
face.id
);
let cos = common::cosine(embedding.as_slice(), &face.reference);
if cos < worst.0 {
worst = (cos, face.id.clone());
}
}
Arm {
compute,
cold_load_ms,
corpus_ms,
worst_cos: worst.0,
worst_id: worst.1,
}
}
#[test]
#[ignore = "requires the staged arcface model (FACEKIT_TEST_MODELS)"]
fn every_placement_is_finite_and_within_parity() {
let reference = common::load_reference();
let faces: Vec<AlignedFace> = reference
.faces
.iter()
.map(|face| {
AlignedFace::from_template_pixels(&face.crop)
.unwrap_or_else(|e| panic!("{}: wrap crop: {e}", face.id))
})
.collect();
let measured: Vec<Arm> = ARMS
.iter()
.map(|compute| measure(*compute, &reference, &faces))
.collect();
eprintln!("[arcface] placement over {} faces", faces.len());
eprintln!(
"[arcface] {:<20} {:>12} {:>12} {:>10} {:>12} worst face",
"arm", "cold load ms", "corpus ms", "faces/s", "min cos"
);
for arm in &measured {
eprintln!(
"[arcface] {:<20} {:>12.1} {:>12.2} {:>10.0} {:>12.7} {}",
format!("{:?}", arm.compute),
arm.cold_load_ms,
arm.corpus_ms,
arm.faces_per_second(faces.len()),
arm.worst_cos,
arm.worst_id
);
}
for arm in &measured {
assert!(
arm.worst_cos >= common::SANITY_COS,
"{:?}: worst cos {:.8} on {} is under the parity floor {}. Every arm must reproduce the \
ONNX — an arm that does not is not a slower arm, it is a different function.",
arm.compute,
arm.worst_cos,
arm.worst_id,
common::SANITY_COS
);
}
let recommended = measured
.iter()
.find(|arm| arm.compute == arcface::RECOMMENDED_COMPUTE)
.expect("the recommended arm must be one of the four measured here");
eprintln!(
"[arcface] recommended {:?}: worst cos {:.8}, 1-cos {:.3e}, {:.0} faces/s",
recommended.compute,
recommended.worst_cos,
1.0 - recommended.worst_cos,
recommended.faces_per_second(faces.len())
);
assert!(
recommended.worst_cos >= common::SANITY_COS,
"the RECOMMENDED arm {:?} is under the parity floor: {:.8} on {}. Whatever else changes \
about a host, the arm this crate tells callers to use has to reproduce the model.",
recommended.compute,
recommended.worst_cos,
recommended.worst_id
);
}