Skip to content

fix(llm): do not take sliding_window as the context length - #6325

Open
David-Wu1119 wants to merge 2 commits into
xorbitsai:mainfrom
David-Wu1119:fix/context-length-sliding-window
Open

David-Wu1119 wants to merge 2 commits into
xorbitsai:mainfrom
David-Wu1119:fix/context-length-sliding-window

Conversation

@David-Wu1119

@David-Wu1119 David-Wu1119 commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Bug

get_context_length_from_config counts sliding_window as a context length and returns the largest candidate. Qwen2 and Qwen2.5 configs declare a sliding window larger than their context:

Qwen/Qwen2.5-7B-Instruct config.json: max_position_embeddings=32768, sliding_window=131072, use_sliding_window=false
Qwen/Qwen2-7B-Instruct   config.json: max_position_embeddings=32768, sliding_window=131072, use_sliding_window=false

So a model launched with the Transformers or MLX engine without an explicit context_length gets a context length of 131072 instead of 32768. PytorchModel takes get_context_length(self._model.config) as its default, and the MLX models use get_context_length_from_config. LLM.get_runtime_context_length uses the same candidates for multimodal Transformers loaders.

from xinference.model.llm.utils import get_context_length_from_config
get_context_length_from_config({"max_position_embeddings": 32768, "sliding_window": 131072, "use_sliding_window": False})
# main: 131072    fixed: 32768

sliding_window became a candidate in #4768. Before that change, get_context_length read only max_sequence_length, seq_length and max_position_embeddings.

Fix

A sliding window is the attention window, not the context length. The declared keys (max_sequence_length, seq_length, max_position_embeddings, here and in text_config) are collected first, keeping only positive integers (normalize_runtime_positive_int, as the runtime accessor already did). sliding_window is used only when no valid declared length exists anywhere in the config, so max_position_embeddings=0 next to sliding_window=4096 still gives 4096, as on main. nested_attrs is made a tuple once, so an iterator works for both passes. A config whose only length values are invalid now gets the 2048 default instead of the invalid value. get_context_length_from_config and LLM.get_runtime_context_length share this through one helper. Configs whose sliding window is smaller than their context, such as Mistral-7B-v0.1 (4096 vs 32768) or Gemma, give the same result as before. An explicit context_length still wins.

Tests

  • New test_sliding_window_is_not_a_context_length in xinference/model/llm/tests/test_context_length.py covers 5 configs: Qwen2/2.5, Mistral-7B-v0.1, a top-level sliding window with a nested text_config length, a sliding window only, and an empty config. New test_loaded_config_sliding_window_is_not_a_context_length in test_runtime_context.py covers the runtime path. On main source 3 fail (the Qwen2 config, the nested case and the runtime case). With this change all pass.
  • test_invalid_declared_length_falls_back_to_a_valid_window (0, -1, True, None next to sliding_window=4096, load and runtime paths) and test_nested_window_is_found_with_iterator_nested_attrs (follow-up 03e6aec). They pass on main and with this change and fail on the first commit of this PR (ba5da9f), except the nested Qwen-style case, which also fails on main (131072).
  • test_context_length.py, test_runtime_context.py and test_utils.py: 437 passed on 03e6aec; the same 12 chat-template tool-history tests in test_utils.py fail on main and here.
  • xinference/model/llm/tests on ba5da9f: 3076 passed, 75 skipped with this change; on main source 3073 passed and the 3 new cases failed. The same 34 tests fail on both: chat-template tests in test_utils.py, test_deepseek_r1_chat_template.py and test_deepseek_v3_chat_template.py, test_minicpmv47.py (a missing module) and one test_llm_family.py case.
  • pre-commit run --files <changed files> passes.
  • Not run: no model was loaded. The config values above are from the public config.json files.

Found by Gemini (through Google Antigravity) while auditing xinference's LLM utilities; Claude Code checked it against the published configs, wrote the fix and the tests.

🤖 Generated with Claude Code

Copilot AI balanced review requested due to automatic review settings December 4, 2026 16:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-04T16:54:41.384078Z ba5da9f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@qinxuye qinxuye left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Qwen raw configs now resolve to 32K, and the 179 focused tests and pre-commit passed locally. One issue remains in how the shared helper selects its fallback.

Comment thread xinference/model/llm/utils.py Resolved

@qinxuye qinxuye left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

David-Wu1119 and others added 2 commits December 5, 2026 09:32
get_context_length_from_config collected sliding_window as a context
length candidate (since xorbitsai#4768) and returned the largest one. Qwen2 and
Qwen2.5 configs set sliding_window=131072 with use_sliding_window=false
next to max_position_embeddings=32768, so Transformers and MLX launches
without an explicit context_length used 131072, as did
LLM.get_runtime_context_length for multimodal Transformers loaders.

Collect the declared length keys first and fall back to sliding_window
only when a config declares none of them, through one helper shared by
both callers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The declared-length pass took any non-None value, so a config with
max_position_embeddings=0 (or a negative or boolean value) and a valid
sliding_window=4096 returned 0 on load and None at runtime, where main
returned 4096. Both passes and the nested recursion also walked
nested_attrs, so an iterator was exhausted after the first pass.

Keep only positive integers (normalize_runtime_positive_int) before
choosing between declared lengths and the sliding window, make
nested_attrs a tuple once, and let get_runtime_context_length use the
validated candidates directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@qinxuye
qinxuye force-pushed the fix/context-length-sliding-window branch from 03e6aec to 8b8cc58 Compare December 5, 2026 01:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants