-
Notifications
You must be signed in to change notification settings - Fork 104
2025年昇腾AI创新大赛-昇思模型开发挑战赛(S1赛季)--MultiModal赛题--今天要吃三碗饭队提交 #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fangfangssj
wants to merge
3
commits into
mindspore-lab:dev
Choose a base branch
from
fangfangssj:dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
2025-Ascend-Innovation-Contest/S1/MultiModal/今天要吃三碗饭/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # MindNLP VLM 推理优化 | ||
|
|
||
| ## 概述 | ||
|
|
||
| 针对 MindNLP 框架下的 **Llama**、**Qwen2-VL** 和 **Janus/Janus-Pro** 模型进行了深度的推理性能优化。 | ||
fangfangssj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 优化核心在于全面迁移至 **MindSpore Mint** 算子体系、启用 **PyBoost** 加速模式、重构**图像预处理流水线**,以及优化 Attention 和 RoPE的计算逻辑。旨在显著降低端到端推理延迟,特别是提升多模态模型的首字(Prefill)响应速度。 | ||
|
|
||
| ## 核心优化内容 | ||
|
|
||
| ### 1\. 算子迁移与 PyBoost 加速 (Operator Optimization) | ||
|
|
||
| * **Mint 算子全面替换:** | ||
| * 将大量遗留的 `mindspore.ops` 算子替换为高性能的 `mindspore.mint` 算子(如 `mint.split`, `mint.cat`, `mint.transpose`, `mint.matmul`, `mint.softmax`, `mint.narrow` 等)。 | ||
fangfangssj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * **收益:** `mint` 算子能更好地映射到底层 Ascend 硬件算子,并提供更接近 PyTorch 的语义,减少了框架层的算子分发开销。 | ||
| * **启用 PyBoost:** | ||
| * 在模型初始化阶段显式调用 `set_pyboost(True)`。 | ||
| * **收益:** 显著降低了小算子的 Kernel Launch(内核启动)开销,直接提升了 Decoding 阶段的 Token 生成速度。 | ||
| * **内存池调优:** | ||
| * 增加了 `mindspore.runtime.set_memory` 配置 (`init_size='2GB'`, `optimize_level='O1'`),优化内存块分配策略,减少内存碎片。 | ||
|
|
||
| ### 2\. 高性能图像预处理 (High-Performance Image Preprocessing) | ||
fangfangssj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| * **从 PIL/NumPy 迁移至 MindData Vision:** | ||
| * 重构了 `Qwen2VLImageProcessor` 和 `Janus` 的 `VLMImageProcessor`。 | ||
| * 将原本基于 Python/NumPy 的 `Resize`、`Rescale`、`Normalize` 操作,替换为 `mindspore.dataset.vision` 下的 C++ 后端算子。 | ||
| * **收益:** 解决了高分辨率图像处理中的 CPU 瓶颈,大幅缩短了多模态输入时的**首字延迟(First Token Latency)**。 | ||
|
|
||
| ### 3\. 模型架构层面的优化 (Model Architecture Optimizations) | ||
|
|
||
| #### **Llama 系列** | ||
fangfangssj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| * **RoPE (旋转位置编码) 优化:** | ||
| * 优化了 `apply_rotary_pos_emb` 和 `rotate_half` 函数。 | ||
| * 移除了不必要的 Tensor 切片与拼接操作,部分实现采用了 `mint.split` 或优化后的广播机制,提升了位置编码的计算效率。 | ||
| * **线性投影层重构:** | ||
| * 在 `LlamaMLP` 和 `LlamaAttention` 中,优化了 `q_proj`, `k_proj`, `v_proj` 及其权重的分割逻辑,减少了冗余的内存拷贝。 | ||
|
|
||
| #### **Qwen2-VL & Janus** | ||
|
|
||
| * **3D 卷积优化:** | ||
| * 将视频/图像 Patch Embedding 中的 `Conv3d` 实现替换为 `mindspore.mint.nn.functional.conv3d`,直接调用底层高性能算子。 | ||
| * **Attention Mask 计算优化:** | ||
| * 移除了低效的 Python 循环生成 Mask 的逻辑。 | ||
| * 改用向量化的 `mint.arange`、`mint.where` 和 `mint.narrow` 进行 Mask 生成与切片,避免了大显存的占用和 host 侧的计算耗时。 | ||
| * **多模态 RoPE 优化:** | ||
| * 预计算 Sin/Cos 表,避免在前向传播中重复计算;优化了多模态位置编码的广播(Broadcast)逻辑。 | ||
|
|
||
| ### 4\. 生成流程优化 (Generation Pipeline) | ||
|
|
||
| * **KV Cache 更新机制:** 优化了 `past_key_value` 的更新逻辑,减少内存复制。 | ||
fangfangssj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * **停止条件判断 (Stopping Criteria):** | ||
| * 将 `generation/utils.py` 中判断 `unfinished_sequences` 的逻辑从标量同步修改为向量化操作 (`unfinished_sequences.max() == 0`),减少了 Device 到 Host 的同步等待时间。 | ||
|
|
||
| ## 性能收益 | ||
|
|
||
| 应用后,预期在 Ascend 硬件上获得以下提升: | ||
|
|
||
| 1. **更低的推理延迟 (Lower Latency):** 得益于 Mint 算子和 PyBoost,单 Token 生成时间(Decode Latency)显著减少。 | ||
| 2. **更快的首字响应 (Faster Prefill):** 图像预处理迁移至 C++ 后端后,VLM 模型的图片编码耗时大幅降低。 | ||
| 3. **显存效率提升:** 内存池调优和 In-place 风格的算子减少了峰值显存占用。 | ||
fangfangssj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.