Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Tighten ML implementation visibility

laurenspriemd0c2c2cfbb37
diff --git a/rust/crates/photos/src/ml/mod.rs b/rust/crates/photos/src/ml/mod.rs
index 3fbfbc9f8f..eaf9fa9654 100644
--- a/rust/crates/photos/src/ml/mod.rs
+++ b/rust/crates/photos/src/ml/mod.rs
@@ -7,12 +7,11 @@ pub mod indexing;
mod models;
pub use models::ModelPaths;
mod onnx;
-pub use onnx::golden_test;
-pub use onnx::golden_test::tooling as golden_tooling;
+pub use onnx::golden_tooling;
mod pet;
mod postprocess;
mod preprocess;
-pub mod runtime;
+mod runtime;
pub mod types;
#[cfg(feature = "usearch")]
pub mod vector_db;
diff --git a/rust/crates/photos/src/ml/onnx.rs b/rust/crates/photos/src/ml/onnx.rs
index bb64caeaf6..32db07a588 100644
--- a/rust/crates/photos/src/ml/onnx.rs
+++ b/rust/crates/photos/src/ml/onnx.rs
@@ -4,7 +4,8 @@ use std::{fmt, path::Path};
use crate::ml::error::{MlError, MlResult};
mod coreml_cache;
-pub mod golden_test;
+mod golden_test;
+pub use golden_test::tooling as golden_tooling;
mod providers;
mod tensor;
mod webgpu;
@@ -15,9 +16,10 @@ pub(crate) fn set_webgpu_enabled(enabled: bool) {
pub(crate) use ort::session::Session as SessionHandle;
pub(crate) use providers::ExecutionMode;
+use tensor::run_golden_tensor;
pub(crate) use tensor::{
- BorrowedFloatTensor, FloatTensorData, PreparedF32Input, run_f32, run_golden_tensor,
- run_i32_f32, with_prepared_float_output,
+ BorrowedFloatTensor, FloatTensorData, PreparedF32Input, run_f32, run_i32_f32,
+ with_prepared_float_output,
};
use providers::{ExecutionProvider, ProviderPlan};
@@ -252,7 +254,7 @@ fn build_next_session(
)))
}
-pub(crate) fn build_cpu_session(model_path: &str) -> MlResult<Session> {
+fn build_cpu_session(model_path: &str) -> MlResult<Session> {
let mut plan = ProviderPlan::new(ExecutionMode::CpuOnly, model_path);
build_next_session(model_path, &mut plan, "golden-tooling")
}
diff --git a/rust/crates/photos/src/ml/onnx/golden_test/data.rs b/rust/crates/photos/src/ml/onnx/golden_test/data.rs
index e2e4cf6951..269dda66b0 100644
--- a/rust/crates/photos/src/ml/onnx/golden_test/data.rs
+++ b/rust/crates/photos/src/ml/onnx/golden_test/data.rs
@@ -8,7 +8,7 @@
use super::{GoldenEntry, GoldenInput, GoldenMetric};
-pub(crate) static GOLDEN_ENTRIES: &[GoldenEntry] = &[
+pub(super) static GOLDEN_ENTRIES: &[GoldenEntry] = &[
GoldenEntry {
model_file: "yolov5s_face_640_640_static_b1.onnx",
model_sha256: "e047647409403d52696035ecd445792173e50d7fbdcccac97b958a585db9aa3d",
diff --git a/rust/crates/photos/src/ml/onnx/golden_test/mod.rs b/rust/crates/photos/src/ml/onnx/golden_test/mod.rs
index 2a764c83c9..e36597c7ba 100644
--- a/rust/crates/photos/src/ml/onnx/golden_test/mod.rs
+++ b/rust/crates/photos/src/ml/onnx/golden_test/mod.rs
@@ -12,18 +12,18 @@ use data::GOLDEN_ENTRIES;
// deliberately loose pending accelerated-hardware measurements. Healthy fp16
// cosine and coordinate divergence is around 1e-3; corrupt output is around
// 1.0 or higher.
-pub(crate) const COSINE_DISTANCE_THRESHOLD: f64 = 0.025;
+const COSINE_DISTANCE_THRESHOLD: f64 = 0.025;
-pub(crate) const RELATIVE_L2_THRESHOLD: f64 = 0.1;
+const RELATIVE_L2_THRESHOLD: f64 = 0.1;
// Background confidence samples are <= ~2e-3, making healthy fp16 relative
// error O(0.1..1); full flush-to-zero is 1.0, while inflated confidences are
// >= ~45.
-pub(crate) const CONFIDENCE_RELATIVE_L2_THRESHOLD: f64 = 2.0;
+const CONFIDENCE_RELATIVE_L2_THRESHOLD: f64 = 2.0;
const MAX_REFERENCE_SAMPLES: usize = 4096;
-pub enum GoldenInput {
+enum GoldenInput {
SeededF32 { seed: u64 },
I32 { data: &'static [i32] },
}
@@ -42,14 +42,14 @@ pub enum GoldenMetric {
}
impl GoldenMetric {
- pub fn label(self) -> &'static str {
+ pub(super) fn label(self) -> &'static str {
match self {
Self::CosineDistance => "cosine distance",
Self::DetectorRelativeL2 { .. } => "detector channel relative L2 error",
}
}
- pub fn threshold(self) -> f64 {
+ fn threshold(self) -> f64 {
match self {
Self::CosineDistance => COSINE_DISTANCE_THRESHOLD,
Self::DetectorRelativeL2 { .. } => CONFIDENCE_RELATIVE_L2_THRESHOLD,
@@ -57,20 +57,20 @@ impl GoldenMetric {
}
}
-pub struct GoldenEntry {
+pub(super) struct GoldenEntry {
// A changed canonical name must fail closed instead of reusing a stale
// golden.
- pub model_file: &'static str,
+ model_file: &'static str,
// Checked against the asset lock in CI, not on device.
- pub model_sha256: &'static str,
- pub input: GoldenInput,
- pub input_shape: &'static [i64],
- pub metric: GoldenMetric,
- pub output_len: usize,
- pub expected_sample: &'static [f32],
+ model_sha256: &'static str,
+ input: GoldenInput,
+ pub(super) input_shape: &'static [i64],
+ pub(super) metric: GoldenMetric,
+ output_len: usize,
+ expected_sample: &'static [f32],
}
-pub fn lookup(model_path: &str) -> Option<&'static GoldenEntry> {
+pub(super) fn lookup(model_path: &str) -> Option<&'static GoldenEntry> {
let file_name = std::path::Path::new(model_path).file_name()?.to_str()?;
GOLDEN_ENTRIES
.iter()
@@ -104,13 +104,13 @@ fn sanitize_file_name(value: &str) -> String {
.collect()
}
-pub enum PreparedGoldenInput {
+pub(super) enum PreparedGoldenInput {
F32(Vec<f32>),
I32(Vec<i32>),
}
impl PreparedGoldenInput {
- pub fn zeroed(&self) -> Self {
+ pub(super) fn zeroed(&self) -> Self {
match self {
Self::F32(data) => Self::F32(vec![0.0; data.len()]),
Self::I32(data) => Self::I32(vec![0; data.len()]),
@@ -118,7 +118,7 @@ impl PreparedGoldenInput {
}
}
-pub fn prepare_input(entry: &GoldenEntry) -> Result<PreparedGoldenInput, String> {
+pub(super) fn prepare_input(entry: &GoldenEntry) -> Result<PreparedGoldenInput, String> {
let element_count: i64 = entry.input_shape.iter().product();
let element_count = usize::try_from(element_count)
.map_err(|_| format!("invalid golden input shape {:?}", entry.input_shape))?;
@@ -141,7 +141,7 @@ pub fn prepare_input(entry: &GoldenEntry) -> Result<PreparedGoldenInput, String>
// Shared by the device self-test and generator. Regenerate the golden data if
// this sequence changes.
-pub fn seeded_noise(seed: u64, len: usize) -> Vec<f32> {
+fn seeded_noise(seed: u64, len: usize) -> Vec<f32> {
let mut state = seed.max(1);
(0..len)
.map(|_| {
@@ -155,7 +155,7 @@ pub fn seeded_noise(seed: u64, len: usize) -> Vec<f32> {
.collect()
}
-pub fn sample_output(output: &[f32]) -> Vec<f32> {
+fn sample_output(output: &[f32]) -> Vec<f32> {
let stride = sample_stride(output.len());
output.iter().copied().step_by(stride).collect()
}
@@ -164,7 +164,7 @@ fn sample_stride(output_len: usize) -> usize {
output_len.div_ceil(MAX_REFERENCE_SAMPLES).max(1)
}
-pub fn compare_output(entry: &GoldenEntry, output: &[f32]) -> Result<f64, String> {
+pub(super) fn compare_output(entry: &GoldenEntry, output: &[f32]) -> Result<f64, String> {
let distances = measure_output(entry, output)?;
match distances {
OutputDistances::Cosine(distance) => ensure_within_threshold(
@@ -191,7 +191,7 @@ pub fn compare_output(entry: &GoldenEntry, output: &[f32]) -> Result<f64, String
}
}
-pub fn output_distance(entry: &GoldenEntry, output: &[f32]) -> Result<f64, String> {
+fn output_distance(entry: &GoldenEntry, output: &[f32]) -> Result<f64, String> {
Ok(match measure_output(entry, output)? {
OutputDistances::Cosine(distance) => distance,
OutputDistances::Detector {
@@ -203,7 +203,7 @@ pub fn output_distance(entry: &GoldenEntry, output: &[f32]) -> Result<f64, Strin
// The zero-input warm-up has no numeric golden, but must return a complete
// finite tensor before the golden run.
-pub fn validate_output(entry: &GoldenEntry, output: &[f32]) -> Result<(), String> {
+pub(super) fn validate_output(entry: &GoldenEntry, output: &[f32]) -> Result<(), String> {
if output.len() != entry.output_len {
return Err(format!(
"output length {} does not match golden length {}",
diff --git a/rust/crates/photos/src/ml/onnx/tensor.rs b/rust/crates/photos/src/ml/onnx/tensor.rs
index 044e6d502c..08e2f54394 100644
--- a/rust/crates/photos/src/ml/onnx/tensor.rs
+++ b/rust/crates/photos/src/ml/onnx/tensor.rs
@@ -62,7 +62,7 @@ impl PreparedF32Input {
}
}
-pub(crate) fn run_golden_tensor(
+pub(super) fn run_golden_tensor(
session: &mut Session,
input_shape: &[i64],
input: &golden_test::PreparedGoldenInput,
diff --git a/rust/crates/photos/src/ml/onnx/webgpu.rs b/rust/crates/photos/src/ml/onnx/webgpu.rs
index 70593346cb..9e349e4fdf 100644
--- a/rust/crates/photos/src/ml/onnx/webgpu.rs
+++ b/rust/crates/photos/src/ml/onnx/webgpu.rs
@@ -44,7 +44,7 @@ const MAX_CONSECUTIVE_FAILURES: u32 = 3;
#[cfg(any(target_os = "android", target_os = "linux", target_os = "windows"))]
static WEBGPU_ENABLED: AtomicBool = AtomicBool::new(false);
-pub(crate) fn set_enabled(enabled: bool) {
+pub(super) fn set_enabled(enabled: bool) {
#[cfg(any(target_os = "android", target_os = "linux", target_os = "windows"))]
WEBGPU_ENABLED.store(enabled, Ordering::Relaxed);
#[cfg(not(any(target_os = "android", target_os = "linux", target_os = "windows")))]
@@ -53,7 +53,7 @@ pub(crate) fn set_enabled(enabled: bool) {
// Adapter probing touches Vulkan and therefore happens after arming, not here.
#[cfg(any(target_os = "android", target_os = "linux", target_os = "windows"))]
-pub(crate) fn attempt_permitted(model_path: &str) -> bool {
+pub(super) fn attempt_permitted(model_path: &str) -> bool {
WEBGPU_ENABLED.load(Ordering::Relaxed)
&& model_dir(model_path).is_some_and(|dir| !quarantined(&dir))
}
@@ -78,7 +78,7 @@ fn vendors_allowlisted(vendor_ids: &[u32]) -> bool {
#[cfg(target_os = "android")]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
-pub(crate) enum AdapterCheck {
+pub(super) enum AdapterCheck {
Allowed,
Denied,
// Probe failures count toward quarantine; policy denials do not.
@@ -88,7 +88,7 @@ pub(crate) enum AdapterCheck {
// Must run inside an armed canary because this is the first Vulkan driver
// access.
#[cfg(target_os = "android")]
-pub(crate) fn check_adapter() -> AdapterCheck {
+pub(super) fn check_adapter() -> AdapterCheck {
static VERDICT: OnceLock<AdapterCheck> = OnceLock::new();
*VERDICT.get_or_init(|| match probe_vulkan_vendor_ids() {
Ok(vendor_ids) => {
@@ -142,7 +142,7 @@ fn probe_vulkan_vendor_ids() -> Result<Vec<u32>, String> {
target_os = "windows",
test
))]
-pub(crate) struct ArmedCanary {
+pub(super) struct ArmedCanary {
path: PathBuf,
}
@@ -152,7 +152,7 @@ pub(crate) struct ArmedCanary {
target_os = "windows",
test
))]
-pub(crate) fn arm_canary(model_path: &str, model_namespace: &str) -> io::Result<ArmedCanary> {
+pub(super) fn arm_canary(model_path: &str, model_namespace: &str) -> io::Result<ArmedCanary> {
let dir = model_dir(model_path).ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidInput,
@@ -172,7 +172,7 @@ pub(crate) fn arm_canary(model_path: &str, model_namespace: &str) -> io::Result<
test
))]
impl ArmedCanary {
- pub(crate) fn disarm(self) {
+ pub(super) fn disarm(self) {
if let Err(error) = remove_file_durably(&self.path) {
log::warn!(
"failed to disarm WebGPU crash canary at '{}': {error}",