Merged
CombinedNMS性能优化 #1712
wuxiyuanAdd on Sep 20
CombinedNMS性能优化 #1712
Merged
Merged fromwuxiyuan/ops-cv9.2.0intocann/ops-cv9.2.0
Total: 3 files changed+88-44
| @@ -33,7 +33,7 @@ constexpr uint32_t WORKSPACE_COUNT = 1; | |||
| 33 | constexpr int32_t SCHEDULE_MODE = 1; | 33 | constexpr int32_t SCHEDULE_MODE = 1; |
| 34 | constexpr uint64_t SIMT_UB_RESERVE = 32 * 1024; | 34 | constexpr uint64_t SIMT_UB_RESERVE = 32 * 1024; |
| 35 | constexpr uint64_t FALLBACK_LOCAL_MEMORY_SIZE = 64 * 1024; | 35 | constexpr uint64_t FALLBACK_LOCAL_MEMORY_SIZE = 64 * 1024; |
| 36 | -constexpr int32_t HOT_UB_MAX_BOXES = 4096; | 36 | +constexpr int32_t HOT_UB_MAX_BOXES = 8704; |
| 37 | 37 | ||
| 38 | struct CombinedNonMaxSuppressionCompileInfo { | 38 | struct CombinedNonMaxSuppressionCompileInfo { |
| 39 | int32_t coreNum = 0; | 39 | int32_t coreNum = 0; |
| @@ -112,6 +112,10 @@ ge::graphStatus ValidateAndFill(gert::TilingContext* context, CombinedNonMaxSupp | |||
| 112 | OP_CHECK_IF(outputSize <= 0 || outputSize > maxTotal, | 112 | OP_CHECK_IF(outputSize <= 0 || outputSize > maxTotal, |
| 113 | OP_LOGE(context, "output_size must be in [1, max_total_size]"), return ge::GRAPH_FAILED); | 113 | OP_LOGE(context, "output_size must be in [1, max_total_size]"), return ge::GRAPH_FAILED); |
| 114 | 114 | ||
| 115 | + // A class cannot contribute more than outputSize candidates to the final merge. | ||
| 116 | + // Preserve its ordered prefix, including the existing deterministic tie break. | ||
| 117 | + maxPerClass = std::min(maxPerClass, static_cast<int32_t>(std::min(numBoxes, outputSize))); | ||
| 118 | + | ||
| 115 | const auto* compileInfo = context->GetCompileInfo<CombinedNonMaxSuppressionCompileInfo>(); | 119 | const auto* compileInfo = context->GetCompileInfo<CombinedNonMaxSuppressionCompileInfo>(); |
| 116 | OP_CHECK_NULL_WITH_CONTEXT(context, compileInfo); | 120 | OP_CHECK_NULL_WITH_CONTEXT(context, compileInfo); |
| 117 | const int64_t taskCount = batch * classes; | 121 | const int64_t taskCount = batch * classes; |
| @@ -16,6 +16,7 @@ | |||
| 16 | #include "simt_api/asc_simt.h" | 16 | #include "simt_api/asc_simt.h" |
| 17 | #include "simt_api/common_functions.h" | 17 | #include "simt_api/common_functions.h" |
| 18 | #include "simt_api/device_functions.h" | 18 | #include "simt_api/device_functions.h" |
| 19 | +#include "simt_api/device_warp_functions.h" | ||
| 19 | #include "combined_non_max_suppression_tiling_data.h" | 20 | #include "combined_non_max_suppression_tiling_data.h" |
| 20 | 21 | ||
| 21 | namespace CombinedNonMaxSuppressionOps { | 22 | namespace CombinedNonMaxSuppressionOps { |
| @@ -24,7 +25,7 @@ using namespace AscendC; | |||
| 24 | constexpr uint32_t THREAD_NUM = 1024; | 25 | constexpr uint32_t THREAD_NUM = 1024; |
| 25 | constexpr uint32_t MIN_THREAD_NUM = 32; | 26 | constexpr uint32_t MIN_THREAD_NUM = 32; |
| 26 | constexpr uint32_t MAX_NUM_CLASSES = 200; | 27 | constexpr uint32_t MAX_NUM_CLASSES = 200; |
| 27 | -constexpr uint32_t HOT_UB_MAX_BOXES = 4096; | 28 | +constexpr uint32_t HOT_UB_MAX_BOXES = 8704; |
| 28 | constexpr uint32_t UB_ALIGN_BYTES = 32; | 29 | constexpr uint32_t UB_ALIGN_BYTES = 32; |
| 29 | constexpr float NEG_INF = -3.402823466e+38F; | 30 | constexpr float NEG_INF = -3.402823466e+38F; |
| 30 | 31 | ||
| @@ -51,6 +52,48 @@ __simt_callee__ __aicore__ inline bool IsBetter(float lhsScore, int32_t lhsIndex | |||
| 51 | return lhsScore > rhsScore || (lhsScore == rhsScore && lhsIndex >= 0 && (rhsIndex < 0 || lhsIndex < rhsIndex)); | 52 | return lhsScore > rhsScore || (lhsScore == rhsScore && lhsIndex >= 0 && (rhsIndex < 0 || lhsIndex < rhsIndex)); |
| 52 | } | 53 | } |
| 53 | 54 | ||
| 55 | +// Preserve the score/index total order while reducing inside registers first. | ||
| 56 | +__simt_callee__ __aicore__ inline void WarpArgMax(float& score, int32_t& index) | ||
| 57 | +{ | ||
| 58 | + for (uint32_t offset = warpSize / 2; offset > 0; offset >>= 1) { | ||
| 59 | + const float otherScore = asc_shfl_down(score, offset); | ||
| 60 | + const int32_t otherIndex = asc_shfl_down(index, offset); | ||
| 61 | + if (IsBetter(otherScore, otherIndex, score, index)) { | ||
| 62 | + score = otherScore; | ||
| 63 | + index = otherIndex; | ||
| 64 | + } | ||
| 65 | + } | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +// All callers launch complete warps (32..1024 threads). Only warp winners | ||
| 69 | +// cross UB; two block barriers replace the per-level shared-memory barriers. | ||
| 70 | +__simt_callee__ __aicore__ inline void BlockArgMax(float score, int32_t index, __ubuf__ float* reduceScores, | ||
| 71 | + __ubuf__ int32_t* reduceIndices) | ||
| 72 | +{ | ||
| 73 | + const uint32_t tid = threadIdx.x; | ||
| 74 | + const uint32_t lane = tid % warpSize; | ||
| 75 | + const uint32_t warp = tid / warpSize; | ||
| 76 | + WarpArgMax(score, index); | ||
| 77 | + if (lane == 0) { | ||
| 78 | + reduceScores[warp] = score; | ||
| 79 | + reduceIndices[warp] = index; | ||
| 80 | + } | ||
| 81 | + asc_syncthreads(); | ||
| 82 | + if (blockDim.x > warpSize) { | ||
| 83 | + if (warp == 0) { | ||
| 84 | + const uint32_t warpCount = blockDim.x / warpSize; | ||
| 85 | + score = lane < warpCount ? reduceScores[lane] : NEG_INF; | ||
| 86 | + index = lane < warpCount ? reduceIndices[lane] : -1; | ||
| 87 | + WarpArgMax(score, index); | ||
| 88 | + if (lane == 0) { | ||
| 89 | + reduceScores[0] = score; | ||
| 90 | + reduceIndices[0] = index; | ||
| 91 | + } | ||
| 92 | + } | ||
| 93 | + asc_syncthreads(); | ||
| 94 | + } | ||
| 95 | +} | ||
| 96 | + | ||
| 54 | template <typename T> | 97 | template <typename T> |
| 55 | __simt_callee__ __aicore__ inline float CalcIou(__gm__ const T* boxes, int64_t lhsOffset, int64_t rhsOffset) | 98 | __simt_callee__ __aicore__ inline float CalcIou(__gm__ const T* boxes, int64_t lhsOffset, int64_t rhsOffset) |
| 56 | { | 99 | { |
| @@ -98,13 +141,17 @@ template <typename T> | |||
| 98 | __simt_vf__ LAUNCH_BOUND(THREAD_NUM) __aicore__ | 141 | __simt_vf__ LAUNCH_BOUND(THREAD_NUM) __aicore__ |
| 99 | void LoadTaskHotData(__gm__ const T* boxes, __gm__ const T* scores, __ubuf__ float* boxesUb, | 142 | void LoadTaskHotData(__gm__ const T* boxes, __gm__ const T* scores, __ubuf__ float* boxesUb, |
| 100 | __ubuf__ float* scoresUb, int32_t batchIdx, int32_t classIdx, int32_t numBoxes, | 143 | __ubuf__ float* scoresUb, int32_t batchIdx, int32_t classIdx, int32_t numBoxes, |
| 101 | - int32_t boxClasses, int32_t numClasses) | 144 | + int32_t boxClasses, int32_t numClasses, float scoreThreshold) |
| 102 | { | 145 | { |
| 103 | const int32_t boxClass = boxClasses == 1 ? 0 : classIdx; | 146 | const int32_t boxClass = boxClasses == 1 ? 0 : classIdx; |
| 104 | for (int32_t anchor = static_cast<int32_t>(threadIdx.x); anchor < numBoxes; | 147 | for (int32_t anchor = static_cast<int32_t>(threadIdx.x); anchor < numBoxes; |
| 105 | anchor += static_cast<int32_t>(blockDim.x)) { | 148 | anchor += static_cast<int32_t>(blockDim.x)) { |
| 106 | const int64_t scoreOffset = (static_cast<int64_t>(batchIdx) * numBoxes + anchor) * numClasses + classIdx; | 149 | const int64_t scoreOffset = (static_cast<int64_t>(batchIdx) * numBoxes + anchor) * numClasses + classIdx; |
| 107 | scoresUb[anchor] = scores[scoreOffset]; | 150 | scoresUb[anchor] = scores[scoreOffset]; |
| 151 | + // Ineligible scores never enter NMS: avoid staging their strided boxes. | ||
| 152 | + if (!(scoresUb[anchor] > scoreThreshold)) { | ||
| 153 | + continue; | ||
| 154 | + } | ||
| 108 | const int64_t boxOffset = ((static_cast<int64_t>(batchIdx) * numBoxes + anchor) * boxClasses + boxClass) * 4; | 155 | const int64_t boxOffset = ((static_cast<int64_t>(batchIdx) * numBoxes + anchor) * boxClasses + boxClass) * 4; |
| 109 | const int32_t ubOffset = anchor * 4; | 156 | const int32_t ubOffset = anchor * 4; |
| 110 | boxesUb[ubOffset] = boxes[boxOffset]; | 157 | boxesUb[ubOffset] = boxes[boxOffset]; |
| @@ -122,7 +169,7 @@ __simt_vf__ LAUNCH_BOUND(THREAD_NUM) __aicore__ | |||
| 122 | { | 169 | { |
| 123 | const uint32_t tid = threadIdx.x; | 170 | const uint32_t tid = threadIdx.x; |
| 124 | for (int32_t index = static_cast<int32_t>(tid); index < numBoxes; index += static_cast<int32_t>(blockDim.x)) { | 171 | for (int32_t index = static_cast<int32_t>(tid); index < numBoxes; index += static_cast<int32_t>(blockDim.x)) { |
| 125 | - suppressed[index] = 0; | 172 | + suppressed[index] = scores[index] > scoreThreshold ? 0 : 1; |
| 126 | } | 173 | } |
| 127 | for (int32_t index = static_cast<int32_t>(tid); index < maxOutputPerClass; | 174 | for (int32_t index = static_cast<int32_t>(tid); index < maxOutputPerClass; |
| 128 | index += static_cast<int32_t>(blockDim.x)) { | 175 | index += static_cast<int32_t>(blockDim.x)) { |
| @@ -145,18 +192,7 @@ __simt_vf__ LAUNCH_BOUND(THREAD_NUM) __aicore__ | |||
| 145 | localBestIndex = anchor; | 192 | localBestIndex = anchor; |
| 146 | } | 193 | } |
| 147 | } | 194 | } |
| 148 | - reduceScores[tid] = localBestScore; | 195 | + BlockArgMax(localBestScore, localBestIndex, reduceScores, reduceIndices); |
| 149 | - reduceIndices[tid] = localBestIndex; | ||
| 150 | - asc_syncthreads(); | ||
| 151 | - | ||
| 152 | - for (uint32_t stride = static_cast<uint32_t>(blockDim.x) / 2; stride > 0; stride >>= 1) { | ||
| 153 | - if (tid < stride && IsBetter(reduceScores[tid + stride], reduceIndices[tid + stride], reduceScores[tid], | ||
| 154 | - reduceIndices[tid])) { | ||
| 155 | - reduceScores[tid] = reduceScores[tid + stride]; | ||
| 156 | - reduceIndices[tid] = reduceIndices[tid + stride]; | ||
| 157 | - } | ||
| 158 | - asc_syncthreads(); | ||
| 159 | - } | ||
| 160 | 196 | ||
| 161 | const int32_t bestIndex = reduceIndices[0]; | 197 | const int32_t bestIndex = reduceIndices[0]; |
| 162 | if (bestIndex < 0) { | 198 | if (bestIndex < 0) { |
| @@ -169,6 +205,10 @@ __simt_vf__ LAUNCH_BOUND(THREAD_NUM) __aicore__ | |||
| 169 | } | 205 | } |
| 170 | asc_syncthreads(); | 206 | asc_syncthreads(); |
| 171 | 207 | ||
| 208 | + // No later selection consumes suppression after the last output. | ||
| 209 | + if (outputIndex + 1 == maxOutputPerClass) { | ||
| 210 | + break; | ||
| 211 | + } | ||
| 172 | const int32_t bestBoxOffset = bestIndex * 4; | 212 | const int32_t bestBoxOffset = bestIndex * 4; |
| 173 | for (int32_t anchor = static_cast<int32_t>(tid); anchor < numBoxes; | 213 | for (int32_t anchor = static_cast<int32_t>(tid); anchor < numBoxes; |
| 174 | anchor += static_cast<int32_t>(blockDim.x)) { | 214 | anchor += static_cast<int32_t>(blockDim.x)) { |
| @@ -219,18 +259,7 @@ __simt_vf__ LAUNCH_BOUND(THREAD_NUM) __aicore__ | |||
| 219 | localBestIndex = anchor; | 259 | localBestIndex = anchor; |
| 220 | } | 260 | } |
| 221 | } | 261 | } |
| 222 | - reduceScores[tid] = localBestScore; | 262 | + BlockArgMax(localBestScore, localBestIndex, reduceScores, reduceIndices); |
| 223 | - reduceIndices[tid] = localBestIndex; | ||
| 224 | - asc_syncthreads(); | ||
| 225 | - | ||
| 226 | - for (uint32_t stride = static_cast<uint32_t>(blockDim.x) / 2; stride > 0; stride >>= 1) { | ||
| 227 | - if (tid < stride && IsBetter(reduceScores[tid + stride], reduceIndices[tid + stride], reduceScores[tid], | ||
| 228 | - reduceIndices[tid])) { | ||
| 229 | - reduceScores[tid] = reduceScores[tid + stride]; | ||
| 230 | - reduceIndices[tid] = reduceIndices[tid + stride]; | ||
| 231 | - } | ||
| 232 | - asc_syncthreads(); | ||
| 233 | - } | ||
| 234 | 263 | ||
| 235 | const int32_t bestIndex = reduceIndices[0]; | 264 | const int32_t bestIndex = reduceIndices[0]; |
| 236 | if (bestIndex < 0) { | 265 | if (bestIndex < 0) { |
| @@ -244,6 +273,9 @@ __simt_vf__ LAUNCH_BOUND(THREAD_NUM) __aicore__ | |||
| 244 | } | 273 | } |
| 245 | asc_syncthreads(); | 274 | asc_syncthreads(); |
| 246 | 275 | ||
| 276 | + if (outputIndex + 1 == maxOutputPerClass) { | ||
| 277 | + break; | ||
| 278 | + } | ||
| 247 | const int64_t bestBoxOffset = ((static_cast<int64_t>(batchIdx) * numBoxes + bestIndex) * boxClasses + | 279 | const int64_t bestBoxOffset = ((static_cast<int64_t>(batchIdx) * numBoxes + bestIndex) * boxClasses + |
| 248 | boxClass) * | 280 | boxClass) * |
| 249 | 4; | 281 | 4; |
| @@ -323,18 +355,7 @@ __simt_vf__ LAUNCH_BOUND(THREAD_NUM) __aicore__ | |||
| 323 | localBestIndex = candidate; | 355 | localBestIndex = candidate; |
| 324 | } | 356 | } |
| 325 | } | 357 | } |
| 326 | - reduceScores[tid] = localBestScore; | 358 | + BlockArgMax(localBestScore, localBestIndex, reduceScores, reduceIndices); |
| 327 | - reduceIndices[tid] = localBestIndex; | ||
| 328 | - asc_syncthreads(); | ||
| 329 | - | ||
| 330 | - for (uint32_t stride = static_cast<uint32_t>(blockDim.x) / 2; stride > 0; stride >>= 1) { | ||
| 331 | - if (tid < stride && IsBetter(reduceScores[tid + stride], reduceIndices[tid + stride], reduceScores[tid], | ||
| 332 | - reduceIndices[tid])) { | ||
| 333 | - reduceScores[tid] = reduceScores[tid + stride]; | ||
| 334 | - reduceIndices[tid] = reduceIndices[tid + stride]; | ||
| 335 | - } | ||
| 336 | - asc_syncthreads(); | ||
| 337 | - } | ||
| 338 | 359 | ||
| 339 | const int32_t bestCandidate = reduceIndices[0]; | 360 | const int32_t bestCandidate = reduceIndices[0]; |
| 340 | if (bestCandidate < 0 || reduceScores[0] == NEG_INF) { | 361 | if (bestCandidate < 0 || reduceScores[0] == NEG_INF) { |
| @@ -469,10 +490,10 @@ private: | |||
| 469 | LocalTensor<int32_t> selectedCountLocal = selectedCountBuffer_.Get<int32_t>(); | 490 | LocalTensor<int32_t> selectedCountLocal = selectedCountBuffer_.Get<int32_t>(); |
| 470 | 491 | ||
| 471 | if (useHotUb_) { | 492 | if (useHotUb_) { |
| 472 | - asc_vf_call<LoadTaskHotData<T>>(dim3(selectThreadNum), boxes_, scores_, | 493 | + asc_vf_call<LoadTaskHotData<T>>( |
| 473 | - reinterpret_cast<__ubuf__ float*>(boxesLocal.GetPhyAddr()), | 494 | + dim3(selectThreadNum), boxes_, scores_, reinterpret_cast<__ubuf__ float*>(boxesLocal.GetPhyAddr()), |
| 474 | - reinterpret_cast<__ubuf__ float*>(scoresLocal.GetPhyAddr()), batchIdx, | 495 | + reinterpret_cast<__ubuf__ float*>(scoresLocal.GetPhyAddr()), batchIdx, classIdx, tiling_->numBoxes, |
| 475 | - classIdx, tiling_->numBoxes, tiling_->boxClasses, tiling_->numClasses); | 496 | + tiling_->boxClasses, tiling_->numClasses, tiling_->scoreThreshold); |
| 476 | asc_vf_call<SelectClassNmsUb>( | 497 | asc_vf_call<SelectClassNmsUb>( |
| 477 | dim3(selectThreadNum), reinterpret_cast<__ubuf__ float*>(boxesLocal.GetPhyAddr()), | 498 | dim3(selectThreadNum), reinterpret_cast<__ubuf__ float*>(boxesLocal.GetPhyAddr()), |
| 478 | reinterpret_cast<__ubuf__ float*>(scoresLocal.GetPhyAddr()), | 499 | reinterpret_cast<__ubuf__ float*>(scoresLocal.GetPhyAddr()), |
| @@ -99,6 +99,25 @@ TEST(CombinedNonMaxSuppressionTiling, BuildsWorkspaceForSharedBoxes) | |||
| 99 | EXPECT_EQ(info.workspaceSizes[0], static_cast<int64_t>(compileInfo.sysWorkspaceSize + 2560U)); | 99 | EXPECT_EQ(info.workspaceSizes[0], static_cast<int64_t>(compileInfo.sysWorkspaceSize + 2560U)); |
| 100 | } | 100 | } |
| 101 | 101 | ||
| 102 | +TEST(CombinedNonMaxSuppressionTiling, CapsClassCandidatesWithoutChangingOutputShape) | ||
| 103 | +{ | ||
| 104 | + int32_t maxPerClass = 984; | ||
| 105 | + int32_t maxTotal = 1000; | ||
| 106 | + float iouThreshold = 0.5F; | ||
| 107 | + float scoreThreshold = 0.5F; | ||
| 108 | + CombinedNonMaxSuppressionCompileInfoForTest compileInfo; | ||
| 109 | + for (int32_t outputSize : {1, 8, 1000}) { | ||
| 110 | + auto context = MakeContext({1, 13, 1, 4}, {1, 13, 3}, {1, outputSize, 4}, &maxPerClass, &maxTotal, | ||
| 111 | + &iouThreshold, &scoreThreshold, &compileInfo); | ||
| 112 | + TilingInfo info; | ||
| 113 | + ASSERT_TRUE(ExecuteTiling(context, info)); | ||
| 114 | + const auto* tiling = reinterpret_cast<const CombinedNonMaxSuppressionTilingData*>(info.tilingData.get()); | ||
| 115 | + EXPECT_EQ(tiling->maxOutputPerClass, outputSize < 13 ? outputSize : 13); | ||
| 116 | + EXPECT_EQ(tiling->outputSize, outputSize); | ||
| 117 | + EXPECT_EQ(tiling->maxTotalSize, 1000); | ||
| 118 | + } | ||
| 119 | +} | ||
| 120 | + | ||
| 102 | TEST(CombinedNonMaxSuppressionTiling, AcceptsFiniteNonnegativeIouThreshold) | 121 | TEST(CombinedNonMaxSuppressionTiling, AcceptsFiniteNonnegativeIouThreshold) |
| 103 | { | 122 | { |
| 104 | int32_t maxPerClass = 2; | 123 | int32_t maxPerClass = 2; |