Skip to content

armgit / ente

Public Imported from GitHub
0

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

Merge branch 'main' into clip_text_rust

laurenspriem1946431c6d30
diff --cc mobile/apps/photos/rust/src/ml/clip/text.rs
index 2c942df165,0000000000..18e5725482
mode 100644,000000..100644
--- a/mobile/apps/photos/rust/src/ml/clip/text.rs
+++ b/mobile/apps/photos/rust/src/ml/clip/text.rs
@@@ -1,68 -1,0 +1,68 @@@
+use crate::ml::{
+ clip::tokenizer,
+ error::{MlError, MlResult},
+ onnx,
+ runtime::MlRuntime,
+ types::ClipResult,
+};
+
+const CLIP_TEXT_TOKEN_COUNT: usize = 77;
+
+pub fn run_clip_text(runtime: &mut MlRuntime, token_ids: &[i32]) -> MlResult<ClipResult> {
+ if token_ids.len() != CLIP_TEXT_TOKEN_COUNT {
+ return Err(MlError::InvalidRequest(format!(
+ "clip text expects exactly {CLIP_TEXT_TOKEN_COUNT} tokens, got {}",
+ token_ids.len()
+ )));
+ }
+
+ let clip_text = runtime.clip_text_session_mut()?;
+ let (shape, output) = onnx::run_i32_f32(
+ clip_text,
+ token_ids.to_vec(),
- vec![1, CLIP_TEXT_TOKEN_COUNT as i64],
++ [1, CLIP_TEXT_TOKEN_COUNT as i64],
+ )?;
+
+ let mut embedding = if shape.len() == 2 {
+ if shape[0] != 1 {
+ return Err(MlError::Postprocess(format!(
+ "unexpected CLIP text batch size in shape {:?}",
+ shape
+ )));
+ }
+ output
+ } else if shape.len() == 1 {
+ output
+ } else {
+ return Err(MlError::Postprocess(format!(
+ "unsupported CLIP text output shape {:?}",
+ shape
+ )));
+ };
+
+ normalize_embedding(&mut embedding);
+ Ok(ClipResult { embedding })
+}
+
+pub fn run_clip_text_query(
+ runtime: &mut MlRuntime,
+ query: &str,
+ vocab_path: &str,
+) -> MlResult<ClipResult> {
+ let token_ids = tokenizer::tokenize_clip_text(query, vocab_path)?;
+ run_clip_text(runtime, &token_ids)
+}
+
+fn normalize_embedding(embedding: &mut [f32]) {
+ let mut norm = 0.0f32;
+ for value in embedding.iter() {
+ norm += value * value;
+ }
+ let norm = norm.sqrt();
+ if norm <= f32::EPSILON {
+ return;
+ }
+ for value in embedding.iter_mut() {
+ *value /= norm;
+ }
+}
diff --cc mobile/apps/photos/rust/src/ml/onnx.rs
index b254f8fa73,56ede40ee7..9eb461843f
--- a/mobile/apps/photos/rust/src/ml/onnx.rs
+++ b/mobile/apps/photos/rust/src/ml/onnx.rs
@@@ -125,19 -125,17 +125,34 @@@ pub fn run_f32<const N: usize>
Ok((shape, data))
}
- pub fn run_i32_f32(
+ 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<_>>())
+ }
++
++pub fn run_i32_f32<const N: usize>(
+ session: &mut Session,
+ input: Vec<i32>,
- input_shape: Vec<i64>,
++ input_shape: [i64; N],
+) -> MlResult<(Vec<i64>, Vec<f32>)> {
+ let input_tensor = Tensor::<i32>::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>()?;
+ let shape = tensor.shape().iter().map(|d| *d as i64).collect::<Vec<_>>();
+ let data = tensor.iter().copied().collect::<Vec<_>>();
+ Ok((shape, data))
+}
diff --cc mobile/apps/photos/scripts/internal_changes.txt
index 6f5f1d25ae,5c44ee674e..6ffe9c41e6
--- a/mobile/apps/photos/scripts/internal_changes.txt
+++ b/mobile/apps/photos/scripts/internal_changes.txt
@@@ -1,4 -1,4 +1,5 @@@
+- Laurens: (I) Clip text encoding in rust
+ - Setal,Ashil: Improve Help & Support section in settings
- Laurens: (I) Internal toggle to let ML run uninterrupted
- Laurens: (I) Generate and compress face thumbnails in rust
- Laurens: (I) Use rust indexing ML pipeline for internal users