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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

- Keep `LlamaArgumentException.name` when native LiteRT-LM reports an argument error from its worker isolate, and say in the native LiteRT-LM raw-prompt refusal whether the prompt is empty or holds only the model's start token ([#1861](https://github.com/leehack/llamadart/issues/1861)).

## 0.19.2

- Keep the default WebGPU bridge assets at `v0.1.83` (unchanged from 0.19.1).
Expand Down
5 changes: 4 additions & 1 deletion lib/src/backends/litert_lm/litert_lm_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,10 @@ class LiteRtLmBackend
case 'llamaUnsupported':
return LlamaUnsupportedException(response.message);
case 'llamaArgument':
return LlamaArgumentException(response.message);
return LlamaArgumentException(
response.message,
name: response.argumentName,
);
case 'llamaModel':
return LlamaModelException(response.message);
case 'llamaContext':
Expand Down
9 changes: 6 additions & 3 deletions lib/src/backends/litert_lm/litert_lm_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,12 @@ class LiteRtLmService {
final rawPrompt = _withoutLeadingStartToken(client, prompt);
if (rawPrompt.isEmpty) {
throw LlamaArgumentException(
'LiteRtLmBackend raw prompt generation needs prompt text: the '
'LiteRT-LM runtime cannot prefill an empty prompt or one that holds '
"only the model's start token.",
prompt.isEmpty
? 'LiteRtLmBackend raw prompt generation needs prompt text: the '
'prompt is empty.'
: 'LiteRtLmBackend raw prompt generation needs prompt text: the '
"prompt holds only the model's start token, which the "
'LiteRT-LM runtime adds itself.',
name: 'prompt',
invalidValue: prompt,
);
Expand Down
6 changes: 6 additions & 0 deletions lib/src/backends/litert_lm/worker_messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,13 @@ class LiteRtLmErrorResponse {
LiteRtLmErrorResponse(
this.message, {
this.kind = 'exception',
this.argumentName,
this.nativeSettlement = LiteRtLmNativeSettlement.settled,
});

/// [LlamaArgumentException.name] of the rejected argument, when known.
final String? argumentName;

/// Native lifetime, independent of the request's error kind.
final LiteRtLmNativeSettlement nativeSettlement;

Expand All @@ -437,6 +441,7 @@ class LiteRtLmErrorResponse {
return LiteRtLmErrorResponse(
response.message,
kind: response.kind,
argumentName: response.argumentName,
nativeSettlement: nativeSettlement,
);
}
Expand All @@ -459,6 +464,7 @@ class LiteRtLmErrorResponse {
return LiteRtLmErrorResponse(
details == null ? error.message : '${error.message} ($details)',
kind: kind,
argumentName: error is LlamaArgumentException ? error.name : null,
);
}
}
Expand Down
57 changes: 57 additions & 0 deletions test/unit/backends/litert_lm/litert_lm_backend_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,63 @@ void main() {
});
}

test('keeps the argument name of a worker llamaArgument error', () async {
final worker = _FakeLiteRtLmWorker(
tokenizeResponse: const <int>[],
detokenizeResponse: '',
generationErrorResponse: LiteRtLmErrorResponse(
'needs prompt text',
kind: 'llamaArgument',
argumentName: 'prompt',
),
);
final backend = LiteRtLmBackend(initialSendPort: worker.sendPort);

try {
await expectLater(
backend.generate(7, 'prompt', const GenerationParams()).drain<void>(),
throwsA(
isA<LlamaArgumentException>().having(
(error) => error.name,
'name',
'prompt',
),
),
);
} finally {
await backend.dispose();
worker.close();
}
});

test('keeps an unnamed worker llamaArgument error unnamed', () async {
final worker = _FakeLiteRtLmWorker(
tokenizeResponse: const <int>[],
detokenizeResponse: '',
generationErrorResponse: LiteRtLmErrorResponse(
'bad combination',
kind: 'llamaArgument',
),
);
final backend = LiteRtLmBackend(initialSendPort: worker.sendPort);

try {
await expectLater(
backend.generate(7, 'prompt', const GenerationParams()).drain<void>(),
throwsA(
isA<LlamaArgumentException>().having(
(error) => error.name,
'name',
isNull,
),
),
);
} finally {
await backend.dispose();
worker.close();
}
});

test('maps unknown worker generation errors to generic exceptions', () async {
final worker = _FakeLiteRtLmWorker(
tokenizeResponse: const <int>[],
Expand Down
19 changes: 15 additions & 4 deletions test/unit/backends/litert_lm/litert_lm_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,20 @@ void main() {
]);
});

for (final (name, start, prompt) in [
('an empty prompt', '', ''),
('a prompt that is only the start token', '<bos>', '<bos>'),
for (final (name, start, prompt, reason) in [
('an empty prompt', '', '', contains('is empty')),
(
'an empty prompt for a runtime with a start token',
'<bos>',
'',
allOf(contains('is empty'), isNot(contains('start token'))),
),
(
'a prompt that is only the start token',
'<bos>',
'<bos>',
contains("holds only the model's start token"),
),
]) {
test('refuses $name before native work', () async {
tokenizer(start, const {});
Expand All @@ -666,7 +677,7 @@ void main() {
.having(
(error) => error.message,
'message',
contains('needs prompt text'),
allOf(contains('needs prompt text'), reason),
),
),
);
Expand Down
10 changes: 10 additions & 0 deletions test/unit/backends/litert_lm/worker_messages_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ void main() {
'bad model (detail)',
);

final named = LiteRtLmErrorResponse.from(
LlamaArgumentException('bad value', name: 'prompt'),
);
expect(named.argumentName, 'prompt');
expect(LiteRtLmErrorResponse.from(StateError('x')).argumentName, isNull);
expect(
LiteRtLmErrorResponse.from(LlamaArgumentException('bad')).argumentName,
isNull,
);

final generic = LiteRtLmErrorResponse.from(Exception('native failed'));
expect(generic.kind, 'exception');
expect(generic.message, contains('native failed'));
Expand Down
2 changes: 2 additions & 0 deletions website/docs/changelog/recent-releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ validate against the exact release tag notes.

## Unreleased

- Keep `LlamaArgumentException.name` when native LiteRT-LM reports an argument error from its worker isolate, and say in the native LiteRT-LM raw-prompt refusal whether the prompt is empty or holds only the model's start token ([#1861](https://github.com/leehack/llamadart/issues/1861)).

## 0.19.2

- Keep default WebGPU bridge assets at `v0.1.83` (unchanged from 0.19.1).
Expand Down
Loading