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
6 changes: 5 additions & 1 deletion doc/testing_matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,11 @@ files stay in its suite-owned temporary directory.
Record source revision, runtime artifact identity and device with the result.
This row qualifies image wiring, history and lifecycle for this model and CPU;
it does not qualify audio, thinking, GPU or perceptual quality generally. The
independent `litert-lm-chat-features-smoke` retains its thinking oracle.
independent `litert-lm-chat-features-smoke` retains its thinking oracle. It
records the thinking row as `not_applicable` (with the reason) when the loaded
bundle's chat template never reads `enable_thinking` and always closes the
think block, as the Qwen3.5 `_int8`, `-VL_int8` and `_int8_gpu_composite_v0190`
bundles do.

### LiteRT-LM lifecycle regression

Expand Down
46 changes: 46 additions & 0 deletions test/unit/tooling/litert_lm_chat_features_smoke_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,52 @@ void main() {
},
);

group('thinkingSupportForTemplate', () {
const closedAlways =
'{%- if m.role == "assistant" %}<think>\n\n</think>\n\n{% endif -%}'
'{%- if add_generation_prompt %}<|im_start|>assistant\n'
'<think>\n\n</think>\n\n{% endif -%}';
const honoursSwitch =
'{%- if add_generation_prompt %}<|im_start|>assistant\n'
'{%- if enable_thinking is defined and enable_thinking is false %}'
'<think>\n\n</think>\n\n{%- else %}<think>\n{%- endif %}'
'{% endif -%}';

test('reports a template that always closes the think block as N/A', () {
final support = smoke.thinkingSupportForTemplate(closedAlways);
expect(support.canProduceThinking, isFalse);
expect(support.reason, contains('enable_thinking'));
});

test('keeps the assertion for a template that reads enable_thinking', () {
expect(
smoke.thinkingSupportForTemplate(honoursSwitch).canProduceThinking,
isTrue,
);
});

test('keeps the assertion when only history closes the think block', () {
const openInPrompt =
'{%- if m.role == "assistant" %}<think>\n\n</think>\n\n{% endif -%}'
'{%- if add_generation_prompt %}<|im_start|>assistant\n<think>\n'
'{% endif -%}';
expect(
smoke.thinkingSupportForTemplate(openInPrompt).canProduceThinking,
isTrue,
);
});

test('keeps the assertion without a readable or closed-think template', () {
expect(smoke.thinkingSupportForTemplate(null).canProduceThinking, isTrue);
expect(
smoke
.thinkingSupportForTemplate('<|im_start|>assistant\n<think>\n')
.canProduceThinking,
isTrue,
);
});
});

test('prints usage and exits 64 without a model path', () async {
final smoke = await compileDartScript(
'tool/litert_lm_chat_features_smoke.dart',
Expand Down
14 changes: 14 additions & 0 deletions test/unit/tooling/test_matrix_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,20 @@ void main() {
},
);

test('documents the LiteRT-LM chat smoke thinking N/A', () {
final row = testMatrixRows.singleWhere(
(row) => row.id == 'litert-lm-chat-features-smoke',
);

expect(
row.covers.replaceAll(RegExp(r'\s+'), ' '),
contains(
'not_applicable with a reason when the bundle template always '
'closes the think block, as for the Qwen3.5 int8 bundles',
),
);
});

test('includes targeted local model smoke rows', () {
final ids = testMatrixRows.map((row) => row.id).toSet();

Expand Down
79 changes: 73 additions & 6 deletions tool/litert_lm_chat_features_smoke.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';

import 'package:llamadart/backend.dart';
import 'package:llamadart/llamadart.dart';
import 'package:llamadart/src/backends/litert_lm/litert_lm_bundle_template.dart';

import 'audio_chat_smoke_support.dart';
import 'litert_lm_tokenizer_fixture.dart';
Expand Down Expand Up @@ -83,6 +84,9 @@ Future<void> main(List<String> args) async {
);
final toolLoopCutoff = await _runToolLoopCutoffScenario(engine);

final thinkingSupport = thinkingSupportForTemplate(
readLiteRtLmBundleChatTemplate(modelPath),
);
final thinking = await _runScenario(
engine: engine,
messages: const [
Expand Down Expand Up @@ -229,7 +233,13 @@ Future<void> main(List<String> args) async {
'plain': plain.toJson(),
'cutoff': cutoff.toJson(),
'toolLoopCutoff': toolLoopCutoff,
'thinking': thinking.toJson(),
'thinking': {
if (!thinkingSupport.canProduceThinking) ...{
'status': 'not_applicable',
'reason': thinkingSupport.reason,
},
...thinking.toJson(),
},
'textHistory': textHistory.toJson(),
'sessionStructuredOutput': sessionStructuredOutput,
'toolCall': toolCall.toJson(),
Expand All @@ -254,6 +264,7 @@ Future<void> main(List<String> args) async {
plain: plain,
cutoff: cutoff,
thinking: thinking,
thinkingSupport: thinkingSupport,
textHistory: textHistory,
toolCall: toolCall,
requiredUnsupportedExpected: requiredUnsupportedExpected,
Expand Down Expand Up @@ -331,10 +342,45 @@ Future<Map<String, Object?>> runSessionStructuredOutputScenario(
return result;
}

/// Whether the chat template a loaded bundle renders can produce thinking.
class ThinkingSupport {
const ThinkingSupport.supported() : canProduceThinking = true, reason = null;

const ThinkingSupport.unsupported(String this.reason)
: canProduceThinking = false;

final bool canProduceThinking;
final String? reason;
}

final RegExp _closedThinkBlock = RegExp(r'<think>\s*</think>');

/// Judges [template], the chat template embedded in a loaded bundle.
///
/// A heuristic for the known always-closed shape: a template that never
/// mentions `enable_thinking` and closes an empty think block in its generation
/// prompt is treated as unable to think. Any other template, or an unreadable
/// one, keeps the thinking assertion.
ThinkingSupport thinkingSupportForTemplate(String? template) {
final generationStart = template?.lastIndexOf('add_generation_prompt') ?? -1;
if (template == null ||
generationStart < 0 ||
template.contains('enable_thinking') ||
!_closedThinkBlock.hasMatch(template.substring(generationStart))) {
return const ThinkingSupport.supported();
}
return const ThinkingSupport.unsupported(
'the bundle chat template never reads enable_thinking and its generation '
'prompt always closes an empty <think></think> block, so it cannot '
'produce thinking',
);
}

void _verifyResult({
required _ScenarioResult plain,
required _ScenarioResult cutoff,
required _ScenarioResult thinking,
required ThinkingSupport thinkingSupport,
required _ScenarioResult textHistory,
required _RequiredToolScenarioResult toolCall,
required bool requiredUnsupportedExpected,
Expand Down Expand Up @@ -365,11 +411,32 @@ void _verifyResult({
'${cutoff.finishReason}, not length.',
);
}
if (thinking.thinking.trim().isEmpty) {
throw StateError('LiteRT-LM thinking scenario produced no thinking delta.');
}
if (thinking.content.trim().isEmpty) {
throw StateError('LiteRT-LM thinking scenario produced no visible answer.');
if (thinkingSupport.canProduceThinking) {
if (thinking.thinking.trim().isEmpty) {
throw StateError(
'LiteRT-LM thinking scenario produced no thinking delta.',
);
}
if (thinking.content.trim().isEmpty) {
throw StateError(
'LiteRT-LM thinking scenario produced no visible answer.',
);
}
} else {
if (thinking.thinking.trim().isNotEmpty) {
throw StateError(
'LiteRT-LM thinking scenario produced a thinking delta although its '
'template cannot think.',
);
}
if (thinking.content.trim().isEmpty) {
throw StateError(
'LiteRT-LM thinking scenario produced no visible answer.',
);
}
stdout.writeln(
'N/A litert_lm_chat_features thinking: ${thinkingSupport.reason}',
);
}
if (!textHistory.content.contains('PINE') ||
textHistory.thinking.isNotEmpty ||
Expand Down
4 changes: 3 additions & 1 deletion tool/testing/test_matrix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,9 @@ const List<TestMatrixRow> testMatrixRows = <TestMatrixRow>[
covers:
'real .litertlm chat, stop and maxTokens length finish reasons, an '
'automatic tool loop cut off at maxTokens, thinking channel '
'reassembly, streaming tool calls, and optional byte-backed audio '
'reassembly (reported not_applicable with a reason when the bundle '
'template always closes the think block, as for the Qwen3.5 int8 '
'bundles), streaming tool calls, and optional byte-backed audio '
'question answering with an exact expected answer',
command:
'dart run tool/testing/run_local_e2e.dart --scenario '
Expand Down
13 changes: 12 additions & 1 deletion website/docs/getting-started/model-families.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ To see what a model will receive, render the prompt with

| Family | Artifacts | Detected format | Evidence | Example repo |
| --- | --- | --- | --- | --- |
| Qwen3.5 | GGUF, `.litertlm` | Qwen3-Coder XML (GGUF); Qwen3 template (`.litertlm`) | Validated: GGUF on CPU, Metal, CUDA, Vulkan and WebGPU; LiteRT-LM on CPU and GPU | `ggml-org/Qwen3.5-0.8B-GGUF`, `litert-community/Qwen3.5-0.8B` |
| Qwen3.5 | GGUF, `.litertlm` | Qwen3-Coder XML (GGUF); bundle's own template (`.litertlm`) | Validated: GGUF on CPU, Metal, CUDA, Vulkan and WebGPU; LiteRT-LM on CPU and GPU (no thinking, see below) | `ggml-org/Qwen3.5-0.8B-GGUF`, `litert-community/Qwen3.5-0.8B` |
| Gemma 4 | GGUF, `.litertlm` | Gemma 4 | Validated: GGUF and LiteRT-LM | `unsloth/gemma-4-E2B-it-GGUF`, `litert-community/gemma-4-E2B-it-litert-lm` |
| Qwen3 | GGUF, `.litertlm` | Hermes | Validated: LiteRT-LM. Template-tested: GGUF | `litert-community/Qwen3-0.6B` |
| Gemma 3, 3n, 2 | GGUF, `.litertlm` | Gemma (generic JSON tool calls) | Validated: LiteRT-LM. Template-tested: GGUF | `litert-community/Gemma3-1B-IT` |
Expand Down Expand Up @@ -76,6 +76,17 @@ thought channel or Mistral's `[THINK]`, the parser returns reasoning
separately from the reply. See
[Text generation and streaming](../guides/generation-and-streaming).

Qwen3.5 `.litertlm` bundles render their own embedded chat template
([#1134](https://github.com/leehack/llamadart/issues/1134)), so
`ModelParams.chatTemplate` is ignored for these files. The
`Qwen3.5-0.8B_int8`, `Qwen3.5-0.8B-VL_int8` and
`Qwen3.5-0.8B_int8_gpu_composite_v0190` bundles (LiteRT-LM 0.19 and earlier)
close an empty `<think>` block in the generation prompt and never read
`enable_thinking`, so `enableThinking: true` produces no reasoning there. The
`Qwen3.5-0.8B_dynamic_wi8_afp32` bundle reads `enable_thinking` but needs
LiteRT-LM 0.20.0 or later, so it fails on the pinned 0.19.1 runtime. Use the
GGUF for Qwen3.5 thinking; Qwen3 and Gemma 4 `.litertlm` bundles support it.

## Vision and audio

GGUF vision and audio need a matching projector (`mmproj`) file. Gemma 4 E2B
Expand Down
Loading