Skip to content

armgit / ente

Public Imported from GitHub
0

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

Move Photos model acquisition to Rust assets

laurenspriem9c029d210871
diff --git a/mobile/apps/photos/changes/ml-model-cache.md b/mobile/apps/photos/changes/ml-model-cache.md
new file mode 100644
index 0000000000..ec72bf0f7a
--- /dev/null
+++ b/mobile/apps/photos/changes/ml-model-cache.md
@@ -0,0 +1 @@
+- Improved ML model download reliability and reuse of existing downloaded models.
diff --git a/mobile/apps/photos/integration_test/ml_parity_shared.dart b/mobile/apps/photos/integration_test/ml_parity_shared.dart
index 80c1eb1020..4b97bfeacf 100644
--- a/mobile/apps/photos/integration_test/ml_parity_shared.dart
+++ b/mobile/apps/photos/integration_test/ml_parity_shared.dart
@@ -10,9 +10,9 @@ import "package:photos/core/configuration.dart";
import "package:photos/core/network/network.dart";
import "package:photos/service_locator.dart";
import "package:photos/services/machine_learning/ml_indexing_isolate.dart";
-import "package:photos/services/machine_learning/ml_model.dart";
-import "package:photos/services/machine_learning/ml_model_assets.dart";
import "package:photos/services/machine_learning/ml_result.dart";
+import "package:photos/src/rust/api/ml_indexing_api.dart" as rust_ml;
+import "package:photos/src/rust/frb_generated.dart" show EntePhotosRust;
import "package:photos/utils/isolate/isolate_operations.dart";
import "package:shared_preferences/shared_preferences.dart";
@@ -44,9 +44,14 @@ class _ManifestItem {
class _ModelSpec {
final String schemaName;
- final MlModelAsset model;
+ final String fileName;
+ final String sha256;
- const _ModelSpec({required this.schemaName, required this.model});
+ const _ModelSpec({
+ required this.schemaName,
+ required this.fileName,
+ required this.sha256,
+ });
}
void runMLParityIntegrationTest({required String expectedPlatform}) {
@@ -169,37 +174,51 @@ List<_ManifestItem> _loadManifestItems() {
}
List<_ModelSpec> _modelSpecs() {
- return [
+ return const [
_ModelSpec(
schemaName: "face_detection",
- model: FaceDetectionModel.instance,
+ fileName: "yolov5s_face_640_640_static_b1.onnx",
+ sha256:
+ "e047647409403d52696035ecd445792173e50d7fbdcccac97b958a585db9aa3d",
),
_ModelSpec(
schemaName: "face_embedding",
- model: FaceEmbeddingModel.instance,
+ fileName: "mobilefacenet_portable_static_b1.onnx",
+ sha256:
+ "0763fc33f54e138476194da95987e133b3e976075a6b1d3e1b2caedb251b1a36",
+ ),
+ _ModelSpec(
+ schemaName: "clip",
+ fileName: "mobileclip_s2_image_gelu_opset20.onnx",
+ sha256:
+ "205a430af825e501c5138e5bb9abea942482a7a4fd4a680e98e47cf0830dce7e",
),
- _ModelSpec(schemaName: "clip", model: ClipImageModel.instance),
];
}
class _LoadedModels {
final Map<String, String> modelMetadata;
- final Map<String, String> modelPathsBySchema;
+ final String assetsDir;
- const _LoadedModels({
- required this.modelMetadata,
- required this.modelPathsBySchema,
- });
+ const _LoadedModels({required this.modelMetadata, required this.assetsDir});
}
Future<_LoadedModels> _downloadModels(List<_ModelSpec> modelSpecs) async {
await _ensureModelNetworkContext();
final modelMetadata = <String, String>{};
- final modelPathsBySchema = <String, String>{};
+ final assetsDir = "${(await getApplicationSupportDirectory()).path}/assets";
+ await EntePhotosRust.init();
+ await rust_ml.initMlRuntime(
+ assetsDir: assetsDir,
+ runFaces: true,
+ runClip: true,
+ runPets: false,
+ );
for (final modelSpec in modelSpecs) {
- final modelPath = await modelSpec.model.getModelPath();
+ final key = modelSpec.fileName.substring(0, modelSpec.fileName.length - 5);
+ final modelPath = "$assetsDir/models/$key/${modelSpec.fileName}";
final modelFile = File(modelPath);
if (!modelFile.existsSync()) {
throw StateError(
@@ -207,23 +226,18 @@ Future<_LoadedModels> _downloadModels(List<_ModelSpec> modelSpecs) async {
);
}
- modelPathsBySchema[modelSpec.schemaName] = modelFile.path;
final modelSHA256 = await _sha256HexOfFile(modelFile);
- if (modelSHA256.toLowerCase() !=
- modelSpec.model.modelSha256.toLowerCase()) {
+ if (modelSHA256.toLowerCase() != modelSpec.sha256.toLowerCase()) {
throw StateError(
"Model SHA mismatch for ${modelSpec.schemaName}: "
- "expected ${modelSpec.model.modelSha256}, got $modelSHA256",
+ "expected ${modelSpec.sha256}, got $modelSHA256",
);
}
modelMetadata[modelSpec.schemaName] =
"${modelFile.uri.pathSegments.last}:$modelSHA256";
}
- return _LoadedModels(
- modelMetadata: modelMetadata,
- modelPathsBySchema: modelPathsBySchema,
- );
+ return _LoadedModels(modelMetadata: modelMetadata, assetsDir: assetsDir);
}
bool _modelNetworkContextInitialized = false;
@@ -283,7 +297,7 @@ Future<void> _stageModelsFromLocalMirror(
await assetsDir.create(recursive: true);
for (final modelSpec in modelSpecs) {
- final canonicalURL = modelSpec.model.modelRemotePath;
+ final canonicalURL = "https://models.ente.com/${modelSpec.fileName}";
final modelFile = Uri.parse(canonicalURL).pathSegments.last;
final targetPath =
"${assetsDir.path}/${_remoteAssetPathToLocalFileName(canonicalURL)}";
@@ -433,23 +447,7 @@ Future<MLResult> _analyzeImage({
"runClip": true,
};
- final faceDetectionModelPath =
- loadedModels.modelPathsBySchema["face_detection"];
- final faceEmbeddingModelPath =
- loadedModels.modelPathsBySchema["face_embedding"];
- final clipImageModelPath = loadedModels.modelPathsBySchema["clip"];
- if (faceDetectionModelPath == null ||
- faceEmbeddingModelPath == null ||
- clipImageModelPath == null) {
- throw StateError(
- "Missing model paths for Rust ML parity route: face_detection/face_embedding/clip",
- );
- }
- args.addAll({
- "faceDetectionModelPath": faceDetectionModelPath,
- "faceEmbeddingModelPath": faceEmbeddingModelPath,
- "clipImageModelPath": clipImageModelPath,
- });
+ args["assetsDir"] = loadedModels.assetsDir;
final resultJSONString =
await MLIndexingIsolate.instance.runInIsolate(
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_computer.dart b/mobile/apps/photos/lib/services/machine_learning/ml_computer.dart
index c449a9725e..2e53a26a10 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_computer.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_computer.dart
@@ -7,14 +7,15 @@ import "package:logging/logging.dart";
import "package:photos/core/errors.dart";
import "package:photos/db/ml/db.dart";
import "package:photos/models/ml/vector.dart";
+import "package:photos/service_locator.dart" show isLocalGalleryMode;
import "package:photos/services/machine_learning/ml_constants.dart";
-import "package:photos/services/machine_learning/ml_model_assets.dart";
+import "package:photos/services/machine_learning/ml_model_download_service.dart";
import "package:photos/services/machine_learning/semantic_search/query_result.dart";
import "package:photos/services/machine_learning/webgpu_execution_policy.dart";
-import "package:photos/services/remote_assets_service.dart";
+import "package:photos/src/rust/api/ml_indexing_api.dart" as rust_ml;
import "package:photos/utils/isolate/isolate_operations.dart";
import "package:photos/utils/isolate/super_isolate.dart";
-import "package:synchronized/synchronized.dart";
+import "package:photos/utils/network_util.dart";
@pragma('vm:entry-point')
class MLComputer extends SuperIsolate {
@@ -22,9 +23,6 @@ class MLComputer extends SuperIsolate {
Logger get logger => _logger;
final _logger = Logger('MLComputer');
- final _initModelLock = Lock();
- String? _clipTextModelPath;
- String? _clipTextVocabPath;
Future<void>? _clipTextWarmupFuture;
@override
@@ -75,24 +73,22 @@ class MLComputer extends SuperIsolate {
Future<List<double>> runClipText(String query) async {
try {
- await _ensureLoadedClipTextModel();
- final modelPath = _clipTextModelPath;
- final vocabPath = _clipTextVocabPath;
- if (modelPath == null || modelPath.trim().isEmpty) {
- throw Exception(
- "RustMLMissingModelPath: Missing required model path: clipTextModelPath",
- );
- }
- if (vocabPath == null || vocabPath.trim().isEmpty) {
- throw Exception(
- "RustMLMissingModelPath: Missing required model path: clipTextVocabPath",
+ final assetsDir = await MLModelDownloadService.instance
+ .getAssetsDirectory();
+ final hasModel = await rust_ml.isClipTextDownloaded(
+ assetsDir: assetsDir,
+ includeVocab: false,
+ );
+ if (!hasModel && !(isLocalGalleryMode || await canUseHighBandwidth())) {
+ throw WiFiUnavailableError(
+ "Could not download clip text model because high bandwidth "
+ "connectivity is unavailable",
);
}
final enableWebGpu = await webGpuExecutionPolicy.isEligible();
final isolateResult = await runInIsolate(IsolateOperation.runClipText, {
"text": query,
- "clipTextModelPath": modelPath,
- "clipTextVocabPath": vocabPath,
+ "assetsDir": assetsDir,
"enableWebGpu": enableWebGpu,
});
if (isolateResult is RustCorruptModelException) {
@@ -131,37 +127,6 @@ class MLComputer extends SuperIsolate {
}
}
- Future<void> _ensureLoadedClipTextModel() async {
- return _initModelLock.synchronized(() async {
- try {
- if (_clipTextVocabPath == null) {
- final tokenizerRemotePath = ClipTextModel.instance.vocabRemotePath;
- _clipTextVocabPath = await RemoteAssetsService.instance.getAssetPath(
- tokenizerRemotePath,
- expectedSha256: ClipTextModel.instance.vocabSha256,
- );
- }
-
- if (_clipTextModelPath != null) {
- return;
- }
-
- final String? downloadedModelPath = await ClipTextModel.instance
- .downloadModelSafe();
- if (downloadedModelPath == null) {
- throw WiFiUnavailableError(
- "Could not download clip text model because high bandwidth "
- "connectivity is unavailable",
- );
- }
- _clipTextModelPath = downloadedModelPath;
- } catch (e, s) {
- _logger.severe("Could not load clip text model in MLComputer", e, s);
- rethrow;
- }
- });
- }
-
Future<Map<String, List<QueryResult>>> computeBulkSimilarities(
Map<String, List<double>> textQueryToEmbeddingMap,
Map<String, double> minimumSimilarityMap,
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_indexing_isolate.dart b/mobile/apps/photos/lib/services/machine_learning/ml_indexing_isolate.dart
index 39b5d2938f..1ceef1463a 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_indexing_isolate.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_indexing_isolate.dart
@@ -3,12 +3,10 @@ import "package:logging/logging.dart";
import "package:photos/models/ml/ml_versions.dart"
show mlIndexFlagCoreML, mlIndexFlagRuntimeRust, mlIndexFlagWebGPU;
import "package:photos/service_locator.dart" show flagService, localSettings;
-import "package:photos/services/machine_learning/ml_model_assets.dart";
import "package:photos/services/machine_learning/ml_model_download_service.dart";
-import "package:photos/services/machine_learning/ml_models_overview.dart";
import "package:photos/services/machine_learning/ml_result.dart";
import "package:photos/services/machine_learning/webgpu_execution_policy.dart";
-import "package:photos/services/remote_assets_service.dart";
+import "package:photos/src/rust/api/ml_indexing_api.dart" as rust_ml;
import "package:photos/utils/isolate/isolate_operations.dart";
import "package:photos/utils/isolate/super_isolate.dart";
import "package:photos/utils/ml_util.dart";
@@ -179,57 +177,19 @@ class MLIndexingIsolate extends SuperIsolate {
if (!MLModelDownloadService.instance.areIndexingModelsDownloaded) return;
if (delete) {
- final remoteModelPaths = <String>[
- for (final model in MLModels.values)
- if (model.isIndexingModel) model.model.modelRemotePath,
- ];
- await RemoteAssetsService.instance.cleanupSelectedModels(
- remoteModelPaths,
+ await rust_ml.removeIndexingModels(
+ assetsDir: await MLModelDownloadService.instance.getAssetsDirectory(),
);
MLModelDownloadService.instance.invalidateModelDownloadCache();
}
}
Future<Map<String, dynamic>> _buildRustRuntimeArgs() async {
- final faceDetectionPath = await FaceDetectionModel.instance.getModelPath();
- final faceEmbeddingPath = await FaceEmbeddingModel.instance.getModelPath();
- final clipImagePath = await ClipImageModel.instance.getModelPath();
-
- String petFaceDetectionPath = "";
- String petFaceEmbeddingDogPath = "";
- String petFaceEmbeddingCatPath = "";
- String petBodyDetectionPath = "";
- String petBodyEmbeddingDogPath = "";
- String petBodyEmbeddingCatPath = "";
-
- if (flagService.petEnabled && localSettings.petRecognitionEnabled) {
- petFaceDetectionPath = await PetFaceDetectionModel.instance
- .getModelPath();
- petFaceEmbeddingDogPath = await PetFaceEmbeddingDogModel.instance
- .getModelPath();
- petFaceEmbeddingCatPath = await PetFaceEmbeddingCatModel.instance
- .getModelPath();
- petBodyDetectionPath = await PetBodyDetectionModel.instance
- .getModelPath();
- petBodyEmbeddingDogPath = await PetBodyEmbeddingDogModel.instance
- .getModelPath();
- petBodyEmbeddingCatPath = await PetBodyEmbeddingCatModel.instance
- .getModelPath();
- }
-
return {
- // Sessions are lazy, so the inference call re-evaluates this app-side
- // policy before Rust applies its own durable crash canary.
+ "assetsDir": await MLModelDownloadService.instance.getAssetsDirectory(),
+ "preparePets":
+ flagService.petEnabled && localSettings.petRecognitionEnabled,
"enableWebGpu": await webGpuExecutionPolicy.isEligible(),
- "faceDetectionModelPath": faceDetectionPath,
- "faceEmbeddingModelPath": faceEmbeddingPath,
- "clipImageModelPath": clipImagePath,
- "petFaceDetectionModelPath": petFaceDetectionPath,
- "petFaceEmbeddingDogModelPath": petFaceEmbeddingDogPath,
- "petFaceEmbeddingCatModelPath": petFaceEmbeddingCatPath,
- "petBodyDetectionModelPath": petBodyDetectionPath,
- "petBodyEmbeddingDogModelPath": petBodyEmbeddingDogPath,
- "petBodyEmbeddingCatModelPath": petBodyEmbeddingCatPath,
};
}
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_model.dart b/mobile/apps/photos/lib/services/machine_learning/ml_model.dart
deleted file mode 100644
index 1e83663d44..0000000000
--- a/mobile/apps/photos/lib/services/machine_learning/ml_model.dart
+++ /dev/null
@@ -1,66 +0,0 @@
-import "package:logging/logging.dart";
-import "package:photos/service_locator.dart" show isLocalGalleryMode;
-import "package:photos/services/remote_assets_service.dart";
-import "package:photos/utils/network_util.dart";
-import "package:synchronized/synchronized.dart";
-
-abstract class MlModelAsset {
- static final _logger = Logger("MlModel");
-
- String get kModelBucketEndpoint => "https://models.ente.com/";
-
- String get modelRemotePath;
-
- String get modelSha256;
-
- final _downloadModelLock = Lock();
-
- // May download the model without checking canUseHighBandwidth.
- Future<String> getModelPath() async {
- return _downloadModelLock.synchronized(() async {
- return RemoteAssetsService.instance.getAssetPath(
- modelRemotePath,
- expectedSha256: modelSha256,
- );
- });
- }
-
- Future<String?> downloadModelSafe() async {
- if (await RemoteAssetsService.instance.hasAsset(modelRemotePath)) {
- return await RemoteAssetsService.instance.getAssetPath(
- modelRemotePath,
- expectedSha256: modelSha256,
- );
- } else {
- if (isLocalGalleryMode || await canUseHighBandwidth()) {
- return await downloadModel();
- } else {
- _logger.warning(
- 'Cannot return model path as it is not available locally and high bandwidth is not available.',
- );
- return null;
- }
- }
- }
-
- Future<String> downloadModel([bool forceRefresh = false]) async {
- return _downloadModelLock.synchronized(() async {
- if (forceRefresh) {
- final file = await RemoteAssetsService.instance.getAssetIfUpdated(
- modelRemotePath,
- expectedSha256: modelSha256,
- );
- return file?.path ??
- await RemoteAssetsService.instance.getAssetPath(
- modelRemotePath,
- expectedSha256: modelSha256,
- );
- } else {
- return await RemoteAssetsService.instance.getAssetPath(
- modelRemotePath,
- expectedSha256: modelSha256,
- );
- }
- });
- }
-}
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_model_assets.dart b/mobile/apps/photos/lib/services/machine_learning/ml_model_assets.dart
deleted file mode 100644
index 5e875dc204..0000000000
--- a/mobile/apps/photos/lib/services/machine_learning/ml_model_assets.dart
+++ /dev/null
@@ -1,157 +0,0 @@
-import "package:photos/services/machine_learning/ml_model.dart";
-
-class FaceDetectionModel extends MlModelAsset {
- static const remoteFileName = "yolov5s_face_640_640_static_b1.onnx";
- static const _sha256 =
- "e047647409403d52696035ecd445792173e50d7fbdcccac97b958a585db9aa3d";
-
- FaceDetectionModel._();
- static final instance = FaceDetectionModel._();
-
- @override
- String get modelRemotePath => kModelBucketEndpoint + remoteFileName;
-
- @override
- String get modelSha256 => _sha256;
-}
-
-class FaceEmbeddingModel extends MlModelAsset {
- static const remoteFileName = "mobilefacenet_portable_static_b1.onnx";
- static const _sha256 =
- "0763fc33f54e138476194da95987e133b3e976075a6b1d3e1b2caedb251b1a36";
-
- FaceEmbeddingModel._();
- static final instance = FaceEmbeddingModel._();
-
- @override
- String get modelRemotePath => kModelBucketEndpoint + remoteFileName;
-
- @override
- String get modelSha256 => _sha256;
-}
-
-class ClipImageModel extends MlModelAsset {
- static const remoteFileName = "mobileclip_s2_image_gelu_opset20.onnx";
- static const _sha256 =
- "205a430af825e501c5138e5bb9abea942482a7a4fd4a680e98e47cf0830dce7e";
-
- ClipImageModel._();
- static final instance = ClipImageModel._();
-
- @override
- String get modelRemotePath => kModelBucketEndpoint + remoteFileName;
-
- @override
- String get modelSha256 => _sha256;
-}
-
-class ClipTextModel extends MlModelAsset {
- static const remoteFileName = "mobileclip_s2_text_opset18_quant.onnx";
- static const _sha256 =
- "d92f33dfcff83077fc2e0d3414250710efbb51795dfd89767bdbefb5fdc47322";
- static const _vocabFileName = "bpe_simple_vocab_16e6.txt";
- static const _vocabSha256 =
- "67603cfda2e032ad77b5f8808af37789d590db664b26df8705d2bf8b3c553fc8";
-
- ClipTextModel._();
- static final instance = ClipTextModel._();
-
- @override
- String get modelRemotePath => kModelBucketEndpoint + remoteFileName;
-
- @override
- String get modelSha256 => _sha256;
-
- String get vocabRemotePath => kModelBucketEndpoint + _vocabFileName;
- String get vocabSha256 => _vocabSha256;
-}
-
-class PetFaceDetectionModel extends MlModelAsset {
- static const remoteFileName = "yolov5s_pet_face_fp16_V2.onnx";
- static const _sha256 =
- "7876d97992eeb5f3a9f3b35eff5e0e133012928172a8b005093108d8c3ad2d1c";
-
- PetFaceDetectionModel._();
- static final instance = PetFaceDetectionModel._();
-
- @override
- String get modelRemotePath => kModelBucketEndpoint + remoteFileName;
-
- @override
- String get modelSha256 => _sha256;
-}
-
-class PetFaceEmbeddingDogModel extends MlModelAsset {
- static const remoteFileName = "dog_face_embedding128.onnx";
- static const _sha256 =
- "fb04d781eb1f7adf6ce3432dc0c5873f16cc051b5c98c14c754afb39e2b92462";
-
- PetFaceEmbeddingDogModel._();
- static final instance = PetFaceEmbeddingDogModel._();
-
- @override
- String get modelRemotePath => kModelBucketEndpoint + remoteFileName;
-
- @override
- String get modelSha256 => _sha256;
-}
-
-class PetFaceEmbeddingCatModel extends MlModelAsset {
- static const remoteFileName = "cat_face_embedding128.onnx";
- static const _sha256 =
- "32b10694a27f6404d2beaddbd64f07ad555f72dccb12ee60a7afe5dcf6aad6cd";
-
- PetFaceEmbeddingCatModel._();
- static final instance = PetFaceEmbeddingCatModel._();
-
- @override
- String get modelRemotePath => kModelBucketEndpoint + remoteFileName;
-
- @override
- String get modelSha256 => _sha256;
-}
-
-class PetBodyDetectionModel extends MlModelAsset {
- static const remoteFileName = "yolov5s_object_fp16.onnx";
- static const _sha256 =
- "113f0c18632eb2c4f6deebcd40eb01c676492e9b43923c2d336e1b4012fce9ef";
-
- PetBodyDetectionModel._();
- static final instance = PetBodyDetectionModel._();
-
- @override
- String get modelRemotePath => kModelBucketEndpoint + remoteFileName;
-
- @override
- String get modelSha256 => _sha256;
-}
-
-class PetBodyEmbeddingDogModel extends MlModelAsset {
- static const remoteFileName = "dog_body_embedding192.onnx";
- static const _sha256 =
- "1d85aa20358137e30f11c2d0baa9a2248b9997928d501fe15365d1fc57522770";
-
- PetBodyEmbeddingDogModel._();
- static final instance = PetBodyEmbeddingDogModel._();
-
- @override
- String get modelRemotePath => kModelBucketEndpoint + remoteFileName;
-
- @override
- String get modelSha256 => _sha256;
-}
-
-class PetBodyEmbeddingCatModel extends MlModelAsset {
- static const remoteFileName = "cat_body_embedding192.onnx";
- static const _sha256 =
- "62fb5891e61be69a96510d8ec56e7525a9541b0283e54574d27c86c9b4a26ddf";
-
- PetBodyEmbeddingCatModel._();
- static final instance = PetBodyEmbeddingCatModel._();
-
- @override
- String get modelRemotePath => kModelBucketEndpoint + remoteFileName;
-
- @override
- String get modelSha256 => _sha256;
-}
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_model_download_service.dart b/mobile/apps/photos/lib/services/machine_learning/ml_model_download_service.dart
index 4d692f465b..9457af7a51 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_model_download_service.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_model_download_service.dart
@@ -2,11 +2,12 @@ import "dart:async";
import "package:connectivity_plus/connectivity_plus.dart";
import "package:logging/logging.dart";
+import "package:path/path.dart" as p;
+import "package:path_provider/path_provider.dart";
import "package:photos/service_locator.dart"
show flagService, hasGrantedMLConsent, isLocalGalleryMode, localSettings;
-import "package:photos/services/machine_learning/ml_model_assets.dart";
-import "package:photos/services/machine_learning/ml_models_overview.dart";
import "package:photos/services/remote_assets_service.dart";
+import "package:photos/src/rust/api/ml_indexing_api.dart" as rust_ml;
import "package:photos/utils/network_util.dart";
import "package:synchronized/synchronized.dart";
@@ -14,6 +15,13 @@ class MLModelDownloadService {
final _logger = Logger("MLModelDownloadService");
final _downloadModelLock = Lock();
+ final _progressController = StreamController<(String, int, int)>.broadcast();
+
+ Stream<(String, int, int)> get progressStream => _progressController.stream;
+
+ Future<String> getAssetsDirectory() async =>
+ p.join((await getApplicationSupportDirectory()).path, "assets");
+
final _activeModelDownloadTriggers = <bool>{};
bool _areIndexingModelsDownloaded = false;
@@ -35,13 +43,10 @@ class MLModelDownloadService {
}
Future<bool> canLoadClipTextModel() async {
- final hasModel = await RemoteAssetsService.instance.hasAsset(
- ClipTextModel.instance.modelRemotePath,
- );
- final hasVocab = await RemoteAssetsService.instance.hasAsset(
- ClipTextModel.instance.vocabRemotePath,
- );
- if (hasModel && hasVocab) {
+ if (await rust_ml.isClipTextDownloaded(
+ assetsDir: await getAssetsDirectory(),
+ includeVocab: true,
+ )) {
return true;
}
return isLocalGalleryMode || await canUseHighBandwidth();
@@ -69,11 +74,9 @@ class MLModelDownloadService {
Future<void> ensureModelsDownloaded({
required bool onlyIndexingModels,
- bool forceRefresh = false,
}) async {
return _downloadModelLock.synchronized(() async {
- if (!forceRefresh &&
- areModelsDownloaded(onlyIndexingModels: onlyIndexingModels)) {
+ if (areModelsDownloaded(onlyIndexingModels: onlyIndexingModels)) {
return;
}
if (!hasGrantedMLConsent) {
@@ -96,17 +99,10 @@ class MLModelDownloadService {
return;
}
- final modelsToDownload = <Future<void>>[];
- if (forceRefresh || !_areIndexingModelsDownloaded) {
- modelsToDownload.addAll(_indexingModelDownloads(forceRefresh));
- }
- if (!onlyIndexingModels &&
- (forceRefresh || !_areNonIndexingModelsDownloaded)) {
- modelsToDownload.addAll(_nonIndexingModelDownloads(forceRefresh));
- }
- if (modelsToDownload.isEmpty) {
- return;
- }
+ final downloadIndexing = !_areIndexingModelsDownloaded;
+ final downloadText =
+ !onlyIndexingModels && !_areNonIndexingModelsDownloaded;
+ if (!downloadIndexing && !downloadText) return;
_logger.info(
onlyIndexingModels
@@ -114,7 +110,20 @@ class MLModelDownloadService {
: "Downloading all ML models",
);
try {
- await Future.wait(modelsToDownload);
+ await RemoteAssetsService.instance.cleanupOldModelsIfNeeded();
+ await for (final update in rust_ml.preloadMlModels(
+ assetsDir: await getAssetsDirectory(),
+ runFaces: downloadIndexing,
+ runClip: downloadIndexing,
+ runPets: downloadIndexing && _shouldDownloadPetModels,
+ includeClipText: downloadText,
+ )) {
+ _progressController.add((
+ update.model,
+ update.downloadedBytes.toInt(),
+ update.totalBytes?.toInt() ?? 0,
+ ));
+ }
} catch (e, s) {
_logger.warning(
"ML model download failed, will retry when high bandwidth "
@@ -127,7 +136,7 @@ class MLModelDownloadService {
);
rethrow;
}
- if (forceRefresh || !_areIndexingModelsDownloaded) {
+ if (!_areIndexingModelsDownloaded) {
_areIndexingModelsDownloaded = true;
}
if (!onlyIndexingModels) {
@@ -142,28 +151,6 @@ class MLModelDownloadService {
});
}
- List<Future<void>> _indexingModelDownloads(bool forceRefresh) {
- final models = <MLModels>[
- ...coreIndexingModels,
- if (_shouldDownloadPetModels) ...petIndexingModels,
- ];
- return [
- for (final model in models) model.model.downloadModel(forceRefresh),
- ];
- }
-
- List<Future<void>> _nonIndexingModelDownloads(bool forceRefresh) {
- return [
- for (final model in nonIndexingModels)
- model.model.downloadModel(forceRefresh),
- RemoteAssetsService.instance.getAssetPath(
- ClipTextModel.instance.vocabRemotePath,
- refetch: forceRefresh,
- expectedSha256: ClipTextModel.instance.vocabSha256,
- ),
- ];
- }
-
bool get _shouldDownloadPetModels {
return flagService.petEnabled && localSettings.petRecognitionEnabled;
}
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_models_overview.dart b/mobile/apps/photos/lib/services/machine_learning/ml_models_overview.dart
deleted file mode 100644
index f1b080784c..0000000000
--- a/mobile/apps/photos/lib/services/machine_learning/ml_models_overview.dart
+++ /dev/null
@@ -1,77 +0,0 @@
-import "package:photos/services/machine_learning/ml_model.dart";
-import "package:photos/services/machine_learning/ml_model_assets.dart";
-
-enum MLModels {
- faceDetection,
- faceEmbedding,
- clipImageEncoder,
- clipTextEncoder,
- petFaceDetection,
- petFaceEmbeddingDog,
- petFaceEmbeddingCat,
- petBodyDetection,
- petBodyEmbeddingDog,
- petBodyEmbeddingCat,
-}
-
-Iterable<MLModels> get petIndexingModels => const <MLModels>[
- MLModels.petFaceDetection,
- MLModels.petFaceEmbeddingDog,
- MLModels.petFaceEmbeddingCat,
- MLModels.petBodyDetection,
- MLModels.petBodyEmbeddingDog,
- MLModels.petBodyEmbeddingCat,
-];
-
-Iterable<MLModels> get coreIndexingModels => MLModels.values.where(
- (model) => model.isIndexingModel && !model.isPetIndexingModel,
-);
-
-Iterable<MLModels> get nonIndexingModels =>
- MLModels.values.where((model) => !model.isIndexingModel);
-
-extension MLModelsExtension on MLModels {
- MlModelAsset get model {
- switch (this) {
- case MLModels.faceDetection:
- return FaceDetectionModel.instance;
- case MLModels.faceEmbedding:
- return FaceEmbeddingModel.instance;
- case MLModels.clipImageEncoder:
- return ClipImageModel.instance;
- case MLModels.clipTextEncoder:
- return ClipTextModel.instance;
- case MLModels.petFaceDetection:
- return PetFaceDetectionModel.instance;
- case MLModels.petFaceEmbeddingDog:
- return PetFaceEmbeddingDogModel.instance;
- case MLModels.petFaceEmbeddingCat:
- return PetFaceEmbeddingCatModel.instance;
- case MLModels.petBodyDetection:
- return PetBodyDetectionModel.instance;
- case MLModels.petBodyEmbeddingDog:
- return PetBodyEmbeddingDogModel.instance;
- case MLModels.petBodyEmbeddingCat:
- return PetBodyEmbeddingCatModel.instance;
- }
- }
-
- bool get isIndexingModel {
- switch (this) {
- case MLModels.faceDetection:
- case MLModels.faceEmbedding:
- case MLModels.clipImageEncoder:
- case MLModels.petFaceDetection:
- case MLModels.petFaceEmbeddingDog:
- case MLModels.petFaceEmbeddingCat:
- case MLModels.petBodyDetection:
- case MLModels.petBodyEmbeddingDog:
- case MLModels.petBodyEmbeddingCat:
- return true;
- case MLModels.clipTextEncoder:
- return false;
- }
- }
-
- bool get isPetIndexingModel => petIndexingModels.contains(this);
-}
diff --git a/mobile/apps/photos/lib/ui/settings/ml/machine_learning_settings_page.dart b/mobile/apps/photos/lib/ui/settings/ml/machine_learning_settings_page.dart
index 77ef2d62a3..dab7c52207 100644
--- a/mobile/apps/photos/lib/ui/settings/ml/machine_learning_settings_page.dart
+++ b/mobile/apps/photos/lib/ui/settings/ml/machine_learning_settings_page.dart
@@ -12,12 +12,10 @@ import "package:photos/db/ml/db.dart";
import "package:photos/events/notification_event.dart";
import "package:photos/service_locator.dart";
import "package:photos/services/machine_learning/ml_indexing_isolate.dart";
-import "package:photos/services/machine_learning/ml_model_assets.dart";
import "package:photos/services/machine_learning/ml_model_download_service.dart";
import "package:photos/services/machine_learning/ml_run_control.dart";
import "package:photos/services/machine_learning/ml_service.dart";
import "package:photos/services/machine_learning/semantic_search/semantic_search_service.dart";
-import "package:photos/services/remote_assets_service.dart";
import "package:photos/services/wake_lock_service.dart";
import "package:photos/ui/common/web_page.dart";
import "package:photos/ui/settings/ml/ml_user_dev_screen.dart";
@@ -401,18 +399,18 @@ class _ModelLoadingStateState extends State<ModelLoadingState> {
@override
void initState() {
- _progressStream = RemoteAssetsService.instance.progressStream.listen((
+ _progressStream = MLModelDownloadService.instance.progressStream.listen((
event,
) {
- final String url = event.$1;
+ final String model = event.$1;
String title = "";
- if (url.contains(ClipImageModel.remoteFileName)) {
+ if (model == "clip-image") {
title = "Image Model";
- } else if (url.contains(ClipTextModel.remoteFileName)) {
+ } else if (model == "clip-text") {
title = "Text Model";
- } else if (url.contains(FaceDetectionModel.remoteFileName)) {
+ } else if (model == "face-detection") {
title = "Face Detection Model";
- } else if (url.contains(FaceEmbeddingModel.remoteFileName)) {
+ } else if (model == "face-embedding") {
title = "Face Embedding Model";
}
if (title.isNotEmpty) {
diff --git a/mobile/apps/photos/lib/utils/isolate/isolate_operations.dart b/mobile/apps/photos/lib/utils/isolate/isolate_operations.dart
index 8aa7a96963..a9c5051ce8 100644
--- a/mobile/apps/photos/lib/utils/isolate/isolate_operations.dart
+++ b/mobile/apps/photos/lib/utils/isolate/isolate_operations.dart
@@ -21,7 +21,7 @@ import "package:photos/utils/ml_util.dart";
final Map<String, dynamic> _isolateCache = {};
const _rustLibLoadedCacheKey = "rustLibLoaded";
-const _rustMlModelPathsCacheKey = "rustMlModelPaths";
+const _rustMlRuntimeConfigCacheKey = "rustMlRuntimeConfig";
class RustCorruptModelException implements Exception {
const RustCorruptModelException(this.modelPath);
@@ -139,23 +139,6 @@ Future<dynamic> isolateFunction(
case IsolateOperation.runClipText:
await _ensureRustLoaded();
final text = args["text"] as String;
- final clipTextModelPath = args["clipTextModelPath"] as String?;
- if (clipTextModelPath == null || clipTextModelPath.trim().isEmpty) {
- throw Exception(
- "RustMLMissingModelPath: Missing required model path: clipTextModelPath",
- );
- }
-
- final clipTextVocabPath = args["clipTextVocabPath"] as String?;
- if (clipTextVocabPath == null || clipTextVocabPath.trim().isEmpty) {
- throw Exception(
- "RustMLMissingModelPath: Missing required model path: clipTextVocabPath",
- );
- }
-
- // Configure execution behavior before the CLIP text session is
- // created; the session is process-global and cannot be reconfigured
- // once built.
await rust_ml.setMlExecutionConfig(
enableWebgpu: (args["enableWebGpu"] as bool?) ?? false,
);
@@ -165,8 +148,7 @@ Future<dynamic> isolateFunction(
result = await rust_ml.runClipTextRust(
req: rust_ml.RunClipTextRequest(
text: text,
- modelPath: clipTextModelPath,
- vocabPath: clipTextVocabPath,
+ assetsDir: args["assetsDir"] as String,
),
);
} on rust_ml.RustMlError_CorruptModel catch (e) {
@@ -340,47 +322,18 @@ Future<void> _ensureRustRuntimePrepared(Map<String, dynamic> args) async {
await rust_ml.setMlExecutionConfig(
enableWebgpu: (args["enableWebGpu"] as bool?) ?? false,
);
- final modelPaths = rust_ml.RustModelPaths(
- faceDetection: (args["faceDetectionModelPath"] as String?) ?? "",
- faceEmbedding: (args["faceEmbeddingModelPath"] as String?) ?? "",
- clipImage: (args["clipImageModelPath"] as String?) ?? "",
- clipText: (args["clipTextModelPath"] as String?) ?? "",
- petFaceDetection: (args["petFaceDetectionModelPath"] as String?) ?? "",
- petFaceEmbeddingDog:
- (args["petFaceEmbeddingDogModelPath"] as String?) ?? "",
- petFaceEmbeddingCat:
- (args["petFaceEmbeddingCatModelPath"] as String?) ?? "",
- petBodyDetection: (args["petBodyDetectionModelPath"] as String?) ?? "",
- petBodyEmbeddingDog:
- (args["petBodyEmbeddingDogModelPath"] as String?) ?? "",
- petBodyEmbeddingCat:
- (args["petBodyEmbeddingCatModelPath"] as String?) ?? "",
+ final assetsDir = args["assetsDir"] as String;
+ final preparePets = (args["preparePets"] as bool?) ?? false;
+ final runtimeConfigKey = "$assetsDir|$preparePets";
+ if (_isolateCache[_rustMlRuntimeConfigCacheKey] == runtimeConfigKey) return;
+
+ await rust_ml.initMlRuntime(
+ assetsDir: assetsDir,
+ runFaces: true,
+ runClip: true,
+ runPets: preparePets,
);
- final modelPathsKey = _modelPathsCacheKey(modelPaths);
- final currentModelPathsKey =
- _isolateCache[_rustMlModelPathsCacheKey] as String?;
- if (currentModelPathsKey == modelPathsKey) {
- return;
- }
-
- final missingModelPaths = <String>[];
- if (modelPaths.faceDetection.trim().isEmpty) {
- missingModelPaths.add("faceDetectionModelPath");
- }
- if (modelPaths.faceEmbedding.trim().isEmpty) {
- missingModelPaths.add("faceEmbeddingModelPath");
- }
- if (modelPaths.clipImage.trim().isEmpty) {
- missingModelPaths.add("clipImageModelPath");
- }
- if (missingModelPaths.isNotEmpty) {
- throw Exception(
- "RustMLMissingModelPath: Missing required model paths: ${missingModelPaths.join(', ')}",
- );
- }
-
- await rust_ml.initMlRuntime(modelPaths: modelPaths);
- _isolateCache[_rustMlModelPathsCacheKey] = modelPathsKey;
+ _isolateCache[_rustMlRuntimeConfigCacheKey] = runtimeConfigKey;
}
Future<void> _releaseRustRuntime() async {
@@ -393,20 +346,5 @@ Future<void> _releaseRustRuntime() async {
} catch (_) {
// no-op: indexing-model release is best-effort.
}
- _isolateCache.remove(_rustMlModelPathsCacheKey);
-}
-
-String _modelPathsCacheKey(rust_ml.RustModelPaths modelPaths) {
- return [
- modelPaths.faceDetection,
- modelPaths.faceEmbedding,
- modelPaths.clipImage,
- modelPaths.clipText,
- modelPaths.petFaceDetection,
- modelPaths.petFaceEmbeddingDog,
- modelPaths.petFaceEmbeddingCat,
- modelPaths.petBodyDetection,
- modelPaths.petBodyEmbeddingDog,
- modelPaths.petBodyEmbeddingCat,
- ].join("|");
+ _isolateCache.remove(_rustMlRuntimeConfigCacheKey);
}
diff --git a/mobile/apps/photos/lib/utils/ml_util.dart b/mobile/apps/photos/lib/utils/ml_util.dart
index acc35663ec..560f9581f3 100644
--- a/mobile/apps/photos/lib/utils/ml_util.dart
+++ b/mobile/apps/photos/lib/utils/ml_util.dart
@@ -799,82 +799,10 @@ Future<MLResult> analyzeImageRust(Map args) async {
final bool runFaces = args["runFaces"] as bool;
final bool runClip = args["runClip"] as bool;
final bool runPets = args["runPets"] as bool? ?? false;
- final String? faceDetectionModelPath =
- args["faceDetectionModelPath"] as String?;
- final String? faceEmbeddingModelPath =
- args["faceEmbeddingModelPath"] as String?;
- final String? clipImageModelPath = args["clipImageModelPath"] as String?;
- final String? petFaceDetectionModelPath =
- args["petFaceDetectionModelPath"] as String?;
- final String? petFaceEmbeddingDogModelPath =
- args["petFaceEmbeddingDogModelPath"] as String?;
- final String? petFaceEmbeddingCatModelPath =
- args["petFaceEmbeddingCatModelPath"] as String?;
- final String? petBodyDetectionModelPath =
- args["petBodyDetectionModelPath"] as String?;
- final String? petBodyEmbeddingDogModelPath =
- args["petBodyEmbeddingDogModelPath"] as String?;
- final String? petBodyEmbeddingCatModelPath =
- args["petBodyEmbeddingCatModelPath"] as String?;
- bool isMissingModelPath(String? path) =>
- path == null || path.trim().isEmpty;
- final missingModelPaths = <String>[];
- if (runFaces) {
- if (isMissingModelPath(faceDetectionModelPath)) {
- missingModelPaths.add("faceDetectionModelPath");
- }
- if (isMissingModelPath(faceEmbeddingModelPath)) {
- missingModelPaths.add("faceEmbeddingModelPath");
- }
- }
- if (runClip && isMissingModelPath(clipImageModelPath)) {
- missingModelPaths.add("clipImageModelPath");
- }
- if (runPets) {
- if (isMissingModelPath(petFaceDetectionModelPath)) {
- missingModelPaths.add("petFaceDetectionModelPath");
- }
- if (isMissingModelPath(petFaceEmbeddingDogModelPath)) {
- missingModelPaths.add("petFaceEmbeddingDogModelPath");
- }
- if (isMissingModelPath(petFaceEmbeddingCatModelPath)) {
- missingModelPaths.add("petFaceEmbeddingCatModelPath");
- }
- if (isMissingModelPath(petBodyDetectionModelPath)) {
- missingModelPaths.add("petBodyDetectionModelPath");
- }
- if (isMissingModelPath(petBodyEmbeddingDogModelPath)) {
- missingModelPaths.add("petBodyEmbeddingDogModelPath");
- }
- if (isMissingModelPath(petBodyEmbeddingCatModelPath)) {
- missingModelPaths.add("petBodyEmbeddingCatModelPath");
- }
- }
- if (missingModelPaths.isNotEmpty) {
- throw Exception(
- "RustMLMissingModelPath: Missing required model paths: ${missingModelPaths.join(', ')}",
- );
- }
-
- // The Rust runtime creates sessions lazily, so configure execution
- // behavior here as well in case the runtime was not prepared explicitly
- // in this isolate.
await rust_ml.setMlExecutionConfig(
enableWebgpu: (args["enableWebGpu"] as bool?) ?? false,
);
- final modelPaths = rust_ml.RustModelPaths(
- faceDetection: faceDetectionModelPath ?? "",
- faceEmbedding: faceEmbeddingModelPath ?? "",
- clipImage: clipImageModelPath ?? "",
- clipText: "",
- petFaceDetection: petFaceDetectionModelPath ?? "",
- petFaceEmbeddingDog: petFaceEmbeddingDogModelPath ?? "",
- petFaceEmbeddingCat: petFaceEmbeddingCatModelPath ?? "",
- petBodyDetection: petBodyDetectionModelPath ?? "",
- petBodyEmbeddingDog: petBodyEmbeddingDogModelPath ?? "",
- petBodyEmbeddingCat: petBodyEmbeddingCatModelPath ?? "",
- );
Future<rust_ml.AnalyzeImageResult> runRustAnalyzeForPath(
String analyzePath,
) {
@@ -885,7 +813,7 @@ Future<MLResult> analyzeImageRust(Map args) async {
runFaces: runFaces,
runClip: runClip,
runPets: runPets,
- modelPaths: modelPaths,
+ assetsDir: args["assetsDir"] as String,
),
);
}
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index e6dd98da36..59e37a7b4d 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -2938,6 +2938,7 @@ dependencies = [
"ente-vecdb",
"flutter_rust_bridge",
"log",
+ "tokio",
]
[[package]]
diff --git a/rust/bindings/frb/photos/Cargo.toml b/rust/bindings/frb/photos/Cargo.toml
index ac1b6f50cb..7af52a8924 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
+tokio = { workspace = true, features = ["rt"] }
[lints]
workspace = true
diff --git a/rust/bindings/frb/photos/src/api/ml_indexing_api.rs b/rust/bindings/frb/photos/src/api/ml_indexing_api.rs
index b75d3024a9..4d27bbc379 100644
--- a/rust/bindings/frb/photos/src/api/ml_indexing_api.rs
+++ b/rust/bindings/frb/photos/src/api/ml_indexing_api.rs
@@ -1,18 +1,10 @@
-use ente_ml::{ModelPaths, error::MlError, indexing, types};
+use std::path::Path;
-#[derive(Clone, Debug)]
-pub struct RustModelPaths {
- pub face_detection: String,
- pub face_embedding: String,
- pub clip_image: String,
- pub clip_text: String,
- pub pet_face_detection: String,
- pub pet_face_embedding_dog: String,
- pub pet_face_embedding_cat: String,
- pub pet_body_detection: String,
- pub pet_body_embedding_dog: String,
- pub pet_body_embedding_cat: String,
-}
+use ente_assets::AssetStore;
+use ente_ml::{assets, error::MlError, indexing, types};
+
+#[cfg(any(feature = "flutter", frb_expand))]
+use crate::frb_generated::StreamSink;
#[derive(Clone, Debug)]
pub struct AnalyzeImageRequest {
@@ -21,7 +13,7 @@ pub struct AnalyzeImageRequest {
pub run_faces: bool,
pub run_clip: bool,
pub run_pets: bool,
- pub model_paths: RustModelPaths,
+ pub assets_dir: String,
}
#[derive(Clone, Debug)]
@@ -116,8 +108,14 @@ pub struct AnalyzeImageResult {
#[derive(Clone, Debug)]
pub struct RunClipTextRequest {
pub text: String,
- pub model_path: String,
- pub vocab_path: String,
+ pub assets_dir: String,
+}
+
+#[derive(Clone, Debug)]
+pub struct ModelDownloadProgress {
+ pub model: String,
+ pub downloaded_bytes: u64,
+ pub total_bytes: Option<u64>,
}
#[derive(Clone, Debug)]
@@ -131,15 +129,42 @@ pub fn set_ml_execution_config(enable_webgpu: bool) {
indexing::set_ml_execution_config(enable_webgpu);
}
-pub fn init_ml_runtime(model_paths: RustModelPaths) {
- indexing::init_ml_runtime(to_model_paths(&model_paths));
+pub async fn init_ml_runtime(
+ assets_dir: String,
+ run_faces: bool,
+ run_clip: bool,
+ run_pets: bool,
+) -> Result<(), RustMlError> {
+ let store = AssetStore::new(&assets_dir);
+ let paths = assets::ensure_indexing_models(
+ &store,
+ Path::new(&assets_dir),
+ run_faces,
+ run_clip,
+ run_pets,
+ )
+ .await?;
+ tokio::task::spawn_blocking(move || indexing::init_ml_runtime(paths))
+ .await
+ .map_err(task_error)
}
pub fn release_ml_runtime() {
indexing::release_ml_runtime();
}
-pub fn analyze_image_rust(req: AnalyzeImageRequest) -> Result<AnalyzeImageResult, RustMlError> {
+pub async fn analyze_image_rust(
+ req: AnalyzeImageRequest,
+) -> Result<AnalyzeImageResult, RustMlError> {
+ let store = AssetStore::new(&req.assets_dir);
+ let model_paths = assets::ensure_indexing_models(
+ &store,
+ Path::new(&req.assets_dir),
+ req.run_faces,
+ req.run_clip,
+ req.run_pets,
+ )
+ .await?;
let shared_req = indexing::AnalyzeImageRequest {
file_id: req.file_id,
source: indexing::ImageSource::Path(req.image_path),
@@ -147,22 +172,28 @@ pub fn analyze_image_rust(req: AnalyzeImageRequest) -> Result<AnalyzeImageResult
run_clip: req.run_clip,
run_pets: req.run_pets,
generate_face_crops: false,
- model_paths: to_model_paths(&req.model_paths),
+ model_paths,
};
- indexing::analyze_image(shared_req)
+ tokio::task::spawn_blocking(move || indexing::analyze_image(shared_req))
+ .await
+ .map_err(task_error)?
.map(to_api_analyze_image_result)
.map_err(RustMlError::from)
}
-pub fn run_clip_text_rust(req: RunClipTextRequest) -> Result<RunClipTextResult, RustMlError> {
+pub async fn run_clip_text_rust(req: RunClipTextRequest) -> Result<RunClipTextResult, RustMlError> {
+ let store = AssetStore::new(&req.assets_dir);
+ let paths = assets::ensure_clip_text(&store, Path::new(&req.assets_dir)).await?;
let shared_req = indexing::RunClipTextRequest {
text: req.text,
- model_path: req.model_path,
- vocab_path: req.vocab_path,
+ model_path: paths.model.to_string_lossy().into_owned(),
+ vocab_path: paths.vocab.to_string_lossy().into_owned(),
};
- indexing::run_clip_text(shared_req)
+ tokio::task::spawn_blocking(move || indexing::run_clip_text(shared_req))
+ .await
+ .map_err(task_error)?
.map(|result| RunClipTextResult {
embedding: result
.embedding
@@ -177,18 +208,67 @@ pub fn tokenize_clip_text_rust(text: String, vocab_path: String) -> Result<Vec<i
indexing::tokenize_clip_text(&text, &vocab_path).map_err(|e| e.to_string())
}
-fn to_model_paths(paths: &RustModelPaths) -> ModelPaths {
- ModelPaths {
- face_detection: paths.face_detection.clone(),
- face_embedding: paths.face_embedding.clone(),
- clip_image: paths.clip_image.clone(),
- clip_text: paths.clip_text.clone(),
- pet_face_detection: paths.pet_face_detection.clone(),
- pet_face_embedding_dog: paths.pet_face_embedding_dog.clone(),
- pet_face_embedding_cat: paths.pet_face_embedding_cat.clone(),
- pet_body_detection: paths.pet_body_detection.clone(),
- pet_body_embedding_dog: paths.pet_body_embedding_dog.clone(),
- pet_body_embedding_cat: paths.pet_body_embedding_cat.clone(),
+#[cfg(any(feature = "flutter", frb_expand))]
+pub async fn preload_ml_models(
+ assets_dir: String,
+ run_faces: bool,
+ run_clip: bool,
+ run_pets: bool,
+ include_clip_text: bool,
+ progress: StreamSink<ModelDownloadProgress>,
+) {
+ let store = AssetStore::new(&assets_dir);
+ let mut selected = assets::indexing_assets(run_faces, run_clip, run_pets);
+ let mut labels = Vec::new();
+ if run_faces {
+ labels.extend(["face-detection", "face-embedding"]);
+ }
+ if run_clip {
+ labels.push("clip-image");
+ }
+ if run_pets {
+ labels.extend([
+ "pet-face-detection",
+ "pet-body-detection",
+ "pet-face-embedding-dog",
+ "pet-face-embedding-cat",
+ "pet-body-embedding-dog",
+ "pet-body-embedding-cat",
+ ]);
+ }
+ if include_clip_text {
+ selected.push(assets::clip_text_asset());
+ labels.push("clip-text");
+ }
+ if let Err(error) =
+ assets::ensure_mobile_models(&store, Path::new(&assets_dir), &selected, |update| {
+ let _ = progress.add(ModelDownloadProgress {
+ model: labels[update.asset_index].to_string(),
+ downloaded_bytes: update.asset_progress.downloaded_bytes,
+ total_bytes: update.asset_progress.total_bytes,
+ });
+ })
+ .await
+ {
+ let _ = progress.add_error(error.to_string());
+ }
+}
+
+pub fn is_clip_text_downloaded(assets_dir: String, include_vocab: bool) -> bool {
+ assets::is_clip_text_available(
+ &AssetStore::new(&assets_dir),
+ Path::new(&assets_dir),
+ include_vocab,
+ )
+}
+
+pub fn remove_indexing_models(assets_dir: String) -> Result<(), String> {
+ assets::remove_mobile_indexing_models(&AssetStore::new(&assets_dir), Path::new(&assets_dir))
+}
+
+fn task_error(error: tokio::task::JoinError) -> RustMlError {
+ RustMlError::Other {
+ message: error.to_string(),
}
}
diff --git a/rust/crates/assets/src/store.rs b/rust/crates/assets/src/store.rs
index 274bbaa01f..88363f67d2 100644
--- a/rust/crates/assets/src/store.rs
+++ b/rust/crates/assets/src/store.rs
@@ -1,7 +1,7 @@
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};
-use std::sync::{Arc, OnceLock};
+use std::sync::{Arc, OnceLock, Weak};
use futures_util::future::join_all;
use tokio::sync::{Mutex, Semaphore};
@@ -99,6 +99,10 @@ impl Asset {
pub struct AssetStore {
root: PathBuf,
+ coordination: Arc<StoreCoordination>,
+}
+
+struct StoreCoordination {
downloader: OnceLock<Downloader>,
asset_locks: std::sync::Mutex<HashMap<Vec<String>, Arc<Mutex<()>>>>,
asset_slots: Semaphore,
@@ -106,12 +110,32 @@ pub struct AssetStore {
impl AssetStore {
pub fn new(root: impl Into<PathBuf>) -> Self {
- Self {
- root: root.into(),
- downloader: OnceLock::new(),
- asset_locks: std::sync::Mutex::new(HashMap::new()),
- asset_slots: Semaphore::new(ASSET_CONCURRENCY),
- }
+ let root = root.into();
+ let registry_root = normalized_root(root.clone());
+ static STORES: OnceLock<std::sync::Mutex<HashMap<PathBuf, Weak<StoreCoordination>>>> =
+ OnceLock::new();
+ #[expect(
+ clippy::expect_used,
+ reason = "The registry critical section invokes no caller code"
+ )]
+ let mut stores = STORES
+ .get_or_init(Default::default)
+ .lock()
+ .expect("store registry");
+ stores.retain(|_, store| store.strong_count() > 0);
+ let coordination = stores
+ .get(&registry_root)
+ .and_then(Weak::upgrade)
+ .unwrap_or_else(|| {
+ let coordination = Arc::new(StoreCoordination {
+ downloader: OnceLock::new(),
+ asset_locks: std::sync::Mutex::new(HashMap::new()),
+ asset_slots: Semaphore::new(ASSET_CONCURRENCY),
+ });
+ stores.insert(registry_root, Arc::downgrade(&coordination));
+ coordination
+ });
+ Self { root, coordination }
}
pub fn asset_dir(&self, asset: &Asset) -> PathBuf {
@@ -127,6 +151,11 @@ impl AssetStore {
}
}
+ pub fn staged_file_path(&self, asset: &Asset, name: &str) -> Option<PathBuf> {
+ self.file_path(asset, name)?;
+ Some(self.staging_dir(asset).join(name))
+ }
+
pub fn is_downloaded(&self, asset: &Asset) -> bool {
let directory = self.asset_dir(asset);
match &asset.kind {
@@ -158,6 +187,17 @@ impl AssetStore {
assets: &[Asset],
on_progress: impl FnMut(AssetDownloadProgress) + Send,
cancellation: CancellationToken,
+ ) -> Result<(), Error> {
+ self.download_with_staged_import(assets, on_progress, cancellation, |_, _| Ok(()))
+ .await
+ }
+
+ pub async fn download_with_staged_import(
+ &self,
+ assets: &[Asset],
+ on_progress: impl FnMut(AssetDownloadProgress) + Send,
+ cancellation: CancellationToken,
+ import: impl Fn(&Asset, &Path) -> Result<(), Error> + Sync,
) -> Result<(), Error> {
if cancellation.is_cancelled() {
return Err(Error::Cancelled);
@@ -183,7 +223,14 @@ impl AssetStore {
.zip(locks)
.enumerate()
.map(|(asset_index, (asset, lock))| {
- self.download_asset(asset_index, asset, lock, &progress, cancellation.clone())
+ self.download_asset(
+ asset_index,
+ asset,
+ lock,
+ &progress,
+ cancellation.clone(),
+ &import,
+ )
});
let results = join_all(jobs).await;
if cancellation.is_cancelled() {
@@ -207,16 +254,18 @@ impl AssetStore {
keys.iter().try_for_each(|key| self.remove_key(key))
}
- async fn download_asset<F>(
+ async fn download_asset<F, I>(
&self,
asset_index: usize,
asset: &Asset,
lock: Arc<Mutex<()>>,
progress: &std::sync::Mutex<BatchProgress<F>>,
cancellation: CancellationToken,
+ import: &I,
) -> Result<(), Error>
where
F: FnMut(AssetDownloadProgress) + Send,
+ I: Fn(&Asset, &Path) -> Result<(), Error> + Sync,
{
let _guard = tokio::select! {
guard = lock.lock() => guard,
@@ -230,12 +279,13 @@ impl AssetStore {
progress.lock().expect("progress lock").skip(asset_index);
return Ok(());
}
+ import(asset, &self.staging_dir(asset))?;
#[expect(
clippy::expect_used,
reason = "The private asset semaphore is never closed"
)]
let _slot = tokio::select! {
- slot = self.asset_slots.acquire() => slot.expect("asset semaphore closed"),
+ slot = self.coordination.asset_slots.acquire() => slot.expect("asset semaphore closed"),
_ = cancellation.cancelled() => return Err(Error::Cancelled),
};
@@ -271,7 +321,11 @@ impl AssetStore {
clippy::expect_used,
reason = "The registry critical section invokes no caller code"
)]
- let mut locks = self.asset_locks.lock().expect("asset lock registry");
+ let mut locks = self
+ .coordination
+ .asset_locks
+ .lock()
+ .expect("asset lock registry");
if let Some((key, lock)) = locks.iter().find(|(key, _)| keys_overlap(key, components)) {
if key == components {
return Ok(Arc::clone(lock));
@@ -297,11 +351,11 @@ impl AssetStore {
}
fn downloader(&self) -> Result<&Downloader, Error> {
- if let Some(downloader) = self.downloader.get() {
+ if let Some(downloader) = self.coordination.downloader.get() {
return Ok(downloader);
}
let downloader = Downloader::new()?;
- Ok(self.downloader.get_or_init(|| downloader))
+ Ok(self.coordination.downloader.get_or_init(|| downloader))
}
fn staging_dir(&self, asset: &Asset) -> PathBuf {
@@ -405,6 +459,28 @@ impl AssetStore {
}
}
+fn normalized_root(root: PathBuf) -> PathBuf {
+ let absolute = std::path::absolute(&root).unwrap_or(root);
+ let mut ancestor = absolute.as_path();
+ let mut missing = Vec::new();
+ loop {
+ if let Ok(mut canonical) = fs::canonicalize(ancestor) {
+ for component in missing.into_iter().rev() {
+ canonical.push(component);
+ }
+ return canonical;
+ }
+ let Some(name) = ancestor.file_name() else {
+ return absolute;
+ };
+ missing.push(name.to_os_string());
+ let Some(parent) = ancestor.parent() else {
+ return absolute;
+ };
+ ancestor = parent;
+ }
+}
+
struct BatchProgress<F> {
assets: Vec<Option<Progress>>,
pending: Vec<bool>,
@@ -536,7 +612,11 @@ fn remove_path(path: &Path) -> Result<(), Error> {
#[cfg(test)]
mod tests {
- use std::io::Cursor;
+ use std::io::{Cursor, Read, Write};
+ use std::net::TcpListener;
+ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
+ use std::thread;
+ use std::time::Duration;
use std::time::{SystemTime, UNIX_EPOCH};
use flate2::Compression;
@@ -546,6 +626,174 @@ mod tests {
use super::*;
+ #[tokio::test]
+ async fn independently_created_stores_share_a_transfer_and_allow_retry() {
+ let root = scratch_dir("shared-transfer");
+ let first = AssetStore::new(&root);
+ fs::create_dir_all(&root).unwrap();
+ let second = AssetStore::new(root.join("."));
+ assert!(Arc::ptr_eq(&first.coordination, &second.coordination));
+ let server = TestServer::new();
+ let mut model = file("model", b"model");
+ model.url = server.url.clone();
+ let asset = Asset::file(key(&["models", "shared"]), model).unwrap();
+
+ let (left, right) = tokio::join!(
+ first.download(
+ std::slice::from_ref(&asset),
+ |_| {},
+ CancellationToken::default()
+ ),
+ second.download(
+ std::slice::from_ref(&asset),
+ |_| {},
+ CancellationToken::default()
+ ),
+ );
+ left.unwrap();
+ right.unwrap();
+ assert_eq!(server.transfers.load(Ordering::SeqCst), 1);
+ assert!(second.is_downloaded(&asset));
+
+ fs::write(
+ first.asset_dir(&asset).join("model"),
+ b"trusted published bytes",
+ )
+ .unwrap();
+ second
+ .download(
+ std::slice::from_ref(&asset),
+ |_| {},
+ CancellationToken::default(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(server.transfers.load(Ordering::SeqCst), 1);
+ assert_eq!(
+ fs::read(first.asset_dir(&asset).join("model")).unwrap(),
+ b"trusted published bytes"
+ );
+
+ first.remove(&asset).unwrap();
+ server.fail.store(true, Ordering::SeqCst);
+ assert!(
+ first
+ .download(
+ std::slice::from_ref(&asset),
+ |_| {},
+ CancellationToken::default()
+ )
+ .await
+ .is_err()
+ );
+ let transfers_after_failure = server.transfers.load(Ordering::SeqCst);
+ assert!(transfers_after_failure > 1);
+ server.fail.store(false, Ordering::SeqCst);
+ second
+ .download(
+ std::slice::from_ref(&asset),
+ |_| {},
+ CancellationToken::default(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(
+ server.transfers.load(Ordering::SeqCst),
+ transfers_after_failure + 1
+ );
+ assert_eq!(
+ fs::read(first.asset_dir(&asset).join("model")).unwrap(),
+ b"model"
+ );
+ drop(server);
+ fs::remove_dir_all(root).unwrap();
+ }
+
+ #[tokio::test]
+ async fn removal_uses_the_shared_download_lock() {
+ let root = scratch_dir("shared-removal");
+ let first = AssetStore::new(&root);
+ let second = AssetStore::new(&root);
+ let asset = Asset::file(key(&["models", "shared"]), file("model", b"model")).unwrap();
+ let lock = first.asset_lock(&asset).unwrap();
+ let guard = lock.lock().await;
+ assert!(matches!(second.remove(&asset), Err(AssetStoreError::Busy)));
+ drop(guard);
+ second.remove(&asset).unwrap();
+ }
+
+ struct TestServer {
+ url: String,
+ transfers: Arc<AtomicUsize>,
+ fail: Arc<AtomicBool>,
+ running: Arc<AtomicBool>,
+ worker: Option<thread::JoinHandle<()>>,
+ }
+
+ impl TestServer {
+ fn new() -> Self {
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
+ listener.set_nonblocking(true).unwrap();
+ let url = format!("http://{}/model", listener.local_addr().unwrap());
+ let transfers = Arc::new(AtomicUsize::new(0));
+ let fail = Arc::new(AtomicBool::new(false));
+ let running = Arc::new(AtomicBool::new(true));
+ let worker = {
+ let transfers = Arc::clone(&transfers);
+ let fail = Arc::clone(&fail);
+ let running = Arc::clone(&running);
+ thread::spawn(move || {
+ while running.load(Ordering::SeqCst) {
+ let (mut stream, _) = match listener.accept() {
+ Ok(connection) => connection,
+ Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
+ thread::sleep(Duration::from_millis(2));
+ continue;
+ }
+ Err(error) => panic!("test server: {error}"),
+ };
+ stream
+ .set_read_timeout(Some(Duration::from_secs(5)))
+ .unwrap();
+ let mut request = Vec::new();
+ while !request.ends_with(b"\r\n\r\n") {
+ let mut byte = [0];
+ stream.read_exact(&mut byte).unwrap();
+ request.push(byte[0]);
+ }
+ let get = request.starts_with(b"GET ");
+ if get {
+ transfers.fetch_add(1, Ordering::SeqCst);
+ }
+ if get && fail.load(Ordering::SeqCst) {
+ stream.write_all(b"HTTP/1.1 410 Gone\r\nContent-Length: 0\r\nConnection: close\r\n\r\n").unwrap();
+ } else {
+ stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\nConnection: close\r\n\r\n").unwrap();
+ if get {
+ thread::sleep(Duration::from_millis(20));
+ stream.write_all(b"model").unwrap();
+ }
+ }
+ }
+ })
+ };
+ Self {
+ url,
+ transfers,
+ fail,
+ running,
+ worker: Some(worker),
+ }
+ }
+ }
+
+ impl Drop for TestServer {
+ fn drop(&mut self) {
+ self.running.store(false, Ordering::SeqCst);
+ self.worker.take().unwrap().join().unwrap();
+ }
+ }
+
#[test]
fn store_and_operations_are_send_and_sync() {
fn assert_send<T: Send>(_: T) {}
diff --git a/rust/crates/ml/src/assets.rs b/rust/crates/ml/src/assets.rs
index d548a01616..b4e6346009 100644
--- a/rust/crates/ml/src/assets.rs
+++ b/rust/crates/ml/src/assets.rs
@@ -1,8 +1,11 @@
use std::fs;
use std::path::{Path, PathBuf};
-use ente_assets::{Asset, AssetFile, AssetStore};
+use ente_assets::{
+ Asset, AssetDownloadProgress, AssetFile, AssetStore, download::CancellationToken,
+};
+use super::error::{MlError, MlResult};
use super::models::{self, Model, ModelPaths};
const MODELS: &str = "models";
@@ -92,6 +95,138 @@ pub fn clip_text_asset() -> Asset {
model_asset(Model::ClipText)
}
+pub async fn ensure_indexing_models(
+ store: &AssetStore,
+ legacy_dir: &Path,
+ run_faces: bool,
+ run_clip: bool,
+ run_pets: bool,
+) -> MlResult<ModelPaths> {
+ ensure_mobile_models(
+ store,
+ legacy_dir,
+ &indexing_assets(run_faces, run_clip, run_pets),
+ |_| {},
+ )
+ .await?;
+ Ok(indexing_model_paths(store, run_faces, run_clip, run_pets))
+}
+
+pub async fn ensure_clip_text(store: &AssetStore, legacy_dir: &Path) -> MlResult<ClipTextPaths> {
+ ensure_mobile_models(store, legacy_dir, &[clip_text_asset()], |_| {}).await?;
+ Ok(clip_text_paths(store))
+}
+
+pub async fn ensure_mobile_models(
+ store: &AssetStore,
+ legacy_dir: &Path,
+ assets: &[Asset],
+ on_progress: impl FnMut(AssetDownloadProgress) + Send,
+) -> MlResult<()> {
+ store
+ .download_with_staged_import(
+ assets,
+ on_progress,
+ CancellationToken::default(),
+ |asset, staging| {
+ for model in Model::ALL {
+ if model_asset(model) == *asset {
+ import_mobile_model(legacy_dir, staging, model_asset_spec(model).files)?;
+ break;
+ }
+ }
+ Ok(())
+ },
+ )
+ .await
+ .map_err(|error| MlError::Runtime(format!("model download failed: {error}")))
+}
+
+pub fn is_clip_text_available(store: &AssetStore, legacy_dir: &Path, include_vocab: bool) -> bool {
+ let spec = model_asset_spec(Model::ClipText);
+ let files = if include_vocab {
+ spec.files
+ } else {
+ &spec.files[..1]
+ };
+ files.iter().all(|file| {
+ store
+ .file_path(&clip_text_asset(), file.name)
+ .is_some_and(|path| path.is_file())
+ || store
+ .staged_file_path(&clip_text_asset(), file.name)
+ .is_some_and(|path| path.is_file())
+ || legacy_mobile_paths(legacy_dir, file.name)
+ .iter()
+ .any(|path| path.is_file())
+ })
+}
+
+pub fn remove_mobile_indexing_models(store: &AssetStore, legacy_dir: &Path) -> Result<(), String> {
+ for model in models::selected_indexing_models(true, true, true) {
+ store
+ .remove(&model_asset(model))
+ .map_err(|error| error.to_string())?;
+ for file in model_asset_spec(model).files {
+ for path in legacy_mobile_paths(legacy_dir, file.name) {
+ for path in [
+ &path,
+ &path.with_extension("temp"),
+ &path.with_extension("temp.resume.json"),
+ ] {
+ remove_file_if_exists(path).map_err(|error| error.to_string())?;
+ }
+ }
+ }
+ }
+ Ok(())
+}
+
+fn import_mobile_model(
+ legacy_dir: &Path,
+ staging: &Path,
+ files: &[ModelAssetFile],
+) -> Result<(), ente_assets::download::Error> {
+ for file in files {
+ for source in legacy_mobile_paths(legacy_dir, file.name) {
+ remove_file_if_exists(&source.with_extension("temp"))?;
+ remove_file_if_exists(&source.with_extension("temp.resume.json"))?;
+ let destination = staging.join(file.name);
+ if !source.is_file()
+ || destination.exists()
+ || staging.join(format!("{}.tmp", file.name)).exists()
+ || staging
+ .join(format!("{}.tmp.ranges.json", file.name))
+ .exists()
+ || staging
+ .join(format!("{}.tmp.partial.json", file.name))
+ .exists()
+ {
+ continue;
+ }
+ fs::create_dir_all(staging)?;
+ fs::rename(source, destination)?;
+ }
+ }
+ Ok(())
+}
+
+fn legacy_mobile_paths(legacy_dir: &Path, name: &str) -> [PathBuf; 2] {
+ ["com", "io"].map(|domain| {
+ let name = format!("models.ente.{domain}/{name}")
+ .chars()
+ .map(|character| {
+ if character.is_ascii_alphanumeric() {
+ character
+ } else {
+ '_'
+ }
+ })
+ .collect::<String>();
+ legacy_dir.join(name)
+ })
+}
+
pub fn migrate_desktop_models(store: &AssetStore, legacy_dir: &Path) -> Vec<String> {
let mut warnings = Vec::new();
if legacy_dir.exists() {
@@ -298,6 +433,212 @@ mod tests {
use super::*;
+ const MOBILE_TEST_FILES: &[ModelAssetFile] = &[
+ ModelAssetFile {
+ name: "mobileclip_s2_text_opset18_quant.onnx",
+ size: 5,
+ sha256: "9372c470eeadd5ecd9c3c74c2b3cb633f8e2f2fad799250a0f70d652b6b825e4",
+ },
+ ModelAssetFile {
+ name: "bpe_simple_vocab_16e6.txt",
+ size: 5,
+ sha256: "273e2549599ddf859b4c24385dfb4dcc23e8e66994c2c176b90c602f77a795c0",
+ },
+ ];
+
+ fn mobile_test_asset() -> Asset {
+ Asset::files(
+ model_key("mobileclip_s2_text_opset18_quant"),
+ MOBILE_TEST_FILES
+ .iter()
+ .map(|file| AssetFile {
+ name: file.name.to_string(),
+ url: ":".to_string(),
+ size: file.size,
+ sha256: file.sha256.to_string(),
+ })
+ .collect(),
+ )
+ .unwrap()
+ }
+
+ async fn acquire_mobile_test_pair(
+ store: &AssetStore,
+ root: &Path,
+ ) -> Result<(), ente_assets::download::Error> {
+ store
+ .download_with_staged_import(
+ &[mobile_test_asset()],
+ |_| {},
+ CancellationToken::default(),
+ |_, staging| import_mobile_model(root, staging, MOBILE_TEST_FILES),
+ )
+ .await
+ }
+
+ #[tokio::test]
+ async fn mobile_legacy_pair_is_renamed_and_validated_without_network() {
+ let root = TempDir::new().unwrap();
+ let store = AssetStore::new(root.path());
+ let bytes = [b"model", b"vocab"];
+ #[cfg(unix)]
+ let mut inodes = Vec::new();
+ for (file, bytes) in MOBILE_TEST_FILES.iter().zip(bytes) {
+ let path = &legacy_mobile_paths(root.path(), file.name)[0];
+ fs::write(path, bytes).unwrap();
+ fs::write(path.with_extension("temp"), b"partial").unwrap();
+ fs::write(path.with_extension("temp.resume.json"), b"metadata").unwrap();
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::MetadataExt;
+ inodes.push(fs::metadata(path).unwrap().ino());
+ }
+ }
+ fs::write(root.path().join("unrelated.temp"), b"unrelated").unwrap();
+ acquire_mobile_test_pair(&store, root.path()).await.unwrap();
+ for (index, file) in MOBILE_TEST_FILES.iter().enumerate() {
+ let path = store.file_path(&mobile_test_asset(), file.name).unwrap();
+ assert_eq!(fs::read(&path).unwrap(), bytes[index]);
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::MetadataExt;
+ assert_eq!(fs::metadata(path).unwrap().ino(), inodes[index]);
+ }
+ for source in legacy_mobile_paths(root.path(), file.name) {
+ assert!(!source.exists());
+ assert!(!source.with_extension("temp").exists());
+ assert!(!source.with_extension("temp.resume.json").exists());
+ }
+ }
+ assert_eq!(
+ fs::read(root.path().join("unrelated.temp")).unwrap(),
+ b"unrelated"
+ );
+ fs::write(
+ store
+ .file_path(&mobile_test_asset(), MOBILE_TEST_FILES[0].name)
+ .unwrap(),
+ b"trusted",
+ )
+ .unwrap();
+ acquire_mobile_test_pair(&AssetStore::new(root.path()), root.path())
+ .await
+ .unwrap();
+ assert_eq!(
+ fs::read(
+ store
+ .file_path(&mobile_test_asset(), MOBILE_TEST_FILES[0].name)
+ .unwrap()
+ )
+ .unwrap(),
+ b"trusted"
+ );
+ }
+
+ #[tokio::test]
+ async fn either_lone_mobile_member_survives_failure_and_is_reused() {
+ for first in [0, 1] {
+ let root = TempDir::new().unwrap();
+ let store = AssetStore::new(root.path());
+ let bytes = [b"model", b"vocab"];
+ let source = &legacy_mobile_paths(root.path(), MOBILE_TEST_FILES[first].name)[0];
+ fs::write(source, bytes[first]).unwrap();
+ assert!(acquire_mobile_test_pair(&store, root.path()).await.is_err());
+ assert!(!source.exists());
+ assert!(!store.is_downloaded(&mobile_test_asset()));
+ let remaining = 1 - first;
+ fs::write(
+ &legacy_mobile_paths(root.path(), MOBILE_TEST_FILES[remaining].name)[0],
+ bytes[remaining],
+ )
+ .unwrap();
+ acquire_mobile_test_pair(&AssetStore::new(root.path()), root.path())
+ .await
+ .unwrap();
+ for (file, bytes) in MOBILE_TEST_FILES.iter().zip(bytes) {
+ assert_eq!(
+ fs::read(store.file_path(&mobile_test_asset(), file.name).unwrap()).unwrap(),
+ bytes
+ );
+ }
+ }
+ }
+
+ #[tokio::test]
+ async fn mobile_import_uses_the_staged_checksum_before_publication() {
+ let root = TempDir::new().unwrap();
+ let store = AssetStore::new(root.path());
+ for (file, bytes) in MOBILE_TEST_FILES.iter().zip([b"wrong", b"vocab"]) {
+ fs::write(&legacy_mobile_paths(root.path(), file.name)[0], bytes).unwrap();
+ }
+ assert!(acquire_mobile_test_pair(&store, root.path()).await.is_err());
+ assert!(!store.is_downloaded(&mobile_test_asset()));
+ assert!(!store.asset_dir(&mobile_test_asset()).exists());
+ }
+
+ #[test]
+ fn mobile_import_preserves_existing_rust_resume_data() {
+ let root = TempDir::new().unwrap();
+ let staging = root.path().join("staging");
+ fs::create_dir(&staging).unwrap();
+ let file = &MOBILE_TEST_FILES[0];
+ let source = &legacy_mobile_paths(root.path(), file.name)[0];
+ fs::write(source, b"model").unwrap();
+ let partial = staging.join(format!("{}.tmp", file.name));
+ let metadata = staging.join(format!("{}.tmp.partial.json", file.name));
+ fs::write(&partial, b"rust partial").unwrap();
+ fs::write(&metadata, b"rust metadata").unwrap();
+ import_mobile_model(root.path(), &staging, MOBILE_TEST_FILES).unwrap();
+ assert_eq!(fs::read(partial).unwrap(), b"rust partial");
+ assert_eq!(fs::read(metadata).unwrap(), b"rust metadata");
+ assert_eq!(fs::read(source).unwrap(), b"model");
+ assert!(!staging.join(file.name).exists());
+ }
+
+ #[tokio::test]
+ async fn mobile_ensure_returns_only_the_requested_published_paths() {
+ let root = TempDir::new().unwrap();
+ let store = AssetStore::new(root.path());
+ for model in models::selected_indexing_models(true, false, false) {
+ fs::create_dir_all(store.asset_dir(&model_asset(model))).unwrap();
+ fs::write(model_path(&store, model), b"trusted published bytes").unwrap();
+ }
+ let paths = ensure_indexing_models(&store, root.path(), true, false, false)
+ .await
+ .unwrap();
+ assert_eq!(
+ fs::read(paths.face_detection).unwrap(),
+ b"trusted published bytes"
+ );
+ assert_eq!(
+ fs::read(paths.face_embedding).unwrap(),
+ b"trusted published bytes"
+ );
+ assert!(paths.clip_image.is_empty());
+ assert!(paths.pet_face_detection.is_empty());
+ assert!(!store.asset_dir(&clip_text_asset()).exists());
+ }
+
+ #[test]
+ fn mobile_clip_readiness_keeps_model_only_admission() {
+ let root = TempDir::new().unwrap();
+ let store = AssetStore::new(root.path());
+ let files = model_asset_spec(Model::ClipText).files;
+ fs::write(
+ &legacy_mobile_paths(root.path(), files[0].name)[0],
+ b"model",
+ )
+ .unwrap();
+ assert!(is_clip_text_available(&store, root.path(), false));
+ assert!(!is_clip_text_available(&store, root.path(), true));
+ fs::write(
+ &legacy_mobile_paths(root.path(), files[1].name)[0],
+ b"vocab",
+ )
+ .unwrap();
+ assert!(is_clip_text_available(&store, root.path(), true));
+ }
+
#[test]
fn catalog_keys_are_unique_and_prefix_free() {
let root = Path::new("assets");