π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Limit iOS refresh ML to remote result sync
laurenspriemd1191fca1e8c
diff --git a/mobile/apps/photos/changes/ios-refresh-ml.md b/mobile/apps/photos/changes/ios-refresh-ml.mdnew file mode 100644index 0000000000..6370d89086--- /dev/null+++ b/mobile/apps/photos/changes/ios-refresh-ml.md@@ -0,0 +1 @@+- Limited ML work during iOS background refresh to syncing existing remote results. Image indexing, clustering, and cache generation run in the foreground or background processing tasks.diff --git a/mobile/apps/photos/lib/main.dart b/mobile/apps/photos/lib/main.dartindex 4e801aad08..2532e225e0 100644--- a/mobile/apps/photos/lib/main.dart+++ b/mobile/apps/photos/lib/main.dart@@ -47,6 +47,7 @@ import 'package:photos/services/favorites_service.dart';import 'package:photos/services/home_widget_service.dart';import "package:photos/services/machine_learning/face_ml/person/person_service.dart";import "package:photos/services/machine_learning/ml_run_control.dart";+import "package:photos/services/machine_learning/ml_run_policy.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/memories/memory_music_service.dart";@@ -288,7 +289,12 @@ Future<void> runBackgroundTask("[BG TASK] No recent foreground activity, proceeding with background work",);- await _runMinimally(taskId, tlog, mlRunControl, mlLockWait, shouldStop);+ await MlRunPolicy.run(+ remoteSyncOnly:+ Platform.isIOS && taskId == BgTaskUtils.iOSBackgroundAppRefreshTask,+ action: () =>+ _runMinimally(taskId, tlog, mlRunControl, mlLockWait, shouldStop),+ );} finally {mlSelfStopTimer?.cancel();mlForegroundWatchTimer.cancel();@@ -411,6 +417,7 @@ Future<void> _runMinimally(if (shouldStop?.call() ?? false) return;final disposition = await MLService.instance.runAllML(force: false,+ remoteSyncOnly: MlRunPolicy.remoteSyncOnly,allowImageIndexing: BgTaskUtils.allowsImageIndexing(taskId),maxFilesToIndex: BgTaskUtils.isRefreshTask(taskId) ? 100 : null,control: mlRunControl,diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_run_policy.dart b/mobile/apps/photos/lib/services/machine_learning/ml_run_policy.dartnew file mode 100644index 0000000000..f8154a7888--- /dev/null+++ b/mobile/apps/photos/lib/services/machine_learning/ml_run_policy.dart@@ -0,0 +1,12 @@+import "dart:async";++class MlRunPolicy {+ static final _remoteSyncOnlyKey = Object();++ static bool get remoteSyncOnly => Zone.current[_remoteSyncOnlyKey] == true;++ static Future<T> run<T>({+ required bool remoteSyncOnly,+ required Future<T> Function() action,+ }) => runZoned(action, zoneValues: {_remoteSyncOnlyKey: remoteSyncOnly});+}diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart b/mobile/apps/photos/lib/services/machine_learning/ml_service.dartindex a9fe6e9dc5..e79511e87a 100644--- a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart+++ b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart@@ -339,6 +339,7 @@ class MLService {Future<MlRunDisposition> runAllML({bool force = false,+ bool remoteSyncOnly = false,bool allowImageIndexing = true,int? maxFilesToIndex,MlRunControl? control,@@ -392,6 +393,7 @@ class MLService {disposition = await _runAllMLProtected(mode: mode,force: force,+ remoteSyncOnly: remoteSyncOnly,allowImageIndexing: allowImageIndexing,maxFilesToIndex: maxFilesToIndex,control: runControl,@@ -434,6 +436,7 @@ class MLService {Future<MlRunDisposition> _runAllMLProtected({required MLMode mode,required bool force,+ required bool remoteSyncOnly,required bool allowImageIndexing,required int? maxFilesToIndex,required MlRunControl control,@@ -445,12 +448,32 @@ class MLService {}final mlDataDB = _dbForMode(mode);try {+ if (remoteSyncOnly && mode == MLMode.localGallery) {+ return MlRunDisposition.completed;+ }await _sync();if (control.stopRequested) {_logRunStopped(control, "after sync");return MlRunDisposition.stopped;}+ if (remoteSyncOnly) {+ if (localSettings.remoteFetchEnabled && canFetch()) {+ await _fetchAndIndexAllImages(+ mode: mode,+ control: control,+ allowImageIndexing: false,+ );+ }+ if (control.stopRequested) {+ return MlRunDisposition.stopped;+ }+ await PersonService.instance.sync();+ return control.stopRequested+ ? MlRunDisposition.stopped+ : MlRunDisposition.completed;+ }+final int unclusteredFacesCount = await mlDataDB.getUnclusteredFaceCount();if (unclusteredFacesCount > _forceClusteringFaceCountForMode(mode)) {@@ -609,7 +632,11 @@ class MLService {);}final Stream<List<FileMLInstruction>> instructionStream =- fetchEmbeddingsAndInstructions(fileDownloadMlLimit, mode: mode);+ fetchEmbeddingsAndInstructions(+ fileDownloadMlLimit,+ mode: mode,+ control: control,+ );int fileAnalyzedCount = 0;int fileAttemptCount = 0;diff --git a/mobile/apps/photos/lib/services/magic_cache_service.dart b/mobile/apps/photos/lib/services/magic_cache_service.dartindex 22c9d61294..d3d3326360 100644--- a/mobile/apps/photos/lib/services/magic_cache_service.dart+++ b/mobile/apps/photos/lib/services/magic_cache_service.dart@@ -23,6 +23,7 @@ import "package:photos/models/search/hierarchical/magic_filter.dart";import "package:photos/models/search/search_types.dart";import "package:photos/module/upload/service/file_uploader.dart";import "package:photos/service_locator.dart";+import "package:photos/services/machine_learning/ml_run_policy.dart";import "package:photos/services/machine_learning/semantic_search/semantic_search_service.dart";import "package:photos/services/search_service.dart";import "package:photos/ui/viewer/search/result/magic_result_screen.dart";@@ -332,6 +333,7 @@ class MagicCacheService {bool forced = false,bool interactive = false,}) async {+ if (MlRunPolicy.remoteSyncOnly) return;if (!enableDiscover) {return;}diff --git a/mobile/apps/photos/lib/services/memories_cache_service.dart b/mobile/apps/photos/lib/services/memories_cache_service.dartindex c3606cbe5e..f98f976948 100644--- a/mobile/apps/photos/lib/services/memories_cache_service.dart+++ b/mobile/apps/photos/lib/services/memories_cache_service.dart@@ -31,6 +31,7 @@ import "package:photos/services/language_service.dart";import "package:photos/services/machine_learning/face_ml/person/person_service.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_run_policy.dart";import "package:photos/services/memories/photo_selector.dart";import "package:photos/services/notification_service.dart";import "package:photos/services/search_service.dart";@@ -694,6 +695,7 @@ class MemoriesCacheService {}Future<void> _calculateRegularFillers() async {+ if (MlRunPolicy.remoteSyncOnly) return;if (_cachedMemories == null) {_cachedMemories = await smartMemoriesService.calcSimpleMemories();Bus.instance.fire(MemoriesChangedEvent());@@ -904,6 +906,7 @@ class MemoriesCacheService {}Future<void> updateCache({bool forced = false, MlRunControl? control}) async {+ if (MlRunPolicy.remoteSyncOnly) return;if (!showAnyMemories) {return;}diff --git a/mobile/apps/photos/lib/utils/ml_util.dart b/mobile/apps/photos/lib/utils/ml_util.dartindex 022325edc7..acc35663ec 100644--- a/mobile/apps/photos/lib/utils/ml_util.dart+++ b/mobile/apps/photos/lib/utils/ml_util.dart@@ -407,12 +407,15 @@ Future<List<FileMLInstruction>> getLocalGalleryFilesForMlIndexing() async {Stream<List<FileMLInstruction>> fetchEmbeddingsAndInstructions(int yieldSize, {required MLMode mode,+ MlRunControl? control,}) async* {+ if (control?.stopRequested ?? false) return;if (mode == MLMode.localGallery) {final List<FileMLInstruction> filesToIndex =await getLocalGalleryFilesForMlIndexing();final List<List<FileMLInstruction>> chunks = filesToIndex.chunks(yieldSize);for (final batch in chunks) {+ if (control?.stopRequested ?? false) return;yield batch;}return;@@ -425,10 +428,12 @@ Stream<List<FileMLInstruction>> fetchEmbeddingsAndInstructions(List<FileMLInstruction> batchToYield = [];for (final chunk in chunks) {+ if (control?.stopRequested ?? false) return;if (!localSettings.remoteFetchEnabled) {_logger.warning("remoteFetchEnabled is false, skiping embedding fetch");final batches = chunk.chunks(yieldSize);for (final batch in batches) {+ if (control?.stopRequested ?? false) return;yield batch;}continue;@@ -437,6 +442,7 @@ Stream<List<FileMLInstruction>> fetchEmbeddingsAndInstructions(chunk,mlDataDB: mlDataDB,);+ if (control?.stopRequested ?? false) return;for (final instruction in pendingInstructions) {if (instruction.pendingML) {batchToYield.add(instruction);@@ -448,7 +454,7 @@ Stream<List<FileMLInstruction>> fetchEmbeddingsAndInstructions(}}}- if (batchToYield.isNotEmpty) {+ if (batchToYield.isNotEmpty && !(control?.stopRequested ?? false)) {_logger.info("queueing indexing for ${batchToYield.length}");yield batchToYield;}