Skip to content

feat: add modular vLLM post-install patch framework with hybrid KV cache fix - #4879

Merged
qinxuye merged 6 commits into
xorbitsai:mainfrom
m199369309:feat/vllm-modular-patches
May 7, 2026
Merged

feat: add modular vLLM post-install patch framework with hybrid KV cache fix#4879
qinxuye merged 6 commits into
xorbitsai:mainfrom
m199369309:feat/vllm-modular-patches

Conversation

@m199369309

@m199369309 m199369309 commented May 7, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Add xinference/model/llm/vllm/patches/ module with auto-discovery registry for applying post-install patches to vLLM site-packages
  • Implement hybrid KV cache page size patch (LCM-based padding) for Qwen3.5/3.6/3.6-MoE models, working around vllm-project/vllm#37121
  • Use subprocess-based vLLM path resolution to correctly handle uv-managed virtual environments and .pth-injected parent site-packages
  • Filter patches by model architectures (not model names), automatically covering fine-tuned and custom-registered models

Motivation

Qwen3.5/3.6 models use hybrid attention (linear_attention + full_attention) with non-divisible KV cache page sizes. vLLM 0.20.x raises NotImplementedError during unify_kv_cache_spec_page_size(). The official fix (vllm-project/vllm#40128) has not been merged.

Previous monkey-patching approach fails because vLLM's EngineCore uses spawn multiprocessing (due to CUDA initialization), which doesn't inherit in-memory patches. File-level patching solves this by modifying the source directly in site-packages.

Design

xinference/model/llm/vllm/patches/
├── __init__.py                          # Auto-discovery registry + apply_vllm_patches()
├── _base.py                             # VllmPatch dataclass
└── hybrid_kv_cache_page_size.py         # Qwen3.5/3.6 KV cache patch
  • Auto-discovery: pkgutil.iter_modules scans for modules exposing PATCH attribute
  • Architecture filtering: Each patch declares architectures set; uses _resolve_architectures() to cover fine-tuned models that inherit from built-in families
  • Idempotent: Marker-based detection prevents double-patching
  • Subprocess path resolution: Uses venv's own Python interpreter to locate vLLM via importlib.util.find_spec, handling .pth injections and uv-managed envs
  • Fallback: Glob pattern for standard venv layouts

Changes

File Change
xinference/model/llm/vllm/patches/__init__.py New: registry + entry point
xinference/model/llm/vllm/patches/_base.py New: VllmPatch dataclass
xinference/model/llm/vllm/patches/hybrid_kv_cache_page_size.py New: KV cache patch with subprocess path resolution
xinference/core/worker.py Add model_name/architectures params to _prepare_virtual_env, call apply_vllm_patches after install

Removal condition

When vLLM officially merges the hybrid KV cache fix (tracking: vllm-project/vllm#37121, #38041), delete the patch file. The framework (__init__.py, _base.py) can remain for future patches or be removed entirely (worker.py has ImportError guard).

Test plan

  • Deploy Qwen3.6-35B-A3B-FP8 with vLLM 0.20.x on multi-GPU worker
  • Verify worker logs show: Applied hybrid KV cache patch to .../kv_cache_utils.py
  • Verify logs show: Hybrid KV cache: padding max page size 1073152 -> 1081344
  • Verify no NotImplementedError during model initialization
  • Verify non-hybrid models (e.g., Qwen2.5) are unaffected (patch skipped)
  • Verify fine-tuned model with model_family="qwen3.6" gets patch applied via architecture inheritance

…che fix

Add a patches/ module with auto-discovery registry for applying
post-install patches to vLLM site-packages. This enables file-level
patching that survives spawn subprocesses (unlike monkey-patching).

First patch: LCM-based page size padding for hybrid attention models
(Qwen3.5/3.6/3.6-MoE) that works around vllm-project/vllm#37121.

Key improvements:
- Use subprocess-based vLLM path resolution to handle uv-managed
  envs and .pth-injected parent site-packages
- Model-family filtering (zero I/O for non-applicable models)
- Idempotent with marker detection
- Auto-discovery via pkgutil (add new patch = add new file)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@XprobeBot XprobeBot added this to the v2.x milestone May 7, 2026
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Comment thread xinference/model/llm/vllm/patches/__init__.py Outdated
m199369309 and others added 2 commits May 7, 2026 16:13
Use model architectures instead of model family names for patch
filtering. This automatically covers fine-tuned models and future
models sharing the same architecture (e.g. Qwen3_5MoeForConditionalGeneration)
without manual maintenance.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Cover non-MoE Qwen3.5 models that also use hybrid attention
and trigger the same KV cache page size issue.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

@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

@qinxuye

qinxuye commented May 7, 2026

Copy link
Copy Markdown
Contributor

Tests failed, seemed that the other model types are effected.

m199369309 and others added 2 commits May 7, 2026 19:32
Use getattr with fallback for model families that don't define
_resolve_architectures (EmbeddingModelFamilyV2, RerankModelFamilyV2,
ImageModelFamilyV2, AudioModelFamilyV2), preventing AttributeError
when loading non-LLM models with virtual environments.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@qinxuye
qinxuye merged commit 61569f7 into xorbitsai:main May 7, 2026
11 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants