Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob][photos] Use isolate abstraction on MLComputer

laurenspriem9fe76eb52726
diff --git a/mobile/lib/services/isolate_functions.dart b/mobile/lib/services/isolate_functions.dart
index e0e98a03d6..75de71c9d3 100644
--- a/mobile/lib/services/isolate_functions.dart
+++ b/mobile/lib/services/isolate_functions.dart
@@ -33,7 +33,9 @@ Future<dynamic> isolateFunction(
Map<String, dynamic> args,
) async {
switch (function) {
- // Cases for MLComputer
+ /// Cases for MLComputer start here
+
+ /// MLComputer
case IsolateOperation.generateFaceThumbnails:
final imagePath = args['imagePath'] as String;
final Uint8List imageData = await File(imagePath).readAsBytes();
@@ -45,6 +47,8 @@ Future<dynamic> isolateFunction(
faceBoxes,
);
return List.from(results);
+
+ /// MLComputer
case IsolateOperation.loadModel:
final modelName = args['modelName'] as String;
final modelPath = args['modelPath'] as String;
@@ -53,18 +57,28 @@ Future<dynamic> isolateFunction(
modelPath,
);
return address;
+
+ /// MLComputer
case IsolateOperation.initializeClipTokenizer:
final vocabPath = args["vocabPath"] as String;
await ClipTextTokenizer.instance.init(vocabPath);
return true;
+
+ /// MLComputer
case IsolateOperation.runClipText:
//TODO:lau check logging here
final textEmbedding = await ClipTextEncoder.predict(args);
return List<double>.from(textEmbedding, growable: false);
+
+ /// MLComputer
case IsolateOperation.testLogging:
final logger = Logger('XXX MLComputerTestLogging');
logger.info("XXX logging from isolate is working!!!");
- throw Exception("XXX logging from isolate testing exception handling");
+ // throw Exception("XXX logging from isolate testing exception handling");
return true;
+
+ /// Cases for MLComputer ends here
+
+ ///
}
}
diff --git a/mobile/lib/services/isolate_service.dart b/mobile/lib/services/isolate_service.dart
index 98b365c0ae..4c7227516e 100644
--- a/mobile/lib/services/isolate_service.dart
+++ b/mobile/lib/services/isolate_service.dart
@@ -9,7 +9,6 @@ import "package:photos/services/isolate_functions.dart";
import "package:synchronized/synchronized.dart";
abstract class SuperIsolate {
- static final Logger abstractLogger = Logger('SuperIsolate');
Logger get logger;
Timer? _inactivityTimer;
@@ -20,7 +19,7 @@ abstract class SuperIsolate {
final _functionLock = Lock();
bool get isDartUiIsolate;
- bool get automaticDispose;
+ bool get shouldAutomaticDispose;
String get isolateName;
late dynamic _isolate;
@@ -49,7 +48,7 @@ abstract class SuperIsolate {
);
_mainSendPort = await _receivePort.first as SendPort;
- if (automaticDispose) _resetInactivityTimer();
+ if (shouldAutomaticDispose) _resetInactivityTimer();
logger.info('initIsolate done');
_isIsolateSpawned = true;
} catch (e) {
@@ -87,13 +86,16 @@ abstract class SuperIsolate {
});
}
- /// The common method to run any operation in the isolate. It sends the [message] to [_isolateMain] and waits for the result.
- Future<dynamic> _runInIsolate(
- (int, Map<String, dynamic>) message,
+ /// The common method to run any operation in the isolate.
+ /// It sends the [message] to [_isolateMain] and waits for the result.
+ /// The actual function executed is [isolateFunction].
+ Future<dynamic> runInIsolate(
+ IsolateOperation operation,
+ Map<String, dynamic> args,
) async {
await _initIsolate();
return _functionLock.synchronized(() async {
- if (automaticDispose) _resetInactivityTimer();
+ if (shouldAutomaticDispose) _resetInactivityTimer();
if (_postLockStop()) {
return null;
@@ -103,7 +105,7 @@ abstract class SuperIsolate {
final answerPort = ReceivePort();
_activeTasks++;
- _mainSendPort.send([message.$1, message.$2, answerPort.sendPort]);
+ _mainSendPort.send([operation.index, args, answerPort.sendPort]);
answerPort.listen((receivedMessage) {
final logs = receivedMessage['logs'] as List<String>;
diff --git a/mobile/lib/services/machine_learning/ml_computer.dart b/mobile/lib/services/machine_learning/ml_computer.dart
index 91f49925af..7293d336f1 100644
--- a/mobile/lib/services/machine_learning/ml_computer.dart
+++ b/mobile/lib/services/machine_learning/ml_computer.dart
@@ -1,166 +1,36 @@
import 'dart:async';
-import "dart:io" show File;
-import 'dart:isolate';
import 'dart:typed_data' show Uint8List;
-import "package:dart_ui_isolate/dart_ui_isolate.dart";
-import "package:flutter/foundation.dart" show kDebugMode;
import "package:logging/logging.dart";
-import "package:photos/core/error-reporting/isolate_logging.dart";
import "package:photos/models/ml/face/box.dart";
-import "package:photos/services/machine_learning/ml_model.dart";
+import "package:photos/services/isolate_functions.dart";
+import "package:photos/services/isolate_service.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/remote_assets_service.dart";
import "package:photos/utils/image_ml_util.dart";
import "package:synchronized/synchronized.dart";
-enum MLComputerOperation {
- generateFaceThumbnails,
- loadModel,
- initializeClipTokenizer,
- runClipText,
- testLogging,
-}
-
-class MLComputer {
+class MLComputer extends SuperIsolate {
+ @override
+ Logger get logger => _logger;
final _logger = Logger('MLComputer');
- final _initLock = Lock();
- final _functionLock = Lock();
final _initModelLock = Lock();
- late ReceivePort _receivePort = ReceivePort();
- late SendPort _mainSendPort;
+ @override
+ bool get isDartUiIsolate => true;
+
+ @override
+ String get isolateName => "MLComputerIsolate";
- bool isSpawned = false;
+ @override
+ bool get shouldAutomaticDispose => false;
// Singleton pattern
MLComputer._privateConstructor();
static final MLComputer instance = MLComputer._privateConstructor();
factory MLComputer() => instance;
- Future<void> _init() async {
- return _initLock.synchronized(() async {
- if (isSpawned) return;
-
- _receivePort = ReceivePort();
-
- try {
- await DartUiIsolate.spawn(
- _isolateMain,
- _receivePort.sendPort,
- );
- _mainSendPort = await _receivePort.first as SendPort;
- isSpawned = true;
- } catch (e) {
- _logger.severe('Could not spawn isolate', e);
- isSpawned = false;
- }
- });
- }
-
- @pragma('vm:entry-point')
- static void _isolateMain(SendPort mainSendPort) async {
- Logger.root.level = kDebugMode ? Level.ALL : Level.INFO;
- final IsolateLogger isolateLogger = IsolateLogger();
- Logger.root.onRecord.listen(isolateLogger.onLogRecordInIsolate);
- final receivePort = ReceivePort();
- mainSendPort.send(receivePort.sendPort);
-
- receivePort.listen((message) async {
- final functionIndex = message[0] as int;
- final function = MLComputerOperation.values[functionIndex];
- final args = message[1] as Map<String, dynamic>;
- final sendPort = message[2] as SendPort;
-
- late final Object data;
- try {
- data = await cases(function, args);
- } catch (e, stackTrace) {
- data = {
- 'error': e.toString(),
- 'stackTrace': stackTrace.toString(),
- };
- }
- final logs = List<String>.from(isolateLogger.getLogStringsAndClear());
- sendPort.send({"data": data, "logs": logs});
- });
- }
-
- /// WARNING: Only return primitives: https://api.flutter.dev/flutter/dart-isolate/SendPort/send.html
- static Future<dynamic> cases(
- MLComputerOperation function,
- Map<String, dynamic> args,
- ) async {
- switch (function) {
- case MLComputerOperation.generateFaceThumbnails:
- final imagePath = args['imagePath'] as String;
- final Uint8List imageData = await File(imagePath).readAsBytes();
- final faceBoxesJson =
- args['faceBoxesList'] as List<Map<String, dynamic>>;
- final List<FaceBox> faceBoxes =
- faceBoxesJson.map((json) => FaceBox.fromJson(json)).toList();
- final List<Uint8List> results = await generateFaceThumbnailsUsingCanvas(
- imageData,
- faceBoxes,
- );
- return List.from(results);
- case MLComputerOperation.loadModel:
- final modelName = args['modelName'] as String;
- final modelPath = args['modelPath'] as String;
- final int address = await MlModel.loadModel(
- modelName,
- modelPath,
- );
- return address;
- case MLComputerOperation.initializeClipTokenizer:
- final vocabPath = args["vocabPath"] as String;
- await ClipTextTokenizer.instance.init(vocabPath);
- return true;
- case MLComputerOperation.runClipText:
- //TODO:lau check logging here
- final textEmbedding = await ClipTextEncoder.predict(args);
- return List<double>.from(textEmbedding, growable: false);
- case MLComputerOperation.testLogging:
- final logger = Logger('XXX MLComputerTestLogging');
- logger.info("XXX logging from isolate is working!!!");
- throw Exception("XXX logging from isolate testing exception handling");
- return true;
- }
- }
-
- /// The common method to run any operation in the isolate. It sends the [message] to [_isolateMain] and waits for the result.
- Future<dynamic> _runInIsolate(
- (MLComputerOperation, Map<String, dynamic>) message,
- ) async {
- await _init();
- return _functionLock.synchronized(() async {
- final completer = Completer<dynamic>();
- final answerPort = ReceivePort();
-
- _mainSendPort.send([message.$1.index, message.$2, answerPort.sendPort]);
-
- answerPort.listen((receivedMessage) {
- final logs = receivedMessage['logs'] as List<String>;
- IsolateLogger.handLogStringsToMainLogger(logs);
- final data = receivedMessage['data'];
- if (data is Map && data.containsKey('error')) {
- // Handle the error
- final errorMessage = data['error'];
- final errorStackTrace = data['stackTrace'];
- final exception = Exception(errorMessage);
- final stackTrace = StackTrace.fromString(errorStackTrace);
- completer.completeError(exception, stackTrace);
- } else {
- completer.complete(data);
- }
- });
-
- return completer.future;
- });
- }
-
/// Generates face thumbnails for all [faceBoxes] in [imageData].
///
/// Uses [generateFaceThumbnailsUsingCanvas] inside the isolate.
@@ -170,14 +40,12 @@ class MLComputer {
) async {
final List<Map<String, dynamic>> faceBoxesJson =
faceBoxes.map((box) => box.toJson()).toList();
- return await _runInIsolate(
- (
- MLComputerOperation.generateFaceThumbnails,
- {
- 'imagePath': imagePath,
- 'faceBoxesList': faceBoxesJson,
- },
- ),
+ return await runInIsolate(
+ IsolateOperation.generateFaceThumbnails,
+ {
+ 'imagePath': imagePath,
+ 'faceBoxesList': faceBoxesJson,
+ },
).then((value) => value.cast<Uint8List>());
}
@@ -185,15 +53,10 @@ class MLComputer {
try {
await _ensureLoadedClipTextModel();
final int clipAddress = ClipTextEncoder.instance.sessionAddress;
- final textEmbedding = await _runInIsolate(
- (
- MLComputerOperation.runClipText,
- {
- "text": query,
- "address": clipAddress,
- }
- ),
- ) as List<double>;
+ final textEmbedding = await runInIsolate(IsolateOperation.runClipText, {
+ "text": query,
+ "address": clipAddress,
+ }) as List<double>;
return textEmbedding;
} catch (e, s) {
_logger.severe("Could not run clip text in isolate", e, s);
@@ -210,11 +73,9 @@ class MLComputer {
ClipTextEncoder.instance.vocabRemotePath;
final String tokenizerVocabPath = await RemoteAssetsService.instance
.getAssetPath(tokenizerRemotePath);
- await _runInIsolate(
- (
- MLComputerOperation.initializeClipTokenizer,
- {'vocabPath': tokenizerVocabPath},
- ),
+ await runInIsolate(
+ IsolateOperation.initializeClipTokenizer,
+ {'vocabPath': tokenizerVocabPath},
);
// Load ClipText model
@@ -224,14 +85,12 @@ class MLComputer {
if (modelPath == null) {
throw Exception("Could not download clip text model, no wifi");
}
- final address = await _runInIsolate(
- (
- MLComputerOperation.loadModel,
- {
- 'modelName': modelName,
- 'modelPath': modelPath,
- },
- ),
+ final address = await runInIsolate(
+ IsolateOperation.loadModel,
+ {
+ 'modelName': modelName,
+ 'modelPath': modelPath,
+ },
) as int;
ClipTextEncoder.instance.storeSessionAddress(address);
} catch (e, s) {
@@ -243,12 +102,8 @@ class MLComputer {
Future<bool> testLogging() async {
try {
- final result = await _runInIsolate(
- (
- MLComputerOperation.testLogging,
- {},
- ),
- ) as bool;
+ final result =
+ await runInIsolate(IsolateOperation.testLogging, {}) as bool;
return result;
} catch (e, s) {
_logger.severe("XXX Could not test logging in isolate", e, s);