Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
14 changes: 10 additions & 4 deletions lib/src/core/engine/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand All @@ -537,8 +545,6 @@ class LlamaEngine {
}
rethrow;
}
await _captureObservedModel(location);
_throwIfDisposedDuringLoad();
companionLocations = locations.sublist(
sources.length - companions.length,
);
Expand Down
6 changes: 3 additions & 3 deletions lib/src/core/speech/speech_to_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/src/core/speech/text_to_speech.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
86 changes: 81 additions & 5 deletions test/unit/core/engine/engine_set_model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <double?>[];
final withProjector = <double?>[];
'half and then 1 once a projector loads', () async {
final backend = _Backend(urlLoadingSupported: true);
final engine = LlamaEngine(backend);
final alone = <Object?>[];
final withProjector = <Object?>[];

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 = <double?>[];

await expectLater(
engine.setModel(
LlamaModel(otherModel, projector: remoteProjector),
onProgress: (event) => progress.add(event.fraction),
),
throwsA(isA<LlamaModelException>()),
);

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 = <double?>[];

await expectLater(
engine.setModel(
LlamaModel(otherModel, projector: remoteProjector),
onProgress: (event) {
progress.add(event.fraction);
if (event.fraction == 1) throw StateError('callback failed');
},
),
throwsA(isA<StateError>()),
);

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<void>();
final engine = LlamaEngine(backend, observers: [_LoadObserver()]);
final progress = <double?>[];

final outcome = expectLater(
engine.setModel(
LlamaModel(otherModel, projector: remoteProjector),
onProgress: (event) => progress.add(event.fraction),
),
throwsA(
isA<LlamaStateException>().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);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/core/speech/speech_to_text_web_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions test/unit/core/speech/text_to_speech_web_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <ModelDownloadProgress>[];
final synthesizer = await TextToSpeechEngine.load(
Expand All @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions website/docs/changelog/recent-releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
3 changes: 1 addition & 2 deletions website/docs/guides/decision-models.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,7 @@ LiteRT-LM Web models report unsupported too.
the document base URL, so a `<base href>` 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
Expand Down
5 changes: 3 additions & 2 deletions website/docs/guides/model-lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
15 changes: 7 additions & 8 deletions website/docs/guides/speech-to-text.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions website/docs/guides/text-to-speech.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions website/docs/platforms/webgpu-bridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
Loading