Vllm ascend post process

From mindspore-ai/akg · View interactive version

Vllm ascend post process is an AI agent skill from the GitHub repository mindspore-ai/akg. 后处理优化 - 对vllm-ascend计算结果进行优化处理,包括Triton Kernel优化、NPU算子优化等。

How to use this skill

Skill file

vllm-ascend 后处理优化 Skill

<role>

你是一个专业的代码优化工程师,擅长对 vllm-ascend 代码仓进行后处理阶段的各种优化。

</role>


背景知识

vllm-ascend 是基于 vllm 二次开发适配 ASCEND (华为昇腾) 的推理框架,使用 PyTorch + NPU 后端,后处理使用 Triton 自定义算子 实现。

vllm-ascend 有两套采样架构:

| Rule | Value |

|------|-------|

| 采样架构 | V1 (PyTorch+NPU) / V2 (Triton) |

| 核心文件 | vllm_ascend/worker/v2/sample/ |

| 优化重点 | Triton Kernel、NPU算子、惩罚计算、Gumbel采样 |


vllm-ascend 后处理流程架构

<fetch>

核心后处理文件清单:

文件路径                                    | 作用                  | 关键函数
vllm_ascend/worker/v2/sample/sampler.py     | V2采样主入口          | AscendSampler.sample()
vllm_ascend/worker/v2/sample/penalties.py  | 核心后处理-惩罚计算   | apply_penalties_and_temperature(), _penalties_and_temperature_kernel (Triton)
vllm_ascend/worker/v2/sample/gumbel.py      | Gumbel采样           | gumbel_sample(), _gumbel_sample_kernel (Triton)
vllm_ascend/sample/sampler.py               | V1采样实现           | AscendSampler, AscendTopKTopPSampler
csrc/apply_top_k_topp_custom/               | Top-K/Top-P 自定义算子 | NPU融合算子实现

</fetch>


后处理流程详解 (V2架构)

模型输出 Logits
     │
     ▼
┌─────────────────────────────────────────┐
│ 1. 惩罚计算 (penalties.py)              │
│    - Repetition Penalty (重复惩罚)      │
│    - Frequency Penalty (频率惩罚)       │
│    - Presence Penalty (存在惩罚)        │
│    - Temperature (温度调节)              │
│    - Triton Kernel: _penalties_and_temperature_kernel
└─────────────────────────────────────────┘
     │
     ▼
┌─────────────────────────────────────────┐
│ 2. Min-P 过滤 (sampler.py)             │
│    - apply_min_p()                      │
└─────────────────────────────────────────┘
     │
     ▼
┌─────────────────────────────────────────┐
│ 3. Top-K/Top-P 过滤 (sampler.py)       │
│    - apply_top_k_top_p()                │
│    - NPU融合算子 / PyTorch原生          │
└─────────────────────────────────────────┘
     │
     ▼
┌─────────────────────────────────────────┐
│ 4. Gumbel采样 (gumbel.py)              │
│    - _gumbel_sample_kernel (Triton)     │
└─────────────────────────────────────────┘
     │
     ▼
   采样结果

关键函数实现细节

3.1 apply_penalties_and_temperature (penalties.py)

使用 Triton Kernel: _penalties_and_temperature_kernel

三种惩罚的默认生效条件:

已实现短路返回(penalties.py 第58-60行):

if not (use_penalty or use_temperature):
    # Early return to avoid loading logits.
    return

3.2 _penalties_and_temperature_kernel (Triton)

3.3 gumbel_sample (gumbel.py)


现有优化点 (vllm-ascend 已实现)

<existing_optimizations>

</existing_optimizations>


优化步骤

按以下检查项进行:

1. 识别代码路径

基于背景知识中后处理流程架构,识别后处理相关代码,遍历相关代码,分析当前后处理流程


2. 分析优化机会

基于以下优化手段,分析优化机会


3. 反思优化收益

对于每个优化点,严格按照以下步骤进行检查,在思考过程中逐个进行


4. 输出结果

输出优化结果,保存为当前目录的opt_result/vllm-ascend-post-process-result.md,必须逐条包含以下内容:

  1. 代码修改路径
  2. 修改前后代码diff,可以还原回原代码
  3. 修改类别(固定为性能优化)
  4. 优化思路与预期结果
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.