Merged
DropOutV3 perf improve by unroll philox & fix nan/inf input precision error #5682
贾剑勇Add on Sep 15
DropOutV3 perf improve by unroll philox & fix nan/inf input precision error #5682
Merged
Merged fromjia-jianyong/ops-math9.2.0intocann/ops-math9.2.0
Total: 5 files changed+207-80
| @@ -178,17 +178,9 @@ ge::graphStatus DropOutV3Tiling::UniqueProcess() | |||
| 178 | } | 178 | } |
| 179 | } | 179 | } |
| 180 | dropOutV3TilingData_.vec = vec; | 180 | dropOutV3TilingData_.vec = vec; |
| 181 | - dropOutV3TilingData_.transportMode = (vec == 1) ? 1 : 0; | ||
| 182 | 181 | ||
| 183 | - // 计算 grid 和 totalThreads | 182 | + // 计算 totalThreads |
| 184 | - int64_t blockSize = SIMT_THREAD_GROUP_SIZE; | 183 | + dropOutV3TilingData_.totalThreads = AIC_CLUSTER_COUNT * MAX_THREADS_PER_AIC; |
| 185 | - int64_t maxThreadsPerMultiProcessor = MAX_THREADS_PER_AIC; | ||
| 186 | - int64_t blocksPerSM = maxThreadsPerMultiProcessor / blockSize; | ||
| 187 | - int64_t multiProcessorCount = AIC_CLUSTER_COUNT; | ||
| 188 | - int64_t blocksCount = multiProcessorCount * blocksPerSM; | ||
| 189 | - int64_t grid = (outputSize + blockSize - 1) / blockSize; | ||
| 190 | - grid = (blocksCount < grid) ? blocksCount : grid; | ||
| 191 | - dropOutV3TilingData_.totalThreads = static_cast<uint64_t>(grid * blockSize); | ||
| 192 | 184 | ||
| 193 | // 分核 | 185 | // 分核 |
| 194 | int64_t coreGranularity = DROPOUT_CORE_GRANULARITY; | 186 | int64_t coreGranularity = DROPOUT_CORE_GRANULARITY; |
| @@ -203,14 +195,15 @@ ge::graphStatus DropOutV3Tiling::UniqueProcess() | |||
| 203 | dropOutV3TilingData_.perCoreElements = perCoreElements; | 195 | dropOutV3TilingData_.perCoreElements = perCoreElements; |
| 204 | dropOutV3TilingData_.tailCoreElements = tailCoreElements; | 196 | dropOutV3TilingData_.tailCoreElements = tailCoreElements; |
| 205 | 197 | ||
| 206 | - // 分 UB (双buffer: 2*input + 2*output + 2*maskBit + randomFloatBuf) | 198 | + // 分 UB (双buffer: 2*input + 2*output + 2*maskBit + [2/1]*randomFloatBuf) |
| 199 | + uint32_t randBufNum = (vec == NUM_2) ? NUM_2 : 1; | ||
| 207 | int64_t perBlockBytes = (NUM_2 * coreGranularity * sizeofT) + (NUM_2 * coreGranularity * sizeofT) + | 200 | int64_t perBlockBytes = (NUM_2 * coreGranularity * sizeofT) + (NUM_2 * coreGranularity * sizeofT) + |
| 208 | - (NUM_2 * coreGranularity / NUM_8) + (coreGranularity * NUM_4); | 201 | + (NUM_2 * coreGranularity / NUM_8) + (coreGranularity * NUM_4 * randBufNum); |
| 209 | int64_t ubFactorElements = Ops::Base::FloorDiv(ubSize_, perBlockBytes) * coreGranularity; | 202 | int64_t ubFactorElements = Ops::Base::FloorDiv(ubSize_, perBlockBytes) * coreGranularity; |
| 210 | while (NUM_2 * Ops::Base::CeilAlign(ubFactorElements * sizeofT, ALIGNMENT_32) + | 203 | while (NUM_2 * Ops::Base::CeilAlign(ubFactorElements * sizeofT, ALIGNMENT_32) + |
| 211 | NUM_2 * Ops::Base::CeilAlign(ubFactorElements * sizeofT, ALIGNMENT_32) + | 204 | NUM_2 * Ops::Base::CeilAlign(ubFactorElements * sizeofT, ALIGNMENT_32) + |
| 212 | NUM_2 * Ops::Base::CeilAlign(ubFactorElements / NUM_8, ALIGNMENT_32) + | 205 | NUM_2 * Ops::Base::CeilAlign(ubFactorElements / NUM_8, ALIGNMENT_32) + |
| 213 | - Ops::Base::CeilAlign(ubFactorElements * NUM_4, ALIGNMENT_32) > | 206 | + Ops::Base::CeilAlign(ubFactorElements * NUM_4 * randBufNum, ALIGNMENT_32) > |
| 214 | ubSize_) { | 207 | ubSize_) { |
| 215 | ubFactorElements -= coreGranularity; | 208 | ubFactorElements -= coreGranularity; |
| 216 | } | 209 | } |
| @@ -27,6 +27,7 @@ constexpr static uint32_t NUM_16 = 16; | |||
| 27 | constexpr static int64_t ALIGNMENT_32 = 32; | 27 | constexpr static int64_t ALIGNMENT_32 = 32; |
| 28 | constexpr static uint16_t CORE_THREAD_NUMBER = 512; | 28 | constexpr static uint16_t CORE_THREAD_NUMBER = 512; |
| 29 | constexpr static float HIFLOAT_MULS = 65536.0f; | 29 | constexpr static float HIFLOAT_MULS = 65536.0f; |
| 30 | +constexpr static int64_t UINT32_MAX_VALUE = 4294967295; | ||
| 30 | 31 | ||
| 31 | // ==================== SIMT Kernels ==================== | 32 | // ==================== SIMT Kernels ==================== |
| 32 | 33 | ||
| @@ -91,7 +92,8 @@ static constexpr Reg::CastTrait castTraitI32ToF32 = {Reg::RegLayout::ZERO, Reg:: | |||
| 91 | template <typename T> | 92 | template <typename T> |
| 92 | __simd_callee__ inline void PhiloxCtrConvertAndDropout(Reg::RegTensor<uint32_t>& currCtr, | 93 | __simd_callee__ inline void PhiloxCtrConvertAndDropout(Reg::RegTensor<uint32_t>& currCtr, |
| 93 | Reg::RegTensor<uint32_t>& mask16Reg, | 94 | Reg::RegTensor<uint32_t>& mask16Reg, |
| 94 | - Reg::RegTensor<float>& zeroFloatReg, uint32_t currCount, | 95 | + Reg::RegTensor<float>& zeroFloatReg, |
| 96 | + Reg::RegTensor<float>& oneFloatReg, uint32_t currCount, | ||
| 95 | float scale, float prob, __ubuf__ float* randomFloatPtr, | 97 | float scale, float prob, __ubuf__ float* randomFloatPtr, |
| 96 | __ubuf__ T* inputPtr, __ubuf__ T* outputPtr) | 98 | __ubuf__ T* inputPtr, __ubuf__ T* outputPtr) |
| 97 | { | 99 | { |
| @@ -129,8 +131,10 @@ __simd_callee__ inline void PhiloxCtrConvertAndDropout(Reg::RegTensor<uint32_t>& | |||
| 129 | 131 | ||
| 130 | Reg::RegTensor<float> scaledInput; | 132 | Reg::RegTensor<float> scaledInput; |
| 131 | Reg::Muls<float>(scaledInput, inputFloatReg, scale, mask); | 133 | Reg::Muls<float>(scaledInput, inputFloatReg, scale, mask); |
| 134 | + Reg::RegTensor<float> maskBitReg; | ||
| 135 | + Reg::Select<float>(maskBitReg, oneFloatReg, zeroFloatReg, dropoutMask); | ||
| 132 | Reg::RegTensor<float> outputFloatReg; | 136 | Reg::RegTensor<float> outputFloatReg; |
| 133 | - Reg::Select<float>(outputFloatReg, scaledInput, zeroFloatReg, dropoutMask); | 137 | + Reg::Mul<float>(outputFloatReg, scaledInput, maskBitReg, mask); |
| 134 | 138 | ||
| 135 | if constexpr (IsSameType<T, float>::value) { | 139 | if constexpr (IsSameType<T, float>::value) { |
| 136 | Reg::StoreAlign<float>(outputPtr, outputFloatReg, mask); | 140 | Reg::StoreAlign<float>(outputPtr, outputFloatReg, mask); |
| @@ -271,12 +275,14 @@ __aicore__ inline void DropOutV3SimdImpl<T, U>::Init(GM_ADDR x, GM_ADDR y, GM_AD | |||
| 271 | GetUintDivMagicAndShift<uint64_t>(magic64_, shift64_, static_cast<uint64_t>(totalThreads_)); | 275 | GetUintDivMagicAndShift<uint64_t>(magic64_, shift64_, static_cast<uint64_t>(totalThreads_)); |
| 272 | blockIdx_ = GetBlockIdx(); | 276 | blockIdx_ = GetBlockIdx(); |
| 273 | 277 | ||
| 278 | + uint32_t randBufNum = (vec_ == NUM_2) ? NUM_2 : 1; | ||
| 274 | int64_t sizeofT = static_cast<int64_t>(sizeof(T)); | 279 | int64_t sizeofT = static_cast<int64_t>(sizeof(T)); |
| 275 | int64_t ubFactor = tilingData->ubFactorElements; | 280 | int64_t ubFactor = tilingData->ubFactorElements; |
| 276 | int64_t inputBufSize = Ops::Base::CeilAlign(ubFactor * sizeofT, ALIGNMENT_32); | 281 | int64_t inputBufSize = Ops::Base::CeilAlign(ubFactor * sizeofT, ALIGNMENT_32); |
| 277 | int64_t outputBufSize = Ops::Base::CeilAlign(ubFactor * sizeofT, ALIGNMENT_32); | 282 | int64_t outputBufSize = Ops::Base::CeilAlign(ubFactor * sizeofT, ALIGNMENT_32); |
| 278 | int64_t maskBitBufSize = Ops::Base::CeilAlign(ubFactor / NUM_8, ALIGNMENT_32); | 283 | int64_t maskBitBufSize = Ops::Base::CeilAlign(ubFactor / NUM_8, ALIGNMENT_32); |
| 279 | - int64_t randomFloatBufSize = Ops::Base::CeilAlign(ubFactor * static_cast<int64_t>(sizeof(float)), ALIGNMENT_32); | 284 | + int64_t randomFloatBufSize = Ops::Base::CeilAlign(ubFactor * static_cast<int64_t>(sizeof(float)) * randBufNum, |
| 285 | + ALIGNMENT_32); | ||
| 280 | pipe_->InitBuffer(inputQue_, NUM_2, inputBufSize); | 286 | pipe_->InitBuffer(inputQue_, NUM_2, inputBufSize); |
| 281 | pipe_->InitBuffer(outputQue_, NUM_2, outputBufSize); | 287 | pipe_->InitBuffer(outputQue_, NUM_2, outputBufSize); |
| 282 | pipe_->InitBuffer(maskBitQue_, NUM_2, maskBitBufSize); | 288 | pipe_->InitBuffer(maskBitQue_, NUM_2, maskBitBufSize); |
| @@ -316,7 +322,7 @@ __aicore__ inline void DropOutV3SimdImpl<T, U>::ComputeContinuous(int64_t baseLi | |||
| 316 | LocalTensor<uint8_t> maskBitUb = maskBitQue_.template AllocTensor<uint8_t>(); | 322 | LocalTensor<uint8_t> maskBitUb = maskBitQue_.template AllocTensor<uint8_t>(); |
| 317 | LocalTensor<float> randomFloatUb = randomFloatBuf_.Get<float>(); | 323 | LocalTensor<float> randomFloatUb = randomFloatBuf_.Get<float>(); |
| 318 | 324 | ||
| 319 | - if (tilingData->outputSize >= 524288) { | 325 | + if (tilingData->outputSize > UINT32_MAX_VALUE) { |
| 320 | __ubuf__ volatile float* randomFloatPtr = (__ubuf__ volatile float*)randomFloatUb.GetPhyAddr(); | 326 | __ubuf__ volatile float* randomFloatPtr = (__ubuf__ volatile float*)randomFloatUb.GetPhyAddr(); |
| 321 | switch (vec_) { | 327 | switch (vec_) { |
| 322 | case VEC_16: | 328 | case VEC_16: |
| @@ -387,6 +393,7 @@ __aicore__ inline void DropOutV3SimdImpl<T, U>::ComputeContinuousSimd(int64_t ba | |||
| 387 | constexpr uint32_t randNum = NUM_4 - (VEC % NUM_4); | 393 | constexpr uint32_t randNum = NUM_4 - (VEC % NUM_4); |
| 388 | constexpr uint32_t counterPerBatch = static_cast<uint32_t>(PhiloxInternal::ELE_CNT_B32_ONCE); | 394 | constexpr uint32_t counterPerBatch = static_cast<uint32_t>(PhiloxInternal::ELE_CNT_B32_ONCE); |
| 389 | constexpr uint32_t elemPerBatch = counterPerBatch * randNum; | 395 | constexpr uint32_t elemPerBatch = counterPerBatch * randNum; |
| 396 | + constexpr uint32_t ctrStride = counterPerBatch * NUM_4; | ||
| 390 | 397 | ||
| 391 | uint32_t key[ALG_KEY_SIZE] = {0, 0}; | 398 | uint32_t key[ALG_KEY_SIZE] = {0, 0}; |
| 392 | uint32_t counter[ALG_COUNTER_SIZE] = {0, 0, 0, 0}; | 399 | uint32_t counter[ALG_COUNTER_SIZE] = {0, 0, 0, 0}; |
| @@ -399,10 +406,19 @@ __aicore__ inline void DropOutV3SimdImpl<T, U>::ComputeContinuousSimd(int64_t ba | |||
| 399 | float scale = 1.0f / prob_; | 406 | float scale = 1.0f / prob_; |
| 400 | 407 | ||
| 401 | uint32_t totalBatches = Ops::Base::CeilDiv(static_cast<uint32_t>(currElements), elemPerBatch); | 408 | uint32_t totalBatches = Ops::Base::CeilDiv(static_cast<uint32_t>(currElements), elemPerBatch); |
| 409 | + uint32_t fullBatches = (totalBatches > 0) ? (totalBatches - 1) : 0; | ||
| 402 | 410 | ||
| 403 | __ubuf__ T* inputPtr = (__ubuf__ T*)inputUb.GetPhyAddr(); | 411 | __ubuf__ T* inputPtr = (__ubuf__ T*)inputUb.GetPhyAddr(); |
| 404 | __ubuf__ T* outputPtr = (__ubuf__ T*)outputUb.GetPhyAddr(); | 412 | __ubuf__ T* outputPtr = (__ubuf__ T*)outputUb.GetPhyAddr(); |
| 405 | __ubuf__ float* randomFloatPtr = (__ubuf__ float*)randomFloatUb.GetPhyAddr(); | 413 | __ubuf__ float* randomFloatPtr = (__ubuf__ float*)randomFloatUb.GetPhyAddr(); |
| 414 | + __ubuf__ uint32_t* ctrBufPtr = (__ubuf__ uint32_t*)randomFloatPtr; | ||
| 415 | + | ||
| 416 | + uint32_t key0Phase2 = key[0]; | ||
| 417 | + uint32_t key1Phase2 = key[1]; | ||
| 418 | + uint32_t key0Phase3 = key[0] + NUM_4 * PhiloxInternal::CONST_KEY_ADD_0; | ||
| 419 | + uint32_t key1Phase3 = key[1] + NUM_4 * PhiloxInternal::CONST_KEY_ADD_1; | ||
| 420 | + uint32_t key0Phase4 = key[0] + NUM_8 * PhiloxInternal::CONST_KEY_ADD_0; | ||
| 421 | + uint32_t key1Phase4 = key[1] + NUM_8 * PhiloxInternal::CONST_KEY_ADD_1; | ||
| 406 | 422 | ||
| 407 | __VEC_SCOPE__ | 423 | __VEC_SCOPE__ |
| 408 | { | 424 | { |
| @@ -415,22 +431,159 @@ __aicore__ inline void DropOutV3SimdImpl<T, U>::ComputeContinuousSimd(int64_t ba | |||
| 415 | Reg::RegTensor<float> zeroFloatReg; | 431 | Reg::RegTensor<float> zeroFloatReg; |
| 416 | Reg::Duplicate(zeroFloatReg, 0.0f); | 432 | Reg::Duplicate(zeroFloatReg, 0.0f); |
| 417 | 433 | ||
| 434 | + Reg::RegTensor<float> oneFloatReg; | ||
| 435 | + Reg::Duplicate(oneFloatReg, 1.0f); | ||
| 436 | + | ||
| 418 | Reg::RegTensor<uint32_t> mask16Reg; | 437 | Reg::RegTensor<uint32_t> mask16Reg; |
| 419 | Reg::Duplicate(mask16Reg, 0xFFFF); | 438 | Reg::Duplicate(mask16Reg, 0xFFFF); |
| 420 | 439 | ||
| 421 | - for (uint16_t batch = 0; batch < static_cast<uint16_t>(totalBatches); batch++) { | 440 | + for (uint16_t batch = 0; batch < static_cast<uint16_t>(fullBatches); batch++) { |
| 422 | - // 映射counter | ||
| 423 | uint32_t batchStart = batch * elemPerBatch; | 441 | uint32_t batchStart = batch * elemPerBatch; |
| 424 | int64_t baseIndex = baseLinearIndex + static_cast<int64_t>(batchStart); | 442 | int64_t baseIndex = baseLinearIndex + static_cast<int64_t>(batchStart); |
| 425 | 443 | ||
| 426 | - Reg::RegTensor<uint32_t> key0, key1; | ||
| 427 | - Reg::Duplicate(key0, key[0]); | ||
| 428 | - Reg::Duplicate(key1, key[1]); | ||
| 429 | Reg::RegTensor<uint32_t> ctr0, ctr1, ctr2, ctr3; | 444 | Reg::RegTensor<uint32_t> ctr0, ctr1, ctr2, ctr3; |
| 430 | VectorThreadMappingAndSkip<VEC>(baseIndex, randNum, magic32_, shift32_, totalThreads_, counter, ctr0, ctr1, | 445 | VectorThreadMappingAndSkip<VEC>(baseIndex, randNum, magic32_, shift32_, totalThreads_, counter, ctr0, ctr1, |
| 431 | ctr2, ctr3, pg); | 446 | ctr2, ctr3, pg); |
| 432 | 447 | ||
| 433 | - // 生成随机数 | 448 | + __ubuf__ uint32_t* storePtr = ctrBufPtr + batch * ctrStride; |
| 449 | + Reg::StoreAlign<uint32_t>(storePtr, ctr0, pg); | ||
| 450 | + Reg::StoreAlign<uint32_t>(storePtr + counterPerBatch, ctr1, pg); | ||
| 451 | + Reg::StoreAlign<uint32_t>(storePtr + NUM_2 * counterPerBatch, ctr2, pg); | ||
| 452 | + Reg::StoreAlign<uint32_t>(storePtr + NUM_3 * counterPerBatch, ctr3, pg); | ||
| 453 | + } | ||
| 454 | + | ||
| 455 | + Reg::LocalMemBar<Reg::MemType::VEC_STORE, Reg::MemType::VEC_LOAD>(); | ||
| 456 | + | ||
| 457 | + for (uint16_t batch = 0; batch < static_cast<uint16_t>(fullBatches); batch++) { | ||
| 458 | + __ubuf__ uint32_t* bufPtr = ctrBufPtr + batch * ctrStride; | ||
| 459 | + | ||
| 460 | + Reg::RegTensor<uint32_t> ctr0, ctr1, ctr2, ctr3; | ||
| 461 | + Reg::LoadAlign<uint32_t>(ctr0, bufPtr); | ||
| 462 | + Reg::LoadAlign<uint32_t>(ctr1, bufPtr + counterPerBatch); | ||
| 463 | + Reg::LoadAlign<uint32_t>(ctr2, bufPtr + NUM_2 * counterPerBatch); | ||
| 464 | + Reg::LoadAlign<uint32_t>(ctr3, bufPtr + NUM_3 * counterPerBatch); | ||
| 465 | + | ||
| 466 | + Reg::RegTensor<uint32_t> key0, key1; | ||
| 467 | + Reg::Duplicate(key0, key0Phase2); | ||
| 468 | + Reg::Duplicate(key1, key1Phase2); | ||
| 469 | + | ||
| 470 | + Reg::RegTensor<uint32_t> tmpL0, tmpH0, tmpL1, tmpH1; | ||
| 471 | + SpNetworkKernel<4>(tmpL0, tmpH0, tmpL1, tmpH1, ctr0, ctr1, ctr2, ctr3, key0, key1, cMul0, cMul1, pg); | ||
| 472 | + | ||
| 473 | + Reg::StoreAlign<uint32_t>(bufPtr, ctr0, pg); | ||
| 474 | + Reg::StoreAlign<uint32_t>(bufPtr + counterPerBatch, ctr1, pg); | ||
| 475 | + Reg::StoreAlign<uint32_t>(bufPtr + NUM_2 * counterPerBatch, ctr2, pg); | ||
| 476 | + Reg::StoreAlign<uint32_t>(bufPtr + NUM_3 * counterPerBatch, ctr3, pg); | ||
| 477 | + } | ||
| 478 | + | ||
| 479 | + Reg::LocalMemBar<Reg::MemType::VEC_STORE, Reg::MemType::VEC_LOAD>(); | ||
| 480 | + | ||
| 481 | + for (uint16_t batch = 0; batch < static_cast<uint16_t>(fullBatches); batch++) { | ||
| 482 | + __ubuf__ uint32_t* bufPtr = ctrBufPtr + batch * ctrStride; | ||
| 483 | + | ||
| 484 | + Reg::RegTensor<uint32_t> ctr0, ctr1, ctr2, ctr3; | ||
| 485 | + Reg::LoadAlign<uint32_t>(ctr0, bufPtr); | ||
| 486 | + Reg::LoadAlign<uint32_t>(ctr1, bufPtr + counterPerBatch); | ||
| 487 | + Reg::LoadAlign<uint32_t>(ctr2, bufPtr + NUM_2 * counterPerBatch); | ||
| 488 | + Reg::LoadAlign<uint32_t>(ctr3, bufPtr + NUM_3 * counterPerBatch); | ||
| 489 | + | ||
| 490 | + Reg::RegTensor<uint32_t> key0, key1; | ||
| 491 | + Reg::Duplicate(key0, key0Phase3); | ||
| 492 | + Reg::Duplicate(key1, key1Phase3); | ||
| 493 | + | ||
| 494 | + Reg::RegTensor<uint32_t> tmpL0, tmpH0, tmpL1, tmpH1; | ||
| 495 | + SpNetworkKernel<4>(tmpL0, tmpH0, tmpL1, tmpH1, ctr0, ctr1, ctr2, ctr3, key0, key1, cMul0, cMul1, pg); | ||
| 496 | + | ||
| 497 | + Reg::StoreAlign<uint32_t>(bufPtr, ctr0, pg); | ||
| 498 | + Reg::StoreAlign<uint32_t>(bufPtr + counterPerBatch, ctr1, pg); | ||
| 499 | + Reg::StoreAlign<uint32_t>(bufPtr + NUM_2 * counterPerBatch, ctr2, pg); | ||
| 500 | + Reg::StoreAlign<uint32_t>(bufPtr + NUM_3 * counterPerBatch, ctr3, pg); | ||
| 501 | + } | ||
| 502 | + | ||
| 503 | + Reg::LocalMemBar<Reg::MemType::VEC_STORE, Reg::MemType::VEC_LOAD>(); | ||
| 504 | + | ||
| 505 | + for (uint16_t batch = 0; batch < static_cast<uint16_t>(fullBatches); batch++) { | ||
| 506 | + __ubuf__ uint32_t* bufPtr = ctrBufPtr + batch * ctrStride; | ||
| 507 | + | ||
| 508 | + Reg::RegTensor<uint32_t> ctr0, ctr1, ctr2, ctr3; | ||
| 509 | + Reg::LoadAlign<uint32_t>(ctr0, bufPtr); | ||
| 510 | + Reg::LoadAlign<uint32_t>(ctr1, bufPtr + counterPerBatch); | ||
| 511 | + Reg::LoadAlign<uint32_t>(ctr2, bufPtr + NUM_2 * counterPerBatch); | ||
| 512 | + Reg::LoadAlign<uint32_t>(ctr3, bufPtr + NUM_3 * counterPerBatch); | ||
| 513 | + | ||
| 514 | + Reg::RegTensor<uint32_t> key0, key1; | ||
| 515 | + Reg::Duplicate(key0, key0Phase4); | ||
| 516 | + Reg::Duplicate(key1, key1Phase4); | ||
| 517 | + | ||
| 518 | + Reg::RegTensor<uint32_t> tmpL0, tmpH0, tmpL1, tmpH1; | ||
| 519 | + SpNetworkKernel<2>(tmpL0, tmpH0, tmpL1, tmpH1, ctr0, ctr1, ctr2, ctr3, key0, key1, cMul0, cMul1, pg); | ||
| 520 | + | ||
| 521 | + if constexpr (randNum == NUM_4) { | ||
| 522 | + Interleave(ctr0, ctr2, ctr0, ctr2); | ||
| 523 | + Interleave(ctr1, ctr3, ctr1, ctr3); | ||
| 524 | + Interleave(ctr0, ctr1, ctr0, ctr1); | ||
| 525 | + Interleave(ctr2, ctr3, ctr2, ctr3); | ||
| 526 | + } else { | ||
| 527 | + Interleave(ctr0, ctr1, ctr0, ctr1); | ||
| 528 | + } | ||
| 529 | + | ||
| 530 | + Reg::StoreAlign<uint32_t>(bufPtr, ctr0, pg); | ||
| 531 | + Reg::StoreAlign<uint32_t>(bufPtr + counterPerBatch, ctr1, pg); | ||
| 532 | + Reg::StoreAlign<uint32_t>(bufPtr + NUM_2 * counterPerBatch, ctr2, pg); | ||
| 533 | + Reg::StoreAlign<uint32_t>(bufPtr + NUM_3 * counterPerBatch, ctr3, pg); | ||
| 534 | + } | ||
| 535 | + | ||
| 536 | + Reg::LocalMemBar<Reg::MemType::VEC_STORE, Reg::MemType::VEC_LOAD>(); | ||
| 537 | + | ||
| 538 | + for (uint16_t batch = 0; batch < static_cast<uint16_t>(fullBatches); batch++) { | ||
| 539 | + uint32_t batchStart = batch * elemPerBatch; | ||
| 540 | + __ubuf__ uint32_t* bufPtr = ctrBufPtr + batch * ctrStride; | ||
| 541 | + | ||
| 542 | + Reg::RegTensor<uint32_t> ctr0, ctr1, ctr2, ctr3; | ||
| 543 | + Reg::LoadAlign<uint32_t>(ctr0, bufPtr); | ||
| 544 | + Reg::LoadAlign<uint32_t>(ctr1, bufPtr + counterPerBatch); | ||
| 545 | + Reg::LoadAlign<uint32_t>(ctr2, bufPtr + NUM_2 * counterPerBatch); | ||
| 546 | + Reg::LoadAlign<uint32_t>(ctr3, bufPtr + NUM_3 * counterPerBatch); | ||
| 547 | + | ||
| 548 | + __ubuf__ T* batchInputPtr = inputPtr + batchStart; | ||
| 549 | + __ubuf__ T* batchOutputPtr = outputPtr + batchStart; | ||
| 550 | + __ubuf__ float* batchRandomPtr = randomFloatPtr + batchStart; | ||
| 551 | + | ||
| 552 | + PhiloxCtrConvertAndDropout<T>(ctr0, mask16Reg, zeroFloatReg, oneFloatReg, counterPerBatch, scale, prob_, | ||
| 553 | + batchRandomPtr, batchInputPtr, batchOutputPtr); | ||
| 554 | + PhiloxCtrConvertAndDropout<T>(ctr1, mask16Reg, zeroFloatReg, oneFloatReg, counterPerBatch, scale, prob_, | ||
| 555 | + batchRandomPtr + counterPerBatch, batchInputPtr + counterPerBatch, | ||
| 556 | + batchOutputPtr + counterPerBatch); | ||
| 557 | + | ||
| 558 | + if constexpr (randNum == NUM_4) { | ||
| 559 | + PhiloxCtrConvertAndDropout<T>(ctr2, mask16Reg, zeroFloatReg, oneFloatReg, counterPerBatch, scale, prob_, | ||
| 560 | + batchRandomPtr + NUM_2 * counterPerBatch, | ||
| 561 | + batchInputPtr + NUM_2 * counterPerBatch, | ||
| 562 | + batchOutputPtr + NUM_2 * counterPerBatch); | ||
| 563 | + PhiloxCtrConvertAndDropout<T>(ctr3, mask16Reg, zeroFloatReg, oneFloatReg, counterPerBatch, scale, prob_, | ||
| 564 | + batchRandomPtr + NUM_3 * counterPerBatch, | ||
| 565 | + batchInputPtr + NUM_3 * counterPerBatch, | ||
| 566 | + batchOutputPtr + NUM_3 * counterPerBatch); | ||
| 567 | + } | ||
| 568 | + } | ||
| 569 | + | ||
| 570 | + if (totalBatches > 0) { | ||
| 571 | + uint32_t batchStart = fullBatches * elemPerBatch; | ||
| 572 | + uint32_t tailElements = static_cast<uint32_t>(currElements) - batchStart; | ||
| 573 | + int64_t baseIndex = baseLinearIndex + static_cast<int64_t>(batchStart); | ||
| 574 | + | ||
| 575 | + __ubuf__ T* tailInputPtr = inputPtr + batchStart; | ||
| 576 | + __ubuf__ T* tailOutputPtr = outputPtr + batchStart; | ||
| 577 | + __ubuf__ float* tailRandomPtr = randomFloatPtr + batchStart; | ||
| 578 | + | ||
| 579 | + Reg::RegTensor<uint32_t> key0, key1; | ||
| 580 | + Reg::Duplicate(key0, key[0]); | ||
| 581 | + Reg::Duplicate(key1, key[1]); | ||
| 582 | + | ||
| 583 | + Reg::RegTensor<uint32_t> ctr0, ctr1, ctr2, ctr3; | ||
| 584 | + VectorThreadMappingAndSkip<VEC>(baseIndex, randNum, magic32_, shift32_, totalThreads_, counter, ctr0, ctr1, | ||
| 585 | + ctr2, ctr3, pg); | ||
| 586 | + | ||
| 434 | Reg::RegTensor<uint32_t> tmpL0, tmpH0, tmpL1, tmpH1; | 587 | Reg::RegTensor<uint32_t> tmpL0, tmpH0, tmpL1, tmpH1; |
| 435 | SpNetworkKernel<10>(tmpL0, tmpH0, tmpL1, tmpH1, ctr0, ctr1, ctr2, ctr3, key0, key1, cMul0, cMul1, pg); | 588 | SpNetworkKernel<10>(tmpL0, tmpH0, tmpL1, tmpH1, ctr0, ctr1, ctr2, ctr3, key0, key1, cMul0, cMul1, pg); |
| 436 | 589 | ||
| @@ -443,48 +596,36 @@ __aicore__ inline void DropOutV3SimdImpl<T, U>::ComputeContinuousSimd(int64_t ba | |||
| 443 | Interleave(ctr0, ctr1, ctr0, ctr1); | 596 | Interleave(ctr0, ctr1, ctr0, ctr1); |
| 444 | } | 597 | } |
| 445 | 598 | ||
| 446 | - // drop out | 599 | + uint32_t remaining = tailElements; |
| 447 | - __ubuf__ T* batchInputPtr = inputPtr + batchStart; | ||
| 448 | - __ubuf__ T* batchOutputPtr = outputPtr + batchStart; | ||
| 449 | - __ubuf__ float* batchRandomPtr = randomFloatPtr + batchStart; | ||
| 450 | - | ||
| 451 | - __ubuf__ T* iPtr0 = batchInputPtr; | ||
| 452 | - __ubuf__ T* iPtr1 = batchInputPtr + counterPerBatch; | ||
| 453 | - __ubuf__ T* oPtr0 = batchOutputPtr; | ||
| 454 | - __ubuf__ T* oPtr1 = batchOutputPtr + counterPerBatch; | ||
| 455 | - __ubuf__ float* rPtr0 = batchRandomPtr; | ||
| 456 | - __ubuf__ float* rPtr1 = batchRandomPtr + counterPerBatch; | ||
| 457 | - | ||
| 458 | - uint32_t remaining = (batchStart + elemPerBatch <= static_cast<uint32_t>(currElements)) ? | ||
| 459 | - elemPerBatch : | ||
| 460 | - static_cast<uint32_t>(currElements) - batchStart; | ||
| 461 | 600 | ||
| 462 | uint32_t currCount0 = (remaining < counterPerBatch) ? remaining : counterPerBatch; | 601 | uint32_t currCount0 = (remaining < counterPerBatch) ? remaining : counterPerBatch; |
| 602 | + PhiloxCtrConvertAndDropout<T>(ctr0, mask16Reg, zeroFloatReg, oneFloatReg, currCount0, scale, prob_, | ||
| 603 | + tailRandomPtr, tailInputPtr, tailOutputPtr); | ||
| 463 | remaining -= currCount0; | 604 | remaining -= currCount0; |
| 605 | + | ||
| 464 | uint32_t currCount1 = (remaining < counterPerBatch) ? remaining : counterPerBatch; | 606 | uint32_t currCount1 = (remaining < counterPerBatch) ? remaining : counterPerBatch; |
| 607 | + PhiloxCtrConvertAndDropout<T>(ctr1, mask16Reg, zeroFloatReg, oneFloatReg, currCount1, scale, prob_, | ||
| 608 | + tailRandomPtr + counterPerBatch, tailInputPtr + counterPerBatch, | ||
| 609 | + tailOutputPtr + counterPerBatch); | ||
| 465 | remaining -= currCount1; | 610 | remaining -= currCount1; |
| 466 | 611 | ||
| 467 | - PhiloxCtrConvertAndDropout<T>(ctr0, mask16Reg, zeroFloatReg, currCount0, scale, prob_, rPtr0, iPtr0, oPtr0); | ||
| 468 | - PhiloxCtrConvertAndDropout<T>(ctr1, mask16Reg, zeroFloatReg, currCount1, scale, prob_, rPtr1, iPtr1, oPtr1); | ||
| 469 | - | ||
| 470 | if constexpr (randNum == NUM_4) { | 612 | if constexpr (randNum == NUM_4) { |
| 471 | - __ubuf__ T* iPtr2 = batchInputPtr + NUM_2 * counterPerBatch; | ||
| 472 | - __ubuf__ T* iPtr3 = batchInputPtr + NUM_3 * counterPerBatch; | ||
| 473 | - __ubuf__ T* oPtr2 = batchOutputPtr + NUM_2 * counterPerBatch; | ||
| 474 | - __ubuf__ T* oPtr3 = batchOutputPtr + NUM_3 * counterPerBatch; | ||
| 475 | - __ubuf__ float* rPtr2 = batchRandomPtr + NUM_2 * counterPerBatch; | ||
| 476 | - __ubuf__ float* rPtr3 = batchRandomPtr + NUM_3 * counterPerBatch; | ||
| 477 | - | ||
| 478 | uint32_t currCount2 = (remaining < counterPerBatch) ? remaining : counterPerBatch; | 613 | uint32_t currCount2 = (remaining < counterPerBatch) ? remaining : counterPerBatch; |
| 614 | + PhiloxCtrConvertAndDropout<T>(ctr2, mask16Reg, zeroFloatReg, oneFloatReg, currCount2, scale, prob_, | ||
| 615 | + tailRandomPtr + NUM_2 * counterPerBatch, | ||
| 616 | + tailInputPtr + NUM_2 * counterPerBatch, | ||
| 617 | + tailOutputPtr + NUM_2 * counterPerBatch); | ||
| 479 | remaining -= currCount2; | 618 | remaining -= currCount2; |
| 480 | - uint32_t currCount3 = (remaining < counterPerBatch) ? remaining : counterPerBatch; | ||
| 481 | 619 | ||
| 482 | - PhiloxCtrConvertAndDropout<T>(ctr2, mask16Reg, zeroFloatReg, currCount2, scale, prob_, rPtr2, iPtr2, | 620 | + uint32_t currCount3 = (remaining < counterPerBatch) ? remaining : counterPerBatch; |
| 483 | - oPtr2); | 621 | + PhiloxCtrConvertAndDropout<T>(ctr3, mask16Reg, zeroFloatReg, oneFloatReg, currCount3, scale, prob_, |
| 484 | - PhiloxCtrConvertAndDropout<T>(ctr3, mask16Reg, zeroFloatReg, currCount3, scale, prob_, rPtr3, iPtr3, | 622 | + tailRandomPtr + NUM_3 * counterPerBatch, |
| 485 | - oPtr3); | 623 | + tailInputPtr + NUM_3 * counterPerBatch, |
| 624 | + tailOutputPtr + NUM_3 * counterPerBatch); | ||
| 486 | } | 625 | } |
| 487 | } | 626 | } |
| 627 | + | ||
| 628 | + Reg::LocalMemBar<Reg::MemType::VEC_STORE, Reg::MemType::VEC_LOAD>(); | ||
| 488 | } | 629 | } |
| 489 | 630 | ||
| 490 | CompareScalar<float, uint8_t>(maskBitUb, randomFloatUb, prob_, CMPMODE::LT, currElements); | 631 | CompareScalar<float, uint8_t>(maskBitUb, randomFloatUb, prob_, CMPMODE::LT, currElements); |
| @@ -565,28 +706,22 @@ __aicore__ inline void DropOutV3SimdImpl<T, U>::Process(const DropOutV3TilingDat | |||
| 565 | return; | 706 | return; |
| 566 | } | 707 | } |
| 567 | 708 | ||
| 568 | - if (blockIdx_ == 0) { | 709 | + if (IsProbEqual(prob_, 0.0f)) { |
| 710 | + ProcessProbZero(tilingData); | ||
| 711 | + } else { | ||
| 712 | + ProcessContinuous(tilingData); | ||
| 713 | + } | ||
| 714 | + | ||
| 715 | + if (blockIdx_ == tilingData->usedCoreNum - 1) { | ||
| 569 | constexpr int64_t BIT_NUMBER = 128; | 716 | constexpr int64_t BIT_NUMBER = 128; |
| 570 | constexpr int64_t UINT8_BIT_NUMBER = 8; | 717 | constexpr int64_t UINT8_BIT_NUMBER = 8; |
| 571 | int64_t maskWrittenBytes = Ops::Base::CeilDiv(tilingData->outputSize, UINT8_BIT_NUMBER); | 718 | int64_t maskWrittenBytes = Ops::Base::CeilDiv(tilingData->outputSize, UINT8_BIT_NUMBER); |
| 572 | int64_t maskTotalBytes = Ops::Base::CeilAlign(tilingData->outputSize, BIT_NUMBER) / UINT8_BIT_NUMBER; | 719 | int64_t maskTotalBytes = Ops::Base::CeilAlign(tilingData->outputSize, BIT_NUMBER) / UINT8_BIT_NUMBER; |
| 573 | - int64_t tailOffset = Ops::Base::FloorAlign(maskWrittenBytes, (int64_t)NUM_2); | 720 | + __gm__ uint8_t* maskU8 = (__gm__ uint8_t*)maskGm_.GetPhyAddr(); |
| 574 | - int64_t tailBytes = maskTotalBytes - tailOffset; | 721 | + for (int64_t i = maskWrittenBytes; i < maskTotalBytes; i++) { |
| 575 | - if (tailBytes > 0) { | 722 | + WriteGmByPassDCache<uint8_t>(maskU8 + i, 0); |
| 576 | - GlobalTensor<uint16_t> maskGmU16; | ||
| 577 | - maskGmU16.SetGlobalBuffer((__gm__ uint16_t*)maskGm_.GetPhyAddr()); | ||
| 578 | - GlobalTensor<uint16_t> maskGmTail = maskGmU16[tailOffset / NUM_2]; | ||
| 579 | - Fill<uint16_t>(maskGmTail, tailBytes / NUM_2, 0); | ||
| 580 | } | 723 | } |
| 581 | } | 724 | } |
| 582 | - SyncAll(); | ||
| 583 | - | ||
| 584 | - if (IsProbEqual(prob_, 0.0f)) { | ||
| 585 | - ProcessProbZero(tilingData); | ||
| 586 | - return; | ||
| 587 | - } | ||
| 588 | - | ||
| 589 | - ProcessContinuous(tilingData); | ||
| 590 | } | 725 | } |
| 591 | } // namespace DropOutV3 | 726 | } // namespace DropOutV3 |
| 592 | #endif // DROP_OUT_V3_SIMD_IMPL_H | 727 | #endif // DROP_OUT_V3_SIMD_IMPL_H |
| @@ -23,7 +23,6 @@ public: | |||
| 23 | float prob = 0; | 23 | float prob = 0; |
| 24 | 24 | ||
| 25 | uint32_t vec = 0; | 25 | uint32_t vec = 0; |
| 26 | - uint32_t transportMode = 0; | ||
| 27 | int64_t totalThreads = 0; | 26 | int64_t totalThreads = 0; |
| 28 | int64_t perCoreElements = 0; | 27 | int64_t perCoreElements = 0; |
| 29 | int64_t tailCoreElements = 0; | 28 | int64_t tailCoreElements = 0; |
| @@ -39,10 +38,10 @@ public: | |||
| 39 | info << "[DropOutV3TilingData] " | 38 | info << "[DropOutV3TilingData] " |
| 40 | << "usedCoreNum: " << usedCoreNum << ", outputSize: " << outputSize << ", seed: " << seed | 39 | << "usedCoreNum: " << usedCoreNum << ", outputSize: " << outputSize << ", seed: " << seed |
| 41 | << ", offset: " << offset << ", ubSize: " << ubSize << ", prob: " << prob << ", vec: " << vec | 40 | << ", offset: " << offset << ", ubSize: " << ubSize << ", prob: " << prob << ", vec: " << vec |
| 42 | - << ", transportMode: " << transportMode << ", totalThreads: " << totalThreads | 41 | + << ", totalThreads: " << totalThreads << ", perCoreElements: " << perCoreElements |
| 43 | - << ", perCoreElements: " << perCoreElements << ", tailCoreElements: " << tailCoreElements | 42 | + << ", tailCoreElements: " << tailCoreElements << ", ubLoopCount: " << ubLoopCount |
| 44 | - << ", ubLoopCount: " << ubLoopCount << ", tailUbLoopCount: " << tailUbLoopCount | 43 | + << ", tailUbLoopCount: " << tailUbLoopCount << ", ubFactorElements: " << ubFactorElements |
| 45 | - << ", ubFactorElements: " << ubFactorElements << ", tailUbFactorElements: " << tailUbFactorElements | 44 | + << ", tailUbFactorElements: " << tailUbFactorElements |
| 46 | << ", tailCoreTailUbFactorElements: " << tailCoreTailUbFactorElements; | 45 | << ", tailCoreTailUbFactorElements: " << tailCoreTailUbFactorElements; |
| 47 | return info.str(); | 46 | return info.str(); |
| 48 | } | 47 | } |
| @@ -29,14 +29,14 @@ __global__ __aicore__ void drop_out_v3(GM_ADDR x, GM_ADDR noiseShape, GM_ADDR p, | |||
| 29 | TPipe pipe; | 29 | TPipe pipe; |
| 30 | 30 | ||
| 31 | if (TILING_KEY_IS(DROP_OUT_V3_DEFAULT_TILING_KEY)) { | 31 | if (TILING_KEY_IS(DROP_OUT_V3_DEFAULT_TILING_KEY)) { |
| 32 | - if (tilingData.transportMode == 0) { | 32 | + if (tilingData.vec == 1) { |
| 33 | - DropOutV3::DropOutV3SimdImpl<DTYPE_X, DTYPE_P> op; | ||
| 34 | - op.Init(x, y, mask, &tilingData, &pipe); | ||
| 35 | - op.Process(&tilingData); | ||
| 36 | - } else { | ||
| 37 | DropOutV3::DropOutV3Impl<DTYPE_X, DTYPE_P> op; | 33 | DropOutV3::DropOutV3Impl<DTYPE_X, DTYPE_P> op; |
| 38 | op.Init(p, mask, workspace, &tilingData, &pipe); | 34 | op.Init(p, mask, workspace, &tilingData, &pipe); |
| 39 | op.Process(x, y, mask, &tilingData); | 35 | op.Process(x, y, mask, &tilingData); |
| 36 | + } else { | ||
| 37 | + DropOutV3::DropOutV3SimdImpl<DTYPE_X, DTYPE_P> op; | ||
| 38 | + op.Init(x, y, mask, &tilingData, &pipe); | ||
| 39 | + op.Process(&tilingData); | ||
| 40 | } | 40 | } |
| 41 | } | 41 | } |
| 42 | } | 42 | } |
| @@ -43,7 +43,7 @@ TEST_F(DropOutV3TilingTest, drop_out_v3_tiling_ascendc_float_01) | |||
| 43 | {{{15376}, {15376}}, ge::DT_UINT8, ge::FORMAT_ND}}, | 43 | {{{15376}, {15376}}, ge::DT_UINT8, ge::FORMAT_ND}}, |
| 44 | &compileInfo); | 44 | &compileInfo); |
| 45 | uint64_t expectTilingKey = 100; | 45 | uint64_t expectTilingKey = 100; |
| 46 | - string expectTilingData = "64 122980 8 4 229376 18236833792 0 123136 1952 4 1 1 11296 1952 4 "; | 46 | + string expectTilingData = "64 122980 8 4 229376 18236833792 159744 1952 4 1 1 11296 1952 4 "; |
| 47 | std::vector<size_t> expectWorkspaces = {16900224}; | 47 | std::vector<size_t> expectWorkspaces = {16900224}; |
| 48 | ExecuteTestCase(tilingContextPara, ge::GRAPH_SUCCESS, expectTilingKey, expectTilingData, expectWorkspaces); | 48 | ExecuteTestCase(tilingContextPara, ge::GRAPH_SUCCESS, expectTilingKey, expectTilingData, expectWorkspaces); |
| 49 | } | 49 | } |