diff --git a/CHANGELOG.md b/CHANGELOG.md index 4adfb52c0..4796faf54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## Unreleased + +- **Behavior change:** on Web, `SpeechToTextEngine.load`, + `TextToSpeechEngine.load` and `LlamaEngine.setModel` with a projector now + report progress from 0 to 1, as `DecisionEngine.load` does: the model fetch + fills the first half and progress reaches 1 once the files have loaded, + before any capability check ([#903](https://github.com/leehack/llamadart/issues/903)). + ## 0.12.2 - Pair this release with Apple companions `llamadart_llama_cpp_flutter` diff --git a/lib/src/core/engine/engine.dart b/lib/src/core/engine/engine.dart index be829b2df..8c3e24c40 100644 --- a/lib/src/core/engine/engine.dart +++ b/lib/src/core/engine/engine.dart @@ -417,8 +417,9 @@ class LlamaEngine { /// the load, so the old model is unloaded before the new one is fetched, /// and a fetch that fails leaves nothing loaded. There a local path is a /// URL relative to the document, or a `blob:` URL, [download] must leave - /// every option at its default, and [onProgress] reports only the model's - /// fetch, as a fraction that ends at 0.5 when there is a projector. + /// every option at its default, and [onProgress] reports a fraction: the + /// model's fetch fills it, or with a projector its first half, and 1 once + /// the projector has loaded. /// /// While a call runs, another [setModel] and [unloadModel] throw /// [LlamaStateException]; stop it with [download]'s cancel token. @@ -529,6 +530,13 @@ class LlamaEngine { ); } throwIfCancelled(); + await _captureObservedModel(location); + _throwIfDisposedDuringLoad(); + if (projector != null && backend.supportsUrlLoading) { + // The runtime reports no progress for the projector fetch, so its + // half completes once the projector has loaded. + onProgress?.call(const ModelDownloadProgress.fraction(1)); + } } catch (_) { try { await _unloadModel(); @@ -537,8 +545,6 @@ class LlamaEngine { } rethrow; } - await _captureObservedModel(location); - _throwIfDisposedDuringLoad(); companionLocations = locations.sublist( sources.length - companions.length, ); diff --git a/lib/src/core/speech/speech_to_text.dart b/lib/src/core/speech/speech_to_text.dart index 40c621072..2631aa91a 100644 --- a/lib/src/core/speech/speech_to_text.dart +++ b/lib/src/core/speech/speech_to_text.dart @@ -687,9 +687,9 @@ class SpeechToTextEngine { /// load, disposes both. Adapters in [ModelParams.loras] given as sources /// download as [LlamaEngine.setModel] downloads them. A URL-loading /// backend, as on the web, fetches each file itself, as - /// [LlamaEngine.setModel] describes. [onProgress] then reports only the - /// main file's fetch, as a fraction from 0 to 0.5 of the two files when - /// there is a projector; the projector fetch reports no progress. + /// [LlamaEngine.setModel] describes. [onProgress] then reports a fraction + /// from 0 to 1: with a projector, the main file's fetch fills the first + /// half and the projector reports 1 once it has loaded. /// /// With a [LiteRtLmAsrAdapter], [load] probes the LiteRT-LM ASR runtime /// before any download and then resolves [SpeechToTextModel.source] and diff --git a/lib/src/core/speech/text_to_speech.dart b/lib/src/core/speech/text_to_speech.dart index e3263bdeb..4570bcf93 100644 --- a/lib/src/core/speech/text_to_speech.dart +++ b/lib/src/core/speech/text_to_speech.dart @@ -476,9 +476,9 @@ class TextToSpeechEngine { /// size is known. Adapters in [ModelParams.loras] given as sources /// download as [LlamaEngine.setModel] downloads them. A URL-loading /// backend, as on the web, fetches each file itself, as - /// [LlamaEngine.setModel] describes. [onProgress] then reports only the - /// main file's fetch, as a fraction from 0 to 0.5 of the two files when - /// there is a projector; the projector fetch reports no progress. + /// [LlamaEngine.setModel] describes. [onProgress] then reports a fraction + /// from 0 to 1: with a projector, the main file's fetch fills the first + /// half and the projector reports 1 once it has loaded. /// /// The synthesizer owns the engine and a [backend] passed in: [dispose], /// or a failed load, disposes both. The load is atomic: when it throws, diff --git a/test/unit/core/engine/engine_set_model_test.dart b/test/unit/core/engine/engine_set_model_test.dart index 135d47777..588c28957 100644 --- a/test/unit/core/engine/engine_set_model_test.dart +++ b/test/unit/core/engine/engine_set_model_test.dart @@ -927,25 +927,101 @@ void main() { }); test('reports the model fetch as the whole progress, or as its first ' - 'half when there is a projector', () async { - final engine = LlamaEngine(_Backend(urlLoadingSupported: true)); - final alone = []; - final withProjector = []; + 'half and then 1 once a projector loads', () async { + final backend = _Backend(urlLoadingSupported: true); + final engine = LlamaEngine(backend); + final alone = []; + final withProjector = []; await engine.setModel( LlamaModel(otherModel), onProgress: (progress) => alone.add(progress.fraction), ); + backend.onProjectorLoad = () => withProjector.add('projector'); await engine.setModel( LlamaModel(otherModel, projector: remoteProjector), onProgress: (progress) => withProjector.add(progress.fraction), ); expect(alone, [0.25]); - expect(withProjector, [0.125]); + expect(withProjector, [0.125, 'projector', 1.0]); + await engine.dispose(); + }); + + test('does not report full progress when the projector fails to ' + 'load', () async { + final backend = _Backend(urlLoadingSupported: true) + ..projectorError = Exception('bad projector'); + final engine = LlamaEngine(backend); + final progress = []; + + await expectLater( + engine.setModel( + LlamaModel(otherModel, projector: remoteProjector), + onProgress: (event) => progress.add(event.fraction), + ), + throwsA(isA()), + ); + + expect(progress, [0.125]); + expect(engine.isReady, isFalse); + await engine.dispose(); + }); + + test('unloads the model and projector when onProgress throws at full ' + 'progress', () async { + final backend = _Backend(urlLoadingSupported: true); + final engine = LlamaEngine(backend); + final progress = []; + + await expectLater( + engine.setModel( + LlamaModel(otherModel, projector: remoteProjector), + onProgress: (event) { + progress.add(event.fraction); + if (event.fraction == 1) throw StateError('callback failed'); + }, + ), + throwsA(isA()), + ); + + expect(progress, [0.125, 1.0]); + expect(engine.isReady, isFalse); + expect(engine.hasMultimodalProjector, isFalse); + expect(backend.events, contains('modelFree')); await engine.dispose(); }); + test('dispose while the loaded model is read for observers reports no ' + 'full progress', () async { + final backend = _Backend(urlLoadingSupported: true) + ..metadataGate = Completer(); + final engine = LlamaEngine(backend, observers: [_LoadObserver()]); + final progress = []; + + final outcome = expectLater( + engine.setModel( + LlamaModel(otherModel, projector: remoteProjector), + onProgress: (event) => progress.add(event.fraction), + ), + throwsA( + isA().having( + (error) => error.message, + 'message', + contains('disposed while loading'), + ), + ), + ); + await backend.metadataStarted.future; + final dispose = engine.dispose(); + backend.metadataGate!.complete(); + await outcome; + await dispose; + + expect(progress, [0.125]); + expect(engine.isReady, isFalse); + }); + test('rejects a download option the runtime fetch cannot apply before it ' 'resolves anything, keeping the loaded model', () async { final backend = _Backend(urlLoadingSupported: true); diff --git a/test/unit/core/speech/speech_to_text_web_test.dart b/test/unit/core/speech/speech_to_text_web_test.dart index 7242e66f7..a4c2c5ca9 100644 --- a/test/unit/core/speech/speech_to_text_web_test.dart +++ b/test/unit/core/speech/speech_to_text_web_test.dart @@ -323,7 +323,7 @@ void main() { expect(backend.modelUrl, 'https://example.com/qwen3-asr.gguf'); expect(backend.projectorUrl, 'https://example.com/qwen3-asr-mmproj.gguf'); - expect([for (final p in progress) p.fraction], [0.25, 0.5]); + expect([for (final p in progress) p.fraction], [0.25, 0.5, 1.0]); final result = await recognizer.transcribeOnce( SpeechToTextRequest( diff --git a/test/unit/core/speech/text_to_speech_web_test.dart b/test/unit/core/speech/text_to_speech_web_test.dart index e04c8b7c9..85a5bee79 100644 --- a/test/unit/core/speech/text_to_speech_web_test.dart +++ b/test/unit/core/speech/text_to_speech_web_test.dart @@ -49,7 +49,8 @@ void main() { expect(backend.lastRequest?.speakerAudioBytes, isNotEmpty); }); - test('load reports the Web model fetch from 0 to 0.5', () async { + test('load reports Web progress from 0 to 1, ending once the projector ' + 'loads', () async { final backend = _WebBackend(); final progress = []; final synthesizer = await TextToSpeechEngine.load( @@ -67,7 +68,7 @@ void main() { expect(backend.modelUrl, 'https://example.com/qwen3-tts.gguf'); expect(backend.projectorUrl, 'https://example.com/qwen3-tts-mmproj.gguf'); - expect([for (final p in progress) p.fraction], [0.25, 0.5]); + expect([for (final p in progress) p.fraction], [0.25, 0.5, 1.0]); }); test('typed text-to-speech rejects browser-local speaker paths', () async { diff --git a/website/docs/changelog/recent-releases.md b/website/docs/changelog/recent-releases.md index 5669068ba..3a9e5e315 100644 --- a/website/docs/changelog/recent-releases.md +++ b/website/docs/changelog/recent-releases.md @@ -7,6 +7,14 @@ For canonical full release notes, use: - [`CHANGELOG.md`](https://github.com/leehack/llamadart/blob/main/CHANGELOG.md) +## Unreleased + +- **Behavior change:** on Web, `SpeechToTextEngine.load`, + `TextToSpeechEngine.load` and `LlamaEngine.setModel` with a projector now + report progress from 0 to 1, as `DecisionEngine.load` does: the model fetch + fills the first half and progress reaches 1 once the files have loaded, + before any capability check ([#903](https://github.com/leehack/llamadart/issues/903)). + ## 0.12.2 - Pair this release with Apple companions `llamadart_llama_cpp_flutter` diff --git a/website/docs/guides/decision-models.md b/website/docs/guides/decision-models.md index f5e2fcd97..277b4a83c 100644 --- a/website/docs/guides/decision-models.md +++ b/website/docs/guides/decision-models.md @@ -481,8 +481,7 @@ LiteRT-LM Web models report unsupported too. the document base URL, so a `` applies; a `blob:` URL also goes in `ModelSource.path`. `download:` must keep every option at its default, since the browser owns the fetch and cache, and `onProgress` reports only the - encoder fetch, as a fraction from 0 to 1 (`attach` reports none). Speech - `load` reports its model fetch from 0 to 0.5 instead. + encoder fetch, as a fraction from 0 to 1 (`attach` reports none). - The bridge downloads the head into its in-memory file system, so peak memory includes the whole head file. The page fetches the config and passes its text to the bridge; a config that cannot be fetched throws diff --git a/website/docs/guides/model-lifecycle.md b/website/docs/guides/model-lifecycle.md index 439cf63f2..19323d397 100644 --- a/website/docs/guides/model-lifecycle.md +++ b/website/docs/guides/model-lifecycle.md @@ -213,8 +213,9 @@ throws `LlamaStateException`. On web the runtime fetches each file itself: `.gguf` URLs load through the llama.cpp WebGPU bridge and `.litertlm` URLs through LiteRT-LM JS. A `ModelSource.path` is a URL relative to the document, or a `blob:` URL; -`download` must stay at its defaults, and `onProgress` reports only the -model's fetch, as a fraction that ends at 0.5 when there is a projector. +`download` must stay at its defaults, and `onProgress` reports a fraction from +0 to 1: with a projector, the model fetch fills the first half and progress +reaches 1 once the projector has loaded. Revisions, private repositories, progress UI, checksums and cache location: [Download and cache models](./model-downloads). diff --git a/website/docs/guides/speech-to-text.md b/website/docs/guides/speech-to-text.md index 13d94d35d..dfee361e6 100644 --- a/website/docs/guides/speech-to-text.md +++ b/website/docs/guides/speech-to-text.md @@ -47,10 +47,10 @@ you loaded yourself, for example to chat with the same model, and its `load` takes `download:` (`ModelLoadOptions` for every remote file: cache policy and directory, authentication, resume, retries and cancel token), -`onProgress:` (one combined progress for all files, except on [Web](#web)), -`store:` (a `ModelFileStore` with your own resolver or download manager), and, -for a prompt adapter, `params:` (`ModelParams`) and `backend:` (by default -`LlamaBackend()`). Every file resolves, main file first, before anything +`onProgress:` (one combined progress for all files; 1 means they have loaded, +before the capability check), `store:` (a `ModelFileStore` with your own +resolver or download manager), and, for a prompt adapter, `params:` +(`ModelParams`) and `backend:` (by default `LlamaBackend()`). Every file resolves, main file first, before anything loads. A local file takes only the cancel token. The bearer token and headers are never sent across hosts: when they are set and the remote files span more than one origin (scheme, host and port), `load` throws @@ -330,10 +330,9 @@ leaves an attached recognizer's `capabilities.isSupported` false, with an actionable reason. On Web, `load` passes the sources to the browser runtime, which fetches each file itself: a `ModelSource.path` is a URL relative to the document, or a `blob:` URL, and `download` must stay at its defaults. -`onProgress` reports only the model fetch, as a fraction from 0 to 0.5 of the -two files; the projector fetch reports nothing, so progress stops at 0.5. -`DecisionEngine.load` instead reports its encoder fetch from 0 to 1, so an app -that loads both on Web gets two progress scales. +`onProgress` reports a fraction from 0 to 1, as `DecisionEngine.load` does: +the model fetch fills the first half, and progress reaches 1 once the +projector has loaded. WebGPU accepts encoded WAV, MP3, and FLAC bytes. Read the selected file into memory and pass `SpeechAudioBytesInput` with a `SpeechAudioFormat` whose diff --git a/website/docs/guides/text-to-speech.md b/website/docs/guides/text-to-speech.md index dc2670200..7840a07f1 100644 --- a/website/docs/guides/text-to-speech.md +++ b/website/docs/guides/text-to-speech.md @@ -67,9 +67,8 @@ the reason when they cannot synthesize speech with the adapter. Loading a projector alone does not prove that the active native or Web runtime exports the required TTS ABI or that the projector matches the model. `download:` takes `ModelLoadOptions` for every remote file, `onProgress:` reports both -files together (on Web, only the model fetch, from 0 to 0.5; the projector -fetch reports nothing), `store:` takes a `ModelFileStore` with your own -resolver or download manager, and `backend:` the `LlamaBackend` (by default +files together, `store:` takes a `ModelFileStore` with your own resolver or +download manager, and `backend:` the `LlamaBackend` (by default `LlamaBackend()`). Both files resolve, model first, before anything loads; a local file takes only the cancel token. The bearer token and headers are never sent across hosts: when they are set and the two remote files are on diff --git a/website/docs/platforms/webgpu-bridge.md b/website/docs/platforms/webgpu-bridge.md index 64f1947e1..23f906ab4 100644 --- a/website/docs/platforms/webgpu-bridge.md +++ b/website/docs/platforms/webgpu-bridge.md @@ -232,8 +232,9 @@ The feature-by-runtime table is in the - The runtime fetches the model and projector itself. `ModelSource.parse` takes a remote `https://` URL; a `ModelSource.path` is a URL relative to the document, or a `blob:` URL. `download` must stay at its defaults, and - `onProgress` reports only the model's fetch, as a fraction that ends at 0.5 - when there is a projector. + `onProgress` reports a fraction from 0 to 1: with a projector, the model + fetch fills the first half and progress reaches 1 once the projector has + loaded. - `LlamaEngine.scoreNextToken(...)` needs bridge assets `v0.1.52+`; older assets report `supportsNextTokenScoring == false`.