Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mobile] Skip CLIP vectors with invalid dimensions

laurenspriemf8c4ec1e4269
diff --git a/mobile/apps/photos/changes/ignore-malformed-clip-vectors.md b/mobile/apps/photos/changes/ignore-malformed-clip-vectors.md
new file mode 100644
index 0000000000..dc02f3b80e
--- /dev/null
+++ b/mobile/apps/photos/changes/ignore-malformed-clip-vectors.md
@@ -0,0 +1 @@
+- Prevented image embeddings with invalid dimensions from interrupting Magic Search and Smart Memories.
diff --git a/mobile/apps/photos/lib/db/ml/dart_db.dart b/mobile/apps/photos/lib/db/ml/dart_db.dart
index 7fb429367a..c1143870b5 100644
--- a/mobile/apps/photos/lib/db/ml/dart_db.dart
+++ b/mobile/apps/photos/lib/db/ml/dart_db.dart
@@ -1721,11 +1721,12 @@ class DartMLDataDB
final List<EmbeddingVector> embeddings = [];
for (final result in results) {
+ final bytes = result[embeddingColumn] as Uint8List;
+ if (bytes.lengthInBytes != ClipVectorDB.embeddingBytesLength) continue;
final embedding = EmbeddingVector(
fileID: result[fileIDColumn],
- embedding: Float32List.view(result[embeddingColumn].buffer),
+ embedding: Float32List.sublistView(bytes),
);
- if (embedding.isEmpty) continue;
embeddings.add(embedding);
}
return embeddings;
diff --git a/mobile/apps/photos/lib/db/ml/rust_db.dart b/mobile/apps/photos/lib/db/ml/rust_db.dart
index 8742ff84a5..0256521322 100644
--- a/mobile/apps/photos/lib/db/ml/rust_db.dart
+++ b/mobile/apps/photos/lib/db/ml/rust_db.dart
@@ -593,7 +593,12 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {
Future<List<EmbeddingVector>> getAllClipVectors() async {
final db = await _db;
final rows = await db.getAllClipVectors();
- return rows.map(mappers.toEmbeddingVector).toList();
+ return rows
+ .where(
+ (row) => row.embedding.length == ClipVectorDB.embeddingDimensions,
+ )
+ .map(mappers.toEmbeddingVector)
+ .toList();
}
@override