Skip to content

armgit / ente

Public Imported from GitHub
0

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

Prepare dependency updates

ente-agent[bot]98bd9e1d7663
diff --git a/mobile/apps/photos/lib/services/machine_learning/semantic_search/semantic_search_service.dart b/mobile/apps/photos/lib/services/machine_learning/semantic_search/semantic_search_service.dart
index 421cbc1c2b..155438c949 100644
--- a/mobile/apps/photos/lib/services/machine_learning/semantic_search/semantic_search_service.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/semantic_search/semantic_search_service.dart
@@ -432,7 +432,7 @@ class SemanticSearchService {
);
final endTime = DateTime.now();
_logger.info(
- "computingSimilarities (rust simsimd exact) took for ${textQueryToEmbeddingMap.length} queries " +
+ "computingSimilarities (rust SIMD exact) took for ${textQueryToEmbeddingMap.length} queries " +
(endTime.millisecondsSinceEpoch -
startTime.millisecondsSinceEpoch)
.toString() +
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 779018c493..93661729a6 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -2923,6 +2923,7 @@ dependencies = [
"ente-vecdb",
"flutter_rust_bridge",
"log",
+ "numkong",
]
[[package]]
diff --git a/rust/bindings/frb/photos/Cargo.toml b/rust/bindings/frb/photos/Cargo.toml
index ac1b6f50cb..69470911af 100644
--- a/rust/bindings/frb/photos/Cargo.toml
+++ b/rust/bindings/frb/photos/Cargo.toml
@@ -18,6 +18,7 @@ ente-photos.workspace = true
ente-vecdb.workspace = true
flutter_rust_bridge.workspace = true
log.workspace = true
+numkong = "7.8.5"
[lints]
workspace = true
diff --git a/rust/bindings/frb/photos/src/api/usearch_api.rs b/rust/bindings/frb/photos/src/api/usearch_api.rs
index 68f6a337c9..ee1ca89ba4 100644
--- a/rust/bindings/frb/photos/src/api/usearch_api.rs
+++ b/rust/bindings/frb/photos/src/api/usearch_api.rs
@@ -1,34 +1,19 @@
use ente_ml::vector_db;
use flutter_rust_bridge::frb;
+use numkong::Dot;
type SearchMatch = (Vec<u64>, Vec<f32>);
type BulkSearchMatch = (Vec<Vec<u64>>, Vec<Vec<f32>>);
type BulkSearchByKeyMatch = (Vec<u64>, Vec<Vec<u64>>, Vec<Vec<f32>>);
-// USearch already vendors and links SimSIMD. Call that single exported symbol
-// directly to avoid pulling a second copy of SimSIMD into the final app binary.
-#[expect(
- unsafe_code,
- reason = "Declare the SimSIMD symbol already linked by USearch"
-)]
-unsafe extern "C" {
- fn simsimd_dot_f32(a: *const f32, b: *const f32, n: u64, d: *mut f64);
-}
-
-#[expect(
- unsafe_code,
- reason = "Call the SimSIMD symbol already linked by USearch"
-)]
-fn simsimd_dot_product(a: &[f32], b: &[f32]) -> f64 {
+// Use the same NumKong dependency as USearch for SIMD dispatch and avoid
+// relying on an implementation-specific symbol from USearch's static library.
+fn dot_product(a: &[f32], b: &[f32]) -> f64 {
debug_assert_eq!(a.len(), b.len());
- let mut score = 0.0_f64;
- // SAFETY:
- // - `a` and `b` are valid, non-null slices for `a.len()` elements.
- // - SimSIMD only reads the input buffers and writes one f64 to `score`.
- unsafe {
- simsimd_dot_f32(a.as_ptr(), b.as_ptr(), a.len() as u64, &mut score);
+ match f32::dot(a, b) {
+ Some(score) => score,
+ None => unreachable!("validated embeddings have equal dimensions"),
}
- score
}
fn validate_semantic_search_exact_image_embeddings(
@@ -127,7 +112,7 @@ fn semantic_search_exact_impl(
let mut query_matches = Vec::with_capacity(image_embeddings.len());
for (file_id, image_embedding) in image_file_ids.iter().zip(image_embeddings.iter()) {
- let score = simsimd_dot_product(image_embedding, query_embedding);
+ let score = dot_product(image_embedding, query_embedding);
if score >= minimum_similarity {
query_matches.push(SemanticSearchExactMatch {
@@ -170,7 +155,7 @@ fn semantic_search_exact_flattened_impl(
let offset = index * image_embedding_dimension;
let image_embedding =
&flattened_image_embeddings[offset..offset + image_embedding_dimension];
- let score = simsimd_dot_product(image_embedding, query_embedding);
+ let score = dot_product(image_embedding, query_embedding);
if score >= minimum_similarity {
query_matches.push(SemanticSearchExactMatch {
diff --git a/rust/checks/lint-exceptions/suppressions.json b/rust/checks/lint-exceptions/suppressions.json
index 97f5c94067..38938d1d01 100644
--- a/rust/checks/lint-exceptions/suppressions.json
+++ b/rust/checks/lint-exceptions/suppressions.json
@@ -11,7 +11,6 @@
"clippy::unwrap_used",
"unsafe_code"
],
- "rust/bindings/frb/photos/src/api/usearch_api.rs": ["unsafe_code"],
"rust/bindings/frb/photos/src/lib.rs": [
"clippy::allow_attributes",
"clippy::allow_attributes_without_reason",
diff --git a/rust/crates/image/src/decode.rs b/rust/crates/image/src/decode.rs
index 4ded16a57b..02a58b29ef 100644
--- a/rust/crates/image/src/decode.rs
+++ b/rust/crates/image/src/decode.rs
@@ -483,14 +483,18 @@ fn bytes_look_like_heif(image_bytes: &[u8]) -> bool {
#[cfg(test)]
mod tests {
- use std::ffi::OsStr;
+ use std::{ffi::OsStr, io::Cursor};
use image::hooks::decoding_hook_registered;
use image::{ColorType, ImageEncoder, ImageFormat, codecs::png::PngEncoder};
use moxcms::ColorProfile;
+ use tiff::{
+ encoder::{TiffEncoder, colortype},
+ tags::ExtraSamples,
+ };
use super::{
- bytes_look_like_heif, decode_image_from_bytes, init_image_decoders,
+ bytes_look_like_heif, decode_image_from_bytes, decode_tiff_from_bytes, init_image_decoders,
should_attempt_tiff_fallback,
};
@@ -507,6 +511,51 @@ mod tests {
assert!(!should_attempt_tiff_fallback(Some(ImageFormat::Avif)));
}
+ #[test]
+ fn tiff_primary_and_fallback_decoders_keep_rgb_samples_in_order() {
+ let rgb = [255, 0, 0, 0, 255, 128];
+ let mut encoded = Cursor::new(Vec::new());
+ {
+ let mut encoder = TiffEncoder::new(&mut encoded).unwrap();
+ encoder
+ .new_image::<colortype::RGB8>(2, 1)
+ .unwrap()
+ .write_data(&rgb)
+ .unwrap();
+ }
+ let bytes = encoded.into_inner();
+
+ let fallback = decode_tiff_from_bytes(&bytes).unwrap();
+ assert_eq!(fallback.image.into_rgb8().into_raw(), rgb);
+
+ let primary = decode_image_from_bytes(&bytes).unwrap();
+ assert_eq!(primary.dimensions.width, 2);
+ assert_eq!(primary.dimensions.height, 1);
+ assert_eq!(primary.rgb, rgb);
+ }
+
+ #[test]
+ fn tiff_unspecified_extra_sample_keeps_rgb_channels() {
+ let mut encoded = Cursor::new(Vec::new());
+ {
+ let mut encoder = TiffEncoder::new(&mut encoded).unwrap();
+ let mut image = encoder.new_image::<colortype::RGB8>(1, 1).unwrap();
+ image.extra_samples(&[ExtraSamples::Unspecified]).unwrap();
+ image.write_data(&[17, 34, 51, 68]).unwrap();
+ }
+ let bytes = encoded.into_inner();
+
+ assert_eq!(
+ decode_tiff_from_bytes(&bytes)
+ .unwrap()
+ .image
+ .into_rgb8()
+ .into_raw(),
+ [17, 34, 51]
+ );
+ assert_eq!(decode_image_from_bytes(&bytes).unwrap().rgb, [17, 34, 51]);
+ }
+
#[test]
fn detects_heif_brand_in_bytes() {
let bytes = b"\0\0\0\x18ftypheic\0\0\0\0";
diff --git a/rust/crates/ml/Cargo.toml b/rust/crates/ml/Cargo.toml
index 6929f8b99c..bc725b6c72 100644
--- a/rust/crates/ml/Cargo.toml
+++ b/rust/crates/ml/Cargo.toml
@@ -22,8 +22,8 @@ plsfix = "0.1.8"
rayon = "1.12.0"
regex.workspace = true
thiserror.workspace = true
-# The FRB photos bridge calls the SimSIMD symbol exported by usearch 2.21.x.
-# Newer usearch releases switched that implementation to numkong symbols.
+# Keep this exact: the FRB photos bridge shares USearch's NumKong dependency
+# for exact dot products, and stored index compatibility needs review on upgrade.
usearch = { version = "=2.26.4", optional = true }
# Cargokit sets ORT_LIB_PATH to Ente's verified iOS archive.
diff --git a/rust/crates/ml/src/clip/tokenizer.rs b/rust/crates/ml/src/clip/tokenizer.rs
index ed127f74ec..af7fe4e40b 100644
--- a/rust/crates/ml/src/clip/tokenizer.rs
+++ b/rust/crates/ml/src/clip/tokenizer.rs
@@ -344,6 +344,8 @@ mod tests {
fn basic_clean_matches_ftfy_style_repairs() {
assert_eq!(basic_clean(" Òœ” No problems "), "βœ” No problems");
assert_eq!(basic_clean("Tom &amp;amp; Jerry"), "Tom & Jerry");
+ assert_eq!(basic_clean("A&#x20;B"), "A B");
+ assert_eq!(basic_clean("A&#x1b;B"), "A&#x1b;B");
}
#[test]
diff --git a/rust/crates/ml/src/cv/warp_rgb.rs b/rust/crates/ml/src/cv/warp_rgb.rs
index 862d4983f3..671f88e5ed 100644
--- a/rust/crates/ml/src/cv/warp_rgb.rs
+++ b/rust/crates/ml/src/cv/warp_rgb.rs
@@ -50,7 +50,7 @@ pub(super) fn warp(source: &RgbImage, projection: Projection, width: u32, height
#[cfg(test)]
mod tests {
use image::Rgb;
- use imageproc::geometric_transformations::{Interpolation, warp_into};
+ use imageproc::geometric_transformations::{Border, Interpolation, warp_into};
use super::*;
@@ -77,15 +77,30 @@ mod tests {
1.0,
])
.expect("valid projection");
+ let inverse = projection.invert();
let actual = warp(&source, projection, 113, 97);
let mut expected = RgbImage::new(113, 97);
warp_into(
&source,
- &projection,
+ projection,
Interpolation::Bilinear,
- Rgb([0, 0, 0]),
+ Border::Constant(Rgb([0, 0, 0])),
&mut expected,
);
+ // The older imageproc reference returned black when any of the
+ // four bilinear source pixels fell outside the image.
+ for (x, y, pixel) in expected.enumerate_pixels_mut() {
+ let (source_x, source_y) = inverse * (x as f32, y as f32);
+ let left = source_x.floor();
+ let top = source_y.floor();
+ if !(left >= 0.0
+ && left + 1.0 < source.width() as f32
+ && top >= 0.0
+ && top + 1.0 < source.height() as f32)
+ {
+ *pixel = Rgb([0, 0, 0]);
+ }
+ }
assert_eq!(actual, expected, "case {index}");
}
}
diff --git a/rust/crates/ml/src/face/align.rs b/rust/crates/ml/src/face/align.rs
index 8316b2140e..0f4ec7afd6 100644
--- a/rust/crates/ml/src/face/align.rs
+++ b/rust/crates/ml/src/face/align.rs
@@ -1,5 +1,5 @@
use image::{ImageBuffer, Rgb, RgbImage};
-use imageproc::geometric_transformations::{Interpolation, Projection, warp_into};
+use imageproc::geometric_transformations::{Border, Interpolation, Projection, warp_into};
use nalgebra::{Matrix2, Matrix3, Vector2};
use crate::{
@@ -243,13 +243,28 @@ fn warp_face_image(source: &RgbImage, affine_matrix: &[[f32; 3]; 3]) -> MlResult
.ok_or_else(|| MlError::Image("invalid affine matrix projection".to_string()))?;
let mut output = RgbImage::from_pixel(FACE_INPUT_SIZE, FACE_INPUT_SIZE, Rgb([114, 114, 114]));
+ let inverse = projection.invert();
warp_into(
source,
- &projection,
+ projection,
Interpolation::Bicubic,
- Rgb([114, 114, 114]),
+ Border::Constant(Rgb([114, 114, 114])),
&mut output,
);
+ // imageproc 0.27 blends border pixels into bicubic samples. The previous
+ // version returned the fill color unless the full 4x4 sample was in bounds.
+ for (x, y, pixel) in output.enumerate_pixels_mut() {
+ let (source_x, source_y) = inverse * (x as f32, y as f32);
+ let left = source_x.floor() - 1.0;
+ let top = source_y.floor() - 1.0;
+ if !(left >= 0.0
+ && left + 4.0 < source.width() as f32
+ && top >= 0.0
+ && top + 4.0 < source.height() as f32)
+ {
+ *pixel = Rgb([114, 114, 114]);
+ }
+ }
Ok(output)
}
@@ -408,11 +423,15 @@ fn variance_2d(matrix: &[i32], rows: usize, cols: usize) -> f32 {
#[cfg(test)]
mod tests {
- use super::run_face_alignment;
+ use super::{
+ MOBILEFACENET_IDEAL_5_LANDMARKS, estimate_similarity_transform, run_face_alignment,
+ warp_face_image,
+ };
use crate::{
error::MlError,
types::{DecodedImage, Dimensions, FaceDetection},
};
+ use image::{Rgb, RgbImage};
#[test]
fn face_alignment_restores_the_decoded_rgb_allocation() {
@@ -453,4 +472,31 @@ mod tests {
assert!(matches!(result, Err(MlError::Image(_))));
assert_eq!(decoded.rgb, (0..12).collect::<Vec<_>>());
}
+
+ #[test]
+ fn face_warp_retains_the_previous_bicubic_border_fill() {
+ let source = RgbImage::from_pixel(112, 112, Rgb([255, 0, 0]));
+ let identity = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
+
+ let output = warp_face_image(&source, &identity).unwrap();
+
+ assert_eq!(*output.get_pixel(0, 0), Rgb([114, 114, 114]));
+ assert_eq!(*output.get_pixel(1, 1), Rgb([255, 0, 0]));
+ assert_eq!(*output.get_pixel(109, 109), Rgb([114, 114, 114]));
+ }
+
+ #[test]
+ fn similarity_transform_recovers_known_scale_and_translation() {
+ let source = MOBILEFACENET_IDEAL_5_LANDMARKS.map(|[x, y]| [2.0 * x + 0.1, 2.0 * y - 0.2]);
+
+ let transform = estimate_similarity_transform(&source)
+ .unwrap()
+ .affine_matrix;
+ let expected = [[0.5, 0.0, -0.05], [0.0, 0.5, 0.1], [0.0, 0.0, 1.0]];
+ for (actual_row, expected_row) in transform.iter().zip(expected) {
+ for (actual, expected) in actual_row.iter().zip(expected_row) {
+ assert!((actual - expected).abs() < 1e-5);
+ }
+ }
+ }
}