Merged
feat(qbmm): account for StreamK Fixpipe write cost #9506
feat(qbmm): account for StreamK Fixpipe write cost #9506
Merged
guiruoyuAdd on Aug 29
Total: 3 files changed+101-8
@@ -50,8 +50,10 @@ using optiling::qmmv3_tiling_const::MXFP_DIVISOR_SIZE;
50using optiling::qmmv3_tiling_const::MXFP_MULTI_BASE_SIZE;50using optiling::qmmv3_tiling_const::MXFP_MULTI_BASE_SIZE;
51using optiling::qmmv3_tiling_const::NUM_HALF;51using optiling::qmmv3_tiling_const::NUM_HALF;
52// Benefit thresholds are tuned for DAV_3510/Ascend950 QBMM MX StreamK. Sizes are bytes, time is ns, and permille52// Benefit thresholds are tuned for DAV_3510/Ascend950 QBMM MX StreamK. Sizes are bytes, time is ns, and permille
53-// values use 1000 as the denominator. The transfer model uses 4 TB/s for MTE2/AIV paths; the fixed overhead and53+// values use 1000 as the denominator. The transfer model uses 4 TB/s for MTE2/AIV paths. Fixpipe uses 80% of
54-// safety margin come from ops-test-kit single-op profiling and cover StreamK scheduling/reduce overhead.54+// 5.2 TB/s when every candidate N tile width is aligned and 25% otherwise. The unaligned value is based on the
55+// 1.308 TB/s median measured by the N-tail profiling suite. The fixed overhead and safety margin come from
56+// ops-test-kit single-op profiling and cover StreamK scheduling/reduce/Fixpipe uncertainty.
55// The StreamK RPC workspace is sized in MiB and is reserved for the multi-block reduce synchronization path.57// The StreamK RPC workspace is sized in MiB and is reserved for the multi-block reduce synchronization path.
56constexpr uint64_t STREAMK_RPC_WORKSPACE_SIZE = STREAMK_RPC_WORKSPACE_MB * 1024UL * 1024UL;58constexpr uint64_t STREAMK_RPC_WORKSPACE_SIZE = STREAMK_RPC_WORKSPACE_MB * 1024UL * 1024UL;
57constexpr uint64_t BENEFIT_MIN_K_THRESHOLD = 4096UL;59constexpr uint64_t BENEFIT_MIN_K_THRESHOLD = 4096UL;
@@ -61,10 +63,14 @@ constexpr uint64_t BENEFIT_MTE2_MIN_SAVED_BYTES = 2UL * 1024UL * 1024UL;
61constexpr uint64_t BENEFIT_MTE2_MIN_SAVED_PERMILLE = 330UL;63constexpr uint64_t BENEFIT_MTE2_MIN_SAVED_PERMILLE = 330UL;
62constexpr uint64_t BENEFIT_MTE2_BW_BYTES_PER_NS = 4000UL;64constexpr uint64_t BENEFIT_MTE2_BW_BYTES_PER_NS = 4000UL;
63constexpr uint64_t BENEFIT_AIV_BW_BYTES_PER_NS = 4000UL;65constexpr uint64_t BENEFIT_AIV_BW_BYTES_PER_NS = 4000UL;
66+constexpr uint64_t BENEFIT_FIXPIPE_BASE_BW_BYTES_PER_NS = 5200UL;
67+constexpr uint64_t BENEFIT_FIXPIPE_ALIGNED_BW_BYTES_PER_NS = BENEFIT_FIXPIPE_BASE_BW_BYTES_PER_NS * 80UL / 100UL;
68+constexpr uint64_t BENEFIT_FIXPIPE_UNALIGNED_BW_BYTES_PER_NS = BENEFIT_FIXPIPE_BASE_BW_BYTES_PER_NS * 25UL / 100UL;
64constexpr uint64_t BENEFIT_STREAMK_FIXED_OVERHEAD_NS = 500UL;69constexpr uint64_t BENEFIT_STREAMK_FIXED_OVERHEAD_NS = 500UL;
65constexpr uint64_t BENEFIT_STREAMK_MIN_MARGIN_NS = 300UL;70constexpr uint64_t BENEFIT_STREAMK_MIN_MARGIN_NS = 300UL;
66constexpr uint64_t BENEFIT_TIME_SAFETY_NUM = 6UL;71constexpr uint64_t BENEFIT_TIME_SAFETY_NUM = 6UL;
67constexpr uint64_t BENEFIT_TIME_SAFETY_DEN = 5UL;72constexpr uint64_t BENEFIT_TIME_SAFETY_DEN = 5UL;
73+constexpr uint64_t FIXPIPE_N_ALIGN_ELEMENTS = 32UL;
68 74 
69const std::vector<int32_t> supportedNpuArch = {static_cast<int32_t>(NpuArch::DAV_3510)};75const std::vector<int32_t> supportedNpuArch = {static_cast<int32_t>(NpuArch::DAV_3510)};
70constexpr int32_t TILING_PRIORITY = optiling::strategy::MX_STREAMK;76constexpr int32_t TILING_PRIORITY = optiling::strategy::MX_STREAMK;
@@ -467,8 +473,11 @@ bool BuildActualStreamKCandidate(const optiling::QuantBatchMatmulInfo& inputPara
467}473}
468 474 
469struct BenefitExtraCost {475struct BenefitExtraCost {
476+ bool allFixpipeNTilesAligned = true;
470 uint64_t workspaceReadBytes = 0UL;477 uint64_t workspaceReadBytes = 0UL;
471 uint64_t outputWriteBytes = 0UL;478 uint64_t outputWriteBytes = 0UL;
479+ uint64_t fixpipeWriteBytes = 0UL;
480+ uint64_t fixpipeTimeNs = 0UL;
472 uint64_t fixedOverheadNs = BENEFIT_STREAMK_FIXED_OVERHEAD_NS;481 uint64_t fixedOverheadNs = BENEFIT_STREAMK_FIXED_OVERHEAD_NS;
473 uint64_t totalBytes = 0UL;482 uint64_t totalBytes = 0UL;
474 uint64_t totalTimeNs = 0UL;483 uint64_t totalTimeNs = 0UL;
@@ -484,6 +493,14 @@ BenefitExtraCost EstimateStreamKExtraBytes(const BenefitCandidate& candidate,
484 uint64_t outputTileBytes = GetSizeWithDataTypeLocal(tileElements, inputParams.cDtype);493 uint64_t outputTileBytes = GetSizeWithDataTypeLocal(tileElements, inputParams.cDtype);
485 cost.workspaceReadBytes = SaturatingMul(SaturatingMul(streamKTailMnCnt, candidate.splitK), workspaceTileBytes);494 cost.workspaceReadBytes = SaturatingMul(SaturatingMul(streamKTailMnCnt, candidate.splitK), workspaceTileBytes);
486 cost.outputWriteBytes = SaturatingMul(streamKTailMnCnt, outputTileBytes);495 cost.outputWriteBytes = SaturatingMul(streamKTailMnCnt, outputTileBytes);
496+ const uint64_t logicalOutputWriteBytes = SaturatingMul(
497+ SaturatingMul(SaturatingMul(inputParams.mSize, inputParams.nSize), candidate.splitK), DATA_SIZE_L0C);
498+ // Both values bound the split-K Fixpipe writeback without reproducing the device-side tile scheduler.
499+ cost.fixpipeWriteBytes = std::min(cost.workspaceReadBytes, logicalOutputWriteBytes);
500+ const auto fixpipeCost = optiling::streamk_cost_model::EstimateFixpipeCost(inputParams.nSize, candidate.baseN,
501+ candidate.nCnt, cost.fixpipeWriteBytes);
502+ cost.allFixpipeNTilesAligned = fixpipeCost.allNTilesAligned;
503+ cost.fixpipeTimeNs = fixpipeCost.valid ? fixpipeCost.timeNs : UINT64_SATURATED;
487 cost.totalBytes = SaturatingAdd(cost.workspaceReadBytes, cost.outputWriteBytes);504 cost.totalBytes = SaturatingAdd(cost.workspaceReadBytes, cost.outputWriteBytes);
488 cost.totalTimeNs = SaturatingAdd(CalcTransferTimeNs(cost.totalBytes, BENEFIT_AIV_BW_BYTES_PER_NS),505 cost.totalTimeNs = SaturatingAdd(CalcTransferTimeNs(cost.totalBytes, BENEFIT_AIV_BW_BYTES_PER_NS),
489 cost.fixedOverheadNs);506 cost.fixedOverheadNs);
@@ -506,8 +523,11 @@ struct BenefitGateEval {
506 uint64_t skMte2Bytes = 0UL;523 uint64_t skMte2Bytes = 0UL;
507 uint64_t savedMte2Bytes = 0UL;524 uint64_t savedMte2Bytes = 0UL;
508 uint64_t extraBytes = 0UL;525 uint64_t extraBytes = 0UL;
526+ bool allFixpipeNTilesAligned = true;
509 uint64_t workspaceExtraBytes = 0UL;527 uint64_t workspaceExtraBytes = 0UL;
510 uint64_t reduceExtraBytes = 0UL;528 uint64_t reduceExtraBytes = 0UL;
529+ uint64_t fixpipeWriteBytes = 0UL;
530+ uint64_t fixpipeTimeNs = 0UL;
511 uint64_t fixedOverheadNs = 0UL;531 uint64_t fixedOverheadNs = 0UL;
512 uint64_t transposeExtraBytes = 0UL;532 uint64_t transposeExtraBytes = 0UL;
513 uint64_t requiredSavedBytes = 0UL;533 uint64_t requiredSavedBytes = 0UL;
@@ -642,8 +662,11 @@ void FillEvalWithActualStreamK(const optiling::QuantBatchMatmulInfo& inputParams
642 eval.skSplitK = actualSk.splitK;662 eval.skSplitK = actualSk.splitK;
643 eval.skMte2Bytes = actualSk.mte2Bytes;663 eval.skMte2Bytes = actualSk.mte2Bytes;
644 eval.extraBytes = extraCost.totalBytes;664 eval.extraBytes = extraCost.totalBytes;
665+ eval.allFixpipeNTilesAligned = extraCost.allFixpipeNTilesAligned;
645 eval.workspaceExtraBytes = extraCost.workspaceReadBytes;666 eval.workspaceExtraBytes = extraCost.workspaceReadBytes;
646 eval.reduceExtraBytes = extraCost.outputWriteBytes;667 eval.reduceExtraBytes = extraCost.outputWriteBytes;
668+ eval.fixpipeWriteBytes = extraCost.fixpipeWriteBytes;
669+ eval.fixpipeTimeNs = extraCost.fixpipeTimeNs;
647 eval.fixedOverheadNs = extraCost.fixedOverheadNs;670 eval.fixedOverheadNs = extraCost.fixedOverheadNs;
648 eval.transposeExtraBytes = 0UL;671 eval.transposeExtraBytes = 0UL;
649 eval.extraCostTimeNs = extraCost.totalTimeNs;672 eval.extraCostTimeNs = extraCost.totalTimeNs;
@@ -685,8 +708,9 @@ void FillMte2SavingEval(const BenefitGateSearchResult& result, BenefitGateEval&
685 eval.savedMte2Permille = CalcPermille(eval.savedMte2Bytes, eval.aswtMte2Bytes);708 eval.savedMte2Permille = CalcPermille(eval.savedMte2Bytes, eval.aswtMte2Bytes);
686 eval.extraCostPermille = CalcPermille(eval.extraBytes, eval.aswtMte2Bytes);709 eval.extraCostPermille = CalcPermille(eval.extraBytes, eval.aswtMte2Bytes);
687 eval.savedMte2TimeNs = CalcTransferTimeNs(eval.savedMte2Bytes, BENEFIT_MTE2_BW_BYTES_PER_NS);710 eval.savedMte2TimeNs = CalcTransferTimeNs(eval.savedMte2Bytes, BENEFIT_MTE2_BW_BYTES_PER_NS);
711+ const uint64_t estimatedExtraTimeNs = SaturatingAdd(eval.extraCostTimeNs, eval.fixpipeTimeNs);
688 eval.requiredSavedTimeNs = SaturatingAdd(712 eval.requiredSavedTimeNs = SaturatingAdd(
689- SaturatingMulDiv(eval.extraCostTimeNs, BENEFIT_TIME_SAFETY_NUM, BENEFIT_TIME_SAFETY_DEN),713+ SaturatingMulDiv(estimatedExtraTimeNs, BENEFIT_TIME_SAFETY_NUM, BENEFIT_TIME_SAFETY_DEN),
690 BENEFIT_STREAMK_MIN_MARGIN_NS);714 BENEFIT_STREAMK_MIN_MARGIN_NS);
691 eval.requiredSavedBytes = std::max(BENEFIT_MTE2_MIN_SAVED_BYTES,715 eval.requiredSavedBytes = std::max(BENEFIT_MTE2_MIN_SAVED_BYTES,
692 SaturatingMul(eval.requiredSavedTimeNs, BENEFIT_MTE2_BW_BYTES_PER_NS));716 SaturatingMul(eval.requiredSavedTimeNs, BENEFIT_MTE2_BW_BYTES_PER_NS));
@@ -756,7 +780,8 @@ void LogBenefitGateEval(const char* opName, const optiling::QuantBatchMatmulInfo
756 "aswtMCnt=%lu aswtNCnt=%lu aswtAFullLoad=%d "780 "aswtMCnt=%lu aswtNCnt=%lu aswtAFullLoad=%d "
757 "skBaseM=%lu skBaseN=%lu skBaseK=%lu skMnCnt=%lu skSingleCoreK=%lu skSplitK=%lu "781 "skBaseM=%lu skBaseN=%lu skBaseK=%lu skMnCnt=%lu skSingleCoreK=%lu skSplitK=%lu "
758 "aswtMte2Bytes=%lu skMte2Bytes=%lu savedMte2Bytes=%lu extraBytes=%lu "782 "aswtMte2Bytes=%lu skMte2Bytes=%lu savedMte2Bytes=%lu extraBytes=%lu "
759- "workspaceExtraBytes=%lu reduceExtraBytes=%lu fixedOverheadNs=%lu transposeExtraBytes=%lu "783+ "workspaceExtraBytes=%lu reduceExtraBytes=%lu fixpipeWriteBytes=%lu "
784+ "allFixpipeNTilesAligned=%d fixpipeTimeNs=%lu fixedOverheadNs=%lu transposeExtraBytes=%lu "
760 "requiredSavedBytes=%lu savedMte2Permille=%lu extraCostPermille=%lu requiredSavedPermille=%lu "785 "requiredSavedBytes=%lu savedMte2Permille=%lu extraCostPermille=%lu requiredSavedPermille=%lu "
761 "savedMte2TimeNs=%lu extraCostTimeNs=%lu requiredSavedTimeNs=%lu.",786 "savedMte2TimeNs=%lu extraCostTimeNs=%lu requiredSavedTimeNs=%lu.",
762 benefitGate.admit ? "ADMIT" : "REJECT", benefitGate.reason, inputParams.mSize, inputParams.nSize,787 benefitGate.admit ? "ADMIT" : "REJECT", benefitGate.reason, inputParams.mSize, inputParams.nSize,
@@ -766,15 +791,44 @@ void LogBenefitGateEval(const char* opName, const optiling::QuantBatchMatmulInfo
766 benefitGate.skBaseM, benefitGate.skBaseN, benefitGate.skBaseK, benefitGate.skMnCnt,791 benefitGate.skBaseM, benefitGate.skBaseN, benefitGate.skBaseK, benefitGate.skMnCnt,
767 benefitGate.skSingleCoreK, benefitGate.skSplitK, benefitGate.aswtMte2Bytes, benefitGate.skMte2Bytes,792 benefitGate.skSingleCoreK, benefitGate.skSplitK, benefitGate.aswtMte2Bytes, benefitGate.skMte2Bytes,
768 benefitGate.savedMte2Bytes, benefitGate.extraBytes, benefitGate.workspaceExtraBytes,793 benefitGate.savedMte2Bytes, benefitGate.extraBytes, benefitGate.workspaceExtraBytes,
769- benefitGate.reduceExtraBytes, benefitGate.fixedOverheadNs, benefitGate.transposeExtraBytes,794+ benefitGate.reduceExtraBytes, benefitGate.fixpipeWriteBytes,
770- benefitGate.requiredSavedBytes, benefitGate.savedMte2Permille, benefitGate.extraCostPermille,795+ static_cast<int32_t>(benefitGate.allFixpipeNTilesAligned), benefitGate.fixpipeTimeNs,
771- benefitGate.requiredSavedPermille, benefitGate.savedMte2TimeNs, benefitGate.extraCostTimeNs,796+ benefitGate.fixedOverheadNs, benefitGate.transposeExtraBytes, benefitGate.requiredSavedBytes,
772- benefitGate.requiredSavedTimeNs);797+ benefitGate.savedMte2Permille, benefitGate.extraCostPermille, benefitGate.requiredSavedPermille,
798+ benefitGate.savedMte2TimeNs, benefitGate.extraCostTimeNs, benefitGate.requiredSavedTimeNs);
773}799}
774} // namespace800} // namespace
775 801 
776namespace optiling {802namespace optiling {
777 803 
804+namespace streamk_cost_model {
805+ 
806+FixpipeCostResult EstimateFixpipeCost(uint64_t n, uint64_t baseN, uint64_t nCnt, uint64_t writeBytes)
807+{
808+ FixpipeCostResult result;
809+ if (n == 0UL || baseN == 0UL || nCnt == 0UL || SafeCeilDiv(n, baseN) != nCnt) {
810+ return result;
811+ }
812+ 
813+ uint64_t tailOffset = 0UL;
814+ if (!CheckedMul(nCnt - 1UL, baseN, tailOffset) || tailOffset >= n) {
815+ return result;
816+ }
817+ const uint64_t tailN = n - tailOffset;
818+ // Check every N width the candidate can produce. This deliberately avoids duplicating the device scheduler;
819+ // an unaligned tail outside the split-K region may be overestimated, which is safe for an admission gate.
820+ result.allNTilesAligned = tailN % FIXPIPE_N_ALIGN_ELEMENTS == 0UL &&
821+ (nCnt == 1UL || baseN % FIXPIPE_N_ALIGN_ELEMENTS == 0UL);
822+ 
823+ const uint64_t bandwidth = result.allNTilesAligned ? BENEFIT_FIXPIPE_ALIGNED_BW_BYTES_PER_NS :
824+ BENEFIT_FIXPIPE_UNALIGNED_BW_BYTES_PER_NS;
825+ result.timeNs = CalcTransferTimeNs(writeBytes, bandwidth);
826+ result.valid = true;
827+ return result;
828+}
829+ 
830+} // namespace streamk_cost_model
831+ 
778QBMMV3StreamKTiling::QBMMV3StreamKTiling(gert::TilingContext* context)832QBMMV3StreamKTiling::QBMMV3StreamKTiling(gert::TilingContext* context)
779 : QuantBatchMatmulV3TilingBase(context, false), tilingData_(tilingDataSelf_)833 : QuantBatchMatmulV3TilingBase(context, false), tilingData_(tilingDataSelf_)
780{834{
@@ -21,6 +21,18 @@
21 21 
22namespace optiling {22namespace optiling {
23 23 
24+namespace streamk_cost_model {
25+ 
26+struct FixpipeCostResult {
27+ bool valid = false;
28+ bool allNTilesAligned = true;
29+ uint64_t timeNs = 0UL;
30+};
31+ 
32+FixpipeCostResult EstimateFixpipeCost(uint64_t n, uint64_t baseN, uint64_t nCnt, uint64_t writeBytes);
33+ 
34+} // namespace streamk_cost_model
35+ 
24class QBMMV3StreamKTiling : public QuantBatchMatmulV3TilingBase {36class QBMMV3StreamKTiling : public QuantBatchMatmulV3TilingBase {
25public:37public:
26 explicit QBMMV3StreamKTiling(gert::TilingContext* context);38 explicit QBMMV3StreamKTiling(gert::TilingContext* context);
@@ -930,6 +930,33 @@ TEST(QuantBatchMatmulV3StreamKSingleCoreKAlign, CubeStreamKAlignsEveryTransposeT
930 }930 }
931}931}
932 932 
933+TEST(QuantBatchMatmulV3StreamKFixpipeCost, UsesEightyPercentPeakBandwidthForAlignedNTiles)
934+{
935+ const auto result = streamk_cost_model::EstimateFixpipeCost(224UL, 224UL, 1UL, 458752UL);
936+ 
937+ ASSERT_TRUE(result.valid);
938+ EXPECT_TRUE(result.allNTilesAligned);
939+ EXPECT_EQ(result.timeNs, 111UL);
940+}
941+ 
942+TEST(QuantBatchMatmulV3StreamKFixpipeCost, UsesQuarterPeakBandwidthWhenAnyNTileIsUnaligned)
943+{
944+ const auto result = streamk_cost_model::EstimateFixpipeCost(418UL, 224UL, 2UL, 5500880UL);
945+ 
946+ ASSERT_TRUE(result.valid);
947+ EXPECT_FALSE(result.allNTilesAligned);
948+ EXPECT_EQ(result.timeNs, 4232UL);
949+}
950+ 
951+TEST(QuantBatchMatmulV3StreamKFixpipeCost, ChecksBaseNEvenWhenShapeNIsAligned)
952+{
953+ const auto result = streamk_cost_model::EstimateFixpipeCost(480UL, 240UL, 2UL, 5200UL);
954+ 
955+ ASSERT_TRUE(result.valid);
956+ EXPECT_FALSE(result.allNTilesAligned);
957+ EXPECT_EQ(result.timeNs, 4UL);
958+}
959+ 
933TEST(QuantBatchMatmulV3StreamKSingleCoreKAlign, MxStreamKAlignsEveryTransposeTo256Bytes)960TEST(QuantBatchMatmulV3StreamKSingleCoreKAlign, MxStreamKAlignsEveryTransposeTo256Bytes)
934{961{
935 for (bool transA : {false, true}) {962 for (bool transA : {false, true}) {