π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
bulk vector search in MLComputer isolate
laurenspriem6223a1529f51
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_computer.dart b/mobile/apps/photos/lib/services/machine_learning/ml_computer.dartindex 939d486f64..4856134fdd 100644--- a/mobile/apps/photos/lib/services/machine_learning/ml_computer.dart+++ b/mobile/apps/photos/lib/services/machine_learning/ml_computer.dart@@ -1,5 +1,8 @@import 'dart:async';+import "dart:typed_data" show Float32List;+import "package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart"+ show Uint64List;import "package:logging/logging.dart";import "package:photos/models/ml/vector.dart";import "package:photos/services/machine_learning/ml_constants.dart";@@ -26,11 +29,31 @@ class MLComputer extends SuperIsolate {@overridebool get shouldAutomaticDispose => false;+ bool _isRustInit = false;+// Singleton patternMLComputer._privateConstructor();static final MLComputer instance = MLComputer._privateConstructor();factory MLComputer() => instance;+ Future<(List<Uint64List>, List<Float32List>)> bulkVectorSearch(+ List<Float32List> clipFloat32,+ bool exact,+ ) async {+ try {+ final result = await runInIsolate(IsolateOperation.bulkVectorSearch, {+ "clipFloat32": clipFloat32,+ "exact": exact,+ "initRust": !_isRustInit,+ });+ _isRustInit = true;+ return result;+ } catch (e, s) {+ _logger.severe("Could not run bulk vector search in MLComputer", e, s);+ rethrow;+ }+ }+Future<List<double>> runClipText(String query) async {try {await _ensureLoadedClipTextModel();@@ -138,4 +161,9 @@ class MLComputer extends SuperIsolate {rethrow;}}++ @override+ Future<void> onDispose() async {+ _isRustInit = false;+ }}diff --git a/mobile/apps/photos/lib/services/machine_learning/similar_images_service.dart b/mobile/apps/photos/lib/services/machine_learning/similar_images_service.dartindex 284ea7b4fa..e439666d6b 100644--- a/mobile/apps/photos/lib/services/machine_learning/similar_images_service.dart+++ b/mobile/apps/photos/lib/services/machine_learning/similar_images_service.dart@@ -5,13 +5,13 @@ import "package:flutter/foundation.dart" show kDebugMode;import "package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart"show Uint64List;import 'package:logging/logging.dart';-import "package:photos/db/ml/clip_vector_db.dart";import "package:photos/db/ml/db.dart";import "package:photos/extensions/stop_watch.dart";import 'package:photos/models/file/file.dart';import "package:photos/models/file/file_type.dart";import "package:photos/models/ml/vector.dart";import "package:photos/models/similar_files.dart";+import "package:photos/services/machine_learning/ml_computer.dart";import "package:photos/services/search_service.dart";class SimilarImagesService {@@ -24,9 +24,9 @@ class SimilarImagesService {/// Returns a list of SimilarFiles, where each SimilarFiles object contains/// a list of files that are perceptually similarFuture<List<SimilarFiles>> getSimilarFiles(- double distanceThreshold,- {bool exact = false,}- ) async {+ double distanceThreshold, {+ bool exact = false,+ }) async {try {final now = DateTime.now();final List<SimilarFiles> result =@@ -66,11 +66,9 @@ class SimilarImagesService {w?.log("getAllClipVectors");// Run bulk vector search- final (vectorKeys, distances) =- await ClipVectorDB.instance.bulkSearchVectors(+ final (vectorKeys, distances) = await MLComputer.instance.bulkVectorSearch(clipFloat32,- BigInt.from(100),- exact: exact,+ exact,);w?.log("bulkSearchVectors");diff --git a/mobile/apps/photos/lib/utils/isolate/isolate_operations.dart b/mobile/apps/photos/lib/utils/isolate/isolate_operations.dartindex ae4e1e8851..c82143f55e 100644--- a/mobile/apps/photos/lib/utils/isolate/isolate_operations.dart+++ b/mobile/apps/photos/lib/utils/isolate/isolate_operations.dart@@ -1,6 +1,7 @@-import 'dart:typed_data' show Uint8List;+import 'dart:typed_data' show Uint8List, Float32List;import "package:ml_linalg/linalg.dart";+import "package:photos/db/ml/clip_vector_db.dart";import "package:photos/models/ml/face/box.dart";import "package:photos/models/ml/vector.dart";import "package:photos/services/machine_learning/face_ml/face_clustering/face_clustering_service.dart";@@ -10,6 +11,7 @@ import "package:photos/services/machine_learning/ml_result.dart";import "package:photos/services/machine_learning/semantic_search/clip/clip_text_encoder.dart";import "package:photos/services/machine_learning/semantic_search/clip/clip_text_tokenizer.dart";import "package:photos/services/machine_learning/semantic_search/query_result.dart";+import "package:photos/src/rust/frb_generated.dart" show RustLib;import "package:photos/utils/image_ml_util.dart";import "package:photos/utils/ml_util.dart";@@ -40,6 +42,9 @@ enum IsolateOperation {/// [MLComputer]computeBulkSimilarities,+ /// [MLComputer]+ bulkVectorSearch,+/// [FaceClusteringService]linearIncrementalClustering,@@ -57,6 +62,18 @@ Future<dynamic> isolateFunction(Map<String, dynamic> args,) async {switch (function) {+ case IsolateOperation.bulkVectorSearch:+ final initRust = args["initRust"] as bool? ?? false;+ if (initRust) await RustLib.init();+ final clipFloat32 = args["clipFloat32"] as List<Float32List>;+ final exact = args["exact"] as bool;++ return ClipVectorDB.instance.bulkSearchVectors(+ clipFloat32,+ BigInt.from(100),+ exact: exact,+ );+/// Cases for MLIndexingIsolate start here/// MLIndexingIsolate