Merged
【FEATURE】A2/A3 GEMMV3 base kernel支持需要broadcast的bias输入 #8446
HKFLYEAdd on Aug 8
【FEATURE】A2/A3 GEMMV3 base kernel支持需要broadcast的bias输入 #8446
Merged
Total: 7 files changed+747-115
| @@ -23,11 +23,9 @@ using namespace gert; | |||
| 23 | namespace { | 23 | namespace { |
| 24 | const size_t kMatMulX1Idx = 0; | 24 | const size_t kMatMulX1Idx = 0; |
| 25 | const size_t kMatMulX2Idx = 1; | 25 | const size_t kMatMulX2Idx = 1; |
| 26 | -const size_t kAddmmSelfMinShapeSize = 1; | ||
| 27 | const size_t kMatmulV2MinShapeSize = 2; | 26 | const size_t kMatmulV2MinShapeSize = 2; |
| 28 | const size_t kAddmmMaxShapeSize = 3; | 27 | const size_t kAddmmMaxShapeSize = 3; |
| 29 | const size_t kOutputIdx = 0; | 28 | const size_t kOutputIdx = 0; |
| 30 | -constexpr float epsilon = 1e-6f; | ||
| 31 | 29 | ||
| 32 | ge::graphStatus InferShapeForGemmV3(InferShapeContext* context) | 30 | ge::graphStatus InferShapeForGemmV3(InferShapeContext* context) |
| 33 | { | 31 | { |
| @@ -37,8 +35,6 @@ ge::graphStatus InferShapeForGemmV3(InferShapeContext* context) | |||
| 37 | auto shape_c = context->GetOptionalInputShape(kMatmulV2MinShapeSize); | 35 | auto shape_c = context->GetOptionalInputShape(kMatmulV2MinShapeSize); |
| 38 | auto shape_out = context->GetOutputShape(kOutputIdx); | 36 | auto shape_out = context->GetOutputShape(kOutputIdx); |
| 39 | auto attrs = context->GetAttrs(); | 37 | auto attrs = context->GetAttrs(); |
| 40 | - auto dtype_c = context->GetInputDesc(kMatmulV2MinShapeSize)->GetDataType(); | ||
| 41 | - auto dtype_out = context->GetOutputDesc(kOutputIdx)->GetDataType(); | ||
| 42 | // 当前仅支持累加场景,shape_c不支持为空 | 38 | // 当前仅支持累加场景,shape_c不支持为空 |
| 43 | OP_CHECK_IF( | 39 | OP_CHECK_IF( |
| 44 | shape_a == nullptr || shape_b == nullptr || shape_c == nullptr || shape_out == nullptr || attrs == nullptr, | 40 | shape_a == nullptr || shape_b == nullptr || shape_c == nullptr || shape_out == nullptr || attrs == nullptr, |
| @@ -57,25 +53,17 @@ ge::graphStatus InferShapeForGemmV3(InferShapeContext* context) | |||
| 57 | "gemmv3 a_shape: %s, b_shape: %s, c_shape:%s, transpose_a: %d, transpose_b: %d, enable_hf32: %d", | 53 | "gemmv3 a_shape: %s, b_shape: %s, c_shape:%s, transpose_a: %d, transpose_b: %d, enable_hf32: %d", |
| 58 | Shape2String(*shape_a).c_str(), Shape2String(*shape_b).c_str(), Shape2String(*shape_c).c_str(), *trans_a, | 54 | Shape2String(*shape_a).c_str(), Shape2String(*shape_b).c_str(), Shape2String(*shape_c).c_str(), *trans_a, |
| 59 | *trans_b, *enable_hf32); | 55 | *trans_b, *enable_hf32); |
| 60 | - if (shape_a->GetDimNum() == kMatmulV2MinShapeSize) { // addmm校验 | 56 | + const size_t rank_a = shape_a->GetDimNum(); |
| 61 | - OP_CHECK_IF((shape_a->GetDimNum() != kMatmulV2MinShapeSize || shape_b->GetDimNum() != kMatmulV2MinShapeSize || | 57 | + const size_t rank_b = shape_b->GetDimNum(); |
| 62 | - shape_c->GetDimNum() != kMatmulV2MinShapeSize), | 58 | + const size_t rank_c = shape_c->GetDimNum(); |
| 63 | - CUBE_INNER_ERR_REPORT(op_name, "addmm input dim num[%zu] [%zu] [%zu]is not 2!", | 59 | + OP_CHECK_IF(rank_a != kMatmulV2MinShapeSize && rank_a != kAddmmMaxShapeSize, |
| 64 | - shape_a->GetDimNum(), shape_b->GetDimNum(), shape_c->GetDimNum()), | 60 | + CUBE_INNER_ERR_REPORT(op_name, "unsupported x1 rank %zu, expected 2 or 3", rank_a), |
| 65 | - return ge::GRAPH_FAILED); | 61 | + return ge::GRAPH_FAILED); |
| 66 | - } else if (shape_a->GetDimNum() == kAddmmMaxShapeSize) { // baddbmm校验 | 62 | + OP_CHECK_IF(rank_b != rank_a, CUBE_INNER_ERR_REPORT(op_name, "x1 and x2 rank mismatch: %zu vs %zu", rank_a, rank_b), |
| 67 | - OP_CHECK_IF((shape_a->GetDimNum() != kAddmmMaxShapeSize || shape_b->GetDimNum() != kAddmmMaxShapeSize || | 63 | + return ge::GRAPH_FAILED); |
| 68 | - shape_c->GetDimNum() != kAddmmMaxShapeSize), | 64 | + OP_CHECK_IF(rank_c == 0 || rank_c > rank_a, |
| 69 | - CUBE_INNER_ERR_REPORT(op_name, "baddbmm input x1、x2、self dim num[%zu] [%zu] [%zu]is not 3!", | 65 | + CUBE_INNER_ERR_REPORT(op_name, "invalid bias rank %zu for output rank %zu", rank_c, rank_a), |
| 70 | - shape_a->GetDimNum(), shape_b->GetDimNum(), shape_c->GetDimNum()), | 66 | + return ge::GRAPH_FAILED); |
| 71 | - return ge::GRAPH_FAILED); | ||
| 72 | - } else { | ||
| 73 | - OP_CHECK_IF( | ||
| 74 | - shape_a->GetDimNum() != kAddmmMaxShapeSize && shape_a->GetDimNum() != kMatmulV2MinShapeSize, | ||
| 75 | - CUBE_INNER_ERR_REPORT(op_name, "unsupport addmm/baddbmm input x1 dim num[%zu], it should be 2 or 3!", | ||
| 76 | - shape_a->GetDimNum()), | ||
| 77 | - return ge::GRAPH_FAILED); | ||
| 78 | - } | ||
| 79 | 67 | ||
| 80 | size_t idx_m = static_cast<size_t>(*trans_a); | 68 | size_t idx_m = static_cast<size_t>(*trans_a); |
| 81 | size_t idx_k_a = idx_m == 0UL ? 1UL : 0UL; | 69 | size_t idx_k_a = idx_m == 0UL ? 1UL : 0UL; |
| @@ -92,23 +80,7 @@ ge::graphStatus InferShapeForGemmV3(InferShapeContext* context) | |||
| 92 | CUBE_INNER_ERR_REPORT(op_name, "The k-axis of a(%ld) and b(%ld) tensors must be the same", | 80 | CUBE_INNER_ERR_REPORT(op_name, "The k-axis of a(%ld) and b(%ld) tensors must be the same", |
| 93 | shape_a->GetDim(idx_k_a), shape_b->GetDim(idx_k_b)), | 81 | shape_a->GetDim(idx_k_a), shape_b->GetDim(idx_k_b)), |
| 94 | return ge::GRAPH_FAILED); | 82 | return ge::GRAPH_FAILED); |
| 95 | - // 校验c和a b 的M N是否相同 | 83 | + uint32_t outputDimNum = rank_a; |
| 96 | - if (shape_a->GetDimNum() == kMatmulV2MinShapeSize) { | ||
| 97 | - OP_CHECK_IF(shape_a->GetDim(idx_m) != shape_c->GetDim(0) || shape_b->GetDim(idx_n) != shape_c->GetDim(1), | ||
| 98 | - CUBE_INNER_ERR_REPORT(op_name, "The m(%ld), n(%ld) tensors must be the same c(%ld, %ld)", | ||
| 99 | - shape_a->GetDim(idx_m), shape_b->GetDim(idx_n), shape_c->GetDim(0), | ||
| 100 | - shape_c->GetDim(1)), | ||
| 101 | - return ge::GRAPH_FAILED); | ||
| 102 | - } else if (shape_a->GetDimNum() == kAddmmMaxShapeSize) { // 当前不支持self与mmout的broadcast | ||
| 103 | - OP_CHECK_IF( | ||
| 104 | - shape_a->GetDim(idx_m) != shape_c->GetDim(1) || shape_b->GetDim(idx_n) != shape_c->GetDim(2) || | ||
| 105 | - shape_a->GetDim(0) != shape_c->GetDim(0), | ||
| 106 | - CUBE_INNER_ERR_REPORT(op_name, "The b(%ld), m(%ld), n(%ld) tensors must be the same c(%ld, %ld, %ld)", | ||
| 107 | - shape_a->GetDim(0), shape_a->GetDim(idx_m), shape_b->GetDim(idx_n), | ||
| 108 | - shape_c->GetDim(0), shape_c->GetDim(1), shape_c->GetDim(2)), | ||
| 109 | - return ge::GRAPH_FAILED); | ||
| 110 | - } | ||
| 111 | - uint32_t outputDimNum = shape_a->GetDimNum(); | ||
| 112 | shape_out->SetDimNum(outputDimNum); | 84 | shape_out->SetDimNum(outputDimNum); |
| 113 | if (outputDimNum == kMatmulV2MinShapeSize) { // gemmv3或addmm场景 | 85 | if (outputDimNum == kMatmulV2MinShapeSize) { // gemmv3或addmm场景 |
| 114 | shape_out->SetDim(0, shape_a->GetDim(idx_m)); | 86 | shape_out->SetDim(0, shape_a->GetDim(idx_m)); |
| @@ -118,20 +90,15 @@ ge::graphStatus InferShapeForGemmV3(InferShapeContext* context) | |||
| 118 | shape_out->SetDim(1, shape_a->GetDim(idx_m)); | 90 | shape_out->SetDim(1, shape_a->GetDim(idx_m)); |
| 119 | shape_out->SetDim(2, shape_b->GetDim(idx_n)); | 91 | shape_out->SetDim(2, shape_b->GetDim(idx_n)); |
| 120 | } | 92 | } |
| 121 | - // 校验y和c是否相同shape | 93 | + const size_t out_axis_offset = outputDimNum - rank_c; |
| 122 | - if (shape_a->GetDimNum() == kMatmulV2MinShapeSize) { | 94 | + for (size_t c_axis = 0; c_axis < rank_c; ++c_axis) { |
| 95 | + const int64_t c_dim = shape_c->GetDim(c_axis); | ||
| 96 | + const int64_t out_dim = shape_out->GetDim(out_axis_offset + c_axis); | ||
| 123 | OP_CHECK_IF( | 97 | OP_CHECK_IF( |
| 124 | - shape_c->GetDim(0) != shape_out->GetDim(0) || shape_c->GetDim(1) != shape_out->GetDim(1), | 98 | + c_dim != 1 && c_dim != out_dim, |
| 125 | - CUBE_INNER_ERR_REPORT(op_name, "The y(%ld, %ld) tensors must be same with c(%ld, %ld)", | 99 | + CUBE_INNER_ERR_REPORT(op_name, "bias dim %ld at axis %zu cannot broadcast to output dim %ld at axis %zu", |
| 126 | - shape_out->GetDim(0), shape_out->GetDim(1), shape_c->GetDim(0), shape_c->GetDim(1)), | 100 | + c_dim, c_axis, out_dim, out_axis_offset + c_axis), |
| 127 | return ge::GRAPH_FAILED); | 101 | return ge::GRAPH_FAILED); |
| 128 | - } else if (shape_a->GetDimNum() == kAddmmMaxShapeSize) { // 当前不支持self与mmout的broadcast | ||
| 129 | - OP_CHECK_IF(shape_c->GetDim(0) != shape_out->GetDim(0) || shape_c->GetDim(1) != shape_out->GetDim(1) || | ||
| 130 | - shape_c->GetDim(2) != shape_out->GetDim(2), | ||
| 131 | - CUBE_INNER_ERR_REPORT(op_name, "The y(%ld, %ld, %ld) tensors must be same with c(%ld, %ld, %ld)", | ||
| 132 | - shape_out->GetDim(0), shape_out->GetDim(1), shape_out->GetDim(2), | ||
| 133 | - shape_c->GetDim(0), shape_c->GetDim(1), shape_c->GetDim(2)), | ||
| 134 | - return ge::GRAPH_FAILED); | ||
| 135 | } | 102 | } |
| 136 | 103 | ||
| 137 | OP_LOGI(op_name, "end infershape for gemmv3."); | 104 | OP_LOGI(op_name, "end infershape for gemmv3."); |
| @@ -29,9 +29,14 @@ static constexpr size_t INDEX_ATTR_BETA = 1; | |||
| 29 | static constexpr size_t INDEX_ATTR_TRANS_A = 2; | 29 | static constexpr size_t INDEX_ATTR_TRANS_A = 2; |
| 30 | static constexpr size_t INDEX_ATTR_TRANS_B = 3; | 30 | static constexpr size_t INDEX_ATTR_TRANS_B = 3; |
| 31 | static constexpr size_t MIN_NUM_DIM = 2; | 31 | static constexpr size_t MIN_NUM_DIM = 2; |
| 32 | +static constexpr size_t MAX_NUM_DIM = 3; | ||
| 32 | static constexpr size_t LAST_DIM_OFFSET = 1; | 33 | static constexpr size_t LAST_DIM_OFFSET = 1; |
| 33 | static constexpr size_t SECOND_LAST_DIM_OFFSET = 2; | 34 | static constexpr size_t SECOND_LAST_DIM_OFFSET = 2; |
| 34 | static constexpr uint64_t NUM_BUFFER = 2; | 35 | static constexpr uint64_t NUM_BUFFER = 2; |
| 36 | +static constexpr uint64_t DATA_BLOCK_SIZE = 32; | ||
| 37 | +static constexpr uint64_t FP32_SIZE = sizeof(float); | ||
| 38 | +static constexpr uint64_t FP32_PER_BLOCK = DATA_BLOCK_SIZE / FP32_SIZE; | ||
| 39 | +static constexpr uint64_t SIZE_UB_C = 32768; | ||
| 35 | } // namespace | 40 | } // namespace |
| 36 | 41 | ||
| 37 | namespace optiling { | 42 | namespace optiling { |
| @@ -75,6 +80,43 @@ ge::graphStatus GemmV3BaseTiling::GetInputDims(const gert::Shape& shapeA, const | |||
| 75 | return ge::GRAPH_SUCCESS; | 80 | return ge::GRAPH_SUCCESS; |
| 76 | } | 81 | } |
| 77 | 82 | ||
| 83 | +ge::graphStatus GemmV3BaseTiling::CalcBiasBroadcastInfo() | ||
| 84 | +{ | ||
| 85 | + const auto shapeC = context_->GetInputShape(INDEX_C)->GetOriginShape(); | ||
| 86 | + const auto shapeA = context_->GetInputShape(INDEX_A)->GetOriginShape(); | ||
| 87 | + const size_t rankC = shapeC.GetDimNum(); | ||
| 88 | + const size_t rankOut = shapeA.GetDimNum(); | ||
| 89 | + OP_TILING_CHECK(rankC == 0 || rankC > rankOut || rankOut > MAX_NUM_DIM, | ||
| 90 | + OP_LOGE(params_.opName, "invalid bias rank %zu for output rank %zu", rankC, rankOut), | ||
| 91 | + return ge::GRAPH_FAILED); | ||
| 92 | + | ||
| 93 | + const int64_t dimN = shapeC.GetDim(rankC - LAST_DIM_OFFSET); | ||
| 94 | + const int64_t dimM = rankC >= MIN_NUM_DIM ? shapeC.GetDim(rankC - SECOND_LAST_DIM_OFFSET) : 1; | ||
| 95 | + const int64_t dimB = rankC >= MAX_NUM_DIM ? shapeC.GetDim(rankC - MAX_NUM_DIM) : 1; | ||
| 96 | + OP_TILING_CHECK( | ||
| 97 | + (dimN != 1 && dimN != params_.n) || (dimM != 1 && dimM != params_.m) || | ||
| 98 | + (rankOut == MAX_NUM_DIM && dimB != 1 && dimB != params_.batchSize), | ||
| 99 | + OP_LOGE(params_.opName, "bias shape %s cannot broadcast to output [%llu, %llu, %llu] with rank %zu", | ||
| 100 | + Ops::Base::ToString(shapeC).c_str(), static_cast<unsigned long long>(params_.batchSize), | ||
| 101 | + static_cast<unsigned long long>(params_.m), static_cast<unsigned long long>(params_.n), rankOut), | ||
| 102 | + return ge::GRAPH_FAILED); | ||
| 103 | + | ||
| 104 | + cNStride_ = dimN == 1 ? 0 : 1; | ||
| 105 | + cMStride_ = dimM == 1 ? 0 : static_cast<uint64_t>(dimN); | ||
| 106 | + cBatchStride_ = rankC < MAX_NUM_DIM || dimB == 1 ? 0 : static_cast<uint64_t>(dimM * dimN); | ||
| 107 | + | ||
| 108 | + if (dimM == 1 && dimN == 1) { | ||
| 109 | + biasBroadcastType_ = BIAS_BCAST_SCALAR; | ||
| 110 | + } else if (dimM == 1) { | ||
| 111 | + biasBroadcastType_ = BIAS_BCAST_N; | ||
| 112 | + } else if (dimN == 1) { | ||
| 113 | + biasBroadcastType_ = BIAS_BCAST_M; | ||
| 114 | + } else { | ||
| 115 | + biasBroadcastType_ = BIAS_BCAST_NONE; | ||
| 116 | + } | ||
| 117 | + return ge::GRAPH_SUCCESS; | ||
| 118 | +} | ||
| 119 | + | ||
| 78 | bool GemmV3BaseTiling::IsCapable() | 120 | bool GemmV3BaseTiling::IsCapable() |
| 79 | { | 121 | { |
| 80 | if (hwInfo_.socVersion != platform_ascendc::SocVersion::ASCEND910B && | 122 | if (hwInfo_.socVersion != platform_ascendc::SocVersion::ASCEND910B && |
| @@ -149,9 +191,37 @@ ge::graphStatus GemmV3BaseTiling::GetWorkspaceSize() | |||
| 149 | return ge::GRAPH_SUCCESS; | 191 | return ge::GRAPH_SUCCESS; |
| 150 | } | 192 | } |
| 151 | 193 | ||
| 194 | +ge::graphStatus GemmV3BaseTiling::CheckBiasBroadcastUbSize() | ||
| 195 | +{ | ||
| 196 | + if (biasBroadcastType_ == BIAS_BCAST_NONE) { | ||
| 197 | + return ge::GRAPH_SUCCESS; | ||
| 198 | + } | ||
| 199 | + const uint64_t biasDtypeSize = ge::GetSizeByDataType(context_->GetInputDesc(INDEX_C)->GetDataType()); | ||
| 200 | + const uint64_t biasPerBlock = DATA_BLOCK_SIZE / biasDtypeSize; | ||
| 201 | + const uint64_t maxMRows = (tilingData_.opShape.m0 + 1) / 2; | ||
| 202 | + const uint64_t maxBiasCount = std::max(tilingData_.opShape.n0, maxMRows); | ||
| 203 | + const uint64_t maxBiasCountAlign = (maxBiasCount + biasPerBlock - 1) / biasPerBlock * biasPerBlock; | ||
| 204 | + const uint64_t rawBytes = (maxBiasCountAlign * biasDtypeSize + DATA_BLOCK_SIZE - 1) / DATA_BLOCK_SIZE * | ||
| 205 | + DATA_BLOCK_SIZE; | ||
| 206 | + const uint64_t fp32Bytes = (maxBiasCountAlign * FP32_SIZE + DATA_BLOCK_SIZE - 1) / DATA_BLOCK_SIZE * | ||
| 207 | + DATA_BLOCK_SIZE; | ||
| 208 | + const uint64_t maxBrcbRows = std::max(maxMRows, 1UL); | ||
| 209 | + const uint64_t brcbBytes = (maxBrcbRows + FP32_PER_BLOCK - 1) / FP32_PER_BLOCK * FP32_PER_BLOCK * DATA_BLOCK_SIZE; | ||
| 210 | + OP_TILING_CHECK( | ||
| 211 | + rawBytes + fp32Bytes + brcbBytes > SIZE_UB_C, | ||
| 212 | + OP_LOGE(params_.opName, "broadcast bias UB size exceeds limit: raw[%llu], fp32[%llu], brcb[%llu], limit[%llu]", | ||
| 213 | + static_cast<unsigned long long>(rawBytes), static_cast<unsigned long long>(fp32Bytes), | ||
| 214 | + static_cast<unsigned long long>(brcbBytes), static_cast<unsigned long long>(SIZE_UB_C)), | ||
| 215 | + return ge::GRAPH_FAILED); | ||
| 216 | + return ge::GRAPH_SUCCESS; | ||
| 217 | +} | ||
| 218 | + | ||
| 152 | ge::graphStatus GemmV3BaseTiling::PostTiling() | 219 | ge::graphStatus GemmV3BaseTiling::PostTiling() |
| 153 | { | 220 | { |
| 154 | tiling_.PrintTiling(); | 221 | tiling_.PrintTiling(); |
| 222 | + if (CheckBiasBroadcastUbSize() != ge::GRAPH_SUCCESS) { | ||
[MAJOR] CheckBiasBroadcastUbSize() 在 PostTiling() 中被无条件调用,未判断 biasBroadcastType_。当 biasBroadcastType_ == BIAS_BCAST_NONE(普通 addmm/baddbmm 非 broadcast 场景,属最常见路径)时,kernel 走 PrepareBias 的直接 CopyGmToUbufAlign 分支,根本不使用 ubBiasRaw_/ubBiasFp32_/ubBiasBrcb_ 这三块 buffer,但该检查仍按 broadcast buffer 的预算(maxBiasCount 与 brcb 项)去校验,语义上不匹配且可能误拒绝合法的非 broadcast 配置(例如 M0 大、N0 小的畸形分块)。 文件: 建议修复: 仅在 biasBroadcastType_ != BIAS_BCAST_NONE 时执行该检查: ![]() ![]() | |||
| 223 | + return ge::GRAPH_FAILED; | ||
| 224 | + } | ||
| 155 | size_t sizeTilingData = sizeof(GemmV3TilingData); | 225 | size_t sizeTilingData = sizeof(GemmV3TilingData); |
| 156 | OP_TILING_CHECK(sizeTilingData % sizeof(uint64_t) != 0, | 226 | OP_TILING_CHECK(sizeTilingData % sizeof(uint64_t) != 0, |
| 157 | OP_LOGE(params_.opName, "tiling data size[%zu] is not aligned to 8", sizeTilingData), | 227 | OP_LOGE(params_.opName, "tiling data size[%zu] is not aligned to 8", sizeTilingData), |
| @@ -187,6 +257,11 @@ ge::graphStatus GemmV3BaseTiling::PostTiling() | |||
| 187 | tilingPtr->enShuffleK = tilingData_.enShuffleK; | 257 | tilingPtr->enShuffleK = tilingData_.enShuffleK; |
| 188 | tilingPtr->alpha = alpha_; | 258 | tilingPtr->alpha = alpha_; |
| 189 | tilingPtr->beta = beta_; | 259 | tilingPtr->beta = beta_; |
| 260 | + tilingPtr->biasBroadcastType = biasBroadcastType_; | ||
| 261 | + tilingPtr->reservedBiasBroadcast = 0; | ||
| 262 | + tilingPtr->cBatchStride = cBatchStride_; | ||
| 263 | + tilingPtr->cMStride = cMStride_; | ||
| 264 | + tilingPtr->cNStride = cNStride_; | ||
| 190 | return ge::GRAPH_SUCCESS; | 265 | return ge::GRAPH_SUCCESS; |
| 191 | } | 266 | } |
| 192 | 267 | ||
| @@ -211,6 +286,9 @@ bool GemmV3BaseTiling::InitParams() | |||
| 211 | if (GetInputDims(shapeA, shapeB) != ge::GRAPH_SUCCESS) { | 286 | if (GetInputDims(shapeA, shapeB) != ge::GRAPH_SUCCESS) { |
| 212 | return false; | 287 | return false; |
| 213 | } | 288 | } |
| 289 | + if (CalcBiasBroadcastInfo() != ge::GRAPH_SUCCESS) { | ||
| 290 | + return false; | ||
| 291 | + } | ||
| 214 | 292 | ||
| 215 | OP_LOGD(params_.opName, "params_.m: %d", params_.m); | 293 | OP_LOGD(params_.opName, "params_.m: %d", params_.m); |
| 216 | OP_LOGD(params_.opName, "params_.k: %d", params_.k); | 294 | OP_LOGD(params_.opName, "params_.k: %d", params_.k); |
| @@ -16,6 +16,7 @@ | |||
| 16 | #ifndef GEMM_V3_BASE_TILING_H | 16 | #ifndef GEMM_V3_BASE_TILING_H |
| 17 | #define GEMM_V3_BASE_TILING_H | 17 | #define GEMM_V3_BASE_TILING_H |
| 18 | #include "../../../transpose_batch_mat_mul/op_host/op_tiling/pp_matmul_default.h" | 18 | #include "../../../transpose_batch_mat_mul/op_host/op_tiling/pp_matmul_default.h" |
| 19 | +#include "../../op_kernel/gemm_v3_tiling_data.h" | ||
| 19 | #include "op_host/tiling_base.h" | 20 | #include "op_host/tiling_base.h" |
| 20 | 21 | ||
| 21 | namespace optiling { | 22 | namespace optiling { |
| @@ -52,6 +53,8 @@ protected: | |||
| 52 | private: | 53 | private: |
| 53 | bool InitParams(); | 54 | bool InitParams(); |
| 54 | ge::graphStatus GetInputDims(const gert::Shape& shapeA, const gert::Shape& shapeB); | 55 | ge::graphStatus GetInputDims(const gert::Shape& shapeA, const gert::Shape& shapeB); |
| 56 | + ge::graphStatus CalcBiasBroadcastInfo(); | ||
| 57 | + ge::graphStatus CheckBiasBroadcastUbSize(); | ||
| 55 | 58 | ||
| 56 | private: | 59 | private: |
| 57 | pp_matmul::PpMatMulDefault tiling_; | 60 | pp_matmul::PpMatMulDefault tiling_; |
| @@ -62,6 +65,10 @@ private: | |||
| 62 | uint32_t numBatchB_{0}; | 65 | uint32_t numBatchB_{0}; |
| 63 | float alpha_{0.0f}; | 66 | float alpha_{0.0f}; |
| 64 | float beta_{0.0f}; | 67 | float beta_{0.0f}; |
| 68 | + uint32_t biasBroadcastType_{BIAS_BCAST_NONE}; | ||
| 69 | + uint64_t cBatchStride_{0}; | ||
| 70 | + uint64_t cMStride_{0}; | ||
| 71 | + uint64_t cNStride_{0}; | ||
| 65 | }; | 72 | }; |
| 66 | } // namespace gemm_v3 | 73 | } // namespace gemm_v3 |
| 67 | } // namespace optiling | 74 | } // namespace optiling |
| @@ -54,7 +54,12 @@ class GemmV3BaseKernel { | |||
| 54 | static constexpr uint32_t CONST_32 = 32; | 54 | static constexpr uint32_t CONST_32 = 32; |
| 55 | static constexpr uint32_t CONST_256 = 256; | 55 | static constexpr uint32_t CONST_256 = 256; |
| 56 | static constexpr uint32_t VEC_ITER_REPEAT = 128; | 56 | static constexpr uint32_t VEC_ITER_REPEAT = 128; |
| 57 | - static constexpr uint32_t VEC_ITER_NUMEL = VEC_ITER_REPEAT * 64; | 57 | + static constexpr uint32_t FP32_PER_REPEAT = CONST_256 / sizeof(AccumDtype); |
| 58 | + static constexpr uint32_t FP32_PER_BLOCK = CONST_32 / sizeof(AccumDtype); | ||
| 59 | + static constexpr uint32_t VEC_ITER_NUMEL = VEC_ITER_REPEAT * FP32_PER_REPEAT; | ||
| 60 | + static constexpr uint64_t SIZE_UB_PROD = 65536; | ||
| 61 | + static constexpr uint64_t SIZE_UB_SUM = 65536; | ||
| 62 | + static constexpr uint64_t SIZE_UB_C = 32768; | ||
| 58 | 63 | ||
| 59 | using OcBuffer = OnChipBuffer<ArchType::ASCEND_V220>; | 64 | using OcBuffer = OnChipBuffer<ArchType::ASCEND_V220>; |
| 60 | template <typename Dtype> | 65 | template <typename Dtype> |
| @@ -79,6 +84,13 @@ public: | |||
| 79 | private: | 84 | private: |
| 80 | __aicore__ FORCE_INLINE void InitBufferCube(const OcBuffer& buf); | 85 | __aicore__ FORCE_INLINE void InitBufferCube(const OcBuffer& buf); |
| 81 | __aicore__ FORCE_INLINE void InitBufferVector(const OcBuffer& buf); | 86 | __aicore__ FORCE_INLINE void InitBufferVector(const OcBuffer& buf); |
| 87 | + __aicore__ FORCE_INLINE void PrepareBias(uint64_t offsetC, uint32_t mRows, uint32_t nTileActual, uint64_t count); | ||
| 88 | + __aicore__ FORCE_INLINE void PrepareBroadcastBias(uint64_t offsetC, uint32_t biasCount, bool needBrcb); | ||
| 89 | + __aicore__ FORCE_INLINE void AddBiasN(uint32_t mRows, uint32_t nTileActual, uint32_t nRound); | ||
| 90 | + __aicore__ FORCE_INLINE void AddBiasM(uint32_t mRows, uint32_t nTileActual, uint32_t nRound); | ||
| 91 | + __aicore__ FORCE_INLINE void AddBiasScalar(uint32_t mRows, uint32_t nTileActual, uint32_t nRound); | ||
| 92 | + __aicore__ FORCE_INLINE void AddMatmulResultAndBias(uint32_t mRows, uint32_t nTileActual, uint32_t nRound, | ||
| 93 | + uint64_t numel, uint64_t count); | ||
| 82 | 94 | ||
| 83 | __aicore__ FORCE_INLINE uint64_t GetOffsetA(const uint64_t batchIdx, const uint64_t mTileIdx, | 95 | __aicore__ FORCE_INLINE uint64_t GetOffsetA(const uint64_t batchIdx, const uint64_t mTileIdx, |
| 84 | const uint64_t kTileIdx) | 96 | const uint64_t kTileIdx) |
| @@ -184,6 +196,9 @@ private: | |||
| 184 | AscendC::LocalTensor<BiasDtype> ubC_; | 196 | AscendC::LocalTensor<BiasDtype> ubC_; |
| 185 | AscendC::LocalTensor<OutDtype> ubY_; | 197 | AscendC::LocalTensor<OutDtype> ubY_; |
| 186 | AscendC::LocalTensor<AccumDtype> ubProd_; | 198 | AscendC::LocalTensor<AccumDtype> ubProd_; |
| 199 | + AscendC::LocalTensor<BiasDtype> ubBiasRaw_; | ||
| 200 | + AscendC::LocalTensor<AccumDtype> ubBiasFp32_; | ||
| 201 | + AscendC::LocalTensor<AccumDtype> ubBiasBrcb_; | ||
| 187 | 202 | ||
| 188 | uint32_t numCore_{0}; | 203 | uint32_t numCore_{0}; |
| 189 | uint32_t numBatchA_{0}; | 204 | uint32_t numBatchA_{0}; |
| @@ -205,6 +220,10 @@ private: | |||
| 205 | uint32_t pingPongFlag_{0}; | 220 | uint32_t pingPongFlag_{0}; |
| 206 | float alpha_{0.0f}; | 221 | float alpha_{0.0f}; |
| 207 | float beta_{0.0f}; | 222 | float beta_{0.0f}; |
| 223 | + uint32_t biasBroadcastType_{BIAS_BCAST_NONE}; | ||
| 224 | + uint64_t cBatchStride_{0}; | ||
| 225 | + uint64_t cMStride_{0}; | ||
| 226 | + uint64_t cNStride_{0}; | ||
| 208 | bool enShuffleK_{false}; | 227 | bool enShuffleK_{false}; |
| 209 | }; | 228 | }; |
| 210 | 229 | ||
| @@ -234,6 +253,10 @@ GemmV3BaseKernel<swizzleDirect, transA, transB, InDtype, BiasDtype, OutDtype, Ac | |||
| 234 | nAlign_ = RoundUp<CONST_16>(n_); | 253 | nAlign_ = RoundUp<CONST_16>(n_); |
| 235 | alpha_ = tilingData.alpha; | 254 | alpha_ = tilingData.alpha; |
| 236 | beta_ = tilingData.beta; | 255 | beta_ = tilingData.beta; |
| 256 | + biasBroadcastType_ = tilingData.biasBroadcastType; | ||
| 257 | + cBatchStride_ = tilingData.cBatchStride; | ||
| 258 | + cMStride_ = tilingData.cMStride; | ||
| 259 | + cNStride_ = tilingData.cNStride; | ||
| 237 | #ifdef __DAV_C220_CUBE__ | 260 | #ifdef __DAV_C220_CUBE__ |
| 238 | coreIdx_ = AscendC::GetBlockIdx(); | 261 | coreIdx_ = AscendC::GetBlockIdx(); |
| 239 | #endif | 262 | #endif |
| @@ -533,6 +556,190 @@ GemmV3BaseKernel<swizzleDirect, transA, transB, InDtype, BiasDtype, OutDtype, Ac | |||
| 533 | #endif | 556 | #endif |
| 534 | } | 557 | } |
| 535 | 558 | ||
| 559 | +template <uint32_t swizzleDirect, bool transA, bool transB, typename InDtype, typename BiasDtype, typename OutDtype, | ||
| 560 | + typename AccumDtype, DataFormat formatA, DataFormat formatB> | ||
| 561 | +__aicore__ FORCE_INLINE void GemmV3BaseKernel<swizzleDirect, transA, transB, InDtype, BiasDtype, OutDtype, AccumDtype, | ||
| 562 | + formatA, formatB>::PrepareBroadcastBias(uint64_t offsetC, | ||
| 563 | + uint32_t biasCount, bool needBrcb) | ||
| 564 | +{ | ||
| 565 | +#ifdef __DAV_C220_VEC__ | ||
| 566 | + constexpr uint32_t biasPerBlock = CONST_32 / sizeof(BiasDtype); | ||
| 567 | + uint32_t biasCountAlign = RoundUp<biasPerBlock>(biasCount); | ||
| 568 | + uint8_t rightPadding = static_cast<uint8_t>(biasCountAlign - biasCount); | ||
| 569 | + AscendC::DataCopyExtParams copyParams(1, static_cast<uint32_t>(biasCount * sizeof(BiasDtype)), 0, 0, 0); | ||
| 570 | + AscendC::DataCopyPadExtParams<BiasDtype> padParams(true, 0, rightPadding, static_cast<BiasDtype>(0)); | ||
| 571 | + AscendC::DataCopyPad(ubBiasRaw_, gmC_[offsetC], copyParams, padParams); | ||
| 572 | + SetFlag<HardEvent::MTE2_V>(EVENT_ID0); | ||
| 573 | + WaitFlag<HardEvent::MTE2_V>(EVENT_ID0); | ||
| 574 | + | ||
| 575 | + if constexpr (std::is_same_v<BiasDtype, AccumDtype>) { | ||
| 576 | + AscendC::Muls(ubBiasFp32_, ubBiasRaw_, static_cast<AccumDtype>(beta_), biasCountAlign); | ||
| 577 | + } else { | ||
| 578 | + AscendC::Cast(ubBiasFp32_, ubBiasRaw_, AscendC::RoundMode::CAST_NONE, biasCountAlign); | ||
| 579 | + AscendC::PipeBarrier<PIPE_V>(); | ||
| 580 | + AscendC::Muls(ubBiasFp32_, ubBiasFp32_, static_cast<AccumDtype>(beta_), biasCountAlign); | ||
| 581 | + } | ||
| 582 | + AscendC::PipeBarrier<PIPE_V>(); | ||
| 583 | + if (needBrcb) { | ||
| 584 | + uint8_t brcbRepeat = static_cast<uint8_t>(CeilDiv<FP32_PER_BLOCK>(biasCount)); | ||
| 585 | + AscendC::Brcb(ubBiasBrcb_, ubBiasFp32_, brcbRepeat, AscendC::BrcbRepeatParams(1, FP32_PER_BLOCK)); | ||
| 586 | + AscendC::PipeBarrier<PIPE_V>(); | ||
| 587 | + } | ||
| 588 | +#endif | ||
| 589 | +} | ||
| 590 | + | ||
| 591 | +template <uint32_t swizzleDirect, bool transA, bool transB, typename InDtype, typename BiasDtype, typename OutDtype, | ||
| 592 | + typename AccumDtype, DataFormat formatA, DataFormat formatB> | ||
| 593 | +__aicore__ FORCE_INLINE void GemmV3BaseKernel<swizzleDirect, transA, transB, InDtype, BiasDtype, OutDtype, AccumDtype, | ||
| 594 | + formatA, formatB>::PrepareBias(uint64_t offsetC, uint32_t mRows, | ||
| 595 | + uint32_t nTileActual, uint64_t count) | ||
| 596 | +{ | ||
| 597 | +#ifdef __DAV_C220_VEC__ | ||
| 598 | + if (biasBroadcastType_ != BIAS_BCAST_NONE) { | ||
| 599 | + uint32_t biasCount = biasBroadcastType_ == BIAS_BCAST_N ? nTileActual : | ||
| 600 | + biasBroadcastType_ == BIAS_BCAST_M ? mRows : | ||
| 601 | + 1; | ||
| 602 | + PrepareBroadcastBias(offsetC, biasCount, biasBroadcastType_ != BIAS_BCAST_N); | ||
| 603 | + return; | ||
| 604 | + } | ||
| 605 | + | ||
| 606 | + AscendC::LocalTensor<BiasDtype> dstUbC = ubC_; | ||
| 607 | + if constexpr (std::is_same_v<BiasDtype, AccumDtype>) { | ||
| 608 | + dstUbC = ubSum_; | ||
| 609 | + } | ||
| 610 | + CopyGmToUbufAlign<BiasDtype>(dstUbC, // dst | ||
| 611 | + gmC_[offsetC], // src | ||
| 612 | + 0, // sid | ||
| 613 | + mRows, // nBurst | ||
| 614 | + nTileActual * sizeof(BiasDtype), // lenBurst | ||
| 615 | + 0, // leftPaddingNum | ||
| 616 | + 0, // rightPaddingNum | ||
| 617 | + (n_ - nTileActual) * sizeof(BiasDtype), // srcGap | ||
| 618 | + 0); // dstGap | ||
| 619 | + SetFlag<HardEvent::MTE2_V>(EVENT_ID0); | ||
| 620 | + WaitFlag<HardEvent::MTE2_V>(EVENT_ID0); | ||
| 621 | + for (uint32_t i = 0; i < count; ++i) { | ||
| 622 | + if constexpr (!std::is_same_v<BiasDtype, AccumDtype>) { | ||
| 623 | + AscendC::Cast<AccumDtype, BiasDtype, false>(ubSum_[i * VEC_ITER_NUMEL], // dst | ||
| 624 | + ubC_[i * VEC_ITER_NUMEL], // src | ||
| 625 | + AscendC::RoundMode::CAST_NONE, // mode | ||
| 626 | + (uint64_t)0, // mask | ||
| 627 | + VEC_ITER_REPEAT, // repeat | ||
| 628 | + AscendC::UnaryRepeatParams(1, 1, 8, 4)); | ||
| 629 | + AscendC::PipeBarrier<PIPE_V>(); | ||
| 630 | + } | ||
| 631 | + AscendC::Muls<AccumDtype, false>(ubSum_[i * VEC_ITER_NUMEL], // dst | ||
| 632 | + ubSum_[i * VEC_ITER_NUMEL], // src | ||
| 633 | + beta_, // scalar | ||
| 634 | + (uint64_t)0, // mask (disabled) | ||
| 635 | + (uint8_t)VEC_ITER_REPEAT, // repeat | ||
| 636 | + AscendC::UnaryRepeatParams(1, 1, 8, 8)); | ||
| 637 | + AscendC::PipeBarrier<PIPE_V>(); | ||
| 638 | + } | ||
| 639 | +#endif | ||
| 640 | +} | ||
| 641 | + | ||
| 642 | +template <uint32_t swizzleDirect, bool transA, bool transB, typename InDtype, typename BiasDtype, typename OutDtype, | ||
| 643 | + typename AccumDtype, DataFormat formatA, DataFormat formatB> | ||
| 644 | +__aicore__ FORCE_INLINE void GemmV3BaseKernel<swizzleDirect, transA, transB, InDtype, BiasDtype, OutDtype, AccumDtype, | ||
| 645 | + formatA, formatB>::AddBiasN(uint32_t mRows, uint32_t nTileActual, | ||
| 646 | + uint32_t nRound) | ||
| 647 | +{ | ||
| 648 | +#ifdef __DAV_C220_VEC__ | ||
| 649 | + uint8_t rowStride = static_cast<uint8_t>(nRound * sizeof(AccumDtype) / CONST_32); | ||
| 650 | + for (uint32_t col = 0; col < nTileActual; col += FP32_PER_REPEAT) { | ||
| 651 | + uint32_t width = nTileActual - col < FP32_PER_REPEAT ? nTileActual - col : FP32_PER_REPEAT; | ||
| 652 | + for (uint32_t processedRows = 0; processedRows < mRows; processedRows += VEC_ITER_REPEAT) { | ||
| 653 | + uint32_t batchRows = mRows - processedRows < VEC_ITER_REPEAT ? mRows - processedRows : VEC_ITER_REPEAT; | ||
| 654 | + uint32_t rowOffset = processedRows * nRound; | ||
| 655 | + AscendC::Add(ubSum_[rowOffset + col], ubProd_[rowOffset + col], ubBiasFp32_[col], | ||
| 656 | + static_cast<uint64_t>(width), static_cast<uint8_t>(batchRows), | ||
| 657 | + AscendC::BinaryRepeatParams(1, 1, 1, rowStride, rowStride, 0)); | ||
| 658 | + } | ||
| 659 | + } | ||
| 660 | +#endif | ||
| 661 | +} | ||
| 662 | + | ||
| 663 | +template <uint32_t swizzleDirect, bool transA, bool transB, typename InDtype, typename BiasDtype, typename OutDtype, | ||
| 664 | + typename AccumDtype, DataFormat formatA, DataFormat formatB> | ||
| 665 | +__aicore__ FORCE_INLINE void GemmV3BaseKernel<swizzleDirect, transA, transB, InDtype, BiasDtype, OutDtype, AccumDtype, | ||
| 666 | + formatA, formatB>::AddBiasM(uint32_t mRows, uint32_t nTileActual, | ||
| 667 | + uint32_t nRound) | ||
| 668 | +{ | ||
| 669 | +#ifdef __DAV_C220_VEC__ | ||
| 670 | + uint8_t rowStride = static_cast<uint8_t>(nRound * sizeof(AccumDtype) / CONST_32); | ||
| 671 | + for (uint32_t col = 0; col < nTileActual; col += FP32_PER_REPEAT) { | ||
| 672 | + uint32_t width = nTileActual - col < FP32_PER_REPEAT ? nTileActual - col : FP32_PER_REPEAT; | ||
| 673 | + for (uint32_t processedRows = 0; processedRows < mRows; processedRows += VEC_ITER_REPEAT) { | ||
| 674 | + uint32_t batchRows = mRows - processedRows < VEC_ITER_REPEAT ? mRows - processedRows : VEC_ITER_REPEAT; | ||
| 675 | + uint32_t rowOffset = processedRows * nRound; | ||
| 676 | + uint32_t biasOffset = processedRows * FP32_PER_BLOCK; | ||
| 677 | + AscendC::Add(ubSum_[rowOffset + col], ubProd_[rowOffset + col], ubBiasBrcb_[biasOffset], | ||
| 678 | + static_cast<uint64_t>(width), static_cast<uint8_t>(batchRows), | ||
| 679 | + AscendC::BinaryRepeatParams(1, 1, 0, rowStride, rowStride, 1)); | ||
| 680 | + } | ||
| 681 | + } | ||
| 682 | +#endif | ||
| 683 | +} | ||
| 684 | + | ||
| 685 | +template <uint32_t swizzleDirect, bool transA, bool transB, typename InDtype, typename BiasDtype, typename OutDtype, | ||
| 686 | + typename AccumDtype, DataFormat formatA, DataFormat formatB> | ||
| 687 | +__aicore__ FORCE_INLINE void GemmV3BaseKernel<swizzleDirect, transA, transB, InDtype, BiasDtype, OutDtype, AccumDtype, | ||
| 688 | + formatA, formatB>::AddBiasScalar(uint32_t mRows, uint32_t nTileActual, | ||
| 689 | + uint32_t nRound) | ||
| 690 | +{ | ||
| 691 | +#ifdef __DAV_C220_VEC__ | ||
| 692 | + uint8_t rowStride = static_cast<uint8_t>(nRound * sizeof(AccumDtype) / CONST_32); | ||
| 693 | + for (uint32_t col = 0; col < nTileActual; col += FP32_PER_REPEAT) { | ||
| 694 | + uint32_t width = nTileActual - col < FP32_PER_REPEAT ? nTileActual - col : FP32_PER_REPEAT; | ||
| 695 | + for (uint32_t processedRows = 0; processedRows < mRows; processedRows += VEC_ITER_REPEAT) { | ||
| 696 | + uint32_t batchRows = mRows - processedRows < VEC_ITER_REPEAT ? mRows - processedRows : VEC_ITER_REPEAT; | ||
| 697 | + uint32_t rowOffset = processedRows * nRound; | ||
| 698 | + AscendC::Add(ubSum_[rowOffset + col], ubProd_[rowOffset + col], ubBiasBrcb_, static_cast<uint64_t>(width), | ||
| 699 | + static_cast<uint8_t>(batchRows), | ||
| 700 | + AscendC::BinaryRepeatParams(1, 1, 0, rowStride, rowStride, 0)); | ||
| 701 | + } | ||
| 702 | + } | ||
| 703 | +#endif | ||
| 704 | +} | ||
| 705 | + | ||
| 706 | +template <uint32_t swizzleDirect, bool transA, bool transB, typename InDtype, typename BiasDtype, typename OutDtype, | ||
| 707 | + typename AccumDtype, DataFormat formatA, DataFormat formatB> | ||
| 708 | +__aicore__ FORCE_INLINE void GemmV3BaseKernel<swizzleDirect, transA, transB, InDtype, BiasDtype, OutDtype, AccumDtype, | ||
| 709 | + formatA, formatB>::AddMatmulResultAndBias(uint32_t mRows, | ||
| 710 | + uint32_t nTileActual, | ||
| 711 | + uint32_t nRound, uint64_t numel, | ||
| 712 | + uint64_t count) | ||
| 713 | +{ | ||
| 714 | +#ifdef __DAV_C220_VEC__ | ||
| 715 | + if (biasBroadcastType_ == BIAS_BCAST_NONE) { | ||
| 716 | + for (uint32_t i = 0; i < count; ++i) { | ||
| 717 | + AscendC::Axpy<AccumDtype, AccumDtype, false>(ubSum_[i * VEC_ITER_NUMEL], // dst | ||
| 718 | + ubProd_[i * VEC_ITER_NUMEL], // src0 | ||
| 719 | + alpha_, // src1 | ||
| 720 | + (uint64_t)0, // mask (disabled) | ||
| 721 | + VEC_ITER_REPEAT, // repeat | ||
| 722 | + AscendC::UnaryRepeatParams(1, 1, 8, 8)); | ||
| 723 | + AscendC::PipeBarrier<PIPE_V>(); | ||
| 724 | + } | ||
| 725 | + return; | ||
| 726 | + } | ||
| 727 | + | ||
| 728 | + AscendC::Muls(ubProd_, ubProd_, static_cast<AccumDtype>(alpha_), static_cast<int32_t>(numel)); | ||
| 729 | + AscendC::PipeBarrier<PIPE_V>(); | ||
| 730 | + if (biasBroadcastType_ == BIAS_BCAST_N) { | ||
| 731 | + AddBiasN(mRows, nTileActual, nRound); | ||
| 732 | + } else if (biasBroadcastType_ == BIAS_BCAST_M) { | ||
| 733 | + AddBiasM(mRows, nTileActual, nRound); | ||
| 734 | + } else { | ||
| 735 | + AddBiasScalar(mRows, nTileActual, nRound); | ||
| 736 | + } | ||
| 737 | + AscendC::PipeBarrier<PIPE_V>(); | ||
| 738 | + // Restore the full vector mask because the following Cast<..., false> could reuse the current hardware mask. | ||
| 739 | + AscendC::SetVectorMask<uint8_t>((uint64_t)-1, (uint64_t)-1); | ||
| 740 | +#endif | ||
| 741 | +} | ||
| 742 | + | ||
| 536 | // vector 数据搬运 | 743 | // vector 数据搬运 |
| 537 | template <uint32_t swizzleDirect, bool transA, bool transB, typename InDtype, typename BiasDtype, typename OutDtype, | 744 | template <uint32_t swizzleDirect, bool transA, bool transB, typename InDtype, typename BiasDtype, typename OutDtype, |
| 538 | typename AccumDtype, DataFormat formatA, DataFormat formatB> | 745 | typename AccumDtype, DataFormat formatA, DataFormat formatB> |
| @@ -555,45 +762,15 @@ GemmV3BaseKernel<swizzleDirect, transA, transB, InDtype, BiasDtype, OutDtype, Ac | |||
| 555 | uint32_t sumSrcGap = std::is_same_v<OutDtype, BiasDtype> ? 0 : prodDstGap; | 762 | uint32_t sumSrcGap = std::is_same_v<OutDtype, BiasDtype> ? 0 : prodDstGap; |
| 556 | uint64_t mTileHalf = (mTileActual + 1) / 2; | 763 | uint64_t mTileHalf = (mTileActual + 1) / 2; |
| 557 | uint64_t mTileHalfActual = (AscendC::GetSubBlockIdx() == 0) ? mTileHalf : (mTileActual - mTileHalf); | 764 | uint64_t mTileHalfActual = (AscendC::GetSubBlockIdx() == 0) ? mTileHalf : (mTileActual - mTileHalf); |
| 558 | - uint64_t offsetCy = batchIdx * m_ * n_ + tileIdx.m * mTile_ * n_ + tileIdx.n * nTile_ + | 765 | + uint64_t mOffset = tileIdx.m * mTile_ + AscendC::GetSubBlockIdx() * mTileHalf; |
| 559 | - AscendC::GetSubBlockIdx() * mTileHalf * n_; | 766 | + uint64_t nOffset = tileIdx.n * nTile_; |
| 767 | + uint64_t offsetC = batchIdx * cBatchStride_ + mOffset * cMStride_ + nOffset * cNStride_; | ||
| 768 | + uint64_t offsetY = batchIdx * m_ * n_ + mOffset * n_ + nOffset; | ||
| 560 | uint64_t numel = mTileHalfActual * nRound; | 769 | uint64_t numel = mTileHalfActual * nRound; |
| 561 | uint64_t count = CeilDiv<VEC_ITER_NUMEL>(numel); | 770 | uint64_t count = CeilDiv<VEC_ITER_NUMEL>(numel); |
| 562 | if (mTileHalfActual != 0) { | 771 | if (mTileHalfActual != 0) { |
| 563 | WaitFlag<HardEvent::MTE3_MTE2>(EVENT_ID0); | 772 | WaitFlag<HardEvent::MTE3_MTE2>(EVENT_ID0); |
| 564 | - AscendC::LocalTensor<BiasDtype> dstUbC = ubC_; | 773 | + PrepareBias(offsetC, static_cast<uint32_t>(mTileHalfActual), static_cast<uint32_t>(nTileActual), count); |
| 565 | - if constexpr (std::is_same_v<BiasDtype, AccumDtype>) { | ||
| 566 | - dstUbC = ubSum_; | ||
| 567 | - } | ||
| 568 | - CopyGmToUbufAlign<BiasDtype>(dstUbC, // dst | ||
| 569 | - gmC_[offsetCy], // src | ||
| 570 | - 0, // sid | ||
| 571 | - mTileHalfActual, // nBurst | ||
| 572 | - nTileActual * sizeof(BiasDtype), // lenBurst | ||
| 573 | - 0, // leftPaddingNum | ||
| 574 | - 0, // rightPaddingNum | ||
| 575 | - (n_ - nTileActual) * sizeof(BiasDtype), // srcGap | ||
| 576 | - 0); // dstGap | ||
| 577 | - SetFlag<HardEvent::MTE2_V>(EVENT_ID0); | ||
| 578 | - WaitFlag<HardEvent::MTE2_V>(EVENT_ID0); | ||
| 579 | - for (uint32_t i = 0; i < count; ++i) { | ||
| 580 | - if constexpr (!std::is_same_v<BiasDtype, AccumDtype>) { | ||
| 581 | - AscendC::Cast<AccumDtype, BiasDtype, false>(ubSum_[i * VEC_ITER_NUMEL], // dst | ||
| 582 | - ubC_[i * VEC_ITER_NUMEL], // src | ||
| 583 | - AscendC::RoundMode::CAST_NONE, // mode | ||
| 584 | - (uint64_t)0, // mask | ||
| 585 | - VEC_ITER_REPEAT, // repeat | ||
| 586 | - AscendC::UnaryRepeatParams(1, 1, 8, 4)); | ||
| 587 | - AscendC::PipeBarrier<PIPE_V>(); | ||
| 588 | - } | ||
| 589 | - AscendC::Muls<AccumDtype, false>(ubSum_[i * VEC_ITER_NUMEL], // dst | ||
| 590 | - ubSum_[i * VEC_ITER_NUMEL], // src | ||
| 591 | - beta_, // scalar | ||
| 592 | - (uint64_t)0, // mask (disabled) | ||
| 593 | - (uint8_t)VEC_ITER_REPEAT, // repeat | ||
| 594 | - AscendC::UnaryRepeatParams(1, 1, 8, 8)); | ||
| 595 | - AscendC::PipeBarrier<PIPE_V>(); | ||
| 596 | - } | ||
| 597 | } | 774 | } |
| 598 | AscendC::CrossCoreWaitFlag(C_NOTIFY_V + cvPingPongFlag); | 775 | AscendC::CrossCoreWaitFlag(C_NOTIFY_V + cvPingPongFlag); |
| 599 | if (mTileHalfActual != 0) { | 776 | if (mTileHalfActual != 0) { |
| @@ -611,14 +788,9 @@ GemmV3BaseKernel<swizzleDirect, transA, transB, InDtype, BiasDtype, OutDtype, Ac | |||
| 611 | AscendC::CrossCoreSetFlag<INTRA_BLOCK_SYNC, PIPE_MTE2>(V_NOTIFY_C + cvPingPongFlag); | 788 | AscendC::CrossCoreSetFlag<INTRA_BLOCK_SYNC, PIPE_MTE2>(V_NOTIFY_C + cvPingPongFlag); |
| 612 | SetFlag<HardEvent::MTE2_V>(EVENT_ID1); | 789 | SetFlag<HardEvent::MTE2_V>(EVENT_ID1); |
| 613 | WaitFlag<HardEvent::MTE2_V>(EVENT_ID1); | 790 | WaitFlag<HardEvent::MTE2_V>(EVENT_ID1); |
| 791 | + AddMatmulResultAndBias(static_cast<uint32_t>(mTileHalfActual), static_cast<uint32_t>(nTileActual), | ||
| 792 | + static_cast<uint32_t>(nRound), numel, count); | ||
| 614 | for (uint32_t i = 0; i < count; ++i) { | 793 | for (uint32_t i = 0; i < count; ++i) { |
| 615 | - AscendC::Axpy<AccumDtype, AccumDtype, false>(ubSum_[i * VEC_ITER_NUMEL], // dst | ||
| 616 | - ubProd_[i * VEC_ITER_NUMEL], // src0 | ||
| 617 | - alpha_, // src1 | ||
| 618 | - (uint64_t)0, // mask (disabled) | ||
| 619 | - VEC_ITER_REPEAT, // repeat | ||
| 620 | - AscendC::UnaryRepeatParams(1, 1, 8, 8)); | ||
| 621 | - AscendC::PipeBarrier<PIPE_V>(); | ||
| 622 | if constexpr (!std::is_same_v<OutDtype, AccumDtype>) { | 794 | if constexpr (!std::is_same_v<OutDtype, AccumDtype>) { |
| 623 | AscendC::Cast<OutDtype, AccumDtype, false>(ubY_[i * VEC_ITER_NUMEL], // dst | 795 | AscendC::Cast<OutDtype, AccumDtype, false>(ubY_[i * VEC_ITER_NUMEL], // dst |
| 624 | ubSum_[i * VEC_ITER_NUMEL], // src | 796 | ubSum_[i * VEC_ITER_NUMEL], // src |
| @@ -635,7 +807,7 @@ GemmV3BaseKernel<swizzleDirect, transA, transB, InDtype, BiasDtype, OutDtype, Ac | |||
| 635 | if constexpr (std::is_same_v<OutDtype, AccumDtype>) { | 807 | if constexpr (std::is_same_v<OutDtype, AccumDtype>) { |
| 636 | SrcUbY = ubSum_; | 808 | SrcUbY = ubSum_; |
| 637 | } | 809 | } |
| 638 | - CopyUbufToGmAlign(gmY_[offsetCy], // dst | 810 | + CopyUbufToGmAlign(gmY_[offsetY], // dst |
| 639 | SrcUbY, // src | 811 | SrcUbY, // src |
| 640 | 0, // sid | 812 | 0, // sid |
| 641 | mTileHalfActual, // nBurst | 813 | mTileHalfActual, // nBurst |
| @@ -673,14 +845,21 @@ __aicore__ FORCE_INLINE void GemmV3BaseKernel<swizzleDirect, transA, transB, InD | |||
| 673 | formatA, formatB>::InitBufferVector(const OcBuffer& buf) | 845 | formatA, formatB>::InitBufferVector(const OcBuffer& buf) |
| 674 | { | 846 | { |
| 675 | #ifdef __DAV_C220_VEC__ | 847 | #ifdef __DAV_C220_VEC__ |
| 676 | - uint64_t sizeUbProd = 65536; | 848 | + constexpr uint32_t biasPerBlock = CONST_32 / sizeof(BiasDtype); |
| 677 | - uint64_t sizeUbSum = 65536; | 849 | + uint64_t maxMRows = (mTile_ + 1) / 2; |
| 678 | - uint64_t sizeUbC = 32768; | 850 | + uint64_t maxBiasCount = nTile_ > maxMRows ? nTile_ : maxMRows; |
| 851 | + uint64_t maxBiasCountAlign = RoundUp<biasPerBlock>(maxBiasCount); | ||
| 852 | + uint64_t rawBytes = RoundUp<CONST_32>(maxBiasCountAlign * sizeof(BiasDtype)); | ||
| 853 | + uint64_t fp32Bytes = RoundUp<CONST_32>(maxBiasCountAlign * sizeof(AccumDtype)); | ||
| 854 | + uint64_t biasBaseOffset = SIZE_UB_PROD + SIZE_UB_SUM; | ||
| 679 | ubProd_ = buf.template GetBuffer<BufferType::ASCEND_UB, AccumDtype>(0); | 855 | ubProd_ = buf.template GetBuffer<BufferType::ASCEND_UB, AccumDtype>(0); |
| 680 | - ubSum_ = buf.template GetBuffer<BufferType::ASCEND_UB, AccumDtype>(sizeUbProd); | 856 | + ubSum_ = buf.template GetBuffer<BufferType::ASCEND_UB, AccumDtype>(SIZE_UB_PROD); |
| 681 | - ubC_ = buf.template GetBuffer<BufferType::ASCEND_UB, BiasDtype>(sizeUbProd + sizeUbSum); | 857 | + ubC_ = buf.template GetBuffer<BufferType::ASCEND_UB, BiasDtype>(biasBaseOffset); |
| 682 | - ubY_ = buf.template GetBuffer<BufferType::ASCEND_UB, OutDtype>(sizeUbProd + sizeUbSum + sizeUbC); | 858 | + ubBiasRaw_ = ubC_; |
| 859 | + ubBiasFp32_ = buf.template GetBuffer<BufferType::ASCEND_UB, AccumDtype>(biasBaseOffset + rawBytes); | ||
| 860 | + ubBiasBrcb_ = buf.template GetBuffer<BufferType::ASCEND_UB, AccumDtype>(biasBaseOffset + rawBytes + fp32Bytes); | ||
| 861 | + ubY_ = buf.template GetBuffer<BufferType::ASCEND_UB, OutDtype>(biasBaseOffset + SIZE_UB_C); | ||
| 683 | #endif | 862 | #endif |
| 684 | } | 863 | } |
| 685 | } // namespace PpMatMulNS | 864 | } // namespace PpMatMulNS |
| 686 | -#endif // GEMM_V3_BASE_KERNEL_H | 865 | +#endif // GEMM_V3_BASE_KERNEL_H |
| @@ -18,6 +18,13 @@ | |||
| 18 | 18 | ||
| 19 | #include <cstdint> | 19 | #include <cstdint> |
| 20 | 20 | ||
| 21 | +enum BiasBroadcastType : uint32_t { | ||
| 22 | + BIAS_BCAST_NONE = 0, | ||
| 23 | + BIAS_BCAST_N = 1, // [N]/[B,1,N], broadcast along M | ||
| 24 | + BIAS_BCAST_M = 2, // [M,1]/[B,M,1], broadcast along N | ||
| 25 | + BIAS_BCAST_SCALAR = 3, // [1]/[B,1,1], scalar broadcast | ||
| 26 | +}; | ||
| 27 | + | ||
| 21 | #pragma pack(push, 8) | 28 | #pragma pack(push, 8) |
| 22 | // 8 means 8 bytes aligned | 29 | // 8 means 8 bytes aligned |
| 23 | struct alignas(8) GemmV3TilingData { | 30 | struct alignas(8) GemmV3TilingData { |
| @@ -43,6 +50,12 @@ struct alignas(8) GemmV3TilingData { | |||
| 43 | uint32_t enShuffleK{0}; | 50 | uint32_t enShuffleK{0}; |
| 44 | float alpha{0.0f}; | 51 | float alpha{0.0f}; |
| 45 | float beta{0.0f}; | 52 | float beta{0.0f}; |
| 53 | + uint32_t biasBroadcastType{BIAS_BCAST_NONE}; | ||
| 54 | + uint32_t reservedBiasBroadcast{0}; // Reserved for future bias-broadcast extensions | ||
[Minor-Q] 这里的 ![]() ![]() | |||
| 55 | + uint64_t cBatchStride{0}; // C stride along Batch, in elements; 0 for broadcast. | ||
| 56 | + uint64_t cMStride{0}; // C stride along M, in elements; 0 for broadcast. | ||
| 57 | + uint64_t cNStride{0}; // C stride along N, in elements; 0 for broadcast. | ||
| 46 | }; | 58 | }; |
| 47 | #pragma pack(pop) | 59 | #pragma pack(pop) |
| 48 | -#endif // GEMM_V3_TILING_DATA_H | 60 | +static_assert(sizeof(GemmV3TilingData) % sizeof(uint64_t) == 0, "GemmV3TilingData must be 8-byte aligned"); |
| 61 | +#endif // GEMM_V3_TILING_DATA_H | ||
| @@ -153,4 +153,95 @@ TEST_F(GemmV3InferShape, GemmV3_GemmV3_Test_Enable_Hf32_True) | |||
| 153 | auto output = holder.GetContext<gert::InferShapeContext>()->GetOutputShape(0); | 153 | auto output = holder.GetContext<gert::InferShapeContext>()->GetOutputShape(0); |
| 154 | EXPECT_EQ(Ops::Base::ToString(*output), "[2048, 64]"); | 154 | EXPECT_EQ(Ops::Base::ToString(*output), "[2048, 64]"); |
| 155 | } | 155 | } |
| 156 | -} // namespace ge | 156 | + |
| 157 | +TEST_F(GemmV3InferShape, GemmV3_Test_Addmm_BiasBroadcastN) | ||
| 158 | +{ | ||
| 159 | + auto infer_shape_func = gert::OpImplRegistry::GetInstance().GetOpImpl("GemmV3")->infer_shape; | ||
| 160 | + gert::StorageShape a_shape = {{32, 64}, {32, 64}}; | ||
| 161 | + gert::StorageShape b_shape = {{64, 128}, {64, 128}}; | ||
| 162 | + gert::StorageShape c_shape = {{128}, {128}}; | ||
| 163 | + gert::StorageShape output_shape = {{}, {}}; | ||
| 164 | + auto holder = gert::InferShapeContextFaker() | ||
| 165 | + .NodeIoNum(3, 1) | ||
| 166 | + .IrInstanceNum({1, 1, 1}) | ||
| 167 | + .InputShapes({&a_shape, &b_shape, &c_shape}) | ||
| 168 | + .OutputShapes({&output_shape}) | ||
| 169 | + .NodeAttrs({{"alpha", Ops::NN::AnyValue::CreateFrom<float>(1.0f)}, | ||
| 170 | + {"beta", Ops::NN::AnyValue::CreateFrom<float>(1.0f)}, | ||
| 171 | + {"transpose_a", Ops::NN::AnyValue::CreateFrom<bool>(false)}, | ||
| 172 | + {"transpose_b", Ops::NN::AnyValue::CreateFrom<bool>(false)}, | ||
| 173 | + {"enable_hf32", Ops::NN::AnyValue::CreateFrom<bool>(false)}}) | ||
| 174 | + .Build(); | ||
| 175 | + | ||
| 176 | + EXPECT_EQ(infer_shape_func(holder.GetContext<gert::InferShapeContext>()), ge::GRAPH_SUCCESS); | ||
| 177 | + EXPECT_EQ(Ops::Base::ToString(*holder.GetContext<gert::InferShapeContext>()->GetOutputShape(0)), "[32, 128]"); | ||
| 178 | +} | ||
| 179 | + | ||
| 180 | +TEST_F(GemmV3InferShape, GemmV3_Test_Addmm_BiasBroadcastM) | ||
| 181 | +{ | ||
| 182 | + auto infer_shape_func = gert::OpImplRegistry::GetInstance().GetOpImpl("GemmV3")->infer_shape; | ||
| 183 | + gert::StorageShape a_shape = {{32, 64}, {32, 64}}; | ||
| 184 | + gert::StorageShape b_shape = {{64, 128}, {64, 128}}; | ||
| 185 | + gert::StorageShape c_shape = {{32, 1}, {32, 1}}; | ||
| 186 | + gert::StorageShape output_shape = {{}, {}}; | ||
| 187 | + auto holder = gert::InferShapeContextFaker() | ||
| 188 | + .NodeIoNum(3, 1) | ||
| 189 | + .IrInstanceNum({1, 1, 1}) | ||
| 190 | + .InputShapes({&a_shape, &b_shape, &c_shape}) | ||
| 191 | + .OutputShapes({&output_shape}) | ||
| 192 | + .NodeAttrs({{"alpha", Ops::NN::AnyValue::CreateFrom<float>(1.0f)}, | ||
| 193 | + {"beta", Ops::NN::AnyValue::CreateFrom<float>(1.0f)}, | ||
| 194 | + {"transpose_a", Ops::NN::AnyValue::CreateFrom<bool>(false)}, | ||
| 195 | + {"transpose_b", Ops::NN::AnyValue::CreateFrom<bool>(false)}, | ||
| 196 | + {"enable_hf32", Ops::NN::AnyValue::CreateFrom<bool>(false)}}) | ||
| 197 | + .Build(); | ||
| 198 | + | ||
| 199 | + EXPECT_EQ(infer_shape_func(holder.GetContext<gert::InferShapeContext>()), ge::GRAPH_SUCCESS); | ||
| 200 | + EXPECT_EQ(Ops::Base::ToString(*holder.GetContext<gert::InferShapeContext>()->GetOutputShape(0)), "[32, 128]"); | ||
| 201 | +} | ||
| 202 | + | ||
| 203 | +TEST_F(GemmV3InferShape, GemmV3_Test_Baddbmm_PerBatchScalar) | ||
| 204 | +{ | ||
| 205 | + auto infer_shape_func = gert::OpImplRegistry::GetInstance().GetOpImpl("GemmV3")->infer_shape; | ||
| 206 | + gert::StorageShape a_shape = {{2, 32, 64}, {2, 32, 64}}; | ||
| 207 | + gert::StorageShape b_shape = {{2, 64, 128}, {2, 64, 128}}; | ||
| 208 | + gert::StorageShape c_shape = {{2, 1, 1}, {2, 1, 1}}; | ||
| 209 | + gert::StorageShape output_shape = {{}, {}}; | ||
| 210 | + auto holder = gert::InferShapeContextFaker() | ||
| 211 | + .NodeIoNum(3, 1) | ||
| 212 | + .IrInstanceNum({1, 1, 1}) | ||
| 213 | + .InputShapes({&a_shape, &b_shape, &c_shape}) | ||
| 214 | + .OutputShapes({&output_shape}) | ||
| 215 | + .NodeAttrs({{"alpha", Ops::NN::AnyValue::CreateFrom<float>(1.0f)}, | ||
| 216 | + {"beta", Ops::NN::AnyValue::CreateFrom<float>(1.0f)}, | ||
| 217 | + {"transpose_a", Ops::NN::AnyValue::CreateFrom<bool>(false)}, | ||
| 218 | + {"transpose_b", Ops::NN::AnyValue::CreateFrom<bool>(false)}, | ||
| 219 | + {"enable_hf32", Ops::NN::AnyValue::CreateFrom<bool>(false)}}) | ||
| 220 | + .Build(); | ||
| 221 | + | ||
| 222 | + EXPECT_EQ(infer_shape_func(holder.GetContext<gert::InferShapeContext>()), ge::GRAPH_SUCCESS); | ||
| 223 | + EXPECT_EQ(Ops::Base::ToString(*holder.GetContext<gert::InferShapeContext>()->GetOutputShape(0)), "[2, 32, 128]"); | ||
| 224 | +} | ||
| 225 | + | ||
| 226 | +TEST_F(GemmV3InferShape, GemmV3_Test_BiasBroadcastInvalid) | ||
| 227 | +{ | ||
| 228 | + auto infer_shape_func = gert::OpImplRegistry::GetInstance().GetOpImpl("GemmV3")->infer_shape; | ||
| 229 | + gert::StorageShape a_shape = {{2, 32, 64}, {2, 32, 64}}; | ||
| 230 | + gert::StorageShape b_shape = {{2, 64, 128}, {2, 64, 128}}; | ||
| 231 | + gert::StorageShape c_shape = {{2, 31, 1}, {2, 31, 1}}; | ||
| 232 | + gert::StorageShape output_shape = {{}, {}}; | ||
| 233 | + auto holder = gert::InferShapeContextFaker() | ||
| 234 | + .NodeIoNum(3, 1) | ||
| 235 | + .IrInstanceNum({1, 1, 1}) | ||
| 236 | + .InputShapes({&a_shape, &b_shape, &c_shape}) | ||
| 237 | + .OutputShapes({&output_shape}) | ||
| 238 | + .NodeAttrs({{"alpha", Ops::NN::AnyValue::CreateFrom<float>(1.0f)}, | ||
| 239 | + {"beta", Ops::NN::AnyValue::CreateFrom<float>(1.0f)}, | ||
| 240 | + {"transpose_a", Ops::NN::AnyValue::CreateFrom<bool>(false)}, | ||
| 241 | + {"transpose_b", Ops::NN::AnyValue::CreateFrom<bool>(false)}, | ||
| 242 | + {"enable_hf32", Ops::NN::AnyValue::CreateFrom<bool>(false)}}) | ||
| 243 | + .Build(); | ||
| 244 | + | ||
| 245 | + EXPECT_EQ(infer_shape_func(holder.GetContext<gert::InferShapeContext>()), ge::GRAPH_FAILED); | ||
| 246 | +} | ||
| 247 | +} // namespace ge | ||
| @@ -18,6 +18,7 @@ | |||
| 18 | #include "register/op_impl_registry.h" | 18 | #include "register/op_impl_registry.h" |
| 19 | #include "kernel_run_context_facker.h" | 19 | #include "kernel_run_context_facker.h" |
| 20 | #include "../../../../mat_mul_v3/op_host/op_tiling/matmul_v3_compile_info.h" | 20 | #include "../../../../mat_mul_v3/op_host/op_tiling/matmul_v3_compile_info.h" |
| 21 | +#include "../../../op_kernel/gemm_v3_tiling_data.h" | ||
| 21 | #include "platform/platform_infos_def.h" | 22 | #include "platform/platform_infos_def.h" |
| 22 | #include "test_cube_util.h" | 23 | #include "test_cube_util.h" |
| 23 | 24 | ||
| @@ -98,6 +99,12 @@ struct TilingTestParam { | |||
| 98 | 99 | ||
| 99 | ge::DataType input_dtype = DT_FLOAT16; | 100 | ge::DataType input_dtype = DT_FLOAT16; |
| 100 | ge::DataType y_dtype = DT_FLOAT; | 101 | ge::DataType y_dtype = DT_FLOAT; |
| 102 | + std::initializer_list<int64_t> c_shape = {}; | ||
| 103 | + int32_t expected_bias_broadcast_type = -1; | ||
| 104 | + uint64_t expected_c_batch_stride = 0; | ||
| 105 | + uint64_t expected_c_m_stride = 0; | ||
| 106 | + uint64_t expected_c_n_stride = 0; | ||
| 107 | + ge::graphStatus expected_tiling_status = ge::GRAPH_SUCCESS; | ||
| 101 | }; | 108 | }; |
| 102 | 109 | ||
| 103 | class GemmV3TilingRuntime : public testing::TestWithParam<TilingTestParam> { | 110 | class GemmV3TilingRuntime : public testing::TestWithParam<TilingTestParam> { |
| @@ -123,7 +130,8 @@ TEST_P(GemmV3TilingRuntime, general_cases) | |||
| 123 | TilingTestParam param = GetParam(); | 130 | TilingTestParam param = GetParam(); |
| 124 | gert::StorageShape x1_shape = {param.x1_shape, param.x1_shape}; | 131 | gert::StorageShape x1_shape = {param.x1_shape, param.x1_shape}; |
| 125 | gert::StorageShape x2_shape = {param.x2_shape, param.x2_shape}; | 132 | gert::StorageShape x2_shape = {param.x2_shape, param.x2_shape}; |
| 126 | - gert::StorageShape x3_shape = {param.y_shape, param.y_shape}; | 133 | + auto c_shape = param.c_shape.size() == 0 ? param.y_shape : param.c_shape; |
| 134 | + gert::StorageShape x3_shape = {c_shape, c_shape}; | ||
| 127 | std::vector<gert::StorageShape> output_shapes(1, {param.y_shape, param.y_shape}); | 135 | std::vector<gert::StorageShape> output_shapes(1, {param.y_shape, param.y_shape}); |
| 128 | std::vector<void*> output_shapes_ref(1); | 136 | std::vector<void*> output_shapes_ref(1); |
| 129 | for (size_t i = 0; i < output_shapes.size(); ++i) { | 137 | for (size_t i = 0; i < output_shapes.size(); ++i) { |
| @@ -189,25 +197,51 @@ TEST_P(GemmV3TilingRuntime, general_cases) | |||
| 189 | .Build(); | 197 | .Build(); |
| 190 | 198 | ||
| 191 | auto tiling_context = holder.GetContext<gert::TilingContext>(); | 199 | auto tiling_context = holder.GetContext<gert::TilingContext>(); |
| 192 | - ASSERT_EQ(tiling_func(tiling_context), ge::GRAPH_SUCCESS); | 200 | + auto tiling_status = tiling_func(tiling_context); |
| 201 | + ASSERT_EQ(tiling_status, param.expected_tiling_status); | ||
| 202 | + if (tiling_status != ge::GRAPH_SUCCESS) { | ||
| 203 | + return; | ||
| 204 | + } | ||
| 193 | uint64_t tiling_key = tiling_context->GetTilingKey(); | 205 | uint64_t tiling_key = tiling_context->GetTilingKey(); |
| 194 | uint32_t block_dim = tiling_context->GetBlockDim(); | 206 | uint32_t block_dim = tiling_context->GetBlockDim(); |
| 195 | auto tiling_data_result = TilingData2Str(tiling_context->GetRawTilingData(), param.case_name); | 207 | auto tiling_data_result = TilingData2Str(tiling_context->GetRawTilingData(), param.case_name); |
| 196 | auto golden_tiling_data = GenGoldenTilingData(param.tiling_data, param.case_name); | 208 | auto golden_tiling_data = GenGoldenTilingData(param.tiling_data, param.case_name); |
| 197 | cout << "===== " << tiling_key << " === " << tiling_data_result << std::endl; | 209 | cout << "===== " << tiling_key << " === " << tiling_data_result << std::endl; |
| 198 | - ASSERT_EQ(tiling_key, param.tiling_key); | 210 | + if (param.tiling_key != UINT64_MAX) { |
| 199 | - ASSERT_EQ(block_dim, param.block_dim); | 211 | + ASSERT_EQ(tiling_key, param.tiling_key); |
| 212 | + } | ||
| 213 | + if (param.block_dim != 0) { | ||
| 214 | + ASSERT_EQ(block_dim, param.block_dim); | ||
| 215 | + } | ||
| 216 | + if (param.expected_bias_broadcast_type >= 0) { | ||
| 217 | + ASSERT_EQ(tiling_context->GetRawTilingData()->GetDataSize(), sizeof(GemmV3TilingData)); | ||
| 218 | + auto result = reinterpret_cast<const GemmV3TilingData*>(tiling_context->GetRawTilingData()->GetData()); | ||
| 219 | + EXPECT_EQ(result->biasBroadcastType, static_cast<uint32_t>(param.expected_bias_broadcast_type)); | ||
| 220 | + EXPECT_EQ(result->cBatchStride, param.expected_c_batch_stride); | ||
| 221 | + EXPECT_EQ(result->cMStride, param.expected_c_m_stride); | ||
| 222 | + EXPECT_EQ(result->cNStride, param.expected_c_n_stride); | ||
| 223 | + } | ||
| 200 | // ASSERT_EQ(tiling_data_result, golden_tiling_data); | 224 | // ASSERT_EQ(tiling_data_result, golden_tiling_data); |
| 201 | } | 225 | } |
| 202 | 226 | ||
| 227 | +static const string kAscend910bCompileInfo = | ||
| 228 | + R"({"_pattern": "GemmV3", "attrs":{"alpha":1.0, "beta":1.0, "transpose_a":false,"transpose_b":false, "enable_hf32":false}, | ||
| 229 | + "binary_attrs":{"bias_flag":false, "nd_flag":true, "split_k_flag":false, "zero_flag":false, "weight_nz":false, "l2_size":201326592},"binary_mode_flag":true, | ||
| 230 | + "block_dim":{"CORE_NUM":24},"corerect_range_flag":null,"dynamic_mode":"dynamic_mkn", "fused_double_operand_num":0, | ||
| 231 | + "hardware_info":{"BT_SIZE":1024, "load3d_constraints":"unknown", "Intrinsic_fix_pipe_l0c2out":true, "Intrinsic_data_move_l12ub":false, "Intrinsic_data_move_l0c2ub":false, "Intrinsic_data_move_out2l1_nd2nz":true, "UB_SIZE":196608, "L2_SIZE":201326592, "L1_SIZE":524288, "L0A_SIZE":65536, "L0B_SIZE":65536, "L0C_SIZE":131072, "CORE_NUM":24, "vector_core_cnt":48, "socVersion":"Ascend910B"}, | ||
| 232 | + "format_a":"ND","format_b":"ND","repo_range":{},"repo_seeds":{}})"; | ||
| 233 | + | ||
| 234 | +static const string kAscend950CompileInfo = | ||
| 235 | + R"({"_pattern": "GemmV3", "attrs":{"alpha":1.0, "beta":1.0, "transpose_a":false,"transpose_b":false, "enable_hf32":false}, | ||
| 236 | + "binary_attrs":{"bias_flag":false, "nd_flag":true, "split_k_flag":false, "zero_flag":false, "weight_nz":false, "l2_size":134217728},"binary_mode_flag":true, | ||
| 237 | + "block_dim":{"CORE_NUM":32},"corerect_range_flag":null,"dynamic_mode":"dynamic_mkn", "fused_double_operand_num":0, | ||
| 238 | + "hardware_info":{"BT_SIZE":4096, "load3d_constraints":"unknown", "Intrinsic_fix_pipe_l0c2out":true, "Intrinsic_data_move_l12ub":false, "Intrinsic_data_move_l0c2ub":false, "Intrinsic_data_move_l12bt":true, "Intrinsic_data_move_out2l1_nd2nz":true, "UB_SIZE":253952, "L2_SIZE":134217728, "L1_SIZE":524288, "L0A_SIZE":65536, "L0B_SIZE":65536, "L0C_SIZE":262144, "CORE_NUM":32, "socVersion":"Ascend950"}, | ||
| 239 | + "format_a":"ND","format_b":"ND","repo_range":{},"repo_seeds":{}})"; | ||
| 240 | + | ||
| 203 | static TilingTestParam general_cases_params[] = { | 241 | static TilingTestParam general_cases_params[] = { |
| 204 | {"GemmV3_950_basic_test01", | 242 | {"GemmV3_950_basic_test01", |
| 205 | "GemmV3", | 243 | "GemmV3", |
| 206 | - R"({"_pattern": "GemmV3", "attrs":{"alpha":1.0, "beta": 1.0, "transpose_a":false,"transpose_b":false, "enable_hf32":false}, | 244 | + kAscend950CompileInfo, |
| 207 | - "binary_attrs":{"bias_flag":false, "nd_flag":true, "split_k_flag":false, "zero_flag":false, "weight_nz": false, "l2_size":134217728},"binary_mode_flag":true, | ||
| 208 | - "block_dim":{"CORE_NUM":32},"corerect_range_flag":null,"dynamic_mode":"dynamic_mkn", "fused_double_operand_num": 0, | ||
| 209 | - "hardware_info": {"BT_SIZE": 4096, "load3d_constraints": "unknown", "Intrinsic_fix_pipe_l0c2out": true, "Intrinsic_data_move_l12ub": false, "Intrinsic_data_move_l0c2ub": false, "Intrinsic_data_move_l12bt": true, "Intrinsic_data_move_out2l1_nd2nz": true, "UB_SIZE": 253952, "L2_SIZE": 134217728, "L1_SIZE": 524288, "L0A_SIZE": 65536, "L0B_SIZE": 65536, "L0C_SIZE": 262144, "CORE_NUM": 32, "socVersion": "Ascend950" }, | ||
| 210 | - "format_a":"ND","format_b":"ND","repo_range":{},"repo_seeds":{}})", | ||
| 211 | ge::FORMAT_ND, | 245 | ge::FORMAT_ND, |
| 212 | ge::FORMAT_ND, | 246 | ge::FORMAT_ND, |
| 213 | ge::FORMAT_ND, | 247 | ge::FORMAT_ND, |
| @@ -227,7 +261,270 @@ static TilingTestParam general_cases_params[] = { | |||
| 227 | 30, | 261 | 30, |
| 228 | 0UL, | 262 | 0UL, |
| 229 | "30 249 311 512 512 48 64 512 48 64 256 4 4 1 1 0 0 0 0 1 1 1 1 2 2 2 2 2 0 0 1 1 1 1 1 0 0 0 4 0"}, | 263 | "30 249 311 512 512 48 64 512 48 64 256 4 4 1 1 0 0 0 0 1 1 1 1 2 2 2 2 2 0 0 1 1 1 1 1 0 0 0 4 0"}, |
| 264 | + {"GemmV3_950_reject_bias_broadcast_n", | ||
| 265 | + "GemmV3", | ||
| 266 | + kAscend950CompileInfo, | ||
| 267 | + ge::FORMAT_ND, | ||
| 268 | + ge::FORMAT_ND, | ||
| 269 | + ge::FORMAT_ND, | ||
| 270 | + ge::FORMAT_ND, | ||
| 271 | + ge::FORMAT_ND, | ||
| 272 | + ge::FORMAT_ND, | ||
| 273 | + false, | ||
| 274 | + false, | ||
| 275 | + 0, | ||
| 276 | + false, | ||
| 277 | + {32, 64}, | ||
| 278 | + {64, 128}, | ||
| 279 | + {32, 128}, | ||
| 280 | + false, | ||
| 281 | + 0, | ||
| 282 | + 0, | ||
| 283 | + 0, | ||
| 284 | + UINT64_MAX, | ||
| 285 | + "", | ||
| 286 | + DT_FLOAT16, | ||
| 287 | + DT_FLOAT, | ||
| 288 | + {128}, | ||
| 289 | + -1, | ||
| 290 | + 0, | ||
| 291 | + 0, | ||
| 292 | + 0, | ||
| 293 | + ge::GRAPH_FAILED}, | ||
| 294 | + {"GemmV3_950_reject_bias_broadcast_m", | ||
| 295 | + "GemmV3", | ||
| 296 | + kAscend950CompileInfo, | ||
| 297 | + ge::FORMAT_ND, | ||
| 298 | + ge::FORMAT_ND, | ||
| 299 | + ge::FORMAT_ND, | ||
| 300 | + ge::FORMAT_ND, | ||
| 301 | + ge::FORMAT_ND, | ||
| 302 | + ge::FORMAT_ND, | ||
| 303 | + false, | ||
| 304 | + false, | ||
| 305 | + 0, | ||
| 306 | + false, | ||
| 307 | + {32, 64}, | ||
| 308 | + {64, 128}, | ||
| 309 | + {32, 128}, | ||
| 310 | + false, | ||
| 311 | + 0, | ||
| 312 | + 0, | ||
| 313 | + 0, | ||
| 314 | + UINT64_MAX, | ||
| 315 | + "", | ||
| 316 | + DT_FLOAT16, | ||
| 317 | + DT_FLOAT, | ||
| 318 | + {32, 1}, | ||
| 319 | + -1, | ||
| 320 | + 0, | ||
| 321 | + 0, | ||
| 322 | + 0, | ||
| 323 | + ge::GRAPH_FAILED}, | ||
| 324 | + {"GemmV3_910B_bias_no_broadcast", | ||
| 325 | + "GemmV3", | ||
| 326 | + kAscend910bCompileInfo, | ||
| 327 | + ge::FORMAT_ND, | ||
| 328 | + ge::FORMAT_ND, | ||
| 329 | + ge::FORMAT_ND, | ||
| 330 | + ge::FORMAT_ND, | ||
| 331 | + ge::FORMAT_ND, | ||
| 332 | + ge::FORMAT_ND, | ||
| 333 | + false, | ||
| 334 | + false, | ||
| 335 | + 0, | ||
| 336 | + false, | ||
| 337 | + {2, 32, 64}, | ||
| 338 | + {2, 64, 128}, | ||
| 339 | + {2, 32, 128}, | ||
| 340 | + false, | ||
| 341 | + 0, | ||
| 342 | + 0, | ||
| 343 | + 0, | ||
| 344 | + UINT64_MAX, | ||
| 345 | + "", | ||
| 346 | + DT_FLOAT16, | ||
| 347 | + DT_FLOAT16, | ||
| 348 | + {2, 32, 128}, | ||
| 349 | + BIAS_BCAST_NONE, | ||
| 350 | + 4096, | ||
| 351 | + 128, | ||
| 352 | + 1}, | ||
| 353 | + {"GemmV3_910B_bias_broadcast_n", | ||
| 354 | + "GemmV3", | ||
| 355 | + kAscend910bCompileInfo, | ||
| 356 | + ge::FORMAT_ND, | ||
| 357 | + ge::FORMAT_ND, | ||
| 358 | + ge::FORMAT_ND, | ||
| 359 | + ge::FORMAT_ND, | ||
| 360 | + ge::FORMAT_ND, | ||
| 361 | + ge::FORMAT_ND, | ||
| 362 | + false, | ||
| 363 | + false, | ||
| 364 | + 0, | ||
| 365 | + false, | ||
| 366 | + {2, 32, 64}, | ||
| 367 | + {2, 64, 128}, | ||
| 368 | + {2, 32, 128}, | ||
| 369 | + false, | ||
| 370 | + 0, | ||
| 371 | + 0, | ||
| 372 | + 0, | ||
| 373 | + UINT64_MAX, | ||
| 374 | + "", | ||
| 375 | + DT_FLOAT16, | ||
| 376 | + DT_FLOAT16, | ||
| 377 | + {128}, | ||
| 378 | + BIAS_BCAST_N, | ||
| 379 | + 0, | ||
| 380 | + 0, | ||
| 381 | + 1}, | ||
| 382 | + {"GemmV3_910B_bias_broadcast_m", | ||
| 383 | + "GemmV3", | ||
| 384 | + kAscend910bCompileInfo, | ||
| 385 | + ge::FORMAT_ND, | ||
| 386 | + ge::FORMAT_ND, | ||
| 387 | + ge::FORMAT_ND, | ||
| 388 | + ge::FORMAT_ND, | ||
| 389 | + ge::FORMAT_ND, | ||
| 390 | + ge::FORMAT_ND, | ||
| 391 | + false, | ||
| 392 | + false, | ||
| 393 | + 0, | ||
| 394 | + false, | ||
| 395 | + {2, 32, 64}, | ||
| 396 | + {2, 64, 128}, | ||
| 397 | + {2, 32, 128}, | ||
| 398 | + false, | ||
| 399 | + 0, | ||
| 400 | + 0, | ||
| 401 | + 0, | ||
| 402 | + UINT64_MAX, | ||
| 403 | + "", | ||
| 404 | + DT_FLOAT16, | ||
| 405 | + DT_FLOAT16, | ||
| 406 | + {32, 1}, | ||
| 407 | + BIAS_BCAST_M, | ||
| 408 | + 0, | ||
| 409 | + 1, | ||
| 410 | + 0}, | ||
| 411 | + {"GemmV3_910B_bias_broadcast_n_per_batch", | ||
| 412 | + "GemmV3", | ||
| 413 | + kAscend910bCompileInfo, | ||
| 414 | + ge::FORMAT_ND, | ||
| 415 | + ge::FORMAT_ND, | ||
| 416 | + ge::FORMAT_ND, | ||
| 417 | + ge::FORMAT_ND, | ||
| 418 | + ge::FORMAT_ND, | ||
| 419 | + ge::FORMAT_ND, | ||
| 420 | + false, | ||
| 421 | + false, | ||
| 422 | + 0, | ||
| 423 | + false, | ||
| 424 | + {2, 32, 64}, | ||
| 425 | + {2, 64, 128}, | ||
| 426 | + {2, 32, 128}, | ||
| 427 | + false, | ||
| 428 | + 0, | ||
| 429 | + 0, | ||
| 430 | + 0, | ||
| 431 | + UINT64_MAX, | ||
| 432 | + "", | ||
| 433 | + DT_FLOAT16, | ||
| 434 | + DT_FLOAT16, | ||
| 435 | + {2, 1, 128}, | ||
| 436 | + BIAS_BCAST_N, | ||
| 437 | + 128, | ||
| 438 | + 0, | ||
| 439 | + 1}, | ||
| 440 | + {"GemmV3_910B_bias_broadcast_m_per_batch", | ||
| 441 | + "GemmV3", | ||
| 442 | + kAscend910bCompileInfo, | ||
| 443 | + ge::FORMAT_ND, | ||
| 444 | + ge::FORMAT_ND, | ||
| 445 | + ge::FORMAT_ND, | ||
| 446 | + ge::FORMAT_ND, | ||
| 447 | + ge::FORMAT_ND, | ||
| 448 | + ge::FORMAT_ND, | ||
| 449 | + false, | ||
| 450 | + false, | ||
| 451 | + 0, | ||
| 452 | + false, | ||
| 453 | + {2, 32, 64}, | ||
| 454 | + {2, 64, 128}, | ||
| 455 | + {2, 32, 128}, | ||
| 456 | + false, | ||
| 457 | + 0, | ||
| 458 | + 0, | ||
| 459 | + 0, | ||
| 460 | + UINT64_MAX, | ||
| 461 | + "", | ||
| 462 | + DT_FLOAT16, | ||
| 463 | + DT_FLOAT16, | ||
| 464 | + {2, 32, 1}, | ||
| 465 | + BIAS_BCAST_M, | ||
| 466 | + 32, | ||
| 467 | + 1, | ||
| 468 | + 0}, | ||
| 469 | + {"GemmV3_910B_bias_broadcast_scalar_per_batch", | ||
| 470 | + "GemmV3", | ||
| 471 | + kAscend910bCompileInfo, | ||
| 472 | + ge::FORMAT_ND, | ||
| 473 | + ge::FORMAT_ND, | ||
| 474 | + ge::FORMAT_ND, | ||
| 475 | + ge::FORMAT_ND, | ||
| 476 | + ge::FORMAT_ND, | ||
| 477 | + ge::FORMAT_ND, | ||
| 478 | + false, | ||
| 479 | + false, | ||
| 480 | + 0, | ||
| 481 | + false, | ||
| 482 | + {2, 32, 64}, | ||
| 483 | + {2, 64, 128}, | ||
| 484 | + {2, 32, 128}, | ||
| 485 | + false, | ||
| 486 | + 0, | ||
| 487 | + 0, | ||
| 488 | + 0, | ||
| 489 | + UINT64_MAX, | ||
| 490 | + "", | ||
| 491 | + DT_FLOAT16, | ||
| 492 | + DT_FLOAT16, | ||
| 493 | + {2, 1, 1}, | ||
| 494 | + BIAS_BCAST_SCALAR, | ||
| 495 | + 1, | ||
| 496 | + 0, | ||
| 497 | + 0}, | ||
| 498 | + {"GemmV3_910B_bias_broadcast_scalar_batch_one_rank3", | ||
| 499 | + "GemmV3", | ||
| 500 | + kAscend910bCompileInfo, | ||
| 501 | + ge::FORMAT_ND, | ||
| 502 | + ge::FORMAT_ND, | ||
| 503 | + ge::FORMAT_ND, | ||
| 504 | + ge::FORMAT_ND, | ||
| 505 | + ge::FORMAT_ND, | ||
| 506 | + ge::FORMAT_ND, | ||
| 507 | + false, | ||
| 508 | + false, | ||
| 509 | + 0, | ||
| 510 | + false, | ||
| 511 | + {1, 32, 64}, | ||
| 512 | + {1, 64, 128}, | ||
| 513 | + {1, 32, 128}, | ||
| 514 | + false, | ||
| 515 | + 0, | ||
| 516 | + 0, | ||
| 517 | + 0, | ||
| 518 | + UINT64_MAX, | ||
| 519 | + "", | ||
| 520 | + DT_FLOAT16, | ||
| 521 | + DT_FLOAT16, | ||
| 522 | + {1, 1, 1}, | ||
| 523 | + BIAS_BCAST_SCALAR, | ||
| 524 | + 0, | ||
| 525 | + 0, | ||
| 526 | + 0}, | ||
| 230 | }; | 527 | }; |
| 231 | 528 | ||
| 232 | INSTANTIATE_TEST_CASE_P(GemmV3, GemmV3TilingRuntime, testing::ValuesIn(general_cases_params)); | 529 | INSTANTIATE_TEST_CASE_P(GemmV3, GemmV3TilingRuntime, testing::ValuesIn(general_cases_params)); |
| 233 | -} // namespace | 530 | +} // namespace |


[MAJOR] brcbBytes 计算高估 8 倍:
brcbBytes = ceil(maxBrcbRows/FP32_PER_BLOCK) * FP32_PER_BLOCK * DATA_BLOCK_SIZE,其中 FP32_PER_BLOCK=8、DATA_BLOCK_SIZE=32,实际结果是 ceil(maxMRows/8)832=ceil(maxMRows/8)*256 字节。而 kernel 侧 Brcb 实际输出为 ceil(biasCount/8)*8 个 float 元素,即 ceil(biasCount/8)84=ceil(biasCount/8)*32 字节(对照 InitBufferVector 中 ubBiasBrcb_ 的分配与 PrepareBroadcastBias 中 Brcb(…, {1, FP32_PER_BLOCK}) 的写法)。正确公式应为* FP32_SIZE(即* DATA_BLOCK_SIZE而非* FP32_PER_BLOCK * DATA_BLOCK_SIZE)。当前写法使 UB 预算校验过度保守,当 M0 较大(如 M0=2048、N0=16 时 brcbBytes 被算成 32768)会误拒绝本来合法的 broadcast 配置。文件:
matmul/gemm_v3/op_host/op_tiling/gemm_v3_base_tiling.cpp行: 206建议修复: 将 brcbBytes 修正为
(maxBrcbRows + FP32_PER_BLOCK - 1) / FP32_PER_BLOCK * DATA_BLOCK_SIZE(即 ceil 后再乘 32 字节),与 kernel 侧 Brcb 输出量保持一致。