Merged
【feature】 matmul_emu_split_weight add A2/A3 support #9237
zhengyuhao3Add on Aug 26
【feature】 matmul_emu_split_weight add A2/A3 support #9237
Merged
Merged fromzhengyuhao3/ops-nnmaster-split_wintocann/ops-nnmaster
Total: 38 files changed+6395-88
| @@ -35,7 +35,7 @@ | |||
| 35 | |[grouped_dynamic_mx_quant_with_dual_axis](../../quant/grouped_dynamic_mx_quant_with_dual_axis/docs/torchapi_grouped_dynamic_mx_quant_with_dual_axis.md)|根据`group_index`描述的行分组,对二维输入`x`同时沿最后一维和倒数第二维进行动态MX量化,输出两个方向的FP8量化结果及对应的FLOAT8_E8M0缩放因子。|-|默认确定性实现| | 35 | |[grouped_dynamic_mx_quant_with_dual_axis](../../quant/grouped_dynamic_mx_quant_with_dual_axis/docs/torchapi_grouped_dynamic_mx_quant_with_dual_axis.md)|根据`group_index`描述的行分组,对二维输入`x`同时沿最后一维和倒数第二维进行动态MX量化,输出两个方向的FP8量化结果及对应的FLOAT8_E8M0缩放因子。|-|默认确定性实现| |
| 36 | |[quant_matmul_activation_quant](../../matmul/quant_matmul_activation_quant/docs/torchapi_quant_matmul_activation_quant.md)| 融合量化的矩阵乘、激活以及动态量化计算,weight仅支持NZ格式。 |-|默认支持确定性计算| | 36 | |[quant_matmul_activation_quant](../../matmul/quant_matmul_activation_quant/docs/torchapi_quant_matmul_activation_quant.md)| 融合量化的矩阵乘、激活以及动态量化计算,weight仅支持NZ格式。 |-|默认支持确定性计算| |
| 37 | |[flat_quant](../../matmul/flat_quant/docs/torchapi_flat_quant.md)| 为矩阵x依次进行两次克罗内克积小矩阵乘法,然后针对矩阵乘的结果进行量化处理。 |默认支持确定性计算|默认支持确定性计算| | 37 | |[flat_quant](../../matmul/flat_quant/docs/torchapi_flat_quant.md)| 为矩阵x依次进行两次克罗内克积小矩阵乘法,然后针对矩阵乘的结果进行量化处理。 |默认支持确定性计算|默认支持确定性计算| |
| 38 | -|[matmul_emu_split_weight](../../matmul/matmul_emu_split_weight/docs/torchapi_matmul_emu_split_weight.md)| 使用双路BF16 GEMM融合模拟FP32精度矩阵乘法。离线将FP32权重拆分为高位BF16与低位残差BF16,推理阶段执行两次BF16 GEMM并做线性组合,激活值全程保持BF16,两路矩阵乘均运行在Cube上。 |-|默认支持确定性计算| | 38 | +|[matmul_emu_split_weight](../../matmul/matmul_emu_split_weight/docs/torchapi_matmul_emu_split_weight.md)| 使用双路BF16 GEMM融合模拟FP32精度矩阵乘法。离线将FP32权重拆分为高位BF16与低位残差BF16,推理阶段执行两次BF16 GEMM并做线性组合,激活值全程保持BF16,两路矩阵乘均运行在Cube上。 |默认支持确定性计算|默认支持确定性计算| |
| 39 | |[cross_entropy_sum_exp_and_index_logit](../../loss/cross_entropy_sum_exp_and_index_logit/docs/torchapi_cross_entropy_sum_exp_and_index_logit.md)| 面向vocab并行(Tensor Parallel)场景的CrossEntropy本地计算融合算子。|-| 默认确定性实现 | | 39 | |[cross_entropy_sum_exp_and_index_logit](../../loss/cross_entropy_sum_exp_and_index_logit/docs/torchapi_cross_entropy_sum_exp_and_index_logit.md)| 面向vocab并行(Tensor Parallel)场景的CrossEntropy本地计算融合算子。|-| 默认确定性实现 | |
| 40 | |[mx_to_block_mx_quant](../../quant/mx_to_block_mx_quant/docs/torchapi_mx_to_block_mx_quant.md)| 将调用 npu_dynamic_mx_quant 量化得到的 FLOAT4 的 Tensor 结合 FLOAT8_E8M0 缩放系数,转换为 FLOAT8 分块量化格式,同时输出 -1 轴和 -2 轴方向的量化尺度。|-| 默认确定性实现 | | 40 | |[mx_to_block_mx_quant](../../quant/mx_to_block_mx_quant/docs/torchapi_mx_to_block_mx_quant.md)| 将调用 npu_dynamic_mx_quant 量化得到的 FLOAT4 的 Tensor 结合 FLOAT8_E8M0 缩放系数,转换为 FLOAT8 分块量化格式,同时输出 -1 轴和 -2 轴方向的量化尺度。|-| 默认确定性实现 | |
| 41 | |[dequant_situ_quant](../../quant/dequant_situ_quant/docs/torchapi_dequant_situ_quant.md)|在Situ激活函数前后添加dequant和quant操作,实现x的DequantSituQuant计算。|默认支持确定性计算|-| | 41 | |[dequant_situ_quant](../../quant/dequant_situ_quant/docs/torchapi_dequant_situ_quant.md)|在Situ激活函数前后添加dequant和quant操作,实现x的DequantSituQuant计算。|默认支持确定性计算|-| |
| @@ -13,6 +13,8 @@ add_all_modules_sources( | |||
| 13 | ) | 13 | ) |
| 14 | 14 | ||
| 15 | add_kernel_sources( | 15 | add_kernel_sources( |
| 16 | + KERNEL_SRC arch22/matmul_emu_split_weight.cpp | ||
| 17 | + COMPUTE_UNITS ascend910b ascend910_93 | ||
| 16 | SIMPLIFIED_KEY None | 18 | SIMPLIFIED_KEY None |
| 17 | AUTO_SYNC false | 19 | AUTO_SYNC false |
| 18 | ) | 20 | ) |
| @@ -20,5 +22,6 @@ add_kernel_sources( | |||
| 20 | add_kernel_sources( | 22 | add_kernel_sources( |
| 21 | KERNEL_SRC arch35/matmul_emu_split_weight.cpp | 23 | KERNEL_SRC arch35/matmul_emu_split_weight.cpp |
| 22 | COMPUTE_UNITS ascend950 | 24 | COMPUTE_UNITS ascend950 |
| 25 | + SIMPLIFIED_KEY None | ||
| 23 | AUTO_SYNC false | 26 | AUTO_SYNC false |
| 24 | ) | 27 | ) |
| @@ -5,8 +5,8 @@ | |||
| 5 | | 产品 | 是否支持 | | 5 | | 产品 | 是否支持 | |
| 6 | | :------------------------------------------------------- | :------: | | 6 | | :------------------------------------------------------- | :------: | |
| 7 | | <term>Ascend 950PR/Ascend 950DT</term> | √ | | 7 | | <term>Ascend 950PR/Ascend 950DT</term> | √ | |
| 8 | -| <term>Atlas A3 训练系列产品/Atlas A3 推理系列产品</term> | × | | 8 | +| <term>Atlas A3 训练系列产品/Atlas A3 推理系列产品</term> | √ | |
| 9 | -| <term>Atlas A2 训练系列产品/Atlas A2 推理系列产品</term> | × | | 9 | +| <term>Atlas A2 训练系列产品/Atlas A2 推理系列产品</term> | √ | |
| 10 | | <term>Atlas 200I/500 A2 推理产品</term> | × | | 10 | | <term>Atlas 200I/500 A2 推理产品</term> | × | |
| 11 | | <term>Atlas 推理系列产品</term> | × | | 11 | | <term>Atlas 推理系列产品</term> | × | |
| 12 | | <term>Atlas 训练系列产品</term> | × | | 12 | | <term>Atlas 训练系列产品</term> | × | |
| @@ -119,26 +119,16 @@ | |||
| 119 | 119 | ||
| 120 | ## 调用说明 | 120 | ## 调用说明 |
| 121 | 121 | ||
| 122 | -<table style="undefined;table-layout: fixed; width: 900px"><colgroup> | 122 | +- <term>Atlas A2 训练系列产品/Atlas A2 推理系列产品、Atlas A3 训练系列产品/Atlas A3 推理系列产品</term>: |
| 123 | - <col style="width: 170px"> | 123 | + |
| 124 | - <col style="width: 300px"> | 124 | + | 调用方式 | 样例代码 | 说明 | |
| 125 | - <col style="width: 430px"> | 125 | + | ---------------- | --------------------------- | --------------------------------------------------- | |
| 126 | - </colgroup> | 126 | + | aclnn接口 | [test_aclnn_matmul_emu_split_weight](examples/test_aclnn_matmul_emu_split_weight.cpp) | 通过<br>[aclnnMatmulEmuSplitWeight](docs/aclnnMatmulEmuSplitWeight.md)<br>接口调用MatmulEmuSplitWeight算子。 | |
| 127 | - <thead> | 127 | + | torch接口 | [matmul_emu_split_weight](torch_extension/matmul_emu_split_weight.py) | 通过<br>[torchapi](docs/torchapi_matmul_emu_split_weight.md)<br>接口调用MatmulEmuSplitWeight算子。 | |
| 128 | - <tr> | 128 | + |
| 129 | - <th>调用方式</th> | 129 | +- <term>Ascend 950PR/Ascend 950DT</term>: |
| 130 | - <th>样例代码</th> | 130 | + |
| 131 | - <th>说明</th> | 131 | + | 调用方式 | 样例代码 | 说明 | |
| 132 | - </tr></thead> | 132 | + | ---------------- | --------------------------- | --------------------------------------------------- | |
| 133 | - <tbody> | 133 | + | aclnn接口 | [test_aclnn_matmul_emu_split_weight](examples/arch35/test_aclnn_matmul_emu_split_weight.cpp) | 通过<br>[aclnnMatmulEmuSplitWeight](docs/aclnnMatmulEmuSplitWeight.md)<br>接口调用MatmulEmuSplitWeight算子。 | |
| 134 | - <tr> | 134 | + | torch接口 | [matmul_emu_split_weight](torch_extension/matmul_emu_split_weight.py) | 通过<br>[torchapi](docs/torchapi_matmul_emu_split_weight.md)<br>接口调用MatmulEmuSplitWeight算子。 | |
| 135 | - <td>aclnn接口</td> | ||
| 136 | - <td><a href="examples/arch35/test_aclnn_matmul_emu_split_weight.cpp">test_aclnn_matmul_emu_split_weight</a></td> | ||
| 137 | - <td>通过<a href="docs/aclnnMatmulEmuSplitWeight.md">aclnnMatmulEmuSplitWeight</a>接口方式调用MatmulEmuSplitWeight算子</td> | ||
| 138 | - </tr> | ||
| 139 | - <tr> | ||
| 140 | - <td>torch接口</td> | ||
| 141 | - <td><a href="torch_extension/matmul_emu_split_weight.py">matmul_emu_split_weight</a></td> | ||
| 142 | - <td>通过<a href="docs/torchapi_matmul_emu_split_weight.md">torchapi</a>接口方式调用MatmulEmuSplitWeight算子</td> | ||
| 143 | - </tr> | ||
| 144 | - </tbody></table> | ||
| @@ -6,10 +6,10 @@ | |||
| 6 | - <term>Ascend 950PR/Ascend 950DT</term>:支持 | 6 | - <term>Ascend 950PR/Ascend 950DT</term>:支持 |
| 7 | <!-- end id1 --> | 7 | <!-- end id1 --> |
| 8 | <!-- npu="A3" id2 --> | 8 | <!-- npu="A3" id2 --> |
| 9 | -- <term>Atlas A3 训练系列产品/Atlas A3 推理系列产品</term>:不支持 | 9 | +- <term>Atlas A3 训练系列产品/Atlas A3 推理系列产品</term>:支持 |
| 10 | <!-- end id2 --> | 10 | <!-- end id2 --> |
| 11 | <!-- npu="910b" id3 --> | 11 | <!-- npu="910b" id3 --> |
| 12 | -- <term>Atlas A2 训练系列产品/Atlas A2 推理系列产品</term>:不支持 | 12 | +- <term>Atlas A2 训练系列产品/Atlas A2 推理系列产品</term>:支持 |
| 13 | <!-- end id3 --> | 13 | <!-- end id3 --> |
| 14 | <!-- npu="310b" id4 --> | 14 | <!-- npu="310b" id4 --> |
| 15 | - <term>Atlas 200I/500 A2 推理产品</term>:不支持 | 15 | - <term>Atlas 200I/500 A2 推理产品</term>:不支持 |
| @@ -265,6 +265,9 @@ aclnnStatus aclnnMatmulEmuSplitWeight( | |||
| 265 | - <term>Ascend 950PR/Ascend 950DT</term>:aclnnMatmulEmuSplitWeight默认确定性实现。 | 265 | - <term>Ascend 950PR/Ascend 950DT</term>:aclnnMatmulEmuSplitWeight默认确定性实现。 |
| 266 | 266 | ||
| 267 | <!-- end id7 --> | 267 | <!-- end id7 --> |
| 268 | + <!-- npu="A3,910b" id8 --> | ||
| 269 | + - <term>Atlas A2 训练系列产品/Atlas A2 推理系列产品</term>、<term>Atlas A3 训练系列产品/Atlas A3 推理系列产品</term>:aclnnMatmulEmuSplitWeight默认确定性实现。 | ||
| 270 | + <!-- end id8 --> | ||
| 268 | 271 | ||
| 269 | - **Shape约束**: | 272 | - **Shape约束**: |
| 270 | - M、K、N取值范围为(0, INT32_MAX]。 | 273 | - M、K、N取值范围为(0, INT32_MAX]。 |
| @@ -6,10 +6,10 @@ | |||
| 6 | - <term>Ascend 950PR/Ascend 950DT</term>:支持 | 6 | - <term>Ascend 950PR/Ascend 950DT</term>:支持 |
同步更新docs/zh/torch_api_list.md中A2/A3的确定性实现支持情况 ![]() ![]() | |||
| 7 | <!-- end id1 --> | 7 | <!-- end id1 --> |
| 8 | <!-- npu="A3" id2 --> | 8 | <!-- npu="A3" id2 --> |
| 9 | -- <term>Atlas A3 训练系列产品/Atlas A3 推理系列产品</term>:不支持 | 9 | +- <term>Atlas A3 训练系列产品/Atlas A3 推理系列产品</term>:支持 |
| 10 | <!-- end id2 --> | 10 | <!-- end id2 --> |
| 11 | <!-- npu="910b" id3 --> | 11 | <!-- npu="910b" id3 --> |
| 12 | -- <term>Atlas A2 训练系列产品/Atlas A2 推理系列产品</term>:不支持 | 12 | +- <term>Atlas A2 训练系列产品/Atlas A2 推理系列产品</term>:支持 |
| 13 | <!-- end id3 --> | 13 | <!-- end id3 --> |
| 14 | <!-- npu="310b" id4 --> | 14 | <!-- npu="310b" id4 --> |
| 15 | - <term>Atlas 200I/500 A2 推理产品</term>:不支持 | 15 | - <term>Atlas 200I/500 A2 推理产品</term>:不支持 |
| @@ -0,0 +1,195 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * 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. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | +#include <cmath> | ||
| 11 | +#include <cstring> | ||
| 12 | +#include <iostream> | ||
| 13 | +#include <memory> | ||
| 14 | +#include <vector> | ||
| 15 | +#include "acl/acl.h" | ||
| 16 | +#include "aclnnop/aclnn_matmul_emu_split_weight.h" | ||
| 17 | + | ||
| 18 | +#define CHECK_RET(cond, return_expr) \ | ||
| 19 | + do { \ | ||
| 20 | + if (!(cond)) { \ | ||
| 21 | + return_expr; \ | ||
| 22 | + } \ | ||
| 23 | + } while (0) | ||
| 24 | + | ||
| 25 | +#define CHECK_FREE_RET(cond, return_expr) \ | ||
| 26 | + do { \ | ||
| 27 | + if (!(cond)) { \ | ||
| 28 | + Finalize(deviceId, stream); \ | ||
| 29 | + return_expr; \ | ||
| 30 | + } \ | ||
| 31 | + } while (0) | ||
| 32 | + | ||
| 33 | +#define LOG_PRINT(message, ...) \ | ||
| 34 | + do { \ | ||
| 35 | + printf(message, ##__VA_ARGS__); \ | ||
| 36 | + } while (0) | ||
| 37 | + | ||
| 38 | +int64_t GetShapeSize(const std::vector<int64_t>& shape) | ||
| 39 | +{ | ||
| 40 | + int64_t shapeSize = 1; | ||
| 41 | + for (auto i : shape) { | ||
| 42 | + shapeSize *= i; | ||
| 43 | + } | ||
| 44 | + return shapeSize; | ||
| 45 | +} | ||
| 46 | + | ||
| 47 | +int Init(int32_t deviceId, aclrtStream* stream) | ||
| 48 | +{ | ||
| 49 | + auto ret = aclInit(nullptr); | ||
| 50 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); | ||
| 51 | + ret = aclrtSetDevice(deviceId); | ||
| 52 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret); | ||
| 53 | + ret = aclrtCreateStream(stream); | ||
| 54 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret); | ||
| 55 | + return 0; | ||
| 56 | +} | ||
| 57 | + | ||
| 58 | +void Finalize(int32_t deviceId, aclrtStream stream) | ||
| 59 | +{ | ||
| 60 | + aclrtDestroyStream(stream); | ||
| 61 | + aclrtResetDevice(deviceId); | ||
| 62 | + aclFinalize(); | ||
| 63 | +} | ||
| 64 | + | ||
| 65 | +// 将bfloat16的uint16_t表示转换为float表示 | ||
| 66 | +float Bf16ToFloat(uint16_t b) | ||
| 67 | +{ | ||
| 68 | + uint32_t bits = static_cast<uint32_t>(b) << 16; | ||
| 69 | + float result; | ||
| 70 | + std::memcpy(&result, &bits, sizeof(float)); | ||
| 71 | + return result; | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +// 将float转换为bfloat16的uint16_t表示 | ||
| 75 | +uint16_t FloatToBf16(float f) | ||
| 76 | +{ | ||
| 77 | + uint32_t bits; | ||
| 78 | + std::memcpy(&bits, &f, sizeof(uint32_t)); | ||
| 79 | + return static_cast<uint16_t>(bits >> 16); | ||
| 80 | +} | ||
| 81 | + | ||
| 82 | +template <typename T> | ||
| 83 | +int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, | ||
| 84 | + aclDataType dataType, aclTensor** tensor) | ||
| 85 | +{ | ||
| 86 | + auto size = GetShapeSize(shape) * sizeof(T); | ||
| 87 | + auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 88 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret); | ||
| 89 | + ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 90 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret); | ||
| 91 | + | ||
| 92 | + std::vector<int64_t> strides(shape.size(), 1); | ||
| 93 | + for (int64_t i = shape.size() - 2; i >= 0; i--) { | ||
| 94 | + strides[i] = shape[i + 1] * strides[i + 1]; | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, | ||
| 98 | + shape.data(), shape.size(), *deviceAddr); | ||
| 99 | + return 0; | ||
| 100 | +} | ||
| 101 | + | ||
| 102 | +int AclnnMatmulEmuSplitWeightTest(int32_t deviceId, aclrtStream& stream) | ||
| 103 | +{ | ||
| 104 | + auto ret = Init(deviceId, &stream); | ||
| 105 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); | ||
| 106 | + | ||
| 107 | + // 2. 构造输入与输出 | ||
| 108 | + // x: BF16 [128, 256], wHigh/wLow: BF16 [256, 128], y: FP32 [128, 128] | ||
| 109 | + std::vector<int64_t> xShape = {128, 256}; | ||
| 110 | + std::vector<int64_t> wShape = {256, 128}; | ||
| 111 | + std::vector<int64_t> yShape = {128, 128}; | ||
| 112 | + void* xDeviceAddr = nullptr; | ||
| 113 | + void* wHighDeviceAddr = nullptr; | ||
| 114 | + void* wLowDeviceAddr = nullptr; | ||
| 115 | + void* yDeviceAddr = nullptr; | ||
| 116 | + aclTensor* x = nullptr; | ||
| 117 | + aclTensor* wHigh = nullptr; | ||
| 118 | + aclTensor* wLow = nullptr; | ||
| 119 | + aclTensor* y = nullptr; | ||
| 120 | + | ||
| 121 | + // 构造BF16数据(用uint16_t存储) | ||
| 122 | + std::vector<uint16_t> xHostData(128 * 256, FloatToBf16(1.0f)); | ||
| 123 | + std::vector<uint16_t> wHighHostData(256 * 128, FloatToBf16(1.0f)); | ||
| 124 | + std::vector<uint16_t> wLowHostData(256 * 128, FloatToBf16(0.0f)); | ||
| 125 | + std::vector<float> yHostData(128 * 128, 0.0f); | ||
| 126 | + | ||
| 127 | + ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_BF16, &x); | ||
| 128 | + std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> xTensorPtr(x, aclDestroyTensor); | ||
| 129 | + std::unique_ptr<void, aclError (*)(void*)> xDeviceAddrPtr(xDeviceAddr, aclrtFree); | ||
| 130 | + CHECK_RET(ret == ACL_SUCCESS, return ret); | ||
| 131 | + | ||
| 132 | + ret = CreateAclTensor(wHighHostData, wShape, &wHighDeviceAddr, aclDataType::ACL_BF16, &wHigh); | ||
| 133 | + std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> wHighTensorPtr(wHigh, aclDestroyTensor); | ||
| 134 | + std::unique_ptr<void, aclError (*)(void*)> wHighDeviceAddrPtr(wHighDeviceAddr, aclrtFree); | ||
| 135 | + CHECK_RET(ret == ACL_SUCCESS, return ret); | ||
| 136 | + | ||
| 137 | + ret = CreateAclTensor(wLowHostData, wShape, &wLowDeviceAddr, aclDataType::ACL_BF16, &wLow); | ||
| 138 | + std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> wLowTensorPtr(wLow, aclDestroyTensor); | ||
| 139 | + std::unique_ptr<void, aclError (*)(void*)> wLowDeviceAddrPtr(wLowDeviceAddr, aclrtFree); | ||
| 140 | + CHECK_RET(ret == ACL_SUCCESS, return ret); | ||
| 141 | + | ||
| 142 | + ret = CreateAclTensor(yHostData, yShape, &yDeviceAddr, aclDataType::ACL_FLOAT, &y); | ||
| 143 | + std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> yTensorPtr(y, aclDestroyTensor); | ||
| 144 | + std::unique_ptr<void, aclError (*)(void*)> yDeviceAddrPtr(yDeviceAddr, aclrtFree); | ||
| 145 | + CHECK_RET(ret == ACL_SUCCESS, return ret); | ||
| 146 | + | ||
| 147 | + // 3. 调用aclnnMatmulEmuSplitWeight | ||
| 148 | + // wLowScale: 缩放因子,仅支持1/256 (0.00390625) | ||
| 149 | + // yDtype: 输出数据类型,仅支持0 (FP32) | ||
| 150 | + float wLowScale = 0.00390625f; | ||
| 151 | + int8_t yDtype = 0; | ||
| 152 | + uint64_t workspaceSize = 0; | ||
| 153 | + aclOpExecutor* executor = nullptr; | ||
| 154 | + | ||
| 155 | + ret = aclnnMatmulEmuSplitWeightGetWorkspaceSize(x, wHigh, wLow, y, wLowScale, yDtype, &workspaceSize, &executor); | ||
| 156 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnMatmulEmuSplitWeightGetWorkspaceSize failed. ERROR: %d\n", ret); | ||
| 157 | + return ret); | ||
| 158 | + | ||
| 159 | + void* workspaceAddr = nullptr; | ||
| 160 | + std::unique_ptr<void, aclError (*)(void*)> workspaceAddrPtr(nullptr, aclrtFree); | ||
| 161 | + if (workspaceSize > 0) { | ||
| 162 | + ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 163 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); | ||
| 164 | + workspaceAddrPtr.reset(workspaceAddr); | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + ret = aclnnMatmulEmuSplitWeight(workspaceAddr, workspaceSize, executor, stream); | ||
| 168 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnMatmulEmuSplitWeight failed. ERROR: %d\n", ret); return ret); | ||
| 169 | + | ||
| 170 | + // 4. 同步等待任务执行结束 | ||
| 171 | + ret = aclrtSynchronizeStream(stream); | ||
| 172 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); | ||
| 173 | + | ||
| 174 | + // 5. 获取输出的值 | ||
| 175 | + auto size = GetShapeSize(yShape); | ||
| 176 | + std::vector<float> resultData(size, 0); | ||
| 177 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), yDeviceAddr, | ||
| 178 | + size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 179 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | ||
| 180 | + for (int64_t i = 0; i < std::min(size, 16L); i++) { | ||
| 181 | + LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); | ||
| 182 | + } | ||
| 183 | + return ACL_SUCCESS; | ||
| 184 | +} | ||
| 185 | + | ||
| 186 | +int main() | ||
| 187 | +{ | ||
| 188 | + int32_t deviceId = 0; | ||
| 189 | + aclrtStream stream; | ||
| 190 | + auto ret = AclnnMatmulEmuSplitWeightTest(deviceId, stream); | ||
| 191 | + CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("AclnnMatmulEmuSplitWeightTest failed. ERROR: %d\n", ret); return ret); | ||
| 192 | + | ||
| 193 | + Finalize(deviceId, stream); | ||
| 194 | + return 0; | ||
| 195 | +} | ||
Amatmul/matmul_emu_split_weight/op_host/config/ascend910_93/matmul_emu_split_weight_binary.json+67-0
| @@ -0,0 +1,67 @@ | |||
| 1 | +{ | ||
| 2 | + "op_type": "MatmulEmuSplitWeight", | ||
| 3 | + "optional_input_mode": "gen_placeholder", | ||
| 4 | + "op_list": [ | ||
| 5 | + { | ||
| 6 | + "bin_filename": "MatmulEmuSplitWeight_ND_ND_ND_ND_BF16_BF16_BF16_FP32", | ||
| 7 | + "inputs": [ | ||
| 8 | + { | ||
| 9 | + "name": "x", | ||
| 10 | + "index": 0, | ||
| 11 | + "dtype": "bfloat16", | ||
| 12 | + "format": "ND", | ||
| 13 | + "paramType": "required", | ||
| 14 | + "shape": [-2] | ||
| 15 | + }, | ||
| 16 | + { | ||
| 17 | + "name": "w_high", | ||
| 18 | + "index": 1, | ||
| 19 | + "dtype": "bfloat16", | ||
| 20 | + "format": "ND", | ||
| 21 | + "paramType": "required", | ||
| 22 | + "shape": [-2] | ||
| 23 | + }, | ||
| 24 | + { | ||
| 25 | + "name": "w_low", | ||
| 26 | + "index": 2, | ||
| 27 | + "dtype": "bfloat16", | ||
| 28 | + "format": "ND", | ||
| 29 | + "paramType": "required", | ||
| 30 | + "shape": [-2] | ||
| 31 | + } | ||
| 32 | + ], | ||
| 33 | + "outputs": [ | ||
| 34 | + { | ||
| 35 | + "name": "y", | ||
| 36 | + "index": 0, | ||
| 37 | + "dtype": "float32", | ||
| 38 | + "format": "ND", | ||
| 39 | + "paramType": "required", | ||
| 40 | + "shape": [-2] | ||
| 41 | + } | ||
| 42 | + ], | ||
| 43 | + "attrs": [ | ||
| 44 | + { | ||
| 45 | + "name": "w_low_scale", | ||
| 46 | + "dtype": "float", | ||
| 47 | + "value": 0.00390625 | ||
| 48 | + }, | ||
| 49 | + { | ||
| 50 | + "name": "transpose_x", | ||
| 51 | + "dtype": "bool", | ||
| 52 | + "value": null | ||
| 53 | + }, | ||
| 54 | + { | ||
| 55 | + "name": "transpose_w", | ||
| 56 | + "dtype": "bool", | ||
| 57 | + "value": null | ||
| 58 | + }, | ||
| 59 | + { | ||
| 60 | + "name": "y_dtype", | ||
| 61 | + "dtype": "int", | ||
| 62 | + "value": 0 | ||
| 63 | + } | ||
| 64 | + ] | ||
| 65 | + } | ||
| 66 | + ] | ||
| 67 | +} | ||
| @@ -0,0 +1,67 @@ | |||
| 1 | +{ | ||
| 2 | + "op_type": "MatmulEmuSplitWeight", | ||
| 3 | + "optional_input_mode": "gen_placeholder", | ||
| 4 | + "op_list": [ | ||
| 5 | + { | ||
| 6 | + "bin_filename": "MatmulEmuSplitWeight_ND_ND_ND_ND_BF16_BF16_BF16_FP32", | ||
| 7 | + "inputs": [ | ||
| 8 | + { | ||
| 9 | + "name": "x", | ||
| 10 | + "index": 0, | ||
| 11 | + "dtype": "bfloat16", | ||
| 12 | + "format": "ND", | ||
| 13 | + "paramType": "required", | ||
| 14 | + "shape": [-2] | ||
| 15 | + }, | ||
| 16 | + { | ||
| 17 | + "name": "w_high", | ||
| 18 | + "index": 1, | ||
| 19 | + "dtype": "bfloat16", | ||
| 20 | + "format": "ND", | ||
| 21 | + "paramType": "required", | ||
| 22 | + "shape": [-2] | ||
| 23 | + }, | ||
| 24 | + { | ||
| 25 | + "name": "w_low", | ||
| 26 | + "index": 2, | ||
| 27 | + "dtype": "bfloat16", | ||
| 28 | + "format": "ND", | ||
| 29 | + "paramType": "required", | ||
| 30 | + "shape": [-2] | ||
| 31 | + } | ||
| 32 | + ], | ||
| 33 | + "outputs": [ | ||
| 34 | + { | ||
| 35 | + "name": "y", | ||
| 36 | + "index": 0, | ||
| 37 | + "dtype": "float32", | ||
| 38 | + "format": "ND", | ||
| 39 | + "paramType": "required", | ||
| 40 | + "shape": [-2] | ||
| 41 | + } | ||
| 42 | + ], | ||
| 43 | + "attrs": [ | ||
| 44 | + { | ||
| 45 | + "name": "w_low_scale", | ||
| 46 | + "dtype": "float", | ||
| 47 | + "value": 0.00390625 | ||
| 48 | + }, | ||
| 49 | + { | ||
| 50 | + "name": "transpose_x", | ||
| 51 | + "dtype": "bool", | ||
| 52 | + "value": null | ||
| 53 | + }, | ||
| 54 | + { | ||
| 55 | + "name": "transpose_w", | ||
| 56 | + "dtype": "bool", | ||
| 57 | + "value": null | ||
| 58 | + }, | ||
| 59 | + { | ||
| 60 | + "name": "y_dtype", | ||
| 61 | + "dtype": "int", | ||
| 62 | + "value": 0 | ||
| 63 | + } | ||
| 64 | + ] | ||
| 65 | + } | ||
| 66 | + ] | ||
| 67 | +} | ||
| @@ -32,6 +32,9 @@ public: | |||
| 32 | .NeedCheckSupportFlag(false) | 32 | .NeedCheckSupportFlag(false) |
| 33 | .PrecisionReduceFlag(true) | 33 | .PrecisionReduceFlag(true) |
| 34 | .ExtendCfgInfo("softsync.flag", "true"); | 34 | .ExtendCfgInfo("softsync.flag", "true"); |
| 35 | + | ||
| 36 | + this->AICore().AddConfig("ascend910b", aicConfig); | ||
| 37 | + this->AICore().AddConfig("ascend910_93", aicConfig); | ||
| 35 | this->AICore().AddConfig("ascend950", aicConfig); | 38 | this->AICore().AddConfig("ascend950", aicConfig); |
| 36 | } | 39 | } |
| 37 | }; | 40 | }; |
| @@ -0,0 +1,216 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * 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. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#include "matmul_emu_split_weight_tiling.h" | ||
| 12 | +#include "matmul_emu_split_weight_tiling_key.h" | ||
| 13 | +#include "error_util.h" | ||
| 14 | +#include "matmul/common/op_host/math_util.h" | ||
| 15 | +#include <cmath> | ||
| 16 | + | ||
| 17 | +namespace { | ||
| 18 | +static constexpr uint32_t INDEX_X = 0; | ||
| 19 | +static constexpr uint32_t INDEX_W_HIGH = 1; | ||
| 20 | +static constexpr uint32_t INDEX_W_LOW = 2; | ||
| 21 | +static constexpr uint32_t INDEX_Y = 0; | ||
| 22 | +static constexpr uint32_t INDEX_ATTR_W_LOW_SCALE = 0; | ||
| 23 | +static constexpr uint32_t INDEX_ATTR_TRANS_X = 1; | ||
| 24 | +static constexpr uint32_t INDEX_ATTR_TRANS_W = 2; | ||
| 25 | +static constexpr uint32_t INDEX_ATTR_Y_DTYPE = 3; | ||
| 26 | +static constexpr int32_t Y_DTYPE_FP32 = 0; | ||
| 27 | +static constexpr uint32_t NUM_TWO = 2UL; | ||
| 28 | +static constexpr float EXPECTED_SCALE = 0.00390625f; | ||
| 29 | +static constexpr uint64_t GM_STAGE_BUFFER_NUM = 2UL; // Y0 | Y1 | ||
| 30 | +static constexpr uint64_t DATA_SIZE_FP32 = 4UL; | ||
| 31 | + | ||
| 32 | +static constexpr uint64_t SYS_WORKSPACE_RESERVED = 16UL * 1024UL * 1024UL; // 框架预留16MB | ||
| 33 | +} // namespace | ||
| 34 | + | ||
| 35 | +namespace optiling { | ||
| 36 | +namespace matmul_emu_split_weight { | ||
| 37 | + | ||
| 38 | +bool MatmulEmuSplitWeightBaseTiling::IsCapable() { return true; } | ||
| 39 | + | ||
| 40 | +ge::graphStatus MatmulEmuSplitWeightBaseTiling::GetPlatformInfo() | ||
| 41 | +{ | ||
| 42 | + auto platformInfo = context_->GetPlatformInfo(); | ||
| 43 | + OP_TILING_CHECK(platformInfo == nullptr, CUBE_INNER_ERR_REPORT(context_->GetNodeName(), "platformInfo is null"), | ||
| 44 | + return ge::GRAPH_FAILED); | ||
| 45 | + auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfo); | ||
| 46 | + aicNum_ = ascendcPlatform.GetCoreNumAic(); | ||
| 47 | + return ge::GRAPH_SUCCESS; | ||
| 48 | +} | ||
| 49 | + | ||
| 50 | +ge::graphStatus MatmulEmuSplitWeightBaseTiling::ExtractAttrs() | ||
| 51 | +{ | ||
| 52 | + auto attrs = context_->GetAttrs(); | ||
| 53 | + OP_TILING_CHECK(attrs == nullptr, CUBE_INNER_ERR_REPORT(context_->GetNodeName(), "attrs is null"), | ||
| 54 | + return ge::GRAPH_FAILED); | ||
| 55 | + | ||
| 56 | + const int32_t* yDtypePtr = attrs->GetAttrPointer<int32_t>(INDEX_ATTR_Y_DTYPE); | ||
| 57 | + yDtype_ = (yDtypePtr != nullptr) ? *yDtypePtr : Y_DTYPE_FP32; | ||
| 58 | + | ||
| 59 | + scale_ = *(attrs->GetAttrPointer<float>(INDEX_ATTR_W_LOW_SCALE)); | ||
| 60 | + | ||
| 61 | + const bool* transXPtr = attrs->GetAttrPointer<bool>(INDEX_ATTR_TRANS_X); | ||
| 62 | + transX_ = (transXPtr != nullptr) ? *transXPtr : false; | ||
| 63 | + const bool* transWPtr = attrs->GetAttrPointer<bool>(INDEX_ATTR_TRANS_W); | ||
| 64 | + transW_ = (transWPtr != nullptr) ? *transWPtr : false; | ||
| 65 | + return ge::GRAPH_SUCCESS; | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +ge::graphStatus MatmulEmuSplitWeightBaseTiling::ExtractShape() | ||
| 69 | +{ | ||
| 70 | + auto xShape = context_->GetInputShape(INDEX_X)->GetOriginShape(); | ||
| 71 | + auto wHighShape = context_->GetInputShape(INDEX_W_HIGH)->GetOriginShape(); | ||
| 72 | + auto wLowShape = context_->GetInputShape(INDEX_W_LOW)->GetOriginShape(); | ||
| 73 | + | ||
| 74 | + m_ = transX_ ? static_cast<uint64_t>(xShape.GetDim(1)) : static_cast<uint64_t>(xShape.GetDim(0)); | ||
| 75 | + k_ = transX_ ? static_cast<uint64_t>(xShape.GetDim(0)) : static_cast<uint64_t>(xShape.GetDim(1)); | ||
| 76 | + n_ = transW_ ? static_cast<uint64_t>(wHighShape.GetDim(0)) : static_cast<uint64_t>(wHighShape.GetDim(1)); | ||
| 77 | + | ||
| 78 | + wHighK_ = transW_ ? static_cast<uint64_t>(wHighShape.GetDim(1)) : static_cast<uint64_t>(wHighShape.GetDim(0)); | ||
| 79 | + wLowK_ = transW_ ? static_cast<uint64_t>(wLowShape.GetDim(1)) : static_cast<uint64_t>(wLowShape.GetDim(0)); | ||
| 80 | + wLowN_ = transW_ ? static_cast<uint64_t>(wLowShape.GetDim(0)) : static_cast<uint64_t>(wLowShape.GetDim(1)); | ||
| 81 | + return ge::GRAPH_SUCCESS; | ||
| 82 | +} | ||
| 83 | + | ||
| 84 | +ge::graphStatus MatmulEmuSplitWeightBaseTiling::ValidateShape() | ||
| 85 | +{ | ||
| 86 | + auto xShape = context_->GetInputShape(INDEX_X)->GetOriginShape(); | ||
| 87 | + auto wHighShape = context_->GetInputShape(INDEX_W_HIGH)->GetOriginShape(); | ||
| 88 | + auto wLowShape = context_->GetInputShape(INDEX_W_LOW)->GetOriginShape(); | ||
| 89 | + | ||
| 90 | + OP_TILING_CHECK( | ||
| 91 | + xShape.GetDimNum() != NUM_TWO || wHighShape.GetDimNum() != NUM_TWO || wLowShape.GetDimNum() != NUM_TWO, | ||
| 92 | + CUBE_INNER_ERR_REPORT(context_->GetNodeName(), "all inputs must be 2D"), return ge::GRAPH_FAILED); | ||
| 93 | + | ||
| 94 | + OP_TILING_CHECK(m_ == 0 || m_ > static_cast<uint64_t>(INT32_MAX) || n_ == 0 || | ||
| 95 | + n_ > static_cast<uint64_t>(INT32_MAX) || k_ == 0 || k_ > static_cast<uint64_t>(INT32_MAX), | ||
| 96 | + CUBE_INNER_ERR_REPORT(context_->GetNodeName(), | ||
| 97 | + "m, k, n of x, w must be within the range (0, INT32_MAX], " | ||
| 98 | + "got m=%lu, k=%lu, n=%lu", | ||
| 99 | + m_, k_, n_), | ||
| 100 | + return ge::GRAPH_FAILED); | ||
| 101 | + | ||
| 102 | + OP_TILING_CHECK(k_ != wHighK_, | ||
| 103 | + CUBE_INNER_ERR_REPORT(context_->GetNodeName(), "x K(%lu) must match w_high K(%lu)", k_, wHighK_), | ||
| 104 | + return ge::GRAPH_FAILED); | ||
| 105 | + OP_TILING_CHECK(k_ != wLowK_, | ||
| 106 | + CUBE_INNER_ERR_REPORT(context_->GetNodeName(), "x K(%lu) must match w_low K(%lu)", k_, wLowK_), | ||
| 107 | + return ge::GRAPH_FAILED); | ||
| 108 | + OP_TILING_CHECK(n_ != wLowN_, | ||
| 109 | + CUBE_INNER_ERR_REPORT(context_->GetNodeName(), "w_high N(%lu) must match w_low N(%lu)", n_, wLowN_), | ||
| 110 | + return ge::GRAPH_FAILED); | ||
| 111 | + | ||
| 112 | + return ge::GRAPH_SUCCESS; | ||
| 113 | +} | ||
| 114 | + | ||
| 115 | +ge::graphStatus MatmulEmuSplitWeightBaseTiling::ValidateAttrs() | ||
| 116 | +{ | ||
| 117 | + OP_TILING_CHECK(yDtype_ != Y_DTYPE_FP32, | ||
| 118 | + CUBE_INNER_ERR_REPORT(context_->GetNodeName(), "y_dtype only supports 0 (FP32), got %d", yDtype_), | ||
| 119 | + return ge::GRAPH_FAILED); | ||
| 120 | + OP_TILING_CHECK( | ||
| 121 | + std::fabs(scale_ - EXPECTED_SCALE) > 1e-7f, | ||
| 122 | + CUBE_INNER_ERR_REPORT(context_->GetNodeName(), "w_low_scale only supports 1/256 (0.00390625), got %f", scale_), | ||
| 123 | + return ge::GRAPH_FAILED); | ||
| 124 | + return ge::GRAPH_SUCCESS; | ||
| 125 | +} | ||
| 126 | + | ||
| 127 | +ge::graphStatus MatmulEmuSplitWeightBaseTiling::GetShapeAttrsInfo() | ||
| 128 | +{ | ||
| 129 | + if (ExtractAttrs() != ge::GRAPH_SUCCESS) { | ||
| 130 | + return ge::GRAPH_FAILED; | ||
| 131 | + } | ||
| 132 | + if (ExtractShape() != ge::GRAPH_SUCCESS) { | ||
| 133 | + return ge::GRAPH_FAILED; | ||
| 134 | + } | ||
| 135 | + if (ValidateShape() != ge::GRAPH_SUCCESS) { | ||
| 136 | + return ge::GRAPH_FAILED; | ||
| 137 | + } | ||
| 138 | + if (ValidateAttrs() != ge::GRAPH_SUCCESS) { | ||
| 139 | + return ge::GRAPH_FAILED; | ||
| 140 | + } | ||
| 141 | + return ge::GRAPH_SUCCESS; | ||
| 142 | +} | ||
| 143 | + | ||
| 144 | +void MatmulEmuSplitWeightBaseTiling::CalcCoreNum() | ||
| 145 | +{ | ||
| 146 | + uint64_t mTileNum = ops::CeilDiv(m_, baseM_); | ||
| 147 | + uint64_t nTileNum = ops::CeilDiv(n_, baseN_); | ||
| 148 | + uint64_t totalTiles = mTileNum * nTileNum; | ||
| 149 | + usedCoreNum_ = std::min(aicNum_, totalTiles); | ||
| 150 | + if (usedCoreNum_ == 0) { | ||
| 151 | + usedCoreNum_ = 1; | ||
| 152 | + } | ||
| 153 | +} | ||
| 154 | + | ||
| 155 | +void MatmulEmuSplitWeightBaseTiling::SetTilingData() | ||
| 156 | +{ | ||
| 157 | + tilingData_.m = static_cast<uint32_t>(m_); | ||
| 158 | + tilingData_.n = static_cast<uint32_t>(n_); | ||
| 159 | + tilingData_.k = static_cast<uint32_t>(k_); | ||
| 160 | + // baseM/baseN/baseK/kL1 are compile-time Catlass L1TileShape/L0TileShape on A2; | ||
| 161 | + // they are not consumed by the A2 kernel and are left at zero. | ||
| 162 | + tilingData_.usedCoreNum = static_cast<uint32_t>(usedCoreNum_); | ||
| 163 | + tilingData_.transX = static_cast<uint8_t>(transX_); | ||
| 164 | + tilingData_.transW = static_cast<uint8_t>(transW_); | ||
| 165 | + tilingData_.yDtype = static_cast<uint8_t>(yDtype_); | ||
| 166 | + tilingData_.scale = scale_; | ||
| 167 | + | ||
| 168 | + MatmulEmuSplitWeightBaseTilingKey tilingKeyObj; | ||
| 169 | + tilingKeyObj.SetTrans(transX_, transW_); | ||
| 170 | + // Swizzle direction is baked into the tiling key so the kernel instantiates | ||
| 171 | + // GemmIdentityBlockSwizzle<3, SwizzleDir> at compile time. | ||
| 172 | + uint64_t swizzleDir = (m_ > n_) ? 0u : 1u; | ||
| 173 | + tilingKeyObj.SetSwizzle(swizzleDir); | ||
| 174 | + tilingKey_ = tilingKeyObj.GetTilingKey(); | ||
| 175 | +} | ||
| 176 | + | ||
| 177 | +ge::graphStatus MatmulEmuSplitWeightBaseTiling::DoOpTiling() | ||
| 178 | +{ | ||
| 179 | + CalcCoreNum(); | ||
| 180 | + SetTilingData(); | ||
| 181 | + return ge::GRAPH_SUCCESS; | ||
| 182 | +} | ||
| 183 | + | ||
| 184 | +ge::graphStatus MatmulEmuSplitWeightBaseTiling::DoLibApiTiling() { return ge::GRAPH_SUCCESS; } | ||
| 185 | + | ||
| 186 | +uint64_t MatmulEmuSplitWeightBaseTiling::GetTilingKey() const { return tilingKey_; } | ||
| 187 | + | ||
| 188 | +ge::graphStatus MatmulEmuSplitWeightBaseTiling::GetWorkspaceSize() | ||
| 189 | +{ | ||
| 190 | + workspaceSize_ = SYS_WORKSPACE_RESERVED + | ||
| 191 | + GM_STAGE_BUFFER_NUM * static_cast<size_t>(m_) * static_cast<size_t>(n_) * DATA_SIZE_FP32; | ||
| 192 | + return ge::GRAPH_SUCCESS; | ||
| 193 | +} | ||
| 194 | + | ||
| 195 | +ge::graphStatus MatmulEmuSplitWeightBaseTiling::PostTiling() | ||
| 196 | +{ | ||
| 197 | + size_t sizeTilingData = sizeof(MatmulEmuSplitWeightTilingData); | ||
| 198 | + OP_TILING_CHECK(sizeTilingData % sizeof(uint64_t) != 0, | ||
| 199 | + OP_LOGE(context_->GetNodeName(), "tiling data size[%zu] is not aligned to 8", sizeTilingData), | ||
| 200 | + return ge::GRAPH_FAILED); | ||
| 201 | + OPS_CHECK_NULL_WITH_CONTEXT(context_, context_->GetRawTilingData()); | ||
| 202 | + context_->GetRawTilingData()->SetDataSize(sizeTilingData); | ||
| 203 | + context_->SetBlockDim(tilingData_.usedCoreNum); | ||
| 204 | + | ||
| 205 | + size_t* workspaces = context_->GetWorkspaceSizes(1); | ||
| 206 | + OP_TILING_CHECK(workspaces == nullptr, CUBE_INNER_ERR_REPORT(context_->GetNodeName(), "workspaces is null"), | ||
| 207 | + return ge::GRAPH_FAILED); | ||
| 208 | + workspaces[0] = workspaceSize_; | ||
| 209 | + | ||
| 210 | + auto tilingPtr = static_cast<MatmulEmuSplitWeightTilingData*>(context_->GetRawTilingData()->GetData()); | ||
| 211 | + *tilingPtr = tilingData_; | ||
| 212 | + return ge::GRAPH_SUCCESS; | ||
| 213 | +} | ||
| 214 | + | ||
| 215 | +} // namespace matmul_emu_split_weight | ||
| 216 | +} // namespace optiling | ||
| @@ -0,0 +1,65 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * 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. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#pragma once | ||
| 12 | + | ||
| 13 | +#include "op_host/tiling_base.h" | ||
| 14 | +#include "../../../op_kernel/matmul_emu_split_weight_tiling_data.h" | ||
| 15 | + | ||
| 16 | +namespace optiling { | ||
| 17 | +namespace matmul_emu_split_weight { | ||
| 18 | + | ||
| 19 | +class MatmulEmuSplitWeightBaseTiling : public Ops::NN::Optiling::TilingBaseClass { | ||
| 20 | +public: | ||
| 21 | + explicit MatmulEmuSplitWeightBaseTiling(gert::TilingContext* context) : TilingBaseClass(context) {} | ||
| 22 | + ~MatmulEmuSplitWeightBaseTiling() override = default; | ||
| 23 | + | ||
| 24 | +protected: | ||
| 25 | + bool IsCapable() override; | ||
| 26 | + ge::graphStatus GetPlatformInfo() override; | ||
| 27 | + ge::graphStatus GetShapeAttrsInfo() override; | ||
| 28 | + ge::graphStatus DoOpTiling() override; | ||
| 29 | + ge::graphStatus DoLibApiTiling() override; | ||
| 30 | + uint64_t GetTilingKey() const override; | ||
| 31 | + ge::graphStatus GetWorkspaceSize() override; | ||
| 32 | + ge::graphStatus PostTiling() override; | ||
| 33 | + | ||
| 34 | +private: | ||
| 35 | + ge::graphStatus ExtractAttrs(); | ||
| 36 | + ge::graphStatus ExtractShape(); | ||
| 37 | + ge::graphStatus ValidateShape(); | ||
| 38 | + ge::graphStatus ValidateAttrs(); | ||
| 39 | + void CalcCoreNum(); | ||
| 40 | + void SetTilingData(); | ||
| 41 | + | ||
| 42 | + uint64_t m_{0}; | ||
| 43 | + uint64_t n_{0}; | ||
| 44 | + uint64_t k_{0}; | ||
| 45 | + uint64_t wHighK_{0}; | ||
| 46 | + uint64_t wLowK_{0}; | ||
| 47 | + uint64_t wLowN_{0}; | ||
| 48 | + float scale_{0.00390625f}; | ||
| 49 | + bool transX_{false}; | ||
| 50 | + bool transW_{false}; | ||
| 51 | + int32_t yDtype_{0}; | ||
| 52 | + uint64_t aicNum_{0}; | ||
| 53 | + uint64_t tilingKey_{0}; | ||
| 54 | + uint64_t usedCoreNum_{0}; | ||
| 55 | + // Host-only, matches compile-time Catlass L1TileShape <128,256,256>. Used for | ||
| 56 | + // core-count; not written into tilingData (A2 kernel does not consume baseMNK). | ||
| 57 | + uint64_t baseM_{128}; | ||
| 58 | + uint64_t baseN_{256}; | ||
| 59 | + uint64_t baseK_{256}; | ||
| 60 | + uint64_t kL1_{256}; | ||
| 61 | + MatmulEmuSplitWeightTilingData tilingData_{}; | ||
| 62 | +}; | ||
| 63 | + | ||
| 64 | +} // namespace matmul_emu_split_weight | ||
| 65 | +} // namespace optiling | ||
Amatmul/matmul_emu_split_weight/op_host/op_tiling/arch22/matmul_emu_split_weight_tiling_key.cpp+22-0
| @@ -0,0 +1,22 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * 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. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#include "matmul_emu_split_weight_tiling_key.h" | ||
| 12 | + | ||
| 13 | +namespace optiling { | ||
| 14 | +namespace matmul_emu_split_weight { | ||
| 15 | + | ||
| 16 | +uint64_t MatmulEmuSplitWeightBaseTilingKey::GetTilingKey() const | ||
| 17 | +{ | ||
| 18 | + return GET_TPL_TILING_KEY(0UL, aTrans_, bTrans_, swizzleDir_); | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | +} // namespace matmul_emu_split_weight | ||
| 22 | +} // namespace optiling | ||
| @@ -0,0 +1,46 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * 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. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#pragma once | ||
| 12 | + | ||
| 13 | +#include "../../../op_kernel/arch22/matmul_emu_split_weight_tiling_key.h" | ||
| 14 | + | ||
| 15 | +namespace optiling { | ||
| 16 | +namespace matmul_emu_split_weight { | ||
| 17 | + | ||
| 18 | +class MatmulEmuSplitWeightBaseTilingKey { | ||
| 19 | +public: | ||
| 20 | + MatmulEmuSplitWeightBaseTilingKey& SetTrans(bool aTrans, bool bTrans) | ||
| 21 | + { | ||
| 22 | + if (aTrans) { | ||
| 23 | + aTrans_ = MATMUL_EMU_SPLIT_WEIGHT_TRANS; | ||
| 24 | + } | ||
| 25 | + if (bTrans) { | ||
| 26 | + bTrans_ = MATMUL_EMU_SPLIT_WEIGHT_TRANS; | ||
| 27 | + } | ||
| 28 | + return *this; | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + MatmulEmuSplitWeightBaseTilingKey& SetSwizzle(uint64_t swizzleDir) | ||
| 32 | + { | ||
| 33 | + swizzleDir_ = swizzleDir; | ||
| 34 | + return *this; | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + uint64_t GetTilingKey() const; | ||
| 38 | + | ||
| 39 | +private: | ||
| 40 | + uint64_t aTrans_{MATMUL_EMU_SPLIT_WEIGHT_NO_TRANS}; | ||
| 41 | + uint64_t bTrans_{MATMUL_EMU_SPLIT_WEIGHT_NO_TRANS}; | ||
| 42 | + uint64_t swizzleDir_{0}; | ||
| 43 | +}; | ||
| 44 | + | ||
| 45 | +} // namespace matmul_emu_split_weight | ||
| 46 | +} // namespace optiling | ||
Amatmul/matmul_emu_split_weight/op_host/op_tiling/arch22/matmul_emu_split_weight_tiling_strategy.h+39-0
| @@ -0,0 +1,39 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * 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. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#pragma once | ||
| 12 | + | ||
| 13 | +#include <map> | ||
| 14 | +#include <vector> | ||
| 15 | +#include <cstdint> | ||
| 16 | + | ||
| 17 | +#include "tiling/platform/platform_ascendc.h" | ||
| 18 | +#include "op_host/tiling_base.h" | ||
| 19 | + | ||
| 20 | +namespace optiling { | ||
| 21 | +namespace matmul_emu_split_weight { | ||
| 22 | +namespace strategy { | ||
| 23 | +constexpr int32_t BASE = 999; | ||
| 24 | + | ||
| 25 | +const static std::map<NpuArch, std::vector<int32_t>> MatmulEmuSplitWeightPrioritiesMap = { | ||
| 26 | + {NpuArch::DAV_2201, {strategy::BASE}}, | ||
| 27 | +}; | ||
| 28 | + | ||
| 29 | +inline std::vector<int32_t> GetMatmulEmuSplitWeightPriorities(NpuArch npuArch) | ||
| 30 | +{ | ||
| 31 | + std::vector<int32_t> priorities = {}; | ||
| 32 | + if (MatmulEmuSplitWeightPrioritiesMap.find(npuArch) != MatmulEmuSplitWeightPrioritiesMap.end()) { | ||
| 33 | + priorities = MatmulEmuSplitWeightPrioritiesMap.at(npuArch); | ||
| 34 | + } | ||
| 35 | + return priorities; | ||
| 36 | +}; | ||
| 37 | +} // namespace strategy | ||
| 38 | +} // namespace matmul_emu_split_weight | ||
| 39 | +} // namespace optiling | ||
Rmatmul/matmul_emu_split_weight/op_host/op_tiling/matmul_emu_split_weight_tiling.cpp→matmul/matmul_emu_split_weight/op_host/op_tiling/matmul_emu_split_weight_tiling_registry.cpp+7-6
| @@ -8,6 +8,7 @@ | |||
| 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 | +#include "arch22/matmul_emu_split_weight_tiling.h" | ||
| 11 | #include "arch35/matmul_emu_split_weight_tiling.h" | 12 | #include "arch35/matmul_emu_split_weight_tiling.h" |
| 12 | #include "arch35/matmul_emu_split_weight_compile_info.h" | 13 | #include "arch35/matmul_emu_split_weight_compile_info.h" |
| 13 | 14 | ||
| @@ -15,6 +16,7 @@ | |||
| 15 | #include "error_util.h" | 16 | #include "error_util.h" |
| 16 | #include "op_host/tiling_templates_registry.h" | 17 | #include "op_host/tiling_templates_registry.h" |
| 17 | 18 | ||
| 19 | +using optiling::matmul_emu_split_weight::MatmulEmuSplitWeightBaseTiling; | ||
| 18 | using optiling::matmul_emu_split_weight::MatmulEmuSplitWeightTiling; | 20 | using optiling::matmul_emu_split_weight::MatmulEmuSplitWeightTiling; |
| 19 | 21 | ||
| 20 | namespace { | 22 | namespace { |
| @@ -34,12 +36,11 @@ static ge::graphStatus MatmulEmuSplitWeightTilingFunc(gert::TilingContext* conte | |||
| 34 | return ge::GRAPH_FAILED); | 36 | return ge::GRAPH_FAILED); |
| 35 | auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfo); | 37 | auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfo); |
| 36 | auto socVersion = ascendcPlatform.GetSocVersion(); | 38 | auto socVersion = ascendcPlatform.GetSocVersion(); |
| 37 | - OP_TILING_CHECK(socVersion != platform_ascendc::SocVersion::ASCEND950, | 39 | + if (socVersion == platform_ascendc::SocVersion::ASCEND950) { |
| 38 | - CUBE_INNER_ERR_REPORT("MatmulEmuSplitWeight", | 40 | + return MatmulEmuSplitWeightTiling(context).DoTiling(); |
| 39 | - "MatmulEmuSplitWeight only supports Ascend950, current socVersion is %d", | 41 | + } |
| 40 | - static_cast<int32_t>(socVersion)), | 42 | + |
| 41 | - return ge::GRAPH_FAILED); | 43 | + return MatmulEmuSplitWeightBaseTiling(context).DoTiling(); |
| 42 | - return MatmulEmuSplitWeightTiling(context).DoTiling(); | ||
| 43 | } | 44 | } |
| 44 | 45 | ||
| 45 | static ge::graphStatus TilingPrepareForMatmulEmuSplitWeight(gert::TilingParseContext* context) | 46 | static ge::graphStatus TilingPrepareForMatmulEmuSplitWeight(gert::TilingParseContext* context) |
| @@ -0,0 +1,271 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025-2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * 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. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#ifndef MATMUL_EMU_CATLASS_ARCH_ARCH_HPP | ||
| 12 | +#define MATMUL_EMU_CATLASS_ARCH_ARCH_HPP | ||
| 13 | + | ||
| 14 | +#include "../matmul_emu_catlass.hpp" | ||
| 15 | + | ||
| 16 | +namespace Catlass::Arch { | ||
| 17 | + | ||
| 18 | +struct AtlasA2 { | ||
| 19 | + static constexpr uint32_t BIAS_SIZE = 1024; | ||
| 20 | + static constexpr uint32_t FIXBUF_SIZE = 7 * 1024; | ||
| 21 | + static constexpr uint32_t UB_SIZE = 192 * 1024; | ||
| 22 | + static constexpr uint32_t L1_SIZE = 512 * 1024; | ||
| 23 | + static constexpr uint32_t L0A_SIZE = 64 * 1024; | ||
| 24 | + static constexpr uint32_t L0B_SIZE = 64 * 1024; | ||
| 25 | + static constexpr uint32_t L0C_SIZE = 128 * 1024; | ||
| 26 | +}; | ||
| 27 | + | ||
| 28 | +template <AscendC::TPosition POS> | ||
| 29 | +using PositionType = std::integral_constant<AscendC::TPosition, POS>; | ||
| 30 | + | ||
| 31 | +using PositionGM = PositionType<AscendC::TPosition::GM>; | ||
| 32 | +using PositionL1 = PositionType<AscendC::TPosition::A1>; | ||
| 33 | +using PositionL0A = PositionType<AscendC::TPosition::A2>; | ||
| 34 | +using PositionL0B = PositionType<AscendC::TPosition::B2>; | ||
| 35 | +using PositionL0C = PositionType<AscendC::TPosition::CO1>; | ||
| 36 | +using PositionBias = PositionType<AscendC::TPosition::C2>; | ||
| 37 | +using PositionUB = PositionType<AscendC::TPosition::VECCALC>; | ||
| 38 | + | ||
| 39 | +struct LocalTensorBufferBase { | ||
| 40 | +public: | ||
| 41 | + template <class Element = half> | ||
| 42 | + CATLASS_DEVICE AscendC::LocalTensor<Element> GetBufferByByte(const uint32_t offset) const | ||
| 43 | + { | ||
| 44 | + return tensor[offset].template ReinterpretCast<Element>(); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | +protected: | ||
| 48 | + CATLASS_DEVICE | ||
| 49 | + LocalTensorBufferBase() = default; | ||
| 50 | + | ||
| 51 | + AscendC::LocalTensor<uint8_t> tensor; | ||
| 52 | +}; | ||
| 53 | + | ||
| 54 | +template <class ArchTag, AscendC::TPosition Position> | ||
| 55 | +struct LocalTensorBuffer { | ||
| 56 | + static_assert(DEPENDENT_FALSE<ArchTag>, "Unsupported local tensor buffer, can not find the specialization."); | ||
| 57 | +}; | ||
| 58 | + | ||
| 59 | +/// Partial specialization for TPosition::A1 | ||
| 60 | +template <class ArchTag> | ||
| 61 | +struct LocalTensorBuffer<ArchTag, AscendC::TPosition::A1> : LocalTensorBufferBase { | ||
| 62 | +public: | ||
| 63 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::A1; | ||
| 64 | + | ||
| 65 | + CATLASS_DEVICE | ||
| 66 | + LocalTensorBuffer() | ||
| 67 | + { | ||
| 68 | + AscendC::TBuf<AscendC::TPosition::A1> tbufA1; | ||
| 69 | + GetTPipePtr()->InitBuffer(tbufA1, ArchTag::L1_SIZE); | ||
| 70 | + tensor = tbufA1.Get<uint8_t>(); | ||
| 71 | + } | ||
| 72 | +}; | ||
| 73 | + | ||
| 74 | +/////////////////////////////////////////////////////////// | ||
| 75 | + | ||
| 76 | +/// Partial specialization for TPosition::A2 | ||
| 77 | +template <class ArchTag> | ||
| 78 | +struct LocalTensorBuffer<ArchTag, AscendC::TPosition::A2> : LocalTensorBufferBase { | ||
| 79 | +public: | ||
| 80 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::A2; | ||
| 81 | + | ||
| 82 | + CATLASS_DEVICE | ||
| 83 | + LocalTensorBuffer() | ||
| 84 | + { | ||
| 85 | + AscendC::TBuf<AscendC::TPosition::A2> tbufA2; | ||
| 86 | + GetTPipePtr()->InitBuffer(tbufA2, ArchTag::L0A_SIZE); | ||
| 87 | + tensor = tbufA2.Get<uint8_t>(); | ||
| 88 | + } | ||
| 89 | +}; | ||
| 90 | + | ||
| 91 | +/////////////////////////////////////////////////////////// | ||
| 92 | + | ||
| 93 | +/// Partial specialization for TPosition::B2 | ||
| 94 | +template <class ArchTag> | ||
| 95 | +struct LocalTensorBuffer<ArchTag, AscendC::TPosition::B2> : LocalTensorBufferBase { | ||
| 96 | +public: | ||
| 97 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::B2; | ||
| 98 | + | ||
| 99 | + CATLASS_DEVICE | ||
| 100 | + LocalTensorBuffer() | ||
| 101 | + { | ||
| 102 | + AscendC::TBuf<AscendC::TPosition::B2> tbufB2; | ||
| 103 | + GetTPipePtr()->InitBuffer(tbufB2, ArchTag::L0B_SIZE); | ||
| 104 | + tensor = tbufB2.Get<uint8_t>(); | ||
| 105 | + } | ||
| 106 | +}; | ||
| 107 | + | ||
| 108 | +/////////////////////////////////////////////////////////// | ||
| 109 | + | ||
| 110 | +/// Partial specialization for TPosition::C2 | ||
| 111 | +template <class ArchTag> | ||
| 112 | +struct LocalTensorBuffer<ArchTag, AscendC::TPosition::C2> : LocalTensorBufferBase { | ||
| 113 | +public: | ||
| 114 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::C2; | ||
| 115 | + | ||
| 116 | + CATLASS_DEVICE | ||
| 117 | + LocalTensorBuffer() | ||
| 118 | + { | ||
| 119 | + AscendC::TBuf<AscendC::TPosition::C2> tbufC2; | ||
| 120 | + GetTPipePtr()->InitBuffer(tbufC2, ArchTag::BIAS_SIZE); | ||
| 121 | + tensor = tbufC2.Get<uint8_t>(); | ||
| 122 | + } | ||
| 123 | +}; | ||
| 124 | + | ||
| 125 | +/////////////////////////////////////////////////////////// | ||
| 126 | + | ||
| 127 | +/// Partial specialization for TPosition::CO1 | ||
| 128 | +template <class ArchTag> | ||
| 129 | +struct LocalTensorBuffer<ArchTag, AscendC::TPosition::CO1> : LocalTensorBufferBase { | ||
| 130 | +public: | ||
| 131 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::CO1; | ||
| 132 | + | ||
| 133 | + CATLASS_DEVICE | ||
| 134 | + LocalTensorBuffer() | ||
| 135 | + { | ||
| 136 | + AscendC::TBuf<AscendC::TPosition::CO1> tbufCO1; | ||
| 137 | + GetTPipePtr()->InitBuffer(tbufCO1, ArchTag::L0C_SIZE); | ||
| 138 | + tensor = tbufCO1.Get<uint8_t>(); | ||
| 139 | + } | ||
| 140 | +}; | ||
| 141 | + | ||
| 142 | +/////////////////////////////////////////////////////////// | ||
| 143 | + | ||
| 144 | +/// Partial specialization for TPosition::C2PIPE2GM | ||
| 145 | +template <class ArchTag> | ||
| 146 | +struct LocalTensorBuffer<ArchTag, AscendC::TPosition::C2PIPE2GM> : LocalTensorBufferBase { | ||
| 147 | +public: | ||
| 148 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::C2PIPE2GM; | ||
| 149 | + | ||
| 150 | + CATLASS_DEVICE | ||
| 151 | + LocalTensorBuffer() | ||
| 152 | + { | ||
| 153 | + AscendC::TBuf<AscendC::TPosition::C2PIPE2GM> tbufC2PIPE2GM; | ||
| 154 | + GetTPipePtr()->InitBuffer(tbufC2PIPE2GM, ArchTag::FIXBUF_SIZE); | ||
| 155 | + tensor = tbufC2PIPE2GM.Get<uint8_t>(); | ||
| 156 | + } | ||
| 157 | +}; | ||
| 158 | + | ||
| 159 | +/////////////////////////////////////////////////////////// | ||
| 160 | + | ||
| 161 | +/// Partial specialization for TPosition::VECCALC | ||
| 162 | +template <class ArchTag> | ||
| 163 | +struct LocalTensorBuffer<ArchTag, AscendC::TPosition::VECCALC> : LocalTensorBufferBase { | ||
| 164 | +public: | ||
| 165 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::VECCALC; | ||
| 166 | + | ||
| 167 | + CATLASS_DEVICE | ||
| 168 | + LocalTensorBuffer() | ||
| 169 | + { | ||
| 170 | + AscendC::TBuf<AscendC::TPosition::VECCALC> tbufVECCALC; | ||
| 171 | + GetTPipePtr()->InitBuffer(tbufVECCALC, ArchTag::UB_SIZE); | ||
| 172 | + tensor = tbufVECCALC.Get<uint8_t>(); | ||
| 173 | + } | ||
| 174 | +}; | ||
| 175 | + | ||
| 176 | +template <class ArchTag> | ||
| 177 | +struct Resource { | ||
| 178 | +public: | ||
| 179 | + AscendC::TPipe pipe; | ||
| 180 | + | ||
| 181 | + LocalTensorBuffer<ArchTag, AscendC::TPosition::A1> l1Buf; | ||
| 182 | + LocalTensorBuffer<ArchTag, AscendC::TPosition::A2> l0ABuf; | ||
| 183 | + LocalTensorBuffer<ArchTag, AscendC::TPosition::B2> l0BBuf; | ||
| 184 | + LocalTensorBuffer<ArchTag, AscendC::TPosition::C2> btBuf; | ||
| 185 | + LocalTensorBuffer<ArchTag, AscendC::TPosition::CO1> l0CBuf; | ||
| 186 | + LocalTensorBuffer<ArchTag, AscendC::TPosition::VECCALC> ubBuf; | ||
| 187 | + LocalTensorBuffer<ArchTag, AscendC::TPosition::C2PIPE2GM> fpBuf; | ||
| 188 | + | ||
| 189 | + CATLASS_DEVICE | ||
| 190 | + Resource() | ||
| 191 | + { | ||
| 192 | + // The initialization of AscendC::Tpipe will insert some synchronization interfaces, | ||
| 193 | + // which may conflict with the usage by users. Therefore, the "destroy" interface is used for releasing. | ||
| 194 | + pipe.Destroy(); | ||
| 195 | + } | ||
| 196 | +}; | ||
| 197 | + | ||
| 198 | +// A certain cross core flag can be continuously set up to 15 times without waiting. In scenarios where there is only | ||
| 199 | +// one-way synchronization between cores and the synchronization operation is executed multiple times, if the core | ||
| 200 | +// performing the set operation runs faster, it may result in the flag being set more than 15 times consecutively, | ||
| 201 | +// leading to a system freeze. To prevent such a freeze, we need to wait for the waiting cores to perform a reverse | ||
| 202 | +// synchronization operation after executing the set operation MAX_REVERSE_DEPTH times. | ||
| 203 | +constexpr uint32_t MAX_REVERSE_DEPTH = 15; | ||
| 204 | + | ||
| 205 | +using FlagID = uint16_t; | ||
| 206 | +constexpr FlagID AIV_INTER_BLOCK_BARRIER = 8; | ||
| 207 | +constexpr FlagID AIC_INTER_BLOCK_BARRIER = 9; | ||
| 208 | +constexpr FlagID AIV_INTER_SUBBLOCK_BARRIER = 10; | ||
| 209 | +constexpr FlagID FFTS_MAX_FLAG = 7; | ||
| 210 | + | ||
| 211 | +struct CrossCoreFlag { | ||
| 212 | + CATLASS_DEVICE | ||
| 213 | + CrossCoreFlag() : id(0) {} | ||
| 214 | + | ||
| 215 | + CATLASS_DEVICE | ||
| 216 | + CrossCoreFlag(FlagID id) : id(id) {} | ||
| 217 | + | ||
| 218 | + FlagID id; | ||
| 219 | +}; | ||
| 220 | + | ||
| 221 | +template <uint32_t REVERSE_DEPTH_ = MAX_REVERSE_DEPTH> | ||
| 222 | +struct CrossCoreFlagWithReverse { | ||
| 223 | + CATLASS_DEVICE | ||
| 224 | + CrossCoreFlagWithReverse() : id(0), reverseId(0) {} | ||
| 225 | + | ||
| 226 | + CATLASS_DEVICE | ||
| 227 | + CrossCoreFlagWithReverse(FlagID id, FlagID reverseId) : id(id), reverseId(reverseId) {} | ||
| 228 | + | ||
| 229 | + FlagID id; | ||
| 230 | + FlagID reverseId; | ||
| 231 | + uint32_t count{0}; | ||
| 232 | +}; | ||
| 233 | + | ||
| 234 | +template <uint8_t MODE, pipe_t PIPE> | ||
| 235 | +CATLASS_DEVICE void CrossCoreSetFlag(CrossCoreFlag& flag) | ||
| 236 | +{ | ||
| 237 | + AscendC::CrossCoreSetFlag<MODE, PIPE>(flag.id); | ||
| 238 | +} | ||
| 239 | + | ||
| 240 | +CATLASS_DEVICE | ||
| 241 | +void CrossCoreWaitFlag(CrossCoreFlag& flag) { AscendC::CrossCoreWaitFlag(flag.id); } | ||
| 242 | + | ||
| 243 | +template <uint8_t MODE, pipe_t PIPE> | ||
| 244 | +CATLASS_DEVICE void CrossCoreWaitFlag(CrossCoreFlag& flag) | ||
| 245 | +{ | ||
| 246 | + AscendC::CrossCoreWaitFlag<MODE, PIPE>(flag.id); | ||
| 247 | +} | ||
| 248 | + | ||
| 249 | +template <uint8_t MODE, pipe_t PIPE, uint32_t REVERSE_DEPTH> | ||
| 250 | +CATLASS_DEVICE void CrossCoreSetFlagWithReverse(CrossCoreFlagWithReverse<REVERSE_DEPTH>& flag) | ||
| 251 | +{ | ||
| 252 | + AscendC::CrossCoreSetFlag<MODE, PIPE>(flag.id); | ||
| 253 | + if (++flag.count >= REVERSE_DEPTH) { | ||
| 254 | + AscendC::CrossCoreWaitFlag(flag.reverseId); | ||
| 255 | + flag.count = 0; | ||
| 256 | + } | ||
| 257 | +} | ||
| 258 | + | ||
| 259 | +template <uint8_t MODE, pipe_t PIPE, uint32_t REVERSE_DEPTH> | ||
| 260 | +CATLASS_DEVICE void CrossCoreWaitFlagWithReverse(CrossCoreFlagWithReverse<REVERSE_DEPTH>& flag) | ||
| 261 | +{ | ||
| 262 | + AscendC::CrossCoreWaitFlag(flag.id); | ||
| 263 | + if (++flag.count >= REVERSE_DEPTH) { | ||
| 264 | + AscendC::CrossCoreSetFlag<MODE, PIPE>(flag.reverseId); | ||
| 265 | + flag.count = 0; | ||
| 266 | + } | ||
| 267 | +} | ||
| 268 | + | ||
| 269 | +} // namespace Catlass::Arch | ||
| 270 | + | ||
| 271 | +#endif // MATMUL_EMU_CATLASS_ARCH_ARCH_HPP | ||
| @@ -0,0 +1,341 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025-2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * 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. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#ifndef CATLASS_EPILOGUE_BLOCK_BLOCK_EPILOGUE_DECOMPOSITION_GM_HPP | ||
| 12 | +#define CATLASS_EPILOGUE_BLOCK_BLOCK_EPILOGUE_DECOMPOSITION_GM_HPP | ||
| 13 | + | ||
| 14 | +#include "../../matmul_emu_catlass.hpp" | ||
| 15 | +#include "../../arch/matmul_emu_arch.hpp" | ||
| 16 | +#include "../../gemm/matmul_emu_gemm_type.hpp" | ||
| 17 | +#include "../../matmul_emu_gemm_coord.hpp" | ||
| 18 | +#include "../../../tla/matmul_emu_layout.hpp" | ||
| 19 | + | ||
| 20 | +namespace Catlass::Epilogue::Tile { | ||
| 21 | + | ||
| 22 | +template <class ArchTag_, class ComputeType_, uint32_t COMPUTE_LENGTH_> | ||
| 23 | +struct TileElemWiseMuls { | ||
| 24 | + using ArchTag = ArchTag_; | ||
| 25 | + using ElementCompute = typename ComputeType_::Element; | ||
| 26 | + static constexpr uint32_t COMPUTE_LENGTH = COMPUTE_LENGTH_; | ||
| 27 | + | ||
| 28 | + CATLASS_DEVICE | ||
| 29 | + TileElemWiseMuls() {} | ||
| 30 | + | ||
| 31 | + CATLASS_DEVICE | ||
| 32 | + void operator()(AscendC::LocalTensor<ElementCompute> dstLocal, AscendC::LocalTensor<ElementCompute> srcTensor, | ||
| 33 | + ElementCompute scalar) | ||
| 34 | + { | ||
| 35 | + AscendC::Muls(dstLocal, srcTensor, scalar, COMPUTE_LENGTH); | ||
| 36 | + } | ||
| 37 | +}; | ||
| 38 | + | ||
| 39 | +template <class ArchTag_, class ComputeType_, uint32_t COMPUTE_LENGTH_> | ||
| 40 | +struct TileElemWiseAdd { | ||
| 41 | + using ArchTag = ArchTag_; | ||
| 42 | + using ElementCompute = typename ComputeType_::Element; | ||
| 43 | + static constexpr uint32_t COMPUTE_LENGTH = COMPUTE_LENGTH_; | ||
| 44 | + | ||
| 45 | + CATLASS_DEVICE | ||
| 46 | + TileElemWiseAdd() {} | ||
| 47 | + | ||
| 48 | + CATLASS_DEVICE | ||
| 49 | + void operator()(AscendC::LocalTensor<ElementCompute> const& ubOut, | ||
| 50 | + AscendC::LocalTensor<ElementCompute> const& ubIn0, | ||
| 51 | + AscendC::LocalTensor<ElementCompute> const& ubIn1) | ||
| 52 | + { | ||
| 53 | + AscendC::Add(ubOut, ubIn0, ubIn1, COMPUTE_LENGTH); | ||
| 54 | + } | ||
| 55 | +}; | ||
| 56 | + | ||
| 57 | +template <class ArchTag, class TensorSrc, class TensorDst, class Enable = void> | ||
| 58 | +struct CopyGm2UbTla { | ||
| 59 | + static_assert(DEPENDENT_FALSE<ArchTag>, "Unsupported CopyGm2UbTla, can not find the specialization."); | ||
| 60 | +}; | ||
| 61 | + | ||
| 62 | +template <class ElementSrc, class ElementDst, class LayoutSrc, class LayoutDst, class CoordSrc, class CoordDst> | ||
| 63 | +struct CopyGm2UbTla< | ||
| 64 | + Arch::AtlasA2, tla::Tensor<AscendC::GlobalTensor<ElementSrc>, LayoutSrc, CoordSrc, AscendC::TPosition::GM>, | ||
| 65 | + tla::Tensor<AscendC::LocalTensor<ElementDst>, LayoutDst, CoordDst, AscendC::TPosition::VECCALC>, | ||
| 66 | + std::enable_if_t<tla::detail::isRowMajor<LayoutSrc>::value && tla::detail::isRowMajor<LayoutDst>::value>> { | ||
| 67 | + static constexpr uint32_t ELE_NUM_PER_BLK = BYTE_PER_BLK / sizeof(ElementSrc); | ||
| 68 | + | ||
| 69 | + CATLASS_DEVICE | ||
| 70 | + CopyGm2UbTla() = default; | ||
| 71 | + | ||
| 72 | + template <class TensorDst, class TensorSrc> | ||
| 73 | + CATLASS_DEVICE void operator()(TensorDst const& dstTensor, TensorSrc const& srcTensor) | ||
| 74 | + { | ||
| 75 | + AscendC::DataCopyExtParams dataCopyParams( | ||
| 76 | + tla::get<0>(srcTensor.originShape()), tla::get<1>(srcTensor.originShape()) * sizeof(ElementSrc), | ||
| 77 | + (tla::get<0>(srcTensor.stride()) - tla::get<1>(srcTensor.originShape())) * sizeof(ElementSrc), | ||
| 78 | + (tla::get<0>(dstTensor.stride()) - tla::get<1>(srcTensor.originShape())) / ELE_NUM_PER_BLK, 0); | ||
| 79 | + AscendC::DataCopyPadExtParams<ElementSrc> padParams(false, 0, 0, 0); | ||
| 80 | + auto dstOffset = dstTensor.layout()(dstTensor.coord()); | ||
| 81 | + auto srcOffset = srcTensor.layout()(srcTensor.coord()); | ||
| 82 | + AscendC::DataCopyPad(dstTensor.data()[dstOffset], srcTensor.data()[srcOffset], dataCopyParams, padParams); | ||
| 83 | + }; | ||
| 84 | +}; | ||
| 85 | + | ||
| 86 | +template <class ArchTag, class TensorSrc, class TensorDst, class Enable = void> | ||
| 87 | +struct CopyUb2GmTla { | ||
| 88 | + static_assert(DEPENDENT_FALSE<ArchTag>, "Unsupported CopyUb2GmTla, can not find the specialization."); | ||
| 89 | +}; | ||
| 90 | + | ||
| 91 | +template <class ElementSrc, class ElementDst, class LayoutSrc, class LayoutDst, class CoordSrc, class CoordDst> | ||
| 92 | +struct CopyUb2GmTla< | ||
| 93 | + Arch::AtlasA2, tla::Tensor<AscendC::LocalTensor<ElementSrc>, LayoutSrc, CoordSrc, AscendC::TPosition::VECCALC>, | ||
| 94 | + tla::Tensor<AscendC::GlobalTensor<ElementDst>, LayoutDst, CoordDst, AscendC::TPosition::GM>, | ||
| 95 | + std::enable_if_t<tla::detail::isRowMajor<LayoutSrc>::value && tla::detail::isRowMajor<LayoutDst>::value>> { | ||
| 96 | + static constexpr uint32_t ELE_NUM_PER_C0 = BYTE_PER_C0 / sizeof(ElementSrc); | ||
| 97 | + | ||
| 98 | + CATLASS_DEVICE | ||
| 99 | + CopyUb2GmTla() = default; | ||
| 100 | + | ||
| 101 | + template <class TensorDst, class TensorSrc> | ||
| 102 | + CATLASS_DEVICE void operator()(TensorDst const& dstTensor, TensorSrc const& srcTensor) | ||
| 103 | + { | ||
| 104 | + AscendC::DataCopyExtParams dataCopyParams( | ||
| 105 | + tla::get<0>(dstTensor.originShape()), tla::get<1>(dstTensor.originShape()) * sizeof(ElementSrc), | ||
| 106 | + (tla::get<0>(srcTensor.stride()) - tla::get<1>(dstTensor.originShape())) / ELE_NUM_PER_C0, | ||
| 107 | + (tla::get<0>(dstTensor.stride()) - tla::get<1>(dstTensor.originShape())) * sizeof(ElementSrc), 0); | ||
| 108 | + auto dstOffset = dstTensor.layout()(dstTensor.coord()); | ||
| 109 | + auto srcOffset = srcTensor.layout()(srcTensor.coord()); | ||
| 110 | + AscendC::DataCopyPad(dstTensor.data()[dstOffset], srcTensor.data()[srcOffset], dataCopyParams); | ||
| 111 | + }; | ||
| 112 | +}; | ||
| 113 | + | ||
| 114 | +} // namespace Catlass::Epilogue::Tile | ||
| 115 | + | ||
| 116 | +namespace Catlass::Epilogue::Block { | ||
| 117 | + | ||
| 118 | +template <class ArchTag_, class TileM_, class TileN_, class ElementC_, class LayoutC_, class LayoutTagA_ = void, | ||
| 119 | + class LayoutTagB_ = void> | ||
| 120 | +struct BlockEpilogueDecompositionGm { | ||
| 121 | + static constexpr bool USE_GM_WORKSPACE = true; | ||
| 122 | + using ArchTag = ArchTag_; | ||
| 123 | + using ElementC = ElementC_; | ||
| 124 | + using LayoutC = LayoutC_; | ||
| 125 | + | ||
| 126 | + static constexpr uint32_t L1_TILE_M = TileM_::value; | ||
| 127 | + static constexpr uint32_t L1_TILE_N = TileN_::value; | ||
| 128 | + // Default scale follows the aclnnMatmulEmuSplitWeight constraint (wLowScale = 1/256); | ||
| 129 | + // may be overridden per-call through Params::scale. | ||
| 130 | + static constexpr float DEFAULT_SCALE = 1.0f / 256.0f; | ||
| 131 | + | ||
| 132 | + // Each AIV subcore (dual AIV) physically holds half of the tile rows. | ||
| 133 | + static constexpr uint32_t SUB_M = L1_TILE_M / 2u; | ||
| 134 | + static constexpr uint32_t STRIDE_C = RoundUp<Catlass::BYTE_PER_C0>(L1_TILE_N); | ||
| 135 | + static constexpr uint32_t SUBBLOCK_ELEMS = SUB_M * STRIDE_C; | ||
| 136 | + static constexpr uint32_t PER_ACCUM_BYTES = SUBBLOCK_ELEMS * sizeof(ElementC); | ||
| 137 | + | ||
| 138 | + // Chunk length for the in-place vector fusion; must divide the full subblock. | ||
| 139 | + static constexpr uint32_t COMPUTE_LENGTH = 8192u; | ||
| 140 | + | ||
| 141 | + static_assert(COMPUTE_LENGTH % Catlass::BYTE_PER_C0 == 0, "COMPUTE_LENGTH must be divisible by BYTE_PER_C0"); | ||
| 142 | + static_assert(SUBBLOCK_ELEMS % COMPUTE_LENGTH == 0, "Full L1 subblock elems must be a multiple of COMPUTE_LENGTH"); | ||
| 143 | + // UB: two full-size GM staging accumulators (ubY0 | ubY1), in-place fusion (no ubOut). | ||
| 144 | + // Full-size staging lets the whole Y0 subblock be pulled GM->UB while the AIC runs the | ||
| 145 | + // second mmad (Y1): 2 * SUBBLOCK_ELEMS * 4 = 128KB for <128,256> on 192KB UB. | ||
| 146 | + static_assert(2u * PER_ACCUM_BYTES <= ArchTag::UB_SIZE, | ||
| 147 | + "UB budget overflow: 2 full-size accumulators exceed UB_SIZE. Fallback to smaller L1 tile."); | ||
| 148 | + | ||
| 149 | + using ComputeType = Catlass::Gemm::GemmType<ElementC, Catlass::layout::RowMajor>; | ||
| 150 | + using TileMuls = Catlass::Epilogue::Tile::TileElemWiseMuls<ArchTag, ComputeType, COMPUTE_LENGTH>; | ||
| 151 | + using TileAdd = Catlass::Epilogue::Tile::TileElemWiseAdd<ArchTag, ComputeType, COMPUTE_LENGTH>; | ||
| 152 | + | ||
| 153 | + struct Params { | ||
| 154 | + GM_ADDR ptrY; | ||
| 155 | + LayoutC layoutY; | ||
| 156 | + float scale; | ||
| 157 | + | ||
| 158 | + CATLASS_HOST_DEVICE | ||
| 159 | + Params() : scale(DEFAULT_SCALE) {} | ||
| 160 | + | ||
| 161 | + CATLASS_HOST_DEVICE | ||
| 162 | + Params(GM_ADDR ptrY_, LayoutC const& layoutY_, float scale_ = DEFAULT_SCALE) | ||
| 163 | + : ptrY(ptrY_), layoutY(layoutY_), scale(scale_) | ||
| 164 | + {} | ||
| 165 | + }; | ||
| 166 | + | ||
| 167 | + CATLASS_DEVICE | ||
| 168 | + BlockEpilogueDecompositionGm(Catlass::Arch::Resource<ArchTag>& resource, Params const& params) | ||
| 169 | + : params_(params), resource_(resource) | ||
| 170 | + { | ||
| 171 | + if ASCEND_IS_AIV { | ||
| 172 | + // Prime the event semaphores: no pending V, MTE3, nor MTE3->MTE2 dependency yet. | ||
| 173 | + AscendC::SetFlag<AscendC::HardEvent::V_MTE2>(EVENT_ID0); | ||
| 174 | + AscendC::SetFlag<AscendC::HardEvent::MTE3_V>(EVENT_ID0); | ||
| 175 | + AscendC::SetFlag<AscendC::HardEvent::MTE3_MTE2>(EVENT_ID0); | ||
| 176 | + } | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + CATLASS_DEVICE | ||
| 180 | + ~BlockEpilogueDecompositionGm() | ||
| 181 | + { | ||
| 182 | + if ASCEND_IS_AIV { | ||
| 183 | + AscendC::WaitFlag<AscendC::HardEvent::V_MTE2>(EVENT_ID0); | ||
| 184 | + AscendC::WaitFlag<AscendC::HardEvent::MTE3_V>(EVENT_ID0); | ||
| 185 | + AscendC::WaitFlag<AscendC::HardEvent::MTE3_MTE2>(EVENT_ID0); | ||
| 186 | + } | ||
| 187 | + } | ||
| 188 | + | ||
| 189 | + // Phase 1 (MTE2): Y0 GM->UB. Issued as soon as the AIC stages Y0 into the GM workspace | ||
| 190 | + // (flagY0Ready), so this copy overlaps with the AIC's second mmad (Y1). | ||
| 191 | + CATLASS_DEVICE | ||
| 192 | + void LoadY0(Catlass::GemmCoord const& actualBlockShapeMNK, AscendC::GlobalTensor<ElementC> const& gmBlockY0, | ||
| 193 | + LayoutC const& layoutBlock) | ||
| 194 | + { | ||
| 195 | + SubblockLayout sub = MakeSubblockLayout(actualBlockShapeMNK, layoutBlock); | ||
| 196 | + if (!sub.valid) { | ||
| 197 | + return; | ||
| 198 | + } | ||
| 199 | + AscendC::LocalTensor<ElementC> ubY0 = resource_.ubBuf.template GetBufferByByte<ElementC>(0); | ||
| 200 | + | ||
| 201 | + // Gate MTE2: the previous tile's UB->GM writeback (MTE3) must be complete and the | ||
| 202 | + // in-place vector fusion must be done before reusing ubY0. | ||
| 203 | + AscendC::WaitFlag<AscendC::HardEvent::MTE3_MTE2>(EVENT_ID0); | ||
| 204 | + AscendC::WaitFlag<AscendC::HardEvent::V_MTE2>(EVENT_ID0); | ||
| 205 | + | ||
| 206 | + auto gmY0Layout = tla::MakeLayout(tla::MakeShape(sub.rows, sub.cols), | ||
| 207 | + tla::MakeStride(sub.gmRowStride, tla::Int<1>{})); | ||
| 208 | + auto ubY0Layout = tla::MakeLayout(tla::MakeShape(sub.rows, sub.cols), | ||
| 209 | + tla::MakeStride(sub.strideC, tla::Int<1>{})); | ||
| 210 | + auto gmY0Tile = tla::MakeTensor(gmBlockY0[sub.subblockRow * sub.gmRowStride], gmY0Layout, | ||
| 211 | + Catlass::Arch::PositionGM{}); | ||
| 212 | + auto ubY0Tile = tla::MakeTensor(ubY0, ubY0Layout, Catlass::Arch::PositionUB{}); | ||
| 213 | + | ||
| 214 | + Catlass::Epilogue::Tile::CopyGm2UbTla<ArchTag, decltype(gmY0Tile), decltype(ubY0Tile)> copyGm2Ub; | ||
| 215 | + copyGm2Ub(ubY0Tile, gmY0Tile); | ||
| 216 | + } | ||
| 217 | + | ||
| 218 | + // Phase 2 (MTE2): Y1 GM->UB. | ||
| 219 | + CATLASS_DEVICE | ||
| 220 | + void LoadY1(Catlass::GemmCoord const& actualBlockShapeMNK, AscendC::GlobalTensor<ElementC> const& gmBlockY1, | ||
| 221 | + LayoutC const& layoutBlock) | ||
| 222 | + { | ||
| 223 | + SubblockLayout sub = MakeSubblockLayout(actualBlockShapeMNK, layoutBlock); | ||
| 224 | + if (!sub.valid) { | ||
| 225 | + return; | ||
| 226 | + } | ||
| 227 | + AscendC::LocalTensor<ElementC> ubY1 = resource_.ubBuf.template GetBufferByByte<ElementC>(PER_ACCUM_BYTES); | ||
| 228 | + | ||
| 229 | + auto gmY1Layout = tla::MakeLayout(tla::MakeShape(sub.rows, sub.cols), | ||
| 230 | + tla::MakeStride(sub.gmRowStride, tla::Int<1>{})); | ||
| 231 | + auto ubY1Layout = tla::MakeLayout(tla::MakeShape(sub.rows, sub.cols), | ||
| 232 | + tla::MakeStride(sub.strideC, tla::Int<1>{})); | ||
| 233 | + auto gmY1Tile = tla::MakeTensor(gmBlockY1[sub.subblockRow * sub.gmRowStride], gmY1Layout, | ||
| 234 | + Catlass::Arch::PositionGM{}); | ||
| 235 | + auto ubY1Tile = tla::MakeTensor(ubY1, ubY1Layout, Catlass::Arch::PositionUB{}); | ||
| 236 | + | ||
| 237 | + Catlass::Epilogue::Tile::CopyGm2UbTla<ArchTag, decltype(gmY1Tile), decltype(ubY1Tile)> copyGm2Ub; | ||
| 238 | + copyGm2Ub(ubY1Tile, gmY1Tile); | ||
| 239 | + // Both Y0/Y1 are now in UB: release the vector pipe. | ||
| 240 | + AscendC::SetFlag<AscendC::HardEvent::MTE2_V>(EVENT_ID0); | ||
| 241 | + } | ||
| 242 | + | ||
| 243 | + // Phase 3 (V + MTE3): in-place Y = Y0 + scale * Y1 on the padded row-major buffers, | ||
| 244 | + // then UB->GM writeback of the (rows x cols) subblock. | ||
| 245 | + CATLASS_DEVICE | ||
| 246 | + void FuseAndStore(Catlass::GemmCoord const& blockShapeMNK, Catlass::GemmCoord const& blockCoordMNK, | ||
| 247 | + Catlass::GemmCoord const& actualBlockShapeMNK, LayoutC const& layoutBlock) | ||
| 248 | + { | ||
| 249 | + using Catlass::MatrixCoord; | ||
| 250 | + | ||
| 251 | + SubblockLayout sub = MakeSubblockLayout(actualBlockShapeMNK, layoutBlock); | ||
| 252 | + if (!sub.valid) { | ||
| 253 | + return; | ||
| 254 | + } | ||
| 255 | + | ||
| 256 | + AscendC::LocalTensor<ElementC> ubY0 = resource_.ubBuf.template GetBufferByByte<ElementC>(0); | ||
| 257 | + AscendC::LocalTensor<ElementC> ubY1 = resource_.ubBuf.template GetBufferByByte<ElementC>(PER_ACCUM_BYTES); | ||
| 258 | + | ||
| 259 | + MatrixCoord blockShape = blockShapeMNK.GetCoordMN(); | ||
| 260 | + MatrixCoord blockCoord = blockCoordMNK.GetCoordMN(); | ||
| 261 | + MatrixCoord blockOffset = blockCoord * blockShape; | ||
| 262 | + | ||
| 263 | + AscendC::GlobalTensor<ElementC> gmY; | ||
| 264 | + gmY.SetGlobalBuffer((__gm__ ElementC*)params_.ptrY); | ||
| 265 | + auto tensorY = tla::MakeTensor(gmY, params_.layoutY, Catlass::Arch::PositionGM{}); | ||
| 266 | + auto gmTileY = GetTile(tensorY, tla::MakeCoord(blockOffset.row() + sub.subblockRow, blockOffset.column()), | ||
| 267 | + tla::MakeShape(sub.rows, sub.cols)); | ||
| 268 | + | ||
| 269 | + TileMuls tileMuls; | ||
| 270 | + TileAdd tileAdd; | ||
| 271 | + | ||
| 272 | + // Wait for both GM->UB loads (MTE2) and the previous UB->GM writeback (MTE3). | ||
| 273 | + AscendC::WaitFlag<AscendC::HardEvent::MTE2_V>(EVENT_ID0); | ||
| 274 | + AscendC::WaitFlag<AscendC::HardEvent::MTE3_V>(EVENT_ID0); | ||
| 275 | + // In-place fusion: Y1 = scale * Y1, Y0 = Y0 + Y1. Vector tiles step by COMPUTE_LENGTH | ||
| 276 | + // (may process padding past totalElems, still inside the full-size subblock buffers). | ||
| 277 | + for (uint32_t off = 0; off < sub.totalElems; off += COMPUTE_LENGTH) { | ||
| 278 | + tileMuls(ubY1[off], ubY1[off], params_.scale); | ||
| 279 | + tileAdd(ubY0[off], ubY0[off], ubY1[off]); | ||
| 280 | + } | ||
| 281 | + AscendC::SetFlag<AscendC::HardEvent::V_MTE2>(EVENT_ID0); | ||
| 282 | + AscendC::SetFlag<AscendC::HardEvent::V_MTE3>(EVENT_ID0); | ||
| 283 | + | ||
| 284 | + auto ubY0Layout = tla::MakeLayout(tla::MakeShape(sub.rows, sub.cols), | ||
| 285 | + tla::MakeStride(sub.strideC, tla::Int<1>{})); | ||
| 286 | + auto ubY0Tile = tla::MakeTensor(ubY0, ubY0Layout, Catlass::Arch::PositionUB{}); | ||
| 287 | + | ||
| 288 | + Catlass::Epilogue::Tile::CopyUb2GmTla<ArchTag, decltype(ubY0Tile), decltype(gmTileY)> copyUb2Gm; | ||
| 289 | + AscendC::WaitFlag<AscendC::HardEvent::V_MTE3>(EVENT_ID0); | ||
| 290 | + copyUb2Gm(gmTileY, ubY0Tile); | ||
| 291 | + // Writeback done: release MTE3->V (next tile's fusion) and MTE3->MTE2 (next tile's load). | ||
| 292 | + AscendC::SetFlag<AscendC::HardEvent::MTE3_V>(EVENT_ID0); | ||
| 293 | + AscendC::SetFlag<AscendC::HardEvent::MTE3_MTE2>(EVENT_ID0); | ||
| 294 | + } | ||
| 295 | + | ||
| 296 | +private: | ||
| 297 | + struct SubblockLayout { | ||
| 298 | + uint32_t rows = 0; | ||
| 299 | + uint32_t cols = 0; | ||
| 300 | + uint32_t strideC = 0; | ||
| 301 | + uint32_t totalElems = 0; | ||
| 302 | + uint32_t gmRowStride = 0; | ||
| 303 | + uint32_t subblockRow = 0; | ||
| 304 | + bool valid = false; | ||
| 305 | + }; | ||
| 306 | + | ||
| 307 | + CATLASS_DEVICE SubblockLayout MakeSubblockLayout(Catlass::GemmCoord const& actualBlockShapeMNK, | ||
| 308 | + LayoutC const& layoutBlock) const | ||
| 309 | + { | ||
| 310 | + using Catlass::MatrixCoord; | ||
| 311 | + MatrixCoord actualBlockShape = actualBlockShapeMNK.GetCoordMN(); | ||
| 312 | + uint32_t subBlockNum = static_cast<uint32_t>(AscendC::GetSubBlockNum()); | ||
| 313 | + MatrixCoord subblockShape{CeilDiv(actualBlockShape.row(), subBlockNum), actualBlockShape.column()}; | ||
| 314 | + MatrixCoord subblockCoord{static_cast<uint32_t>(AscendC::GetSubBlockIdx()), 0}; | ||
| 315 | + MatrixCoord actualSubblockShape = MatrixCoord::Min(subblockShape, | ||
| 316 | + actualBlockShape - subblockCoord * subblockShape); | ||
| 317 | + MatrixCoord subblockOffset = subblockCoord * subblockShape; | ||
| 318 | + | ||
| 319 | + SubblockLayout sub; | ||
| 320 | + if (actualSubblockShape.row() == 0 || actualSubblockShape.column() == 0) { | ||
| 321 | + return sub; // valid == false | ||
| 322 | + } | ||
| 323 | + sub.rows = actualSubblockShape.row(); | ||
| 324 | + sub.cols = actualSubblockShape.column(); | ||
| 325 | + sub.strideC = RoundUp<Catlass::BYTE_PER_C0>(sub.cols); | ||
| 326 | + sub.totalElems = sub.rows * sub.strideC; | ||
| 327 | + sub.gmRowStride = static_cast<uint32_t>(tla::get<0>(layoutBlock.stride())); | ||
| 328 | + sub.subblockRow = subblockOffset.row(); | ||
| 329 | + sub.valid = true; | ||
| 330 | + return sub; | ||
| 331 | + } | ||
| 332 | + | ||
| 333 | + static constexpr int32_t EVENT_ID0 = 0; | ||
| 334 | + | ||
| 335 | + Params params_; | ||
| 336 | + Catlass::Arch::Resource<ArchTag>& resource_; | ||
| 337 | +}; | ||
| 338 | + | ||
| 339 | +} // namespace Catlass::Epilogue::Block | ||
| 340 | + | ||
| 341 | +#endif // CATLASS_EPILOGUE_BLOCK_BLOCK_EPILOGUE_DECOMPOSITION_GM_HPP | ||
| @@ -0,0 +1,326 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025-2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * 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. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#ifndef MATMUL_EMU_CATLASS_GEMM_BLOCK_BLOCK_MMAD_HPP | ||
| 12 | +#define MATMUL_EMU_CATLASS_GEMM_BLOCK_BLOCK_MMAD_HPP | ||
| 13 | + | ||
| 14 | +#include "../../matmul_emu_catlass.hpp" | ||
| 15 | +#include "../matmul_emu_dispatch_policy.hpp" | ||
| 16 | +#include "../tile/matmul_emu_tile_copy.hpp" | ||
| 17 | +#include "../../matmul_emu_gemm_coord.hpp" | ||
| 18 | +#include "../../../tla/matmul_emu_layout.hpp" | ||
| 19 | + | ||
| 20 | +namespace Catlass::Gemm::Block { | ||
| 21 | + | ||
| 22 | +template < | ||
| 23 | + class DispatchPolicy, class L1TileShape, class L0TileShape, class ElementA, class ElementB, class ElementC, | ||
| 24 | + class ElementBias = void, | ||
| 25 | + class TileCopy = Gemm::Tile::PackedTileCopyTla<typename DispatchPolicy::ArchTag, ElementA, layout::RowMajor, | ||
| 26 | + ElementB, layout::RowMajor, ElementC, layout::RowMajor, ElementBias>, | ||
| 27 | + class TileMmad = Gemm::Tile::TileMmadTla<typename DispatchPolicy::ArchTag, ElementA, | ||
| 28 | + typename TileCopy::LayoutTagL1A> > | ||
| 29 | +struct BlockMmadTla { | ||
| 30 | + static_assert(DEPENDENT_FALSE<DispatchPolicy>, "BlockMmadTla is not implemented for this DispatchPolicy"); | ||
| 31 | +}; | ||
| 32 | + | ||
| 33 | +template <class ArchTag_, bool ENABLE_UNIT_FLAG_, bool USE_HF32_MODE_, uint32_t L0C_STAGES_, bool ENABLE_L1_RESIDENT_, | ||
| 34 | + uint32_t L1A_STAGES_, uint32_t L1B_STAGES_, uint32_t L0A_STAGES_, uint32_t L0B_STAGES_, class L1TileShape_, | ||
| 35 | + class L0TileShape_, class ElementA_, class ElementB_, class ElementC_, class ElementBias_, class TileCopy_, | ||
| 36 | + class TileMmad_> | ||
| 37 | +struct BlockMmadTla<MmadPingpong<ArchTag_, ENABLE_UNIT_FLAG_, USE_HF32_MODE_, L0C_STAGES_, ENABLE_L1_RESIDENT_, | ||
| 38 | + L1A_STAGES_, L1B_STAGES_, L0A_STAGES_, L0B_STAGES_>, | ||
| 39 | + L1TileShape_, L0TileShape_, ElementA_, ElementB_, ElementC_, ElementBias_, TileCopy_, TileMmad_> { | ||
| 40 | +public: | ||
| 41 | + // Type Aliases | ||
| 42 | + using DispatchPolicy = MmadPingpong<ArchTag_, ENABLE_UNIT_FLAG_, USE_HF32_MODE_, L0C_STAGES_, ENABLE_L1_RESIDENT_, | ||
| 43 | + L1A_STAGES_, L1B_STAGES_, L0A_STAGES_, L0B_STAGES_>; | ||
| 44 | + using ArchTag = typename DispatchPolicy::ArchTag; | ||
| 45 | + using TileCopy = TileCopy_; | ||
| 46 | + using L1TileShape = L1TileShape_; | ||
| 47 | + using L0TileShape = L0TileShape_; | ||
| 48 | + using ElementA = ElementA_; | ||
| 49 | + using LayoutA = typename TileCopy::LayoutA; | ||
| 50 | + using ElementB = ElementB_; | ||
| 51 | + using LayoutB = typename TileCopy::LayoutB; | ||
| 52 | + using ElementC = ElementC_; | ||
| 53 | + using LayoutC = typename TileCopy::LayoutC; | ||
| 54 | + using ElementBias = ElementBias_; | ||
| 55 | + | ||
| 56 | + using TileMmad = TileMmad_; | ||
| 57 | + | ||
| 58 | + using CopyL1ToL0A = typename TileCopy::CopyL1ToL0A; | ||
| 59 | + using CopyL1ToL0B = typename TileCopy::CopyL1ToL0B; | ||
| 60 | + using CopyL1ToBT = typename TileCopy::CopyL1ToBT; | ||
| 61 | + | ||
| 62 | + using ElementAccumulator = typename TileCopy::ElementAccumulator; | ||
| 63 | + | ||
| 64 | + static constexpr bool HAS_BIAS = TileCopy::HAS_BIAS; | ||
| 65 | + | ||
| 66 | + using LayoutTagL1A = typename TileCopy::LayoutTagL1A; | ||
| 67 | + using LayoutTagL1B = typename TileCopy::LayoutTagL1B; | ||
| 68 | + using LayoutTagL0A = typename TileCopy::LayoutTagL0A; | ||
| 69 | + using LayoutTagL0B = typename TileCopy::LayoutTagL0B; | ||
| 70 | + | ||
| 71 | + using L1AAlignHelper = typename TileCopy_::L1AAlignHelper; | ||
| 72 | + using L1BAlignHelper = typename TileCopy_::L1BAlignHelper; | ||
| 73 | + | ||
| 74 | + static_assert(tla::is_tuple<L1TileShape>::value && tla::is_static<L1TileShape>::value, | ||
| 75 | + "L1TileShape must be tla::tuple and static!"); | ||
| 76 | + static_assert(tla::is_tuple<L0TileShape>::value && tla::is_static<L0TileShape>::value, | ||
| 77 | + "L0TileShape must be tla::tuple and static!"); | ||
| 78 | + | ||
| 79 | + static constexpr bool ENABLE_UNIT_FLAG = DispatchPolicy::ENABLE_UNIT_FLAG; | ||
| 80 | + static constexpr bool USE_HF32_MODE = DispatchPolicy::USE_HF32_MODE; | ||
| 81 | + static constexpr bool ENABLE_L1_RESIDENT = DispatchPolicy::ENABLE_L1_RESIDENT; | ||
| 82 | + static constexpr uint32_t L1A_STAGES = DispatchPolicy::L1A_STAGES; | ||
| 83 | + static constexpr uint32_t L1B_STAGES = DispatchPolicy::L1B_STAGES; | ||
| 84 | + static constexpr uint32_t L0A_STAGES = DispatchPolicy::L0A_STAGES; | ||
| 85 | + static constexpr uint32_t L0B_STAGES = DispatchPolicy::L0B_STAGES; | ||
| 86 | + static constexpr uint32_t L0C_STAGES = DispatchPolicy::L0C_STAGES; | ||
| 87 | + static constexpr uint32_t L1_TILE_M = tla::get<0>(L1TileShape{}); | ||
| 88 | + static constexpr uint32_t L1_TILE_N = tla::get<1>(L1TileShape{}); | ||
| 89 | + static constexpr uint32_t L1_TILE_K = tla::get<2>(L1TileShape{}); | ||
| 90 | + static constexpr uint32_t L0_TILE_M = tla::get<0>(L0TileShape{}); | ||
| 91 | + static constexpr uint32_t L0_TILE_N = tla::get<1>(L0TileShape{}); | ||
| 92 | + static constexpr uint32_t L0_TILE_K = tla::get<2>(L0TileShape{}); | ||
| 93 | + | ||
| 94 | + // L1 tile size | ||
| 95 | + static constexpr uint32_t L1A_TILE_SIZE = L1_TILE_M * L1_TILE_K * sizeof(ElementA); | ||
| 96 | + static constexpr uint32_t L1B_TILE_SIZE = L1_TILE_N * L1_TILE_K * sizeof(ElementB); | ||
| 97 | + // L0 tile size | ||
| 98 | + static constexpr uint32_t L0A_TILE_SIZE = L0_TILE_M * L0_TILE_K * sizeof(ElementA); | ||
| 99 | + static constexpr uint32_t L0B_TILE_SIZE = L0_TILE_K * L0_TILE_N * sizeof(ElementB); | ||
| 100 | + static constexpr uint32_t L0C_TILE_SIZE = L1_TILE_M * L1_TILE_N * sizeof(ElementAccumulator); | ||
| 101 | + | ||
| 102 | + // Check HF32_MODE | ||
| 103 | + static_assert(!USE_HF32_MODE || | ||
| 104 | + (USE_HF32_MODE && std::is_same_v<ElementA, float> && std::is_same_v<ElementB, float>), | ||
| 105 | + "HF32 MODE only supports in float!"); | ||
| 106 | + | ||
| 107 | + // Check L0C_STAGES | ||
| 108 | + static_assert(!(ENABLE_UNIT_FLAG && L0C_STAGES != 1), "L0C_STAGES must be 1 when UnitFlag is true!"); | ||
| 109 | + | ||
| 110 | + // Check LayoutC | ||
| 111 | + static_assert(tla::detail::isRowMajor<LayoutC>::value || | ||
| 112 | + ((std::is_same_v<ElementC, half> || std::is_same_v<ElementC, bfloat16_t> || | ||
| 113 | + std::is_same_v<ElementC, float>) && | ||
| 114 | + tla::detail::iszN<ElementC, LayoutC>::value), | ||
| 115 | + "LayoutC only supports zN in half or bfloat16 or float, RowMajor in all dtype yet!"); | ||
| 116 | + | ||
| 117 | + // Check L1TileShape | ||
| 118 | + static_assert(L1A_TILE_SIZE * L1A_STAGES + L1B_TILE_SIZE * L1B_STAGES <= ArchTag::L1_SIZE, | ||
| 119 | + "L1TileShape exceeding the L1 space!"); | ||
| 120 | + | ||
| 121 | + // Check L0TileShape | ||
| 122 | + static_assert(L0A_TILE_SIZE * L0A_STAGES <= ArchTag::L0A_SIZE, "L0TileShape exceeding the L0A space!"); | ||
| 123 | + static_assert(L0B_TILE_SIZE * L0B_STAGES <= ArchTag::L0B_SIZE, "L0TileShape exceeding the L0B space!"); | ||
| 124 | + static_assert(L0C_TILE_SIZE * L0C_STAGES <= ArchTag::L0C_SIZE, "L0TileShape exceeding the L0C space!"); | ||
| 125 | + | ||
| 126 | + static constexpr uint32_t _32B = 32 * 8; // in bits | ||
| 127 | + static_assert(L1_TILE_M == L0_TILE_M && L1_TILE_N == L0_TILE_N, | ||
| 128 | + "The situation where the basic blocks of L1 and L0 differ on the m and n axes is not supported yet"); | ||
| 129 | + static_assert(L0_TILE_K <= L1_TILE_K, "L0TileShape::K cannot exceed L1TileShape::K"); | ||
| 130 | +#if (defined(CATLASS_ARCH) && CATLASS_ARCH == 2201) | ||
| 131 | + static_assert(L1_TILE_M * SizeOfBits<ElementA>::value % _32B == 0, "L1TileShape::M must be 32B aligned."); | ||
| 132 | + static_assert(L1_TILE_K * SizeOfBits<ElementA>::value % _32B == 0, "L1TileShape::K must be 32B aligned."); | ||
| 133 | + static_assert(L1_TILE_K * SizeOfBits<ElementB>::value % _32B == 0, "L1TileShape::K must be 32B aligned."); | ||
| 134 | + static_assert(L1_TILE_N * SizeOfBits<ElementB>::value % _32B == 0, "L1TileShape::N must be 32B aligned."); | ||
| 135 | + static_assert(L0_TILE_K * SizeOfBits<ElementB>::value % _32B == 0, "L0TileShape::K must be 32B aligned."); | ||
| 136 | +#endif | ||
| 137 | + | ||
| 138 | + static_assert((!HAS_BIAS && (L1A_STAGES + L1B_STAGES) <= 8) || (HAS_BIAS && (L1A_STAGES + L1B_STAGES) <= 7), | ||
| 139 | + "L1 Buffer overflow: Exceeds the supported range of EVENT(0~7)"); | ||
| 140 | + | ||
| 141 | + static_assert((!HAS_BIAS && (L0A_STAGES + L0B_STAGES) <= 8) || (HAS_BIAS && (L0A_STAGES + L0B_STAGES) <= 7), | ||
| 142 | + "L0 Buffer overflow: Exceeds the supported range of EVENT_ID(0~7)"); | ||
| 143 | + | ||
| 144 | + static constexpr auto L1A_LAYOUT = tla::MakeLayout<ElementA, LayoutTagL1A>(tla::Int<L1_TILE_M>{}, | ||
| 145 | + tla::Int<L1_TILE_K>{}); | ||
| 146 | + static constexpr auto L1B_LAYOUT = tla::MakeLayout<ElementB, LayoutTagL1B>(tla::Int<L1_TILE_K>{}, | ||
| 147 | + tla::Int<L1_TILE_N>{}); | ||
| 148 | + static constexpr auto L1BIAS_LAYOUT = tla::MakeLayout(tla::Int<L1_TILE_N>{}); | ||
| 149 | + static constexpr auto L0BIAS_LAYOUT = tla::MakeLayout(tla::Int<L0_TILE_N>{}); | ||
| 150 | + | ||
| 151 | + // When enabling L1 resident mode, restore the pointer and coordinates that record the last state | ||
| 152 | + // to the initial state. if two blockmmad instances need to be consecutively invoked at the kernel layer, | ||
| 153 | + // RestoreStatus() must be inserted between them. | ||
| 154 | + CATLASS_DEVICE | ||
| 155 | + void RestoreStatus() | ||
| 156 | + { | ||
| 157 | + for (int i = 0; i < L1A_STAGES; ++i) { | ||
| 158 | + lastAddrA[i] = nullptr; | ||
| 159 | + lastCoordA[i] = MatrixCoord{0U, 0U}; | ||
| 160 | + } | ||
| 161 | + for (int i = 0; i < L1B_STAGES; ++i) { | ||
| 162 | + lastAddrB[i] = nullptr; | ||
| 163 | + lastCoordB[i] = MatrixCoord{0U, 0U}; | ||
| 164 | + } | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + /// Construct | ||
| 168 | + CATLASS_DEVICE | ||
| 169 | + BlockMmadTla(Arch::Resource<ArchTag>& resource, uint32_t l1BufAddrStart = 0) | ||
| 170 | + { | ||
| 171 | + if ASCEND_IS_AIC { | ||
| 172 | + if constexpr (USE_HF32_MODE) { | ||
| 173 | + AscendC::SetHF32Mode(true); | ||
| 174 | + } else { | ||
| 175 | + AscendC::SetHF32Mode(false); | ||
| 176 | + } | ||
| 177 | + if constexpr (ENABLE_UNIT_FLAG && tla::detail::isRowMajor<LayoutC>::value) { | ||
| 178 | + AscendC::SetMMLayoutTransform(true); | ||
| 179 | + } | ||
| 180 | + InitAicBuffers(resource, l1BufAddrStart); | ||
| 181 | + if constexpr (ENABLE_L1_RESIDENT) { | ||
| 182 | + RestoreStatus(); | ||
| 183 | + } | ||
| 184 | + } | ||
| 185 | + } | ||
| 186 | + | ||
| 187 | + /// Destructor | ||
| 188 | + CATLASS_DEVICE | ||
| 189 | + ~BlockMmadTla() | ||
| 190 | + { | ||
| 191 | + if ASCEND_IS_AIC { | ||
| 192 | + if constexpr (USE_HF32_MODE) { | ||
| 193 | + AscendC::SetHF32Mode(false); | ||
| 194 | + } | ||
| 195 | + if constexpr (ENABLE_UNIT_FLAG && tla::detail::isRowMajor<LayoutC>::value) { | ||
| 196 | + AscendC::SetMMLayoutTransform(false); | ||
| 197 | + } | ||
| 198 | + for (uint32_t i = 0; i < L1A_STAGES; i++) { | ||
| 199 | + AscendC::WaitFlag<AscendC::HardEvent::MTE1_MTE2>(l1AEventList[i]); | ||
| 200 | + } | ||
| 201 | + for (uint32_t i = 0; i < L1B_STAGES; i++) { | ||
| 202 | + AscendC::WaitFlag<AscendC::HardEvent::MTE1_MTE2>(l1BEventList[i]); | ||
| 203 | + } | ||
| 204 | + for (uint32_t i = 0; i < L0A_STAGES; i++) { | ||
| 205 | + AscendC::WaitFlag<AscendC::HardEvent::M_MTE1>(l0AEventList[i]); | ||
| 206 | + } | ||
| 207 | + for (uint32_t i = 0; i < L0B_STAGES; i++) { | ||
| 208 | + AscendC::WaitFlag<AscendC::HardEvent::M_MTE1>(l0BEventList[i]); | ||
| 209 | + } | ||
| 210 | + if constexpr (!ENABLE_UNIT_FLAG) { | ||
| 211 | + for (uint32_t i = 0; i < L0C_STAGES; i++) { | ||
| 212 | + AscendC::WaitFlag<AscendC::HardEvent::FIX_M>(l0CEventList[i]); | ||
| 213 | + } | ||
| 214 | + } | ||
| 215 | + if constexpr (HAS_BIAS) { | ||
| 216 | + AscendC::WaitFlag<AscendC::HardEvent::MTE1_MTE2>(L1A_STAGES + L1B_STAGES); | ||
| 217 | + AscendC::WaitFlag<AscendC::HardEvent::M_MTE1>(L0A_STAGES + L0B_STAGES); | ||
| 218 | + } | ||
| 219 | + } | ||
| 220 | + } | ||
| 221 | + | ||
| 222 | + /// Perform a block-scoped matrix multiply-accumulate | ||
| 223 | + template <class TensorA, class TensorB, class TensorC, class TensorBias = EmptyClass> | ||
| 224 | + CATLASS_DEVICE void operator()(TensorA& tensorA, TensorB& tensorB, TensorC& tensorC, GemmCoord const& actualShape, | ||
| 225 | + TensorBias const& tensorBias = {}) | ||
| 226 | + { | ||
| 227 | + CheckBiasL1Space(); | ||
| 228 | + using CopyGmToL1A = typename TileCopy_::template CopyGmToL1A<TensorA>; | ||
| 229 | + using CopyGmToL1B = typename TileCopy_::template CopyGmToL1B<TensorB>; | ||
| 230 | + CopyGmToL1A copyGmToL1A; | ||
| 231 | + CopyGmToL1B copyGmToL1B; | ||
| 232 | +#if (defined(CATLASS_ARCH) && CATLASS_ARCH == 2201) | ||
| 233 | + using CopyL0CToGm = typename TileCopy_::template CopyL0CToGm<TensorC>; | ||
| 234 | + CopyL0CToGm copyL0CToDst; | ||
| 235 | +#endif | ||
| 236 | + | ||
| 237 | + uint32_t mBlockActual = actualShape.m(); | ||
| 238 | + uint32_t kBlockActual = actualShape.k(); | ||
| 239 | + uint32_t nBlockActual = actualShape.n(); | ||
| 240 | + uint32_t mL1Actual = AdjustML1Actual(mBlockActual); | ||
| 241 | + uint32_t nL1Actual = nBlockActual; | ||
| 242 | + | ||
| 243 | + auto layoutInL0C = tla::MakeLayoutL0C(mL1Actual, nL1Actual); | ||
| 244 | + auto tensorL0C = tla::MakeTensor(l0CTensorList[l0CListId], layoutInL0C, Arch::PositionL0C{}); | ||
| 245 | + auto tensorL0Bias = tla::MakeTensor(l0BiasTensor, L0BIAS_LAYOUT, Arch::PositionBias{}); | ||
| 246 | + | ||
| 247 | + uint32_t kL1Actual = Min(kBlockActual, L1_TILE_K); | ||
| 248 | + LoadFirstL1Tiles(copyGmToL1A, copyGmToL1B, tensorA, tensorB, mBlockActual, nBlockActual, kL1Actual); | ||
| 249 | + LoadFirstBias(tensorBias); | ||
| 250 | + if constexpr (!ENABLE_UNIT_FLAG) { | ||
| 251 | + AscendC::WaitFlag<AscendC::HardEvent::FIX_M>(l0CEventList[l0CListId]); | ||
| 252 | + } | ||
| 253 | + | ||
| 254 | + uint32_t mL0Loop = CeilDiv<L0_TILE_M>(mL1Actual); | ||
| 255 | + uint32_t nL0Loop = CeilDiv<L0_TILE_N>(nL1Actual); | ||
| 256 | + uint32_t kL1Loop = CeilDiv<L1_TILE_K>(kBlockActual); | ||
| 257 | + for (uint32_t kL1Idx = 0; kL1Idx < kL1Loop; kL1Idx++) { | ||
| 258 | + uint32_t l1AListIdNext = (l1AListId + 1 < L1A_STAGES) ? (l1AListId + 1) : 0; | ||
| 259 | + uint32_t l1BListIdNext = (l1BListId + 1 < L1B_STAGES) ? (l1BListId + 1) : 0; | ||
| 260 | + uint32_t kL1ActualNext = 0; | ||
| 261 | + if (kL1Idx < kL1Loop - 1) { | ||
| 262 | + kL1ActualNext = PreloadNextL1(copyGmToL1A, copyGmToL1B, tensorA, tensorB, kL1Idx, kL1Loop, kBlockActual, | ||
| 263 | + mBlockActual, nBlockActual, l1AListIdNext, l1BListIdNext); | ||
| 264 | + } | ||
| 265 | + ComputeCurrentKL1(tensorL0C, tensorL0Bias, tensorBias, mL1Actual, nL1Actual, kL1Actual, kL1Idx, kL1Loop, | ||
| 266 | + mL0Loop, nL0Loop); | ||
| 267 | + l1AListId = l1AListIdNext; | ||
| 268 | + l1BListId = l1BListIdNext; | ||
| 269 | + kL1Actual = kL1ActualNext; | ||
| 270 | + } | ||
| 271 | + | ||
| 272 | +#if (defined(CATLASS_ARCH) && CATLASS_ARCH == 2201) | ||
| 273 | + StoreBlockOut(copyL0CToDst, tensorC, tensorL0C); | ||
| 274 | +#endif | ||
| 275 | + } | ||
| 276 | + | ||
| 277 | +protected: | ||
| 278 | +#include "matmul_emu_block_mmad_impl.hpp" | ||
| 279 | + | ||
| 280 | + template <class TensorA> | ||
| 281 | + CATLASS_DEVICE auto GetTileA(TensorA& tensorA, uint32_t mIndex, uint32_t kIndex, uint32_t mSize, uint32_t kSize) | ||
| 282 | + { | ||
| 283 | + if constexpr (tla::detail::isVector<LayoutA>::value) { | ||
| 284 | + return GetTile(tensorA, tla::MakeCoord(kIndex), tla::MakeShape(kSize)); | ||
| 285 | + } else { | ||
| 286 | + return GetTile(tensorA, tla::MakeCoord(mIndex, kIndex), tla::MakeShape(mSize, kSize)); | ||
| 287 | + } | ||
| 288 | + } | ||
| 289 | + | ||
| 290 | + // Multi-stage tensors list | ||
| 291 | + AscendC::LocalTensor<ElementA> l1ATensorList[L1A_STAGES]; | ||
| 292 | + AscendC::LocalTensor<ElementB> l1BTensorList[L1B_STAGES]; | ||
| 293 | + AscendC::LocalTensor<ElementA> l0ATensorList[L0A_STAGES]; | ||
| 294 | + AscendC::LocalTensor<ElementB> l0BTensorList[L0B_STAGES]; | ||
| 295 | + AscendC::LocalTensor<ElementAccumulator> l0CTensorList[L0C_STAGES]; | ||
| 296 | + AscendC::LocalTensor<uint8_t> l1BiasTensor; | ||
| 297 | + AscendC::LocalTensor<ElementAccumulator> l0BiasTensor; | ||
| 298 | + | ||
| 299 | + // Multi-stage event id list | ||
| 300 | + int32_t l1AEventList[L1A_STAGES]; | ||
| 301 | + int32_t l1BEventList[L1B_STAGES]; | ||
| 302 | + int32_t l0AEventList[L0A_STAGES]; | ||
| 303 | + int32_t l0BEventList[L0B_STAGES]; | ||
| 304 | + int32_t l0CEventList[L0C_STAGES]; | ||
| 305 | + | ||
| 306 | + __gm__ typename AscendC::GlobalTensor<ElementA>::PrimType* lastAddrA[L1A_STAGES]; | ||
| 307 | + __gm__ typename AscendC::GlobalTensor<ElementB>::PrimType* lastAddrB[L1B_STAGES]; | ||
| 308 | + MatrixCoord lastCoordA[L1A_STAGES]; | ||
| 309 | + MatrixCoord lastCoordB[L1B_STAGES]; | ||
| 310 | + | ||
| 311 | + // The id of current stage | ||
| 312 | + uint32_t l1AListId{0}; | ||
| 313 | + uint32_t l1BListId{0}; | ||
| 314 | + uint32_t l0AListId{0}; | ||
| 315 | + uint32_t l0BListId{0}; | ||
| 316 | + uint32_t l0CListId{0}; | ||
| 317 | + | ||
| 318 | + TileMmad tileMmad; | ||
| 319 | + CopyL1ToL0A copyL1ToL0A; | ||
| 320 | + CopyL1ToL0B copyL1ToL0B; | ||
| 321 | + CopyL1ToBT copyL1ToBT; | ||
| 322 | +}; | ||
| 323 | + | ||
| 324 | +} // namespace Catlass::Gemm::Block | ||
| 325 | + | ||
| 326 | +#endif // MATMUL_EMU_CATLASS_GEMM_BLOCK_BLOCK_MMAD_HPP | ||
Amatmul/matmul_emu_split_weight/op_kernel/arch22/catlass/gemm/block/matmul_emu_block_mmad_impl.hpp+301-0
| @@ -0,0 +1,301 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025-2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * 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. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#ifndef MATMUL_EMU_CATLASS_GEMM_BLOCK_BLOCK_MMAD_IMPL_HPP | ||
| 12 | +#define MATMUL_EMU_CATLASS_GEMM_BLOCK_BLOCK_MMAD_IMPL_HPP | ||
| 13 | + | ||
| 14 | +CATLASS_DEVICE | ||
| 15 | +void CheckBiasL1Space() | ||
| 16 | +{ | ||
| 17 | + if constexpr (HAS_BIAS) { | ||
| 18 | + static constexpr uint32_t BIAS_BUF_SIZE = L0_TILE_N * sizeof(ElementAccumulator); | ||
| 19 | + static constexpr uint32_t L1BIAS_SIZE = L1_TILE_N * sizeof(ElementBias); | ||
| 20 | + static_assert(BIAS_BUF_SIZE <= ArchTag::BIAS_SIZE, "BIAS_BUF_SIZE exceeding the BT space! Reduce L0_TILE_N"); | ||
| 21 | + static_assert(L1A_TILE_SIZE * L1A_STAGES + L1B_TILE_SIZE * L1B_STAGES + L1BIAS_SIZE <= ArchTag::L1_SIZE, | ||
| 22 | + "L1TileShape exceeding the L1 space!"); | ||
| 23 | + } | ||
| 24 | +} | ||
| 25 | + | ||
| 26 | +CATLASS_DEVICE | ||
| 27 | +uint32_t AdjustML1Actual(uint32_t mBlockActual) | ||
| 28 | +{ | ||
| 29 | + uint32_t mL1Actual = mBlockActual; | ||
| 30 | + if constexpr (std::is_same_v<ArchTag, Arch::AtlasA2>) { | ||
| 31 | + if (mL1Actual == 1) { | ||
| 32 | + mL1Actual = 16; | ||
| 33 | + } | ||
| 34 | + } | ||
| 35 | + return mL1Actual; | ||
| 36 | +} | ||
| 37 | + | ||
| 38 | +CATLASS_DEVICE | ||
| 39 | +void InitAicBuffers(Arch::Resource<ArchTag>& resource, uint32_t l1BufAddrStart) | ||
| 40 | +{ | ||
| 41 | + uint32_t l1AOffset = l1BufAddrStart; | ||
| 42 | + uint32_t l1BOffset = l1BufAddrStart + L1A_TILE_SIZE * L1A_STAGES; | ||
| 43 | + for (uint32_t i = 0; i < L1A_STAGES; i++) { | ||
| 44 | + l1ATensorList[i] = resource.l1Buf.template GetBufferByByte<ElementA>(l1AOffset + L1A_TILE_SIZE * i); | ||
| 45 | + l1AEventList[i] = i; | ||
| 46 | + AscendC::SetFlag<AscendC::HardEvent::MTE1_MTE2>(l1AEventList[i]); | ||
| 47 | + } | ||
| 48 | + for (uint32_t i = 0; i < L1B_STAGES; i++) { | ||
| 49 | + l1BTensorList[i] = resource.l1Buf.template GetBufferByByte<ElementB>(l1BOffset + L1B_TILE_SIZE * i); | ||
| 50 | + l1BEventList[i] = i + L1A_STAGES; | ||
| 51 | + AscendC::SetFlag<AscendC::HardEvent::MTE1_MTE2>(l1BEventList[i]); | ||
| 52 | + } | ||
| 53 | + for (uint32_t i = 0; i < L0A_STAGES; i++) { | ||
| 54 | + l0ATensorList[i] = resource.l0ABuf.template GetBufferByByte<ElementA>(L0A_TILE_SIZE * i); | ||
| 55 | + l0AEventList[i] = i; | ||
| 56 | + AscendC::SetFlag<AscendC::HardEvent::M_MTE1>(l0AEventList[i]); | ||
| 57 | + } | ||
| 58 | + for (uint32_t i = 0; i < L0B_STAGES; i++) { | ||
| 59 | + l0BTensorList[i] = resource.l0BBuf.template GetBufferByByte<ElementB>(L0B_TILE_SIZE * i); | ||
| 60 | + l0BEventList[i] = i + L0A_STAGES; | ||
| 61 | + AscendC::SetFlag<AscendC::HardEvent::M_MTE1>(l0BEventList[i]); | ||
| 62 | + } | ||
| 63 | + if constexpr (!ENABLE_UNIT_FLAG) { | ||
| 64 | + for (uint32_t i = 0; i < L0C_STAGES; i++) { | ||
| 65 | + l0CTensorList[i] = resource.l0CBuf.template GetBufferByByte<ElementAccumulator>(L0C_TILE_SIZE * i); | ||
| 66 | + l0CEventList[i] = i; | ||
| 67 | + AscendC::SetFlag<AscendC::HardEvent::FIX_M>(l0CEventList[i]); | ||
| 68 | + } | ||
| 69 | + } else { | ||
| 70 | + l0CTensorList[0] = resource.l0CBuf.template GetBufferByByte<ElementAccumulator>(0); | ||
| 71 | + } | ||
| 72 | + if constexpr (HAS_BIAS) { | ||
| 73 | + uint32_t l1BiasOffset = l1BOffset + L1B_TILE_SIZE * L1B_STAGES; | ||
| 74 | + l1BiasTensor = resource.l1Buf.template GetBufferByByte<uint8_t>(l1BiasOffset); | ||
| 75 | + l0BiasTensor = resource.btBuf.template GetBufferByByte<ElementAccumulator>(0); | ||
| 76 | + AscendC::SetFlag<AscendC::HardEvent::MTE1_MTE2>(L1A_STAGES + L1B_STAGES); | ||
| 77 | + AscendC::SetFlag<AscendC::HardEvent::M_MTE1>(L0A_STAGES + L0B_STAGES); | ||
| 78 | + } | ||
| 79 | +} | ||
| 80 | + | ||
| 81 | +template <class Element, class CopyFn, class TensorL1, class TensorTile> | ||
| 82 | +CATLASS_DEVICE void CopyGmToL1MaybeResident(CopyFn& copyFn, TensorL1 const& dst, TensorTile const& src, | ||
| 83 | + __gm__ typename AscendC::GlobalTensor<Element>::PrimType** lastAddr, | ||
| 84 | + MatrixCoord* lastCoord, uint32_t listId) | ||
| 85 | +{ | ||
| 86 | + if constexpr (ENABLE_L1_RESIDENT) { | ||
| 87 | + if (lastAddr[listId] != src.data().GetPhyAddr() || tla::get<0>(src.coord()) != lastCoord[listId].row() || | ||
| 88 | + tla::get<1>(src.coord()) != lastCoord[listId].column()) { | ||
| 89 | + copyFn(dst, src); | ||
| 90 | + lastCoord[listId] = MatrixCoord{tla::get<0>(src.coord()), tla::get<1>(src.coord())}; | ||
| 91 | + lastAddr[listId] = const_cast<__gm__ typename AscendC::GlobalTensor<Element>::PrimType*>( | ||
| 92 | + src.data().GetPhyAddr()); | ||
| 93 | + } | ||
| 94 | + } else { | ||
| 95 | + copyFn(dst, src); | ||
| 96 | + } | ||
| 97 | +} | ||
| 98 | + | ||
| 99 | +template <class CopyGmToL1A, class CopyGmToL1B, class TensorA, class TensorB> | ||
| 100 | +CATLASS_DEVICE void LoadFirstL1Tiles(CopyGmToL1A& copyGmToL1A, CopyGmToL1B& copyGmToL1B, TensorA& tensorA, | ||
| 101 | + TensorB& tensorB, uint32_t mBlockActual, uint32_t nBlockActual, uint32_t kL1Actual) | ||
| 102 | +{ | ||
| 103 | + AscendC::WaitFlag<AscendC::HardEvent::MTE1_MTE2>(l1AEventList[l1AListId]); | ||
| 104 | + auto tensorL1A = tla::MakeTensor(l1ATensorList[l1AListId], L1A_LAYOUT, Arch::PositionL1{}); | ||
| 105 | + auto tensorTileA = GetTileA(tensorA, 0, 0, mBlockActual, kL1Actual); | ||
| 106 | + CopyGmToL1MaybeResident<ElementA>(copyGmToL1A, tensorL1A, tensorTileA, lastAddrA, lastCoordA, l1AListId); | ||
| 107 | + AscendC::SetFlag<AscendC::HardEvent::MTE2_MTE1>(l1AEventList[l1AListId]); | ||
| 108 | + | ||
| 109 | + AscendC::WaitFlag<AscendC::HardEvent::MTE1_MTE2>(l1BEventList[l1BListId]); | ||
| 110 | + auto tensorL1B = tla::MakeTensor(l1BTensorList[l1BListId], L1B_LAYOUT, Arch::PositionL1{}); | ||
| 111 | + auto tensorTileB = GetTile(tensorB, tla::MakeCoord(0, 0), tla::MakeShape(kL1Actual, nBlockActual)); | ||
| 112 | + CopyGmToL1MaybeResident<ElementB>(copyGmToL1B, tensorL1B, tensorTileB, lastAddrB, lastCoordB, l1BListId); | ||
| 113 | + AscendC::SetFlag<AscendC::HardEvent::MTE2_MTE1>(l1BEventList[l1BListId]); | ||
| 114 | +} | ||
| 115 | + | ||
| 116 | +template <class TensorBias> | ||
| 117 | +CATLASS_DEVICE void LoadFirstBias(TensorBias const& tensorBias) | ||
| 118 | +{ | ||
| 119 | + if constexpr (HAS_BIAS && !std::is_same_v<TensorBias, EmptyClass>) { | ||
| 120 | + using CopyGmToL1Bias = typename TileCopy::template CopyGmToL1Bias<TensorBias>; | ||
| 121 | + CopyGmToL1Bias copyGmToL1Bias; | ||
| 122 | + AscendC::WaitFlag<AscendC::HardEvent::MTE1_MTE2>(L1A_STAGES + L1B_STAGES); | ||
| 123 | + auto l1Bias = l1BiasTensor.template ReinterpretCast<ElementBias>(); | ||
| 124 | + auto tensorL1Bias = tla::MakeTensor(l1Bias, L1BIAS_LAYOUT, Arch::PositionL1{}); | ||
| 125 | + copyGmToL1Bias(tensorL1Bias, tensorBias); | ||
| 126 | + AscendC::SetFlag<AscendC::HardEvent::MTE2_MTE1>(L1A_STAGES + L1B_STAGES); | ||
| 127 | + } | ||
| 128 | +} | ||
| 129 | + | ||
| 130 | +template <class CopyGmToL1A, class CopyGmToL1B, class TensorA, class TensorB> | ||
| 131 | +CATLASS_DEVICE uint32_t PreloadNextL1(CopyGmToL1A& copyGmToL1A, CopyGmToL1B& copyGmToL1B, TensorA& tensorA, | ||
| 132 | + TensorB& tensorB, uint32_t kL1Idx, uint32_t kL1Loop, uint32_t kBlockActual, | ||
| 133 | + uint32_t mBlockActual, uint32_t nBlockActual, uint32_t l1AListIdNext, | ||
| 134 | + uint32_t l1BListIdNext) | ||
| 135 | +{ | ||
| 136 | + uint32_t kL1IdxNext = kL1Idx + 1; | ||
| 137 | + uint32_t kL1ActualNext = (kL1IdxNext < kL1Loop - 1) ? L1_TILE_K : (kBlockActual - kL1IdxNext * L1_TILE_K); | ||
| 138 | + | ||
| 139 | + auto tensorL1A = tla::MakeTensor(l1ATensorList[l1AListIdNext], L1A_LAYOUT, Arch::PositionL1{}); | ||
| 140 | + auto tensorL1B = tla::MakeTensor(l1BTensorList[l1BListIdNext], L1B_LAYOUT, Arch::PositionL1{}); | ||
| 141 | + auto tensorTileA = GetTileA(tensorA, 0, kL1IdxNext * L1_TILE_K, mBlockActual, kL1ActualNext); | ||
| 142 | + auto tensorTileB = GetTile(tensorB, tla::MakeCoord(kL1IdxNext * L1_TILE_K, 0), | ||
| 143 | + tla::MakeShape(kL1ActualNext, nBlockActual)); | ||
| 144 | + | ||
| 145 | + AscendC::WaitFlag<AscendC::HardEvent::MTE1_MTE2>(l1AEventList[l1AListIdNext]); | ||
| 146 | + CopyGmToL1MaybeResident<ElementA>(copyGmToL1A, tensorL1A, tensorTileA, lastAddrA, lastCoordA, l1AListIdNext); | ||
| 147 | + AscendC::SetFlag<AscendC::HardEvent::MTE2_MTE1>(l1AEventList[l1AListIdNext]); | ||
| 148 | + | ||
| 149 | + AscendC::WaitFlag<AscendC::HardEvent::MTE1_MTE2>(l1BEventList[l1BListIdNext]); | ||
| 150 | + CopyGmToL1MaybeResident<ElementB>(copyGmToL1B, tensorL1B, tensorTileB, lastAddrB, lastCoordB, l1BListIdNext); | ||
| 151 | + AscendC::SetFlag<AscendC::HardEvent::MTE2_MTE1>(l1BEventList[l1BListIdNext]); | ||
| 152 | + return kL1ActualNext; | ||
| 153 | +} | ||
| 154 | + | ||
| 155 | +template <class TensorL0C, class TensorL0Bias, class TensorBias> | ||
| 156 | +CATLASS_DEVICE void ComputeCurrentKL1(TensorL0C& tensorL0C, TensorL0Bias& tensorL0Bias, TensorBias const& tensorBias, | ||
| 157 | + uint32_t mL1Actual, uint32_t nL1Actual, uint32_t kL1Actual, uint32_t kL1Idx, | ||
| 158 | + uint32_t kL1Loop, uint32_t mL0Loop, uint32_t nL0Loop) | ||
| 159 | +{ | ||
| 160 | + auto tensorL1A = tla::MakeTensor(l1ATensorList[l1AListId], L1A_LAYOUT, Arch::PositionL1{}); | ||
| 161 | + auto tensorL1B = tla::MakeTensor(l1BTensorList[l1BListId], L1B_LAYOUT, Arch::PositionL1{}); | ||
| 162 | + uint32_t kL0Loop = CeilDiv<L0_TILE_K>(kL1Actual); | ||
| 163 | + for (int mL0Idx = 0; mL0Idx < mL0Loop; mL0Idx++) { | ||
| 164 | + uint32_t mL0Actual = (mL0Idx < mL0Loop - 1) ? L0_TILE_M : (mL1Actual - mL0Idx * L0_TILE_M); | ||
| 165 | + ComputeKL0Loop(tensorL0C, tensorL0Bias, tensorBias, tensorL1A, tensorL1B, mL0Idx, mL0Actual, mL0Loop, kL0Loop, | ||
| 166 | + kL1Actual, kL1Idx, kL1Loop, nL1Actual, nL0Loop); | ||
| 167 | + } | ||
| 168 | +} | ||
| 169 | + | ||
| 170 | +template <class TensorL0C, class TensorL0Bias, class TensorBias, class TensorL1A, class TensorL1B> | ||
| 171 | +CATLASS_DEVICE void ComputeKL0Loop(TensorL0C& tensorL0C, TensorL0Bias& tensorL0Bias, TensorBias const& tensorBias, | ||
| 172 | + TensorL1A& tensorL1A, TensorL1B& tensorL1B, int mL0Idx, uint32_t mL0Actual, | ||
| 173 | + uint32_t mL0Loop, uint32_t kL0Loop, uint32_t kL1Actual, uint32_t kL1Idx, | ||
| 174 | + uint32_t kL1Loop, uint32_t nL1Actual, uint32_t nL0Loop) | ||
| 175 | +{ | ||
| 176 | + for (int kL0Idx = 0; kL0Idx < kL0Loop; kL0Idx++) { | ||
| 177 | + uint32_t kL0Actual = (kL0Idx < kL0Loop - 1) ? L0_TILE_K : (kL1Actual - kL0Idx * L0_TILE_K); | ||
| 178 | + auto l0ATile = l0ATensorList[l0AListId]; | ||
| 179 | + auto layoutAInL0 = tla::MakeLayout<ElementA, LayoutTagL0A>(mL0Actual, kL0Actual); | ||
| 180 | + auto tensorL0A = tla::MakeTensor(l0ATile, layoutAInL0, Arch::PositionL0A{}); | ||
| 181 | + auto tensorTileL1A = GetTileA(tensorL1A, mL0Idx * L0_TILE_M, kL0Idx * L0_TILE_K, mL0Actual, kL0Actual); | ||
| 182 | + | ||
| 183 | + AscendC::WaitFlag<AscendC::HardEvent::M_MTE1>(l0AEventList[l0AListId]); | ||
| 184 | + if ((mL0Idx == 0) && (kL0Idx == 0)) { | ||
| 185 | + AscendC::WaitFlag<AscendC::HardEvent::MTE2_MTE1>(l1AEventList[l1AListId]); | ||
| 186 | + } | ||
| 187 | + copyL1ToL0A(tensorL0A, tensorTileL1A); | ||
| 188 | + if ((mL0Idx == mL0Loop - 1) && (kL0Idx == kL0Loop - 1)) { | ||
| 189 | + AscendC::SetFlag<AscendC::HardEvent::MTE1_MTE2>(l1AEventList[l1AListId]); | ||
| 190 | + } | ||
| 191 | + | ||
| 192 | + bool initC = ((kL1Idx == 0) && (kL0Idx == 0)); | ||
| 193 | + ComputeNL0Loop(tensorL0C, tensorL0Bias, tensorBias, tensorL0A, tensorL1B, mL0Idx, mL0Actual, mL0Loop, kL0Idx, | ||
| 194 | + kL0Actual, kL0Loop, kL1Idx, kL1Loop, nL1Actual, nL0Loop, initC); | ||
| 195 | + AscendC::SetFlag<AscendC::HardEvent::M_MTE1>(l0AEventList[l0AListId]); | ||
| 196 | + l0AListId = (l0AListId + 1 < L0A_STAGES) ? (l0AListId + 1) : 0; | ||
| 197 | + } | ||
| 198 | +} | ||
| 199 | + | ||
| 200 | +template <class TensorL0C, class TensorL0Bias, class TensorBias, class TensorL0A, class TensorL1B> | ||
| 201 | +CATLASS_DEVICE void ComputeNL0Loop(TensorL0C& tensorL0C, TensorL0Bias& tensorL0Bias, TensorBias const& tensorBias, | ||
| 202 | + TensorL0A& tensorL0A, TensorL1B& tensorL1B, int mL0Idx, uint32_t mL0Actual, | ||
| 203 | + uint32_t mL0Loop, int kL0Idx, uint32_t kL0Actual, uint32_t kL0Loop, uint32_t kL1Idx, | ||
| 204 | + uint32_t kL1Loop, uint32_t nL1Actual, uint32_t nL0Loop, bool initC) | ||
| 205 | +{ | ||
| 206 | + (void)tensorBias; | ||
| 207 | + for (int nL0Idx = 0; nL0Idx < nL0Loop; nL0Idx++) { | ||
| 208 | + uint32_t nL0Actual = (nL0Idx < nL0Loop - 1) ? L0_TILE_N : (nL1Actual - nL0Idx * L0_TILE_N); | ||
| 209 | + auto l0BTile = l0BTensorList[l0BListId]; | ||
| 210 | + auto layoutBInL0 = tla::MakeLayout<ElementB, LayoutTagL0B>(kL0Actual, nL0Actual); | ||
| 211 | + auto tensorL0B = tla::MakeTensor(l0BTile, layoutBInL0, Arch::PositionL0B{}); | ||
| 212 | + auto tensorTileL1B = GetTile(tensorL1B, tla::MakeCoord(kL0Idx * L0_TILE_K, nL0Idx * L0_TILE_N), | ||
| 213 | + tla::MakeShape(kL0Actual, nL0Actual)); | ||
| 214 | + | ||
| 215 | + AscendC::WaitFlag<AscendC::HardEvent::M_MTE1>(l0BEventList[l0BListId]); | ||
| 216 | + if ((mL0Idx == 0) && (kL0Idx == 0) && (nL0Idx == 0)) { | ||
| 217 | + AscendC::WaitFlag<AscendC::HardEvent::MTE2_MTE1>(l1BEventList[l1BListId]); | ||
| 218 | + } | ||
| 219 | + copyL1ToL0B(tensorL0B, tensorTileL1B); | ||
| 220 | + if ((mL0Idx == mL0Loop - 1) && (kL0Idx == kL0Loop - 1) && (nL0Idx == nL0Loop - 1)) { | ||
| 221 | + AscendC::SetFlag<AscendC::HardEvent::MTE1_MTE2>(l1BEventList[l1BListId]); | ||
| 222 | + } | ||
| 223 | + | ||
| 224 | + LoadBiasToL0<TensorBias>(tensorL0Bias, nL0Idx, nL0Actual, nL0Loop, initC); | ||
| 225 | + | ||
| 226 | + AscendC::SetFlag<AscendC::HardEvent::MTE1_M>(l0CEventList[l0CListId]); | ||
| 227 | + auto tensorTileL0C = GetTile(tensorL0C, tla::MakeCoord(mL0Idx * L0_TILE_M, nL0Idx * L0_TILE_N), | ||
| 228 | + tla::MakeShape(mL0Actual, nL0Actual)); | ||
| 229 | + AscendC::WaitFlag<AscendC::HardEvent::MTE1_M>(l0CEventList[l0CListId]); | ||
| 230 | + | ||
| 231 | + uint8_t unitFlag = 0b00; | ||
| 232 | + if constexpr (ENABLE_UNIT_FLAG) { | ||
| 233 | + if ((kL1Idx == kL1Loop - 1) && (mL0Idx == mL0Loop - 1) && (kL0Idx == kL0Loop - 1) && | ||
| 234 | + (nL0Idx == nL0Loop - 1)) { | ||
| 235 | + unitFlag = 0b11; | ||
| 236 | + } else { | ||
| 237 | + unitFlag = 0b10; | ||
| 238 | + } | ||
| 239 | + } | ||
| 240 | + IssueMmad<TensorBias>(tensorTileL0C, tensorL0A, tensorL0B, tensorL0Bias, mL0Actual, nL0Actual, kL0Actual, initC, | ||
| 241 | + unitFlag); | ||
| 242 | + AscendC::SetFlag<AscendC::HardEvent::M_MTE1>(l0BEventList[l0BListId]); | ||
| 243 | + l0BListId = (l0BListId + 1 < L0B_STAGES) ? (l0BListId + 1) : 0; | ||
| 244 | + } | ||
| 245 | +} | ||
| 246 | + | ||
| 247 | +template <class TensorBias, class TensorL0Bias> | ||
| 248 | +CATLASS_DEVICE void LoadBiasToL0(TensorL0Bias& tensorL0Bias, int nL0Idx, uint32_t nL0Actual, uint32_t nL0Loop, | ||
| 249 | + bool initC) | ||
| 250 | +{ | ||
| 251 | + if constexpr (HAS_BIAS && !std::is_same_v<TensorBias, EmptyClass>) { | ||
| 252 | + if (initC) { | ||
| 253 | + if (nL0Idx == 0) { | ||
| 254 | + AscendC::WaitFlag<AscendC::HardEvent::MTE2_MTE1>(L1A_STAGES + L1B_STAGES); | ||
| 255 | + } | ||
| 256 | + AscendC::WaitFlag<AscendC::HardEvent::M_MTE1>(L0A_STAGES + L0B_STAGES); | ||
| 257 | + auto l1Bias = l1BiasTensor.template ReinterpretCast<ElementBias>(); | ||
| 258 | + auto tensorL1Bias = tla::MakeTensor(l1Bias, L1BIAS_LAYOUT, Arch::PositionL1{}); | ||
| 259 | + auto tensorTileL1Bias = GetTile(tensorL1Bias, tla::MakeCoord(nL0Idx * L0_TILE_N), | ||
| 260 | + tla::MakeShape(nL0Actual)); | ||
| 261 | + copyL1ToBT(tensorL0Bias, tensorTileL1Bias); | ||
| 262 | + if (nL0Idx == nL0Loop - 1) { | ||
| 263 | + AscendC::SetFlag<AscendC::HardEvent::MTE1_MTE2>(L1A_STAGES + L1B_STAGES); | ||
| 264 | + } | ||
| 265 | + } | ||
| 266 | + } | ||
| 267 | +} | ||
| 268 | + | ||
| 269 | +template <class TensorBias, class TensorTileL0C, class TensorL0A, class TensorL0B, class TensorL0Bias> | ||
| 270 | +CATLASS_DEVICE void IssueMmad(TensorTileL0C const& tensorTileL0C, TensorL0A const& tensorL0A, | ||
| 271 | + TensorL0B const& tensorL0B, TensorL0Bias const& tensorL0Bias, uint32_t mL0Actual, | ||
| 272 | + uint32_t nL0Actual, uint32_t kL0Actual, bool initC, uint8_t unitFlag) | ||
| 273 | +{ | ||
| 274 | + if constexpr (HAS_BIAS && !std::is_same_v<TensorBias, EmptyClass>) { | ||
| 275 | + if (initC) { | ||
| 276 | + tileMmad(tensorTileL0C, tensorL0A, tensorL0B, tensorL0Bias, mL0Actual, nL0Actual, kL0Actual, initC, | ||
| 277 | + unitFlag); | ||
| 278 | + AscendC::SetFlag<AscendC::HardEvent::M_MTE1>(L0A_STAGES + L0B_STAGES); | ||
| 279 | + } else { | ||
| 280 | + tileMmad(tensorTileL0C, tensorL0A, tensorL0B, mL0Actual, nL0Actual, kL0Actual, initC, unitFlag); | ||
| 281 | + } | ||
| 282 | + } else { | ||
| 283 | + tileMmad(tensorTileL0C, tensorL0A, tensorL0B, mL0Actual, nL0Actual, kL0Actual, initC, unitFlag); | ||
| 284 | + } | ||
| 285 | +} | ||
| 286 | + | ||
| 287 | +template <class CopyL0CToDst, class TensorC, class TensorL0C> | ||
| 288 | +CATLASS_DEVICE void StoreBlockOut(CopyL0CToDst& copyL0CToDst, TensorC& tensorC, TensorL0C& tensorL0C) | ||
| 289 | +{ | ||
| 290 | + if constexpr (!ENABLE_UNIT_FLAG) { | ||
| 291 | + AscendC::SetFlag<AscendC::HardEvent::M_FIX>(l0CEventList[l0CListId]); | ||
| 292 | + AscendC::WaitFlag<AscendC::HardEvent::M_FIX>(l0CEventList[l0CListId]); | ||
| 293 | + copyL0CToDst(tensorC, tensorL0C); | ||
| 294 | + AscendC::SetFlag<AscendC::HardEvent::FIX_M>(l0CEventList[l0CListId]); | ||
| 295 | + l0CListId = (l0CListId + 1 < L0C_STAGES) ? (l0CListId + 1) : 0; | ||
| 296 | + } else { | ||
| 297 | + copyL0CToDst(tensorC, tensorL0C, 0b11); | ||
| 298 | + } | ||
| 299 | +} | ||
| 300 | + | ||
| 301 | +#endif // MATMUL_EMU_CATLASS_GEMM_BLOCK_BLOCK_MMAD_IMPL_HPP | ||
Amatmul/matmul_emu_split_weight/op_kernel/arch22/catlass/gemm/block/matmul_emu_block_swizzle.hpp+123-0
| @@ -0,0 +1,123 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025-2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * 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. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#ifndef MATMUL_EMU_CATLASS_GEMM_BLOCK_BLOCK_SWIZZLE_HPP | ||
| 12 | +#define MATMUL_EMU_CATLASS_GEMM_BLOCK_BLOCK_SWIZZLE_HPP | ||
| 13 | + | ||
| 14 | +#include "../../matmul_emu_catlass.hpp" | ||
| 15 | +#include "../../matmul_emu_gemm_coord.hpp" | ||
| 16 | + | ||
| 17 | +namespace Catlass::Gemm::Block { | ||
| 18 | + | ||
| 19 | +///////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 20 | + | ||
| 21 | +/// Block swizzling function for Gemms | ||
| 22 | +template <uint32_t SwizzleOffset = 1, uint32_t SwizzleDirection = 0> | ||
| 23 | +struct GemmIdentityBlockSwizzle { | ||
| 24 | + /// Data members | ||
| 25 | + | ||
| 26 | + GemmCoord problemShape; | ||
| 27 | + MatrixCoord tileMN; | ||
| 28 | + MatrixCoord loopsMN; | ||
| 29 | + | ||
| 30 | + /// Methods | ||
| 31 | + | ||
| 32 | + CATLASS_DEVICE | ||
| 33 | + GemmIdentityBlockSwizzle() {} | ||
| 34 | + | ||
| 35 | + CATLASS_DEVICE | ||
| 36 | + GemmIdentityBlockSwizzle(GemmCoord const& problemShape_, MatrixCoord const& tileMN_) | ||
| 37 | + : problemShape(problemShape_), tileMN(tileMN_) | ||
| 38 | + { | ||
| 39 | + loopsMN = CeilDiv(MatrixCoord(problemShape.GetCoordMN()), tileMN); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + CATLASS_DEVICE | ||
| 43 | + GemmIdentityBlockSwizzle(GemmCoord const& problemShape_, MatrixCoord const& tileMN_, MatrixCoord const& loopsMN_) | ||
| 44 | + : problemShape(problemShape_), tileMN(tileMN_), loopsMN(loopsMN_) | ||
| 45 | + {} | ||
| 46 | + | ||
| 47 | + CATLASS_DEVICE | ||
| 48 | + void Update(GemmCoord const& problemShape_, MatrixCoord const& tileMN_) | ||
| 49 | + { | ||
| 50 | + problemShape = problemShape_; | ||
| 51 | + tileMN = tileMN_; | ||
| 52 | + | ||
| 53 | + loopsMN = CeilDiv(MatrixCoord(problemShape.GetCoordMN()), tileMN); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + CATLASS_DEVICE | ||
| 57 | + void Update(GemmCoord const& problemShape_, MatrixCoord const& tileMN_, MatrixCoord const& loopsMN_) | ||
| 58 | + { | ||
| 59 | + problemShape = problemShape_; | ||
| 60 | + tileMN = tileMN_; | ||
| 61 | + loopsMN = loopsMN_; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + CATLASS_DEVICE | ||
| 65 | + uint32_t GetCoreLoops() const { return loopsMN.row() * loopsMN.column(); } | ||
| 66 | + | ||
| 67 | + CATLASS_DEVICE | ||
| 68 | + uint32_t GetBatchIdx(uint32_t taskIdx) { return taskIdx / (GetCoreLoops()); } | ||
| 69 | + | ||
| 70 | + CATLASS_DEVICE | ||
| 71 | + GemmCoord GetBlockCoord(uint32_t taskIdx) | ||
| 72 | + { | ||
| 73 | + uint32_t innerIdx = taskIdx % GetCoreLoops(); | ||
| 74 | + if constexpr (SwizzleDirection == 0) { // Zn | ||
| 75 | + uint32_t tileBlockLoop = CeilDiv(loopsMN.row(), SwizzleOffset); | ||
| 76 | + uint32_t tileBlockIdx = innerIdx / (SwizzleOffset * loopsMN.column()); | ||
| 77 | + uint32_t inTileBlockIdx = innerIdx % (SwizzleOffset * loopsMN.column()); | ||
| 78 | + | ||
| 79 | + uint32_t nRow = SwizzleOffset; | ||
| 80 | + if (tileBlockIdx == tileBlockLoop - 1) { | ||
| 81 | + nRow = loopsMN.row() - SwizzleOffset * tileBlockIdx; | ||
| 82 | + } | ||
| 83 | + uint32_t mIdx = tileBlockIdx * SwizzleOffset + inTileBlockIdx % nRow; | ||
| 84 | + uint32_t nIdx = inTileBlockIdx / nRow; | ||
| 85 | + if (tileBlockIdx % 2 == 1) { | ||
| 86 | + nIdx = loopsMN.column() - nIdx - 1; | ||
| 87 | + } | ||
| 88 | + return GemmCoord{mIdx, nIdx, 0}; | ||
| 89 | + } else if constexpr (SwizzleDirection == 1) { // Nz | ||
| 90 | + uint32_t tileBlockLoop = CeilDiv(loopsMN.column(), SwizzleOffset); | ||
| 91 | + uint32_t tileBlockIdx = innerIdx / (SwizzleOffset * loopsMN.row()); | ||
| 92 | + uint32_t inTileBlockIdx = innerIdx % (SwizzleOffset * loopsMN.row()); | ||
| 93 | + | ||
| 94 | + uint32_t nCol = SwizzleOffset; | ||
| 95 | + if (tileBlockIdx == tileBlockLoop - 1) { | ||
| 96 | + nCol = loopsMN.column() - SwizzleOffset * tileBlockIdx; | ||
| 97 | + } | ||
| 98 | + uint32_t mIdx = inTileBlockIdx / nCol; | ||
| 99 | + uint32_t nIdx = tileBlockIdx * SwizzleOffset + inTileBlockIdx % nCol; | ||
| 100 | + if (tileBlockIdx % 2 == 1) { | ||
| 101 | + mIdx = loopsMN.row() - mIdx - 1; | ||
| 102 | + } | ||
| 103 | + return GemmCoord{mIdx, nIdx, 0}; | ||
| 104 | + } | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + CATLASS_DEVICE | ||
| 108 | + GemmCoord GetActualBlockShape(GemmCoord blockCoord) | ||
| 109 | + { | ||
| 110 | + uint32_t mActual = (blockCoord.m() == (loopsMN.row() - 1)) ? | ||
| 111 | + (problemShape.m() - blockCoord.m() * tileMN.row()) : | ||
| 112 | + tileMN.row(); | ||
| 113 | + uint32_t nActual = (blockCoord.n() == (loopsMN.column() - 1)) ? | ||
| 114 | + (problemShape.n() - blockCoord.n() * tileMN.column()) : | ||
| 115 | + tileMN.column(); | ||
| 116 | + uint32_t kActual = problemShape.k(); | ||
| 117 | + return GemmCoord{mActual, nActual, kActual}; | ||
| 118 | + } | ||
| 119 | +}; | ||
| 120 | + | ||
| 121 | +} // namespace Catlass::Gemm::Block | ||
| 122 | + | ||
| 123 | +#endif // MATMUL_EMU_CATLASS_GEMM_BLOCK_BLOCK_SWIZZLE_HPP | ||
Amatmul/matmul_emu_split_weight/op_kernel/arch22/catlass/gemm/kernel/matmul_decomposition_gm.hpp+253-0
| @@ -0,0 +1,253 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * 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. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +// ============================================================================ | ||
| 12 | +// MatmulDecompositionGm — Atlas A2 fused Decomposition Matmul kernel. | ||
| 13 | +// | ||
| 14 | +// Single BlockScheduler loop, per output tile: | ||
| 15 | +// [AIC] two blockMmad calls (X@W_high -> GM Y0, X@W_low -> GM Y1, L0C->GM), | ||
| 16 | +// explicit FIX_M event guard between the two calls (L0C[0] WAR). | ||
| 17 | +// [AIV] blockEpilogue split into LoadY0 / LoadY1 / FuseAndStore phases: | ||
| 18 | +// GM->UB read Y0/Y1, Y = Y0 + scale*Y1, UB->GM writeback. | ||
| 19 | +// | ||
| 20 | +// CV overlap: A2/A3 has no L0C->UB path, so every mmad result is staged through GM. | ||
| 21 | +// The AIC publishes two separate flags — flagY0Ready (right after mmad0 + FIX_M guard) | ||
| 22 | +// and flagY1Ready (after mmad1). The AIV waits flagY0Ready and immediately issues the | ||
| 23 | +// Y0 GM->UB copy (MTE2), which overlaps the AIC's second mmad (Y1); it then waits | ||
| 24 | +// flagY1Ready before loading Y1. Single-direction handshake, same as | ||
| 25 | +// BasicMatmulTlaVisitor (AIC Set PIPE_FIX -> AIV Wait PIPE_MTE2). | ||
| 26 | +// Workspace = 2 * M * N * sizeof(float) (Y0 | Y1 GM staging). | ||
| 27 | +// ============================================================================ | ||
| 28 | + | ||
| 29 | +#ifndef CATLASS_GEMM_KERNEL_MATMUL_DECOMPOSITION_GM_HPP | ||
| 30 | +#define CATLASS_GEMM_KERNEL_MATMUL_DECOMPOSITION_GM_HPP | ||
| 31 | + | ||
| 32 | +#include "../../matmul_emu_catlass.hpp" | ||
| 33 | +#include "../../arch/matmul_emu_arch.hpp" | ||
| 34 | +#include "../block/matmul_emu_block_swizzle.hpp" | ||
| 35 | +#include "../tile/matmul_emu_tile_copy.hpp" | ||
| 36 | +#include "../../matmul_emu_gemm_coord.hpp" | ||
| 37 | +#include "../../layout/matmul_emu_layout.hpp" | ||
| 38 | +#include "../../../tla/matmul_emu_layout.hpp" | ||
| 39 | + | ||
| 40 | +namespace Catlass::Gemm::Kernel { | ||
| 41 | + | ||
| 42 | +namespace detail { | ||
| 43 | + | ||
| 44 | +template <class T, class = void> | ||
| 45 | +struct HasSetSwizzleParams : std::false_type {}; | ||
| 46 | + | ||
| 47 | +template <class T> | ||
| 48 | +struct HasSetSwizzleParams<T, std::void_t<decltype(std::declval<T>().SetSwizzleParams(0u, 0u))>> : std::true_type {}; | ||
| 49 | + | ||
| 50 | +} // namespace detail | ||
| 51 | + | ||
| 52 | +template <class BlockMmad_, class BlockEpilogue_, class BlockScheduler_, class LayoutTagA_ = void, | ||
| 53 | + class LayoutTagB_ = void> | ||
| 54 | +class MatmulDecompositionGm { | ||
| 55 | +public: | ||
| 56 | + using BlockMmad = BlockMmad_; | ||
| 57 | + using ArchTag = typename BlockMmad::ArchTag; | ||
| 58 | + using L1TileShape = typename BlockMmad::L1TileShape; | ||
| 59 | + using ElementA = typename BlockMmad::ElementA; | ||
| 60 | + using LayoutA = std::conditional_t<std::is_void_v<LayoutTagA_>, typename BlockMmad::LayoutA, | ||
| 61 | + Catlass::detail::TagToLayout_t<ElementA, LayoutTagA_>>; | ||
| 62 | + using ElementB = typename BlockMmad::ElementB; | ||
| 63 | + using LayoutB = std::conditional_t<std::is_void_v<LayoutTagB_>, typename BlockMmad::LayoutB, | ||
| 64 | + Catlass::detail::TagToLayout_t<ElementB, LayoutTagB_>>; | ||
| 65 | + using ElementC = typename BlockMmad::ElementC; | ||
| 66 | + using LayoutC = typename BlockMmad::LayoutC; | ||
| 67 | + | ||
| 68 | + using BlockEpilogue = BlockEpilogue_; | ||
| 69 | + using EpilogueParams = typename BlockEpilogue::Params; | ||
| 70 | + using BlockScheduler = BlockScheduler_; | ||
| 71 | + | ||
| 72 | + static_assert(std::is_same_v<ArchTag, Catlass::Arch::AtlasA2>, "MatmulDecompositionGm only supports AtlasA2"); | ||
| 73 | + static_assert(BlockEpilogue::USE_GM_WORKSPACE == true, | ||
| 74 | + "BlockEpilogue must use GM workspace semantics (USE_GM_WORKSPACE == true)"); | ||
| 75 | + | ||
| 76 | + static constexpr uint32_t L1_TILE_M = tla::get<0>(L1TileShape{}); | ||
| 77 | + static constexpr uint32_t L1_TILE_N = tla::get<1>(L1TileShape{}); | ||
| 78 | + static constexpr uint32_t L1_TILE_K = tla::get<2>(L1TileShape{}); | ||
| 79 | + | ||
| 80 | + struct Params { | ||
| 81 | + Catlass::GemmCoord problemShape; | ||
| 82 | + GM_ADDR ptrX; | ||
| 83 | + LayoutA layoutA; | ||
| 84 | + GM_ADDR ptrW_high; | ||
| 85 | + GM_ADDR ptrW_low; | ||
| 86 | + LayoutB layoutB; | ||
| 87 | + GM_ADDR ptrWorkspace; | ||
| 88 | + EpilogueParams epilogueParams; | ||
| 89 | + | ||
| 90 | + CATLASS_HOST_DEVICE | ||
| 91 | + Params() {} | ||
| 92 | + | ||
| 93 | + CATLASS_HOST_DEVICE | ||
| 94 | + Params(Catlass::GemmCoord const& problemShape_, GM_ADDR ptrX_, LayoutA const& layoutA_, GM_ADDR ptrW_high_, | ||
| 95 | + GM_ADDR ptrW_low_, LayoutB const& layoutB_, GM_ADDR ptrWorkspace_, EpilogueParams const& epilogueParams_) | ||
| 96 | + : problemShape(problemShape_), | ||
| 97 | + ptrX(ptrX_), | ||
| 98 | + layoutA(layoutA_), | ||
| 99 | + ptrW_high(ptrW_high_), | ||
| 100 | + ptrW_low(ptrW_low_), | ||
| 101 | + layoutB(layoutB_), | ||
| 102 | + ptrWorkspace(ptrWorkspace_), | ||
| 103 | + epilogueParams(epilogueParams_) | ||
| 104 | + {} | ||
| 105 | + }; | ||
| 106 | + | ||
| 107 | + static size_t GetWorkspaceSize(Params const& params) | ||
| 108 | + { | ||
| 109 | + return 2ul * params.problemShape.m() * params.problemShape.n() * sizeof(float); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + CATLASS_DEVICE | ||
| 113 | + MatmulDecompositionGm() {} | ||
| 114 | + | ||
| 115 | + CATLASS_DEVICE | ||
| 116 | + void operator()(Params const& params) | ||
| 117 | + { | ||
| 118 | + BlockScheduler scheduler(params.problemShape, Catlass::MakeCoord(L1_TILE_M, L1_TILE_N)); | ||
| 119 | + if constexpr (detail::HasSetSwizzleParams<BlockScheduler>::value) { | ||
| 120 | + uint32_t swizzleDir = (params.problemShape.m() > params.problemShape.n()) ? 0u : 1u; | ||
| 121 | + scheduler.SetSwizzleParams(3u, swizzleDir); | ||
| 122 | + } | ||
| 123 | + BlockMmad blockMmad(resource); | ||
| 124 | + BlockEpilogue blockEpilogue(resource, params.epilogueParams); | ||
| 125 | + RunCoreLoops(params, scheduler, blockMmad, blockEpilogue); | ||
| 126 | + AscendC::PipeBarrier<PIPE_ALL>(); | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | +private: | ||
| 130 | + template <class Scheduler, class Mmad, class Epilogue> | ||
| 131 | + CATLASS_DEVICE void RunCoreLoops(Params const& params, Scheduler& scheduler, Mmad& blockMmad, | ||
| 132 | + Epilogue& blockEpilogue) | ||
| 133 | + { | ||
| 134 | + uint32_t coreLoops = scheduler.GetCoreLoops(); | ||
| 135 | + | ||
| 136 | + AscendC::GlobalTensor<ElementA> gmX; | ||
| 137 | + gmX.SetGlobalBuffer((__gm__ ElementA*)params.ptrX); | ||
| 138 | + AscendC::GlobalTensor<ElementB> gmWh; | ||
| 139 | + gmWh.SetGlobalBuffer((__gm__ ElementB*)params.ptrW_high); | ||
| 140 | + AscendC::GlobalTensor<ElementB> gmWl; | ||
| 141 | + gmWl.SetGlobalBuffer((__gm__ ElementB*)params.ptrW_low); | ||
| 142 | + | ||
| 143 | + auto tensorX = tla::MakeTensor(gmX, params.layoutA, Catlass::Arch::PositionGM{}); | ||
| 144 | + auto tensorWh = tla::MakeTensor(gmWh, params.layoutB, Catlass::Arch::PositionGM{}); | ||
| 145 | + auto tensorWl = tla::MakeTensor(gmWl, params.layoutB, Catlass::Arch::PositionGM{}); | ||
| 146 | + | ||
| 147 | + uint32_t m = params.problemShape.m(); | ||
| 148 | + uint32_t n = params.problemShape.n(); | ||
| 149 | + uint32_t mnElems = m * n; | ||
| 150 | + | ||
| 151 | + AscendC::GlobalTensor<ElementC> gmWs0; | ||
| 152 | + gmWs0.SetGlobalBuffer((__gm__ ElementC*)params.ptrWorkspace); | ||
| 153 | + AscendC::GlobalTensor<ElementC> gmWs1; | ||
| 154 | + gmWs1.SetGlobalBuffer((__gm__ ElementC*)params.ptrWorkspace + mnElems); | ||
| 155 | + | ||
| 156 | + auto layoutWs = tla::MakeLayout<ElementC, layout::RowMajor>(m, n); | ||
| 157 | + auto tensorWs0 = tla::MakeTensor(gmWs0, layoutWs, Catlass::Arch::PositionGM{}); | ||
| 158 | + auto tensorWs1 = tla::MakeTensor(gmWs1, layoutWs, Catlass::Arch::PositionGM{}); | ||
| 159 | + | ||
| 160 | + uint32_t aicoreIndex = AscendC::GetBlockIdx(); | ||
| 161 | + if ASCEND_IS_AIV { | ||
| 162 | + aicoreIndex /= AscendC::GetSubBlockNum(); | ||
| 163 | + } | ||
| 164 | + uint32_t loopStep = AscendC::GetBlockNum(); | ||
| 165 | + Catlass::GemmCoord blockShape(L1_TILE_M, L1_TILE_N, L1_TILE_K); | ||
| 166 | + | ||
| 167 | + for (uint32_t loopIdx = aicoreIndex; loopIdx < coreLoops; loopIdx += loopStep) { | ||
| 168 | + ProcessOneBlock(scheduler, blockMmad, blockEpilogue, tensorX, tensorWh, tensorWl, tensorWs0, tensorWs1, | ||
| 169 | + gmWs0, gmWs1, layoutWs, blockShape, loopIdx); | ||
| 170 | + } | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + template <class Scheduler, class Mmad, class Epilogue, class TensorX, class TensorW, class TensorWs, class GmWs, | ||
| 174 | + class LayoutWs> | ||
| 175 | + CATLASS_DEVICE void ProcessOneBlock(Scheduler& scheduler, Mmad& blockMmad, Epilogue& blockEpilogue, | ||
| 176 | + TensorX& tensorX, TensorW& tensorWh, TensorW& tensorWl, TensorWs& tensorWs0, | ||
| 177 | + TensorWs& tensorWs1, GmWs& gmWs0, GmWs& gmWs1, LayoutWs const& layoutWs, | ||
| 178 | + Catlass::GemmCoord const& blockShape, uint32_t loopIdx) | ||
| 179 | + { | ||
| 180 | + Catlass::GemmCoord blockCoord = scheduler.GetBlockCoord(loopIdx); | ||
| 181 | + Catlass::GemmCoord actualBlockShape = scheduler.GetActualBlockShape(blockCoord); | ||
| 182 | + | ||
| 183 | + auto tileOffset = tla::MakeCoord(blockCoord.m() * L1_TILE_M, blockCoord.n() * L1_TILE_N); | ||
| 184 | + auto tileShape = tla::MakeShape(actualBlockShape.m(), actualBlockShape.n()); | ||
| 185 | + auto layoutTile = tla::GetTileLayout(layoutWs, tileShape, tla::MakeCoord(0, 0)); | ||
| 186 | + | ||
| 187 | + if ASCEND_IS_AIC { | ||
| 188 | + RunAicBlock(blockMmad, tensorX, tensorWh, tensorWl, tensorWs0, tensorWs1, blockCoord, actualBlockShape, | ||
| 189 | + tileOffset, tileShape); | ||
| 190 | + } else if ASCEND_IS_AIV { | ||
| 191 | + RunAivBlock(blockEpilogue, gmWs0, gmWs1, layoutWs, layoutTile, blockShape, blockCoord, actualBlockShape, | ||
| 192 | + tileOffset); | ||
| 193 | + } | ||
| 194 | + } | ||
| 195 | + | ||
| 196 | + template <class Mmad, class TensorX, class TensorW, class TensorWs, class TileOffset, class TileShape> | ||
| 197 | + CATLASS_DEVICE void RunAicBlock(Mmad& blockMmad, TensorX& tensorX, TensorW& tensorWh, TensorW& tensorWl, | ||
| 198 | + TensorWs& tensorWs0, TensorWs& tensorWs1, Catlass::GemmCoord const& blockCoord, | ||
| 199 | + Catlass::GemmCoord const& actualBlockShape, TileOffset const& tileOffset, | ||
| 200 | + TileShape const& tileShape) | ||
| 201 | + { | ||
| 202 | + auto tensorBlockA = GetTile(tensorX, tla::MakeCoord(blockCoord.m() * L1_TILE_M, blockCoord.k() * L1_TILE_K), | ||
| 203 | + tla::MakeShape(actualBlockShape.m(), actualBlockShape.k())); | ||
| 204 | + auto tensorBlockBh = GetTile(tensorWh, tla::MakeCoord(blockCoord.k() * L1_TILE_K, blockCoord.n() * L1_TILE_N), | ||
| 205 | + tla::MakeShape(actualBlockShape.k(), actualBlockShape.n())); | ||
| 206 | + auto tensorBlockBl = GetTile(tensorWl, tla::MakeCoord(blockCoord.k() * L1_TILE_K, blockCoord.n() * L1_TILE_N), | ||
| 207 | + tla::MakeShape(actualBlockShape.k(), actualBlockShape.n())); | ||
| 208 | + auto tensorBlockY0 = GetTile(tensorWs0, tileOffset, tileShape); | ||
| 209 | + auto tensorBlockY1 = GetTile(tensorWs1, tileOffset, tileShape); | ||
| 210 | + | ||
| 211 | + blockMmad(tensorBlockA, tensorBlockBh, tensorBlockY0, actualBlockShape); | ||
| 212 | + | ||
| 213 | + AscendC::SetFlag<AscendC::HardEvent::FIX_M>(EVENT_L0C_REUSE); | ||
| 214 | + AscendC::WaitFlag<AscendC::HardEvent::FIX_M>(EVENT_L0C_REUSE); | ||
| 215 | + Catlass::Arch::CrossCoreSetFlagWithReverse<0x2, PIPE_FIX>(flagY0Ready); | ||
| 216 | + | ||
| 217 | + blockMmad(tensorBlockA, tensorBlockBl, tensorBlockY1, actualBlockShape); | ||
| 218 | + Catlass::Arch::CrossCoreSetFlagWithReverse<0x2, PIPE_FIX>(flagY1Ready); | ||
| 219 | + } | ||
| 220 | + | ||
| 221 | + template <class Epilogue, class GmWs, class LayoutWs, class LayoutTile, class TileOffset> | ||
| 222 | + CATLASS_DEVICE void RunAivBlock(Epilogue& blockEpilogue, GmWs& gmWs0, GmWs& gmWs1, LayoutWs const& layoutWs, | ||
| 223 | + LayoutTile const& layoutTile, Catlass::GemmCoord const& blockShape, | ||
| 224 | + Catlass::GemmCoord const& blockCoord, Catlass::GemmCoord const& actualBlockShape, | ||
| 225 | + TileOffset const& tileOffset) | ||
| 226 | + { | ||
| 227 | + auto gmBlockY0 = gmWs0[layoutWs(tileOffset)]; | ||
| 228 | + auto gmBlockY1 = gmWs1[layoutWs(tileOffset)]; | ||
| 229 | + | ||
| 230 | + Catlass::Arch::CrossCoreWaitFlagWithReverse<0x2, PIPE_MTE2>(flagY0Ready); | ||
| 231 | + blockEpilogue.LoadY0(actualBlockShape, gmBlockY0, layoutTile); | ||
| 232 | + Catlass::Arch::CrossCoreWaitFlagWithReverse<0x2, PIPE_MTE2>(flagY1Ready); | ||
| 233 | + blockEpilogue.LoadY1(actualBlockShape, gmBlockY1, layoutTile); | ||
| 234 | + blockEpilogue.FuseAndStore(blockShape, blockCoord, actualBlockShape, layoutTile); | ||
| 235 | + } | ||
| 236 | + | ||
| 237 | + // Two independent single-direction handshakes: Y0 staged -> AIV load Y0 (overlaps | ||
| 238 | + // mmad1), Y1 staged -> AIV load Y1. Reverse flags drain the AIVs every 15 tiles. | ||
| 239 | + static constexpr Catlass::Arch::FlagID FLAG_AIC_Y0_READY = 0; | ||
| 240 | + static constexpr Catlass::Arch::FlagID RV_FLAG_AIC_Y0_READY = 1; | ||
| 241 | + static constexpr Catlass::Arch::FlagID FLAG_AIC_Y1_READY = 2; | ||
| 242 | + static constexpr Catlass::Arch::FlagID RV_FLAG_AIC_Y1_READY = 3; | ||
| 243 | + Catlass::Arch::CrossCoreFlagWithReverse<> flagY0Ready{FLAG_AIC_Y0_READY, RV_FLAG_AIC_Y0_READY}; | ||
| 244 | + Catlass::Arch::CrossCoreFlagWithReverse<> flagY1Ready{FLAG_AIC_Y1_READY, RV_FLAG_AIC_Y1_READY}; | ||
| 245 | + | ||
| 246 | + static constexpr int32_t EVENT_L0C_REUSE = 0; | ||
| 247 | + | ||
| 248 | + Catlass::Arch::Resource<ArchTag> resource; | ||
| 249 | +}; | ||
| 250 | + | ||
| 251 | +} // namespace Catlass::Gemm::Kernel | ||
| 252 | + | ||
| 253 | +#endif // CATLASS_GEMM_KERNEL_MATMUL_DECOMPOSITION_GM_HPP | ||
| @@ -0,0 +1,41 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025-2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * 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. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#ifndef MATMUL_EMU_CATLASS_GEMM_DISPATCH_POLICY_HPP | ||
| 12 | +#define MATMUL_EMU_CATLASS_GEMM_DISPATCH_POLICY_HPP | ||
| 13 | + | ||
| 14 | +#include "../matmul_emu_catlass.hpp" | ||
| 15 | +#include "../arch/matmul_emu_arch.hpp" | ||
| 16 | + | ||
| 17 | +namespace Catlass::Gemm { | ||
| 18 | + | ||
| 19 | +template <class ArchTag_, bool ASYNC_ = false> | ||
| 20 | +struct MmadBase { | ||
| 21 | + using ArchTag = ArchTag_; | ||
| 22 | + static constexpr uint32_t ASYNC = ASYNC_; | ||
| 23 | +}; | ||
| 24 | + | ||
| 25 | +template <class ArchTag_, bool ENABLE_UNIT_FLAG_ = false, bool USE_HF32_MODE_ = false, uint32_t L0C_STAGES_ = 1, | ||
| 26 | + bool ENABLE_L1_RESIDENT_ = false, uint32_t L1A_STAGES_ = 2, uint32_t L1B_STAGES_ = 2, | ||
| 27 | + uint32_t L0A_STAGES_ = 2, uint32_t L0B_STAGES_ = 2> | ||
| 28 | +struct MmadPingpong : public MmadBase<ArchTag_, false> { | ||
| 29 | + static constexpr uint32_t L1A_STAGES = L1A_STAGES_; | ||
| 30 | + static constexpr uint32_t L1B_STAGES = L1B_STAGES_; | ||
| 31 | + static constexpr uint32_t L0A_STAGES = L0A_STAGES_; | ||
| 32 | + static constexpr uint32_t L0B_STAGES = L0B_STAGES_; | ||
| 33 | + static constexpr uint32_t L0C_STAGES = L0C_STAGES_; | ||
| 34 | + static constexpr bool ENABLE_UNIT_FLAG = ENABLE_UNIT_FLAG_; | ||
| 35 | + static constexpr bool USE_HF32_MODE = USE_HF32_MODE_; | ||
| 36 | + static constexpr bool ENABLE_L1_RESIDENT = ENABLE_L1_RESIDENT_; | ||
| 37 | +}; | ||
| 38 | + | ||
| 39 | +} // namespace Catlass::Gemm | ||
| 40 | + | ||
| 41 | +#endif // MATMUL_EMU_CATLASS_GEMM_DISPATCH_POLICY_HPP | ||
| @@ -0,0 +1,29 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * 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. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#ifndef MATMUL_EMU_CATLASS_GEMM_GEMM_TYPE_HPP | ||
| 12 | +#define MATMUL_EMU_CATLASS_GEMM_GEMM_TYPE_HPP | ||
| 13 | + | ||
| 14 | +#include "../matmul_emu_catlass.hpp" | ||
| 15 | + | ||
| 16 | +namespace Catlass::Gemm { | ||
| 17 | + | ||
| 18 | +//////////////////////////////////////////////////////////////////// | ||
| 19 | + | ||
| 20 | +template <class Element_, class Layout_, AscendC::TPosition POSITION_ = AscendC::TPosition::GM> | ||
| 21 | +struct GemmType { | ||
| 22 | + using Element = Element_; | ||
| 23 | + using Layout = Layout_; | ||
| 24 | + static constexpr AscendC::TPosition POSITION = POSITION_; | ||
| 25 | +}; | ||
| 26 | + | ||
| 27 | +} // namespace Catlass::Gemm | ||
| 28 | + | ||
| 29 | +#endif // MATMUL_EMU_CATLASS_GEMM_GEMM_TYPE_HPP | ||
| @@ -0,0 +1,418 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025-2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * 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. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#ifndef MATMUL_EMU_CATLASS_GEMM_TILE_TILE_COPY_HPP | ||
| 12 | +#define MATMUL_EMU_CATLASS_GEMM_TILE_TILE_COPY_HPP | ||
| 13 | + | ||
| 14 | +#include <type_traits> | ||
| 15 | + | ||
| 16 | +#include "../../matmul_emu_catlass.hpp" | ||
| 17 | +#include "../../arch/matmul_emu_arch.hpp" | ||
| 18 | +#include "../matmul_emu_gemm_type.hpp" | ||
| 19 | +#include "../../layout/matmul_emu_layout.hpp" | ||
| 20 | +#include "../../../tla/matmul_emu_layout.hpp" | ||
| 21 | + | ||
| 22 | +namespace Catlass::detail { | ||
| 23 | + | ||
| 24 | +template <class Element, class LayoutTag> | ||
| 25 | +struct TagToLayout { | ||
| 26 | + using type = LayoutTag; | ||
| 27 | +}; | ||
| 28 | + | ||
| 29 | +template <class Element> | ||
| 30 | +struct TagToLayout<Element, layout::RowMajor> { | ||
| 31 | + using type = tla::Layout<tla::Shape<uint32_t, uint32_t>, tla::Stride<int64_t, tla::Int<1>>>; | ||
| 32 | +}; | ||
| 33 | + | ||
| 34 | +template <class Element> | ||
| 35 | +struct TagToLayout<Element, layout::ColumnMajor> { | ||
| 36 | + using type = tla::Layout<tla::Shape<uint32_t, uint32_t>, tla::Stride<tla::Int<1>, int64_t>>; | ||
| 37 | +}; | ||
| 38 | + | ||
| 39 | +template <class Element> | ||
| 40 | +struct TagToLayout<Element, layout::VectorLayout> { | ||
| 41 | + using type = tla::Layout<tla::Shape<uint32_t>, tla::Stride<tla::Int<1>>>; | ||
| 42 | +}; | ||
| 43 | + | ||
| 44 | +template <class Element> | ||
| 45 | +struct TagToLayout<Element, layout::zN> { | ||
| 46 | + static constexpr uint32_t ELE_NUM_PER_C0 = BytesToBits(BYTE_PER_C0) / SizeOfBits<Element>::value; | ||
| 47 | + static constexpr uint32_t ELE_NUM_PER_FRACTAL = BytesToBits(BYTE_PER_FRACTAL) / SizeOfBits<Element>::value; | ||
| 48 | + using type = tla::Layout< | ||
| 49 | + tla::Shape<tla::Shape<tla::Int<C0_NUM_PER_FRACTAL>, uint32_t>, tla::Shape<tla::Int<ELE_NUM_PER_C0>, uint32_t>>, | ||
| 50 | + tla::Stride<tla::Stride<tla::Int<ELE_NUM_PER_C0>, tla::Int<ELE_NUM_PER_FRACTAL>>, | ||
| 51 | + tla::Stride<tla::Int<1>, int64_t>>>; | ||
| 52 | +}; | ||
| 53 | + | ||
| 54 | +template <class Element> | ||
| 55 | +struct TagToLayout<Element, layout::zZ> { | ||
| 56 | + static constexpr uint32_t ELE_NUM_PER_C0 = BytesToBits(BYTE_PER_C0) / SizeOfBits<Element>::value; | ||
| 57 | + static constexpr uint32_t ELE_NUM_PER_FRACTAL = BytesToBits(BYTE_PER_FRACTAL) / SizeOfBits<Element>::value; | ||
| 58 | + using type = tla::Layout< | ||
| 59 | + tla::Shape<tla::Shape<tla::Int<C0_NUM_PER_FRACTAL>, uint32_t>, tla::Shape<tla::Int<ELE_NUM_PER_C0>, uint32_t>>, | ||
| 60 | + tla::Stride<tla::Stride<tla::Int<ELE_NUM_PER_C0>, int64_t>, | ||
| 61 | + tla::Stride<tla::Int<1>, tla::Int<ELE_NUM_PER_FRACTAL>>>>; | ||
| 62 | +}; | ||
| 63 | + | ||
| 64 | +template <class Element> | ||
| 65 | +struct TagToLayout<Element, layout::nZ> { | ||
| 66 | + static constexpr uint32_t ELE_NUM_PER_C0 = BytesToBits(BYTE_PER_C0) / SizeOfBits<Element>::value; | ||
| 67 | + static constexpr uint32_t ELE_NUM_PER_FRACTAL = BytesToBits(BYTE_PER_FRACTAL) / SizeOfBits<Element>::value; | ||
| 68 | + using type = tla::Layout< | ||
| 69 | + tla::Shape<tla::Shape<tla::Int<ELE_NUM_PER_C0>, uint32_t>, tla::Shape<tla::Int<C0_NUM_PER_FRACTAL>, uint32_t>>, | ||
| 70 | + tla::Stride<tla::Stride<tla::Int<1>, int64_t>, | ||
| 71 | + tla::Stride<tla::Int<ELE_NUM_PER_C0>, tla::Int<ELE_NUM_PER_FRACTAL>>>>; | ||
| 72 | +}; | ||
| 73 | + | ||
| 74 | +template <class Element, class LayoutTag> | ||
| 75 | +using TagToLayout_t = typename TagToLayout<Element, LayoutTag>::type; | ||
| 76 | + | ||
| 77 | +constexpr uint32_t ELE_NUM_PER_FRACTAL_L0C = 256; | ||
| 78 | +using LayoutL0C = tla::Layout< | ||
| 79 | + tla::Shape<tla::Shape<tla::Int<C0_NUM_PER_FRACTAL>, uint32_t>, tla::Shape<tla::Int<C0_NUM_PER_FRACTAL>, uint32_t>>, | ||
| 80 | + tla::Stride<tla::Stride<tla::Int<C0_NUM_PER_FRACTAL>, tla::Int<ELE_NUM_PER_FRACTAL_L0C>>, | ||
| 81 | + tla::Stride<tla::Int<1>, int64_t>>>; | ||
| 82 | + | ||
| 83 | +} // namespace Catlass::detail | ||
| 84 | + | ||
| 85 | +namespace Catlass::Gemm::helper { | ||
| 86 | + | ||
| 87 | +template <class Element, class Layout> | ||
| 88 | +struct L1AlignHelper { | ||
| 89 | + static_assert(DEPENDENT_FALSE<Element>, "Unsupported align helper, can not find the specialization."); | ||
| 90 | +}; | ||
| 91 | + | ||
| 92 | +template <class Element> | ||
| 93 | +struct L1AlignHelper<Element, layout::RowMajor> { | ||
| 94 | + static constexpr uint32_t ELE_NUM_PER_C0 = BytesToBits(BYTE_PER_C0) / SizeOfBits<Element>::value; | ||
| 95 | + static constexpr uint32_t M_ALIGNED = C0_NUM_PER_FRACTAL; | ||
| 96 | + static constexpr uint32_t K_ALIGNED = ELE_NUM_PER_C0; | ||
| 97 | + static constexpr uint32_t N_ALIGNED = ELE_NUM_PER_C0; | ||
| 98 | +}; | ||
| 99 | + | ||
| 100 | +template <class Element> | ||
| 101 | +struct L1AlignHelper<Element, layout::ColumnMajor> { | ||
| 102 | + static constexpr uint32_t ELE_NUM_PER_C0 = BytesToBits(BYTE_PER_C0) / SizeOfBits<Element>::value; | ||
| 103 | + static constexpr uint32_t M_ALIGNED = ELE_NUM_PER_C0; | ||
| 104 | + static constexpr uint32_t K_ALIGNED = ELE_NUM_PER_C0; | ||
| 105 | + static constexpr uint32_t N_ALIGNED = C0_NUM_PER_FRACTAL; | ||
| 106 | +}; | ||
| 107 | + | ||
| 108 | +template <class Element> | ||
| 109 | +struct L1AlignHelper<Element, layout::zN> { | ||
| 110 | + static constexpr uint32_t ELE_NUM_PER_C0 = BytesToBits(BYTE_PER_C0) / SizeOfBits<Element>::value; | ||
| 111 | + static constexpr uint32_t M_ALIGNED = C0_NUM_PER_FRACTAL; | ||
| 112 | + static constexpr uint32_t K_ALIGNED = ELE_NUM_PER_C0; | ||
| 113 | + static constexpr uint32_t N_ALIGNED = ELE_NUM_PER_C0; | ||
| 114 | +}; | ||
| 115 | + | ||
| 116 | +template <class Element> | ||
| 117 | +struct L1AlignHelper<Element, layout::nZ> { | ||
| 118 | + static constexpr uint32_t ELE_NUM_PER_C0 = BytesToBits(BYTE_PER_C0) / SizeOfBits<Element>::value; | ||
| 119 | + static constexpr uint32_t M_ALIGNED = ELE_NUM_PER_C0; | ||
| 120 | + static constexpr uint32_t K_ALIGNED = ELE_NUM_PER_C0; | ||
| 121 | + static constexpr uint32_t N_ALIGNED = C0_NUM_PER_FRACTAL; | ||
| 122 | +}; | ||
| 123 | + | ||
| 124 | +template <class ElementA, class ElementB> | ||
| 125 | +struct ElementAccumulatorSelector { | ||
| 126 | + static_assert(DEPENDENT_FALSE<ElementA>, | ||
| 127 | + "Unsupported element accumulator selector, can not find the specialization."); | ||
| 128 | +}; | ||
| 129 | + | ||
| 130 | +template <> | ||
| 131 | +struct ElementAccumulatorSelector<bfloat16_t, bfloat16_t> { | ||
| 132 | + using ElementAccumulator = float; | ||
| 133 | +}; | ||
| 134 | + | ||
| 135 | +template <class GmAType> | ||
| 136 | +struct L1ATypeSelector { | ||
| 137 | + static_assert(DEPENDENT_FALSE<GmAType>, "Unsupported layout selector, can not find the specialization."); | ||
| 138 | +}; | ||
| 139 | + | ||
| 140 | +template <class Element> | ||
| 141 | +struct L1ATypeSelector<Gemm::GemmType<Element, layout::RowMajor>> { | ||
| 142 | + using L1AType = Gemm::GemmType<Element, layout::zN, AscendC::TPosition::A1>; | ||
| 143 | +}; | ||
| 144 | + | ||
| 145 | +template <class Element> | ||
| 146 | +struct L1ATypeSelector<Gemm::GemmType<Element, layout::ColumnMajor>> { | ||
| 147 | + using L1AType = Gemm::GemmType<Element, layout::nZ, AscendC::TPosition::A1>; | ||
| 148 | +}; | ||
| 149 | + | ||
| 150 | +template <class GmBType> | ||
| 151 | +struct L1BTypeSelector { | ||
| 152 | + static_assert(DEPENDENT_FALSE<GmBType>, "Unsupported layout selector, can not find the specialization."); | ||
| 153 | +}; | ||
| 154 | + | ||
| 155 | +template <class Element> | ||
| 156 | +struct L1BTypeSelector<Gemm::GemmType<Element, layout::RowMajor>> { | ||
| 157 | + using L1BType = Gemm::GemmType<Element, layout::zN, AscendC::TPosition::A1>; | ||
| 158 | +}; | ||
| 159 | + | ||
| 160 | +template <class Element> | ||
| 161 | +struct L1BTypeSelector<Gemm::GemmType<Element, layout::ColumnMajor>> { | ||
| 162 | + using L1BType = Gemm::GemmType<Element, layout::nZ, AscendC::TPosition::A1>; | ||
| 163 | +}; | ||
| 164 | + | ||
| 165 | +template <class ArchTag> | ||
| 166 | +struct L0ALayoutSelector { | ||
| 167 | + static_assert(DEPENDENT_FALSE<ArchTag>, "Unsupported layout selector, can not find the specialization."); | ||
| 168 | +}; | ||
| 169 | + | ||
| 170 | +template <> | ||
| 171 | +struct L0ALayoutSelector<Arch::AtlasA2> { | ||
| 172 | + using Layout = layout::zZ; | ||
| 173 | +}; | ||
| 174 | + | ||
| 175 | +} // namespace Catlass::Gemm::helper | ||
| 176 | + | ||
| 177 | +namespace Catlass::Gemm::Tile { | ||
| 178 | + | ||
| 179 | +namespace copy_impl { | ||
| 180 | + | ||
| 181 | +template <uint32_t ELE_NUM_PER_C0, class TensorDst, class TensorSrc> | ||
| 182 | +CATLASS_DEVICE void CopyGmNdToL1Nz(TensorDst const& dstTensor, TensorSrc const& srcTensor, uint32_t nValue, | ||
| 183 | + uint32_t dValue, uint32_t srcDValue, uint32_t dstInnerStrideRow, | ||
| 184 | + uint32_t dstOuterStrideCol) | ||
| 185 | +{ | ||
| 186 | + AscendC::Nd2NzParams intriParams; | ||
| 187 | + intriParams.ndNum = 1; | ||
| 188 | + intriParams.dValue = dValue; | ||
| 189 | + intriParams.srcNdMatrixStride = 0; | ||
| 190 | + intriParams.dstNzC0Stride = dstOuterStrideCol / ELE_NUM_PER_C0; | ||
| 191 | + intriParams.dstNzMatrixStride = 0; | ||
| 192 | + | ||
| 193 | + auto dstOffset = dstTensor.layout()(dstTensor.coord()); | ||
| 194 | + auto srcOffset = srcTensor.layout()(srcTensor.coord()); | ||
| 195 | + | ||
| 196 | + if (srcDValue < STRIDE_LIMIT) { | ||
| 197 | + intriParams.nValue = nValue; | ||
| 198 | + intriParams.srcDValue = srcDValue; | ||
| 199 | + intriParams.dstNzNStride = dstInnerStrideRow / ELE_NUM_PER_C0; | ||
| 200 | + AscendC::DataCopy(dstTensor.data()[dstOffset], srcTensor.data()[srcOffset], intriParams); | ||
| 201 | + } else { | ||
| 202 | + intriParams.nValue = 1; | ||
| 203 | + intriParams.srcDValue = 0; | ||
| 204 | + intriParams.dstNzNStride = 0; | ||
| 205 | + for (uint32_t i = 0; i < nValue; i++) { | ||
| 206 | + AscendC::DataCopy(dstTensor.data()[dstOffset + i * ELE_NUM_PER_C0], | ||
| 207 | + srcTensor.data()[srcOffset + i * srcDValue], intriParams); | ||
| 208 | + } | ||
| 209 | + } | ||
| 210 | +} | ||
| 211 | + | ||
| 212 | +CATLASS_DEVICE | ||
| 213 | +AscendC::LoadData2DParams MakeLoadData2DParams(uint32_t repeatTimes, uint32_t srcStride, bool ifTranspose) | ||
| 214 | +{ | ||
| 215 | + AscendC::LoadData2DParams params; | ||
| 216 | + params.startIndex = 0; | ||
| 217 | + params.repeatTimes = repeatTimes; | ||
| 218 | + params.srcStride = srcStride; | ||
| 219 | + params.sid = 0; | ||
| 220 | + params.dstGap = 0; | ||
| 221 | + params.ifTranspose = ifTranspose; | ||
| 222 | + params.addrMode = 0; | ||
| 223 | + return params; | ||
| 224 | +} | ||
| 225 | + | ||
| 226 | +template <class TensorDst, class TensorSrc> | ||
| 227 | +CATLASS_DEVICE void LoadData2DRows(TensorDst const& dstTensor, TensorSrc const& srcTensor, uint32_t dstOuterShapeRow, | ||
| 228 | + uint32_t dstOuterStrideRow, uint32_t srcOuterStrideRow, | ||
| 229 | + AscendC::LoadData2DParams const& loadDataParams) | ||
| 230 | +{ | ||
| 231 | + auto dstOffset = dstTensor.layout()(dstTensor.coord()); | ||
| 232 | + auto srcOffset = srcTensor.layout()(srcTensor.coord()); | ||
| 233 | + for (uint32_t i = 0; i < dstOuterShapeRow; i++) { | ||
| 234 | + AscendC::LoadData(dstTensor.data()[dstOffset + i * dstOuterStrideRow], | ||
| 235 | + srcTensor.data()[srcOffset + i * srcOuterStrideRow], loadDataParams); | ||
| 236 | + } | ||
| 237 | +} | ||
| 238 | + | ||
| 239 | +} // namespace copy_impl | ||
| 240 | + | ||
| 241 | +template <class ArchTag, class TensorSrc, class TensorDst, class Enable = void> | ||
| 242 | +struct TileCopyTla { | ||
| 243 | + static_assert(DEPENDENT_FALSE<ArchTag>, "Unsupported TileCopyTla, can not find the specialization."); | ||
| 244 | +}; | ||
| 245 | + | ||
| 246 | +///////////////////////////////////////////TileCopyTla////////////////////////////////////////////////////// | ||
| 247 | +/// Partial specialization for CopyGmToL1, AtlasA2, RowMajor in and zN out. | ||
| 248 | +template <class ElementSrc, class ElementDst, class LayoutSrc, class LayoutDst, class CoordSrc, class CoordDst> | ||
| 249 | +struct TileCopyTla< | ||
| 250 | + Arch::AtlasA2, tla::Tensor<AscendC::GlobalTensor<ElementSrc>, LayoutSrc, CoordSrc, AscendC::TPosition::GM>, | ||
| 251 | + tla::Tensor<AscendC::LocalTensor<ElementDst>, LayoutDst, CoordDst, AscendC::TPosition::A1>, | ||
| 252 | + std::enable_if_t<tla::detail::isRowMajor<LayoutSrc>::value && tla::detail::iszN<ElementDst, LayoutDst>::value>> { | ||
| 253 | + static constexpr uint32_t ELE_NUM_PER_C0 = BytesToBits(BYTE_PER_C0) / SizeOfBits<ElementSrc>::value; | ||
| 254 | + | ||
| 255 | + // Methods | ||
| 256 | + | ||
| 257 | + CATLASS_DEVICE | ||
| 258 | + TileCopyTla() {}; | ||
| 259 | + | ||
| 260 | + template <class TensorDst, class TensorSrc> | ||
| 261 | + CATLASS_DEVICE void operator()(TensorDst const& dstTensor, TensorSrc const& srcTensor) | ||
| 262 | + { | ||
| 263 | + static_assert( | ||
| 264 | + tla::detail::isRowMajor<typename TensorSrc::Layout>::value && | ||
| 265 | + tla::detail::iszN<typename TensorDst::Element, typename TensorDst::Layout>::value && | ||
| 266 | + TensorSrc::position == AscendC::TPosition::GM && TensorDst::position == AscendC::TPosition::A1, | ||
| 267 | + "The input parameters do not match. TensorSrc must be GM and RowMajor, while TensorDst must be L1 and zN"); | ||
| 268 | + | ||
| 269 | + const uint32_t nValue = tla::get<0>(srcTensor.originShape()); | ||
| 270 | + const uint32_t dValue = tla::get<1>(srcTensor.originShape()); | ||
| 271 | + const uint32_t srcDValue = tla::get<0>(srcTensor.stride()); | ||
| 272 | + const uint32_t dstInnerStrideRow = tla::get<0, 0>(dstTensor.stride()); | ||
| 273 | + const uint32_t dstOuterStrideCol = tla::get<1, 1>(dstTensor.stride()); | ||
| 274 | + | ||
| 275 | + copy_impl::CopyGmNdToL1Nz<ELE_NUM_PER_C0>(dstTensor, srcTensor, nValue, dValue, srcDValue, dstInnerStrideRow, | ||
| 276 | + dstOuterStrideCol); | ||
| 277 | + } | ||
| 278 | +}; | ||
| 279 | + | ||
| 280 | +/// Partial specialization for CopyGmToL1, AtlasA2, ColumnMajor in and nZ out. | ||
| 281 | +template <class ElementSrc, class ElementDst, class LayoutSrc, class LayoutDst, class CoordSrc, class CoordDst> | ||
| 282 | +struct TileCopyTla< | ||
| 283 | + Arch::AtlasA2, tla::Tensor<AscendC::GlobalTensor<ElementSrc>, LayoutSrc, CoordSrc, AscendC::TPosition::GM>, | ||
| 284 | + tla::Tensor<AscendC::LocalTensor<ElementDst>, LayoutDst, CoordDst, AscendC::TPosition::A1>, | ||
| 285 | + std::enable_if_t<tla::detail::isColumnMajor<LayoutSrc>::value && tla::detail::isnZ<ElementDst, LayoutDst>::value>> { | ||
| 286 | + static constexpr uint32_t ELE_NUM_PER_C0 = BytesToBits(BYTE_PER_C0) / SizeOfBits<ElementSrc>::value; | ||
| 287 | + | ||
| 288 | + // Methods | ||
| 289 | + | ||
| 290 | + CATLASS_DEVICE | ||
| 291 | + TileCopyTla() {}; | ||
| 292 | + | ||
| 293 | + template <class TensorDst, class TensorSrc> | ||
| 294 | + CATLASS_DEVICE void operator()(TensorDst const& dstTensor, TensorSrc const& srcTensor) | ||
| 295 | + { | ||
| 296 | + static_assert(tla::detail::isColumnMajor<typename TensorSrc::Layout>::value && | ||
| 297 | + tla::detail::isnZ<typename TensorDst::Element, typename TensorDst::Layout>::value && | ||
| 298 | + TensorSrc::position == AscendC::TPosition::GM && | ||
| 299 | + TensorDst::position == AscendC::TPosition::A1, | ||
| 300 | + "The input parameters do not match. TensorSrc must be GM and ColumnMajor, " | ||
| 301 | + "while TensorDst must be L1 and nZ"); | ||
| 302 | + | ||
| 303 | + const uint32_t nValue = tla::get<1>(srcTensor.originShape()); | ||
| 304 | + const uint32_t dValue = tla::get<0>(srcTensor.originShape()); | ||
| 305 | + const uint32_t srcDValue = tla::get<1>(srcTensor.stride()); | ||
| 306 | + const uint32_t dstInnerStrideRow = tla::get<1, 0>(dstTensor.stride()); | ||
| 307 | + const uint32_t dstOuterStrideCol = tla::get<0, 1>(dstTensor.stride()); | ||
| 308 | + | ||
| 309 | + copy_impl::CopyGmNdToL1Nz<ELE_NUM_PER_C0>(dstTensor, srcTensor, nValue, dValue, srcDValue, dstInnerStrideRow, | ||
| 310 | + dstOuterStrideCol); | ||
| 311 | + } | ||
| 312 | +}; | ||
| 313 | + | ||
| 314 | +enum class ScaleGranularity { UNDEFINED = -1, NO_QUANT = 0, PER_TENSOR, PER_CHANNEL, PER_GROUP }; | ||
| 315 | + | ||
| 316 | +template <class ArchTag, class ElementSrc, class ElementDst, | ||
| 317 | + ScaleGranularity DEQUANT_GRANULARITY = ScaleGranularity::NO_QUANT> | ||
| 318 | +struct CopyL0CToGmQuantMode { | ||
| 319 | + static_assert(DEPENDENT_FALSE<ArchTag>, "Unsupported copy l0c to gm, can not find the specialization."); | ||
| 320 | +}; | ||
| 321 | + | ||
| 322 | +// CopyL0CToGm fp32 to fp32 | ||
| 323 | +template <> | ||
| 324 | +struct CopyL0CToGmQuantMode<Catlass::Arch::AtlasA2, float, float, ScaleGranularity::NO_QUANT> { | ||
| 325 | + static constexpr auto VALUE = QuantMode_t::NoQuant; | ||
| 326 | +}; | ||
| 327 | + | ||
| 328 | +#include "matmul_emu_tile_copy_l0.hpp" | ||
| 329 | + | ||
| 330 | +template <class ArchTag, class ElementA_, class LayoutTagA_, class ElementB_, class LayoutTagB_, class ElementC_, | ||
| 331 | + class LayoutTagC_, class ElementBias = void> | ||
| 332 | +struct PackedTileCopyTla { | ||
| 333 | + using ElementA = ElementA_; | ||
| 334 | + using ElementB = ElementB_; | ||
| 335 | + using LayoutTagA = LayoutTagA_; | ||
| 336 | + using LayoutTagB = LayoutTagB_; | ||
| 337 | + using LayoutTagC = LayoutTagC_; | ||
| 338 | + using ElementAccumulator = typename Gemm::helper::ElementAccumulatorSelector<ElementA, | ||
| 339 | + ElementB>::ElementAccumulator; | ||
| 340 | + static constexpr bool ReluEnable = false; | ||
| 341 | + static constexpr ScaleGranularity DEQUANT_GRANULARITY = ScaleGranularity::NO_QUANT; | ||
| 342 | + static constexpr bool HAS_BIAS = false; | ||
| 343 | + static constexpr bool HAS_QUANT_TENSOR = false; | ||
| 344 | + | ||
| 345 | + using LayoutTagL1A = typename helper::L1ATypeSelector<Gemm::GemmType<ElementA, LayoutTagA>>::L1AType::Layout; | ||
| 346 | + using LayoutTagL1B = typename helper::L1BTypeSelector<Gemm::GemmType<ElementB, LayoutTagB>>::L1BType::Layout; | ||
| 347 | + using LayoutTagL0A = typename helper::L0ALayoutSelector<ArchTag>::Layout; | ||
| 348 | + using LayoutTagL0B = layout::nZ; | ||
| 349 | + using LayoutTagL0C = layout::L0C; | ||
| 350 | + | ||
| 351 | + using LayoutA = Catlass::detail::TagToLayout_t<ElementA, LayoutTagA>; | ||
| 352 | + using LayoutB = Catlass::detail::TagToLayout_t<ElementB, LayoutTagB>; | ||
| 353 | + using LayoutC = Catlass::detail::TagToLayout_t<ElementC_, LayoutTagC>; | ||
| 354 | + | ||
| 355 | + using LayoutL1A = Catlass::detail::TagToLayout_t<ElementA, LayoutTagL1A>; | ||
| 356 | + using LayoutL1B = Catlass::detail::TagToLayout_t<ElementB, LayoutTagL1B>; | ||
| 357 | + using LayoutL0A = Catlass::detail::TagToLayout_t<ElementA, LayoutTagL0A>; | ||
| 358 | + using LayoutL0B = Catlass::detail::TagToLayout_t<ElementB, LayoutTagL0B>; | ||
| 359 | + using LayoutL0C = typename Catlass::detail::LayoutL0C; | ||
| 360 | + | ||
| 361 | + using TensorL1A = tla::Tensor<AscendC::LocalTensor<ElementA>, LayoutL1A, tla::Coord<tla::_0, tla::_0>, | ||
| 362 | + AscendC::TPosition::A1>; | ||
| 363 | + using TensorL1B = tla::Tensor<AscendC::LocalTensor<ElementB>, LayoutL1B, tla::Coord<tla::_0, tla::_0>, | ||
| 364 | + AscendC::TPosition::A1>; | ||
| 365 | + using TensorL0A = tla::Tensor<AscendC::LocalTensor<ElementA>, LayoutL0A, tla::Coord<tla::_0, tla::_0>, | ||
| 366 | + AscendC::TPosition::A2>; | ||
| 367 | + using TensorL0B = tla::Tensor<AscendC::LocalTensor<ElementB>, LayoutL0B, tla::Coord<tla::_0, tla::_0>, | ||
| 368 | + AscendC::TPosition::B2>; | ||
| 369 | + using TensorL0C = tla::Tensor<AscendC::LocalTensor<ElementAccumulator>, LayoutL0C, tla::Coord<tla::_0, tla::_0>, | ||
| 370 | + AscendC::TPosition::CO1>; | ||
| 371 | + | ||
| 372 | + using L1AAlignHelper = Gemm::helper::L1AlignHelper<ElementA, LayoutTagA>; | ||
| 373 | + using L1BAlignHelper = Gemm::helper::L1AlignHelper<ElementB, LayoutTagB>; | ||
| 374 | + | ||
| 375 | + template <class TensorA> | ||
| 376 | + using CopyGmToL1A = Gemm::Tile::TileCopyTla<ArchTag, TensorA, TensorL1A>; | ||
| 377 | + | ||
| 378 | + template <class TensorB> | ||
| 379 | + using CopyGmToL1B = Gemm::Tile::TileCopyTla<ArchTag, TensorB, TensorL1B>; | ||
| 380 | + | ||
| 381 | + using CopyL1ToL0A = Gemm::Tile::TileCopyTla<ArchTag, TensorL1A, TensorL0A>; | ||
| 382 | + using CopyL1ToL0B = Gemm::Tile::TileCopyTla<ArchTag, TensorL1B, TensorL0B>; | ||
| 383 | + using CopyL1ToBT = EmptyClass; | ||
| 384 | + | ||
| 385 | + template <class TensorC> | ||
| 386 | + using CopyL0CToGm = Gemm::Tile::CopyL0CToGmTla<ArchTag, TensorL0C, TensorC, DEQUANT_GRANULARITY, ReluEnable>; | ||
| 387 | +}; | ||
| 388 | + | ||
| 389 | +template <class ArchTag, class ElementA, class LayoutTagL1A> | ||
| 390 | +struct TileMmadTla { | ||
| 391 | + CATLASS_DEVICE | ||
| 392 | + TileMmadTla() {} | ||
| 393 | + | ||
| 394 | + template <class TensorC, class TensorA, class TensorB> | ||
| 395 | + CATLASS_DEVICE void operator()(TensorC const& l0CTensor, TensorA const& l0ATensor, TensorB const& l0BTensor, | ||
| 396 | + uint32_t m, uint32_t n, uint32_t k, bool initC = true, uint8_t unitFlag = 0) | ||
| 397 | + { | ||
| 398 | + AscendC::MmadParams mmadParams; | ||
| 399 | + mmadParams.m = m; | ||
| 400 | + mmadParams.n = n; | ||
| 401 | + mmadParams.k = k; | ||
| 402 | + mmadParams.unitFlag = unitFlag; | ||
| 403 | + mmadParams.cmatrixInitVal = initC; | ||
| 404 | + if constexpr (std::is_same_v<ElementA, float> && std::is_same_v<LayoutTagL1A, layout::nZ>) { | ||
| 405 | + mmadParams.kDirectionAlign = true; | ||
| 406 | + } | ||
| 407 | + AscendC::Mmad(l0CTensor.data(), l0ATensor.data(), l0BTensor.data(), mmadParams); | ||
| 408 | + | ||
| 409 | + const uint32_t PIPE_M_BARRIER_THRESHOLD = 10; | ||
| 410 | + if ((m / C0_NUM_PER_FRACTAL) * (n / C0_NUM_PER_FRACTAL) < PIPE_M_BARRIER_THRESHOLD) { | ||
| 411 | + AscendC::PipeBarrier<PIPE_M>(); | ||
| 412 | + } | ||
| 413 | + } | ||
| 414 | +}; | ||
| 415 | + | ||
| 416 | +} // namespace Catlass::Gemm::Tile | ||
| 417 | + | ||
| 418 | +#endif // MATMUL_EMU_CATLASS_GEMM_TILE_TILE_COPY_HPP | ||


同步更新docs/zh/op_api_list.md中A2/A3的确定性支持情况