Skip to content

feat: support qwen3 dspark decoding on NPU with Python executor. - #2263

Open
yinjiawei01 wants to merge 1 commit into
xLLM-AI:mainfrom
yinjiawei01:features/qwen3_dspark_python_npu
Open

feat: support qwen3 dspark decoding on NPU with Python executor.#2263
yinjiawei01 wants to merge 1 commit into
xLLM-AI:mainfrom
yinjiawei01:features/qwen3_dspark_python_npu

Conversation

@yinjiawei01

Copy link
Copy Markdown
Collaborator

背景

当前 xLLM 已支持基于 C++/ATB 模型的 DSpark 推理,但 Python NPU 模型链路尚缺少以下能力:

  • Target 模型中间层 hidden states 捕获;
  • Target 与 Draft 模型之间的权重共享;
  • Python Draft 模型的 Markov Head 和 Confidence Head;
  • Python 模型与现有 C++ DSpark Worker 的接口对接。

本 PR 为 Qwen3 dense Python NPU 模型补充 DSpark 支持,同时抽取少量可供后续模型复用的通用组件。

主要改动

1. Target 中间层捕获

  • 新增 AuxHiddenCapture,按 layers_to_capture 捕获 Qwen3 指定层的 residual hidden states;
  • 保持配置中的层顺序,并将多层 hidden states 拼接后传递给 Draft 模型;
  • 当未配置捕获层时,Qwen3 仍返回普通 Tensor,不影响原有执行链路;
  • 扩展 Python executor,使其能够处理 (hidden_states, aux_hidden_states) 输出。

2. 通用 DSpark Python 组件

新增模型无关的 DSpark 基础组件:

  • DSparkMarkovHead:根据前一个 token 计算低秩 Markov bias;
  • DSparkConfidenceHead:计算每个 draft token 的置信度;
  • DSparkForCausalLMBase:向 C++ runtime 暴露 Markov bias、Confidence Head 和权重共享接口。

这些组件可直接复用于后续其他 Python 模型的 DSpark 适配,模型实现只需负责 backbone、权重加载和 context hidden 注入。

3. Qwen3 DSpark Draft 模型

新增 Qwen3DSparkForCausalLM

  • 支持 DSpark checkpoint 配置解析和校验;
  • 加载 Draft Transformer、context projection、Markov Head 和 Confidence Head 权重;
  • 使用非因果 attention 完成 block draft forward;
  • 将 Target 捕获的多层 hidden states 投影为 Draft context;
  • 支持从 Target 共享 embedding 和 LM Head;
  • 当前要求 Target 与 Draft vocabulary size 一致。

4. C++ 与 Python 模型桥接

扩展 PyCausalLM

  • 支持获取 Python 模型输出的 auxiliary hidden states;
  • 转发 DSpark Markov bias 和 Confidence Head 调用;
  • 支持 Target 到 Draft 的 Python 权重共享;
  • 将 Qwen3 DSpark 配置中的 markov_rank、Confidence Head 开关传递给 Python 模型。

同时调整 DFlash/DSpark 初始化逻辑,优先调用统一的 share_weights_from 接口,并保留原有 C++/ATB 模型的权重共享方式。

5. 配置与注册

  • 新增 python_model_path 配置,用于指定 Python 模型模块搜索路径;
  • 注册 DSparkDraftModel 到 Python 模型 Registry;
  • Python 模型模式下自动使用 TORCH NPU kernel backend;
  • Draft worker 不再创建 tokenizer,也不会执行仅属于 Target worker 的 JSON grammar 预热;
  • 支持从 Qwen3 Draft config 读取:
    • markov_rank
    • enable_confidence_head
    • confidence_head_with_markov

执行流程

Target Qwen3 Python forward
  → 捕获配置指定层的 hidden states
  → Python executor 返回 hidden + aux_hidden
  → DSpark Worker 构建 block draft 输入
  → Qwen3 DSpark Draft 注入 target context
  → 非因果 block forward 生成 base logits
  → Markov Head 根据前序 token 修正每个位置的 logits
  → Confidence Head 输出 draft token 置信度
  → 复用现有 DSpark validation 和 rejection sampling

测试覆盖

新增以下测试:

  • DSpark Markov Head 和 Confidence Head;
  • Qwen3 中间层捕获及输出顺序;
  • Qwen3 DSpark 配置校验;
  • Draft 非因果 attention;
  • context projection 和 KV cache 写入;
  • Draft checkpoint 权重加载;
  • Python 模型 Registry;
  • Qwen3 DSpark 配置到 ModelArgs 的传递。

@yinjiawei01
yinjiawei01 force-pushed the features/qwen3_dspark_python_npu branch from 7f17cfe to 16e3019 Compare August 18, 2026 14:34
@yinjiawei01 yinjiawei01 changed the title feat: support qwen3 dspark decoding with python npu models. feat: support qwen3 dspark decoding on NPU with Python executor. Aug 18, 2026
@@ -0,0 +1,47 @@
"""Intermediate residual-stream capture for Python target models."""

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Because this is a newly added source file, starting directly with the module docstring omits the required copyright and license header. Please add the 2026 project header as required by .agents/skills/code-review/references/custom-code-style.md:39-49.

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.

2 participants