已合并
【PR】: 修改文档表述,不再import inductor整个组件 #1655
zzq创建于 17 天前
【PR】: 修改文档表述,不再import inductor整个组件 #1655
已合并
共 8 个文件变更+231-74
| @@ -11,7 +11,7 @@ autofuse/ | |||
| 11 | ├── ascendc # ascendc api 定义 | 11 | ├── ascendc # ascendc api 定义 |
| 12 | ├── ascir # 算子注册 ascir | 12 | ├── ascir # 算子注册 ascir |
| 13 | ├── att # 自动 tiling 生成 模块 | 13 | ├── att # 自动 tiling 生成 模块 |
| 14 | -├── autofuse # config 配置 | 14 | +├── cmake # cmake 脚本文件 |
W | |||
| 15 | ├── codegen # kernel 代码生成 模块 | 15 | ├── codegen # kernel 代码生成 模块 |
| 16 | ├── common # 通用工具方法 | 16 | ├── common # 通用工具方法 |
| 17 | ├── compiler # 对外API 接口 | 17 | ├── compiler # 对外API 接口 |
| @@ -31,12 +31,14 @@ autofuse/ | |||
| 31 | 参考[执行构建](../docs/zh/build.md)。 | 31 | 参考[执行构建](../docs/zh/build.md)。 |
| 32 | 32 | ||
| 33 | ## 上板验证指导 | 33 | ## 上板验证指导 |
| 34 | -用户如果想在昇腾设备上体验Autofuse 的功能与性能,可以先参考[快速安装](../docs/zh/quick_install.md)准备环境。无论是没有昇腾设备的开发者,还是已有昇腾设备的开发者,都可以快速搭建好环境。在此基础上,按照上一步[构建与安装](../docs/zh/build.md),增量安装了graph-autofusion仓编译生成的cann包。 | 34 | +用户如果想在昇腾设备上体验 Autofuse 的功能与性能,可以先参考[快速安装](../docs/zh/quick_install.md)准备环境。无论是没有昇腾设备的开发者,还是已有昇腾设备的开发者,都可以快速搭建好环境。在此基础上,按照上一步[构建与安装](../docs/zh/build.md),增量安装了graph-autofusion仓编译生成的cann包。 |
| 35 | 35 | ||
| 36 | -此处指导如何搭建 Pytorch 环境,创建脚本,跑通 Inductor + Autofuse场景,并可视化生成的自动融合算子,以及观察最后的kernel性能。 | 36 | +AutoFuse 当前提供 PyTorch 和 TensorFlow 两种框架下的 Sample 用例,未来我们可能会支持更多框架。可根据实际使用场景参考对应文档完成环境安装和用例执行: |
| 37 | 37 | ||
| 38 | -当前自动融合支持elementwise类型+element类型,element类型+broadcast类型,element类型+reduce类型算子的融合。更多融合场景的支持(concat,gather等等)逐步开放中。 | 38 | +- [PyTorch 场景用例](./examples/pytorch/README.md) |
| 39 | +- [TensorFlow 场景用例](./examples/tensorflow/README.md) | ||
| 39 | 40 | ||
| 41 | +以下以 Pytorch 场景为例,指导如何搭建 Pytorch 环境,跑通 Pytorch场景下用例,并通过profiling数据观察最后的kernel性能。 | ||
| 40 | 42 | ||
| 41 | ### 安装依赖 | 43 | ### 安装依赖 |
| 42 | 44 | ||
| @@ -45,7 +47,7 @@ autofuse/ | |||
| 45 | pip3 install numpy | 47 | pip3 install numpy |
| 46 | pip3 install pyyaml | 48 | pip3 install pyyaml |
| 47 | pip3 install setuptools | 49 | pip3 install setuptools |
| 48 | -pip3 install torch_npu==2.8 # 通过pip 安装 torch_npu 时,会自动安装依赖的torch 版本 | 50 | +pip3 install torch_npu==2.10.0 # torch_npu版本应为 2.9.0 及以上。通过pip 安装 torch_npu 时,会自动安装依赖的torch 版本。 |
W 现在这个2.10.0版本有点特殊,通过pip源下载的torch_npu,也不包含inductor_npu_ext的能力,这个只能算是过度吧。你可以在这块写如果体验用例执行,优先使用你的脚本来安装环境。 ![]() ![]() | |||
| 49 | ``` | 51 | ``` |
| 50 | 52 | ||
| 51 | #### 其他环境依赖 | 53 | #### 其他环境依赖 |
| @@ -62,10 +64,6 @@ sudo yum install cmake gcc | |||
| 62 | sudo apt-get install cmake gcc | 64 | sudo apt-get install cmake gcc |
| 63 | ``` | 65 | ``` |
| 64 | 66 | ||
| 65 | - | ||
| 66 | -### sample 用例 | ||
| 67 | -autofuse 提供了丰富的 sample 用例,可以参考[Autofuse样例](./examples/pytorch/README.md)。 | ||
| 68 | - | ||
| 69 | ### 设置环境变量 | 67 | ### 设置环境变量 |
| 70 | 68 | ||
| 71 | 执行用例前,需要设置如下环境变量,设置运行NPU设备。 | 69 | 执行用例前,需要设置如下环境变量,设置运行NPU设备。 |
| @@ -119,13 +117,34 @@ export AUTOFUSE_DFX_FLAGS="--codegen_compile_debug=true;--debug_dir=/path-to-dum | |||
| 119 | 注意:Autofuse 后端会在设置的 dump 路径下生成每个融合算子的 dump 图。 | 117 | 注意:Autofuse 后端会在设置的 dump 路径下生成每个融合算子的 dump 图。 |
| 120 | 118 | ||
| 121 | ### 结果分析 & 调测输出分析 | 119 | ### 结果分析 & 调测输出分析 |
| 122 | -用户开启 TORCH_COMPILE_DEBUG 后,调试信息输出位于执行目录下的torch_compile_debug子目录,带有 autofused_ 前缀的目录为 inductor-npu-ext 相关产物,其余均为 inductor 原生产物。每一个autofused_ 前缀的目录,都表示一个融合算子的白盒结构。如果没有融合算子产生(即未发生融合,需要通过打屏的 "Fallback aten.xxxx $reason: xx原因" 信息去判断原因。具体可参考[inductor-npu-ext使用手册](https://gitcode.com/Ascend/torchair/blob/master/experimental/_inductor_npu_ext/docs/manuals.md)。 | 120 | +用户开启 `TORCH_COMPILE_DEBUG` 后,调试信息会输出到当前执行目录下的 `torch_compile_debug` 子目录。其中,以 `autofused_` 为前缀的目录是 `torch_npu` AscendC 后端生成的融合算子产物,其余目录为 PyTorch Inductor 生成的原生产物。每个以 `autofused_` 为前缀的目录对应一个融合算子的白盒结构,可用于查看融合范围和代码生成结果。如果未生成以 `autofused_` 为前缀的目录,则说明当前编译过程中没有产生融合算子。此时,可以根据终端输出中的 `Fallback aten.xxxx $reason: xx原因` 信息分析未发生融合的原因。 |
| 123 | 121 | ||
| 124 | -用户也可以通过profiling的相关配置,观察使能自动融合后,算子性能收益情况。对于上面的sample用例,可以注释 "model = torch.compile(model, dynamic=False, fullgraph=True)" 这一行,即可走单算子流程。然后对比profiling里,单算子场景所有算子的总耗时,与使能 Inductor+Autofuse,融合算子的总耗时。详细的Profling性能分析工具的使用方法,可参见[Profiling性能分析工具指南](https://hiascend.com/document/redirect/CannCommunityToolProfiling)。 | 122 | +用户也可以通过 Profiling 相关配置,观察使能自动融合后的算子性能收益。对于上述 Sample 用例,可以注释整个 `torch.compile(...)` 代码块,使模型以非编译模式执行,作为未使能自动融合的对照场景。 |
| 123 | +```python | ||
| 124 | +# model = torch.compile( | ||
| 125 | +# model, | ||
| 126 | +# dynamic=False, | ||
| 127 | +# fullgraph=True, | ||
| 128 | +# options={"npu_backend": "ascendc"}, | ||
| 129 | +# ) | ||
| 130 | +``` | ||
| 131 | +分别采集未使能自动融合和使能自动融合两种场景的 Profiling 数据,并对比相同计算范围内所有相关算子的总耗时。 | ||
| 125 | 132 | ||
| 126 | -需要注意的是,不是模型里所有的算子都能被融合,对于在 Inductor 层未被 lowering 的算子,最后仍然以单算子形式存在。融合提升比,等于 (融合后所有算子耗时-融合前所有算子耗时)/融合前所有算子耗时。更进一步的,可以观察融合算子的 aiv_mte2_time(输入搬运耗时)和 aiv_mte3_time(输出搬运耗时)的提升情况。 | 133 | +详细的Profiling性能分析工具的使用方法,可参见[Profiling性能分析工具指南](https://hiascend.com/document/redirect/CannCommunityToolProfiling)。 |
| 134 | + | ||
| 135 | +需要注意的是,不是模型里所有的算子都能被融合,对于在 Inductor 层未被 lowering 的算子,最后仍然以单算子形式存在。融合提升比,等于 (融合前所有算子耗时-融合后所有算子耗时)/融合前所有算子耗时。更进一步的,可以观察融合算子相比于单算子的 aiv_mte2_time(输入搬运耗时)和 aiv_mte3_time(输出搬运耗时)的提升情况。 | ||
| 127 | 136 | ||
| 128 | 对于精度的分析,详细的精度调试工具的使用方法,可参见[精度调试工具指南](https://hiascend.com/document/redirect/CannCommunityToolAccucacy)。 | 137 | 对于精度的分析,详细的精度调试工具的使用方法,可参见[精度调试工具指南](https://hiascend.com/document/redirect/CannCommunityToolAccucacy)。 |
| 129 | 138 | ||
| 130 | ### 复杂网络使能 | 139 | ### 复杂网络使能 |
| 131 | -用户如果想在网络里,使能 Autofuse 功能,只需要在模型文件的开头,导入torch后面,加上 import inductor_npu_ext 即可。 | 140 | +用户在网络中使能 AutoFuse 时,无需单独导入 `inductor_npu_ext`, |
| 141 | +只需在 `torch.compile` 中指定 AscendC 后端: | ||
| 142 | + | ||
| 143 | +```python | ||
| 144 | +model = torch.compile( | ||
| 145 | + model, | ||
| 146 | + dynamic=False, | ||
| 147 | + fullgraph=True, | ||
| 148 | + options={"npu_backend": "ascendc"}, | ||
| 149 | +) | ||
| 150 | +``` | ||
| @@ -2,31 +2,29 @@ | |||
| 2 | 2 | ||
| 3 | ## Introduction | 3 | ## Introduction |
| 4 | 4 | ||
| 5 | -AutoFuse is an automatic fusion framework based on Ascend C, supporting automatic fusion scope identification, automatic operator code generation, Auto Tiling optimization, dynamic shape, and mixed precision features. In algorithm networks, due to numerous Vector calculations, substantial memory transfers occur between Vector calculations, causing Memory Bound issues. AutoFuse automatically fuses multiple operators into a single operator, reducing the number of operators and memory transfers in the network, thereby alleviating Memory Bound issues, releasing Ascend computing power, and improving model execution performance. | 5 | +AutoFuse is an automatic fusion framework based on Ascend C. It supports automatic fusion scope identification, automatic operator code generation, Auto Tiling optimization, dynamic shape, mixed precision, and other features. In algorithm networks, a large number of Vector computations may cause substantial memory transfers between Vector computations, resulting in Memory Bound issues. AutoFuse automatically fuses multiple operators into a single operator, reducing the number of operators and memory transfers in the network. This alleviates Memory Bound issues, unleashes Ascend computing power, and improves model execution performance. |
| 6 | 6 | ||
| 7 | -For detailed introduction, refer to "[Autofuse Automatic Fusion](https://www.hiascend.com/document/detail/zh/canncommercial/850/graph/autofuse)". | 7 | +For details, refer to [AutoFuse Automatic Fusion](https://www.hiascend.com/document/detail/zh/canncommercial/850/graph/autofuse). |
| 8 | 8 | ||
| 9 | ## Autofuse Directory Structure | 9 | ## Autofuse Directory Structure |
| 10 | 10 | ||
| 11 | ```text | 11 | ```text |
| 12 | autofuse/ | 12 | autofuse/ |
| 13 | -├── ascendc # ascendc api definitions | 13 | +├── ascendc # Ascend C API definitions |
| 14 | -├── ascir # operator registration ascir | 14 | +├── ascir # AscIR operator registration |
| 15 | -├── att # automatic tiling generation module | 15 | +├── att # Automatic tiling generation module |
| 16 | -├── autofuse # config configuration | 16 | +├── cmake # CMake script files |
| 17 | -├── cmake # third-party library related configuration | 17 | +├── codegen # Kernel code generation module |
| 18 | -├── codegen # kernel code generation module | 18 | +├── common # Common utility methods |
| 19 | -├── common # common utility methods | 19 | +├── compiler # External API interfaces |
| 20 | -├── compiler # external API interface | 20 | +├── examples # Example scripts demonstrating typical usage |
| 21 | -├── examples # example scripts demonstrating typical usage | 21 | +├── graph_metadef # Basic graph interfaces |
| 22 | -├── graph_metadef # basic graph interface | 22 | +├── inc # Interfaces provided for GE |
| 23 | -├── inc # interface for GE calls | 23 | +├── optimize # Scheduling and partitioning module |
| 24 | -├── optimize # scheduling and partitioning module | 24 | +├── scripts # Script directory |
| 25 | -├── scripts # script path | 25 | +├── v35 # Ascend 950 chip-related optimizations |
| 26 | -├── v35 # Ascend 950 chip related optimization | ||
| 27 | ├── CMakeLists.txt # CMake configuration file | 26 | ├── CMakeLists.txt # CMake configuration file |
| 28 | -├── blacklist.txt # project configuration file | 27 | +├── blacklist.txt # Project configuration file |
| 29 | -├── build_third_party.sh # third-party library installation script | ||
| 30 | ├── README.md | 28 | ├── README.md |
| 31 | ``` | 29 | ``` |
| 32 | 30 | ||
| @@ -36,11 +34,14 @@ Refer to [Build Instructions](../docs/en/build.md). | |||
| 36 | 34 | ||
| 37 | ## On-Device Verification Guide | 35 | ## On-Device Verification Guide |
| 38 | 36 | ||
| 39 | -Users who wish to experience AutoFuse functionality and performance on Ascend devices can first refer to [Quick Installation](../docs/en/quick_install.md) to prepare the environment. Whether developers have Ascend devices or not, they can quickly set up the environment. On this basis, following the previous [Build and Installation](../docs/en/build.md), incrementally install the CANN package compiled from the graph-autofusion repository. | 37 | +Users who want to experience the functionality and performance of AutoFuse on Ascend devices can first refer to [Quick Installation](../docs/en/quick_install.md) to prepare the environment. Both developers without Ascend devices and developers who already have Ascend devices can quickly set up the environment. On this basis, follow the previous [Build and Installation](../docs/en/build.md) instructions to incrementally install the CANN package compiled from the graph-autofusion repository. |
| 40 | 38 | ||
| 41 | -This section guides how to set up a PyTorch environment, create scripts, run through the Inductor + AutoFuse scenario, visualize generated auto-fusion operators, and observe final kernel performance. | 39 | +AutoFuse currently provides sample use cases for both PyTorch and TensorFlow, with support for additional frameworks planned in the future. Refer to the corresponding documentation based on your actual use case to set up the environment and run the samples: |
| 42 | 40 | ||
| 43 | -Currently, auto-fusion supports fusion of elementwise + element type, element + broadcast type, and element + reduce type operators. Support for more fusion scenarios (concat, gather, and so on) is gradually being released. | 41 | +- [PyTorch Scenario Use Cases](./examples/pytorch/README_en.md) |
| 42 | +- [TensorFlow Scenario Use Cases](./examples/tensorflow/README.md) | ||
| 43 | + | ||
| 44 | +The following uses a PyTorch scenario as an example to demonstrate how to set up the PyTorch environment, run the sample, and evaluate the performance of the resulting kernels using profiling data. | ||
| 44 | 45 | ||
| 45 | ### Install Dependencies | 46 | ### Install Dependencies |
| 46 | 47 | ||
| @@ -50,15 +51,7 @@ Currently, auto-fusion supports fusion of elementwise + element type, element + | |||
| 50 | pip3 install numpy | 51 | pip3 install numpy |
| 51 | pip3 install pyyaml | 52 | pip3 install pyyaml |
| 52 | pip3 install setuptools | 53 | pip3 install setuptools |
| 53 | -pip3 install torch_npu==2.8 # Installing torch_npu via pip automatically installs the dependent torch version | 54 | +pip3 install torch_npu==2.10.0 # torch_npu must be version 2.9.0 or later. Installing torch_npu through pip automatically installs the required torch version. |
| 54 | -``` | ||
| 55 | - | ||
| 56 | -#### Install inductor-npu-ext (AutoFuse enabling framework in Inductor) | ||
| 57 | - | ||
| 58 | -```bash | ||
| 59 | -git clone https://gitcode.com/Ascend/torchair.git | ||
| 60 | -cd torchair/experimental/_inductor_npu_ext/ | ||
| 61 | -pip3 install -e ./python/ | ||
| 62 | ``` | 55 | ``` |
| 63 | 56 | ||
| 64 | #### Other Environment Dependencies | 57 | #### Other Environment Dependencies |
| @@ -67,91 +60,126 @@ pip3 install -e ./python/ | |||
| 67 | CMake >= 3.16.0 | 60 | CMake >= 3.16.0 |
| 68 | GCC >= 7.3.0 | 61 | GCC >= 7.3.0 |
| 69 | ``` | 62 | ``` |
| 70 | -On openEuler systems, you can install through the following commands: | 63 | + |
| 64 | +On openEuler systems, run the following command: | ||
| 65 | + | ||
| 71 | ```bash | 66 | ```bash |
| 72 | sudo yum install cmake gcc | 67 | sudo yum install cmake gcc |
| 73 | ``` | 68 | ``` |
| 74 | -On Ubuntu systems, you can install through the following commands: | 69 | + |
| 70 | +On Ubuntu systems, run the following command: | ||
| 71 | + | ||
| 75 | ```bash | 72 | ```bash |
| 76 | sudo apt-get install cmake gcc | 73 | sudo apt-get install cmake gcc |
| 77 | ``` | 74 | ``` |
| 78 | 75 | ||
| 79 | -### Sample Use Cases | ||
| 80 | - | ||
| 81 | -AutoFuse provides abundant sample use cases. Refer to [AutoFuse Samples](./examples/pytorch/README_en.md). | ||
| 82 | - | ||
| 83 | ### Set Environment Variables | 76 | ### Set Environment Variables |
| 84 | 77 | ||
| 85 | -Before executing use cases, set the following environment variables to configure the NPU device: | 78 | +Before executing the use cases, set the following environment variables to configure the NPU device: |
| 79 | + | ||
| 86 | ```bash | 80 | ```bash |
| 87 | -# Your own driver package installation path | 81 | +# Installation path of your driver package |
| 88 | source /usr/local/Ascend/driver/bin/setenv.sh | 82 | source /usr/local/Ascend/driver/bin/setenv.sh |
| 89 | -# Your own CANN package installation path | 83 | +# Installation path of your CANN package |
| 90 | source /usr/local/Ascend/ascend-toolkit/set_env.sh | 84 | source /usr/local/Ascend/ascend-toolkit/set_env.sh |
| 91 | -# Assume running on card 0, keep consistent with script | 85 | +# Assume that the script runs on device 0, consistent with the device configured in the script |
| 92 | export ASCEND_DEVICE_ID=0 | 86 | export ASCEND_DEVICE_ID=0 |
| 93 | ``` | 87 | ``` |
| 94 | 88 | ||
| 95 | ### Execute Use Cases | 89 | ### Execute Use Cases |
| 96 | 90 | ||
| 97 | -Assume the use case name is test.py, execute directly: | 91 | +Assume that the use case is named `test.py`. Run it directly: |
| 98 | 92 | ||
| 99 | ```bash | 93 | ```bash |
| 100 | python3 test.py | 94 | python3 test.py |
| 101 | ``` | 95 | ``` |
| 102 | 96 | ||
| 103 | -### More Debugging Related Environment Variables | 97 | +### More Debugging-Related Environment Variables |
| 104 | 98 | ||
| 105 | #### TORCH_COMPILE_DEBUG | 99 | #### TORCH_COMPILE_DEBUG |
| 106 | 100 | ||
| 107 | -Purpose: Native torch environment variable that enables detailed debug logging and saving of compilation intermediate artifacts. | 101 | +Purpose: A native torch environment variable that enables detailed debugging logs and saves intermediate compilation artifacts. |
| 108 | 102 | ||
| 109 | Usage: | 103 | Usage: |
| 104 | + | ||
| 110 | ```bash | 105 | ```bash |
| 111 | export TORCH_COMPILE_DEBUG=1 | 106 | export TORCH_COMPILE_DEBUG=1 |
| 112 | ``` | 107 | ``` |
| 113 | -Note: Multiple executions of the same script may skip compilation due to cache. Can use with TORCHINDUCTOR_FORCE_DISABLE_CACHES to force recompilation each execution. | 108 | + |
| 109 | +Note: Repeatedly executing the same script may skip compilation because of cached data. You can use `TORCHINDUCTOR_FORCE_DISABLE_CACHES` together with this variable to force recompilation during each execution. | ||
| 114 | 110 | ||
| 115 | #### TORCHINDUCTOR_FORCE_DISABLE_CACHES | 111 | #### TORCHINDUCTOR_FORCE_DISABLE_CACHES |
| 116 | 112 | ||
| 117 | -Purpose: Native torch environment variable that disables Inductor cache, forcing recompilation each execution. | 113 | +Purpose: A native torch environment variable that disables the Inductor cache and forces recompilation during each execution. |
| 118 | 114 | ||
| 119 | Usage: | 115 | Usage: |
| 116 | + | ||
| 120 | ```bash | 117 | ```bash |
| 121 | export TORCHINDUCTOR_FORCE_DISABLE_CACHES=1 | 118 | export TORCHINDUCTOR_FORCE_DISABLE_CACHES=1 |
| 122 | ``` | 119 | ``` |
| 123 | -Note: Significantly increases graph startup time. Do not use this environment variable in actual deployment. | 120 | + |
| 121 | +Note: This significantly increases graph startup time. Do not use this environment variable in actual deployment. | ||
| 124 | 122 | ||
| 125 | #### Optional: ASCEND_LAUNCH_BLOCKING | 123 | #### Optional: ASCEND_LAUNCH_BLOCKING |
| 126 | 124 | ||
| 127 | -Purpose: Native torch_npu environment variable that enables Ascend kernel synchronous execution. Each kernel launch waits for completion, facilitating identification of the first erroneous kernel. | 125 | +Purpose: A native torch_npu environment variable that enables synchronous execution of Ascend kernels. Each Kernel launch waits for completion, making it easier to identify the first Kernel that reports an error. |
| 128 | 126 | ||
| 129 | Usage: | 127 | Usage: |
| 128 | + | ||
| 130 | ```bash | 129 | ```bash |
| 131 | export ASCEND_LAUNCH_BLOCKING=1 | 130 | export ASCEND_LAUNCH_BLOCKING=1 |
| 132 | ``` | 131 | ``` |
| 133 | -Note: Significantly reduces launch performance. Do not use this environment variable in actual deployment. | 132 | + |
| 133 | +Note: This significantly reduces launch performance. Do not use this environment variable in actual deployment. | ||
| 134 | 134 | ||
| 135 | #### Optional: AUTOFUSE_DFX_FLAGS | 135 | #### Optional: AUTOFUSE_DFX_FLAGS |
| 136 | 136 | ||
| 137 | -Purpose: AutoFuse DFX environment variable that dumps internal fusion graph structure for each auto-fusion operator. pbtxt files can be opened with netron.app. | 137 | +Purpose: An AutoFuse DFX environment variable that saves the internal fusion graph structure corresponding to each automatically fused operator. The generated `.pbtxt` files can be opened and viewed using netron.app. |
| 138 | 138 | ||
| 139 | Usage: | 139 | Usage: |
| 140 | + | ||
| 140 | ```bash | 141 | ```bash |
| 141 | export AUTOFUSE_DFX_FLAGS="--codegen_compile_debug=true;--debug_dir=/path-to-dump/" | 142 | export AUTOFUSE_DFX_FLAGS="--codegen_compile_debug=true;--debug_dir=/path-to-dump/" |
| 142 | ``` | 143 | ``` |
| 143 | -Note: Generates dump graphs for each fused operator from AutoFuse backend in the specified dump path. | 144 | + |
| 145 | +Note: The AutoFuse backend generates a dump graph for each fused operator in the specified dump path. | ||
| 144 | 146 | ||
| 145 | ### Result Analysis & Debug Output Analysis | 147 | ### Result Analysis & Debug Output Analysis |
| 146 | 148 | ||
| 147 | -After enabling TORCH_COMPILE_DEBUG, debug information output is located in the torch_compile_debug subdirectory under the execution directory. Directories prefixed with autofused_ are artifacts related to inductor-npu-ext, others are native Inductor artifacts. Each autofused_ prefix directory represents a white-box structure of a fusion operator. If no fusion operator is generated (that is, no fusion occurs), check the printed "Fallback aten.xxxx $reason: xx reason" information to determine the cause. Refer to [inductor-npu-ext User Manual](https://gitcode.com/Ascend/torchair/blob/master/experimental/_inductor_npu_ext/docs/manuals.md). | 149 | +After `TORCH_COMPILE_DEBUG` is enabled, debugging information is output to the `torch_compile_debug` subdirectory under the current execution directory. Directories prefixed with `autofused_` contain fused operator artifacts generated by the `torch_npu` AscendC backend, while the remaining directories contain native artifacts generated by PyTorch Inductor. Each directory prefixed with `autofused_` corresponds to the white-box structure of a fused operator and can be used to view the fusion scope and code generation results. If no directory prefixed with `autofused_` is generated, no fused operator was produced during the current compilation process. In this case, analyze the reason why fusion did not occur based on information such as `Fallback aten.xxxx $reason: xx reason` in the terminal output. |
| 148 | 150 | ||
| 149 | -Users can also observe operator performance gains after enabling auto-fusion through profiling configuration. For the sample use cases above, comment the "model = torch.compile(model, dynamic=False, fullgraph=True)" line to run single-operator flow. Then compare the total time of all operators in single-operator scenario in profiling with the total time of fusion operators when enabling Inductor + AutoFuse. For detailed Profiling performance analysis tool usage, refer to [Profiling Performance Analysis Tool Guide](https://hiascend.com/document/redirect/CannCommunityToolProfiling). | 151 | +Users can also use Profiling configurations to observe the operator performance gains after automatic fusion is enabled. For the preceding Sample use cases, comment out the entire `torch.compile(...)` code block so that the model runs in non-compiled mode, which can be used as a comparison scenario without automatic fusion enabled. |
| 150 | 152 | ||
| 151 | -Note that not all operators in the model can be fused. Operators not lowered at the Inductor layer still exist as single operators. Fusion improvement ratio equals (total operator time after fusion - total operator time before fusion) / total operator time before fusion. Further, observe the improvement of aiv_mte2_time (input transfer time) and aiv_mte3_time (output transfer time) for fusion operators. | 153 | +```python |
| 154 | +# model = torch.compile( | ||
| 155 | +# model, | ||
| 156 | +# dynamic=False, | ||
| 157 | +# fullgraph=True, | ||
| 158 | +# options={"npu_backend": "ascendc"}, | ||
| 159 | +# ) | ||
| 160 | +``` | ||
| 152 | 161 | ||
| 153 | -For precision analysis, refer to [Precision Debugging Tool Guide](https://hiascend.com/document/redirect/CannCommunityToolAccucacy). | 162 | +Collect profiling data for both scenarios—with auto-fusion disabled and enabled—and compare the total execution time of all relevant operators within the same computation scope. |
| 154 | 163 | ||
| 155 | -### Enabling in Complex Networks | 164 | +For details about how to use the Profiling performance analysis tool, refer to the [Profiling Performance Analysis Tool Guide](https://hiascend.com/document/redirect/CannCommunityToolProfiling). |
| 156 | 165 | ||
| 157 | -Users who wish to enable AutoFuse in networks only need to import inductor_npu_ext after importing torch at the beginning of the model file. | 166 | +Note that not all operators in a model can be fused. Operators that are not lowered at the Inductor layer remain as standalone operators.The fusion performance improvement is calculated as follows: |
| 167 | + | ||
| 168 | +`(Total execution time of all operators before fusion - Total execution time of all operators after fusion) / Total execution time of all operators before fusion` | ||
| 169 | + | ||
| 170 | +For further analysis, you can compare the fused operator with the corresponding standalone operators in terms of `aiv_mte2_time` (input data transfer time) and `aiv_mte3_time` (output data transfer time) to evaluate the reduction in data transfer overhead. | ||
| 171 | + | ||
| 172 | +For precision analysis, refer to the [Precision Debugging Tool Guide](https://hiascend.com/document/redirect/CannCommunityToolAccucacy). | ||
| 173 | + | ||
| 174 | +### Enabling AutoFuse in Complex Networks | ||
| 175 | + | ||
| 176 | +To enable AutoFuse in a network, users do not need to import `inductor_npu_ext` separately. Specify the AscendC backend in `torch.compile`: | ||
| 177 | + | ||
| 178 | +```python | ||
| 179 | +model = torch.compile( | ||
| 180 | + model, | ||
| 181 | + dynamic=False, | ||
| 182 | + fullgraph=True, | ||
| 183 | + options={"npu_backend": "ascendc"}, | ||
| 184 | +) | ||
| 185 | +``` | ||
| @@ -38,7 +38,7 @@ pytorch | |||
| 38 | 38 | ||
| 39 | Before running these examples, carefully read the [PyTorch Environment Installation Guide](../../../docs/env_install/pytorch/env_pytorch.md) and complete the following steps: | 39 | Before running these examples, carefully read the [PyTorch Environment Installation Guide](../../../docs/env_install/pytorch/env_pytorch.md) and complete the following steps: |
| 40 | 40 | ||
| 41 | -1. Ensure that the CANN package version is `9.0.0` or later. Install the toolkit and ops packages correctly by using [CANN Quick Installation](https://www.hiascend.com/cann/download?versionId=745&ids=d802%2Ch0501%2Ch0602%2Ch0701). For more information, see the [Installation Guide](../../../docs/zh/quick_install.md). | 41 | +1. Ensure that the CANN package version is `9.0.0` or later. Install the toolkit and ops packages correctly by using [CANN Quick Installation](https://www.hiascend.com/cann/download?versionId=745&ids=d802%2Ch0501%2Ch0602%2Ch0701). For more information, see the [Installation Guide](../../../docs/en/quick_install.md). |
| 42 | 42 | ||
| 43 | 2. Ensure that the `torch_npu` version is `2.9.0` or later. You can use the [environment quick installation script](../../../scripts/env_install/pytorch/setup_torch_npu_daily.sh) to quickly install the Python environment and `torch_npu`. | 43 | 2. Ensure that the `torch_npu` version is `2.9.0` or later. You can use the [environment quick installation script](../../../scripts/env_install/pytorch/setup_torch_npu_daily.sh) to quickly install the Python environment and `torch_npu`. |
| 44 | 44 | ||
| @@ -103,6 +103,6 @@ If the operator list contains a kernel whose name starts with `autofused_`, the | |||
| 103 | 103 | ||
| 104 | ## References | 104 | ## References |
| 105 | 105 | ||
| 106 | -* [Autofuse Overview and Quick Start](../../README.md) | 106 | +* [Autofuse Overview and Quick Start](../../README_en.md) |
| 107 | * [Profiling Performance Analysis Tool Guide](https://hiascend.com/document/redirect/CannCommunityToolProfiling) | 107 | * [Profiling Performance Analysis Tool Guide](https://hiascend.com/document/redirect/CannCommunityToolProfiling) |
| 108 | * [Accuracy Debugging Tool Guide](https://hiascend.com/document/redirect/CannCommunityToolAccucacy) | 108 | * [Accuracy Debugging Tool Guide](https://hiascend.com/document/redirect/CannCommunityToolAccucacy) |
| @@ -8,8 +8,10 @@ | |||
| 8 | 8 | ||
| 9 | ```text | 9 | ```text |
| 10 | ├── README.md # 本文档 | 10 | ├── README.md # 本文档 |
| 11 | +├── README_en.md # 英文文档 | ||
| 11 | └── af_tf_eleandele/ # elementwise 类型算子融合的样例 | 12 | └── af_tf_eleandele/ # elementwise 类型算子融合的样例 |
| 12 | ├── README.md # 样例说明 | 13 | ├── README.md # 样例说明 |
| 14 | + ├── README_en.md # 英文样例说明 | ||
| 13 | └── test_abs_relu_exp.py # 通过 autofuse 完成 abs + relu + exp 三个 elementwise 算子的融合 | 15 | └── test_abs_relu_exp.py # 通过 autofuse 完成 abs + relu + exp 三个 elementwise 算子的融合 |
| 14 | ``` | 16 | ``` |
| 15 | 17 | ||
| @@ -71,7 +73,6 @@ python3 test_abs_relu_exp.py --mode tf2-compat | |||
| 71 | ## 参考 | 73 | ## 参考 |
| 72 | 74 | ||
| 73 | - [Autofuse 简介](../../README.md) | 75 | - [Autofuse 简介](../../README.md) |
| 74 | -- [Autofuse 业务流程](../../../docs/zh/component_workflow.md) | ||
| 75 | - [环境编译部署](../../../docs/env_install/tensorflow/env_tf.md) | 76 | - [环境编译部署](../../../docs/env_install/tensorflow/env_tf.md) |
| 76 | - [aarch64 架构 TF 源码编译](../../../docs/env_install/tensorflow/build_tf_aarch64.md) | 77 | - [aarch64 架构 TF 源码编译](../../../docs/env_install/tensorflow/build_tf_aarch64.md) |
| 77 | - [精度调试工具指南](https://hiascend.com/document/redirect/CannCommunityToolAccucacy) | 78 | - [精度调试工具指南](https://hiascend.com/document/redirect/CannCommunityToolAccucacy) |
| @@ -0,0 +1,81 @@ | |||
| 1 | +# TensorFlow Scenario Examples | ||
| 2 | + | ||
| 3 | +## Function Description | ||
| 4 | + | ||
| 5 | +Use AutoFuse to perform operator fusion in TensorFlow networks. The AutoFuse fusion pass in GE (Graph Engine) automatically identifies operators that can be fused and completes the fusion. | ||
| 6 | + | ||
| 7 | +## Directory Structure | ||
| 8 | + | ||
| 9 | +```text | ||
| 10 | +├── README.md # Chinese documentation | ||
| 11 | +├── README_en.md # English documentation | ||
| 12 | +└── af_tf_eleandele/ # Example of elementwise operator fusion | ||
| 13 | + ├── README.md # Chinese example description | ||
| 14 | + ├── README_en.md # English example description | ||
| 15 | + └── test_abs_relu_exp.py # Uses AutoFuse to fuse the abs, relu, and exp elementwise operators | ||
| 16 | +``` | ||
| 17 | + | ||
| 18 | +## Prerequisites | ||
| 19 | + | ||
| 20 | +Before running this example, complete the following steps in sequence: | ||
| 21 | + | ||
| 22 | +1. Follow the [Installation Guide](../../../docs/en/quick_install.md) to correctly install the Toolkit and Ops packages and configure the environment variables. | ||
| 23 | +2. Follow [Environment Build and Deployment](../../../docs/env_install/tensorflow/env_tf.md) to set up the TensorFlow environment. On x86_64, TensorFlow can be installed directly using pip. On aarch64, TensorFlow must be built from source. | ||
| 24 | +3. Alternatively, use the one-click configuration script to automatically set up the environment. This script is available only for the **x86_64 architecture**: | ||
| 25 | + | ||
| 26 | + ```bash | ||
| 27 | + bash scripts/env_install/tensorflow/setup_tf_env.sh | ||
| 28 | + ``` | ||
| 29 | + | ||
| 30 | + After the script is complete, activate the environment: | ||
| 31 | + | ||
| 32 | + ```bash | ||
| 33 | + source scripts/env_install/env/activate_tf1.sh # TensorFlow 1.15 | ||
| 34 | + # Or | ||
| 35 | + source scripts/env_install/env/activate_tf2.sh # TensorFlow 2.6.5 | ||
| 36 | + ``` | ||
| 37 | + | ||
| 38 | + > **This script does not support the aarch64 architecture.** On aarch64, follow [Building TensorFlow from Source on aarch64](../../../docs/env_install/tensorflow/build_tf_aarch64.md) to perform the build manually. | ||
| 39 | + | ||
| 40 | +## Set Environment Variables | ||
| 41 | + | ||
| 42 | +```bash | ||
| 43 | +# Define the CANN package installation path based on the actual installation location. | ||
| 44 | +export CANN_INSTALL_PATH=/usr/local/Ascend | ||
| 45 | + | ||
| 46 | +# Load the driver-related environment variables from the CANN package. | ||
| 47 | +source $CANN_INSTALL_PATH/driver/bin/setenv.sh | ||
| 48 | + | ||
| 49 | +# Load the Toolkit-related environment variables from the CANN package. | ||
| 50 | +source $CANN_INSTALL_PATH/ascend-toolkit/set_env.sh | ||
| 51 | + | ||
| 52 | +# Assume that the example runs on device 0. | ||
| 53 | +export ASCEND_DEVICE_ID=0 | ||
| 54 | + | ||
| 55 | +# Enable automatic fusion. | ||
| 56 | +export AUTOFUSE_FLAGS="--enable_autofuse=true" | ||
| 57 | +``` | ||
| 58 | + | ||
| 59 | +## Run the Example | ||
| 60 | + | ||
| 61 | +```bash | ||
| 62 | +cd af_tf_eleandele | ||
| 63 | + | ||
| 64 | +# TensorFlow 1.15 environment | ||
| 65 | +python3 test_abs_relu_exp.py --mode tf1 | ||
| 66 | + | ||
| 67 | +# TensorFlow 2.6.5 environment in compatibility mode | ||
| 68 | +python3 test_abs_relu_exp.py --mode tf2-compat | ||
| 69 | +``` | ||
| 70 | + | ||
| 71 | +## Expected Result | ||
| 72 | + | ||
| 73 | +The script performs 100 inference steps. If no error is reported, the fused operator has been executed successfully. You can further verify the fusion result using graph dump files or Profiling data. | ||
| 74 | + | ||
| 75 | +## References | ||
| 76 | + | ||
| 77 | +- [AutoFuse Introduction](../../README_en.md) | ||
| 78 | +- [Environment Build and Deployment](../../../docs/env_install/tensorflow/env_tf.md) | ||
| 79 | +- [Building TensorFlow from Source on aarch64](../../../docs/env_install/tensorflow/build_tf_aarch64.md) | ||
| 80 | +- [Precision Debugging Tool Guide](https://hiascend.com/document/redirect/CannCommunityToolAccucacy) | ||
| 81 | +- [Profiling Performance Analysis Tool Guide](https://hiascend.com/document/redirect/CannCommunityToolProfiling) | ||
| @@ -0,0 +1,28 @@ | |||
| 1 | +# Elementwise + Elementwise Fusion Example (abs + relu + exp) | ||
| 2 | + | ||
| 3 | +## Function Description | ||
| 4 | + | ||
| 5 | +AutoFuse fuses the three elementwise operators `abs + relu + exp`. The script uses the `--mode` parameter to select the TensorFlow version: | ||
| 6 | + | ||
| 7 | +| Mode | TensorFlow Version | NPU Integration Method | Graph API | | ||
| 8 | +|------|--------------------|------------------------|-----------| | ||
| 9 | +| `tf1` | TensorFlow 1.15.0 | `npu_bridge` (registered through import side effects) | `tf.placeholder` + `Session` + `NpuOptimizer` | | ||
| 10 | +| `tf2-compat` | TensorFlow 2.6.5 | `npu_device.compat.enable_v1()` | `tf.compat.v1.placeholder` + `tf.compat.v1.Session` | | ||
| 11 | + | ||
| 12 | +## Execution Commands | ||
| 13 | + | ||
| 14 | +```bash | ||
| 15 | +# TensorFlow 1.15 environment | ||
| 16 | +source scripts/env_install/env/activate_tf1.sh | ||
| 17 | +python3 test_abs_relu_exp.py --mode tf1 | ||
| 18 | + | ||
| 19 | +# TensorFlow 2.6.5 environment (compatibility mode) | ||
| 20 | +source scripts/env_install/env/activate_tf2.sh | ||
| 21 | +python3 test_abs_relu_exp.py --mode tf2-compat | ||
| 22 | +``` | ||
| 23 | + | ||
| 24 | +## Expected Result | ||
| 25 | + | ||
| 26 | +The script constructs an `abs → relu → exp` computation graph and performs 100 inference steps on the NPU. If no error is reported, the fusion is successful. The three operators are fused into an `AscBackend`-type fused operator named `autofuse_pointwise_0_Abs_Relu_Exp`, which is executed as a single Kernel on the NPU. | ||
| 27 | + | ||
| 28 | +To view the fusion result, enable Profiling, which is already configured in the script. After execution is complete, check `PROF_*/mindstudio_profiler_output/op_summary_*.csv` in the `./profiling` directory. If the only Kernel is named `autofuse_pointwise_0_Abs_Relu_Exp`, the three operators have been fused into a single fused operator. | ||
| @@ -320,4 +320,4 @@ After installation, you can perform developer testing. Ensure you have completed | |||
| 320 | 320 | ||
| 321 | After execution, check the coverage status through the output log. Confirm all test cases pass. | 321 | After execution, check the coverage status through the output log. Confirm all test cases pass. |
| 322 | 322 | ||
| 323 | -**After installation, refer to [Sample Execution](../../super_kernel/examples/README_en.md) to try running samples**. | 323 | +**After the installation is complete, you can refer to the [SuperKernel sample](../../super_kernel/examples/README_en.md) or the [AutoFusion sample](../../autofuse/examples/pytorch/README_en.md) to run the examples.** |
| @@ -320,4 +320,4 @@ bash build.sh --pkg | |||
| 320 | 320 | ||
| 321 | 执行完成后根据输出日志查看覆盖率情况,确认所有测试用例通过。 | 321 | 执行完成后根据输出日志查看覆盖率情况,确认所有测试用例通过。 |
| 322 | 322 | ||
| 323 | -**安装完成后可参考[样例运行](../../super_kernel/examples/README.md)尝试运行样例**。 | 323 | +**安装完成后可参考[super_kernel样例运行](../../super_kernel/examples/README.md)或[autofusion样例运行](../../autofuse/examples/pytorch/README.md)尝试运行样例**。 |


config 配置是不是改为cmake 脚本文件