π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Single batch rust call for suggestion
laurenspriem0be4c200dc12
diff --git a/mobile/apps/photos/lib/db/ml/cluster_centroid_vector_db.dart b/mobile/apps/photos/lib/db/ml/cluster_centroid_vector_db.dartindex 591720cfbd..ebcc9b6a26 100644--- a/mobile/apps/photos/lib/db/ml/cluster_centroid_vector_db.dart+++ b/mobile/apps/photos/lib/db/ml/cluster_centroid_vector_db.dart@@ -242,6 +242,65 @@ class ClusterCentroidVectorDB {}}+ Future<List<List<(int, double)>>>+ bulkSearchApproxClosestCentroidsWithinDistance(+ List<List<double>> queries,+ Set<int> allowedClusterVectorIDs, {+ required double maxDistance,+ int count = 10,+ }) async {+ if (!await checkIfMigrationDone()) {+ throw StateError(+ "Cluster centroid vector DB migration is not done, cannot run approximate search",+ );+ }+ if (count <= 0 || queries.isEmpty || allowedClusterVectorIDs.isEmpty) {+ return const <List<(int, double)>>[];+ }+ final db = await _vectorDB;+ try {+ final rustQueries = <Float32List>[];+ for (final query in queries) {+ if (query is Float32List) {+ rustQueries.add(query);+ } else {+ rustQueries.add(Float32List.fromList(query));+ }+ }+ final allowedKeys = Uint64List.fromList(+ allowedClusterVectorIDs.toList(growable: false),+ );+ final result = await db.bulkApproxFilteredSearchVectorsWithinDistance(+ queries: rustQueries,+ allowedKeys: allowedKeys,+ count: BigInt.from(count),+ maxDistance: maxDistance,+ );++ final keysPerQuery = result.$1;+ final distancesPerQuery = result.$2;+ final alignedQueryLength = keysPerQuery.length < distancesPerQuery.length+ ? keysPerQuery.length+ : distancesPerQuery.length;+ final output = <List<(int, double)>>[];+ for (var i = 0; i < alignedQueryLength; i++) {+ final keys = keysPerQuery[i];+ final distances = distancesPerQuery[i];+ final alignedLength =+ keys.length < distances.length ? keys.length : distances.length;+ final matches = <(int, double)>[];+ for (var j = 0; j < alignedLength; j++) {+ matches.add((keys[j].toInt(), distances[j]));+ }+ output.add(matches);+ }+ return output;+ } catch (e, s) {+ _logger.severe("Error bulk searching filtered centroids", e, s);+ rethrow;+ }+ }+Future<Map<int, List<(int, double)>>>bulkSearchApproxClosestCentroidsForKeysWithinDistance(Set<int> queryClusterVectorIDs, {diff --git a/mobile/apps/photos/lib/services/machine_learning/face_ml/feedback/cluster_feedback.dart b/mobile/apps/photos/lib/services/machine_learning/face_ml/feedback/cluster_feedback.dartindex 0c6e122acd..72dfbd11b2 100644--- a/mobile/apps/photos/lib/services/machine_learning/face_ml/feedback/cluster_feedback.dart+++ b/mobile/apps/photos/lib/services/machine_learning/face_ml/feedback/cluster_feedback.dart@@ -644,29 +644,45 @@ class ClusterFeedbackService<T> {continue;}+ final queryClusterIDs = <String>[];+ final queryVectors = <List<double>>[];for (final queryClusterID in entry.value) {final queryVector = clusterAvg[queryClusterID];if (queryVector == null) {continue;}+ queryClusterIDs.add(queryClusterID);+ queryVectors.add(queryVector.toList(growable: false));+ }+ if (queryClusterIDs.isEmpty) {+ continue;+ }- List<(int, double)> filteredMatches;- try {- filteredMatches = await _clusterCentroidVectorDB- .searchApproxClosestCentroidsWithinDistance(- queryVector.toList(),- allowedVectorIDs,- maxDistance: 0.65,- count: _allPeopleSuggestionVectorResultCount,- );- } catch (e, s) {- _logger.warning(- "Filtered centroid fallback search failed for query cluster $queryClusterID",- e,- s,- );- continue;- }+ List<List<(int, double)>> filteredMatchesByQuery;+ try {+ filteredMatchesByQuery = await _clusterCentroidVectorDB+ .bulkSearchApproxClosestCentroidsWithinDistance(+ queryVectors,+ allowedVectorIDs,+ maxDistance: 0.65,+ count: _allPeopleSuggestionVectorResultCount,+ );+ } catch (e, s) {+ _logger.warning(+ "Filtered centroid fallback search failed for person $personID",+ e,+ s,+ );+ continue;+ }++ final alignedLength = min(+ queryClusterIDs.length,+ filteredMatchesByQuery.length,+ );+ for (var i = 0; i < alignedLength; i++) {+ final queryClusterID = queryClusterIDs[i];+ final filteredMatches = filteredMatchesByQuery[i];if (filteredMatches.isEmpty) {continue;}@@ -1949,19 +1965,36 @@ class ClusterFeedbackService<T> {for (final entry in clusterIDToVectorID.entries) entry.value: entry.key,};- final bestSuggestionPerCluster = <String, (double, String)>{};+ final orderedPersonClusterIDs = <String>[];+ final queryVectors = <List<double>>[];for (final personClusterID in validPersonClusters) {final queryVector = clusterAvg[personClusterID];if (queryVector == null) {continue;}- final matches = await _clusterCentroidVectorDB- .searchApproxClosestCentroidsWithinDistance(- queryVector.toList(),- baseAllowedVectorIDs,- maxDistance: maxClusterDistance,- count: perClusterResultCount,- );+ orderedPersonClusterIDs.add(personClusterID);+ queryVectors.add(queryVector.toList(growable: false));+ }+ if (orderedPersonClusterIDs.isEmpty) {+ return [];+ }++ final matchesPerQuery = await _clusterCentroidVectorDB+ .bulkSearchApproxClosestCentroidsWithinDistance(+ queryVectors,+ baseAllowedVectorIDs,+ maxDistance: maxClusterDistance,+ count: perClusterResultCount,+ );++ final bestSuggestionPerCluster = <String, (double, String)>{};+ final alignedQueryLength = min(+ orderedPersonClusterIDs.length,+ matchesPerQuery.length,+ );+ for (var i = 0; i < alignedQueryLength; i++) {+ final personClusterID = orderedPersonClusterIDs[i];+ final matches = matchesPerQuery[i];for (final match in matches) {final suggestedClusterID = vectorIDToClusterID[match.$1];diff --git a/mobile/apps/photos/rust/src/api/usearch_api.rs b/mobile/apps/photos/rust/src/api/usearch_api.rsindex 411b7ba75d..07e03e659d 100644--- a/mobile/apps/photos/rust/src/api/usearch_api.rs+++ b/mobile/apps/photos/rust/src/api/usearch_api.rs@@ -178,6 +178,60 @@ impl VectorDB {Self::truncate_sorted_matches_within_distance(matches.keys, matches.distances, max_distance)}+ pub fn bulk_approx_filtered_search_vectors_within_distance(+ &self,+ queries: &Vec<Vec<f32>>,+ allowed_keys: &[u64],+ count: usize,+ max_distance: f32,+ ) -> (Vec<Vec<u64>>, Vec<Vec<f32>>) {+ let query_count = queries.len();+ if query_count == 0 {+ return (Vec::new(), Vec::new());+ }++ let empty_aligned_results = || {+ (+ vec![Vec::<u64>::new(); query_count],+ vec![Vec::<f32>::new(); query_count],+ )+ };++ let index_size = self.index.size();+ if index_size == 0 || count == 0 || allowed_keys.is_empty() {+ return empty_aligned_results();+ }+ if !max_distance.is_finite() || max_distance < 0.0 {+ return empty_aligned_results();+ }++ let allowed = allowed_keys.iter().copied().collect::<HashSet<u64>>();+ let search_count = count.min(allowed.len()).min(index_size);+ if search_count == 0 {+ return empty_aligned_results();+ }++ let mut all_keys = Vec::with_capacity(query_count);+ let mut all_distances = Vec::with_capacity(query_count);++ for query in queries {+ let matches = self+ .index+ .filtered_search(query, search_count, |key| allowed.contains(&key))+ .expect("Failed to run filtered vector search");++ let (keys, distances) = Self::truncate_sorted_matches_within_distance(+ matches.keys,+ matches.distances,+ max_distance,+ );+ all_keys.push(keys);+ all_distances.push(distances);+ }++ (all_keys, all_distances)+ }+fn fast_search_vectors_within_distance(&self,query: &[f32],