Merged
feat: broadcast 5维以上特殊场景路由至UB BRC模板 #797
hahaha22Add on Aug 29
feat: broadcast 5维以上特殊场景路由至UB BRC模板 #797
Merged
Merged fromhahaha22/opbasenddma_ubbrcintocann/opbasemaster
Total: 2 files changed+73-0
| @@ -422,6 +422,70 @@ private: | |||
| 422 | brcBaseTilingData->inputDims[i][1] = tailLength; | 422 | brcBaseTilingData->inputDims[i][1] = tailLength; |
| 423 | } | 423 | } |
| 424 | } | 424 | } |
| 425 | + /** | ||
| 426 | + * 判断是否为5维以上的特殊场景, 该场景走ub brc。 | ||
| 427 | + * 前置条件(任一不满足直接返回false): | ||
| 428 | + * 1. 输入输出总个数不超过SPECIAL_DIM_MAX_IN_OUT_NUM | ||
| 429 | + * 2. dtype大小为2或4字节 | ||
| 430 | + * 3. 输出维度数大于SPECIAL_DIM_THRESHOLD,且输出的第5维维不超过2 | ||
| 431 | + * 需同时存在以下两类输入,且每个输入均满足对应条件,才返回true: | ||
| 432 | + * 1. last brc输入(尾轴stride为0):全维度元素个数乘dTypeSize小于CACHE_LINE_512 | ||
| 433 | + * 2. 非last brc输入(无任何广播轴):内5维(从右往左)之积不超过SPECIAL_DIM_IN_THRESHOLD, | ||
| 434 | + * 外层维度之积不小于OUTER_PRODUCT_THRESHOLD且不超过2*OUTER_PRODUCT_THRESHOLD | ||
| 435 | + * @param dTypeSize dtype大小 | ||
| 436 | + * @return | ||
| 437 | + */ | ||
| 438 | + bool IsSpecialDimAboveFive(int64_t dTypeSize) | ||
IsSpecialDimAboveFive 命中时没有任何日志。目前 DoBroadcastOpTiling 里的日志只能看到最终的 isUbBroadcast 结果,无法区分是白名单、IsBigNLastBrc 还是这个新分支命中的。这类路由判定后续排查性能问题时不好定位,建议命中时补一条 INFO 日志说明走了 5 维以上特殊场景分支。 ![]() ![]() | |||
| 439 | + { | ||
| 440 | + if (tilingData.dims.size() > SPECIAL_DIM_MAX_IN_OUT_NUM) { | ||
| 441 | + return false; | ||
| 442 | + } | ||
| 443 | + if (dTypeSize != DTYPE_BYTES_2 && dTypeSize != DTYPE_BYTES_4) { | ||
| 444 | + return false; | ||
| 445 | + } | ||
| 446 | + const auto& outDims = tilingData.dims.back(); | ||
| 447 | + int64_t outDimNum = static_cast<int64_t>(outDims.size()); | ||
| 448 | + if (outDimNum <= SPECIAL_DIM_THRESHOLD) { | ||
| 449 | + return false; | ||
| 450 | + } | ||
| 451 | + int64_t innerStart = outDimNum - SPECIAL_DIM_THRESHOLD; | ||
| 452 | + if (outDims[innerStart] > VALUE_TWO) { | ||
| 453 | + return false; | ||
| 454 | + } | ||
| 455 | + bool checkB = false; | ||
| 456 | + bool checkA = false; | ||
| 457 | + for (uint64_t i = 0; i < tilingData.dims.size() - 1; i++) { | ||
| 458 | + if (tilingData.strides[i].back() == 0) { | ||
| 459 | + int64_t inputProduct = 1; | ||
| 460 | + for (const auto& dim : tilingData.dims[i]) { | ||
| 461 | + inputProduct *= dim; | ||
这一段新增代码里有 9 行(461-483 行间)用了 tab 开头再接空格的缩进,比如 inputProduct *= dim; 和 outerProduct *= tilingData.dims[i][j];,而整个文件其余部分都是纯空格缩进,混用后不同编辑器下对齐会漂移,建议统一改成空格。 ![]() ![]() | |||
| 462 | + } | ||
| 463 | + if (inputProduct * dTypeSize >= CACHE_LINE_512) { | ||
| 464 | + return false; | ||
| 465 | + } | ||
| 466 | + checkB = true; | ||
| 467 | + } else { | ||
| 468 | + int64_t innerProduct = 1; | ||
| 469 | + int64_t outerProduct = 1; | ||
| 470 | + for (uint64_t j = 0; j < tilingData.dims[i].size(); j++) { | ||
| 471 | + if (tilingData.strides[i][j] == 0) { | ||
| 472 | + return false; | ||
| 473 | + } | ||
| 474 | + if (j < static_cast<uint64_t>(innerStart)) { | ||
| 475 | + outerProduct *= tilingData.dims[i][j]; | ||
| 476 | + } else { | ||
| 477 | + innerProduct *= tilingData.dims[i][j]; | ||
| 478 | + } | ||
| 479 | + } | ||
| 480 | + if (innerProduct > SPECIAL_DIM_IN_THRESHOLD || outerProduct < OUTER_PRODUCT_THRESHOLD || | ||
| 481 | + outerProduct > VALUE_TWO * OUTER_PRODUCT_THRESHOLD) { | ||
| 482 | + return false; | ||
| 483 | + } | ||
| 484 | + checkA = true; | ||
| 485 | + } | ||
| 486 | + } | ||
| 487 | + return checkA && checkB; | ||
| 488 | + } | ||
| 425 | /** | 489 | /** |
| 426 | * 判断是否为nlast brc并且尾轴大于4096场景, 该场景走ub brc。 | 490 | * 判断是否为nlast brc并且尾轴大于4096场景, 该场景走ub brc。 |
| 427 | * 1. 该输入必须是copyInBrc节点 | 491 | * 1. 该输入必须是copyInBrc节点 |
| @@ -506,6 +570,9 @@ private: | |||
| 506 | OP_CHECK_NULL_WITH_CONTEXT(context_, inputDesc); | 570 | OP_CHECK_NULL_WITH_CONTEXT(context_, inputDesc); |
| 507 | auto inputDtype = inputDesc->GetDataType(); | 571 | auto inputDtype = inputDesc->GetDataType(); |
| 508 | int64_t dTypeSize = ge::GetSizeByDataType(inputDtype); | 572 | int64_t dTypeSize = ge::GetSizeByDataType(inputDtype); |
| 573 | + if (IsSpecialDimAboveFive(dTypeSize)) { | ||
IsSpecialDimAboveFive 命中后直接改走 UB BRC,会改变现网这类 5 维以上非对齐场景的默认执行路径(原来走 NDDMA)。另外该分支放开了 dtype 约束:原有白名单 ubBroadcastDtypes 只包含 1/2 字节类型,fp32/int32/uint32 等 4 字节 dtype 配合 CopyInBrc 走 UB BRC 属于新增组合。PR 描述中测试栏还是"待补充",建议合入前补齐二级冒烟和算子泛化验证,重点覆盖 4 字节 dtype 与非对齐尾轴的精度和性能回归。 ![]() ![]() | |||
| 574 | + return true; | ||
| 575 | + } | ||
| 509 | if (dTypeSize != 0 && lastDim % (BLOCK_LENGTH / dTypeSize) == 0 && ubBroadcastDtypes.count(inputDtype)) { | 576 | if (dTypeSize != 0 && lastDim % (BLOCK_LENGTH / dTypeSize) == 0 && ubBroadcastDtypes.count(inputDtype)) { |
| 510 | return true; | 577 | return true; |
| 511 | } | 578 | } |
| @@ -53,6 +53,12 @@ constexpr int64_t DB_LOOP = 2; | |||
| 53 | constexpr uint64_t DIM_TWO = 2; | 53 | constexpr uint64_t DIM_TWO = 2; |
| 54 | constexpr uint64_t DIM_THREE = 3; | 54 | constexpr uint64_t DIM_THREE = 3; |
| 55 | constexpr int64_t SINGLE_CORE_SIZE_LIMIT = 8 * 1024; | 55 | constexpr int64_t SINGLE_CORE_SIZE_LIMIT = 8 * 1024; |
| 56 | +constexpr int64_t SPECIAL_DIM_THRESHOLD = 5; | ||
| 57 | +constexpr int64_t SPECIAL_DIM_IN_THRESHOLD = 64; | ||
| 58 | +constexpr int64_t SPECIAL_DIM_MAX_IN_OUT_NUM = 3; | ||
| 59 | +constexpr int64_t OUTER_PRODUCT_THRESHOLD = 32768; | ||
| 60 | +constexpr int64_t DTYPE_BYTES_2 = 2; | ||
| 61 | +constexpr int64_t DTYPE_BYTES_4 = 4; | ||
| 56 | 62 | ||
| 57 | static constexpr uint32_t BLOCK_LENGTH = 32; | 63 | static constexpr uint32_t BLOCK_LENGTH = 32; |
| 58 | static constexpr uint64_t BROADCAST_COMPUTE_KEY_OFFSET = 1000000; | 64 | static constexpr uint64_t BROADCAST_COMPUTE_KEY_OFFSET = 1000000; |


条件约束太多了,需要更通用的约束条件