已合并
迁移symm/trmm/planarComplex/950gmmGelu至examples目录 #1109
sunhao_hw创建于 7 天前
迁移symm/trmm/planarComplex/950gmmGelu至examples目录 #1109
已合并
共 29 个文件变更+248-456
| @@ -14,3 +14,6 @@ | |||
| 14 | - [52_quant_multi_core_splitk_matmul_tla](../../../../examples/52_quant_multi_core_splitk_matmul_tla/52_quant_multi_core_splitk_matmul_tla.md) - 拆解模板库下的样例52、quant量化下的多核切K样例的实现。 | 14 | - [52_quant_multi_core_splitk_matmul_tla](../../../../examples/52_quant_multi_core_splitk_matmul_tla/52_quant_multi_core_splitk_matmul_tla.md) - 拆解模板库下的样例52、quant量化下的多核切K样例的实现。 |
| 15 | - [65_ascend950_fp8_mx_grouped_matmul_slice_m_swiglu_mx_quant](../../../../examples/65_ascend950_fp8_mx_grouped_matmul_slice_m_swiglu_mx_quant/65_ascend950_fp8_mx_grouped_matmul_slice_m_swiglu_mx_quant.md) - 拆解模板库下的样例65、亲和昇腾950硬件的FP8分组Matmul+Swiglu+Quant样例的实现。 | 15 | - [65_ascend950_fp8_mx_grouped_matmul_slice_m_swiglu_mx_quant](../../../../examples/65_ascend950_fp8_mx_grouped_matmul_slice_m_swiglu_mx_quant/65_ascend950_fp8_mx_grouped_matmul_slice_m_swiglu_mx_quant.md) - 拆解模板库下的样例65、亲和昇腾950硬件的FP8分组Matmul+Swiglu+Quant样例的实现。 |
| 16 | - [71_ascend950_fp8_mx_grouped_matmul_finalize_routing](../../../../examples/71_ascend950_fp8_mx_grouped_matmul_finalize_routing/71_ascend950_fp8_mx_grouped_matmul_finalize_routing.md) - 拆解模板库下的样例71、亲和昇腾950硬件的FP8分组Matmul+FinalizeRouting样例的实现。 | 16 | - [71_ascend950_fp8_mx_grouped_matmul_finalize_routing](../../../../examples/71_ascend950_fp8_mx_grouped_matmul_finalize_routing/71_ascend950_fp8_mx_grouped_matmul_finalize_routing.md) - 拆解模板库下的样例71、亲和昇腾950硬件的FP8分组Matmul+FinalizeRouting样例的实现。 |
| 17 | +- [75_symm](../../../../examples/75_symm/75_symm.md) - 拆解模板库下的样例75、symm对称矩阵乘样例的实现。 | ||
| 18 | +- [76_trmm](../../../../examples/76_trmm/76_trmm.md) - 拆解模板库下的样例76、trmm矩阵乘样例的实现。 | ||
| 19 | +- [77_planar_complex_matmul](../../../../examples/77_planar_complex_matmul/77_planar_complex_matmul.md) - 拆解模板库下的样例77、平面复数矩阵乘样例的实现。 | ||
| @@ -118,7 +118,7 @@ experimental | |||
| 118 | 118 | ||
| 119 | ## 编译及运行 | 119 | ## 编译及运行 |
| 120 | 120 | ||
| 121 | -- 获取代码之后编译相应的算子可执行文件,可参考[quickstart](../../../docs/zh/1_Practice/01_quick_start.md#编译执行),本用例为 Ascend950(3510)算子,编译时需加 `-DCATLASS_ARCH=3510`。 | 121 | +- 获取代码之后编译相应的算子可执行文件,可参考[quickstart](../../docs/zh/1_Practice/01_quick_start.md#编译执行),本用例为 Ascend950(3510)算子,编译时需加 `-DCATLASS_ARCH=3510`。 |
| 122 | 122 | ||
| 123 | ```shell | 123 | ```shell |
| 124 | # 编译指定用例 | 124 | # 编译指定用例 |
| @@ -22,7 +22,7 @@ experimental | |||
| 22 | 22 | ||
| 23 | ## 使用示例 | 23 | ## 使用示例 |
| 24 | 24 | ||
| 25 | -- 获取代码之后编译相应的算子可执行文件,可参考 [quickstart](../../../docs/zh/1_Practice/01_quick_start.md#编译执行),本用例为 Ascend950(3510)算子,编译时需加 `-DCATLASS_ARCH=3510`。 | 25 | +- 获取代码之后编译相应的算子可执行文件,可参考 [quickstart](../../docs/zh/1_Practice/01_quick_start.md#编译执行),本用例为 Ascend950(3510)算子,编译时需加 `-DCATLASS_ARCH=3510`。 |
| 26 | - 执行算子 | 26 | - 执行算子 |
| 27 | 27 | ||
| 28 | ```bash | 28 | ```bash |
| @@ -1,6 +1,6 @@ | |||
| 1 | # 对称矩阵乘算子(symm)设计文档 | 1 | # 对称矩阵乘算子(symm)设计文档 |
| 2 | 2 | ||
| 3 | -本文档用于说明 `experimental/matmul/symm` 对称矩阵乘算子示例所依赖的 Catlass GEMM 模板库能力、外部接口、分层设计方案。 | 3 | +本文档用于说明 `./examples/75_symm` 对称矩阵乘算子示例所依赖的 Catlass GEMM 模板库能力、外部接口、分层设计方案。 |
| 4 | 4 | ||
| 5 | ## 1. 功能说明 | 5 | ## 1. 功能说明 |
| 6 | 6 | ||
| @@ -364,7 +364,7 @@ using L0TileShape = Shape<256, 128, 32>; | |||
| 364 | ### 6.1 编译 | 364 | ### 6.1 编译 |
| 365 | 365 | ||
| 366 | ``` | 366 | ``` |
| 367 | -bash scripts/build.sh symm | 367 | +bash scripts/build.sh 75_symm |
| 368 | ``` | 368 | ``` |
| 369 | 369 | ||
| 370 | ### 6.2 运行 | 370 | ### 6.2 运行 |
| @@ -372,23 +372,23 @@ bash scripts/build.sh symm | |||
| 372 | 左乘,上三角: | 372 | 左乘,上三角: |
| 373 | 373 | ||
| 374 | ```bash | 374 | ```bash |
| 375 | -./symm 768 4096 768 0 0 1 | 375 | +.output/bin/75_symm 768 4096 768 0 0 1 |
| 376 | ``` | 376 | ``` |
| 377 | 377 | ||
| 378 | 左乘,下三角: | 378 | 左乘,下三角: |
| 379 | 379 | ||
| 380 | ```bash | 380 | ```bash |
| 381 | -./symm 768 4096 768 0 0 0 | 381 | +.output/bin/75_symm 768 4096 768 0 0 0 |
| 382 | ``` | 382 | ``` |
| 383 | 383 | ||
| 384 | 右乘,上三角: | 384 | 右乘,上三角: |
| 385 | 385 | ||
| 386 | ```bash | 386 | ```bash |
| 387 | -./symm 4096 768 768 0 1 1 | 387 | +.output/bin/75_symm 4096 768 768 0 1 1 |
| 388 | ``` | 388 | ``` |
| 389 | 389 | ||
| 390 | 右乘,下三角: | 390 | 右乘,下三角: |
| 391 | 391 | ||
| 392 | ```bash | 392 | ```bash |
| 393 | -./symm 4096 768 768 0 1 0 | 393 | +.output/bin/75_symm 4096 768 768 0 1 0 |
| 394 | ``` | 394 | ``` |
| @@ -9,4 +9,4 @@ | |||
| 9 | # ----------------------------------------------------------------------------------------------------------- | 9 | # ----------------------------------------------------------------------------------------------------------- |
| 10 | 10 | ||
| 11 | set_source_files_properties(symm.cpp PROPERTIES LANGUAGE ASC) | 11 | set_source_files_properties(symm.cpp PROPERTIES LANGUAGE ASC) |
| 12 | -catlass_example_add_executable(symm cube symm.cpp) | 12 | +catlass_example_add_executable(75_symm cube symm.cpp) |
| @@ -1,10 +1,8 @@ | |||
| 1 | -# SymmMatmul Example Readme | 1 | +# symmMatmul Example Readme |
| 2 | - | ||
| 3 | -> **注意**:本样例位于 `experimental/` 目录下,如需编译运行,请先将样例目录拷贝至 `examples/` 下,并在 `examples/CMakeLists.txt` 中添加样例名称 `symm`。 | ||
| 4 | 2 | ||
| 5 | ## 功能说明 | 3 | ## 功能说明 |
| 6 | 4 | ||
| 7 | -- 算子功能:完成对称矩阵乘法计算(SYMM),利用输入矩阵的对称性,仅读取上三角(或下三角)有效数据,通过 direct/transpose 双路径完成等价的全量矩阵乘。 | 5 | +- 算子功能:完成对称矩阵乘法计算(75_symm),利用输入矩阵的对称性,仅读取上三角(或下三角)有效数据,通过 direct/transpose 双路径完成等价的全量矩阵乘。 |
| 8 | - 计算公式: | 6 | - 计算公式: |
| 9 | 7 | ||
| 10 | 左侧对称矩阵乘(`symmSide=0`,要求 `M == K`): | 8 | 左侧对称矩阵乘(`symmSide=0`,要求 `M == K`): |
| @@ -56,12 +54,12 @@ | |||
| 56 | ## 代码组织 | 54 | ## 代码组织 |
| 57 | 55 | ||
| 58 | ```text | 56 | ```text |
| 59 | -experimental | 57 | +examples |
| 60 | -├── matmul | 58 | +├── 75_symm |
| 61 | -│ └── symm | 59 | +│ ├── CMakeLists.txt # CMake 编译文件 |
| 62 | -│ ├── CMakeLists.txt # CMake 编译文件 | 60 | +│ ├── 75_symm.md # 设计文档 |
| 63 | -│ ├── README.md | 61 | +│ ├── README.md |
| 64 | -│ └── symm.cpp # 主文件(包含 host 数据生成、kernel 调度、正确性验证) | 62 | +│ └── 75_symm.cpp # 主文件(包含 host 数据生成、kernel 调度、正确性验证) |
| 65 | ``` | 63 | ``` |
| 66 | 64 | ||
| 67 | ## 使用示例 | 65 | ## 使用示例 |
| @@ -69,7 +67,7 @@ experimental | |||
| 69 | 1. 编译样例代码: | 67 | 1. 编译样例代码: |
| 70 | 68 | ||
| 71 | ```bash | 69 | ```bash |
| 72 | - bash scripts/build.sh symm | 70 | + bash scripts/build.sh 75_symm |
| 73 | ``` | 71 | ``` |
| 74 | 72 | ||
| 75 | 2. 执行算子样例程序: | 73 | 2. 执行算子样例程序: |
| @@ -77,17 +75,18 @@ experimental | |||
| 77 | ```bash | 75 | ```bash |
| 78 | cd output/bin | 76 | cd output/bin |
| 79 | 77 | ||
| 78 | + # 可执行文件名 |矩阵m轴|n轴|k轴|Device ID|symmSide|symmFill | ||
| 80 | # 左乘 + 上三角:C = S(M×M) × B(M×N),S 上三角有效 | 79 | # 左乘 + 上三角:C = S(M×M) × B(M×N),S 上三角有效 |
| 81 | - ./symm 768 4096 768 0 0 1 | 80 | + ./75_symm 768 4096 768 0 0 1 |
| 82 | 81 | ||
| 83 | # 左乘 + 下三角 | 82 | # 左乘 + 下三角 |
| 84 | - ./symm 768 4096 768 0 0 0 | 83 | + ./75_symm 768 4096 768 0 0 0 |
| 85 | 84 | ||
| 86 | # 右乘 + 上三角:C = B(M×K) × S(K×K),S 上三角有效 | 85 | # 右乘 + 上三角:C = B(M×K) × S(K×K),S 上三角有效 |
| 87 | - ./symm 4096 768 768 0 1 1 | 86 | + ./75_symm 4096 768 768 0 1 1 |
| 88 | 87 | ||
| 89 | # 右乘 + 下三角 | 88 | # 右乘 + 下三角 |
| 90 | - ./symm 4096 768 768 0 1 0 | 89 | + ./75_symm 4096 768 768 0 1 0 |
| 91 | ``` | 90 | ``` |
| 92 | 91 | ||
| 93 | 参数含义依次为:`m n k deviceId symmSide symmFill`。 | 92 | 参数含义依次为:`m n k deviceId symmSide symmFill`。 |
| @@ -37,13 +37,13 @@ CATLASS [`trmm` 样例](./README.md)算子是基于 CATLASS Gemm API 实现的 | |||
| 37 | 关键组件包括: | 37 | 关键组件包括: |
| 38 | 38 | ||
| 39 | - **Example 组装**:[trmm.cpp](./trmm.cpp) | 39 | - **Example 组装**:[trmm.cpp](./trmm.cpp) |
| 40 | -- **Kernel 实现**:[trmm.hpp](../../../include/catlass/gemm/kernel/trmm.hpp) | 40 | +- **Kernel 实现**:[trmm.hpp](../../include/catlass/gemm/kernel/trmm.hpp) |
| 41 | - **Block 组件**: | 41 | - **Block 组件**: |
| 42 | - - 通用 MMAD 组件 [block_mmad_pingpong_tla.hpp](../../../include/catlass/gemm/block/block_mmad_pingpong_tla.hpp) | 42 | + - 通用 MMAD 组件 [block_mmad_pingpong_tla.hpp](../../include/catlass/gemm/block/block_mmad_pingpong_tla.hpp) |
| 43 | - - 基本块分发策略 [block_swizzle.hpp](../../../include/catlass/gemm/block/block_swizzle.hpp) | 43 | + - 基本块分发策略 [block_swizzle.hpp](../../include/catlass/gemm/block/block_swizzle.hpp) |
| 44 | - **Tile 组件**: | 44 | - **Tile 组件**: |
| 45 | - - GM/L1/L0 搬运组件 [tile_copy_tla.hpp](../../../include/catlass/gemm/tile/tile_copy_tla.hpp) | 45 | + - GM/L1/L0 搬运组件 [tile_copy_tla.hpp](../../include/catlass/gemm/tile/tile_copy_tla.hpp) |
| 46 | - - AIV 后处理搬运组件 [copy_gm_to_ub_tla.hpp](../../../include/catlass/epilogue/tile/copy_gm_to_ub_tla.hpp) 和 [copy_ub_to_gm_tla.hpp](../../../include/catlass/epilogue/tile/copy_ub_to_gm_tla.hpp) | 46 | + - AIV 后处理搬运组件 [copy_gm_to_ub_tla.hpp](../../include/catlass/epilogue/tile/copy_gm_to_ub_tla.hpp) 和 [copy_ub_to_gm_tla.hpp](../../include/catlass/epilogue/tile/copy_ub_to_gm_tla.hpp) |
| 47 | - **CMake 组装**:[CMakeLists.txt](./CMakeLists.txt) | 47 | - **CMake 组装**:[CMakeLists.txt](./CMakeLists.txt) |
| 48 | 48 | ||
| 49 | ## Example 组装 | 49 | ## Example 组装 |
| @@ -53,7 +53,7 @@ CATLASS [`trmm` 样例](./README.md)算子是基于 CATLASS Gemm API 实现的 | |||
| 53 | `trmm` 的命令格式为: | 53 | `trmm` 的命令格式为: |
| 54 | 54 | ||
| 55 | ```bash | 55 | ```bash |
| 56 | -./output/bin/trmm m n side uplo trans diag alpha [device_id] | 56 | +./output/bin/76_trmm m n side uplo trans diag alpha [device_id] |
| 57 | ``` | 57 | ``` |
| 58 | 58 | ||
| 59 | Host 侧通过 `TrmmOptions` 解析命令行参数,并根据 `side` 推导 `K`: | 59 | Host 侧通过 `TrmmOptions` 解析命令行参数,并根据 `side` 推导 `K`: |
| @@ -71,7 +71,7 @@ Host 示例在 CPU 侧构造输入: | |||
| 71 | - `side=left` 时,matA 为三角矩阵,matB 为 dense 矩阵。 | 71 | - `side=left` 时,matA 为三角矩阵,matB 为 dense 矩阵。 |
| 72 | - `side=right` 时,matA 为 dense 矩阵,matB 为三角矩阵。 | 72 | - `side=right` 时,matA 为 dense 矩阵,matB 为三角矩阵。 |
| 73 | - 三角矩阵按原始 `uplo` 保留 active half,inactive half 写 0。 | 73 | - 三角矩阵按原始 `uplo` 保留 active half,inactive half 写 0。 |
| 74 | -- dense 矩阵使用 [FillRandomData](../../../examples/common/golden/fill_data.hpp),三角矩阵使用 [FillTriangularData](../../../examples/common/golden/fill_data.hpp)。 | 74 | +- dense 矩阵使用 [FillRandomData](../../examples/common/golden/fill_data.hpp),三角矩阵使用 [FillTriangularData](../../examples/common/golden/fill_data.hpp)。 |
| 75 | 75 | ||
| 76 | inactive half 置零是当前 kernel 满足 TRMM 语义的前置条件。Kernel 做 tile 级 K 范围裁剪,不做逐元素三角 mask;同一个输出 tile 内仍可能覆盖到三角矩阵 inactive half,因此 inactive half 需要由调用侧保证为 0。 | 76 | inactive half 置零是当前 kernel 满足 TRMM 语义的前置条件。Kernel 做 tile 级 K 范围裁剪,不做逐元素三角 mask;同一个输出 tile 内仍可能覆盖到三角矩阵 inactive half,因此 inactive half 需要由调用侧保证为 0。 |
| 77 | 77 | ||
| @@ -224,7 +224,7 @@ CMake 写法与其他 matmul 类样例一致: | |||
| 224 | 224 | ||
| 225 | ```cmake | 225 | ```cmake |
| 226 | set_source_files_properties(trmm.cpp PROPERTIES LANGUAGE ASC) | 226 | set_source_files_properties(trmm.cpp PROPERTIES LANGUAGE ASC) |
| 227 | -catlass_example_add_executable(trmm mix trmm.cpp) | 227 | +catlass_example_add_executable(76_trmm mix trmm.cpp) |
| 228 | target_link_libraries(trmm PRIVATE pthread) | 228 | target_link_libraries(trmm PRIVATE pthread) |
| 229 | ``` | 229 | ``` |
| 230 | 230 | ||
| @@ -232,34 +232,34 @@ target_link_libraries(trmm PRIVATE pthread) | |||
| 232 | 232 | ||
| 233 | ```bash | 233 | ```bash |
| 234 | source /usr/local/Ascend/ascend-toolkit/set_env.sh | 234 | source /usr/local/Ascend/ascend-toolkit/set_env.sh |
| 235 | -bash scripts/build.sh trmm | 235 | +bash scripts/build.sh 76_trmm |
| 236 | ``` | 236 | ``` |
| 237 | 237 | ||
| 238 | 编译成功后会生成: | 238 | 编译成功后会生成: |
| 239 | 239 | ||
| 240 | ```text | 240 | ```text |
| 241 | -output/bin/trmm | 241 | +output/bin/76_trmm |
| 242 | ``` | 242 | ``` |
| 243 | 243 | ||
| 244 | 运行命令格式为: | 244 | 运行命令格式为: |
| 245 | 245 | ||
| 246 | ```bash | 246 | ```bash |
| 247 | -./output/bin/trmm m n side uplo trans diag alpha [device_id] | 247 | +./output/bin/76_trmm m n side uplo trans diag alpha [device_id] |
| 248 | ``` | 248 | ``` |
| 249 | 249 | ||
| 250 | 运行示例: | 250 | 运行示例: |
| 251 | 251 | ||
| 252 | ```bash | 252 | ```bash |
| 253 | -./output/bin/trmm 128 96 0 0 0 0 1.0 0 | 253 | +./output/bin/76_trmm 128 96 0 0 0 0 1.0 0 |
| 254 | -./output/bin/trmm 128 96 0 1 1 0 0.5 0 | 254 | +./output/bin/76_trmm 128 96 0 1 1 0 0.5 0 |
| 255 | -./output/bin/trmm 96 128 1 0 0 0 1.0 0 | 255 | +./output/bin/76_trmm 96 128 1 0 0 0 1.0 0 |
| 256 | -./output/bin/trmm 96 128 1 1 1 0 0.5 0 | 256 | +./output/bin/76_trmm 96 128 1 1 1 0 0.5 0 |
| 257 | ``` | 257 | ``` |
| 258 | 258 | ||
| 259 | Profiling 示例: | 259 | Profiling 示例: |
| 260 | 260 | ||
| 261 | ```bash | 261 | ```bash |
| 262 | -WARMUP=5 REPEAT=20 SKIP_OUTPUT=1 ./output/bin/trmm 4608 256 1 1 1 0 1.0 0 | 262 | +WARMUP=5 REPEAT=20 SKIP_OUTPUT=1 ./output/bin/76_trmm 4608 256 1 1 1 0 1.0 0 |
| 263 | ``` | 263 | ``` |
| 264 | 264 | ||
| 265 | ## 约束说明 | 265 | ## 约束说明 |
| @@ -9,5 +9,5 @@ | |||
| 9 | # ----------------------------------------------------------------------------------------------------------- | 9 | # ----------------------------------------------------------------------------------------------------------- |
| 10 | 10 | ||
| 11 | set_source_files_properties(trmm.cpp PROPERTIES LANGUAGE ASC) | 11 | set_source_files_properties(trmm.cpp PROPERTIES LANGUAGE ASC) |
| 12 | -catlass_example_add_executable(trmm mix trmm.cpp) | 12 | +catlass_example_add_executable(76_trmm mix trmm.cpp) |
| 13 | -target_link_libraries(trmm PRIVATE pthread) | 13 | +target_link_libraries(76_trmm PRIVATE pthread) |
| @@ -1,7 +1,5 @@ | |||
| 1 | # trmm | 1 | # trmm |
| 2 | 2 | ||
| 3 | -> **注意**:本样例位于 `experimental/` 目录下,如需编译运行,请先将样例目录拷贝至 `examples/` 下,并在 `examples/CMakeLists.txt` 中添加样例名称 `trmm`。 | ||
| 4 | - | ||
| 5 | ## 功能说明 | 3 | ## 功能说明 |
| 6 | 4 | ||
| 7 | 本样例演示基于 CATLASS GEMM kernel 组件实现 TRMM(triangular matrix multiply): | 5 | 本样例演示基于 CATLASS GEMM kernel 组件实现 TRMM(triangular matrix multiply): |
| @@ -16,7 +14,7 @@ | |||
| 16 | ## 构建 | 14 | ## 构建 |
| 17 | 15 | ||
| 18 | ```bash | 16 | ```bash |
| 19 | -bash scripts/build.sh trmm | 17 | +bash scripts/build.sh 76_trmm |
| 20 | ``` | 18 | ``` |
| 21 | 19 | ||
| 22 | ## 运行 | 20 | ## 运行 |
| @@ -24,14 +22,14 @@ bash scripts/build.sh trmm | |||
| 24 | 命令格式: | 22 | 命令格式: |
| 25 | 23 | ||
| 26 | ```bash | 24 | ```bash |
| 27 | -./output/bin/trmm m n side uplo trans diag alpha [device_id] | 25 | +./output/bin/76_trmm m n side uplo trans diag alpha [device_id] |
| 28 | ``` | 26 | ``` |
| 29 | 27 | ||
| 30 | 示例: | 28 | 示例: |
| 31 | 29 | ||
| 32 | ```bash | 30 | ```bash |
| 33 | -./output/bin/trmm 256 512 0 0 0 0 1.0 | 31 | +./output/bin/76_trmm 256 512 0 0 0 0 1.0 |
| 34 | -./output/bin/trmm 512 256 1 1 1 0 1.0 | 32 | +./output/bin/76_trmm 512 256 1 1 1 0 1.0 |
| 35 | ``` | 33 | ``` |
| 36 | 34 | ||
| 37 | 程序会在 host 侧构造三角矩阵与 dense 矩阵,运行 NPU kernel,并与 CPU reference 结果做精度比对。 | 35 | 程序会在 host 侧构造三角矩阵与 dense 矩阵,运行 NPU kernel,并与 CPU reference 结果做精度比对。 |
Rexperimental/matmul/planar_complex_matmul/77_planar_complex_matmul.md→examples/77_planar_complex_matmul/77_planar_complex_matmul.md+2-77
| @@ -1,7 +1,5 @@ | |||
| 1 | # PlanarComplexMatmul | 1 | # PlanarComplexMatmul |
| 2 | 2 | ||
| 3 | -本文档用于说明 `experimental/matmul/planar_complex_matmul` 平面复数矩阵乘算子示例所依赖的 Catlass GEMM 模板库能力、外部接口、分层设计方案。 | ||
| 4 | - | ||
| 5 | ## 1. 功能说明 | 3 | ## 1. 功能说明 |
| 6 | 4 | ||
| 7 | - 算子功能:完成平面复数矩阵乘计算。复数矩阵以实部、虚部分离的 planar complex 形式输入,样例输出实部与虚部两路结果。 | 5 | - 算子功能:完成平面复数矩阵乘计算。复数矩阵以实部、虚部分离的 planar complex 形式输入,样例输出实部与虚部两路结果。 |
| @@ -61,7 +59,7 @@ PlanarComplexMatmul 所涉及的关键模板参数如下: | |||
| 61 | 59 | ||
| 62 | #### 4.1.1 参数解析 | 60 | #### 4.1.1 参数解析 |
| 63 | 61 | ||
| 64 | -`planar_complex_matmul` 命令执行参数: | 62 | +`77_planar_complex_matmul` 命令执行参数: |
| 65 | 63 | ||
| 66 | ```text | 64 | ```text |
| 67 | m, n, k, [device_id], [--datapath DATA_PATH] | 65 | m, n, k, [device_id], [--datapath DATA_PATH] |
| @@ -186,7 +184,7 @@ Four-Pass 复用通用 BlockMmadTla。kernel 层负责 4 次调用的编排和 a | |||
| 186 | 184 | ||
| 187 | Fused 使用 `BlockMmadTla` 针对 `MmadPlanarComplexFused` policy 的偏特化(`block_mmad_planar_complex_fused_tla.hpp`): | 185 | Fused 使用 `BlockMmadTla` 针对 `MmadPlanarComplexFused` policy 的偏特化(`block_mmad_planar_complex_fused_tla.hpp`): |
| 188 | 186 | ||
| 189 | -1. **4 路输入 tensor**:`A_real`、(`A_imag` or `A_imag_signed`)、`B_real`、(`B_imag` or `B_imag_signed`)。block 对 `NEGATE_A` 无感知,始终从 Signed 槽位读 C_real 交叉项,从原始槽位读 C_imag。 | 187 | +1. **4 路输入 tensor**:`A_real`、(`A_imag` or `A_imag_signed`)、`B_real`、(`B_imag` or `B_imag_signed`)。block 对 `NEGATE_A` 无感知,始终从 Signed 槽位读 C_real 交叉项,从原始槽位读 C_imag。 |
| 190 | 2. **L1 4 槽 K-pingpong**:`[A_K0 | A_K1 | B_K0 | B_K1]`,A/B 槽位通用,GM 来源按子迭代交替。 | 188 | 2. **L1 4 槽 K-pingpong**:`[A_K0 | A_K1 | B_K0 | B_K1]`,A/B 槽位通用,GM 来源按子迭代交替。 |
| 191 | 3. **L0A/L0B 双缓冲 pingpong**:重叠 L1->L0 搬运与 Cube MMAD。 | 189 | 3. **L0A/L0B 双缓冲 pingpong**:重叠 L1->L0 搬运与 Cube MMAD。 |
| 192 | 4. **L0C 单缓冲**:C_real FixPipe 完成后 C_imag 才开始,分时复用。 | 190 | 4. **L0C 单缓冲**:C_real FixPipe 完成后 C_imag 才开始,分时复用。 |
| @@ -235,76 +233,3 @@ L0C 单缓冲(C_real/C_imag 分时复用)打满 128 KB;L0B 双缓冲打满 | |||
| 235 | - `NEGATE_A=false`(`m >= n`):`K × N × 2B`(对 `B_imag` 取负) | 233 | - `NEGATE_A=false`(`m >= n`):`K × N × 2B`(对 `B_imag` 取负) |
| 236 | 234 | ||
| 237 | Host 选择较小的一侧取负以减少 workspace 开销。 | 235 | Host 选择较小的一侧取负以减少 workspace 开销。 |
| 238 | - | ||
| 239 | -## 6. 代码组织 | ||
| 240 | - | ||
| 241 | -``` | ||
| 242 | -├── experimental | ||
| 243 | -│ └── matmul | ||
| 244 | -│ └── planar_complex_matmul | ||
| 245 | -│ ├── CMakeLists.txt # CMake 编译文件 | ||
| 246 | -│ ├── README.md # 本文档 | ||
| 247 | -│ ├── gen_data_compare.py # NumPy golden 数据生成与精度比对脚本 | ||
| 248 | -│ └── planar_complex_matmul.cpp # 样例主文件(Host 层入口) | ||
| 249 | -└── include | ||
| 250 | - └── catlass | ||
| 251 | - ├── gemm | ||
| 252 | - │ ├── block | ||
| 253 | - │ │ └── block_mmad_planar_complex_fused_tla.hpp | ||
| 254 | - │ │ # BlockMmadTla 针对 MmadPlanarComplexFused 的偏特化 | ||
| 255 | - │ │ # 实现 Fused 路径的双 stage K-loop 与 L0C 分时复用 | ||
| 256 | - │ └── kernel | ||
| 257 | - │ └── planar_complex_gemm_tla.hpp | ||
| 258 | - │ # PlanarComplexGemm 统一 kernel(Four-Pass / Fused 编排) | ||
| 259 | - │ # 含 NegateMatrixAiv AIV 取负预处理 | ||
| 260 | - └── dispatch_policy.hpp | ||
| 261 | - # MmadPlanarComplexFused policy(ENABLE_SHUFFLE_K 参数) | ||
| 262 | -``` | ||
| 263 | - | ||
| 264 | -## 7. 使用示例 | ||
| 265 | - | ||
| 266 | -1. 编译样例代码,并生成相应的算子可执行文件。 | ||
| 267 | - | ||
| 268 | -``` | ||
| 269 | -bash scripts/build.sh planar_complex_matmul | ||
| 270 | -``` | ||
| 271 | - | ||
| 272 | -2. 切换到可执行文件的编译目录 `output/bin` 下,执行算子样例程序。该方式随机生成输入数据,只输出 kernel 调度路径与平均耗时,不做精度比对。 | ||
| 273 | - | ||
| 274 | -``` | ||
| 275 | -cd output/bin | ||
| 276 | -./planar_complex_matmul 256 512 1024 0 | ||
| 277 | -``` | ||
| 278 | - | ||
| 279 | -• 256:矩阵 m 轴 | ||
| 280 | - | ||
| 281 | -• 512:矩阵 n 轴 | ||
| 282 | - | ||
| 283 | -• 1024:矩阵 k 轴 | ||
| 284 | - | ||
| 285 | -• 0:Device ID,可选,默认为 0 | ||
| 286 | - | ||
| 287 | -执行结果中包含如下信息,说明样例执行成功。 | ||
| 288 | - | ||
| 289 | -``` | ||
| 290 | -PlanarComplexGemm dispatch: M=256 N=512 K=1024 ... -> Fused ... | ||
| 291 | -PlanarComplexGemm: M=256 N=512 K=1024 variant=Fused gemm=... ms (20 iters) | ||
| 292 | -No --datapath provided, skipping validation. | ||
| 293 | -``` | ||
| 294 | - | ||
| 295 | -3. 使用 `gen_data_compare.py` 生成输入数据、运行 NPU 可执行文件并与 NumPy golden 结果进行比对。脚本默认从仓库根目录自动定位 `output/bin/planar_complex_matmul`,默认在当前目录下生成 `data`、`golden` 目录并在结束后删除;如需指定保存路径可使用 `--save_path`,如需保留可指定 `--clean false`。 | ||
| 296 | - | ||
| 297 | -``` | ||
| 298 | -python examples/planar_complex_matmul/gen_data_compare.py 256 512 1024 | ||
| 299 | -``` | ||
| 300 | - | ||
| 301 | -执行结果如下,说明精度比对成功。 | ||
| 302 | - | ||
| 303 | -``` | ||
| 304 | -Data generated: M=256, N=512, K=1024, BLAS threads=8 | ||
| 305 | -------计算npu------ | ||
| 306 | ------- 计算相对误差 ----- | ||
| 307 | -Precision metric: ... | ||
| 308 | ------- 开始比较 ------ | ||
| 309 | -比较结果:Compare success | ||
| 310 | -``` | ||
Rexperimental/matmul/planar_complex_matmul/CMakeLists.txt→examples/77_planar_complex_matmul/CMakeLists.txt+1-1
| @@ -10,4 +10,4 @@ | |||
| 10 | 10 | ||
| 11 | # Mix mode is required: AIV produces the signed imaginary workspace before AIC consumes it. | 11 | # Mix mode is required: AIV produces the signed imaginary workspace before AIC consumes it. |
| 12 | set_source_files_properties(planar_complex_matmul.cpp PROPERTIES LANGUAGE ASC) | 12 | set_source_files_properties(planar_complex_matmul.cpp PROPERTIES LANGUAGE ASC) |
| 13 | -catlass_example_add_executable(planar_complex_matmul mix planar_complex_matmul.cpp) | 13 | +catlass_example_add_executable(77_planar_complex_matmul mix planar_complex_matmul.cpp) |
| @@ -0,0 +1,110 @@ | |||
| 1 | +# PlanarComplexMatmul | ||
| 2 | + | ||
| 3 | +本文档用于说明 `examples/77_planar_complex_matmul` 平面复数矩阵乘算子示例所依赖的 Catlass GEMM 模板库能力、外部接口、分层设计方案。 | ||
| 4 | + | ||
| 5 | +## 1. 功能说明 | ||
| 6 | + | ||
| 7 | + - 算子功能:完成平面复数矩阵乘计算。复数矩阵以实部、虚部分离的 planar complex 形式输入,样例输出实部与虚部两路结果。 | ||
| 8 | + - 计算公式: | ||
| 9 | + | ||
| 10 | +$$ | ||
| 11 | +\begin{aligned} | ||
| 12 | + A &= A_{real} + iA_{imag} \\ | ||
| 13 | + B &= B_{real} + iB_{imag} \\ | ||
| 14 | + C &= A \times B \\ | ||
| 15 | + C_{real} &= A_{real} \times B_{real} - A_{imag} \times B_{imag} \\ | ||
| 16 | + C_{imag} &= A_{real} \times B_{imag} + A_{imag} \times B_{real} | ||
| 17 | + \end{aligned} | ||
| 18 | +$$ | ||
| 19 | + | ||
| 20 | + 其中 `A_real`、`A_imag` 是形如 `(m, k)` 的左矩阵实部和虚部,`B_real`、`B_imag` 是形如 `(k, n)` 的右矩阵实部和虚部,`C_real`、`C_imag` 是形如 `(m, n)` 的输出矩阵实部和虚部。 | ||
| 21 | + | ||
| 22 | + | ||
| 23 | +## 2. 参数说明 | ||
| 24 | + | ||
| 25 | +以下是本样例可执行文件的运行参数: | ||
| 26 | + | ||
| 27 | +| 参数名 | 描述 | 约束 | | ||
| 28 | +| ----- | -------- | ------ | | ||
| 29 | +| `m` | 复数矩阵乘中左矩阵 A 的行 | 正整数 | | ||
| 30 | +| `n` | 复数矩阵乘中右矩阵 B 的列 | 正整数 | | ||
| 31 | +| `k` | 复数矩阵乘中左矩阵 A 的列,也即右矩阵 B 的行 | 正整数 | | ||
| 32 | +| `deviceId` | 使用的 NPU 卡 ID(默认 0) | 在设备 NPU 有效范围内 | | ||
| 33 | +| `--datapath` | 输入数据与输出数据目录 | 可选;未指定时仅随机生成输入并统计 kernel 耗时 | | ||
| 34 | + | ||
| 35 | + | ||
| 36 | +## 3. 代码组织 | ||
| 37 | + | ||
| 38 | +```text | ||
| 39 | +├── examples | ||
| 40 | +│ └── 77_planar_complex_matmul | ||
| 41 | +│ ├── CMakeLists.txt # CMake 编译文件 | ||
| 42 | +│ ├── README.md # 本文档 | ||
| 43 | +│ ├── 77_planar_complex_matmul.md # 设计文档 | ||
| 44 | +│ ├── gen_data_compare.py # NumPy golden 数据生成与精度比对脚本 | ||
| 45 | +│ └── planar_complex_matmul.cpp # 样例主文件(Host 层入口) | ||
| 46 | +└── include | ||
| 47 | + └── catlass | ||
| 48 | + ├── gemm | ||
| 49 | + │ ├── block | ||
| 50 | + │ │ └── block_mmad_planar_complex_fused_tla.hpp | ||
| 51 | + │ │ # BlockMmadTla 针对 MmadPlanarComplexFused 的偏特化 | ||
| 52 | + │ │ # 实现 Fused 路径的双 stage K-loop 与 L0C 分时复用 | ||
| 53 | + │ ├── device | ||
| 54 | + │ │ └── (复用主仓库 device_gemm.hpp) | ||
| 55 | + │ │ # Device 层薄封装:参数透传 + KernelAdapter 启动 | ||
| 56 | + │ └── kernel | ||
| 57 | + │ └── planar_complex_gemm_tla.hpp | ||
| 58 | + │ # PlanarComplexGemm 统一 kernel(Four-Pass / Fused 编排) | ||
| 59 | + │ # 含 NegateMatrixAiv AIV 取负预处理组件 | ||
| 60 | + └── dispatch_policy.hpp | ||
| 61 | + # MmadPlanarComplexFused policy(仅 ENABLE_SHUFFLE_K 参数) | ||
| 62 | +``` | ||
| 63 | + | ||
| 64 | +## 4. 使用示例 | ||
| 65 | + | ||
| 66 | +1. 编译样例代码,并生成相应的算子可执行文件。 | ||
| 67 | + | ||
| 68 | + ``` | ||
| 69 | + bash scripts/build.sh 77_planar_complex_matmul | ||
| 70 | + ``` | ||
| 71 | + | ||
| 72 | +2. 切换到可执行文件的编译目录 `output/bin` 下,执行算子样例程序。该方式随机生成输入数据,只输出 kernel 调度路径与平均耗时,不做精度比对。 | ||
| 73 | + | ||
| 74 | + ``` | ||
| 75 | + cd output/bin | ||
| 76 | + ./77_planar_complex_matmul 256 512 1024 0 | ||
| 77 | + ``` | ||
| 78 | + | ||
| 79 | + • 256:矩阵 m 轴 | ||
| 80 | + | ||
| 81 | + • 512:矩阵 n 轴 | ||
| 82 | + | ||
| 83 | + • 1024:矩阵 k 轴 | ||
| 84 | + | ||
| 85 | + • 0:Device ID,可选,默认为 0 | ||
| 86 | + | ||
| 87 | + 执行结果中包含如下信息,说明样例执行成功。 | ||
| 88 | + | ||
| 89 | + ``` | ||
| 90 | + PlanarComplexGemm dispatch: M=256 N=512 K=1024 ... -> Fused ... | ||
| 91 | + PlanarComplexGemm: M=256 N=512 K=1024 variant=Fused gemm=... ms (20 iters) | ||
| 92 | + No --datapath provided, skipping validation. | ||
| 93 | + ``` | ||
| 94 | + | ||
| 95 | +3. 在`catlass`目录下,使用 `gen_data_compare.py` 生成输入数据、运行 NPU 可执行文件并与 NumPy golden 结果进行比对。脚本默认从仓库根目录自动定位 `output/bin/77_planar_complex_matmul`,默认在当前目录下生成 `data`、`golden` 目录并在结束后删除;如需指定保存路径可使用 `--save_path`,如需保留可指定 `--clean false`。 | ||
| 96 | + | ||
| 97 | + ``` | ||
| 98 | + python examples/77_planar_complex_matmul/gen_data_compare.py 256 512 1024 | ||
| 99 | + ``` | ||
| 100 | + | ||
| 101 | + 执行结果如下,说明精度比对成功。 | ||
| 102 | + | ||
| 103 | + ``` | ||
| 104 | + Data generated: M=256, N=512, K=1024, BLAS threads=8 | ||
| 105 | + ------计算npu------ | ||
| 106 | + ------ 计算相对误差 ----- | ||
| 107 | + Precision metric: ... | ||
| 108 | + ------ 开始比较 ------ | ||
| 109 | + 比较结果:Compare success | ||
| 110 | + ``` | ||
Rexperimental/matmul/planar_complex_matmul/gen_data_compare.py→examples/77_planar_complex_matmul/gen_data_compare.py+1-1
| @@ -67,7 +67,7 @@ def gen_data(m, n, k, seed, blas_threads, save_path): | |||
| 67 | def get_default_op_path(): | 67 | def get_default_op_path(): |
| 68 | current_dir = os.path.dirname(os.path.abspath(__file__)) | 68 | current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 69 | catlass_home_dir = os.path.dirname(os.path.dirname(current_dir)) | 69 | catlass_home_dir = os.path.dirname(os.path.dirname(current_dir)) |
| 70 | - return os.path.join(catlass_home_dir, "output", "bin", "planar_complex_matmul") | 70 | + return os.path.join(catlass_home_dir, "output", "bin", "77_planar_complex_matmul") |
| 71 | 71 | ||
| 72 | 72 | ||
| 73 | def run_op(op_path, m, n, k, device_id, save_path): | 73 | def run_op(op_path, m, n, k, device_id, save_path): |
Rexperimental/matmul/planar_complex_matmul/planar_complex_matmul.cpp→examples/77_planar_complex_matmul/planar_complex_matmul.cpp+0-0
文件重命名但无更改。
Rexperimental/gmm/grouped_matmul_slice_m_gelu/CMakeLists.txt→examples/80_ascend950_grouped_matmul_slice_m_gelu/CMakeLists.txt+3-3
| @@ -8,6 +8,6 @@ | |||
| 8 | # See LICENSE in the root of the software repository for the full text of the License. | 8 | # See LICENSE in the root of the software repository for the full text of the License. |
| 9 | # ---------------------------------------------------------------------------- | 9 | # ---------------------------------------------------------------------------- |
| 10 | 10 | ||
| 11 | -set_source_files_properties(grouped_matmul_slice_m_gelu.cpp PROPERTIES LANGUAGE ASC) | 11 | +set_source_files_properties(ascend950_grouped_matmul_slice_m_gelu.cpp PROPERTIES LANGUAGE ASC) |
| 12 | -catlass_example_add_executable(grouped_matmul_slice_m_gelu mix grouped_matmul_slice_m_gelu.cpp) | 12 | +catlass_example_add_executable(80_ascend950_grouped_matmul_slice_m_gelu mix ascend950_grouped_matmul_slice_m_gelu.cpp) |
| 13 | -target_compile_definitions(grouped_matmul_slice_m_gelu PRIVATE L2_CACHE_HINT) | 13 | +target_compile_definitions(80_ascend950_grouped_matmul_slice_m_gelu PRIVATE L2_CACHE_HINT) |
Rexperimental/gmm/grouped_matmul_slice_m_gelu/README.md→examples/80_ascend950_grouped_matmul_slice_m_gelu/README.md+7-9
| @@ -1,14 +1,12 @@ | |||
| 1 | # GroupedMatmulSliceMGelu Example Readme | 1 | # GroupedMatmulSliceMGelu Example Readme |
| 2 | 2 | ||
| 3 | -> **注意**:本样例位于 `experimental/` 目录下,如需编译运行,请先将样例目录拷贝至 `examples/` 下,并在 `examples/CMakeLists.txt` 中添加样例名称 `grouped_matmul_slice_m_gelu`。 | ||
| 4 | - | ||
| 5 | ## 代码组织 | 3 | ## 代码组织 |
| 6 | 4 | ||
| 7 | ``` | 5 | ``` |
| 8 | -./experimental/gmm/grouped_matmul_slice_m_gelu/ | 6 | +./examples/80_ascend950_grouped_matmul_slice_m_gelu/ |
| 9 | ├── CMakeLists.txt // CMake编译文件 | 7 | ├── CMakeLists.txt // CMake编译文件 |
| 10 | ├── gen_data.py // 数据生成脚本 | 8 | ├── gen_data.py // 数据生成脚本 |
| 11 | -├── grouped_matmul_slice_m_gelu.cpp // 主文件 | 9 | +├── 80_ascend950_grouped_matmul_slice_m_gelu.cpp // 主文件 |
| 12 | ├── launcher | 10 | ├── launcher |
| 13 | │ └── grouped_matmul_slice_m_gelu_launcher.hpp // launcher文件 | 11 | │ └── grouped_matmul_slice_m_gelu_launcher.hpp // launcher文件 |
| 14 | └── README.md // 说明文件 | 12 | └── README.md // 说明文件 |
| @@ -23,12 +21,12 @@ | |||
| 23 | ## 使用示例 | 21 | ## 使用示例 |
| 24 | 22 | ||
| 25 | 因为GroupedMatmulGelu参数较多,所以该示例直接在代码中承载输出参数列表`groupList`, 通过`golden::GenerateGroupList`来生成随机切分的序列。 | 23 | 因为GroupedMatmulGelu参数较多,所以该示例直接在代码中承载输出参数列表`groupList`, 通过`golden::GenerateGroupList`来生成随机切分的序列。 |
| 26 | -相关输入配置具体详见[grouped_matmul_slice_m_gelu.cpp](grouped_matmul_slice_m_gelu.cpp)。 | 24 | +相关输入配置具体详见[ascend950_grouped_matmul_slice_m_gelu.cpp](ascend950_grouped_matmul_slice_m_gelu.cpp)。 |
| 27 | 如果需要输入grouplist配置(例如通过tensorList方式构造输入),可以参考python_extension中相应实现 | 25 | 如果需要输入grouplist配置(例如通过tensorList方式构造输入),可以参考python_extension中相应实现 |
| 28 | 26 | ||
| 29 | example使用 | 27 | example使用 |
| 30 | 28 | ||
| 31 | -- 获取代码之后编译相应的算子可执行文件,可参考[快速入门](../../../docs/zh/1_Practice/01_quick_start.md) | 29 | +- 获取代码之后编译相应的算子可执行文件,可参考[快速入门](../../docs/zh/1_Practice/01_quick_start.md) |
| 32 | - 执行算子 | 30 | - 执行算子 |
| 33 | 31 | ||
| 34 | ``` | 32 | ``` |
| @@ -36,7 +34,7 @@ example使用 | |||
| 36 | function build() { | 34 | function build() { |
| 37 | rm -rf ./build | 35 | rm -rf ./build |
| 38 | rm -rf ./output | 36 | rm -rf ./output |
| 39 | - bash scripts/build.sh grouped_matmul_slice_m_gelu -DCATLASS_ARCH=3510 | 37 | + bash scripts/build.sh 80_ascend950_grouped_matmul_slice_m_gelu -DCATLASS_ARCH=3510 |
| 40 | } | 38 | } |
| 41 | build | 39 | build |
| 42 | 40 | ||
| @@ -46,9 +44,9 @@ n=256 | |||
| 46 | k=256 | 44 | k=256 |
| 47 | device_id=1 | 45 | device_id=1 |
| 48 | 46 | ||
| 49 | -python ./examples/grouped_matmul_slice_m_gelu/gen_data.py "$group_num" "$m" "$n" "$k" "$device_id" | 47 | +python ./examples/80_ascend950_grouped_matmul_slice_m_gelu/gen_data.py "$group_num" "$m" "$n" "$k" "$device_id" |
| 50 | 48 | ||
| 51 | -./output/bin/grouped_matmul_slice_m_gelu $group_num $m $n $k $device_id | 49 | +./output/bin/80_ascend950_grouped_matmul_slice_m_gelu $group_num $m $n $k $device_id |
| 52 | ``` | 50 | ``` |
| 53 | 51 | ||
| 54 | 执行结果如下,说明精度比对成功。 | 52 | 执行结果如下,说明精度比对成功。 |
Rexperimental/gmm/grouped_matmul_slice_m_gelu/grouped_matmul_slice_m_gelu.cpp→examples/80_ascend950_grouped_matmul_slice_m_gelu/ascend950_grouped_matmul_slice_m_gelu.cpp+1-1
| @@ -31,7 +31,7 @@ using Options = GroupedGemmOptions; | |||
| 31 | 31 | ||
| 32 | void Run(Options const& options) | 32 | void Run(Options const& options) |
| 33 | { | 33 | { |
| 34 | - std::string datapath = "./examples/grouped_matmul_slice_m_gelu/data/"; | 34 | + std::string datapath = "./examples/80_ascend950_grouped_matmul_slice_m_gelu/data/"; |
| 35 | 35 | ||
| 36 | aclrtStream stream{nullptr}; | 36 | aclrtStream stream{nullptr}; |
| 37 | ACL_CHECK(aclInit(nullptr)); | 37 | ACL_CHECK(aclInit(nullptr)); |
Rexperimental/gmm/grouped_matmul_slice_m_gelu/gen_data.py→examples/80_ascend950_grouped_matmul_slice_m_gelu/gen_data.py+0-0
文件重命名但无更改。
Rexperimental/gmm/grouped_matmul_slice_m_gelu/launcher/grouped_matmul_slice_m_gelu_launcher.h→examples/80_ascend950_grouped_matmul_slice_m_gelu/launcher/grouped_matmul_slice_m_gelu_launcher.h+0-0
文件重命名但无更改。
| @@ -141,6 +141,9 @@ set(EXAMPLE_ATLASA2 | |||
| 141 | 44_quant_matmul_full_loadA_tla | 141 | 44_quant_matmul_full_loadA_tla |
| 142 | 45_strided_batched_matmul_tla | 142 | 45_strided_batched_matmul_tla |
| 143 | 52_quant_multi_core_splitk_matmul_tla | 143 | 52_quant_multi_core_splitk_matmul_tla |
| 144 | + 75_symm | ||
| 145 | + 76_trmm | ||
| 146 | + 77_planar_complex_matmul | ||
| 144 | 102_dynamic_optimized_matmul | 147 | 102_dynamic_optimized_matmul |
| 145 | 103_dynamic_optimized_quant_matmul_per_token_basic | 148 | 103_dynamic_optimized_quant_matmul_per_token_basic |
| 146 | ) | 149 | ) |
| @@ -174,6 +177,7 @@ set(EXAMPLE_ASCEND950 | |||
| 174 | 71_ascend950_fp8_mx_grouped_matmul_finalize_routing | 177 | 71_ascend950_fp8_mx_grouped_matmul_finalize_routing |
| 175 | 73_ascend950_matmul_full_loadA | 178 | 73_ascend950_matmul_full_loadA |
| 176 | 74_ascend950_weight_quant_a8w4_grouped_mx_matmul | 179 | 74_ascend950_weight_quant_a8w4_grouped_mx_matmul |
| 180 | + 80_ascend950_grouped_matmul_slice_m_gelu | ||
| 177 | ) | 181 | ) |
| 178 | 182 | ||
| 179 | if(CATLASS_ARCH STREQUAL "2201") | 183 | if(CATLASS_ARCH STREQUAL "2201") |
Dexperimental/matmul/planar_complex_matmul/README.md+0-308
| @@ -1,308 +0,0 @@ | |||
| 1 | -# PlanarComplexMatmul | ||
| 2 | - | ||
| 3 | -> **注意**:本样例位于 `experimental/` 目录下,如需编译运行,请先将样例目录拷贝至 `examples/` 下,并在 `examples/CMakeLists.txt` 中添加样例名称 `planar_complex_matmul`。 | ||
| 4 | - | ||
| 5 | -本文档用于说明 `experimental/matmul/planar_complex_matmul` 平面复数矩阵乘算子示例所依赖的 Catlass GEMM 模板库能力、外部接口、分层设计方案。 | ||
| 6 | - | ||
| 7 | -## 1. 功能说明 | ||
| 8 | - | ||
| 9 | - - 算子功能:完成平面复数矩阵乘计算。复数矩阵以实部、虚部分离的 planar complex 形式输入,样例输出实部与虚部两路结果。 | ||
| 10 | - - 计算公式: | ||
| 11 | - | ||
| 12 | -$$ | ||
| 13 | -\begin{aligned} | ||
| 14 | - A &= A_{real} + iA_{imag} \\ | ||
| 15 | - B &= B_{real} + iB_{imag} \\ | ||
| 16 | - C &= A \times B \\ | ||
| 17 | - C_{real} &= A_{real} \times B_{real} - A_{imag} \times B_{imag} \\ | ||
| 18 | - C_{imag} &= A_{real} \times B_{imag} + A_{imag} \times B_{real} | ||
| 19 | - \end{aligned} | ||
| 20 | -$$ | ||
| 21 | - | ||
| 22 | - 其中 `A_real`、`A_imag` 是形如 `(m, k)` 的左矩阵实部和虚部,`B_real`、`B_imag` 是形如 `(k, n)` 的右矩阵实部和虚部,`C_real`、`C_imag` 是形如 `(m, n)` 的输出矩阵实部和虚部。 | ||
| 23 | - | ||
| 24 | - | ||
| 25 | -## 2. 参数说明 | ||
| 26 | - | ||
| 27 | -以下是本样例可执行文件的运行参数: | ||
| 28 | - | ||
| 29 | -| 参数名 | 描述 | 约束 | | ||
| 30 | -| ----- | -------- | ------ | | ||
| 31 | -| `m` | 复数矩阵乘中左矩阵 A 的行 | 正整数 | | ||
| 32 | -| `n` | 复数矩阵乘中右矩阵 B 的列 | 正整数 | | ||
| 33 | -| `k` | 复数矩阵乘中左矩阵 A 的列,也即右矩阵 B 的行 | 正整数 | | ||
| 34 | -| `deviceId` | 使用的 NPU 卡 ID(默认 0) | 在设备 NPU 有效范围内 | | ||
| 35 | -| `--datapath` | 输入数据与输出数据目录 | 可选;未指定时仅随机生成输入并统计 kernel 耗时 | | ||
| 36 | - | ||
| 37 | -PlanarComplexMatmul 所涉及的关键模板参数如下: | ||
| 38 | - | ||
| 39 | -| 模板参数 | 说明 | 当前样例取值 | | ||
| 40 | -| ----- | -------- | -------------- | | ||
| 41 | -| `ElementA` | 左矩阵实部/虚部的数据类型 | `half` | | ||
| 42 | -| `ElementB` | 右矩阵实部/虚部的数据类型 | `half` | | ||
| 43 | -| `ElementC` | 输出矩阵实部/虚部的数据类型 | `float` | | ||
| 44 | -| `LayoutA` | 左矩阵排布方式 | `layout::RowMajor` | | ||
| 45 | -| `LayoutB` | 右矩阵排布方式 | `layout::ColumnMajor` | | ||
| 46 | -| `LayoutC` | 输出矩阵排布方式 | `layout::RowMajor` | | ||
| 47 | -| `ArchTag` | 目标架构 | `Arch::AtlasA2` | | ||
| 48 | -| `L1TileShape` | L1 tile 形状 | `GemmShape<128, 256, 256>` | | ||
| 49 | -| `L0TileShape` | L0 tile 形状 | `GemmShape<128, 256, 64>` | | ||
| 50 | -| `DispatchPolicy` (Four-Pass) | 4-pass 路径的 MMAD 调度策略 | `Gemm::MmadPingpong<ArchTag, true>` | | ||
| 51 | -| `DispatchPolicy` (Fused) | Fused 路径的 MMAD 调度策略 | `Gemm::MmadPlanarComplexFused<ArchTag, true>` | | ||
| 52 | - | ||
| 53 | -## 3. 约束说明 | ||
| 54 | - | ||
| 55 | - - 输入矩阵实部和虚部均为 fp16,输出实部和虚部均为 fp32。 | ||
| 56 | - - `B_real`、`B_imag` 在设备侧按 `layout::ColumnMajor` 读取;使用 `gen_data_compare.py` 校验时脚本会将 NumPy 生成的 B 矩阵转置后写入输入文件。 | ||
| 57 | - - 样例根据 shape 在 Host 侧选择执行路径:当 `k >= 6000` 且每个 AIC core 分到的 MN tile 数不少于 3 时选择 Four-Pass,否则选择 Fused。 | ||
| 58 | - - 样例根据 `m` 与 `n` 的关系选择对 `A_imag` 或 `B_imag` 取负后写入 workspace,用于计算 `C_real` 中的负号项。 | ||
| 59 | - | ||
| 60 | -## 4. 具体设计方案 | ||
| 61 | - | ||
| 62 | -### 4.1 Host 层 | ||
| 63 | - | ||
| 64 | -#### 4.1.1 参数解析 | ||
| 65 | - | ||
| 66 | -`planar_complex_matmul` 命令执行参数: | ||
| 67 | - | ||
| 68 | -```text | ||
| 69 | -m, n, k, [device_id], [--datapath DATA_PATH] | ||
| 70 | -``` | ||
| 71 | - | ||
| 72 | -Host 层除了常规的 GEMM shape,还需要根据m, n, k,动态选择路径: | ||
| 73 | - | ||
| 74 | -1. 选择执行路径(Four-Pass 或 Fused); | ||
| 75 | -2. 选择对 `A_imag` 还是 `B_imag` 取负(`NEGATE_A`); | ||
| 76 | -3. 选择 block swizzle 方向(`m >= n` 时行优先扫描,`m < n` 时列优先扫描)。 | ||
| 77 | - | ||
| 78 | -#### 4.1.2 路径选择 | ||
| 79 | - | ||
| 80 | -Host 侧基于 cost-model 选择 kernel 变体: | ||
| 81 | - | ||
| 82 | -```text | ||
| 83 | -K >= 6000 AND per_core >= 3 tiles -> Four-Pass | ||
| 84 | -否则 -> Fused | ||
| 85 | -``` | ||
| 86 | - | ||
| 87 | -- `coreLoops = CeilDiv(m, L1_TILE_M) * CeilDiv(n, L1_TILE_N)` | ||
| 88 | -- `perCore = coreLoops / aicCoreNum` | ||
| 89 | - | ||
| 90 | -Four-Pass 每 pass 只读写一路 C,适合 K 大、per-core tile 多的场景;Fused 单遍完成,适合 K 小或 per-core tile 少的场景。 | ||
| 91 | - | ||
| 92 | -#### 4.1.3 NEGATE_A 选择 | ||
| 93 | - | ||
| 94 | -`C_real = A_real * B_real - A_imag * B_imag` 中的负号项通过预先对 `A_imag` 或 `B_imag` 取负实现: | ||
| 95 | - | ||
| 96 | -- `m < n`:对 `A_imag` 取负(`NEGATE_A=true`),workspace 尺寸为 `M*K*half`; | ||
| 97 | -- `m >= n`:对 `B_imag` 取负(`NEGATE_A=false`),workspace 尺寸为 `K*N*half`。 | ||
| 98 | - | ||
| 99 | -选择较小的一侧可以减少 workspace 开销和 AIV 取负工作量。 | ||
| 100 | - | ||
| 101 | -#### 4.1.4 device memory 与 copy | ||
| 102 | - | ||
| 103 | -设备内存分配: | ||
| 104 | - | ||
| 105 | -```text | ||
| 106 | -deviceAReal, deviceAImag // 输入 A 实部/虚部 | ||
| 107 | -deviceBReal, deviceBImag // 输入 B 实部/虚部 | ||
| 108 | -deviceCReal, deviceCImag // 输出 C 实部/虚部 | ||
| 109 | -deviceWorkspace // AIV 取负后的 signed 工作区 | ||
| 110 | -``` | ||
| 111 | - | ||
| 112 | -Host 将 6 个输入指针 + 2 个输出指针 + workspace 传入 `DeviceGemm::Arguments`,由 kernel 层根据 `NEGATE_A` 决定 `ptrAImagSigned`/`ptrBImagSigned` 指向 workspace 还是原始指针。 | ||
| 113 | - | ||
| 114 | -### 4.2 Kernel 层 | ||
| 115 | - | ||
| 116 | -#### 4.2.1 统一 kernel 模板 | ||
| 117 | - | ||
| 118 | -```cpp | ||
| 119 | -Gemm::Kernel::PlanarComplexGemm< | ||
| 120 | - USE_FOUR_PASS, | ||
| 121 | - NEGATE_A, | ||
| 122 | - BlockMmadFourPass, | ||
| 123 | - BlockMmadFused, | ||
| 124 | - BlockScheduler> | ||
| 125 | -``` | ||
| 126 | - | ||
| 127 | -kernel 通过 `USE_FOUR_PASS` 编译期开关选择 block 类型,未选中的路径以 `void` 传入,不会被实例化。 | ||
| 128 | - | ||
| 129 | -#### 4.2.2 AIV 取负预处理 | ||
| 130 | - | ||
| 131 | -`NegateMatrixAiv` 是 kernel 内的 AIV 组件: | ||
| 132 | - | ||
| 133 | -- 输入:原始 `A_imag`(或 `B_imag`)GM tensor; | ||
| 134 | -- 输出:取负后的 signed GM workspace; | ||
| 135 | -- 单缓冲:src/dst 各占 UB 的一半,compute(Muls)相对 GM 带宽可忽略,双缓冲无收益。 | ||
| 136 | - | ||
| 137 | -AIV 路径在 Mix kernel prologue 阶段执行取负,AIC 路径通过 `ptrAImagSigned`/`ptrBImagSigned` 消费结果。 | ||
| 138 | - | ||
| 139 | -#### 4.2.3 Four-Pass 编排(`USE_FOUR_PASS=true`) | ||
| 140 | - | ||
| 141 | -4 次顺序 `BlockMmad` 调用,fixpipe atomic-add 把交叉项累加回 C: | ||
| 142 | - | ||
| 143 | -```text | ||
| 144 | -pass1: C_real = A_real * B_real (无 atomic) | ||
| 145 | -pass2: C_real += signed_imag_cross_term (atomic add) | ||
| 146 | -pass3: C_imag = A_imag * B_real (无 atomic) | ||
| 147 | -pass4: C_imag += A_real * B_imag (atomic add) | ||
| 148 | -``` | ||
| 149 | - | ||
| 150 | -其中 pass 2 的 `signed_imag_cross_term` 为: | ||
| 151 | - | ||
| 152 | -\- `NEGATE_A = true`:`A_imag_signed * B_imag` | ||
| 153 | - | ||
| 154 | -\- `NEGATE_A = false`:`A_imag * B_imag_signed` | ||
| 155 | - | ||
| 156 | -#### 4.2.4 Fused 编排(`USE_FOUR_PASS=false`) | ||
| 157 | - | ||
| 158 | -单遍 K-loop,C_real 与 C_imag 分时复用同一块 L0C: | ||
| 159 | - | ||
| 160 | -```text | ||
| 161 | -Stage 1 (C_real): 2K 个子迭代 | ||
| 162 | - even sub: A_real * B_real -> l0C (initC) | ||
| 163 | - odd sub: AImagSigned * BImagSigned -> l0C (accumulate) | ||
| 164 | - ... FixPipe l0C -> GM_C_real | ||
| 165 | - | ||
| 166 | -Stage 2 (C_imag): 2K 个子迭代 | ||
| 167 | - even sub: A_imag * B_real -> l0C (initC) | ||
| 168 | - odd sub: A_real * B_imag -> l0C (accumulate) | ||
| 169 | - ... FixPipe l0C -> GM_C_imag | ||
| 170 | -``` | ||
| 171 | - | ||
| 172 | -C_real 的 FixPipe 与 C_imag 首个子迭代的 MTE2(GM->L1)重叠,隐藏 fixpipe 延迟。 | ||
| 173 | - | ||
| 174 | -### 4.3 Block 层 | ||
| 175 | - | ||
| 176 | -#### 4.3.1 Four-Pass block | ||
| 177 | - | ||
| 178 | -Four-Pass 复用通用 BlockMmadTla。kernel 层负责 4 次调用的编排和 atomic-add。 | ||
| 179 | - | ||
| 180 | -#### 4.3.2 Fused block | ||
| 181 | - | ||
| 182 | -Fused 使用 `BlockMmadTla` 针对 `MmadPlanarComplexFused` policy 的偏特化(`block_mmad_planar_complex_fused_tla.hpp`): | ||
| 183 | - | ||
| 184 | -1. **4 路输入 tensor**:`A_real`、(`A_imag` or `A_imag_signed`)、`B_real`、(`B_imag` or `B_imag_signed`)。block 对 `NEGATE_A` 无感知,始终从 Signed 槽位读 C_real 交叉项,从原始槽位读 C_imag。 | ||
| 185 | -2. **L1 4 槽 K-pingpong**:`[A_K0 | A_K1 | B_K0 | B_K1]`,A/B 槽位通用,GM 来源按子迭代交替。 | ||
| 186 | -3. **L0A/L0B 双缓冲 pingpong**:重叠 L1->L0 搬运与 Cube MMAD。 | ||
| 187 | -4. **L0C 单缓冲**:C_real FixPipe 完成后 C_imag 才开始,分时复用。 | ||
| 188 | -5. **K-shuffle**:`ENABLE_SHUFFLE_K=true` 时按 `GetBlockIdx()` 偏移 K tile 顺序,分散 L2 访问热点。 | ||
| 189 | - | ||
| 190 | -### 4.4 DispatchPolicy 设计 | ||
| 191 | - | ||
| 192 | -#### 4.4.1 Fused: `MmadPlanarComplexFused` | ||
| 193 | - | ||
| 194 | -模板参数: | ||
| 195 | - | ||
| 196 | -| 参数 | 说明 | 当前样例取值 | | ||
| 197 | -|---|---|---| | ||
| 198 | -| `ArchTag` | 目标架构 | `Arch::AtlasA2` | | ||
| 199 | -| `ENABLE_SHUFFLE_K` | 是否启用 K 维 shuffle | `true` | | ||
| 200 | - | ||
| 201 | -## 5. 空间分配 | ||
| 202 | - | ||
| 203 | -### 5.1 Tile Shape 设计 | ||
| 204 | - | ||
| 205 | -```cpp | ||
| 206 | -using L1TileShape = GemmShape<128, 256, 256>; // M, N, K | ||
| 207 | -using L0TileShape = GemmShape<128, 256, 64>; // M, N, K | ||
| 208 | -``` | ||
| 209 | - | ||
| 210 | -### 5.2 存储空间计算 | ||
| 211 | - | ||
| 212 | -数据类型:`ElementA = ElementB = half`(2 字节),`ElementAccumulator = float`(4 字节),pingpong `STAGES = 2`。硬件 buffer 容量:`L1 = 512 KB`、`L0A = 64 KB`、`L0B = 64 KB`、`L0C = 128 KB`。 | ||
| 213 | - | ||
| 214 | -**Fused 路径 `<128, 256, 256> / <128, 256, 64>`:** | ||
| 215 | - | ||
| 216 | -| Buffer | 单级尺寸 | 计算 | 单级容量 | ×2 stages | 硬件上限 | 利用率 | | ||
| 217 | -|---|---|---|---|---|---|---| | ||
| 218 | -| L1A | 128 × 256 | × 2B | 64 KB | 128 KB | - | - | | ||
| 219 | -| L1B | 256 × 256 | × 2B | 128 KB | 256 KB | - | - | | ||
| 220 | -| **L1 合计** | - | - | 192 KB | **384 KB** | 512 KB | **75%** | | ||
| 221 | -| L0A | 128 × 64 | × 2B | 16 KB | **32 KB** | 64 KB | **50%** | | ||
| 222 | -| L0B | 64 × 256 | × 2B | 32 KB | **64 KB** | 64 KB | **100%** | | ||
| 223 | -| L0C | 128 × 256 | × 4B | 128 KB | - (单缓冲) | 128 KB | **100%** | | ||
| 224 | - | ||
| 225 | -L0C 单缓冲(C_real/C_imag 分时复用)打满 128 KB;L0B 双缓冲打满 64 KB;L1 合计 384/512 KB(75%)。L0A 利用率 50%,留出余量给对角 tile 等特殊处理。 | ||
| 226 | - | ||
| 227 | -**GM workspace(AIV 取负):** | ||
| 228 | - | ||
| 229 | -- `NEGATE_A=true`(`m < n`):`M × K × 2B`(对 `A_imag` 取负) | ||
| 230 | -- `NEGATE_A=false`(`m >= n`):`K × N × 2B`(对 `B_imag` 取负) | ||
| 231 | - | ||
| 232 | -Host 选择较小的一侧取负以减少 workspace 开销。 | ||
| 233 | - | ||
| 234 | -## 6. 代码组织 | ||
| 235 | - | ||
| 236 | -``` | ||
| 237 | -├── experimental | ||
| 238 | -│ └── matmul | ||
| 239 | -│ └── planar_complex_matmul | ||
| 240 | -│ ├── CMakeLists.txt # CMake 编译文件 | ||
| 241 | -│ ├── README.md # 本文档 | ||
| 242 | -│ ├── gen_data_compare.py # NumPy golden 数据生成与精度比对脚本 | ||
| 243 | -│ └── planar_complex_matmul.cpp # 样例主文件(Host 层入口) | ||
| 244 | -└── include | ||
| 245 | - └── catlass | ||
| 246 | - ├── gemm | ||
| 247 | - │ ├── block | ||
| 248 | - │ │ └── block_mmad_planar_complex_fused_tla.hpp | ||
| 249 | - │ │ # BlockMmadTla 针对 MmadPlanarComplexFused 的偏特化 | ||
| 250 | - │ │ # 实现 Fused 路径的双 stage K-loop 与 L0C 分时复用 | ||
| 251 | - │ ├── device | ||
| 252 | - │ │ └── (复用主仓库 device_gemm.hpp) | ||
| 253 | - │ │ # Device 层薄封装:参数透传 + KernelAdapter 启动 | ||
| 254 | - │ └── kernel | ||
| 255 | - │ └── planar_complex_gemm_tla.hpp | ||
| 256 | - │ # PlanarComplexGemm 统一 kernel(Four-Pass / Fused 编排) | ||
| 257 | - │ # 含 NegateMatrixAiv AIV 取负预处理组件 | ||
| 258 | - └── dispatch_policy.hpp | ||
| 259 | - # MmadPlanarComplexFused policy(仅 ENABLE_SHUFFLE_K 参数) | ||
| 260 | -``` | ||
| 261 | - | ||
| 262 | -## 7. 使用示例 | ||
| 263 | - | ||
| 264 | -1. 编译样例代码,并生成相应的算子可执行文件。 | ||
| 265 | - | ||
| 266 | -``` | ||
| 267 | -bash scripts/build.sh planar_complex_matmul | ||
| 268 | -``` | ||
| 269 | - | ||
| 270 | -2. 切换到可执行文件的编译目录 `output/bin` 下,执行算子样例程序。该方式随机生成输入数据,只输出 kernel 调度路径与平均耗时,不做精度比对。 | ||
| 271 | - | ||
| 272 | -``` | ||
| 273 | -cd output/bin | ||
| 274 | -./planar_complex_matmul 256 512 1024 0 | ||
| 275 | -``` | ||
| 276 | - | ||
| 277 | -• 256:矩阵 m 轴 | ||
| 278 | - | ||
| 279 | -• 512:矩阵 n 轴 | ||
| 280 | - | ||
| 281 | -• 1024:矩阵 k 轴 | ||
| 282 | - | ||
| 283 | -• 0:Device ID,可选,默认为 0 | ||
| 284 | - | ||
| 285 | -执行结果中包含如下信息,说明样例执行成功。 | ||
| 286 | - | ||
| 287 | -``` | ||
| 288 | -PlanarComplexGemm dispatch: M=256 N=512 K=1024 ... -> Fused ... | ||
| 289 | -PlanarComplexGemm: M=256 N=512 K=1024 variant=Fused gemm=... ms (20 iters) | ||
| 290 | -No --datapath provided, skipping validation. | ||
| 291 | -``` | ||
| 292 | - | ||
| 293 | -3. 使用 `gen_data_compare.py` 生成输入数据、运行 NPU 可执行文件并与 NumPy golden 结果进行比对。脚本默认从仓库根目录自动定位 `output/bin/planar_complex_matmul`,默认在当前目录下生成 `data`、`golden` 目录并在结束后删除;如需指定保存路径可使用 `--save_path`,如需保留可指定 `--clean false`。 | ||
| 294 | - | ||
| 295 | -``` | ||
| 296 | -python examples/planar_complex_matmul/gen_data_compare.py 256 512 1024 | ||
| 297 | -``` | ||
| 298 | - | ||
| 299 | -执行结果如下,说明精度比对成功。 | ||
| 300 | - | ||
| 301 | -``` | ||
| 302 | -Data generated: M=256, N=512, K=1024, BLAS threads=8 | ||
| 303 | -------计算npu------ | ||
| 304 | ------- 计算相对误差 ----- | ||
| 305 | -Precision metric: ... | ||
| 306 | ------- 开始比较 ------ | ||
| 307 | -比较结果:Compare success | ||
| 308 | -``` | ||
| @@ -1,3 +1,11 @@ | |||
| 1 | +# This program is free software, you can redistribute it and/or modify. | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This file is a part of the CANN Open Software. | ||
| 4 | +# Licensed under CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. See LICENSE in the root of | ||
| 8 | +# the software repository for the full text of the License. | ||
| 1 | import re | 9 | import re |
| 2 | 10 | ||
| 3 | import pytest | 11 | import pytest |
| @@ -1,3 +1,14 @@ | |||
| 1 | +# This program is free software, you can redistribute it and/or modify. | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This file is a part of the CANN Open Software. | ||
| 4 | +# Licensed under CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# Please refer to the License for details. You may not use this file except in compliance | ||
| 6 | +# with the License. | ||
| 7 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS | ||
| 8 | +# OR IMPLIED, INCLUDING | ||
| 9 | +# BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | + | ||
| 1 | import pytest | 12 | import pytest |
| 2 | import torch | 13 | import torch |
| 3 | import torch_npu | 14 | import torch_npu |
Rexperimental/matmul/planar_complex_matmul/test_77_planar_complex_matmul.py→tests/optest/tests/test_77_planar_complex_matmul.py+11-0
| @@ -1,3 +1,14 @@ | |||
| 1 | +# This program is free software, you can redistribute it and/or modify. | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This file is a part of the CANN Open Software. | ||
| 4 | +# Licensed under CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# Please refer to the License for details. You may not use this file except in compliance | ||
| 6 | +# with the License. | ||
| 7 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS | ||
| 8 | +# OR IMPLIED, INCLUDING | ||
| 9 | +# BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | + | ||
| 1 | import pytest | 12 | import pytest |
| 2 | import torch | 13 | import torch |
| 3 | import torch_catlass | 14 | import torch_catlass |
Rexperimental/gmm/grouped_matmul_slice_m_gelu/test_80_grouped_matmul_slice_m_gelu.py→tests/optest/tests/test_80_grouped_matmul_slice_m_gelu.py+9-8
| @@ -1,3 +1,12 @@ | |||
| 1 | +# This program is free software, you can redistribute it and/or modify. | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This file is a part of the CANN Open Software. | ||
| 4 | +# Licensed under CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. See LICENSE in the root of | ||
| 8 | +# the software repository for the full text of the License. | ||
| 9 | + | ||
| 1 | import re | 10 | import re |
| 2 | 11 | ||
| 3 | import pytest | 12 | import pytest |
| @@ -14,11 +23,9 @@ M_TOTAL = sum(GROUP_SIZES) | |||
| 14 | N = 256 | 23 | N = 256 |
| 15 | K = 256 | 24 | K = 256 |
| 16 | 25 | ||
| 17 | - | ||
| 18 | def _prefix_sum_group_list(group_sizes: tuple[int, ...]) -> torch.Tensor: | 26 | def _prefix_sum_group_list(group_sizes: tuple[int, ...]) -> torch.Tensor: |
| 19 | return torch.tensor(group_sizes, dtype=torch.int64).cumsum(0).npu() | 27 | return torch.tensor(group_sizes, dtype=torch.int64).cumsum(0).npu() |
| 20 | 28 | ||
| 21 | - | ||
| 22 | def _grouped_matmul_reference( | 29 | def _grouped_matmul_reference( |
| 23 | a: torch.Tensor, b: torch.Tensor, group_sizes: tuple[int, ...] | 30 | a: torch.Tensor, b: torch.Tensor, group_sizes: tuple[int, ...] |
| 24 | ) -> torch.Tensor: | 31 | ) -> torch.Tensor: |
| @@ -30,7 +37,6 @@ def _grouped_matmul_reference( | |||
| 30 | offset = end | 37 | offset = end |
| 31 | return torch.cat(expected, dim=0) | 38 | return torch.cat(expected, dim=0) |
| 32 | 39 | ||
| 33 | - | ||
| 34 | def group_matmul_reference(tensor_a, tensor_b, m_cumsum_list): | 40 | def group_matmul_reference(tensor_a, tensor_b, m_cumsum_list): |
| 35 | if m_cumsum_list is None: | 41 | if m_cumsum_list is None: |
| 36 | return None | 42 | return None |
| @@ -48,12 +54,10 @@ def group_matmul_reference(tensor_a, tensor_b, m_cumsum_list): | |||
| 48 | ab[m_start:m_end, :] = ab_in_group | 54 | ab[m_start:m_end, :] = ab_in_group |
| 49 | return ab | 55 | return ab |
| 50 | 56 | ||
| 51 | - | ||
| 52 | def gelu_reference_origin(ab): | 57 | def gelu_reference_origin(ab): |
| 53 | sqrt_2_over_pi = math.sqrt(2.0 / math.pi) | 58 | sqrt_2_over_pi = math.sqrt(2.0 / math.pi) |
| 54 | return 0.5 * ab * (1.0 + torch.tanh(sqrt_2_over_pi * (ab + 0.044715 * torch.pow(ab, 3)))) | 59 | return 0.5 * ab * (1.0 + torch.tanh(sqrt_2_over_pi * (ab + 0.044715 * torch.pow(ab, 3)))) |
| 55 | 60 | ||
| 56 | - | ||
| 57 | def gelu_reference_sigmod(x: torch.Tensor) -> torch.Tensor: | 61 | def gelu_reference_sigmod(x: torch.Tensor) -> torch.Tensor: |
| 58 | # 近似公式实现 | 62 | # 近似公式实现 |
| 59 | term = x + 0.044715 * torch.pow(x, 3) | 63 | term = x + 0.044715 * torch.pow(x, 3) |
| @@ -61,7 +65,6 @@ def gelu_reference_sigmod(x: torch.Tensor) -> torch.Tensor: | |||
| 61 | denominator = 1 + torch.exp(exponent) | 65 | denominator = 1 + torch.exp(exponent) |
| 62 | return x / denominator | 66 | return x / denominator |
| 63 | 67 | ||
| 64 | - | ||
| 65 | def _grouped_matmul_slice_m_gelu_reference(tensor_a, tensor_b, m_cumsum_list, gelu_flag=0): | 68 | def _grouped_matmul_slice_m_gelu_reference(tensor_a, tensor_b, m_cumsum_list, gelu_flag=0): |
| 66 | ab = group_matmul_reference(tensor_a, tensor_b, m_cumsum_list) | 69 | ab = group_matmul_reference(tensor_a, tensor_b, m_cumsum_list) |
| 67 | if gelu_flag == 0: | 70 | if gelu_flag == 0: |
| @@ -70,7 +73,6 @@ def _grouped_matmul_slice_m_gelu_reference(tensor_a, tensor_b, m_cumsum_list, ge | |||
| 70 | gelu_out = gelu_reference_sigmod(ab) | 73 | gelu_out = gelu_reference_sigmod(ab) |
| 71 | return ab, gelu_out | 74 | return ab, gelu_out |
| 72 | 75 | ||
| 73 | - | ||
| 74 | 76 | ||
| 75 | def test_grouped_matmul_slice_gelu(): | 77 | def test_grouped_matmul_slice_gelu(): |
| 76 | a = torch.randn(M_TOTAL, K, dtype=torch.float16) | 78 | a = torch.randn(M_TOTAL, K, dtype=torch.float16) |
| @@ -92,6 +94,5 @@ def test_grouped_matmul_slice_gelu(): | |||
| 92 | f"max diff = {(result.cpu().float() - expected.cpu()).abs().max().item()}" | 94 | f"max diff = {(result.cpu().float() - expected.cpu()).abs().max().item()}" |
| 93 | ) | 95 | ) |
| 94 | 96 | ||
| 95 | - | ||
| 96 | if __name__ == "__main__": | 97 | if __name__ == "__main__": |
| 97 | pytest.main([__file__, "-v", "-s"]) | 98 | pytest.main([__file__, "-v", "-s"]) |
| @@ -508,6 +508,35 @@ class CatlassExampleTest(unittest.TestCase): | |||
| 508 | "74_ascend950_weight_quant_a8w4_grouped_mx_matmul", case_cpp | 508 | "74_ascend950_weight_quant_a8w4_grouped_mx_matmul", case_cpp |
| 509 | ) | 509 | ) |
| 510 | 510 | ||
| 511 | + | ||
| 512 | + def test_80_ascned950_grouped_matmul_slice_m_gelu(self): | ||
| 513 | + case_py = [ | ||
| 514 | + "4", # group_num | ||
| 515 | + "2048", # m | ||
| 516 | + "256", # n | ||
| 517 | + "256", # k | ||
| 518 | + "0", # device_id | ||
| 519 | + ] | ||
| 520 | + ret = subprocess.run( | ||
| 521 | + [ | ||
| 522 | + "python3", | ||
| 523 | + os.path.join( | ||
| 524 | + CMAKE_EXAMPLES_PATH, | ||
| 525 | + "80_ascend950_grouped_matmul_slice_m_gelu", | ||
| 526 | + "gen_data.py", | ||
| 527 | + ), | ||
| 528 | + ] | ||
| 529 | + + case_py, | ||
| 530 | + stdout=subprocess.PIPE, | ||
| 531 | + stderr=subprocess.PIPE, | ||
| 532 | + ) | ||
| 533 | + self._ret_check(ret) | ||
| 534 | + | ||
| 535 | + case_cpp = ["4", "2048", "256", "256", "0"] | ||
| 536 | + self.run_case( | ||
| 537 | + "80_ascend950_grouped_matmul_slice_m_gelu", case_cpp | ||
| 538 | + ) | ||
| 539 | + | ||
| 511 | normal_cases_2201 = [ | 540 | normal_cases_2201 = [ |
| 512 | "00_basic_matmul 256 512 1024 0", | 541 | "00_basic_matmul 256 512 1024 0", |
| 513 | "01_batched_matmul 5 256 512 1024 0", | 542 | "01_batched_matmul 5 256 512 1024 0", |
| @@ -544,6 +573,9 @@ normal_cases_2201 = [ | |||
| 544 | "44_quant_matmul_full_loadA_tla 256 512 1024 0", | 573 | "44_quant_matmul_full_loadA_tla 256 512 1024 0", |
| 545 | "45_strided_batched_matmul_tla 5 256 512 1024 0", | 574 | "45_strided_batched_matmul_tla 5 256 512 1024 0", |
| 546 | "52_quant_multi_core_splitk_matmul_tla 256 512 1024 0", | 575 | "52_quant_multi_core_splitk_matmul_tla 256 512 1024 0", |
| 576 | + "75_symm 256 512 256 0 0 0", | ||
| 577 | + "76_trmm 512 256 0 0 0 0 1.0 0", | ||
| 578 | + "77_planar_complex_matmul 256 512 1024 0", | ||
| 547 | "102_dynamic_optimized_matmul 256 512 1024 0 0 0" | 579 | "102_dynamic_optimized_matmul 256 512 1024 0 0 0" |
| 548 | "103_dynamic_optimized_quant_matmul_per_token_basic 256 512 1024 0 0 0", | 580 | "103_dynamic_optimized_quant_matmul_per_token_basic 256 512 1024 0 0 0", |
| 549 | ] | 581 | ] |
🟠 High Priority
建议:在 EXAMPLE_ATLASA2 列表中补充 77_planar_complex_matmul,使其被 add_subdirectory 构建。