Skip to content

armgit / ente

Public Imported from GitHub
0

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

Move ONNX golden validation under ONNX

laurenspriem5a1fcbc625fe
diff --git a/rust/crates/photos/src/ml/mod.rs b/rust/crates/photos/src/ml/mod.rs
index 560687f2c7..f0c79a77e1 100644
--- a/rust/crates/photos/src/ml/mod.rs
+++ b/rust/crates/photos/src/ml/mod.rs
@@ -3,12 +3,11 @@ mod clip;
mod diagnostics;
pub mod error;
pub mod face;
-pub mod golden;
-mod golden_data;
-pub mod golden_tooling;
pub mod indexing;
mod model;
mod onnx;
+pub use onnx::golden;
+pub use onnx::golden::tooling as golden_tooling;
mod pet;
mod postprocess;
mod preprocess;
diff --git a/rust/crates/photos/src/ml/onnx.rs b/rust/crates/photos/src/ml/onnx.rs
index e4762c0dcc..ec63e9cd8c 100644
--- a/rust/crates/photos/src/ml/onnx.rs
+++ b/rust/crates/photos/src/ml/onnx.rs
@@ -2,9 +2,9 @@ use ort::session::Session;
use std::{fmt, path::Path};
use crate::ml::error::{MlError, MlResult};
-use crate::ml::golden;
mod coreml_cache;
+pub mod golden;
mod providers;
mod tensor;
mod webgpu;
diff --git a/rust/crates/photos/src/ml/golden_data.rs b/rust/crates/photos/src/ml/onnx/golden/data.rs
similarity index 99%
rename from rust/crates/photos/src/ml/golden_data.rs
rename to rust/crates/photos/src/ml/onnx/golden/data.rs
index 245e5f13a7..8ec62d64f4 100644
--- a/rust/crates/photos/src/ml/golden_data.rs
+++ b/rust/crates/photos/src/ml/onnx/golden/data.rs
@@ -6,7 +6,7 @@
// TODO: Pet models have no golden entries because they are CPU-only. Add
// entries before moving them off CPU (see `MlRuntime::new` in runtime.rs).
-use crate::ml::golden::{GoldenEntry, GoldenInput, GoldenMetric};
+use super::{GoldenEntry, GoldenInput, GoldenMetric};
pub(crate) static GOLDEN_ENTRIES: &[GoldenEntry] = &[
GoldenEntry {
diff --git a/rust/crates/photos/src/ml/golden.rs b/rust/crates/photos/src/ml/onnx/golden/mod.rs
similarity index 99%
rename from rust/crates/photos/src/ml/golden.rs
rename to rust/crates/photos/src/ml/onnx/golden/mod.rs
index 8801ccb69e..a178723824 100644
--- a/rust/crates/photos/src/ml/golden.rs
+++ b/rust/crates/photos/src/ml/onnx/golden/mod.rs
@@ -3,7 +3,10 @@
// reporting success. Deterministic inputs exercise the same kernels as real
// inputs because these models have no data-dependent control flow.
-use crate::ml::golden_data::GOLDEN_ENTRIES;
+mod data;
+pub mod tooling;
+
+use data::GOLDEN_ENTRIES;
// False rejections fall back to CPU, so these unvalidated thresholds are
// deliberately loose pending accelerated-hardware measurements. Healthy fp16
@@ -136,7 +139,7 @@ pub fn prepare_input(entry: &GoldenEntry) -> Result<PreparedGoldenInput, String>
}
}
-// Shared by the device self-test and generator. Regenerate `golden_data.rs` if
+// 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> {
let mut state = seed.max(1);
diff --git a/rust/crates/photos/src/ml/golden_tooling.rs b/rust/crates/photos/src/ml/onnx/golden/tooling.rs
similarity index 91%
rename from rust/crates/photos/src/ml/golden_tooling.rs
rename to rust/crates/photos/src/ml/onnx/golden/tooling.rs
index 63a6c78052..c642280d25 100644
--- a/rust/crates/photos/src/ml/golden_tooling.rs
+++ b/rust/crates/photos/src/ml/onnx/golden/tooling.rs
@@ -3,9 +3,10 @@ use std::path::Path;
use ort::value::ValueType;
-use crate::ml::{clip, golden, golden_data::GOLDEN_ENTRIES, onnx};
+use crate::ml::{clip, onnx};
-pub use crate::ml::golden::GoldenMetric;
+pub use super::GoldenMetric;
+use super::data::GOLDEN_ENTRIES;
const GOLDEN_NOISE_SEED: u64 = 0x0060_1DE2_5EED_2026;
@@ -18,7 +19,7 @@ const GENERATED_FILE_HEADER: &str = "\
// TODO: Pet models have no golden entries because they are CPU-only. Add
// entries before moving them off CPU (see `MlRuntime::new` in runtime.rs).
-use crate::ml::golden::{GoldenEntry, GoldenInput, GoldenMetric};
+use super::{GoldenEntry, GoldenInput, GoldenMetric};
pub(crate) static GOLDEN_ENTRIES: &[GoldenEntry] = &[
";
@@ -54,23 +55,23 @@ pub fn render_golden_data(models: &[GoldenModelSpec]) -> Result<String, String>
pub fn measure_zero_golden_separation(model_path: &str) -> Result<ZeroGoldenSeparation, String> {
let model_file = model_file_name(model_path)?;
- let entry = golden::lookup(model_path)
+ let entry = super::lookup(model_path)
.ok_or_else(|| format!("{model_file}: no committed golden entry"))?;
let mut session = build_cpu_session(model_path)?;
- let golden_input = golden::prepare_input(entry)
+ let golden_input = super::prepare_input(entry)
.map_err(|error| format!("{model_file}: preparing golden input: {error}"))?;
let zero_input = golden_input.zeroed();
let zero_output = onnx::run_golden_tensor(&mut session, entry.input_shape, &zero_input)
.map_err(|error| format!("{model_file}: zero-input CPU inference failed: {error}"))?;
- golden::validate_output(entry, &zero_output)
+ super::validate_output(entry, &zero_output)
.map_err(|error| format!("{model_file}: invalid zero-input output: {error}"))?;
let golden_output = onnx::run_golden_tensor(&mut session, entry.input_shape, &golden_input)
.map_err(|error| format!("{model_file}: golden CPU inference failed: {error}"))?;
- let golden_distance = golden::compare_output(entry, &golden_output)
+ let golden_distance = super::compare_output(entry, &golden_output)
.map_err(|error| format!("{model_file}: golden output rejected: {error}"))?;
- let zero_distance = golden::output_distance(entry, &zero_output)
+ let zero_distance = super::output_distance(entry, &zero_output)
.map_err(|error| format!("{model_file}: measuring zero-input output: {error}"))?;
Ok(ZeroGoldenSeparation {
@@ -103,11 +104,11 @@ pub fn verify_goldens_against_pins(pins: &[PinnedModel]) -> Vec<String> {
for (file_name, pinned_sha) in &pinned {
match committed.get(file_name) {
None => failures.push(format!(
- "{file_name}: no committed golden entry; regenerate golden_data.rs"
+ "{file_name}: no committed golden entry; regenerate ONNX golden data"
)),
Some(committed_sha) if committed_sha != pinned_sha => failures.push(format!(
"{file_name}: model content changed (pinned sha256 {pinned_sha}, golden \
- generated from {committed_sha}); regenerate golden_data.rs"
+ generated from {committed_sha}); regenerate ONNX golden data"
)),
Some(_) => {}
}
@@ -116,7 +117,7 @@ pub fn verify_goldens_against_pins(pins: &[PinnedModel]) -> Vec<String> {
if !pinned.contains_key(file_name) {
failures.push(format!(
"{file_name}: committed golden entry does not correspond to any pinned \
- production model; regenerate golden_data.rs"
+ production model; regenerate ONNX golden data"
));
}
}
@@ -135,10 +136,7 @@ fn render_entry(spec: &GoldenModelSpec) -> Result<String, String> {
let (input_source, prepared_input) = match &spec.input {
GoldenSpecInput::SeededNoise => (
format!("GoldenInput::SeededF32 {{ seed: {GOLDEN_NOISE_SEED:#x} }}"),
- golden::PreparedGoldenInput::F32(golden::seeded_noise(
- GOLDEN_NOISE_SEED,
- element_count,
- )),
+ super::PreparedGoldenInput::F32(super::seeded_noise(GOLDEN_NOISE_SEED, element_count)),
),
GoldenSpecInput::ClipText { phrase, vocab_path } => {
let token_ids = clip::tokenize_clip_text(phrase, vocab_path)
@@ -152,7 +150,7 @@ fn render_entry(spec: &GoldenModelSpec) -> Result<String, String> {
}
(
format!("GoldenInput::I32 {{ data: &{token_ids:?} }}"),
- golden::PreparedGoldenInput::I32(token_ids),
+ super::PreparedGoldenInput::I32(token_ids),
)
}
};
@@ -167,7 +165,7 @@ fn render_entry(spec: &GoldenModelSpec) -> Result<String, String> {
"{file_name}: CPU output contains non-finite values; cannot be used as a golden"
));
}
- let sample = golden::sample_output(&output);
+ let sample = super::sample_output(&output);
let mut entry = String::new();
entry.push_str(" GoldenEntry {\n");
@@ -226,8 +224,8 @@ fn static_input_shape(
#[cfg(test)]
mod tests {
+ use super::super::data::GOLDEN_ENTRIES;
use super::{PinnedModel, verify_goldens_against_pins};
- use crate::ml::golden_data::GOLDEN_ENTRIES;
fn pinned_production_models() -> Vec<PinnedModel> {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
diff --git a/rust/crates/photos/src/ml/onnx/providers.rs b/rust/crates/photos/src/ml/onnx/providers.rs
index a5b8b50676..20f24630cd 100644
--- a/rust/crates/photos/src/ml/onnx/providers.rs
+++ b/rust/crates/photos/src/ml/onnx/providers.rs
@@ -28,10 +28,7 @@ use super::coreml_cache;
target_os = "macos",
target_os = "windows"
))]
-use super::model_file_label;
-#[cfg(any(target_os = "android", target_os = "linux", target_os = "windows"))]
-use super::webgpu;
-use crate::ml::error::MlResult;
+use super::golden;
#[cfg(any(
target_os = "android",
target_os = "ios",
@@ -39,7 +36,10 @@ use crate::ml::error::MlResult;
target_os = "macos",
target_os = "windows"
))]
-use crate::ml::golden;
+use super::model_file_label;
+#[cfg(any(target_os = "android", target_os = "linux", target_os = "windows"))]
+use super::webgpu;
+use crate::ml::error::MlResult;
#[cfg(any(target_os = "ios", target_os = "macos"))]
const ENABLE_PERSISTENT_COREML_CACHE: bool = true;
diff --git a/rust/crates/photos/src/ml/onnx/tensor.rs b/rust/crates/photos/src/ml/onnx/tensor.rs
index 0d9d1893eb..c11f5f4ac7 100644
--- a/rust/crates/photos/src/ml/onnx/tensor.rs
+++ b/rust/crates/photos/src/ml/onnx/tensor.rs
@@ -6,12 +6,9 @@ use ort::{
value::{Tensor, TensorElementType, TensorRef, ValueType},
};
-use crate::ml::{
- error::{MlError, MlResult},
- golden,
-};
+use crate::ml::error::{MlError, MlResult};
-use super::{SessionRunError, SessionRunResult};
+use super::{SessionRunError, SessionRunResult, golden};
pub(crate) struct PreparedF32Input {
f32_data: Vec<f32>,
diff --git a/rust/crates/photos/tests/support/ml_indexing.rs b/rust/crates/photos/tests/support/ml_indexing.rs
index 1a97886fbc..505dfd892c 100644
--- a/rust/crates/photos/tests/support/ml_indexing.rs
+++ b/rust/crates/photos/tests/support/ml_indexing.rs
@@ -401,7 +401,7 @@ impl GoldenTestAssets {
}
pub(crate) fn golden_data_path() -> Result<PathBuf> {
- Ok(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/ml/golden_data.rs"))
+ Ok(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/ml/onnx/golden/data.rs"))
}
}