cannbot-skills图模式适配优化

cannbot-skills图模式适配优化 【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skillsname: model-infer-graph-mode description: 基于 PyTorch 框架的昇腾 NPU 模型推理图模式适配技能。将模型适配到 torch.compile 图模式以加速推理性能。触发场景npugraph_ex 或 GE 图模式适配、torch.compile 在昇腾 NPU 上的使用、图中断Graph Break修复、aclgraph 图编译问题。LLM 模型的图模式适配优先阅读 LLM 模型改造指南。图模式适配优化技能提供 npugraph_ex 和 GE 两种图模式的适配指南覆盖方案设计、图中断修复、FA 参数配置和验证测试。重要原则前置条件模型必须在 NPU 上运行且已导入torch_npu图模式仅适用于 Decode 阶段Prefill 阶段输入长度动态变化不适合图模式保持 eager 执行保持模型完整性不为适配图模式而简化模型逻辑NPU 不支持的后端aot_eager、inductor、cudagraphs等不可用固定 tensor 图外预创建atten_mask、KV cache 等推理全程不变的 tensor 在图外预创建用torch._dynamo.mark_static()标记LLM Decode 重编译注意kv_len/actual_seq_lengths_kv每步变化npugraph_ex 可能触发重编译遇到hit recompile_limit参考重编译解决方案排查精度问题调试遇到精度问题可调用model-infer-precision-debugskill 进行排查工作流程第一步方案设计分析模型结构识别模型中可能阻碍图模式的代码模式识别问题点找出 Graph Break、重编译、动态 shape 等问题设计改造方案需要修改哪些文件修改的具体内容对现有功能的影响评估输出设计方案文档以 Markdown 格式呈现第二步方案确认等待用户确认方案后再进行开发。有修改意见则返回第一步。第三步实施开发按照确认的设计方案逐步实施代码修改。第四步验证测试由 Agent 实际执行以下测试记录真实结果编译验证运行torch.compile检查是否成功记录编译日志功能验证运行模型对比图模式前后输出性能验证记录 Prefill/Decode 阶段耗时测试报告整理测试环境、测试用例、对比数据图模式选择在开始适配前先确定使用哪种模式。如果用户未指定默认采用 npugraph_ex。场景推荐模式详细文档LLM 大语言模型npugraph_ex优先阅读 LLM 指南references/llm-model-guide.md通用模型npugraph_exreferences/npugraph_ex-guide.md需要 GE 图模式GEAscend IRreferences/ge-graph-guide.md特性npugraph_ex 后端 (aclgraph)GE 图模式 (Ascend IR)启用方式backendnpugraph_extorchair.get_npu_backend()实现原理捕获模式 (Capture Replay)FX 图转换为 Ascend IRGE 引擎编译执行成熟度试验特性暂不支持商用更成熟稳定PyTorch版本需要 2.6.0无特殊要求支持场景在线推理通用场景类似技术torch.cuda.CUDAGraph传统图编译npugraph_ex 快速示例import torch import torch_npu model YourModel().to(npu) opt_model torch.compile(model, backendnpugraph_ex, fullgraphTrue, dynamicFalse) # 注LLM Decode 场景 actual_seq_lengths 每步变化时需 dynamicTrue output opt_model(input_tensor)关键约束PyTorch 2.6.0、仅支持在线推理、不支持随机数算子和动态控制流、forward 中不可使用.item()GE 图模式快速示例import torch import torch_npu import torchair from torchair import patch_for_hcom patch_for_hcom() # 集合通信入图有 TP/EP 并行时需调用 model YourModel().to(npu) config torchair.CompilerConfig() npu_backend torchair.get_npu_backend(compiler_configconfig) opt_model torch.compile(model, backendnpu_backend) output opt_model(input_tensor)详细文档npugraph_ex 见references/npugraph_ex-guide.mdGE 见references/ge-graph-guide.mdLLM 模型适配要点对于 LLM 推理模型必须严格区分 prefill 和 decode 阶段。Prefill 与 Decode 阶段限制阶段是否支持图模式原因Prefill禁止使用输入长度动态变化、首 token 生成逻辑复杂、shape 不固定Decode推荐使用输入长度固定通常为 1、shape 稳定、适合图捕获实现建议将 prefill 和 decode 的 forward 逻辑分离成不同方法仅对 decode 方法应用torch.compile图模式prefill 阶段使用 eager 模式执行class YourModel: def prefill(self, input_ids, ...): Prefill 阶段使用 eager 模式 # 输入长度动态变化不适合图模式 return self._forward(input_ids, ...) def decode(self, input_ids, ...): Decode 阶段可使用图模式 # 输入长度固定通常为 1适合图捕获 return self._forward(input_ids, ...) # 仅对 decode 方法应用图模式, model.prefill保持 eager model.decode torch.compile(model.decode, backendnpugraph_ex, ...)核心改造原则核心原则将动态变化的东西提取为模型输入模型内部尽量保证静态。动态因素问题表现解决思路内存地址变化Guard 失败、重编译预分配固定大小原地更新Shape 变化图中断、多次编译固定 shape 或通过参数控制Python 控制流Graph Break使用 Tensor 操作或模式参数.item()调用强制 Graph Break保持 Tensor 或外部传入详细改造指南references/llm-model-guide.md问题定界流程问题定界优先基于本 skill 内置的知识进行独立分析。问题发生 │ ├─→ aot_eager 验证 ──失败──→ 修复用户脚本 │ ↓ 正常 │ ├─→ force_eager/run-eagerly ──失败──→ 修复用户脚本 │ ↓ 正常 │ └─→ 图模式问题 ├── 重编译问题 → 阅读 LLM 指南 npugraph_ex 指南 ├── Graph Break 问题 → 阅读 TorchAir 在线文档中的典型案例 └── 其他问题 → 阅读对应模式文档npugraph_ex-guide.md 或 ge-graph-guide.md调试知识来源按优先级本 SKILL.md 中的方法、原则和检查清单references/npugraph_ex-guide.md- npugraph_ex 详细指南references/llm-model-guide.md- LLM 模型改造指南references/ge-graph-guide.md- GE 图模式指南TorchAir 官方文档注意避免盲目复制其他模型的图模式配置应基于当前模型结构独立分析。图模式 FA 融合算子快速 Debug图模式与 FA 融合算子结合时actual_seq_lengths参数的处理是最常见的出错点。 本节提供快速诊断和解决方案。问题现象图模式 FA 场景下的典型问题编译报错actual_seq_lengths类型不匹配运行时报错重编译recompile触发性能问题动态 shape 导致无法充分优化关键参数actual_seq_lengthsFA 算子的actual_seq_lengths/actual_seq_qlen/actual_seq_kvlen参数在不同图模式下有不同的要求图模式FA 接口来源actual_seq_lengths 类型dynamic 设置执行模式约束说明GE 模式推荐torchair FA 接口TensordynamicFalse仅支持 GE 图模式最佳方案静态图GE 模式不推荐torch_npu FA 接口list[int]dynamicTruemark_static无限制需额外配置易出错npugraph_ex 模式torch_npu FA 接口list[int]dynamicTrue无限制动态捕获模式npugraph_ex 模式torch_npu FA 接口Tensor如有dynamicFalse无限制需确认接口是否支持GE 模式配置指南方案一torchair FA 接口推荐import torch import torch_npu import torchair from torchair.ge_concrete_graph.ge_graph import mark_static # 使用 torchair 提供的 FA 接口 # actual_seq_lengths 为 Tensor 类型 attn_output torchair.ops.npu_fused_infer_attention_score( query, key, value, actual_seq_qlenactual_seq_qlen_tensor, # Tensor 类型 actual_seq_kvlenactual_seq_kvlen_tensor, # Tensor 类型 # ... 其他参数 ) # 编译配置 opt_model torch.compile(model, backendnpu_backend, dynamicFalse)优点dynamicFalse可获得更好的静态图优化无需额外的mark_static配置图编译更稳定约束TorchAir FA 接口仅支持 GE 图模式不支持 Eager 模式和 npugraph_ex 模式调用必须在 GE 图模式torchair.get_npu_backend()下使用不可在 Eager 或 npugraph_ex 模式下调用方案二torch_npu FA 接口不推荐import torch import torch_npu import torchair from torchair.ge_concrete_graph.ge_graph import mark_static # 使用 torch_npu 的 FA 接口 # actual_seq_lengths 为 list[int] 类型 attn_output torch.ops.npu.npu_fused_infer_attention_score( query, key, value, actual_seq_lengths[seq_len], # list[int] 类型 actual_seq_lengths_kv[kv_len], # ... 其他参数 ) # 必须配置 dynamicTrue # 并使用 mark_static 标记除 actual_seq_lengths 外的静态输入 mark_static(input_ids) # 静态输入 mark_static(position_ids) # 静态输入 mark_static(attention_mask) # 静态输入 # actual_seq_lengths 保持动态 # 编译配置 opt_model torch.compile(model, backendnpu_backend, dynamicTrue)缺点需要配置dynamicTrue性能略逊于静态图需要手动调用mark_static标记所有静态输入配置繁琐易遗漏导致问题npugraph_ex 模式配置指南方案一list[int] 类型 dynamicTrueimport torch import torch_npu # 使用 torch_npu 的 FA 接口 # actual_seq_lengths 为 list[int] 类型 attn_output torch.ops.npu.npu_fused_infer_attention_score( query, key, value, actual_seq_lengths[seq_len], # list[int] 类型 actual_seq_lengths_kv[kv_len], # ... 其他参数 ) # 必须配置 dynamicTrue opt_model torch.compile(model, backendnpugraph_ex, dynamicTrue)方案二Tensor 类型 dynamicFalse如有接口支持import torch import torch_npu # 查询是否有 Tensor 类型的 actual_seq_lengths 接口 # 通过 subagent 调用 /model-infer-fusion 查询 # 如果有支持的接口 attn_output torch.ops.npu.npu_fused_infer_attention_score( query, key, value, actual_seq_lengthsactual_seq_lengths_tensor, # Tensor 类型 actual_seq_lengths_kvactual_seq_kvlen_tensor, # ... 其他参数 ) # 可配置 dynamicFalse opt_model torch.compile(model, backendnpugraph_ex, dynamicFalse)FA 接口查询如需查询具体的 FA 接口参数和版本支持使用 subagent 调用/model-infer-fusion技能启动 subagent类型general-purpose提示词包含 1. 明确指示调用 /model-infer-fusion 技能 2. 询问具体问题例如 - 查询 npu_fused_infer_attention_score 的 actual_seq_lengths 参数是否支持 Tensor 类型 - 查询 npu_fused_infer_attention_score_v2 在图模式下的推荐配置 - 查询 torchair 提供的 FA 接口及其参数要求 3. 提供当前使用的图模式GE / npugraph_ex常见错误与修复错误现象根因修复方案编译报错actual_seq_lengths 类型错误GE 模式下 torch_npu FA 接口传 Tensor改用 torchair FA 接口或改用 list[int] dynamicTrue运行时频繁重编译dynamicFalse 但 actual_seq_lengths 为 list[int]改用 Tensor 类型 torchair 接口或设置 dynamicTrue性能不达预期dynamicTrue 导致无法充分优化尽量使用 torchair FA 接口 dynamicFalsenpugraph_ex 模式报错actual_seq_lengths 为 Tensor 但接口不支持确认接口支持情况或改用 list[int] dynamicTrueEager 或 npugraph_ex 模式调用 TorchAir FA 报错TorchAir FA 接口仅支持 GE 图模式改用 torch_npu FA 接口或切换到 GE 图模式Debug 检查清单在图模式 FA 场景下按以下清单逐一排查确认使用的图模式GE 还是 npugraph_ex确认 FA 接口来源torch_npu 还是 torchair若使用 torchair FA 接口确认当前为 GE 图模式不支持 Eager 和 npugraph_ex检查 actual_seq_lengths 类型GEtorchair→TensorGEtorch_npu→list[int]dynamicTruenpugraph_ex→list[int]dynamicTrue检查 dynamic 配置是否与 actual_seq_lengths 类型匹配若使用 GE torch_npu FA list[int]检查是否已 mark_static 标记所有静态输入如有疑问调用model-infer-fusionskill 查询接口详情TorchAir 官方文档索引在线文档gitcode.com/Ascend/torchair/docs/zh入门必读主题在线链接简介overview.md支持的 ATen API 清单aten_api.mdGE 图模式主题在线链接GE 图模式总览ascend_ir.mdGE 图模式快速上手quick_start.mdGE 图模式功能基础basic/GE 图模式功能进阶advanced/GE 图模式 API 参考api/自定义算子入图主题在线链接概述overview.mdIn-place 算子开发和入图样例in_place_op_cases.md非 In-place 算子开发和入图样例non_in_place_op_cases.md算子插件化适配op_plugin_adapt_torchair.md常见案例和定位方法主题在线链接大模型图模式推理案例infer_cases.md动/静态图概念及典型问题定位dynamic_static_graph/入图失败定界与定位graph_failed_cases.md性能分析与精度比对主题在线链接图模式性能分析perfermance_cases.md图模式精度比对accuracy_cases.mdFAQfaq.md【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skills创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考