Merged
reduceprod性能优化 #690
reduceprod性能优化 #690
Merged
sunzhongwen1Add on Jul 31
Total: 1 files changed+54-63
@@ -112,6 +112,18 @@ __aicore__ inline uint16_t FindClosestPowerOfTwo(uint32_t dimR)
112 return r;112 return r;
113}113}
114 114 
115+__aicore__ inline uint16_t LogBase2(uint16_t n)
116+{
117+ if (n <= 0) {
118+ return 0;
119+ }
120+ uint16_t result = 0;
121+ while (n >>= 1) {
122+ result++;
123+ }
124+ return result;
125+}
126+ 
115#if defined(__NPU_ARCH__) && \127#if defined(__NPU_ARCH__) && \
116 ((__NPU_ARCH__ == 3510) || (__NPU_ARCH__ == 5102) || (__NPU_ARCH__ == 3003) || (__NPU_ARCH__ == 3113)) || \128 ((__NPU_ARCH__ == 3510) || (__NPU_ARCH__ == 5102) || (__NPU_ARCH__ == 3003) || (__NPU_ARCH__ == 3113)) || \
117 defined(__ASC_NPU_HOST__)129 defined(__ASC_NPU_HOST__)
@@ -770,13 +782,12 @@ public:
770 782 
771 template <typename T, const AscendC::MicroAPI::RegTrait& Trait>783 template <typename T, const AscendC::MicroAPI::RegTrait& Trait>
772 __aicore__ inline void ReduceARFoldBase(784 __aicore__ inline void ReduceARFoldBase(
773- __ubuf__ T* dstAddr, __ubuf__ T* srcAddr, const int32_t dimR, uint32_t maskElems, uint16_t blkElems,785+ __ubuf__ T* srcAddr, const int32_t dimR, const uint16_t vLElems, const uint16_t loopANum, uint16_t fold1,
774- const uint16_t vLElems, const uint16_t loopANum, uint16_t fold1, uint16_t fold2, uint16_t fold3,786+ uint16_t fold2, uint16_t fold3, AscendC::MicroAPI::MaskReg& mask, AscendC::MicroAPI::RegTensor<T, Trait>& vreg0,
775- uint16_t isBlkRepeat, uint16_t blkRepeats, AscendC::MicroAPI::MaskReg& mask,787+ AscendC::MicroAPI::RegTensor<T, Trait>& vreg1, AscendC::MicroAPI::RegTensor<T, Trait>& vreg2,
776- AscendC::MicroAPI::RegTensor<T, Trait>& vreg0, AscendC::MicroAPI::RegTensor<T, Trait>& vreg1,788+ AscendC::MicroAPI::RegTensor<T, Trait>& vreg3, AscendC::MicroAPI::RegTensor<T, Trait>& vreg4,
777- AscendC::MicroAPI::RegTensor<T, Trait>& vreg2, AscendC::MicroAPI::RegTensor<T, Trait>& vreg3,789+ AscendC::MicroAPI::RegTensor<T, Trait>& vreg5, AscendC::MicroAPI::RegTensor<T, Trait>& vreg6,
778- AscendC::MicroAPI::RegTensor<T, Trait>& vreg4, AscendC::MicroAPI::RegTensor<T, Trait>& vreg5,790+ AscendC::MicroAPI::RegTensor<T, Trait>& vreg7)
779- AscendC::MicroAPI::RegTensor<T, Trait>& vreg6, AscendC::MicroAPI::RegTensor<T, Trait>& vreg7)
780 {791 {
781 for (uint16_t i = 0; i < fold1; i++) {792 for (uint16_t i = 0; i < fold1; i++) {
782 mask = AscendC::MicroAPI::CreateMask<T, AscendC::MicroAPI::MaskPattern::ALL, Trait>();793 mask = AscendC::MicroAPI::CreateMask<T, AscendC::MicroAPI::MaskPattern::ALL, Trait>();
@@ -828,30 +839,14 @@ public:
828 AscendC::MicroAPI::DataCopy(srcAddr + loopA * dimR, vreg0, mask);839 AscendC::MicroAPI::DataCopy(srcAddr + loopA * dimR, vreg0, mask);
829 }840 }
830 }841 }
831- AscendC::MicroAPI::LocalMemBar<AscendC::MicroAPI::MemType::VEC_STORE, AscendC::MicroAPI::MemType::VEC_LOAD>();
832- 
833- mask = AscendC::MicroAPI::UpdateMask<PromteT, Trait>(maskElems);
834- for (uint16_t i = 0; i < isBlkRepeat; i++) {
835- for (uint16_t loopA = 0; loopA < loopANum; loopA++) {
836- AscendC::MicroAPI::DataCopy<T, AscendC::MicroAPI::LoadDist::DIST_BLK>(vreg0, srcAddr + loopA * dimR);
837- for (uint16_t loopR = 0; loopR < blkRepeats; loopR++) {
838- AscendC::MicroAPI::DataCopy<T, AscendC::MicroAPI::LoadDist::DIST_BLK>(
839- vreg1, srcAddr + loopA * dimR + blkElems + blkElems * loopR);
840- AscendC::MicroAPI::Mul(vreg0, vreg0, vreg1, mask);
841- }
842- AscendC::MicroAPI::DataCopy(srcAddr + loopA * dimR, vreg0, mask);
843- }
844- AscendC::MicroAPI::LocalMemBar<AscendC::MicroAPI::MemType::VEC_STORE,
845- AscendC::MicroAPI::MemType::VEC_LOAD>();
846- }
847 }842 }
848 843 
849 template <typename DIndex, const AscendC::MicroAPI::RegTrait& Trait>844 template <typename DIndex, const AscendC::MicroAPI::RegTrait& Trait>
850 __aicore__ inline void ReduceAR(__ubuf__ PromteT* dstAddr, __ubuf__ PromteT* srcAddr, int32_t dimA, int32_t dimR,845 __aicore__ inline void ReduceAR(__ubuf__ PromteT* dstAddr, __ubuf__ PromteT* srcAddr, int32_t dimA, int32_t dimR,
851 int32_t mainR, int32_t outerR, int32_t innerR)846 int32_t mainR, int32_t outerR, int32_t innerR)
852 {847 {
853- constexpr uint16_t vLElems = IsB64<PromteT>() ? VL_LEN / sizeof(float) : VL_LEN / sizeof(PromteT);848+ constexpr uint16_t vLElems = VL_LEN / sizeof(PromteT);
854- constexpr uint16_t blkElems = IsB64<PromteT>() ? BLOCK_SIZE / sizeof(float) : BLOCK_SIZE / sizeof(PromteT);849+ constexpr uint16_t blkElems = BLOCK_SIZE / sizeof(PromteT);
855 if (outerR == 1 && innerR <= static_cast<int32_t>(blkElems * CONST2) &&850 if (outerR == 1 && innerR <= static_cast<int32_t>(blkElems * CONST2) &&
856 dimA >= static_cast<int32_t>(vLElems / CONST2)) {851 dimA >= static_cast<int32_t>(vLElems / CONST2)) {
857 if constexpr (IsSameType<PromteT, int32_t>::value || IsSameType<PromteT, uint32_t>::value ||852 if constexpr (IsSameType<PromteT, int32_t>::value || IsSameType<PromteT, uint32_t>::value ||
@@ -876,11 +871,9 @@ public:
876 const uint16_t fold1 = tailFolds == FOLD1 ? 1 : 0;871 const uint16_t fold1 = tailFolds == FOLD1 ? 1 : 0;
877 const uint16_t fold2 = tailFolds == FOLD2 ? 1 : 0;872 const uint16_t fold2 = tailFolds == FOLD2 ? 1 : 0;
878 const uint16_t fold3 = tailFolds == FOLD3 ? 1 : 0;873 const uint16_t fold3 = tailFolds == FOLD3 ? 1 : 0;
879- const uint16_t rNum = mainR < vLElems ? mainR : vLElems;
880- const int16_t blkRepeats = rNum / blkElems - 1;
881- const uint16_t isBlkRepeat = blkRepeats > 0 ? 1 : 0;
882- const uint16_t reservedR = rNum < blkElems ? rNum : blkElems;
883 const uint16_t loopANum = dimA;874 const uint16_t loopANum = dimA;
875+ const uint16_t eleCount = mainR >= vLElems ? vLElems : mainR;
876+ const uint16_t repeatTime = LogBase2(eleCount);
884 877 
885 __VEC_SCOPE__878 __VEC_SCOPE__
886 {879 {
@@ -974,28 +967,26 @@ public:
974 AscendC::MicroAPI::MemType::VEC_LOAD>();967 AscendC::MicroAPI::MemType::VEC_LOAD>();
975 }968 }
976 969 
977- ReduceARFoldBase<PromteT, Trait>(dstAddr, srcAddr, dimR, blkElems, blkElems, vLElems, loopANum, fold1,970+ ReduceARFoldBase<PromteT, Trait>(srcAddr, dimR, vLElems, loopANum, fold1, fold2, fold3, mask, vreg0, vreg1,
978- fold2, fold3, isBlkRepeat, blkRepeats, mask, vreg0, vreg1, vreg2, vreg3,971+ vreg2, vreg3, vreg4, vreg5, vreg6, vreg7);
979- vreg4, vreg5, vreg6, vreg7);
980 972 
981 // Reduce to 1973 // Reduce to 1
982- uint32_t sreg1 = 1U;974+ AscendC::MicroAPI::LocalMemBar<AscendC::MicroAPI::MemType::VEC_STORE,
975+ AscendC::MicroAPI::MemType::VEC_LOAD>();
976+ uint32_t sreg1 = mainR;
983 mask = AscendC::MicroAPI::UpdateMask<PromteT, Trait>(sreg1);977 mask = AscendC::MicroAPI::UpdateMask<PromteT, Trait>(sreg1);
978+ AscendC::MicroAPI::RegTensor<PromteT, Trait> tempOne;
979+ AscendC::MicroAPI::Duplicate(tempOne, 1);
984 for (uint16_t loopA = 0; loopA < loopANum; loopA++) {980 for (uint16_t loopA = 0; loopA < loopANum; loopA++) {
985- AscendC::MicroAPI::Duplicate(vreg0, 1);981+ AscendC::MicroAPI::DataCopy(vreg0, srcAddr + loopA * dimR);
986- for (uint16_t loopR = 0; loopR < reservedR; loopR++) {982+ AscendC::MicroAPI::Select(vreg0, vreg0, tempOne, mask);
987- if constexpr (sizeof(PromteT) == CONST2) {983+ for (uint16_t i = 0; i < repeatTime; i++) {
988- AscendC::MicroAPI::DataCopy<PromteT, AscendC::MicroAPI::LoadDist::DIST_BRC_B16>(984+ AscendC::MicroAPI::DeInterleave(vreg1, vreg0, vreg0, tempOne);
989- vreg1, srcAddr + loopA * dimR + loopR);985+ AscendC::MicroAPI::Mul(vreg0, vreg1, vreg0, fullMask);
990- } else {
991- AscendC::MicroAPI::DataCopy<PromteT, AscendC::MicroAPI::LoadDist::DIST_BRC_B32>(
992- vreg1, srcAddr + loopA * dimR + loopR);
993- }
994- AscendC::MicroAPI::Mul(vreg0, vreg0, vreg1, mask);
995 }986 }
996- AscendC::MicroAPI::DataCopyUnAlign((__ubuf__ PromteT*&)dstAddr, vreg0, uDst, 1);987+ DataCopyUnAlign(dstAddr, vreg0, uDst, 1);
997 }988 }
998- AscendC::MicroAPI::DataCopyUnAlignPost((__ubuf__ PromteT*&)dstAddr, uDst, 0);989+ AscendC::MicroAPI::DataCopyUnAlignPost(dstAddr, uDst, 0);
999 }990 }
1000 }991 }
1001 992 
@@ -1003,8 +994,8 @@ public:
1003 __aicore__ inline void ReduceARI64(__ubuf__ PromteT* dstAddr, __ubuf__ PromteT* srcAddr, int32_t dimA, int32_t dimR,994 __aicore__ inline void ReduceARI64(__ubuf__ PromteT* dstAddr, __ubuf__ PromteT* srcAddr, int32_t dimA, int32_t dimR,
1004 int32_t mainR, int32_t outerR, int32_t innerR)995 int32_t mainR, int32_t outerR, int32_t innerR)
1005 {996 {
1006- constexpr uint16_t vLElems = IsB64<PromteT>() ? VL_LEN / sizeof(float) : VL_LEN / sizeof(PromteT);997+ constexpr uint16_t vLElems = VL_LEN / sizeof(float);
1007- constexpr uint16_t blkElems = IsB64<PromteT>() ? BLOCK_SIZE / sizeof(float) : BLOCK_SIZE / sizeof(PromteT);998+ constexpr uint16_t blkElems = BLOCK_SIZE / sizeof(float);
1008 if (outerR == 1 && innerR <= static_cast<int32_t>(blkElems * CONST2) &&999 if (outerR == 1 && innerR <= static_cast<int32_t>(blkElems * CONST2) &&
1009 dimA >= static_cast<int32_t>(vLElems / CONST2)) {1000 dimA >= static_cast<int32_t>(vLElems / CONST2)) {
1010 return ReduceARLessBlockI64<DIndex, float, Trait>(dstAddr, srcAddr, dimA, dimR, innerR);1001 return ReduceARLessBlockI64<DIndex, float, Trait>(dstAddr, srcAddr, dimA, dimR, innerR);
@@ -1023,11 +1014,9 @@ public:
1023 const uint16_t tailFolds = folds % BASE_FOLD_I64;1014 const uint16_t tailFolds = folds % BASE_FOLD_I64;
1024 const uint16_t fold1 = tailFolds == FOLD1 ? 1 : 0;1015 const uint16_t fold1 = tailFolds == FOLD1 ? 1 : 0;
1025 const uint16_t fold2 = tailFolds == FOLD2 ? 1 : 0;1016 const uint16_t fold2 = tailFolds == FOLD2 ? 1 : 0;
1026- const uint16_t rNum = mainR < vLElems ? mainR : vLElems;
1027- const int16_t blkRepeats = rNum / blkElems - 1;
1028- const uint16_t isBlkRepeat = blkRepeats > 0 ? 1 : 0;
1029- const uint16_t reservedR = rNum < blkElems ? rNum : blkElems;
1030 const uint16_t loopANum = dimA;1017 const uint16_t loopANum = dimA;
1018+ const uint16_t eleCount = mainR >= vLElems ? vLElems : mainR;
1019+ const uint16_t repeatTime = LogBase2(eleCount);
1031 1020 
1032 __VEC_SCOPE__1021 __VEC_SCOPE__
1033 {1022 {
@@ -1096,22 +1085,24 @@ public:
1096 AscendC::MicroAPI::MemType::VEC_LOAD>();1085 AscendC::MicroAPI::MemType::VEC_LOAD>();
1097 }1086 }
1098 1087 
1099- ReduceARFoldBase<PromteT, Trait>(dstAddr, srcAddr, dimR, blkElems, blkElems, vLElems, loopANum, fold1,1088+ ReduceARFoldBase<PromteT, Trait>(srcAddr, dimR, vLElems, loopANum, fold1, fold2, 0, mask, vreg0, vreg1,
1100- fold2, 0, isBlkRepeat, blkRepeats, mask, vreg0, vreg1, vreg2, vreg3, vreg4,1089+ vreg2, vreg3, vreg4, vreg5, vreg6, vreg7);
1101- vreg5, vreg6, vreg7);
1102 1090 
1103 // Reduce to 11091 // Reduce to 1
1104- uint32_t sreg1 = 1U;1092+ AscendC::MicroAPI::LocalMemBar<AscendC::MicroAPI::MemType::VEC_STORE,
1093+ AscendC::MicroAPI::MemType::VEC_LOAD>();
1094+ uint32_t sreg1 = mainR;
1105 mask = AscendC::MicroAPI::UpdateMask<PromteT, Trait>(sreg1);1095 mask = AscendC::MicroAPI::UpdateMask<PromteT, Trait>(sreg1);
1096+ AscendC::MicroAPI::RegTensor<PromteT, Trait> tempOne;
1097+ AscendC::MicroAPI::Duplicate(tempOne, 1);
1106 for (uint16_t loopA = 0; loopA < loopANum; loopA++) {1098 for (uint16_t loopA = 0; loopA < loopANum; loopA++) {
1107- AscendC::MicroAPI::Duplicate(vreg0, 1);1099+ AscendC::MicroAPI::DataCopy(vreg0, srcAddr + loopA * dimR);
1108- for (uint16_t loopR = 0; loopR < reservedR; loopR++) {1100+ AscendC::MicroAPI::Select(vreg0, vreg0, tempOne, mask);
1109- auto src = srcAddr + loopA * dimR + loopR;1101+ for (uint16_t i = 0; i < repeatTime; i++) {
1110- AscendC::MicroAPI::DataCopyUnAlignPre(uSrc, src);1102+ AscendC::MicroAPI::DeInterleave(vreg1, vreg0, vreg0, tempOne);
1111- AscendC::MicroAPI::DataCopyUnAlign(vreg1, uSrc, src, 1);1103+ AscendC::MicroAPI::Mul(vreg0, vreg1, vreg0, fullMask);
1112- AscendC::MicroAPI::Mul(vreg0, vreg0, vreg1, mask);
1113 }1104 }
1114- AscendC::MicroAPI::DataCopyUnAlign(dstAddr, vreg0, uDst, 1);1105+ DataCopyUnAlign(dstAddr, vreg0, uDst, 1);
1115 }1106 }
1116 AscendC::MicroAPI::DataCopyUnAlignPost(dstAddr, uDst, 0);1107 AscendC::MicroAPI::DataCopyUnAlignPost(dstAddr, uDst, 0);
1117 }1108 }
@@ -1424,7 +1415,7 @@ public:
1424 constexpr uint16_t vLElems = IsB64<PromteT>() ? VL_LEN / sizeof(float) : VL_LEN / sizeof(PromteT);1415 constexpr uint16_t vLElems = IsB64<PromteT>() ? VL_LEN / sizeof(float) : VL_LEN / sizeof(PromteT);
1425 constexpr uint16_t blkElems = IsB64<PromteT>() ? BLOCK_SIZE / sizeof(float) : BLOCK_SIZE / sizeof(PromteT);1416 constexpr uint16_t blkElems = IsB64<PromteT>() ? BLOCK_SIZE / sizeof(float) : BLOCK_SIZE / sizeof(PromteT);
1426 if constexpr (!Pattern::TailA) {1417 if constexpr (!Pattern::TailA) {
1427- if (padding.burstPaddingRepeat == 1U && shape.innerR <= blkElems * CONST2 &&1418+ if (shape.outerR == 1 && padding.burstPaddingRepeat == 1U && shape.innerR <= blkElems * CONST2 &&
1428 shape.value[0] >= vLElems / CONST2) {1419 shape.value[0] >= vLElems / CONST2) {
1429 return;1420 return;
1430 }1421 }