From a2b779564e1b640f88aa7d2cc4f5939a03522c79 Mon Sep 17 00:00:00 2001 From: dane madsen Date: Thu, 9 Nov 2023 20:17:10 +1000 Subject: [PATCH 1/3] Further updates --- lib/core/{core.dart => local_generation.dart} | 14 +++++++------- lib/pages/home_page.dart | 6 +++--- lib/utilities/memory_manager.dart | 4 ++-- lib/utilities/message_manager.dart | 12 ++++++------ 4 files changed, 18 insertions(+), 18 deletions(-) rename lib/core/{core.dart => local_generation.dart} (93%) diff --git a/lib/core/core.dart b/lib/core/local_generation.dart similarity index 93% rename from lib/core/core.dart rename to lib/core/local_generation.dart index 2ba3ecb7..ad08ea5a 100644 --- a/lib/core/core.dart +++ b/lib/core/local_generation.dart @@ -11,18 +11,18 @@ import 'package:maid/utilities/character.dart'; import 'package:maid/utilities/model.dart'; import 'package:maid/utilities/memory_manager.dart'; -class Core { +class LocalGeneration { static SendPort? _sendPort; late NativeLibrary _nativeLibrary; // Make the default constructor private - Core._(); + LocalGeneration._(); // Private reference to the global instance - static final Core _instance = Core._(); + static final LocalGeneration _instance = LocalGeneration._(); // Public accessor to the global instance - static Core get instance { + static LocalGeneration get instance { _instance._initialize(); return _instance; } @@ -64,11 +64,11 @@ class Core { } } - static promptIsolate(Map args) async { + static _promptIsolate(Map args) async { _sendPort = args['port'] as SendPort?; String input = args['input']; Pointer text = input.trim().toNativeUtf8().cast(); - Core.instance._nativeLibrary.core_prompt(text, Pointer.fromFunction(_maidOutputBridge)); + LocalGeneration.instance._nativeLibrary.core_prompt(text, Pointer.fromFunction(_maidOutputBridge)); } @@ -136,7 +136,7 @@ class Core { _init(input); } else { Logger.log("Input: $input"); - Isolate.spawn(promptIsolate, { + Isolate.spawn(_promptIsolate, { 'input': input, 'port': _sendPort }); diff --git a/lib/pages/home_page.dart b/lib/pages/home_page.dart index e1043f58..3341bb02 100644 --- a/lib/pages/home_page.dart +++ b/lib/pages/home_page.dart @@ -7,7 +7,7 @@ import 'package:maid/utilities/message_manager.dart'; import 'package:system_info2/system_info2.dart'; import 'package:maid/utilities/model.dart'; -import 'package:maid/core/core.dart'; +import 'package:maid/core/local_generation.dart'; import 'package:maid/pages/character_page.dart'; import 'package:maid/pages/model_page.dart'; @@ -89,7 +89,7 @@ class MaidHomePageState extends State { MessageManager.add(UniqueKey()); if (MemoryManager.checkFileExists(model.parameters["model_path"])) { - Core.instance.prompt(promptController.text.trim()); + LocalGeneration.instance.prompt(promptController.text.trim()); setState(() { model.busy = true; promptController.clear(); @@ -247,7 +247,7 @@ class MaidHomePageState extends State { children: [ if (model.busy) IconButton( - onPressed: Core.instance.stop, + onPressed: LocalGeneration.instance.stop, iconSize: 50, icon: const Icon( Icons.stop_circle_sharp, diff --git a/lib/utilities/memory_manager.dart b/lib/utilities/memory_manager.dart index 32355aa0..8272a026 100644 --- a/lib/utilities/memory_manager.dart +++ b/lib/utilities/memory_manager.dart @@ -4,7 +4,7 @@ import 'dart:io'; import 'package:maid/utilities/logger.dart'; import 'package:shared_preferences/shared_preferences.dart'; -import 'package:maid/core/core.dart'; +import 'package:maid/core/local_generation.dart'; import 'package:maid/utilities/model.dart'; import 'package:maid/utilities/character.dart'; @@ -47,7 +47,7 @@ class MemoryManager { prefs.setString("current_model", model.nameController.text); prefs.setString("current_character", character.nameController.text); - Core.instance.cleanup(); + LocalGeneration.instance.cleanup(); }); } diff --git a/lib/utilities/message_manager.dart b/lib/utilities/message_manager.dart index 05e78a72..9909eb57 100644 --- a/lib/utilities/message_manager.dart +++ b/lib/utilities/message_manager.dart @@ -3,7 +3,7 @@ import 'dart:collection'; import 'package:flutter/material.dart'; import 'package:maid/utilities/model.dart'; -import 'package:maid/core/core.dart'; +import 'package:maid/core/local_generation.dart'; class MessageManager { static void Function()? _callback; @@ -47,7 +47,7 @@ class MessageManager { parent.children.removeWhere((element) => element.key == key); _tail = _root.findTail(); } - Core.instance.cleanup(); + LocalGeneration.instance.cleanup(); _callback?.call(); } @@ -68,7 +68,7 @@ class MessageManager { } else { branch(key, false); model.busy = true; - Core.instance.prompt(parent.message); + LocalGeneration.instance.prompt(parent.message); } } @@ -79,7 +79,7 @@ class MessageManager { _tail = _root.findTail(); } add(UniqueKey(), userGenerated: userGenerated); - Core.instance.cleanup(); + LocalGeneration.instance.cleanup(); _callback?.call(); } @@ -102,7 +102,7 @@ class MessageManager { } } - Core.instance.cleanup(); + LocalGeneration.instance.cleanup(); _callback?.call(); } @@ -120,7 +120,7 @@ class MessageManager { } } - Core.instance.cleanup(); + LocalGeneration.instance.cleanup(); _callback?.call(); } From dba61058477a03bee37f69ae15556ad5241617b0 Mon Sep 17 00:00:00 2001 From: dane madsen Date: Thu, 9 Nov 2023 20:51:38 +1000 Subject: [PATCH 2/3] Update llama.cpp --- core/llama.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/llama.cpp b/core/llama.cpp index 866198e4..7feec2ef 160000 --- a/core/llama.cpp +++ b/core/llama.cpp @@ -1 +1 @@ -Subproject commit 866198e477de5c3e12302cdd84b5541927b950d8 +Subproject commit 7feec2efee385a5fb6b4cc9f847be2ca66c95174 From d5f6f409ec978e2a3aa7c7e24091369e4a3f7dc4 Mon Sep 17 00:00:00 2001 From: dane madsen Date: Thu, 9 Nov 2023 21:21:00 +1000 Subject: [PATCH 3/3] Fix crash on prompt --- lib/core/local_generation.dart | 4 ++-- lib/utilities/memory_manager.dart | 33 ++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/core/local_generation.dart b/lib/core/local_generation.dart index ad08ea5a..a8232e2b 100644 --- a/lib/core/local_generation.dart +++ b/lib/core/local_generation.dart @@ -129,10 +129,10 @@ class LocalGeneration { await completer.future; } - void prompt(String input) { + void prompt(String input) async { MessageManager.busy = true; if (!_hasStarted) { - MemoryManager.save(); + await MemoryManager.asave(); _init(input); } else { Logger.log("Input: $input"); diff --git a/lib/utilities/memory_manager.dart b/lib/utilities/memory_manager.dart index 8272a026..c29ebbbb 100644 --- a/lib/utilities/memory_manager.dart +++ b/lib/utilities/memory_manager.dart @@ -33,24 +33,33 @@ class MemoryManager { }); } - static void save() { - SharedPreferences.getInstance().then((prefs) { - prefs.clear(); + static void _save(SharedPreferences prefs) { + prefs.clear(); - _models[model.nameController.text] = model.toMap(); - Logger.log("Model Saved: ${model.nameController.text}"); - _characters[character.nameController.text] = character.toMap(); - Logger.log("Character Saved: ${character.nameController.text}"); + _models[model.nameController.text] = model.toMap(); + Logger.log("Model Saved: ${model.nameController.text}"); + _characters[character.nameController.text] = character.toMap(); + Logger.log("Character Saved: ${character.nameController.text}"); - prefs.setString("models", json.encode(_models)); - prefs.setString("characters", json.encode(_characters)); - prefs.setString("current_model", model.nameController.text); - prefs.setString("current_character", character.nameController.text); + prefs.setString("models", json.encode(_models)); + prefs.setString("characters", json.encode(_characters)); + prefs.setString("current_model", model.nameController.text); + prefs.setString("current_character", character.nameController.text); + + LocalGeneration.instance.cleanup(); + } - LocalGeneration.instance.cleanup(); + static void save() { + SharedPreferences.getInstance().then((prefs) { + _save(prefs); }); } + static Future asave() async { + var prefs = await SharedPreferences.getInstance(); + _save(prefs); + } + static void updateModel(String newName) { String oldName = model.nameController.text; Logger.log("Updating model $oldName ====> $newName");