Skip to content

armgit / ente

Public Imported from GitHub
0

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

perf(rust): reduce onnx shape and output copy overhead

laurenspriem01966d45eef7
diff --git a/mobile/apps/photos/rust/src/ml/clip/image.rs b/mobile/apps/photos/rust/src/ml/clip/image.rs
index a6342f9243..c712b1d275 100644
--- a/mobile/apps/photos/rust/src/ml/clip/image.rs
+++ b/mobile/apps/photos/rust/src/ml/clip/image.rs
@@ -8,7 +8,7 @@ use crate::ml::{
pub fn run_clip_image(runtime: &mut MlRuntime, decoded: &DecodedImage) -> MlResult<ClipResult> {
let input = preprocess::preprocess_clip(decoded)?;
let clip_image = runtime.clip_image_session_mut()?;
- let (shape, output) = onnx::run_f32(clip_image, input, vec![1, 3, 256, 256])?;
+ let (shape, output) = onnx::run_f32(clip_image, input, [1, 3, 256, 256])?;
let mut embedding = if shape.len() == 2 {
if shape[0] != 1 {
diff --git a/mobile/apps/photos/rust/src/ml/face/detect.rs b/mobile/apps/photos/rust/src/ml/face/detect.rs
index 043c9e1c71..a71991eaa1 100644
--- a/mobile/apps/photos/rust/src/ml/face/detect.rs
+++ b/mobile/apps/photos/rust/src/ml/face/detect.rs
@@ -17,10 +17,10 @@ pub fn run_face_detection(
let (input, scaled_width, scaled_height, pad_left, pad_top) =
preprocess::preprocess_yolo(decoded)?;
let face_detection = runtime.face_detection_session_mut()?;
- let (output_shape, output_data) = onnx::run_f32(
+ let output_data = onnx::run_f32_data(
face_detection,
input,
- vec![1, 3, INPUT_HEIGHT as i64, INPUT_WIDTH as i64],
+ [1, 3, INPUT_HEIGHT as i64, INPUT_WIDTH as i64],
)?;
let row_len = 16usize;
@@ -75,7 +75,6 @@ pub fn run_face_detection(
});
}
- let _ = output_shape;
Ok(naive_non_max_suppression(detections, IOU_THRESHOLD))
}
diff --git a/mobile/apps/photos/rust/src/ml/face/embed.rs b/mobile/apps/photos/rust/src/ml/face/embed.rs
index 508c0a4bbd..309dead3a2 100644
--- a/mobile/apps/photos/rust/src/ml/face/embed.rs
+++ b/mobile/apps/photos/rust/src/ml/face/embed.rs
@@ -42,7 +42,7 @@ pub fn run_face_embedding(
let (shape, output) = onnx::run_f32(
face_embedding,
input,
- vec![
+ [
aligned_faces.len() as i64,
FACE_INPUT_HEIGHT,
FACE_INPUT_WIDTH,
diff --git a/mobile/apps/photos/rust/src/ml/onnx.rs b/mobile/apps/photos/rust/src/ml/onnx.rs
index 4bf4471d61..56ede40ee7 100644
--- a/mobile/apps/photos/rust/src/ml/onnx.rs
+++ b/mobile/apps/photos/rust/src/ml/onnx.rs
@@ -108,10 +108,10 @@ fn build_session_with_providers(
Ok(session)
}
-pub fn run_f32(
+pub fn run_f32<const N: usize>(
session: &mut Session,
input: Vec<f32>,
- input_shape: Vec<i64>,
+ input_shape: [i64; N],
) -> MlResult<(Vec<i64>, Vec<f32>)> {
let input_tensor = Tensor::<f32>::from_array((input_shape, input))?;
let outputs = session.run(ort::inputs![input_tensor]?)?;
@@ -124,3 +124,18 @@ pub fn run_f32(
let data = tensor.iter().copied().collect::<Vec<_>>();
Ok((shape, data))
}
+
+pub fn run_f32_data<const N: usize>(
+ session: &mut Session,
+ input: Vec<f32>,
+ input_shape: [i64; N],
+) -> MlResult<Vec<f32>> {
+ let input_tensor = Tensor::<f32>::from_array((input_shape, input))?;
+ let outputs = session.run(ort::inputs![input_tensor]?)?;
+ if outputs.is_empty() {
+ return Err(MlError::Ort("missing first output tensor".to_string()));
+ }
+ let output = &outputs[0];
+ let tensor = output.try_extract_tensor::<f32>()?;
+ Ok(tensor.iter().copied().collect::<Vec<_>>())
+}