Merged
mmv3 support 8d input #8785
yuning_chenAdd on Aug 17
mmv3 support 8d input #8785
Merged
Merged fromyolic/ops-nnmm8d-kernel-a2intocann/ops-nnmaster
Total: 13 files changed+318-155
| @@ -33,10 +33,12 @@ namespace batch_mat_mul_v3 { | |||
| 33 | const std::vector<uint64_t> SUPPORT_ND2NZ_GM2L0_WITHOUT32B = {64, 96, 128, 160, 192, 224, 256, 384}; | 33 | const std::vector<uint64_t> SUPPORT_ND2NZ_GM2L0_WITHOUT32B = {64, 96, 128, 160, 192, 224, 256, 384}; |
| 34 | constexpr uint64_t BLOCK_CUBE = 16; | 34 | constexpr uint64_t BLOCK_CUBE = 16; |
| 35 | constexpr uint64_t NO_BATCH_SHAPE_DIM = 2; | 35 | constexpr uint64_t NO_BATCH_SHAPE_DIM = 2; |
| 36 | -constexpr uint64_t ONE_BATCH_SHAPE_DIM = 3; | 36 | + |
| 37 | -constexpr uint64_t TWO_BATCH_SHAPE_DIM = 4; | 37 | +template <typename ShapeT> |
| 38 | -constexpr uint64_t THREE_BATCH_SHAPE_DIM = 5; | 38 | +static inline uint64_t GetBatchAxisFromEnd(const ShapeT& shape, size_t dims, size_t fromEnd) |
| 39 | -constexpr uint64_t FOUR_BATCH_SHAPE_DIM = 6; | 39 | +{ |
| 40 | + return dims >= fromEnd ? static_cast<uint64_t>(shape.GetDim(dims - fromEnd)) : 1UL; | ||
| 41 | +} | ||
| 40 | constexpr uint64_t ND_NZ_DIM_DIFF = 2; | 42 | constexpr uint64_t ND_NZ_DIM_DIFF = 2; |
| 41 | constexpr uint64_t ALIGNMENT_32 = 32; | 43 | constexpr uint64_t ALIGNMENT_32 = 32; |
| 42 | constexpr uint64_t DEFAULT_SIZE = 32; | 44 | constexpr uint64_t DEFAULT_SIZE = 32; |
| @@ -108,21 +110,31 @@ bool BatchMatmulV3BaseTiling::GetBatchInfo() | |||
| 108 | if (args_.outFormat == ge::FORMAT_FRACTAL_NZ) { | 110 | if (args_.outFormat == ge::FORMAT_FRACTAL_NZ) { |
| 109 | cDims = context_->GetOutputShape(0)->GetStorageShape().GetDimNum() - ND_NZ_DIM_DIFF; | 111 | cDims = context_->GetOutputShape(0)->GetStorageShape().GetDimNum() - ND_NZ_DIM_DIFF; |
| 110 | } | 112 | } |
| 111 | - batchInfo_.batchA3 = aDims > NO_BATCH_SHAPE_DIM ? aShape.GetDim(aDims - ONE_BATCH_SHAPE_DIM) : 1; | 113 | + // Host a0..a5 = tensor dim0..dim5 (pad 1 if missing). Blob mapping is in SetBatchDimInfo. |
| 112 | - batchInfo_.batchA2 = aDims > ONE_BATCH_SHAPE_DIM ? aShape.GetDim(aDims - TWO_BATCH_SHAPE_DIM) : 1; | 114 | + batchInfo_.batchA0 = GetBatchAxisFromEnd(aShape, aDims, 8); |
| 113 | - batchInfo_.batchA1 = aDims > TWO_BATCH_SHAPE_DIM ? aShape.GetDim(aDims - THREE_BATCH_SHAPE_DIM) : 1; | 115 | + batchInfo_.batchA1 = GetBatchAxisFromEnd(aShape, aDims, 7); |
| 114 | - batchInfo_.batchA0 = aDims > THREE_BATCH_SHAPE_DIM ? aShape.GetDim(aDims - FOUR_BATCH_SHAPE_DIM) : 1; | 116 | + batchInfo_.batchA2 = GetBatchAxisFromEnd(aShape, aDims, 6); |
| 115 | - batchInfo_.batchB3 = bDims > NO_BATCH_SHAPE_DIM ? bShape.GetDim(bDims - ONE_BATCH_SHAPE_DIM) : 1; | 117 | + batchInfo_.batchA3 = GetBatchAxisFromEnd(aShape, aDims, 5); |
| 116 | - batchInfo_.batchB2 = bDims > ONE_BATCH_SHAPE_DIM ? bShape.GetDim(bDims - TWO_BATCH_SHAPE_DIM) : 1; | 118 | + batchInfo_.batchA4 = GetBatchAxisFromEnd(aShape, aDims, 4); |
| 117 | - batchInfo_.batchB1 = bDims > TWO_BATCH_SHAPE_DIM ? bShape.GetDim(bDims - THREE_BATCH_SHAPE_DIM) : 1; | 119 | + batchInfo_.batchA5 = GetBatchAxisFromEnd(aShape, aDims, 3); |
| 118 | - batchInfo_.batchB0 = bDims > THREE_BATCH_SHAPE_DIM ? bShape.GetDim(bDims - FOUR_BATCH_SHAPE_DIM) : 1; | 120 | + batchInfo_.batchB0 = GetBatchAxisFromEnd(bShape, bDims, 8); |
| 119 | - batchInfo_.batchC3 = cDims > NO_BATCH_SHAPE_DIM ? cShape.GetDim(cDims - ONE_BATCH_SHAPE_DIM) : 1; | 121 | + batchInfo_.batchB1 = GetBatchAxisFromEnd(bShape, bDims, 7); |
| 120 | - batchInfo_.batchC2 = cDims > ONE_BATCH_SHAPE_DIM ? cShape.GetDim(cDims - TWO_BATCH_SHAPE_DIM) : 1; | 122 | + batchInfo_.batchB2 = GetBatchAxisFromEnd(bShape, bDims, 6); |
| 121 | - batchInfo_.batchC1 = cDims > TWO_BATCH_SHAPE_DIM ? cShape.GetDim(cDims - THREE_BATCH_SHAPE_DIM) : 1; | 123 | + batchInfo_.batchB3 = GetBatchAxisFromEnd(bShape, bDims, 5); |
| 122 | - batchInfo_.batchC0 = cDims > THREE_BATCH_SHAPE_DIM ? cShape.GetDim(cDims - FOUR_BATCH_SHAPE_DIM) : 1; | 124 | + batchInfo_.batchB4 = GetBatchAxisFromEnd(bShape, bDims, 4); |
| 123 | - batchInfo_.batchA = batchInfo_.batchA0 * batchInfo_.batchA1 * batchInfo_.batchA2 * batchInfo_.batchA3; | 125 | + batchInfo_.batchB5 = GetBatchAxisFromEnd(bShape, bDims, 3); |
| 124 | - batchInfo_.batchB = batchInfo_.batchB0 * batchInfo_.batchB1 * batchInfo_.batchB2 * batchInfo_.batchB3; | 126 | + batchInfo_.batchC0 = GetBatchAxisFromEnd(cShape, cDims, 8); |
| 125 | - batchInfo_.batchC = batchInfo_.batchC0 * batchInfo_.batchC1 * batchInfo_.batchC2 * batchInfo_.batchC3; | 127 | + batchInfo_.batchC1 = GetBatchAxisFromEnd(cShape, cDims, 7); |
| 128 | + batchInfo_.batchC2 = GetBatchAxisFromEnd(cShape, cDims, 6); | ||
| 129 | + batchInfo_.batchC3 = GetBatchAxisFromEnd(cShape, cDims, 5); | ||
| 130 | + batchInfo_.batchC4 = GetBatchAxisFromEnd(cShape, cDims, 4); | ||
| 131 | + batchInfo_.batchC5 = GetBatchAxisFromEnd(cShape, cDims, 3); | ||
| 132 | + batchInfo_.batchA = batchInfo_.batchA0 * batchInfo_.batchA1 * batchInfo_.batchA2 * batchInfo_.batchA3 * | ||
| 133 | + batchInfo_.batchA4 * batchInfo_.batchA5; | ||
| 134 | + batchInfo_.batchB = batchInfo_.batchB0 * batchInfo_.batchB1 * batchInfo_.batchB2 * batchInfo_.batchB3 * | ||
| 135 | + batchInfo_.batchB4 * batchInfo_.batchB5; | ||
| 136 | + batchInfo_.batchC = batchInfo_.batchC0 * batchInfo_.batchC1 * batchInfo_.batchC2 * batchInfo_.batchC3 * | ||
| 137 | + batchInfo_.batchC4 * batchInfo_.batchC5; | ||
| 126 | // Check if batch info is valid, if batch is M broadcast to N, return failed. | 138 | // Check if batch info is valid, if batch is M broadcast to N, return failed. |
| 127 | bool batch3Invalid = batchInfo_.batchA3 != batchInfo_.batchB3 && batchInfo_.batchA3 != 1UL && | 139 | bool batch3Invalid = batchInfo_.batchA3 != batchInfo_.batchB3 && batchInfo_.batchA3 != 1UL && |
| 128 | batchInfo_.batchB3 != 1UL; | 140 | batchInfo_.batchB3 != 1UL; |
| @@ -132,7 +144,11 @@ bool BatchMatmulV3BaseTiling::GetBatchInfo() | |||
| 132 | batchInfo_.batchB1 != 1UL; | 144 | batchInfo_.batchB1 != 1UL; |
| 133 | bool batch0Invalid = batchInfo_.batchA0 != batchInfo_.batchB0 && batchInfo_.batchA0 != 1UL && | 145 | bool batch0Invalid = batchInfo_.batchA0 != batchInfo_.batchB0 && batchInfo_.batchA0 != 1UL && |
| 134 | batchInfo_.batchB0 != 1UL; | 146 | batchInfo_.batchB0 != 1UL; |
| 135 | - if (batch3Invalid || batch2Invalid || batch1Invalid || batch0Invalid) { | 147 | + bool batch4Invalid = batchInfo_.batchA4 != batchInfo_.batchB4 && batchInfo_.batchA4 != 1UL && |
| 148 | + batchInfo_.batchB4 != 1UL; | ||
| 149 | + bool batch5Invalid = batchInfo_.batchA5 != batchInfo_.batchB5 && batchInfo_.batchA5 != 1UL && | ||
| 150 | + batchInfo_.batchB5 != 1UL; | ||
| 151 | + if (batch3Invalid || batch2Invalid || batch1Invalid || batch0Invalid || batch4Invalid || batch5Invalid) { | ||
| 136 | OP_LOGE("[BatchMatMulV3]", "Is M broadcast to N situation, do not support!"); | 152 | OP_LOGE("[BatchMatMulV3]", "Is M broadcast to N situation, do not support!"); |
| 137 | return false; | 153 | return false; |
| 138 | } | 154 | } |
| @@ -162,20 +178,15 @@ bool BatchMatmulV3BaseTiling::GetBiasWithBatchInfo() | |||
| 162 | return false; | 178 | return false; |
| 163 | } | 179 | } |
| 164 | 180 | ||
| 165 | - uint64_t batchBias3 = biasDims > NO_BATCH_SHAPE_DIM ? | 181 | + uint64_t batchBias0 = GetBatchAxisFromEnd(biasShape, biasDims, 8); |
| 166 | - static_cast<uint64_t>(biasShape.GetDim(biasDims - ONE_BATCH_SHAPE_DIM)) : | 182 | + uint64_t batchBias1 = GetBatchAxisFromEnd(biasShape, biasDims, 7); |
| 167 | - 1; | 183 | + uint64_t batchBias2 = GetBatchAxisFromEnd(biasShape, biasDims, 6); |
| 168 | - uint64_t batchBias2 = biasDims > ONE_BATCH_SHAPE_DIM ? | 184 | + uint64_t batchBias3 = GetBatchAxisFromEnd(biasShape, biasDims, 5); |
| 169 | - static_cast<uint64_t>(biasShape.GetDim(biasDims - TWO_BATCH_SHAPE_DIM)) : | 185 | + uint64_t batchBias4 = GetBatchAxisFromEnd(biasShape, biasDims, 4); |
| 170 | - 1; | 186 | + uint64_t batchBias5 = GetBatchAxisFromEnd(biasShape, biasDims, 3); |
| 171 | - uint64_t batchBias1 = biasDims > TWO_BATCH_SHAPE_DIM ? | 187 | + bool biasBatchValid = batchBias0 == batchInfo_.batchC0 && batchBias1 == batchInfo_.batchC1 && |
| 172 | - static_cast<uint64_t>(biasShape.GetDim(biasDims - THREE_BATCH_SHAPE_DIM)) : | 188 | + batchBias2 == batchInfo_.batchC2 && batchBias3 == batchInfo_.batchC3 && |
| 173 | - 1; | 189 | + batchBias4 == batchInfo_.batchC4 && batchBias5 == batchInfo_.batchC5; |
| 174 | - uint64_t batchBias0 = biasDims > THREE_BATCH_SHAPE_DIM ? | ||
| 175 | - static_cast<uint64_t>(biasShape.GetDim(biasDims - FOUR_BATCH_SHAPE_DIM)) : | ||
| 176 | - 1; | ||
| 177 | - bool biasBatchValid = batchBias3 == batchInfo_.batchC3 && batchBias2 == batchInfo_.batchC2 && | ||
| 178 | - batchBias1 == batchInfo_.batchC1 && batchBias0 == batchInfo_.batchC0; | ||
| 179 | if (batchInfo_.biasWithBatch && !biasBatchValid) { | 190 | if (batchInfo_.biasWithBatch && !biasBatchValid) { |
| 180 | OP_LOGE("[BatchMatMulV3]", "Batch of Bias must be equal to C !"); | 191 | OP_LOGE("[BatchMatMulV3]", "Batch of Bias must be equal to C !"); |
| 181 | return false; | 192 | return false; |
| @@ -199,11 +210,15 @@ void BatchMatmulV3BaseTiling::MergeBatchAndMAxis() | |||
| 199 | batchInfo_.batchA2 = 1UL; | 210 | batchInfo_.batchA2 = 1UL; |
| 200 | batchInfo_.batchA1 = 1UL; | 211 | batchInfo_.batchA1 = 1UL; |
| 201 | batchInfo_.batchA0 = 1UL; | 212 | batchInfo_.batchA0 = 1UL; |
| 213 | + batchInfo_.batchA4 = 1UL; | ||
| 214 | + batchInfo_.batchA5 = 1UL; | ||
| 202 | batchInfo_.batchA = 1UL; | 215 | batchInfo_.batchA = 1UL; |
| 203 | batchInfo_.batchC3 = 1UL; | 216 | batchInfo_.batchC3 = 1UL; |
| 204 | batchInfo_.batchC2 = 1UL; | 217 | batchInfo_.batchC2 = 1UL; |
| 205 | batchInfo_.batchC1 = 1UL; | 218 | batchInfo_.batchC1 = 1UL; |
| 206 | batchInfo_.batchC0 = 1UL; | 219 | batchInfo_.batchC0 = 1UL; |
| 220 | + batchInfo_.batchC4 = 1UL; | ||
| 221 | + batchInfo_.batchC5 = 1UL; | ||
| 207 | batchInfo_.batchC = 1UL; | 222 | batchInfo_.batchC = 1UL; |
| 208 | } | 223 | } |
| 209 | return; | 224 | return; |
| @@ -215,14 +230,20 @@ bool BatchMatmulV3BaseTiling::CheckBMMTilingDataIsValid() const | |||
| 215 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchA2, args_.opName, "batchInfo_.batchA2") || | 230 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchA2, args_.opName, "batchInfo_.batchA2") || |
| 216 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchA1, args_.opName, "batchInfo_.batchA1") || | 231 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchA1, args_.opName, "batchInfo_.batchA1") || |
| 217 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchA0, args_.opName, "batchInfo_.batchA0") || | 232 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchA0, args_.opName, "batchInfo_.batchA0") || |
| 233 | + optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchA4, args_.opName, "batchInfo_.batchA4") || | ||
| 234 | + optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchA5, args_.opName, "batchInfo_.batchA5") || | ||
| 218 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchB3, args_.opName, "batchInfo_.batchB3") || | 235 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchB3, args_.opName, "batchInfo_.batchB3") || |
| 219 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchB2, args_.opName, "batchInfo_.batchB2") || | 236 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchB2, args_.opName, "batchInfo_.batchB2") || |
| 220 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchB1, args_.opName, "batchInfo_.batchB1") || | 237 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchB1, args_.opName, "batchInfo_.batchB1") || |
| 221 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchB0, args_.opName, "batchInfo_.batchB0") || | 238 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchB0, args_.opName, "batchInfo_.batchB0") || |
| 239 | + optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchB4, args_.opName, "batchInfo_.batchB4") || | ||
| 240 | + optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchB5, args_.opName, "batchInfo_.batchB5") || | ||
| 222 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchC3, args_.opName, "batchInfo_.batchC3") || | 241 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchC3, args_.opName, "batchInfo_.batchC3") || |
| 223 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchC2, args_.opName, "batchInfo_.batchC2") || | 242 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchC2, args_.opName, "batchInfo_.batchC2") || |
| 224 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchC1, args_.opName, "batchInfo_.batchC1") || | 243 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchC1, args_.opName, "batchInfo_.batchC1") || |
| 225 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchC0, args_.opName, "batchInfo_.batchC0") || | 244 | optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchC0, args_.opName, "batchInfo_.batchC0") || |
| 245 | + optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchC4, args_.opName, "batchInfo_.batchC4") || | ||
| 246 | + optiling::matmul_v3::CheckNumberIsValid(batchInfo_.batchC5, args_.opName, "batchInfo_.batchC5") || | ||
| 226 | optiling::matmul_v3::CheckNumberIsValid(aBatchDimAll_, args_.opName, "batchInfo_.aBatchDimAll") || | 247 | optiling::matmul_v3::CheckNumberIsValid(aBatchDimAll_, args_.opName, "batchInfo_.aBatchDimAll") || |
| 227 | optiling::matmul_v3::CheckNumberIsValid(bBatchDimAll_, args_.opName, "batchInfo_.bBatchDimAll") || | 248 | optiling::matmul_v3::CheckNumberIsValid(bBatchDimAll_, args_.opName, "batchInfo_.bBatchDimAll") || |
| 228 | optiling::matmul_v3::CheckNumberIsValid(cBatchDimAll_, args_.opName, "batchInfo_.cBatchDimAll")); | 249 | optiling::matmul_v3::CheckNumberIsValid(cBatchDimAll_, args_.opName, "batchInfo_.cBatchDimAll")); |
| @@ -307,25 +328,36 @@ void BatchMatmulV3BaseTiling::DoL2CacheAndCalOrderTiling() | |||
| 307 | void BatchMatmulV3BaseTiling::SetBatchDimInfo() | 328 | void BatchMatmulV3BaseTiling::SetBatchDimInfo() |
| 308 | { | 329 | { |
| 309 | bmmTilingData_.multiBatchInfo.batchUsedCoreNum = bmmTilingData_.matmulTiling.matmulTiling.usedCoreNum; | 330 | bmmTilingData_.multiBatchInfo.batchUsedCoreNum = bmmTilingData_.matmulTiling.matmulTiling.usedCoreNum; |
| 310 | - bmmTilingData_.multiBatchInfo.aBatchDim3 = static_cast<uint32_t>(batchInfo_.batchA3); | 331 | + // Host a0..a5 = tensor dim0..dim5. Blob Dim* are slots, not axis ids (aN != DimN). |
| 311 | - bmmTilingData_.multiBatchInfo.aBatchDim2 = static_cast<uint32_t>(batchInfo_.batchA2); | 332 | + // a0->D5, a1->D4, a2->D0, a3->D1, a4->D2, a5->D3 (see MultiBatchInfo). |
| 312 | - bmmTilingData_.multiBatchInfo.aBatchDim1 = static_cast<uint32_t>(batchInfo_.batchA1); | 333 | + bmmTilingData_.multiBatchInfo.aBatchDim0 = static_cast<uint32_t>(batchInfo_.batchA2); |
| 313 | - bmmTilingData_.multiBatchInfo.aBatchDim0 = static_cast<uint32_t>(batchInfo_.batchA0); | 334 | + bmmTilingData_.multiBatchInfo.aBatchDim1 = static_cast<uint32_t>(batchInfo_.batchA3); |
| 314 | - bmmTilingData_.multiBatchInfo.bBatchDim3 = static_cast<uint32_t>(batchInfo_.batchB3); | 335 | + bmmTilingData_.multiBatchInfo.aBatchDim2 = static_cast<uint32_t>(batchInfo_.batchA4); |
| 315 | - bmmTilingData_.multiBatchInfo.bBatchDim2 = static_cast<uint32_t>(batchInfo_.batchB2); | 336 | + bmmTilingData_.multiBatchInfo.aBatchDim3 = static_cast<uint32_t>(batchInfo_.batchA5); |
| 316 | - bmmTilingData_.multiBatchInfo.bBatchDim1 = static_cast<uint32_t>(batchInfo_.batchB1); | 337 | + bmmTilingData_.multiBatchInfo.aBatchDim4 = static_cast<uint32_t>(batchInfo_.batchA1); |
| 317 | - bmmTilingData_.multiBatchInfo.bBatchDim0 = static_cast<uint32_t>(batchInfo_.batchB0); | 338 | + bmmTilingData_.multiBatchInfo.aBatchDim5 = static_cast<uint32_t>(batchInfo_.batchA0); |
| 318 | - bmmTilingData_.multiBatchInfo.cBatchDim3 = static_cast<uint32_t>(batchInfo_.batchC3); | 339 | + bmmTilingData_.multiBatchInfo.bBatchDim0 = static_cast<uint32_t>(batchInfo_.batchB2); |
| 319 | - bmmTilingData_.multiBatchInfo.cBatchDim2 = static_cast<uint32_t>(batchInfo_.batchC2); | 340 | + bmmTilingData_.multiBatchInfo.bBatchDim1 = static_cast<uint32_t>(batchInfo_.batchB3); |
| 320 | - bmmTilingData_.multiBatchInfo.cBatchDim1 = static_cast<uint32_t>(batchInfo_.batchC1); | 341 | + bmmTilingData_.multiBatchInfo.bBatchDim2 = static_cast<uint32_t>(batchInfo_.batchB4); |
| 321 | - bmmTilingData_.multiBatchInfo.cBatchDim0 = static_cast<uint32_t>(batchInfo_.batchC0); | 342 | + bmmTilingData_.multiBatchInfo.bBatchDim3 = static_cast<uint32_t>(batchInfo_.batchB5); |
| 343 | + bmmTilingData_.multiBatchInfo.bBatchDim4 = static_cast<uint32_t>(batchInfo_.batchB1); | ||
| 344 | + bmmTilingData_.multiBatchInfo.bBatchDim5 = static_cast<uint32_t>(batchInfo_.batchB0); | ||
| 345 | + bmmTilingData_.multiBatchInfo.cBatchDim0 = static_cast<uint32_t>(batchInfo_.batchC2); | ||
| 346 | + bmmTilingData_.multiBatchInfo.cBatchDim1 = static_cast<uint32_t>(batchInfo_.batchC3); | ||
| 347 | + bmmTilingData_.multiBatchInfo.cBatchDim2 = static_cast<uint32_t>(batchInfo_.batchC4); | ||
| 348 | + bmmTilingData_.multiBatchInfo.cBatchDim3 = static_cast<uint32_t>(batchInfo_.batchC5); | ||
| 349 | + bmmTilingData_.multiBatchInfo.cBatchDim4 = static_cast<uint32_t>(batchInfo_.batchC1); | ||
| 350 | + bmmTilingData_.multiBatchInfo.cBatchDim5 = static_cast<uint32_t>(batchInfo_.batchC0); | ||
| 322 | } | 351 | } |
| 323 | 352 | ||
| 324 | void BatchMatmulV3BaseTiling::CalcBatchDimAll() | 353 | void BatchMatmulV3BaseTiling::CalcBatchDimAll() |
| 325 | { | 354 | { |
| 326 | - aBatchDimAll_ = batchInfo_.batchA0 * batchInfo_.batchA1 * batchInfo_.batchA2 * batchInfo_.batchA3; | 355 | + aBatchDimAll_ = batchInfo_.batchA0 * batchInfo_.batchA1 * batchInfo_.batchA2 * batchInfo_.batchA3 * |
| 327 | - bBatchDimAll_ = batchInfo_.batchB0 * batchInfo_.batchB1 * batchInfo_.batchB2 * batchInfo_.batchB3; | 356 | + batchInfo_.batchA4 * batchInfo_.batchA5; |
| 328 | - cBatchDimAll_ = batchInfo_.batchC0 * batchInfo_.batchC1 * batchInfo_.batchC2 * batchInfo_.batchC3; | 357 | + bBatchDimAll_ = batchInfo_.batchB0 * batchInfo_.batchB1 * batchInfo_.batchB2 * batchInfo_.batchB3 * |
| 358 | + batchInfo_.batchB4 * batchInfo_.batchB5; | ||
| 359 | + cBatchDimAll_ = batchInfo_.batchC0 * batchInfo_.batchC1 * batchInfo_.batchC2 * batchInfo_.batchC3 * | ||
| 360 | + batchInfo_.batchC4 * batchInfo_.batchC5; | ||
| 329 | bmmTilingData_.multiBatchInfo.aBatchDimAll = static_cast<uint32_t>(aBatchDimAll_); | 361 | bmmTilingData_.multiBatchInfo.aBatchDimAll = static_cast<uint32_t>(aBatchDimAll_); |
| 330 | bmmTilingData_.multiBatchInfo.bBatchDimAll = static_cast<uint32_t>(bBatchDimAll_); | 362 | bmmTilingData_.multiBatchInfo.bBatchDimAll = static_cast<uint32_t>(bBatchDimAll_); |
| 331 | bmmTilingData_.multiBatchInfo.cBatchDimAll = static_cast<uint32_t>(cBatchDimAll_); | 363 | bmmTilingData_.multiBatchInfo.cBatchDimAll = static_cast<uint32_t>(cBatchDimAll_); |
| @@ -620,8 +652,7 @@ bool BatchMatmulV3BaseTiling::DoMultiBatchOutTiling() | |||
| 620 | 652 | ||
| 621 | void BatchMatmulV3BaseTiling::DoMultiBatchTiling() | 653 | void BatchMatmulV3BaseTiling::DoMultiBatchTiling() |
| 622 | { | 654 | { |
| 623 | - bool isEqualBatch = batchInfo_.batchA0 == batchInfo_.batchB0 && batchInfo_.batchA1 == batchInfo_.batchB1 && | 655 | + bool isEqualBatch = batchInfo_.IsEqualBatch(); // 广播 |
| 624 | - batchInfo_.batchA2 == batchInfo_.batchB2 && batchInfo_.batchA3 == batchInfo_.batchB3; // 广播 | ||
| 625 | if (!isEqualBatch || (args_.hasBias && !batchInfo_.biasWithBatch)) { // 不支持broadcast\非多batch bias | 656 | if (!isEqualBatch || (args_.hasBias && !batchInfo_.biasWithBatch)) { // 不支持broadcast\非多batch bias |
| 626 | return; | 657 | return; |
| 627 | } | 658 | } |
| @@ -687,8 +718,7 @@ void BatchMatmulV3BaseTiling::DoMultiBatchTiling() | |||
| 687 | bool BatchMatmulV3BaseTiling::IsMultiBatchAL1FullLoad() | 718 | bool BatchMatmulV3BaseTiling::IsMultiBatchAL1FullLoad() |
| 688 | { | 719 | { |
| 689 | // 白名单:((1500,1,128),(1500,512,128)) || !(!args_.isATrans && args_.isBTrans) | 720 | // 白名单:((1500,1,128),(1500,512,128)) || !(!args_.isATrans && args_.isBTrans) |
| 690 | - bool isEqualBatch = batchInfo_.batchA0 == batchInfo_.batchB0 && batchInfo_.batchA1 == batchInfo_.batchB1 && | 721 | + bool isEqualBatch = batchInfo_.IsEqualBatch(); |
| 691 | - batchInfo_.batchA2 == batchInfo_.batchB2 && batchInfo_.batchA3 == batchInfo_.batchB3; | ||
| 692 | constexpr uint64_t BATCH_DIM_ALL = 1500; | 722 | constexpr uint64_t BATCH_DIM_ALL = 1500; |
| 693 | constexpr uint64_t M_VALUE = 1; | 723 | constexpr uint64_t M_VALUE = 1; |
| 694 | constexpr uint64_t K_VALUE_128 = 128; | 724 | constexpr uint64_t K_VALUE_128 = 128; |
| @@ -734,9 +764,8 @@ void BatchMatmulV3BaseTiling::DoMultiBatchL1FullLoadTilingImpl() | |||
| 734 | 764 | ||
| 735 | void BatchMatmulV3BaseTiling::DoMultiBatchL1FullLoadTiling() | 765 | void BatchMatmulV3BaseTiling::DoMultiBatchL1FullLoadTiling() |
| 736 | { | 766 | { |
| 737 | - bool isEqualBatch = batchInfo_.batchA0 == batchInfo_.batchB0 && batchInfo_.batchA1 == batchInfo_.batchB1 && | 767 | + bool isEqualBatch = batchInfo_.IsEqualBatch(); // 广播 |
| 738 | - batchInfo_.batchA2 == batchInfo_.batchB2 && batchInfo_.batchA3 == batchInfo_.batchB3; // 广播 | 768 | + if (!isEqualBatch || args_.hasBias) { // 暂时不支持bias |
| 739 | - if (!isEqualBatch || args_.hasBias) { // 暂时不支持bias | ||
| 740 | return; | 769 | return; |
| 741 | } | 770 | } |
| 742 | uint64_t shapeM = ops::CeilAlign(static_cast<uint64_t>(bmmTilingData_.matmulTiling.matmulTiling.M), BLOCK_CUBE); | 771 | uint64_t shapeM = ops::CeilAlign(static_cast<uint64_t>(bmmTilingData_.matmulTiling.matmulTiling.M), BLOCK_CUBE); |
| @@ -23,21 +23,34 @@ namespace optiling { | |||
| 23 | namespace batch_mat_mul_v3 { | 23 | namespace batch_mat_mul_v3 { |
| 24 | struct BatchShapeInfo { | 24 | struct BatchShapeInfo { |
| 25 | uint64_t batchA = 1; | 25 | uint64_t batchA = 1; |
| 26 | + // a0..a5 = tensor dim0..dim5 (nearest-M is a5). Not blob aBatchDim0..5; mapping in SetBatchDimInfo. | ||
| 26 | uint64_t batchA0 = 1; | 27 | uint64_t batchA0 = 1; |
| 27 | uint64_t batchA1 = 1; | 28 | uint64_t batchA1 = 1; |
| 28 | uint64_t batchA2 = 1; | 29 | uint64_t batchA2 = 1; |
| 29 | uint64_t batchA3 = 1; | 30 | uint64_t batchA3 = 1; |
| 31 | + uint64_t batchA4 = 1; | ||
| 32 | + uint64_t batchA5 = 1; | ||
| 30 | uint64_t batchB = 1; | 33 | uint64_t batchB = 1; |
| 31 | uint64_t batchB0 = 1; | 34 | uint64_t batchB0 = 1; |
| 32 | uint64_t batchB1 = 1; | 35 | uint64_t batchB1 = 1; |
| 33 | uint64_t batchB2 = 1; | 36 | uint64_t batchB2 = 1; |
| 34 | uint64_t batchB3 = 1; | 37 | uint64_t batchB3 = 1; |
| 38 | + uint64_t batchB4 = 1; | ||
| 39 | + uint64_t batchB5 = 1; | ||
| 35 | uint64_t batchC = 1; | 40 | uint64_t batchC = 1; |
| 36 | uint64_t batchC0 = 1; | 41 | uint64_t batchC0 = 1; |
| 37 | uint64_t batchC1 = 1; | 42 | uint64_t batchC1 = 1; |
| 38 | uint64_t batchC2 = 1; | 43 | uint64_t batchC2 = 1; |
| 39 | uint64_t batchC3 = 1; | 44 | uint64_t batchC3 = 1; |
| 45 | + uint64_t batchC4 = 1; | ||
| 46 | + uint64_t batchC5 = 1; | ||
| 40 | bool biasWithBatch = false; | 47 | bool biasWithBatch = false; |
| 48 | + | ||
| 49 | + bool IsEqualBatch() const | ||
| 50 | + { | ||
| 51 | + return batchA0 == batchB0 && batchA1 == batchB1 && batchA2 == batchB2 && batchA3 == batchB3 && | ||
| 52 | + batchA4 == batchB4 && batchA5 == batchB5; | ||
| 53 | + } | ||
| 41 | }; | 54 | }; |
| 42 | 55 | ||
| 43 | enum class TilingCalcSelect // 选择不同的计算Tiling的方法 | 56 | enum class TilingCalcSelect // 选择不同的计算Tiling的方法 |
| @@ -120,18 +120,26 @@ __aicore__ inline void BatchMatMulUnalignedKernel<A_TYPE, B_TYPE, C_TYPE, BIAS_T | |||
| 120 | #if defined(__CCE_AICORE__) && __CCE_AICORE__ == 220 || \ | 120 | #if defined(__CCE_AICORE__) && __CCE_AICORE__ == 220 || \ |
| 121 | (defined(__NPU_ARCH__) && (__NPU_ARCH__ == 3003 || __NPU_ARCH__ == 3113)) | 121 | (defined(__NPU_ARCH__) && (__NPU_ARCH__ == 3003 || __NPU_ARCH__ == 3113)) |
| 122 | const TCubeTiling& tiling = tilingPtr_->matmulTiling.matmulTiling; | 122 | const TCubeTiling& tiling = tilingPtr_->matmulTiling.matmulTiling; |
| 123 | - const uint32_t& batchA1 = tilingPtr_->multiBatchInfo.aBatchDim0; | 123 | + // Locals a0..a5 = tensor dim0..dim5. Blob Dim* are slots: a0=D5, a1=D4, a2=D0 .. a5=D3. |
| 124 | - const uint32_t& batchA2 = tilingPtr_->multiBatchInfo.aBatchDim1; | 124 | + // Dim4/5 are appended; callers that omit them leave 0 (loop skip / divide-by-zero). Treat as missing axis (=1). |
| 125 | - const uint32_t& batchA3 = tilingPtr_->multiBatchInfo.aBatchDim2; | 125 | + uint32_t batchA0 = tilingPtr_->multiBatchInfo.aBatchDim5 == 0 ? 1 : tilingPtr_->multiBatchInfo.aBatchDim5; |
| 126 | - const uint32_t& batchA4 = tilingPtr_->multiBatchInfo.aBatchDim3; | 126 | + uint32_t batchA1 = tilingPtr_->multiBatchInfo.aBatchDim4 == 0 ? 1 : tilingPtr_->multiBatchInfo.aBatchDim4; |
| 127 | - const uint32_t& batchB1 = tilingPtr_->multiBatchInfo.bBatchDim0; | 127 | + uint32_t batchA2 = tilingPtr_->multiBatchInfo.aBatchDim0; |
| 128 | - const uint32_t& batchB2 = tilingPtr_->multiBatchInfo.bBatchDim1; | 128 | + uint32_t batchA3 = tilingPtr_->multiBatchInfo.aBatchDim1; |
| 129 | - const uint32_t& batchB3 = tilingPtr_->multiBatchInfo.bBatchDim2; | 129 | + uint32_t batchA4 = tilingPtr_->multiBatchInfo.aBatchDim2; |
| 130 | - const uint32_t& batchB4 = tilingPtr_->multiBatchInfo.bBatchDim3; | 130 | + uint32_t batchA5 = tilingPtr_->multiBatchInfo.aBatchDim3; |
| 131 | - const uint32_t& batchC1 = tilingPtr_->multiBatchInfo.cBatchDim0; | 131 | + uint32_t batchB0 = tilingPtr_->multiBatchInfo.bBatchDim5 == 0 ? 1 : tilingPtr_->multiBatchInfo.bBatchDim5; |
| 132 | - const uint32_t& batchC2 = tilingPtr_->multiBatchInfo.cBatchDim1; | 132 | + uint32_t batchB1 = tilingPtr_->multiBatchInfo.bBatchDim4 == 0 ? 1 : tilingPtr_->multiBatchInfo.bBatchDim4; |
| 133 | - const uint32_t& batchC3 = tilingPtr_->multiBatchInfo.cBatchDim2; | 133 | + uint32_t batchB2 = tilingPtr_->multiBatchInfo.bBatchDim0; |
| 134 | - const uint32_t& batchC4 = tilingPtr_->multiBatchInfo.cBatchDim3; | 134 | + uint32_t batchB3 = tilingPtr_->multiBatchInfo.bBatchDim1; |
| 135 | + uint32_t batchB4 = tilingPtr_->multiBatchInfo.bBatchDim2; | ||
| 136 | + uint32_t batchB5 = tilingPtr_->multiBatchInfo.bBatchDim3; | ||
| 137 | + uint32_t batchC0 = tilingPtr_->multiBatchInfo.cBatchDim5 == 0 ? 1 : tilingPtr_->multiBatchInfo.cBatchDim5; | ||
| 138 | + uint32_t batchC1 = tilingPtr_->multiBatchInfo.cBatchDim4 == 0 ? 1 : tilingPtr_->multiBatchInfo.cBatchDim4; | ||
| 139 | + uint32_t batchC2 = tilingPtr_->multiBatchInfo.cBatchDim0; | ||
| 140 | + uint32_t batchC3 = tilingPtr_->multiBatchInfo.cBatchDim1; | ||
| 141 | + uint32_t batchC4 = tilingPtr_->multiBatchInfo.cBatchDim2; | ||
| 142 | + uint32_t batchC5 = tilingPtr_->multiBatchInfo.cBatchDim3; | ||
| 135 | 143 | ||
| 136 | uint64_t offsetA = static_cast<uint64_t>(tiling.M) * tiling.Ka * sizeof(A_T); | 144 | uint64_t offsetA = static_cast<uint64_t>(tiling.M) * tiling.Ka * sizeof(A_T); |
| 137 | uint64_t offsetB = static_cast<uint64_t>(tiling.Kb) * tiling.N * sizeof(B_T); | 145 | uint64_t offsetB = static_cast<uint64_t>(tiling.Kb) * tiling.N * sizeof(B_T); |
| @@ -140,6 +148,8 @@ __aicore__ inline void BatchMatMulUnalignedKernel<A_TYPE, B_TYPE, C_TYPE, BIAS_T | |||
| 140 | if (tilingPtr_->multiBatchInfo.biasWithBatch) { | 148 | if (tilingPtr_->multiBatchInfo.biasWithBatch) { |
| 141 | batchBiasOffset = static_cast<uint64_t>(tiling.N) * sizeof(BiasT); | 149 | batchBiasOffset = static_cast<uint64_t>(tiling.N) * sizeof(BiasT); |
| 142 | } | 150 | } |
| 151 | + bool isBatchA0One = (batchA0 == 1); | ||
| 152 | + bool isBatchB0One = (batchB0 == 1); | ||
| 143 | bool isBatchA1One = (batchA1 == 1); | 153 | bool isBatchA1One = (batchA1 == 1); |
| 144 | bool isBatchB1One = (batchB1 == 1); | 154 | bool isBatchB1One = (batchB1 == 1); |
| 145 | bool isBatchA2One = (batchA2 == 1); | 155 | bool isBatchA2One = (batchA2 == 1); |
| @@ -148,34 +158,49 @@ __aicore__ inline void BatchMatMulUnalignedKernel<A_TYPE, B_TYPE, C_TYPE, BIAS_T | |||
| 148 | bool isBatchB3One = (batchB3 == 1); | 158 | bool isBatchB3One = (batchB3 == 1); |
| 149 | bool isBatchA4One = (batchA4 == 1); | 159 | bool isBatchA4One = (batchA4 == 1); |
| 150 | bool isBatchB4One = (batchB4 == 1); | 160 | bool isBatchB4One = (batchB4 == 1); |
| 151 | - for (uint64_t i1 = 0; i1 < batchC1; i1++) { | 161 | + bool isBatchA5One = (batchA5 == 1); |
| 152 | - uint64_t iA1 = isBatchA1One ? 0 : (i1 * batchA2 * batchA3 * batchA4); | 162 | + bool isBatchB5One = (batchB5 == 1); |
| 153 | - uint64_t iB1 = isBatchB1One ? 0 : (i1 * batchB2 * batchB3 * batchB4); | 163 | + uint64_t aInner4 = static_cast<uint64_t>(batchA2) * batchA3 * batchA4 * batchA5; |
| 154 | - uint64_t iC1 = i1 * batchC2 * batchC3 * batchC4; | 164 | + uint64_t bInner4 = static_cast<uint64_t>(batchB2) * batchB3 * batchB4 * batchB5; |
| 155 | - for (uint64_t i2 = 0; i2 < batchC2; i2++) { | 165 | + uint64_t cInner4 = static_cast<uint64_t>(batchC2) * batchC3 * batchC4 * batchC5; |
| 156 | - uint64_t iA2 = isBatchA2One ? 0 : (i2 * batchA3 * batchA4); | 166 | + for (uint64_t i0 = 0; i0 < batchC0; i0++) { |
| 157 | - uint64_t iB2 = isBatchB2One ? 0 : (i2 * batchB3 * batchB4); | 167 | + uint64_t iA0 = isBatchA0One ? 0 : (i0 * batchA1 * aInner4); |
| 158 | - uint64_t iC2 = i2 * batchC3 * batchC4; | 168 | + uint64_t iB0 = isBatchB0One ? 0 : (i0 * batchB1 * bInner4); |
| 159 | - for (uint64_t i3 = 0; i3 < batchC3; i3++) { | 169 | + uint64_t iC0 = i0 * batchC1 * cInner4; |
| 160 | - uint64_t iA3 = isBatchA3One ? 0 : (i3 * batchA4); | 170 | + for (uint64_t i1 = 0; i1 < batchC1; i1++) { |
| 161 | - uint64_t iB3 = isBatchB3One ? 0 : (i3 * batchB4); | 171 | + uint64_t iA1 = isBatchA1One ? 0 : (i1 * aInner4); |
| 162 | - uint64_t iC3 = i3 * batchC4; | 172 | + uint64_t iB1 = isBatchB1One ? 0 : (i1 * bInner4); |
| 163 | - for (uint64_t i4 = 0; i4 < batchC4; i4++) { | 173 | + uint64_t iC1 = i1 * cInner4; |
| 164 | - uint64_t iA4 = isBatchA4One ? 0 : i4; | 174 | + for (uint64_t i2 = 0; i2 < batchC2; i2++) { |
| 165 | - uint64_t iB4 = isBatchB4One ? 0 : i4; | 175 | + uint64_t iA2 = isBatchA2One ? 0 : (i2 * batchA3 * batchA4 * batchA5); |
| 166 | - uint64_t iA = iA1 + iA2 + iA3 + iA4; | 176 | + uint64_t iB2 = isBatchB2One ? 0 : (i2 * batchB3 * batchB4 * batchB5); |
| 167 | - uint64_t iB = iB1 + iB2 + iB3 + iB4; | 177 | + uint64_t iC2 = i2 * batchC3 * batchC4 * batchC5; |
| 168 | - uint64_t iC = iC1 + iC2 + iC3 + i4; | 178 | + for (uint64_t i3 = 0; i3 < batchC3; i3++) { |
| 169 | - mm_.UpdateGlobalTensor(aGM_ + offsetA * iA, bGM_ + offsetB * iB, cGM_ + offsetC * iC, | 179 | + uint64_t iA3 = isBatchA3One ? 0 : (i3 * batchA4 * batchA5); |
| 170 | - biasGM_ + batchBiasOffset * iC, offsetWGM_, workspaceGM_); | 180 | + uint64_t iB3 = isBatchB3One ? 0 : (i3 * batchB4 * batchB5); |
| 171 | - mm_.Process(); | 181 | + uint64_t iC3 = i3 * batchC4 * batchC5; |
| 172 | - if ASCEND_IS_AIC { | 182 | + for (uint64_t i4 = 0; i4 < batchC4; i4++) { |
| 173 | - NotifyEvent<PIPE_FIX>(4); | 183 | + uint64_t iA4 = isBatchA4One ? 0 : (i4 * batchA5); |
| 174 | - } | 184 | + uint64_t iB4 = isBatchB4One ? 0 : (i4 * batchB5); |
| 175 | - if ASCEND_IS_AIV { | 185 | + uint64_t iC4 = i4 * batchC5; |
| 176 | - WaitEvent(4); | 186 | + for (uint64_t i5 = 0; i5 < batchC5; i5++) { |
| 177 | - SyncAll(); | 187 | + uint64_t iA5 = isBatchA5One ? 0 : i5; |
| 178 | - PipeBarrier<PIPE_ALL>(); | 188 | + uint64_t iB5 = isBatchB5One ? 0 : i5; |
| 189 | + uint64_t iA = iA0 + iA1 + iA2 + iA3 + iA4 + iA5; | ||
| 190 | + uint64_t iB = iB0 + iB1 + iB2 + iB3 + iB4 + iB5; | ||
| 191 | + uint64_t iC = iC0 + iC1 + iC2 + iC3 + iC4 + i5; | ||
| 192 | + mm_.UpdateGlobalTensor(aGM_ + offsetA * iA, bGM_ + offsetB * iB, cGM_ + offsetC * iC, | ||
| 193 | + biasGM_ + batchBiasOffset * iC, offsetWGM_, workspaceGM_); | ||
| 194 | + mm_.Process(); | ||
| 195 | + if ASCEND_IS_AIC { | ||
| 196 | + NotifyEvent<PIPE_FIX>(4); | ||
| 197 | + } | ||
| 198 | + if ASCEND_IS_AIV { | ||
| 199 | + WaitEvent(4); | ||
| 200 | + SyncAll(); | ||
| 201 | + PipeBarrier<PIPE_ALL>(); | ||
| 202 | + } | ||
| 203 | + } | ||
| 179 | } | 204 | } |
| 180 | } | 205 | } |
| 181 | } | 206 | } |
| @@ -108,24 +108,34 @@ template <class C_TYPE> | |||
| 108 | __aicore__ inline void BatchMatMulCommonBaseBlock::InitBatchCount() | 108 | __aicore__ inline void BatchMatMulCommonBaseBlock::InitBatchCount() |
| 109 | { | 109 | { |
| 110 | params_.isHf32 = batchMatmulTilingData_->matmulTiling.matmulRunInfo.isHf32; | 110 | params_.isHf32 = batchMatmulTilingData_->matmulTiling.matmulRunInfo.isHf32; |
| 111 | - params_.batchA1 = batchMatmulTilingData_->multiBatchInfo.aBatchDim0; | 111 | + const MultiBatchInfo& multiBatch = batchMatmulTilingData_->multiBatchInfo; |
| 112 | - params_.batchA2 = batchMatmulTilingData_->multiBatchInfo.aBatchDim1; | 112 | + // Locals a0..a5 = tensor dim0..dim5. Blob Dim* are slots: a0=D5, a1=D4, a2=D0 .. a5=D3. |
| 113 | - params_.batchA3 = batchMatmulTilingData_->multiBatchInfo.aBatchDim2; | 113 | + // Dim4/5 are appended; callers that omit them leave 0 (loop skip / divide-by-zero). Treat as missing axis (=1). |
| 114 | - params_.batchA4 = batchMatmulTilingData_->multiBatchInfo.aBatchDim3; | 114 | + params_.batchA0 = multiBatch.aBatchDim5 == 0 ? 1 : multiBatch.aBatchDim5; |
| 115 | - params_.batchB1 = batchMatmulTilingData_->multiBatchInfo.bBatchDim0; | 115 | + params_.batchA1 = multiBatch.aBatchDim4 == 0 ? 1 : multiBatch.aBatchDim4; |
| 116 | - params_.batchB2 = batchMatmulTilingData_->multiBatchInfo.bBatchDim1; | 116 | + params_.batchA2 = multiBatch.aBatchDim0; |
| 117 | - params_.batchB3 = batchMatmulTilingData_->multiBatchInfo.bBatchDim2; | 117 | + params_.batchA3 = multiBatch.aBatchDim1; |
| 118 | - params_.batchB4 = batchMatmulTilingData_->multiBatchInfo.bBatchDim3; | 118 | + params_.batchA4 = multiBatch.aBatchDim2; |
| 119 | - params_.batchC1 = batchMatmulTilingData_->multiBatchInfo.cBatchDim0; | 119 | + params_.batchA5 = multiBatch.aBatchDim3; |
| 120 | - params_.batchC2 = batchMatmulTilingData_->multiBatchInfo.cBatchDim1; | 120 | + params_.batchB0 = multiBatch.bBatchDim5 == 0 ? 1 : multiBatch.bBatchDim5; |
| 121 | - params_.batchC3 = batchMatmulTilingData_->multiBatchInfo.cBatchDim2; | 121 | + params_.batchB1 = multiBatch.bBatchDim4 == 0 ? 1 : multiBatch.bBatchDim4; |
| 122 | - params_.batchC4 = batchMatmulTilingData_->multiBatchInfo.cBatchDim3; | 122 | + params_.batchB2 = multiBatch.bBatchDim0; |
| 123 | - params_.aBatchDimAll = batchMatmulTilingData_->multiBatchInfo.aBatchDimAll; | 123 | + params_.batchB3 = multiBatch.bBatchDim1; |
| 124 | - params_.bBatchDimAll = batchMatmulTilingData_->multiBatchInfo.bBatchDimAll; | 124 | + params_.batchB4 = multiBatch.bBatchDim2; |
| 125 | - params_.cBatchDimAll = batchMatmulTilingData_->multiBatchInfo.cBatchDimAll; | 125 | + params_.batchB5 = multiBatch.bBatchDim3; |
| 126 | - params_.biasWithBatch = batchMatmulTilingData_->multiBatchInfo.biasWithBatch; | 126 | + params_.batchC0 = multiBatch.cBatchDim5 == 0 ? 1 : multiBatch.cBatchDim5; |
| 127 | + params_.batchC1 = multiBatch.cBatchDim4 == 0 ? 1 : multiBatch.cBatchDim4; | ||
| 128 | + params_.batchC2 = multiBatch.cBatchDim0; | ||
| 129 | + params_.batchC3 = multiBatch.cBatchDim1; | ||
| 130 | + params_.batchC4 = multiBatch.cBatchDim2; | ||
| 131 | + params_.batchC5 = multiBatch.cBatchDim3; | ||
| 132 | + params_.aBatchDimAll = multiBatch.aBatchDimAll; | ||
| 133 | + params_.bBatchDimAll = multiBatch.bBatchDimAll; | ||
| 134 | + params_.cBatchDimAll = multiBatch.cBatchDimAll; | ||
| 135 | + params_.biasWithBatch = multiBatch.biasWithBatch; | ||
| 127 | 136 | ||
| 128 | - params_.batchCnt = params_.batchC1 * params_.batchC2 * params_.batchC3 * params_.batchC4; | 137 | + params_.batchCnt = params_.batchC0 * params_.batchC1 * params_.batchC2 * params_.batchC3 * params_.batchC4 * |
| 138 | + params_.batchC5; | ||
| 129 | params_.mCnt = MMV3DivCeil( | 139 | params_.mCnt = MMV3DivCeil( |
| 130 | batchMatmulTilingData_->matmulTiling.matmulTiling.M, | 140 | batchMatmulTilingData_->matmulTiling.matmulTiling.M, |
| 131 | static_cast<uint64_t>(batchMatmulTilingData_->matmulTiling.matmulTiling.singleCoreM)); // m方向上分多少个base块 | 141 | static_cast<uint64_t>(batchMatmulTilingData_->matmulTiling.matmulTiling.singleCoreM)); // m方向上分多少个base块 |
| @@ -350,11 +360,15 @@ __aicore__ inline void BatchMatMulCommonBaseBlock::CalcGMOffset(uint64_t mTileIn | |||
| 350 | uint64_t totalBmTileIndex = bmTileStartIndex + tileInnerMIndex; | 360 | uint64_t totalBmTileIndex = bmTileStartIndex + tileInnerMIndex; |
| 351 | 361 | ||
| 352 | // batch维度合并到M轴一起使用,先解析再重新计算 | 362 | // batch维度合并到M轴一起使用,先解析再重新计算 |
| 353 | - uint64_t batchC1Index = totalBmTileIndex / (params_.batchC2 * params_.batchC3 * params_.batchC4); | 363 | + uint64_t cInner4 = params_.batchC2 * params_.batchC3 * params_.batchC4 * params_.batchC5; |
| 354 | - uint64_t batchC2Index = totalBmTileIndex % (params_.batchC2 * params_.batchC3 * params_.batchC4) / | 364 | + uint64_t batchC0Index = totalBmTileIndex / (params_.batchC1 * cInner4); |
| 355 | - (params_.batchC3 * params_.batchC4); | 365 | + uint64_t batchC1Index = (totalBmTileIndex / cInner4) % params_.batchC1; |
| 356 | - uint64_t batchC3Index = totalBmTileIndex % (params_.batchC3 * params_.batchC4) / params_.batchC4; | 366 | + uint64_t low4Index = totalBmTileIndex % cInner4; |
| 357 | - uint64_t batchC4Index = totalBmTileIndex % params_.batchC4; | 367 | + uint64_t batchC2Index = low4Index / (params_.batchC3 * params_.batchC4 * params_.batchC5); |
| 368 | + uint64_t batchC3Index = low4Index % (params_.batchC3 * params_.batchC4 * params_.batchC5) / | ||
| 369 | + (params_.batchC4 * params_.batchC5); | ||
| 370 | + uint64_t batchC4Index = low4Index % (params_.batchC4 * params_.batchC5) / params_.batchC5; | ||
| 371 | + uint64_t batchC5Index = low4Index % params_.batchC5; | ||
| 358 | uint64_t batchCIndex = totalBmTileIndex; | 372 | uint64_t batchCIndex = totalBmTileIndex; |
| 359 | 373 | ||
| 360 | // Tile 内 mIndex | 374 | // Tile 内 mIndex |
| @@ -364,22 +378,30 @@ __aicore__ inline void BatchMatMulCommonBaseBlock::CalcGMOffset(uint64_t mTileIn | |||
| 364 | static_cast<uint64_t>(batchMatmulTilingData_->matmulTiling.matmulTiling.M); | 378 | static_cast<uint64_t>(batchMatmulTilingData_->matmulTiling.matmulTiling.M); |
| 365 | CalcCOffset<C_TYPE>(batchCIndex, mCntIndex, nCntIndex, mInnerBatchOffset); | 379 | CalcCOffset<C_TYPE>(batchCIndex, mCntIndex, nCntIndex, mInnerBatchOffset); |
| 366 | 380 | ||
| 381 | + uint64_t batchA0Index = batchC0Index % params_.batchA0; | ||
| 367 | uint64_t batchA1Index = batchC1Index % params_.batchA1; | 382 | uint64_t batchA1Index = batchC1Index % params_.batchA1; |
| 368 | uint64_t batchA2Index = batchC2Index % params_.batchA2; | 383 | uint64_t batchA2Index = batchC2Index % params_.batchA2; |
| 369 | uint64_t batchA3Index = batchC3Index % params_.batchA3; | 384 | uint64_t batchA3Index = batchC3Index % params_.batchA3; |
| 370 | uint64_t batchA4Index = batchC4Index % params_.batchA4; | 385 | uint64_t batchA4Index = batchC4Index % params_.batchA4; |
| 371 | - uint64_t batchAIndex = batchA1Index * (params_.batchA2 * params_.batchA3 * params_.batchA4) + | 386 | + uint64_t batchA5Index = batchC5Index % params_.batchA5; |
| 372 | - batchA2Index * (params_.batchA3 * params_.batchA4) + batchA3Index * params_.batchA4 + | 387 | + uint64_t aInner4 = params_.batchA2 * params_.batchA3 * params_.batchA4 * params_.batchA5; |
| 373 | - batchA4Index; | 388 | + uint64_t batchAIndex = batchA0Index * (params_.batchA1 * aInner4) + batchA1Index * aInner4 + |
| 389 | + batchA2Index * (params_.batchA3 * params_.batchA4 * params_.batchA5) + | ||
| 390 | + batchA3Index * (params_.batchA4 * params_.batchA5) + batchA4Index * params_.batchA5 + | ||
| 391 | + batchA5Index; | ||
| 374 | CalcAOffset<A_TYPE>(batchAIndex, mCntIndex, mInnerBatchOffset); | 392 | CalcAOffset<A_TYPE>(batchAIndex, mCntIndex, mInnerBatchOffset); |
| 375 | 393 | ||
| 394 | + uint64_t batchB0Index = batchC0Index % params_.batchB0; | ||
| 376 | uint64_t batchB1Index = batchC1Index % params_.batchB1; | 395 | uint64_t batchB1Index = batchC1Index % params_.batchB1; |
| 377 | uint64_t batchB2Index = batchC2Index % params_.batchB2; | 396 | uint64_t batchB2Index = batchC2Index % params_.batchB2; |
| 378 | uint64_t batchB3Index = batchC3Index % params_.batchB3; | 397 | uint64_t batchB3Index = batchC3Index % params_.batchB3; |
| 379 | uint64_t batchB4Index = batchC4Index % params_.batchB4; | 398 | uint64_t batchB4Index = batchC4Index % params_.batchB4; |
| 380 | - uint64_t batchBIndex = batchB1Index * (params_.batchB2 * params_.batchB3 * params_.batchB4) + | 399 | + uint64_t batchB5Index = batchC5Index % params_.batchB5; |
| 381 | - batchB2Index * (params_.batchB3 * params_.batchB4) + batchB3Index * params_.batchB4 + | 400 | + uint64_t bInner4 = params_.batchB2 * params_.batchB3 * params_.batchB4 * params_.batchB5; |
| 382 | - batchB4Index; | 401 | + uint64_t batchBIndex = batchB0Index * (params_.batchB1 * bInner4) + batchB1Index * bInner4 + |
| 402 | + batchB2Index * (params_.batchB3 * params_.batchB4 * params_.batchB5) + | ||
| 403 | + batchB3Index * (params_.batchB4 * params_.batchB5) + batchB4Index * params_.batchB5 + | ||
| 404 | + batchB5Index; | ||
| 383 | CalcBOffset<B_TYPE>(batchBIndex, nCntIndex); | 405 | CalcBOffset<B_TYPE>(batchBIndex, nCntIndex); |
| 384 | 406 | ||
| 385 | if (batchMatmulTilingData_->matmulTiling.matmulTiling.isBias) { | 407 | if (batchMatmulTilingData_->matmulTiling.matmulTiling.isBias) { |
| @@ -501,4 +523,4 @@ __aicore__ inline void BatchMatMulCommonBaseBlock::UpdateBlockIndex() | |||
| 501 | } | 523 | } |
| 502 | } | 524 | } |
| 503 | 525 | ||
| 504 | -#endif // BATCH_MATMUL_V3_COM_BASE_BLOCK_H | 526 | +#endif // BATCH_MATMUL_V3_COM_BASE_BLOCK_H |
| @@ -23,18 +23,25 @@ struct CommonKernelBlockOffset { | |||
| 23 | }; | 23 | }; |
| 24 | 24 | ||
| 25 | struct CommonKernelBaseBlockArgs { | 25 | struct CommonKernelBaseBlockArgs { |
| 26 | + // a0..a5 = tensor dim0..dim5. Filled from blob slots D5,D4,D0,D1,D2,D3 (not DimN=aN). | ||
| 27 | + uint64_t batchA0; | ||
| 26 | uint64_t batchA1; | 28 | uint64_t batchA1; |
| 27 | uint64_t batchA2; | 29 | uint64_t batchA2; |
| 28 | uint64_t batchA3; | 30 | uint64_t batchA3; |
| 29 | uint64_t batchA4; | 31 | uint64_t batchA4; |
| 32 | + uint64_t batchA5; | ||
| 33 | + uint64_t batchB0; | ||
| 30 | uint64_t batchB1; | 34 | uint64_t batchB1; |
| 31 | uint64_t batchB2; | 35 | uint64_t batchB2; |
| 32 | uint64_t batchB3; | 36 | uint64_t batchB3; |
| 33 | uint64_t batchB4; | 37 | uint64_t batchB4; |
| 38 | + uint64_t batchB5; | ||
| 39 | + uint64_t batchC0; | ||
| 34 | uint64_t batchC1; | 40 | uint64_t batchC1; |
| 35 | uint64_t batchC2; | 41 | uint64_t batchC2; |
| 36 | uint64_t batchC3; | 42 | uint64_t batchC3; |
| 37 | uint64_t batchC4; | 43 | uint64_t batchC4; |
| 44 | + uint64_t batchC5; | ||
| 38 | uint64_t aBatchDimAll; | 45 | uint64_t aBatchDimAll; |
| 39 | uint64_t bBatchDimAll; | 46 | uint64_t bBatchDimAll; |
| 40 | uint64_t cBatchDimAll; | 47 | uint64_t cBatchDimAll; |
| @@ -80,4 +87,4 @@ struct CommonKernelBaseBlockArgs { | |||
| 80 | bool enableL2Cache; | 87 | bool enableL2Cache; |
| 81 | }; | 88 | }; |
| 82 | 89 | ||
| 83 | -#endif // BATCH_MATMUL_V3_COM_BASE_BLOCK_STRUCT_H | 90 | +#endif // BATCH_MATMUL_V3_COM_BASE_BLOCK_STRUCT_H |
| @@ -32,6 +32,11 @@ struct alignas(8) MultiBatchInfo { | |||
| 32 | uint32_t aBatchDimAll; | 32 | uint32_t aBatchDimAll; |
| 33 | uint32_t bBatchDimAll; | 33 | uint32_t bBatchDimAll; |
| 34 | uint32_t cBatchDimAll; | 34 | uint32_t cBatchDimAll; |
| 35 | + // a0..a5 = tensor batch axes left-to-right (dim0=a0, nearest-M=a5). Missing axes pad 1. | ||
| 36 | + // aBatchDim0..5 are blob SLOT names, not axis numbers. Do not assign DimN = aN. | ||
| 37 | + // tensor axis a0 a1 a2 a3 a4 a5 | ||
| 38 | + // blob slot D5 D4 D0 D1 D2 D3 | ||
| 39 | + // Dim0..3 keep 6D offsets (8D's dim2..dim5). Dim4/5 are appended (8D dim1/dim0). | ||
| 35 | uint32_t aBatchDim0; | 40 | uint32_t aBatchDim0; |
| 36 | uint32_t bBatchDim0; | 41 | uint32_t bBatchDim0; |
| 37 | uint32_t cBatchDim0; | 42 | uint32_t cBatchDim0; |
| @@ -50,6 +55,13 @@ struct alignas(8) MultiBatchInfo { | |||
| 50 | uint32_t batchTileBlock; | 55 | uint32_t batchTileBlock; |
| 51 | uint32_t aBatch; | 56 | uint32_t aBatch; |
| 52 | uint32_t bBatch; | 57 | uint32_t bBatch; |
| 58 | + // Appended 8D slots: D4=a1, D5=a0. See table above aBatchDim0. | ||
| 59 | + uint32_t aBatchDim4; | ||
| 60 | + uint32_t bBatchDim4; | ||
| 61 | + uint32_t cBatchDim4; | ||
| 62 | + uint32_t aBatchDim5; | ||
| 63 | + uint32_t bBatchDim5; | ||
| 64 | + uint32_t cBatchDim5; | ||
| 53 | }; | 65 | }; |
| 54 | #pragma pack(pop) | 66 | #pragma pack(pop) |
| 55 | 67 | ||
| @@ -81,4 +93,4 @@ struct alignas(8) BatchMatmulTilingData { | |||
| 81 | }; | 93 | }; |
| 82 | #pragma pack(pop) | 94 | #pragma pack(pop) |
| 83 | 95 | ||
| 84 | -#endif | 96 | +#endif |
| @@ -684,7 +684,7 @@ static TilingTestParam ascend910B_cases_params[] = { | |||
| 684 | 65537, | 684 | 65537, |
| 685 | "24 1 128 512 512 1 128 512 16 128 64 64 8 1 1 0 0 0 0 65536 1024 0 1 1 1 1 32 4 0 0 2 2 1 0 0 0 0 0 0 0 0 0 0 0 " | 685 | "24 1 128 512 512 1 128 512 16 128 64 64 8 1 1 0 0 0 0 65536 1024 0 1 1 1 1 32 4 0 0 2 2 1 0 0 0 0 0 0 0 0 0 0 0 " |
| 686 | "0 0 0 0 1 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24 1500 1500 1500 1 1 1 1 1 1 1 1 1 1500 1500 1500 0 0 1 " | 686 | "0 0 0 0 1 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24 1500 1500 1500 1 1 1 1 1 1 1 1 1 1500 1500 1500 0 0 1 " |
| 687 | - "1500 7 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "}, | 687 | + "1500 7 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "}, |
| 688 | {"BatchMatMulV3_MultiBatch_AL1FullLoad_general_test_02", | 688 | {"BatchMatMulV3_MultiBatch_AL1FullLoad_general_test_02", |
| 689 | "BatchMatMulV3", | 689 | "BatchMatMulV3", |
| 690 | R"({"_pattern": "MatMul", "attrs":{"transpose_a":false,"transpose_b":true,"offset_x":0,"enable_hf32":0}, | 690 | R"({"_pattern": "MatMul", "attrs":{"transpose_a":false,"transpose_b":true,"offset_x":0,"enable_hf32":0}, |
| @@ -712,7 +712,7 @@ static TilingTestParam ascend910B_cases_params[] = { | |||
| 712 | 65537, | 712 | 65537, |
| 713 | "24 1 512 128 128 1 512 128 16 64 128 16 8 1 1 0 0 0 0 24576 2048 0 1 1 1 1 8 4 0 0 2 2 1 0 0 0 0 0 0 0 0 0 0 0 0 " | 713 | "24 1 512 128 128 1 512 128 16 64 128 16 8 1 1 0 0 0 0 24576 2048 0 1 1 1 1 8 4 0 0 2 2 1 0 0 0 0 0 0 0 0 0 0 0 0 " |
| 714 | "0 0 0 1 0 1 1 1 16 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 24 1500 1500 1500 1 1 1 1 1 1 1 1 1 1500 1500 1500 0 0 1 1500 " | 714 | "0 0 0 1 0 1 1 1 16 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 24 1500 1500 1500 1 1 1 1 1 1 1 1 1 1500 1500 1500 0 0 1 1500 " |
| 715 | - "31 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "}}; | 715 | + "31 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "}}; |
| 716 | 716 | ||
| 717 | static TilingTestParam ascend950_cases_params[] = { | 717 | static TilingTestParam ascend950_cases_params[] = { |
| 718 | {"BatchMatMulV3_950_test_iterbatch_1", | 718 | {"BatchMatMulV3_950_test_iterbatch_1", |
| @@ -132,6 +132,12 @@ TEST_F(batch_mat_mul_v3_test, batch_mat_mul_v3_test_0) | |||
| 132 | tiling_data->multiBatchInfo.aBatchDim3 = 1; | 132 | tiling_data->multiBatchInfo.aBatchDim3 = 1; |
| 133 | tiling_data->multiBatchInfo.bBatchDim3 = 1; | 133 | tiling_data->multiBatchInfo.bBatchDim3 = 1; |
| 134 | tiling_data->multiBatchInfo.cBatchDim3 = 1; | 134 | tiling_data->multiBatchInfo.cBatchDim3 = 1; |
| 135 | + tiling_data->multiBatchInfo.aBatchDim4 = 1; | ||
| 136 | + tiling_data->multiBatchInfo.bBatchDim4 = 1; | ||
| 137 | + tiling_data->multiBatchInfo.cBatchDim4 = 1; | ||
| 138 | + tiling_data->multiBatchInfo.aBatchDim5 = 1; | ||
| 139 | + tiling_data->multiBatchInfo.bBatchDim5 = 1; | ||
| 140 | + tiling_data->multiBatchInfo.cBatchDim5 = 1; | ||
| 135 | tiling_data->multiBatchInfo.iterBatch = 1; | 141 | tiling_data->multiBatchInfo.iterBatch = 1; |
| 136 | ICPU_SET_TILING_KEY(10000000000000001001UL); | 142 | ICPU_SET_TILING_KEY(10000000000000001001UL); |
| 137 | 143 | ||
| @@ -253,6 +259,12 @@ TEST_F(batch_mat_mul_v3_test, batch_mat_mul_v3_test_1) | |||
| 253 | tiling_data->multiBatchInfo.aBatchDim3 = 16; | 259 | tiling_data->multiBatchInfo.aBatchDim3 = 16; |
| 254 | tiling_data->multiBatchInfo.bBatchDim3 = 16; | 260 | tiling_data->multiBatchInfo.bBatchDim3 = 16; |
| 255 | tiling_data->multiBatchInfo.cBatchDim3 = 16; | 261 | tiling_data->multiBatchInfo.cBatchDim3 = 16; |
| 262 | + tiling_data->multiBatchInfo.aBatchDim4 = 1; | ||
| 263 | + tiling_data->multiBatchInfo.bBatchDim4 = 1; | ||
| 264 | + tiling_data->multiBatchInfo.cBatchDim4 = 1; | ||
| 265 | + tiling_data->multiBatchInfo.aBatchDim5 = 1; | ||
| 266 | + tiling_data->multiBatchInfo.bBatchDim5 = 1; | ||
| 267 | + tiling_data->multiBatchInfo.cBatchDim5 = 1; | ||
| 256 | tiling_data->multiBatchInfo.iterBatch = 0; | 268 | tiling_data->multiBatchInfo.iterBatch = 0; |
| 257 | tiling_data->multiBatchInfo.biasWithBatch = 0; | 269 | tiling_data->multiBatchInfo.biasWithBatch = 0; |
| 258 | tiling_data->multiBatchInfo.mOri = 1830; | 270 | tiling_data->multiBatchInfo.mOri = 1830; |
| @@ -175,16 +175,12 @@ static aclnnStatus CreateBatchMatmulOpInfo(const aclTensor* self, const aclTenso | |||
| 175 | static inline FVector<int64_t> GetBatchDim(const aclTensor* inputTensor) | 175 | static inline FVector<int64_t> GetBatchDim(const aclTensor* inputTensor) |
| 176 | { | 176 | { |
| 177 | size_t tensorDim = inputTensor->GetViewShape().GetDimNum(); | 177 | size_t tensorDim = inputTensor->GetViewShape().GetDimNum(); |
| 178 | - // When dimension > 2 , the case pattern is [B, M, K] | 178 | + auto dimFromEnd = [inputTensor, tensorDim](size_t fromEnd) -> int64_t { |
| 179 | - int64_t batchA3 = tensorDim > 2 ? inputTensor->GetViewShape().GetDim(tensorDim - 3) : 1; | 179 | + return tensorDim >= fromEnd ? inputTensor->GetViewShape().GetDim(tensorDim - fromEnd) : 1; |
| 180 | - // When dimension > 3 , the case pattern is [B1, B2, M, K] | 180 | + }; |
| 181 | - int64_t batchA2 = tensorDim > 3 ? inputTensor->GetViewShape().GetDim(tensorDim - 4) : 1; | 181 | + // Outermost to closest-to-M: rank-8 .. rank-3. Missing axes pad 1. |
| 182 | - // When dimension > 4 , the case pattern is [B1, B2, B3, M, K] | 182 | + FVector<int64_t> batchDim = {dimFromEnd(8), dimFromEnd(7), dimFromEnd(6), |
| 183 | - int64_t batchA1 = tensorDim > 4 ? inputTensor->GetViewShape().GetDim(tensorDim - 5) : 1; | 183 | + dimFromEnd(5), dimFromEnd(4), dimFromEnd(3)}; |
| 184 | - // When dimension > 5 , the case pattern is [B1, B2, B3, B4, M, K] | ||
| 185 | - int64_t batchA0 = tensorDim > 5 ? inputTensor->GetViewShape().GetDim(tensorDim - 6) : 1; | ||
| 186 | - FVector<int64_t> batchDim = {batchA0, batchA1, batchA2, batchA3}; | ||
| 187 | - | ||
| 188 | return batchDim; | 184 | return batchDim; |
| 189 | } | 185 | } |
| 190 | 186 | ||
| @@ -241,11 +237,16 @@ static bool CheckAscendCScenario(const aclTensor* x1, const aclTensor* x2, const | |||
| 241 | return false; | 237 | return false; |
| 242 | } | 238 | } |
| 243 | 239 | ||
| 240 | + size_t x1DimNum = x1->GetViewShape().GetDimNum(); | ||
| 241 | + size_t x2DimNum = x2->GetViewShape().GetDimNum(); | ||
| 242 | + if (x1DimNum > 6 || x2DimNum > 6) { | ||
| 243 | + OP_LOGI("Hit batch_mat_mul_v3 for rank 7/8."); | ||
| 244 | + return true; | ||
| 245 | + } | ||
| 246 | + | ||
| 244 | // vector kernel scenario: effective N dim of x2 is 1 | 247 | // vector kernel scenario: effective N dim of x2 is 1 |
| 245 | // 当nDim = 1时,触发vector kernel场景 | 248 | // 当nDim = 1时,触发vector kernel场景 |
| 246 | - size_t x2DimNum = x2->GetViewShape().GetDimNum(); | ||
| 247 | int64_t nDim = x2->GetViewShape().GetDim(x2DimNum - 2); | 249 | int64_t nDim = x2->GetViewShape().GetDim(x2DimNum - 2); |
| 248 | - size_t x1DimNum = x1->GetViewShape().GetDimNum(); | ||
| 249 | int64_t mDim = x1->GetViewShape().GetDim(x1DimNum - 2); | 250 | int64_t mDim = x1->GetViewShape().GetDim(x1DimNum - 2); |
| 250 | int64_t kDim = x1->GetViewShape().GetDim(x1DimNum - 1); | 251 | int64_t kDim = x1->GetViewShape().GetDim(x1DimNum - 1); |
| 251 | int64_t MAX_MK_DIM = 8; | 252 | int64_t MAX_MK_DIM = 8; |
| @@ -262,15 +263,15 @@ static bool CheckAscendCScenario(const aclTensor* x1, const aclTensor* x2, const | |||
| 262 | const aclIntArray* GetOutputSize(const aclTensor* x1, const aclTensor* x2, const bool adjX1, const bool adjX2, | 263 | const aclIntArray* GetOutputSize(const aclTensor* x1, const aclTensor* x2, const bool adjX1, const bool adjX2, |
| 263 | aclOpExecutor* executor) | 264 | aclOpExecutor* executor) |
| 264 | { | 265 | { |
| 265 | - constexpr size_t maxDim = 6; | 266 | + constexpr size_t maxDim = 8; |
| 266 | constexpr size_t minDim = 2; | 267 | constexpr size_t minDim = 2; |
| 267 | - constexpr size_t maxBatchDim = 4; | 268 | + constexpr size_t maxBatchDim = 6; |
| 268 | size_t x1DimSize = x1->GetViewShape().GetDimNum(); | 269 | size_t x1DimSize = x1->GetViewShape().GetDimNum(); |
| 269 | size_t x2DimSize = x2->GetViewShape().GetDimNum(); | 270 | size_t x2DimSize = x2->GetViewShape().GetDimNum(); |
| 270 | if ((x1DimSize < minDim || x1DimSize > maxDim) || (x2DimSize < minDim || x2DimSize > maxDim)) { | 271 | if ((x1DimSize < minDim || x1DimSize > maxDim) || (x2DimSize < minDim || x2DimSize > maxDim)) { |
| 271 | OP_LOGE( | 272 | OP_LOGE( |
| 272 | ACLNN_ERR_INNER_NULLPTR, | 273 | ACLNN_ERR_INNER_NULLPTR, |
| 273 | - "Calculate BatchMatMul out unsuccessfully, one of input dim belows 2 or exceeds 6, which is %zu and %zu.", | 274 | + "Calculate BatchMatMul out unsuccessfully, one of input dim belows 2 or exceeds 8, which is %zu and %zu.", |
| 274 | x1DimSize, x2DimSize); | 275 | x1DimSize, x2DimSize); |
| 275 | return nullptr; | 276 | return nullptr; |
| 276 | } | 277 | } |
| @@ -281,7 +282,7 @@ const aclIntArray* GetOutputSize(const aclTensor* x1, const aclTensor* x2, const | |||
| 281 | FVector<int64_t> batchDimForX2 = GetBatchDim(x2); | 282 | FVector<int64_t> batchDimForX2 = GetBatchDim(x2); |
| 282 | 283 | ||
| 283 | std::vector<int64_t> outShape; | 284 | std::vector<int64_t> outShape; |
| 284 | - for (size_t i = maxDim - outDimSize; i < maxBatchDim; i++) { // outDimSize is 2~6, i is 0~3 | 285 | + for (size_t i = maxDim - outDimSize; i < maxBatchDim; i++) { // outDimSize is 2~8, i is 0~5 |
| 285 | outShape.emplace_back(std::max(batchDimForX1[i], batchDimForX2[i])); | 286 | outShape.emplace_back(std::max(batchDimForX1[i], batchDimForX2[i])); |
| 286 | } | 287 | } |
| 287 | outShape.emplace_back(outM); | 288 | outShape.emplace_back(outM); |
| @@ -23,7 +23,7 @@ | |||
| 23 | 23 | ||
| 24 | ## 功能说明 | 24 | ## 功能说明 |
| 25 | 25 | ||
| 26 | -- 接口功能:完成张量self与张量mat2的矩阵乘计算(支持1维到6维作为输入的矩阵乘)。 | 26 | +- 接口功能:完成张量self与张量mat2的矩阵乘计算(支持1维到8维作为输入的矩阵乘)。 |
| 27 | 相似接口有aclnnMm(支持2维Tensor作为输入的矩阵乘)和aclnnBatchMatmul(仅支持3维的矩阵乘,其中第1维为batch)。 | 27 | 相似接口有aclnnMm(支持2维Tensor作为输入的矩阵乘)和aclnnBatchMatmul(仅支持3维的矩阵乘,其中第1维为batch)。 |
| 28 | - 计算公式: | 28 | - 计算公式: |
| 29 | 29 | ||
| @@ -86,7 +86,7 @@ aclnnStatus aclnnMatmul( | |||
| 86 | <li>需要与mat2满足<a href="../../../docs/zh/context/broadcast_relationship.md">broadcast关系</a>。</li></ul></td> | 86 | <li>需要与mat2满足<a href="../../../docs/zh/context/broadcast_relationship.md">broadcast关系</a>。</li></ul></td> |
| 87 | <td>BFLOAT16、FLOAT16、FLOAT32</td> | 87 | <td>BFLOAT16、FLOAT16、FLOAT32</td> |
| 88 | <td>ND</td> | 88 | <td>ND</td> |
| 89 | - <td>1~6</td> | 89 | + <td>1~8</td> |
| 90 | <td>√</td> | 90 | <td>√</td> |
| 91 | </tr> | 91 | </tr> |
| 92 | <tr> | 92 | <tr> |
| @@ -97,7 +97,7 @@ aclnnStatus aclnnMatmul( | |||
| 97 | </td> | 97 | </td> |
| 98 | <td>BFLOAT16、FLOAT16、FLOAT32</td> | 98 | <td>BFLOAT16、FLOAT16、FLOAT32</td> |
| 99 | <td>ND</td> | 99 | <td>ND</td> |
| 100 | - <td>1~6</td> | 100 | + <td>1~8</td> |
| 101 | <td>√</td> | 101 | <td>√</td> |
| 102 | </tr> | 102 | </tr> |
| 103 | <tr> | 103 | <tr> |
| @@ -107,7 +107,7 @@ aclnnStatus aclnnMatmul( | |||
| 107 | <td>数据类型需要与self与mat2推导之后的数据类型满足推导规则(参见<a href="../../../docs/zh/context/deduction_relationship.md">互推导关系</a>和<a href="#约束说明">约束说明</a>)。</td> | 107 | <td>数据类型需要与self与mat2推导之后的数据类型满足推导规则(参见<a href="../../../docs/zh/context/deduction_relationship.md">互推导关系</a>和<a href="#约束说明">约束说明</a>)。</td> |
| 108 | <td>BFLOAT16、FLOAT16、FLOAT32</td> | 108 | <td>BFLOAT16、FLOAT16、FLOAT32</td> |
| 109 | <td>ND</td> | 109 | <td>ND</td> |
| 110 | - <td>1~6</td> | 110 | + <td>1~8</td> |
| 111 | <td>-</td> | 111 | <td>-</td> |
| 112 | </tr> | 112 | </tr> |
| 113 | <tr> | 113 | <tr> |
| @@ -49,7 +49,7 @@ static constexpr size_t LAST_FIRST_DIM_INDEX = 1; | |||
| 49 | static const int NZ_K0_VALUE_16 = 16; | 49 | static const int NZ_K0_VALUE_16 = 16; |
| 50 | static const int NZ_K0_VALUE_32 = 8; | 50 | static const int NZ_K0_VALUE_32 = 8; |
| 51 | static const int NZ_STORAGE_PENULTIMATE_DIM = 16; | 51 | static const int NZ_STORAGE_PENULTIMATE_DIM = 16; |
| 52 | -static const size_t MAX_SUPPORT_MATMUL_DIMS_NUMS = 6; | 52 | +static const size_t MAX_SUPPORT_MATMUL_DIMS_NUMS = 8; |
| 53 | static const std::initializer_list<op::DataType> DTYPE_SUPPORT_LIST = {DataType::DT_FLOAT, DataType::DT_FLOAT16, | 53 | static const std::initializer_list<op::DataType> DTYPE_SUPPORT_LIST = {DataType::DT_FLOAT, DataType::DT_FLOAT16, |
| 54 | DataType::DT_BF16}; | 54 | DataType::DT_BF16}; |
| 55 | static const std::initializer_list<op::DataType> DTYPE_SUPPORT_LIST_WITHOUT_BF16 = {DataType::DT_FLOAT, | 55 | static const std::initializer_list<op::DataType> DTYPE_SUPPORT_LIST_WITHOUT_BF16 = {DataType::DT_FLOAT, |
| @@ -436,6 +436,46 @@ TEST_F(l2_matmul_test, test_max_dimentions_fp32_d8_d9) | |||
| 436 | MatMulCommonTest(a_t_desc, b_desc, out_desc, ACLNN_ERR_PARAM_INVALID); | 436 | MatMulCommonTest(a_t_desc, b_desc, out_desc, ACLNN_ERR_PARAM_INVALID); |
| 437 | } | 437 | } |
| 438 | 438 | ||
| 439 | +TEST_F(l2_matmul_test, test_bmm_fp32_d8_d8) | ||
| 440 | +{ | ||
| 441 | + TensorDesc a_t_desc = TensorDesc({1, 1, 2, 1, 2, 1, 4, 8}, ACL_FLOAT, ACL_FORMAT_ND); | ||
| 442 | + TensorDesc b_desc = TensorDesc({1, 1, 1, 2, 1, 1, 8, 4}, ACL_FLOAT, ACL_FORMAT_ND); | ||
| 443 | + TensorDesc out_desc = TensorDesc({1, 1, 2, 2, 2, 1, 4, 4}, ACL_FLOAT, ACL_FORMAT_ND).Precision(0.005, 0.005); | ||
| 444 | + MatMulCommonTest(a_t_desc, b_desc, out_desc, ACL_SUCCESS); | ||
| 445 | +} | ||
| 446 | + | ||
| 447 | +TEST_F(l2_matmul_test, test_bmm_fp32_d8_d2) | ||
| 448 | +{ | ||
| 449 | + TensorDesc a_t_desc = TensorDesc({1, 2, 1, 1, 2, 1, 4, 8}, ACL_FLOAT, ACL_FORMAT_ND); | ||
| 450 | + TensorDesc b_desc = TensorDesc({8, 3}, ACL_FLOAT, ACL_FORMAT_ND); | ||
| 451 | + TensorDesc out_desc = TensorDesc({1, 2, 1, 1, 2, 1, 4, 3}, ACL_FLOAT, ACL_FORMAT_ND).Precision(0.005, 0.005); | ||
| 452 | + MatMulCommonTest(a_t_desc, b_desc, out_desc, ACL_SUCCESS); | ||
| 453 | +} | ||
| 454 | + | ||
| 455 | +TEST_F(l2_matmul_test, test_bmm_fp32_d2_d8) | ||
| 456 | +{ | ||
| 457 | + TensorDesc a_t_desc = TensorDesc({4, 8}, ACL_FLOAT, ACL_FORMAT_ND); | ||
| 458 | + TensorDesc b_desc = TensorDesc({1, 2, 1, 1, 2, 1, 8, 3}, ACL_FLOAT, ACL_FORMAT_ND); | ||
| 459 | + TensorDesc out_desc = TensorDesc({1, 2, 1, 1, 2, 1, 4, 3}, ACL_FLOAT, ACL_FORMAT_ND).Precision(0.005, 0.005); | ||
| 460 | + MatMulCommonTest(a_t_desc, b_desc, out_desc, ACL_SUCCESS); | ||
| 461 | +} | ||
| 462 | + | ||
| 463 | +TEST_F(l2_matmul_test, test_bmm_fp32_d7_d7) | ||
| 464 | +{ | ||
| 465 | + TensorDesc a_t_desc = TensorDesc({1, 2, 1, 1, 2, 4, 8}, ACL_FLOAT, ACL_FORMAT_ND); | ||
| 466 | + TensorDesc b_desc = TensorDesc({2, 1, 1, 2, 1, 8, 3}, ACL_FLOAT, ACL_FORMAT_ND); | ||
| 467 | + TensorDesc out_desc = TensorDesc({2, 2, 1, 2, 2, 4, 3}, ACL_FLOAT, ACL_FORMAT_ND).Precision(0.005, 0.005); | ||
| 468 | + MatMulCommonTest(a_t_desc, b_desc, out_desc, ACL_SUCCESS); | ||
| 469 | +} | ||
| 470 | + | ||
| 471 | +TEST_F(l2_matmul_test, test_bmm_fp16_d8_d6_broadcast) | ||
| 472 | +{ | ||
| 473 | + TensorDesc a_t_desc = TensorDesc({2, 1, 1, 1, 1, 1, 4, 8}, ACL_FLOAT16, ACL_FORMAT_ND); | ||
| 474 | + TensorDesc b_desc = TensorDesc({1, 2, 1, 1, 8, 3}, ACL_FLOAT16, ACL_FORMAT_ND); | ||
| 475 | + TensorDesc out_desc = TensorDesc({2, 1, 1, 2, 1, 1, 4, 3}, ACL_FLOAT16, ACL_FORMAT_ND).Precision(0.005, 0.005); | ||
| 476 | + MatMulCommonTest(a_t_desc, b_desc, out_desc, ACL_SUCCESS); | ||
| 477 | +} | ||
| 478 | + | ||
| 439 | TEST_F(l2_matmul_test, test_broadcast_fp32_d3_d3) | 479 | TEST_F(l2_matmul_test, test_broadcast_fp32_d3_d3) |
| 440 | { | 480 | { |
| 441 | // 使用**Desc描述host api输入输出 | 481 | // 使用**Desc描述host api输入输出 |
| @@ -211,7 +211,8 @@ static TilingTestParam ascend910B_cases_params[] = { | |||
| 211 | 24, | 211 | 24, |
| 212 | 0, | 212 | 0, |
| 213 | "24 64 256 128 128 64 256 128 64 256 64 32 8 1 1 0 0 0 0 69632 16384 0 1 1 1 1 16 4 0 0 2 2 1 0 0 0 0 0 0 0 0 0 0 " | 213 | "24 64 256 128 128 64 256 128 64 256 64 32 8 1 1 0 0 0 0 69632 16384 0 1 1 1 1 16 4 0 0 2 2 1 0 0 0 0 0 0 0 0 0 0 " |
| 214 | - "0 0 0 0 0 0 0 1 1 4 2 0 0 123 123 0 0 0 0 0 0 0 0 0 0 0 0 24 2 2 2 1 1 1 1 1 1 1 1 1 2 2 2 0 0 64 2 0 0 1 0 ", | 214 | + "0 0 0 0 0 0 0 1 1 4 2 0 0 123 123 0 0 0 0 0 0 0 0 0 0 0 0 24 2 2 2 1 1 1 1 1 1 1 1 1 2 2 2 0 0 64 2 0 0 0 0 0 0 " |
| 215 | + "0 0 1 0 ", | ||
| 215 | {0, 1, 2}, | 216 | {0, 1, 2}, |
| 216 | {0, 1, 2}, | 217 | {0, 1, 2}, |
| 217 | {0, 1, 2}, | 218 | {0, 1, 2}, |
| @@ -242,7 +243,8 @@ static TilingTestParam ascend910B_cases_params[] = { | |||
| 242 | 24, | 243 | 24, |
| 243 | 32, | 244 | 32, |
| 244 | "24 64 256 128 128 64 256 128 64 256 64 32 8 1 1 0 0 0 0 69632 16384 0 1 1 1 1 16 4 0 0 2 2 1 0 0 0 0 0 0 0 0 0 0 " | 245 | "24 64 256 128 128 64 256 128 64 256 64 32 8 1 1 0 0 0 0 69632 16384 0 1 1 1 1 16 4 0 0 2 2 1 0 0 0 0 0 0 0 0 0 0 " |
| 245 | - "0 0 0 0 0 0 0 1 1 4 2 0 0 213 123 0 0 0 0 0 0 0 0 0 0 0 0 24 2 2 2 1 1 1 1 1 1 1 1 1 2 2 2 0 0 64 2 0 0 1 0 ", | 246 | + "0 0 0 0 0 0 0 1 1 4 2 0 0 213 123 0 0 0 0 0 0 0 0 0 0 0 0 24 2 2 2 1 1 1 1 1 1 1 1 1 2 2 2 0 0 64 2 0 0 0 0 0 0 " |
| 247 | + "0 0 1 0 ", | ||
| 246 | {1, 0, 2}, | 248 | {1, 0, 2}, |
| 247 | {0, 1, 2}, | 249 | {0, 1, 2}, |
| 248 | {0, 1, 2}, | 250 | {0, 1, 2}, |
| @@ -274,7 +276,7 @@ static TilingTestParam ascend910B_cases_params[] = { | |||
| 274 | 32, | 276 | 32, |
| 275 | "24 48 512 144 144 48 512 144 48 512 32 80 8 1 1 0 0 0 0 104448 16384 0 1 1 1 1 40 4 0 0 2 2 1 0 0 0 0 0 0 0 0 0 " | 277 | "24 48 512 144 144 48 512 144 48 512 32 80 8 1 1 0 0 0 0 104448 16384 0 1 1 1 1 40 4 0 0 2 2 1 0 0 0 0 0 0 0 0 0 " |
| 276 | "0 0 0 0 0 0 0 0 1 1 3 2 0 0 213 123 1 0 0 0 0 0 0 0 16 144 0 0 24 128 128 128 1 1 1 1 1 1 1 1 1 128 128 128 0 0 " | 278 | "0 0 0 0 0 0 0 0 1 1 3 2 0 0 213 123 1 0 0 0 0 0 0 0 16 144 0 0 24 128 128 128 1 1 1 1 1 1 1 1 1 128 128 128 0 0 " |
| 277 | - "48 128 0 0 1 0 ", | 279 | + "48 128 0 0 0 0 0 0 0 0 1 0 ", |
| 278 | {1, 0, 2}, | 280 | {1, 0, 2}, |
| 279 | {0, 1, 2}, | 281 | {0, 1, 2}, |
| 280 | {1, 0, 2}, | 282 | {1, 0, 2}, |
| @@ -305,8 +307,8 @@ static TilingTestParam ascend910B_cases_params[] = { | |||
| 305 | 24, | 307 | 24, |
| 306 | 32, | 308 | 32, |
| 307 | "24 1 1024 4096 4096 1 384 4096 16 384 32 256 8 1 1 0 0 0 0 393216 16384 0 1 1 1 1 128 4 0 0 2 2 1 0 0 0 0 0 0 0 " | 309 | "24 1 1024 4096 4096 1 384 4096 16 384 32 256 8 1 1 0 0 0 0 393216 16384 0 1 1 1 1 128 4 0 0 2 2 1 0 0 0 0 0 0 0 " |
| 308 | - "0 0 0 0 0 0 0 0 0 0 1 1 1 8 0 0 213 123 0 0 0 0 0 0 0 0 0 0 0 0 24 8 8 8 1 1 1 1 1 1 1 1 1 8 8 8 0 0 1 8 0 0 1 " | 310 | + "0 0 0 0 0 0 0 0 0 0 1 1 1 8 0 0 213 123 0 0 0 0 0 0 0 0 0 0 0 0 24 8 8 8 1 1 1 1 1 1 1 1 1 8 8 8 0 0 1 8 0 0 0 0 " |
| 309 | - "0 ", | 311 | + "0 0 0 0 1 0 ", |
| 310 | {1, 0, 2}, | 312 | {1, 0, 2}, |
| 311 | {0, 1, 2}, | 313 | {0, 1, 2}, |
| 312 | {1, 0, 2}, | 314 | {1, 0, 2}, |
公共接口的修改是否兼容A5?A5当前还不支持8d输入