Skip to content

armgit / ente

Public Imported from GitHub
0

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

Warmup vectorDB index for faster initial search

laurenspriema99916ae4451
diff --git a/mobile/apps/photos/lib/db/ml/clip_vector_db.dart b/mobile/apps/photos/lib/db/ml/clip_vector_db.dart
index 0c0062e7da..478b6ff86a 100644
--- a/mobile/apps/photos/lib/db/ml/clip_vector_db.dart
+++ b/mobile/apps/photos/lib/db/ml/clip_vector_db.dart
@@ -34,6 +34,7 @@ class ClipVectorDB {
// only have a single app-wide reference to the database
Future<VectorDb>? _vectorDbFuture;
+ Future<void>? _warmupFuture;
Future<VectorDb> get _vectorDB async {
_vectorDbFuture ??= _initVectorDB();
@@ -217,6 +218,42 @@ class ClipVectorDB {
}
}
+ Future<void> warmupApproxSearch() async {
+ _warmupFuture ??= _warmupApproxSearchInternal();
+ await _warmupFuture;
+ }
+
+ Future<void> _warmupApproxSearchInternal() async {
+ final stopwatch = Stopwatch()..start();
+ try {
+ final db = await _vectorDB;
+ final stats = await getIndexStats(db);
+ if (stats.size == 0) {
+ _logger.info("Skipping VectorDB warmup: index is empty");
+ return;
+ }
+
+ final warmupQuery = List<double>.filled(
+ stats.dimensions,
+ 0.0,
+ growable: false,
+ );
+ await db.searchVectors(
+ query: warmupQuery,
+ count: BigInt.one,
+ exact: false,
+ );
+ _logger.info(
+ "VectorDB warmup finished in ${stopwatch.elapsedMilliseconds} ms",
+ );
+ } catch (e, s) {
+ _logger.warning("VectorDB warmup failed", e, s);
+ _warmupFuture = null;
+ } finally {
+ stopwatch.stop();
+ }
+ }
+
Future<List<QueryResult>> searchApproxSimilaritiesWithinThreshold(
List<double> query,
double minimumSimilarity,
@@ -332,6 +369,7 @@ class ClipVectorDB {
try {
await db.deleteIndex();
_vectorDbFuture = null;
+ _warmupFuture = null;
} catch (e, s) {
_logger.severe("Error deleting index", e, s);
rethrow;
@@ -349,6 +387,7 @@ class ClipVectorDB {
}
_logger.info("Deleted index file on disk");
_vectorDbFuture = null;
+ _warmupFuture = null;
if (undoMigration) {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_migrationKey, false);
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 c78727f283..b4efa7a10d 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
@@ -68,7 +68,7 @@ class SemanticSearchService {
});
if (flagService.usearchForSearch) {
- unawaited(_mlDataDB.checkMigrateFillClipVectorDB());
+ unawaited(_prepareVectorDbForSearch());
}
unawaited(_loadTextModel(delay: true));
@@ -78,6 +78,17 @@ class SemanticSearchService {
return hasGrantedMLConsent && _textModelIsLoaded;
}
+ Future<void> _prepareVectorDbForSearch() async {
+ try {
+ await _mlDataDB.checkMigrateFillClipVectorDB();
+ if (await _canUseVectorDbForSearch()) {
+ await _vectorDB.warmupApproxSearch();
+ }
+ } catch (e, s) {
+ _logger.warning("Failed to prepare VectorDB for search", e, s);
+ }
+ }
+
// searchScreenQuery should only be used for the user initiate query on the search screen.
// If there are multiple call tho this method, then for all the calls, the result will be the same as the last query.
Future<(String, List<EnteFile>)> searchScreenQuery(String query) async {