GE 图模式使用指南

【免费下载链接】cannbot-skills CANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体,本仓库为其提供可复用的 Skills 模块。 【免费下载链接】cannbot-skills 项目地址: https://gitcode.com/cann/cannbot-skills

GE图模式通过 TorchAir 的 CompilerConfig 开启,将 FX 图转换为 Ascend IR 图,并通过 GE 图引擎实现图编译和执行。


适用场景

  • 生产环境:稳定性优先
  • 通用场景:功能丰富,支持广泛
  • 复杂模型:需要更多配置选项

与 npugraph_ex 对比

特性 npugraph_ex 后端 GE 图模式
启用方式 backend="npugraph_ex" torchair.get_npu_backend()
实现原理 捕获模式 (Capture & Replay) FX图转换为Ascend IR,GE引擎编译执行
默认模式 需显式指定 系统默认模式 mode="max-autotune"
成熟度 试验特性,暂不支持商用 更成熟稳定
PyTorch版本 需要 2.6.0+ 无特殊要求
支持场景 在线推理 通用场景
配置方式 options={} 参数 CompilerConfig 对象

快速上手

# 导包(必须先导 torch_npu 再导 torchair)
import torch
import torch_npu
import torchair

# Patch方式实现集合通信入图(可选)
from torchair import patch_for_hcom
patch_for_hcom()  # 集合通信入图(有 TP/EP 并行时需调用)

model = YourModel().npu()

# 图执行模式默认为 max-autotune
config = torchair.CompilerConfig()
npu_backend = torchair.get_npu_backend(compiler_config=config)

# 基于 TorchAir backend 进行 compile
opt_model = torch.compile(model, backend=npu_backend)

# 执行编译后的 Model
output = opt_model(input_tensor)

完整示例见 GE 图模式快速上手


CompilerConfig 配置

config = torchair.CompilerConfig()

# debug 类功能
config.debug.xxx = ...

# export 类功能(离线导图)
config.export.xxx = ...

# dump_config 类功能
config.dump_config.xxx = ...

# fusion_config 类功能
config.fusion_config.xxx = ...

# experimental_config 类功能
config.experimental_config.xxx = ...

# inference_config 类功能
config.inference_config.xxx = ...

# ge_config 类功能
config.ge_config.xxx = ...

核心 API

API 用途 文档路径
CompilerConfig类 配置图模式功能 compiler_config.md
get_npu_backend() 获取 NPU 后端 get_npu_backend.md
get_compiler() 获取编译器 get_compiler.md
dynamo_export() 导出模型 dynamo_export.md
register_fx_node_ge_converter() 注册转换器 register_fx_node_ge_converter.md
register_replacement() 自定义算子融合 register_replacement.md

功能文档索引

在线文档:gitcode.com/Ascend/torchair/docs/zh/ascend_ir

概述

功能 在线链接
GE 图模式总览 ascend_ir.md
GE 图模式快速上手 quick_start.md

基础功能

功能 在线链接
基础功能总览 basic/

高级功能

功能 在线链接
进阶功能总览 advanced/

API 参考

API 在线链接
API 总览 api/
API 列表 api_list.md
torchair 核心 torchair/
torchair.ge ge/
torchair.inference inference/
torchair.ops ops/
torchair.scope scope/

相关文档

  • LLM 模型改造指南llm-model-guide.md(LLM 适配优先阅读)
  • npugraph_ex 指南npugraph_ex-guide.md

【免费下载链接】cannbot-skills CANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体,本仓库为其提供可复用的 Skills 模块。 【免费下载链接】cannbot-skills 项目地址: https://gitcode.com/cann/cannbot-skills

Logo

小龙虾开发者社区是 CSDN 旗下专注 OpenClaw 生态的官方阵地,聚焦技能开发、插件实践与部署教程,为开发者提供可直接落地的方案、工具与交流平台,助力高效构建与落地 AI 应用

更多推荐