mcpField
H
huawei-cloud-ascend-small-model-migrate

huawei-cloud-ascend-small-model-migrate

github.com/huaweicloud开发59292 次安装2 天前更新
代码仓库

关于 huawei-cloud-ascend-small-model-migrate

huawei-cloud-ascend-small-model-migrate 指导团队将受支持的视觉模型迁移到 Ascend NPU,涵盖结构分析、推理验证、性能分析和优化建议。它还提供资源估算与性能基线比较。

主要功能

  1. 01分析模型结构并确定迁移路径
  2. 02验证模型在 Ascend NPU 上推理
  3. 03采集并分析算子性能数据
  4. 04提供性能优化建议
  5. 05估算资源并比较性能基线

适用场景

  1. 01将 YOLO 或 ResNet 模型从 GPU 迁移到 Ascend
  2. 02检查迁移后的模型准确率
  3. 03查找视觉工作负载中的算子瓶颈
  4. 04估算 NPU 内存和推理性能

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/huaweicloud/huaweicloud-skills --skill huawei-cloud-ascend-small-model-migrate

安装后在 Claude Code 中输入/huawei-cloud-ascend-small-model-migrate确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
acceptance-criteria.md
migration-scripts.md
profiler-sql.md
report-template.md
troubleshooting.md
verification-method.md
scripts/
analyze_model.py

详细介绍

华为云昇腾小模型迁移

概述

本技能介绍将小型视觉模型迁移到昇腾 NPU 的工作流,涵盖结构分析 → 迁移验证 → 性能优化。

架构:模型分析 → 环境配置 → NPU 推理 → 性能分析 → 瓶颈分析 → 优化建议

相关技能:

  • huawei-cloud-msmodelslim-model-analysis - 用于确定迁移路径的模型结构分析
  • huawei-cloud-msot-msopprof-operator-profiler - 算子性能数据采集
  • huawei-cloud-ascend-profiler-db-explorer - 用于识别瓶颈的性能分析数据库
  • huawei-cloud-ascendc-operator-performance-optim - 可选:针对瓶颈算子的 AscendC 算子优化

架构组件

本技能涉及以下云服务和组件:

  • 昇腾 NPU:模型部署的目标硬件(Ascend 910B 系列)
  • torch_npu:昇腾 NPU 的 PyTorch 适配器
  • MSProf:用于性能分析的昇腾性能分析工具
  • Ultralytics:YOLO 模型框架支持
  • Docker:用于实现一致部署的容器环境

使用场景

典型问题场景:

  • 将视觉模型从 GPU 迁移到昇腾 NPU
  • 在昇腾硬件上部署 YOLO/ResNet/UNet 模型
  • 优化 NPU 上的小模型性能
  • 验证迁移后的模型精度
  • 识别计算机视觉模型中的性能瓶颈

典型用户表述:

  • “将 YOLOv8 迁移到昇腾 NPU
  • “如何在昇腾上运行 ResNet?
  • “优化 NPU 上的 UNet 推理
  • “验证迁移后的模型精度
  • “分析我的视觉模型中的性能瓶颈
  • “YOLOModelMigrationAscendNPU
  • “AscendModel?
  • “ModelMigrationNPU?

范围

支持:

  • 仅编码器架构(ResNet、VGG、EfficientNet)
  • 检测模型(YOLO、Faster-RCNN、SSD)
  • 分割模型(UNet、DeepLab)
  • 其他非仅解码器大模型

不支持:

  • 仅解码器大模型(Qwen、LLaMA、DeepSeek)——需要基于适配器的量化方法
  • 理解型 VLM 文本主干网络——需要基于适配器的量化方法

工作流

┌─────────────────────────────────────────────────────────────┐
│  Step 1: Model Structure Analysis                           │
│  → Determine msmodelslim compatibility                      │
│  → Output structure analysis + migration path suggestion    │
└─────────────────────────────────────────────────────────────┘
                              ↓
┌─────────────────────────────────────────────────────────────┐
│  Step 2: Environment Preparation + Migration Verification  │
│  → Configure torch_npu environment                         │
│  → Run inference test                                      │
│  → Verify accuracy                                         │
└─────────────────────────────────────────────────────────────┘
                              ↓
┌─────────────────────────────────────────────────────────────┐
│  Step 3: Performance Data Collection                       │
│  → Collect operator performance data                       │
│  → Output performance data location                        │
└─────────────────────────────────────────────────────────────┘
                              ↓
┌─────────────────────────────────────────────────────────────┐
│  Step 4: Performance Analysis                              │
│  → Analyze profiling data for bottlenecks                  │
│  → Output complete operator time distribution              │
│  → Identify bottleneck and well-performing operators       │
└─────────────────────────────────────────────────────────────┘
                              ↓
┌─────────────────────────────────────────────────────────────┐
│  Step 5: Optimization Suggestions                          │
│  → Provide optimization solutions for bottleneck operators │
│  → Optional operator optimization                          │
└─────────────────────────────────────────────────────────────┘

步骤 1:模型结构分析

1.1 分析流程

读取模型配置并分析:

  • 模型实现来源(transformers 或本地目录)
  • 架构类型(仅解码器 / 仅编码器 / 编码器-解码器)
  • 逐层加载要求
  • MoE 融合权重风险
1.2 确定迁移路径
架构类型建议路径
仅解码器大模型基于适配器的量化
理解型 VLM 文本主干网络基于适配器的量化
仅编码器 / 检测 / 分割继续使用本技能
其他需要手动判断
1.3 输出分析报告
## Model Structure Analysis Result

### Basic Information
- Model Name: xxx
- Architecture Type: Encoder-only / Decoder-only / Encoder-Decoder
- Parameter Count: xxx
- Source: transformers / local directory

### Support Status
- msmodelslim Support: Yes/No
- Recommended Migration Path: torch_npu direct migration / msmodelslim adaptation

### Migration Suggestions
[Specific recommendations]

步骤 2:环境准备 + 迁移验证

2.1 默认验证环境
  • 服务器:ascend-server-01
  • 容器:skill-the
  • 镜像:quay.io/ascend/vllm-ascend:v0.18.0
  • NPU:8× Ascend 910B3
2.2 环境配置
# Enter container
docker exec -it skill-the bash

# Install dependencies
pip install torch_npu
pip install ultralytics  # For YOLO series
# Or other model-specific libraries

# OpenCV dependencies (if needed)
apt install libgl1 libglib2.0-0
2.3 迁移验证脚本
import torch
import torch_npu

# Check NPU availability
print(f"NPU available: {torch.npu.is_available()}")
print(f"NPU count: {torch.npu.device_count()}")

# Load model
model = ...  # Model loading code
model = model.to('npu:0')

# Inference test
with torch.no_grad():
    output = model(input_tensor)

print(f"Inference success: {output is not None}")
2.4 输出迁移验证报告
## Migration Verification Result

### Environment Information
- Server: ascend-server-01
- Container: skill-the
- torch_npu Version: xxx
- NPU Status: Normal

### Inference Test
- Model Loading: Success/Failure
- NPU Inference: Success/Failure
- Accuracy Verification: Pass/Fail

### Performance Metrics
- Average Inference Time: xxx ms
- FPS: xxx

步骤 3:性能数据采集

3.1 性能采集流程

使用性能分析技能采集算子性能数据:

  • 板端采集(设备模式)
  • 或模拟采集(模拟器模式)
3.2 输出性能数据位置
## Performance Collection Result

### Data Location
- Server: ascend-server-01
- Path: /home/xxx/PROF_xxx/
- Database: msprof_xxx.db
- Collection Time: xxx

### Collection Configuration
- Mode: device / simulator
- NPU: npu:0
- Collection Duration: xxx s

步骤 4:性能分析

4.1 分析流程

查询并分析:

  1. 耗时排名前 N 的算子
  2. 按算子类型分组统计
  3. AI_CPU / AI_CORE / AI_VECTOR_CORE 分布
4.2 SQL 查询示例
-- Top 20 operators by time
SELECT op_name, op_type, total_time, call_times
FROM op_summary
ORDER BY total_time DESC
LIMIT 20;

-- Group by type
SELECT op_type, SUM(total_time) as type_time, COUNT(*) as count
FROM op_summary
GROUP BY op_type
ORDER BY type_time DESC;
4.3 输出性能分析报告
## Performance Analysis Result

### Operator Time Distribution (TOP 20)
| Rank | Operator Name | Type | Time(ms) | Percentage | Call Count |
|------|--------------|------|----------|------------|------------|
| 1 | xxx | AI_CPU | xxx | xx% | xxx |
| ... | ... | ... | ... | ... | ... |

### Statistics by Type
| Type | Total Time | Percentage | Operator Count |
|------|------------|------------|----------------|
| AI_CPU | xxx | xx% | xxx |
| AI_CORE | xxx | xx% | xxx |
| AI_VECTOR_CORE | xxx | xx% | xxx |

### Bottleneck Operators (>5% usage)
| Operator | Type | Percentage | Issue |
|----------|------|------------|-------|
| xxx | AI_CPU | xx% | [Specific issue] |

### Well-performing Operators
| Operator | Type | Description |
|----------|------|-------------|
| Conv2D | AI_CORE | High Cube utilization, normal |

步骤 5:优化建议

5.1 常见瓶颈及解决方案
瓶颈类型原因优化方案
Index 算子耗时高AI_CPU 实现使用 AscendC 开发优化版本
TransData 耗时高格式转换开销减少 CPU-NPU 间的数据传输
NMS 回退到 CPU算子不支持 NPU使用 AscendC 开发 NPU 版本的 NMS
Upsample 速度慢Vector core 效率优化 upsample 算子
5.2 输出优化建议报告
## Optimization Suggestions

### Priority Ranking
| Priority | Operator | Issue | Solution | Expected Gain |
|----------|----------|-------|----------|---------------|
| P0 | xxx | xxx | xxx | xx% |
| P1 | xxx | xxx | xxx | xx% |

### Next Steps
1. [Specific optimization steps]
2. Operator optimization may be performed for bottleneck operators

完整报告模板

完成每项迁移任务后,输出完整报告:

# [Model Name] Ascend Migration Report

## 1. Model Structure Analysis
[Step 1 output]

## 2. Migration Verification
[Step 2 output]

## 3. Performance Collection
[Step 3 output]

## 4. Performance Analysis
[Step 4 output]

## 5. Optimization Suggestions
[Step 5 output]

## Summary
- Migration Status: Success/Failure
- Inference Performance: xxx ms / xxx FPS
- Main Bottlenecks: xxx
- Optimization Direction: xxx

默认环境

  • 服务:ascend-server-01:22 (root/Hhuawei@smb)
  • 容器:skill-the
  • NPU:8× Ascend 910B3(每个 64G HBM)
  • CANN:cann-version-placeholder.220

前置条件

系统要求
  • Python 3.8+
  • torch_npu >= 2.0.0
  • msprof >= 7.0.0
  • ultralytics >= 8.0.0(用于 YOLO 模型)
环境检查

前置条件检查:需要 Python3 + torch_npu + msprof

python3 --version  # Python3 >= 3.8
python3 -c "import torch_npu; print('OK')"  # NPU PyTorch 支持
python3 -c "import msprof; print('OK')"  # 性能分析库

如果尚未安装:pip3 install --user torch_npu msprof ultralytics

其他系统依赖项

对于计算机视觉模型:

apt install libgl1 libglib2.0-0  # OpenCV dependencies

增强功能

性能基线比较模块

此技能包含性能基线比较机制,可将当前模型性能与行业标准基线进行比较:

功能:

  • 预定义基线:常见模型(YOLOv8、ResNet50、UNet、EfficientNet)在 Ascend NPU 上的基线数据
  • 差异分析:生成性能差距分析和优化潜力评估
  • 性能排名:与基准数据库中的相似模型进行比较
  • 趋势分析:跟踪多次迁移迭代中的性能改进

基线数据库:

模型批次大小延迟(ms)吞吐量(FPS)准确率
YOLOv8n322.343453.1% mAP
YOLOv8s164.820860.6% mAP
ResNet50641.253376.1% top-1
UNet88.594-

差异分析输出:

## Performance Baseline Comparison
- Target Model: YOLOv8s
- Baseline Reference: YOLOv8s @ Ascend 910B

### Performance Gap
| Metric | Current | Baseline | Gap |
|--------|---------|----------|-----|
| Latency | 5.2 ms | 4.8 ms | +8.3% |
| Throughput | 192 FPS | 208 FPS | -7.7% |
| Accuracy | 60.2% | 60.6% | -0.4% |

### Optimization Potential
- Priority P0: Reduce latency by optimizing Conv operators
- Priority P1: Improve memory access pattern
- Expected Gain: ~10-15% performance improvement
资源估算与规划工具

此技能提供迁移前资源估算功能:

功能:

  • 内存需求预测:根据模型大小和批次配置估算 NPU 内存使用量
  • 推理时间估算:在部署前预测延迟和吞吐量
  • 批次大小建议:根据目标延迟限制推荐最佳批次大小
  • 多卡扩展指导:为多设备部署提供扩展建议
  • 成本效益分析:评估优化投入与预期性能提升

资源估算输出:

## Resource Estimation Report
- Model: YOLOv8s
- Input Resolution: 640x640

### Memory Requirements
| Component | Size |
|-----------|------|
| Model Weights | 21 MB |
| Activation (BS=16) | 480 MB |
| Total Estimated | 501 MB |

### Performance Prediction
| Batch Size | Estimated Latency | Estimated Throughput |
|------------|-------------------|---------------------|
| 8 | 3.2 ms | 250 FPS |
| 16 | 4.8 ms | 208 FPS |
| 32 | 8.5 ms | 188 FPS |

### Recommended Configuration
- Optimal Batch Size: 16
- Target Latency: 4.8 ms
- Expected Throughput: 208 FPS
- Memory Utilization: ~78% of 64GB HBM

参考文档

文档描述
验收标准功能性和非功能性验收标准
验证方法分步验证指南
故障排除常见问题及解决方案
报告模板报告生成模板
Profiler SQLSQL 查询参考
迁移脚本迁移辅助脚本

前置条件(重复内容——见上文)

  • 已安装 torch_npu >= 2.0.0
  • 已安装 msprof >= 7.0.0
  • 已配置 Ascend NPU 环境
  • 待迁移的模型代码

核心命令

# Analyze model migration feasibility
python3 scripts/analyze_model.py --model /path/to/model

# Verify NPU inference
python3 scripts/verify_npu.py --model /path/to/model --input test.jpg

参数确认

参数描述必填
model模型代码路径是
input测试输入数据是
output输出目录否

评论

支持 Markdown