From d2f58aaa5d8602bedfe3f66156954853aaf1bd00 Mon Sep 17 00:00:00 2001 From: leehack Date: Tue, 10 Nov 2026 11:45:20 -0500 Subject: [PATCH] Return the LiteRT-LM Web executor context limit from getContextSize Refs #1861 --- CHANGELOG.md | 2 ++ .../litert_lm/litert_lm_backend_web.dart | 2 +- .../litert_lm/litert_lm_backend_web_test.dart | 23 +++++++++++++++++++ website/docs/changelog/recent-releases.md | 2 ++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3033bf1b0..d7a7dbcb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ Releases before 0.10.0 are in the ## Unreleased +- `engine.getContextSize()` on LiteRT-LM Web `withTextHistory()` loads returns the context size the runtime's executor enforces when its load probe finds one below the requested size, such as Qwen3 0.6B's 4096 on WASM CPU, instead of the requested `ModelParams.contextSize` ([#1861](https://github.com/leehack/llamadart/issues/1861)). + ## 0.19.4 - Keep the default WebGPU bridge assets at `v0.1.83` (unchanged from 0.19.3). diff --git a/lib/src/backends/litert_lm/litert_lm_backend_web.dart b/lib/src/backends/litert_lm/litert_lm_backend_web.dart index b305c91a8..2add42435 100644 --- a/lib/src/backends/litert_lm/litert_lm_backend_web.dart +++ b/lib/src/backends/litert_lm/litert_lm_backend_web.dart @@ -345,7 +345,7 @@ class LiteRtLmBackend @override Future getContextSize(int contextHandle) async { _requireContextHandle(contextHandle); - return _modelParams?.contextSize ?? 0; + return _reportedContextTokens ?? _modelParams?.contextSize ?? 0; } @override diff --git a/test/unit/backends/litert_lm/litert_lm_backend_web_test.dart b/test/unit/backends/litert_lm/litert_lm_backend_web_test.dart index 3a915c873..71a47b447 100644 --- a/test/unit/backends/litert_lm/litert_lm_backend_web_test.dart +++ b/test/unit/backends/litert_lm/litert_lm_backend_web_test.dart @@ -1616,6 +1616,29 @@ void main() { } }); + test('the context size is the executor limit its probe found, else the ' + 'requested size', () async { + for (final (executorTokens, expected) in <(int?, int)>[ + (4096, 4096), + (null, 65536), + ]) { + _installCountingFakeEngine( + counts: (turn, budget) => turn == 0 + ? (decoded: budget, total: 20) + : (decoded: 100, total: 200), + maxNumTokens: 65536, + executorTokens: executorTokens, + ); + final engine = LlamaEngine(spi.LlamaBackend.withTextHistory()); + await engine.loadModelFromUrl( + 'model.litertlm', + modelParams: const ModelParams(contextSize: 65536), + ); + expect(await engine.getContextSize(), expected); + await engine.dispose(); + } + }); + test('an automatic tool loop runs a call from a finished reply and ' 'answers from its result', () async { final ran = []; diff --git a/website/docs/changelog/recent-releases.md b/website/docs/changelog/recent-releases.md index 16c891e7e..ddebd6157 100644 --- a/website/docs/changelog/recent-releases.md +++ b/website/docs/changelog/recent-releases.md @@ -13,6 +13,8 @@ validate against the exact release tag notes. ## Unreleased +- `engine.getContextSize()` on LiteRT-LM Web `withTextHistory()` loads returns the context size the runtime's executor enforces when its load probe finds one below the requested size, such as Qwen3 0.6B's 4096 on WASM CPU, instead of the requested `ModelParams.contextSize` ([#1861](https://github.com/leehack/llamadart/issues/1861)). + ## 0.19.4 - Keep default WebGPU bridge assets at `v0.1.83` (unchanged from 0.19.3).