昇腾算子调度Agent项目
Access rate limit exceeded. Please log in to continue.
昇腾CANN算子智能调度系统
基于多 Agent 协同的昇腾 NPU 模型算子融合优化系统。通过 LLM 自动分析 ONNX 模型的计算图和性能热点,从向量知识库检索融合方案,生成 C++ 融合规则代码并编译部署到昇腾 NPU,通过迭代式性能对比实现全自动模型优化。
目录
核心能力
| 能力 | 说明 |
|---|---|
| 计算图+性能数据提取 | ATC 编译 ONNX → OM,msprof 采集算子级性能数据,合并为 LLM 输入 |
| 多 Agent 协同分析 | Agent1(策略分析)→ Agent2(RAG 检索验证)→ Agent3(C++ 代码生成) |
| 向量知识库检索 | 基于 Qdrant + SentenceTransformer 的 RAG,检索昇腾算子融合知识 |
| 融合规则自动编译部署 | cmake → make → make install 一键下发到 NPU |
| 迭代式性能优化 | 性能未达阈值自动进入新迭代,支持多轮策略调整 |
| 双模式性能基准 | 默认模式对比自定义 pass 增量收益;关闭内置融合模式对比融合系统总收益 |
系统架构
用户入口层: main.py / chat_cli.py
│
核心引擎层: CannAgent/workflow.py (LangGraph)
│ │
│ ┌───────┼───────┐
│ ▼ ▼ ▼
│ Agent1 Agent2 Agent3
│ │ │ │
│ ▼ ▼ ▼
│ prompts.py / fuse_pass_template.md
│
工具函数层: tools/agent_tools.py
│ ├── ATC 编译 ONNX→OM
│ ├── msprof 性能采集
│ ├── cmake→make→install 编译下发
│ └── 性能对比分析 (compare_performance)
│
基础设施层: ATC / msprof / CANN GE / Qdrant
环境配置
前置条件
安装并配置CANN环境,请先确保ATC、msprof、GE等功能可用
测试环境为CANN 8.5.0,NPU型号为Ascend910B,python版本为3.11.13
一、配置环境变量
查看并修改.env文件内容
二、安装python环境
# Requires Python >= 3.10
pip install -r requirements.txt
三、下载嵌入模型
python embedding_model/download_model.py # 请查看并修改下载路径
检查.env文件中嵌入模型路径是否正确
四、安装并配置Qdrant
需要使用docker部署Qdrant向量数据库
docker pull qdrant/qdrant
docker run -p 6333:6333 -p 6334:6334 \
-v "/root/qdrant_storage:/qdrant/storage:z" \
qdrant/qdrant
数据库下载链接:https://pan.baidu.com/s/10knDdkVaQ6ZAtLHHQbtpnA?pwd=kteu
vector_store文件夹里即已经处理好的向量数据,嵌入模型为bge-large-zh-v1.5
将数据库上传至Qdrant并命名为 ascend_knowledge_base
python upload_to_qdrant.py # 使用此脚本上传数据库至Qdrant
五、拉取GE源码
编译环境需要用到GE的include和lib文件
cd tools
git clone -b 8.5.0 --single-branch https://gitcode.com/cann/ge.git # 与CANN版本对应
快速开始
# 交互式模式
python chat_cli.py
# 命令行模式
python main.py --onnx your_model.onnx
# 清理环境,清理系统运行副产物
bash tools/cleanup.sh y
项目结构
cannagent/
├── main.py # 命令行入口
├── chat_cli.py # 交互式 Chat CLI
├── requirements.txt # Python 依赖
├── .env # 环境变量配置
│
├── CannAgent/ # 多 Agent 工作流引擎
│ ├── workflow.py # LangGraph 编排(核心)
│ └── prompts.py # Agent 提示词模板
│
├── tools/ # 工具函数与依赖
│ ├── agent_tools.py # 核心工具函数库
│ ├── cleanup.sh # 环境清理脚本
│ ├── support_opp.py # 昇腾算子库清单
│ ├── fusion_switch.json # 关闭内置融合配置
│ ├── fuse_pass_template.md # C++ 融合规则模板
│ ├── ge/ # GE 图引擎源码
│ ├── fuse_pass/ # 融合规则编译依赖
│ └── get_llm_input/ # 算图提取 + ACL 推理脚本
│
├── test_module/ # 独立测试脚本集
│ ├── profile_model.py # 性能测试工具
│ ├── compile_fuse_pass.py # 融合规则编译下发
│ ├── extract_graph_perf.py # 算图+性能数据提取
│ ├── multi_round_test.py # 多轮重复测试
│ └── mypass.cpp # 示例融合规则
│
├── embedding_model/ # 嵌入模型文件
├── build/ # 运行时产物 (可清理)
└── output/ # 优化报告输出
使用方式
命令行模式
python main.py --onnx models/model.onnx # 默认模式
python main.py --onnx models/model.onnx --no-builtin-fusion # 关闭内置融合基准
python main.py --onnx models/model.onnx --max-iterations 5 # 最大5轮迭代
| 参数 | 默认值 | 说明 |
|---|---|---|
| --onnx | 必填 | ONNX 模型路径 |
| --soc-version | Ascend910B | 昇腾 SoC 版本 |
| --top-k | 5 | 知识库检索片段数 |
| --max-strategies | 1 | Agent1 最大策略数 |
| --max-iterations | 3 | 最大优化迭代轮数 |
| --no-builtin-fusion | False | 关闭内置融合基准 |
| --output-dir | output | 报告输出目录 |
交互式 Chat CLI
python chat_cli.py
# 输入含 .onnx 的指令自动触发优化
> 帮我优化models/model.onnx
# 输入其他内容进入 LLM 对话
> 什么是算子融合?
# 退出
> quit
工作流详解
迭代流程
START
→ [build_llm_input] ATC 编译 + msprof 采集 → 生成 input.json
→ [prepare_input] 解析图结构 + 性能数据
→ [agent1_analysis] LLM 分析热点,识别融合策略
→ [agent2_rag] 向量检索 → LLM 验证可行性 → 生成实施步骤
→ [agent3_code_gen] LLM 生成 C++ 融合规则代码
→ [compile_deploy] cmake + make + install → 部署到 NPU
→ [rebuild_model] ATC 重新编译 ONNX (含自定义 pass)
→ [profile_performance] msprof 采集优化后性能
→ [compare_performance] 对比基线 vs 优化后
├── 提升 > 5% → [generate_final_report] → END
├── 达最大迭代 → [generate_final_report] → END
└── 否则 → [prepare_input] 继续下一轮
三种性能对比模式
| 模式 | 对比内容 | 适用场景 |
|---|---|---|
| 默认 | 自定义 pass 增量收益 (vs 内置融合) | 评估 LLM 生成的融合规则效果 |
--no-builtin-fusion |
融合系统总收益 (vs 完全无融合) | 评估整体融合优化幅度 |
Agent 角色
| Agent | 输入 | 输出 | 核心约束 |
|---|---|---|---|
| Agent1 | 计算图 + 性能数据 | 策略列表 (名称/类别/证据/检索查询) | 优先穷尽算子融合策略 |
| Agent2 | 策略 + 知识库片段 | 实施步骤 + 可行性验证 | 算子必须在昇腾支持清单中 |
| Agent3 | 策略 + 实施步骤 | C++ 融合规则代码 | 继承 PatternFusionPass,使用 eager style API |
独立测试工具
test_module/ 目录下的工具可脱离系统独立使用:
| 工具 | 功能 | 命令 |
|---|---|---|
profile_model.py |
模型性能测试 | python test_module/profile_model.py --model model.onnx |
compile_fuse_pass.py |
编译下发融合规则 | python test_module/compile_fuse_pass.py --source pass.cpp --name MyPass |
extract_graph_perf.py |
算图+性能提取 | python test_module/extract_graph_perf.py --model model.onnx |
multi_round_test.py |
多轮重复测试 | python test_module/multi_round_test.py --model model.onnx --rounds 5 |