diff --git a/2025-Ascend-Innovation-Contest/S1/MultiModal/545yyds/README.md b/2025-Ascend-Innovation-Contest/S1/MultiModal/545yyds/README.md new file mode 100644 index 00000000..21329e92 --- /dev/null +++ b/2025-Ascend-Innovation-Contest/S1/MultiModal/545yyds/README.md @@ -0,0 +1,88 @@ +# MindNLP 模型优化详细说明 (janus_model & Qwen) + +本文档详细记录了针对 janus_model 和 Qwen 模型的关键性能优化点,并附带了相应的核心代码实现。 + +## 1. janus_model 模型优化 + +### 1.1 Attention 计算算子优化 优化痛点: + +优化痛点:原有的 Attention 实现中使用 `ops.transpose` 结合 `view` 对 Query、Key、Value 状态进行维度变换。在 MindSpore 的 Ascend 后端执行时,`ops.transpose` 可能会产生额外的算子开销或内存重排效率不如专用算子库。 + +改进方案:将通用的 `ops.transpose` 替换为 MindSpore Mint 模块下的 `mindspore.mint.swapaxes`。Mint 系列算子通常针对 PyTorch 语义对齐及底层硬件(如 NPU)进行了更深度的适配和融合优化,能够提升维度交换操作的效率。 + +**源码实现** (`mindnlp/transformers/models/llama/modeling_llama.py`): + +**Python** + +```python +# 修改前: +query_states = ops.transpose(query_states.view(bsz, q_len, self.num_heads, self.head_dim), 1, 2) +key_states = ops.transpose(key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim), 1, 2) +value_states = ops.transpose(value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim), 1, 2) + +# 修改后: +query_states = mindspore.mint.swapaxes(query_states.view(bsz, q_len, self.num_heads, self.head_dim), 1, 2) +key_states = mindspore.mint.swapaxes(key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim), 1, 2) +value_states = mindspore.mint.swapaxes(value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim), 1, 2) +``` + + + +## 2. Qwen2-VL 模型优化 + +### 2.1 RoPE 与 Attention 逻辑优化 (针对 Qwen2-VL) + +优化痛点: + +1. **RoPE 切片操作**: 在旋转位置编码(RoPE)的 `rotate_half` 函数中,使用切片索引 `x[..., :half]` 和 `x[..., half:]` 可能会导致非连续内存访问,增加数据搬运开销。 +2. **Attention 掩码拷贝**: 在准备 causal mask 时,`causal_mask.copy()` 执行了深拷贝,导致不必要的内存占用和拷贝耗时。 +3. **算子选择**: 同样存在使用 Tensor 自身的 `.swapaxes` 方法而非高性能 `mint` 算子的问题。 + +改进方案: + +1. **RoPE**: 使用 `ops.split` 替代手动切片,在图编译层面更易优化,减少 strided slice 带来的开销。 +2. **Mask**: 去除 `.copy()` 操作,直接在原 Tensor 上操作或广播,减少内存拷贝。 +3. **Attention**: 全面将 `.swapaxes` 替换为 `mindspore.mint.swapaxes`。 + +**源码实现** (`mindnlp/transformers/models/qwen2_vl/modeling_qwen2_vl.py`): + +**Python** + +```python +# 1. RoPE 优化 (rotate_half) +def rotate_half(x): + """Rotates half the hidden dims of the input.""" + # 修改前: x1 = x[..., : x.shape[-1] // 2]; x2 = x[..., x.shape[-1] // 2 :] + # 修改后: 使用 ops.split + x1, x2 = ops.split(x, x.shape[-1] // 2, dim=-1) + return ops.cat((-x2, x1), dim=-1) + +# 2. Attention 算子与 Mask 优化 (Qwen2VLAttention) +# 修改前: +causal_mask = causal_mask.copy() +# 修改后: 注释掉 copy +causal_mask = causal_mask.copy() # copy to contiguous memory for in-place edit + +# 3. Attention 维度交换优化 +# 修改前: +query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).swapaxes(1, 2) +attn_weights = ops.matmul(query_states, key_states.swapaxes(2, 3)) / math.sqrt(self.head_dim) + +# 修改后: +query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim) +query_states = mindspore.mint.swapaxes(query_states, 1, 2) +# ... (key/value 同理) +attn_weights = ops.matmul(query_states, mindspore.mint.swapaxes(key_states, 2, 3)) / math.sqrt(self.head_dim) +``` + + + + +## 评测结果 + +| 评测指标 | 平均得分 | +|---------|---------| +| 峰值显存得分 | 100 | +| Prefill时延得分 | 106.8524 | +| Decode时延得分 | 111.755 | +| **总分** | **106.2025** | \ No newline at end of file diff --git a/2025-Ascend-Innovation-Contest/S1/MultiModal/545yyds/patches.zip b/2025-Ascend-Innovation-Contest/S1/MultiModal/545yyds/patches.zip new file mode 100644 index 00000000..f3ca8066 Binary files /dev/null and b/2025-Ascend-Innovation-Contest/S1/MultiModal/545yyds/patches.zip differ