diff --git a/doc/testing_matrix.md b/doc/testing_matrix.md index 2947d60c0..8ae551cd6 100644 --- a/doc/testing_matrix.md +++ b/doc/testing_matrix.md @@ -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 diff --git a/test/unit/tooling/litert_lm_chat_features_smoke_test.dart b/test/unit/tooling/litert_lm_chat_features_smoke_test.dart index c81339ba1..f1dec3cc6 100644 --- a/test/unit/tooling/litert_lm_chat_features_smoke_test.dart +++ b/test/unit/tooling/litert_lm_chat_features_smoke_test.dart @@ -196,6 +196,52 @@ void main() { }, ); + group('thinkingSupportForTemplate', () { + const closedAlways = + '{%- if m.role == "assistant" %}\n\n\n\n{% endif -%}' + '{%- if add_generation_prompt %}<|im_start|>assistant\n' + '\n\n\n\n{% endif -%}'; + const honoursSwitch = + '{%- if add_generation_prompt %}<|im_start|>assistant\n' + '{%- if enable_thinking is defined and enable_thinking is false %}' + '\n\n\n\n{%- else %}\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" %}\n\n\n\n{% endif -%}' + '{%- if add_generation_prompt %}<|im_start|>assistant\n\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\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', diff --git a/test/unit/tooling/test_matrix_test.dart b/test/unit/tooling/test_matrix_test.dart index e332cc5f5..926fc83c5 100644 --- a/test/unit/tooling/test_matrix_test.dart +++ b/test/unit/tooling/test_matrix_test.dart @@ -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(); diff --git a/tool/litert_lm_chat_features_smoke.dart b/tool/litert_lm_chat_features_smoke.dart index 0e9abd54c..5a2f548f7 100644 --- a/tool/litert_lm_chat_features_smoke.dart +++ b/tool/litert_lm_chat_features_smoke.dart @@ -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'; @@ -83,6 +84,9 @@ Future main(List args) async { ); final toolLoopCutoff = await _runToolLoopCutoffScenario(engine); + final thinkingSupport = thinkingSupportForTemplate( + readLiteRtLmBundleChatTemplate(modelPath), + ); final thinking = await _runScenario( engine: engine, messages: const [ @@ -229,7 +233,13 @@ Future main(List 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(), @@ -254,6 +264,7 @@ Future main(List args) async { plain: plain, cutoff: cutoff, thinking: thinking, + thinkingSupport: thinkingSupport, textHistory: textHistory, toolCall: toolCall, requiredUnsupportedExpected: requiredUnsupportedExpected, @@ -331,10 +342,45 @@ Future> 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'\s*'); + +/// 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 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, @@ -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 || diff --git a/tool/testing/test_matrix.dart b/tool/testing/test_matrix.dart index 6cd9fce82..f35eff45c 100644 --- a/tool/testing/test_matrix.dart +++ b/tool/testing/test_matrix.dart @@ -1285,7 +1285,9 @@ const List testMatrixRows = [ 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 ' diff --git a/website/docs/getting-started/model-families.md b/website/docs/getting-started/model-families.md index 74e47e607..3e6d1d1ad 100644 --- a/website/docs/getting-started/model-families.md +++ b/website/docs/getting-started/model-families.md @@ -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` | @@ -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 `` 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