| @@ -18,7 +18,6 @@ | |||
| 18 | #include "interface/function/function.h" | 18 | #include "interface/function/function.h" |
| 19 | #include "passes/pass_check/iso_partitioner_checker.h" | 19 | #include "passes/pass_check/iso_partitioner_checker.h" |
| 20 | #include "passes/pass_log/pass_log.h" | 20 | #include "passes/pass_log/pass_log.h" |
| 21 | -#include "passes/pass_utils/common_operation_eliminate_utils.h" | ||
| 22 | 21 | ||
| 23 | namespace npu::tile_fwk { | 22 | namespace npu::tile_fwk { |
| 24 | 23 | ||
| @@ -37,10 +36,6 @@ Status RunIsoPartition(Function& function) | |||
| 37 | APASS_LOG_ERROR_F(Elements::Function, "GraphPartition failed."); | 36 | APASS_LOG_ERROR_F(Elements::Function, "GraphPartition failed."); |
| 38 | return FAILED; | 37 | return FAILED; |
| 39 | } | 38 | } |
| 40 | - if (CommonOperationEliminateUtils::EliminateCommonOperation(function) != SUCCESS) { | ||
| 41 | - APASS_LOG_ERROR_F(Elements::Operation, "Common operation eliminate failed!"); | ||
| 42 | - return FAILED; | ||
| 43 | - } | ||
| 44 | APASS_LOG_INFO_F(Elements::Function, "===> End GraphPartition."); | 39 | APASS_LOG_INFO_F(Elements::Function, "===> End GraphPartition."); |
| 45 | return SUCCESS; | 40 | return SUCCESS; |
| 46 | } | 41 | } |
| @@ -27,16 +27,21 @@ | |||
| 27 | 27 | ||
| 28 | namespace npu::tile_fwk { | 28 | namespace npu::tile_fwk { |
| 29 | 29 | ||
| 30 | -Status IsoPartitioner::PartitionGraph(Function& function) | 30 | +bool IsoPartitioner::HandleLiteNPU(Function& function) |
| 31 | { | 31 | { |
| 32 | - if (IsLiteNPU(Platform::Instance().GetSoc().GetNPUArch())) { | 32 | + if (!IsLiteNPU(Platform::Instance().GetSoc().GetNPUArch())) { |
| 33 | - for (auto& op : function.Operations()) { | 33 | + return false; |
| 34 | - op.UpdateSubgraphID(0); | ||
| 35 | - } | ||
| 36 | - function.SetTotalSubGraphCount(1); | ||
| 37 | - APASS_LOG_INFO_F(Elements::Operation, "Graph Partition is skipped."); | ||
| 38 | - return SUCCESS; | ||
| 39 | } | 34 | } |
| 35 | + for (auto& op : function.Operations(false)) { | ||
| 36 | + op.UpdateSubgraphID(0); | ||
| 37 | + } | ||
| 38 | + function.SetTotalSubGraphCount(1); | ||
| 39 | + APASS_LOG_INFO_F(Elements::Operation, "Graph Partition is skipped."); | ||
| 40 | + return true; | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +Status IsoPartitioner::RunPartitionSteps(Function& function) | ||
| 44 | +{ | ||
| 40 | if (EstimateCycleUB(function) != SUCCESS) { | 45 | if (EstimateCycleUB(function) != SUCCESS) { |
| 41 | APASS_LOG_ERROR_F(Elements::Config, "Estimate and refresh cycleUB_ failed."); | 46 | APASS_LOG_ERROR_F(Elements::Config, "Estimate and refresh cycleUB_ failed."); |
| 42 | return FAILED; | 47 | return FAILED; |
| @@ -79,14 +84,23 @@ Status IsoPartitioner::PartitionGraph(Function& function) | |||
| 79 | return SUCCESS; | 84 | return SUCCESS; |
| 80 | } | 85 | } |
| 81 | 86 | ||
| 87 | +Status IsoPartitioner::PartitionGraph(Function& function) | ||
| 88 | +{ | ||
| 89 | + if (HandleLiteNPU(function)) { | ||
| 90 | + return SUCCESS; | ||
| 91 | + } | ||
| 92 | + function.SortOperations(SortOperationsMode::LIGHTWEIGHT); | ||
| 93 | + return RunPartitionSteps(function); | ||
| 94 | +} | ||
| 95 | + | ||
| 82 | Status IsoPartitioner::BuildIsomorphismGroups() | 96 | Status IsoPartitioner::BuildIsomorphismGroups() |
| 83 | { | 97 | { |
| 84 | std::vector<int32_t> idxInLinkNum; | 98 | std::vector<int32_t> idxInLinkNum; |
| 85 | std::deque<int32_t> zeroInQueue; | 99 | std::deque<int32_t> zeroInQueue; |
| 86 | std::unordered_set<int32_t> currentNodeSet; | 100 | std::unordered_set<int32_t> currentNodeSet; |
| 87 | - for (size_t i = 0; i < superNodeInfo_->nodeInGraph_.size(); i++) { | 101 | + for (size_t i = 0; i < superNodeInfo_->nodeInGraphList_.size(); i++) { |
| 88 | - idxInLinkNum.push_back(superNodeInfo_->nodeInGraph_[i].size()); | 102 | + idxInLinkNum.push_back(superNodeInfo_->nodeInGraphList_[i].size()); |
| 89 | - if (superNodeInfo_->nodeInGraph_[i].size() == 0) { | 103 | + if (superNodeInfo_->nodeInGraphList_[i].size() == 0) { |
| 90 | zeroInQueue.push_front(i); | 104 | zeroInQueue.push_front(i); |
| 91 | } | 105 | } |
| 92 | } | 106 | } |
| @@ -160,7 +174,7 @@ Status IsomorphismGraphGroup::BuildGraphGroup(std::shared_ptr<OperationGraphInfo | |||
| 160 | Status IsomorphismGraphGroup::InLinkCountDelete(int32_t nodeIdx, std::vector<int32_t>& idxInLinkNum, | 174 | Status IsomorphismGraphGroup::InLinkCountDelete(int32_t nodeIdx, std::vector<int32_t>& idxInLinkNum, |
| 161 | std::deque<int32_t>& zeroInQueue) | 175 | std::deque<int32_t>& zeroInQueue) |
| 162 | { | 176 | { |
| 163 | - for (int32_t consumer : superNodeInfo_->nodeOutGraph_[nodeIdx]) { | 177 | + for (int32_t consumer : superNodeInfo_->nodeOutGraphList_[nodeIdx]) { |
| 164 | if (consumer < 0 || consumer >= static_cast<int32_t>(idxInLinkNum.size())) { | 178 | if (consumer < 0 || consumer >= static_cast<int32_t>(idxInLinkNum.size())) { |
| 165 | APASS_LOG_ERROR_F(Elements::Operation, "Consumer index(%d) illegal in InLinkCountDelete.", consumer); | 179 | APASS_LOG_ERROR_F(Elements::Operation, "Consumer index(%d) illegal in InLinkCountDelete.", consumer); |
| 166 | return FAILED; | 180 | return FAILED; |
| @@ -259,7 +273,7 @@ bool IsomorphismGraphGroup::IsLegalIsoGraphExtender(std::vector<int32_t>& expand | |||
| 259 | if (currentNodeSet.count(candidate) > 0) { | 273 | if (currentNodeSet.count(candidate) > 0) { |
| 260 | return false; | 274 | return false; |
| 261 | } | 275 | } |
| 262 | - for (int32_t fromNode : superNodeInfo_->nodeInGraph_[candidate]) { | 276 | + for (int32_t fromNode : superNodeInfo_->nodeInGraphList_[candidate]) { |
| 263 | if (subVisitedNodeSet_.count(fromNode) > 0 && !isoGraphs_[i]->HasNode(fromNode)) { | 277 | if (subVisitedNodeSet_.count(fromNode) > 0 && !isoGraphs_[i]->HasNode(fromNode)) { |
| 264 | return false; | 278 | return false; |
| 265 | } | 279 | } |
| @@ -278,7 +292,7 @@ bool IsomorphismGraphGroup::IsLegalIsoGraphExtender(std::vector<int32_t>& expand | |||
| 278 | return false; | 292 | return false; |
| 279 | } | 293 | } |
| 280 | } | 294 | } |
| 281 | - std::set<int32_t> candSet(expandCandidate.begin(), expandCandidate.end()); | 295 | + std::unordered_set<int32_t> candSet(expandCandidate.begin(), expandCandidate.end()); |
| 282 | if (candSet.size() != expandCandidate.size()) { | 296 | if (candSet.size() != expandCandidate.size()) { |
| 283 | return false; | 297 | return false; |
| 284 | } | 298 | } |
| @@ -294,7 +308,7 @@ int32_t SubGraph::GetExpandCandidate(size_t expandNodeIdx, size_t expandLinkIdx, | |||
| 294 | res = GraphExtendResult::EXTEND_NODE_EXHAUST; | 308 | res = GraphExtendResult::EXTEND_NODE_EXHAUST; |
| 295 | return 0; | 309 | return 0; |
| 296 | } | 310 | } |
| 297 | - if (expandLinkIdx >= superNodeInfo_->nodeOutGraph_[nodeList_[expandNodeIdx]].size()) { | 311 | + if (expandLinkIdx >= superNodeInfo_->nodeOutGraphList_[nodeList_[expandNodeIdx]].size()) { |
| 298 | res = GraphExtendResult::EXTEND_LINK_EXHAUST; | 312 | res = GraphExtendResult::EXTEND_LINK_EXHAUST; |
| 299 | return 0; | 313 | return 0; |
| 300 | } | 314 | } |
| @@ -310,17 +324,21 @@ void SubGraph::BuildInOutSet() | |||
| 310 | inNodes_.clear(); | 324 | inNodes_.clear(); |
| 311 | outNodes_.clear(); | 325 | outNodes_.clear(); |
| 312 | for (int32_t nodeIdx : nodeList_) { | 326 | for (int32_t nodeIdx : nodeList_) { |
| 313 | - for (int32_t inIdx : superNodeInfo_->nodeInGraph_[nodeIdx]) { | 327 | + for (int32_t inIdx : superNodeInfo_->nodeInGraphList_[nodeIdx]) { |
| 314 | if (nodeSet_.count(inIdx) == 0) { | 328 | if (nodeSet_.count(inIdx) == 0) { |
| 315 | - inNodes_.insert(inIdx); | 329 | + inNodes_.push_back(inIdx); |
| 316 | } | 330 | } |
| 317 | } | 331 | } |
| 318 | - for (int32_t outIdx : superNodeInfo_->nodeOutGraph_[nodeIdx]) { | 332 | + for (int32_t outIdx : superNodeInfo_->nodeOutGraphList_[nodeIdx]) { |
| 319 | if (nodeSet_.count(outIdx) == 0) { | 333 | if (nodeSet_.count(outIdx) == 0) { |
| 320 | - outNodes_.insert(outIdx); | 334 | + outNodes_.push_back(outIdx); |
| 321 | } | 335 | } |
| 322 | } | 336 | } |
| 323 | } | 337 | } |
| 338 | + std::sort(inNodes_.begin(), inNodes_.end()); | ||
| 339 | + inNodes_.erase(std::unique(inNodes_.begin(), inNodes_.end()), inNodes_.end()); | ||
| 340 | + std::sort(outNodes_.begin(), outNodes_.end()); | ||
| 341 | + outNodes_.erase(std::unique(outNodes_.begin(), outNodes_.end()), outNodes_.end()); | ||
| 324 | } | 342 | } |
| 325 | 343 | ||
| 326 | std::shared_ptr<SubGraph> IsomorphismGraphGroup::GetSubGraph(int32_t idx) | 344 | std::shared_ptr<SubGraph> IsomorphismGraphGroup::GetSubGraph(int32_t idx) |
| @@ -350,18 +368,18 @@ void IsomorphismGraphGroup::Clear() | |||
| 350 | } | 368 | } |
| 351 | 369 | ||
| 352 | Status IsoPartitioner::IsomorphismGroupMergePrepare(std::vector<std::pair<int32_t, int32_t>>& isoSubIdxs, | 370 | Status IsoPartitioner::IsomorphismGroupMergePrepare(std::vector<std::pair<int32_t, int32_t>>& isoSubIdxs, |
| 353 | - std::vector<std::set<int32_t>>& isoInGraph, | 371 | + std::vector<std::unordered_set<int32_t>>& isoInGraph, |
| 354 | - std::vector<std::set<int32_t>>& isoOutGraph, | 372 | + std::vector<std::unordered_set<int32_t>>& isoOutGraph, |
| 355 | std::vector<std::vector<int32_t>>& isoNodeList, | 373 | std::vector<std::vector<int32_t>>& isoNodeList, |
| 356 | std::vector<int32_t>& isoIdx2color) | 374 | std::vector<int32_t>& isoIdx2color) |
| 357 | { | 375 | { |
| 358 | - isoSubIdxs.resize(superNodeInfo_->nodeInGraph_.size()); | 376 | + isoSubIdxs.resize(superNodeInfo_->nodeInGraphList_.size()); |
| 359 | isoInGraph.resize(isoSubGroups_.size()); | 377 | isoInGraph.resize(isoSubGroups_.size()); |
| 360 | isoOutGraph.resize(isoSubGroups_.size()); | 378 | isoOutGraph.resize(isoSubGroups_.size()); |
| 361 | for (size_t i = 0; i < isoSubGroups_.size(); i++) { | 379 | for (size_t i = 0; i < isoSubGroups_.size(); i++) { |
| 362 | for (size_t j = 0; j < isoSubGroups_[i]->Size(); j++) { | 380 | for (size_t j = 0; j < isoSubGroups_[i]->Size(); j++) { |
| 363 | for (int32_t nodeIdx : isoSubGroups_[i]->GetSubGraph(j)->GetNodeList()) { | 381 | for (int32_t nodeIdx : isoSubGroups_[i]->GetSubGraph(j)->GetNodeList()) { |
| 364 | - if (nodeIdx < 0 || nodeIdx >= static_cast<int32_t>(superNodeInfo_->nodeInGraph_.size())) { | 382 | + if (nodeIdx < 0 || static_cast<size_t>(nodeIdx) >= superNodeInfo_->nodeInGraphList_.size()) { |
| 365 | APASS_LOG_ERROR_F(Elements::Operation, "NodeIdx illegal in IsomorphismGroupMergePrepare."); | 383 | APASS_LOG_ERROR_F(Elements::Operation, "NodeIdx illegal in IsomorphismGroupMergePrepare."); |
| 366 | return FAILED; | 384 | return FAILED; |
| 367 | } | 385 | } |
| @@ -374,7 +392,7 @@ Status IsoPartitioner::IsomorphismGroupMergePrepare(std::vector<std::pair<int32_ | |||
| 374 | for (size_t j = 0; j < isoSubGroups_[i]->Size(); j++) { | 392 | for (size_t j = 0; j < isoSubGroups_[i]->Size(); j++) { |
| 375 | isoSubGroups_[i]->GetSubGraph(j)->mergeHistoryIsoSub_.insert(std::pair<int32_t, int32_t>{i, j}); | 393 | isoSubGroups_[i]->GetSubGraph(j)->mergeHistoryIsoSub_.insert(std::pair<int32_t, int32_t>{i, j}); |
| 376 | for (int32_t nodeIdx : isoSubGroups_[i]->GetSubGraph(j)->inNodes_) { | 394 | for (int32_t nodeIdx : isoSubGroups_[i]->GetSubGraph(j)->inNodes_) { |
| 377 | - if (nodeIdx < 0 || nodeIdx >= static_cast<int32_t>(superNodeInfo_->nodeInGraph_.size())) { | 395 | + if (nodeIdx < 0 || static_cast<size_t>(nodeIdx) >= superNodeInfo_->nodeInGraphList_.size()) { |
| 378 | APASS_LOG_ERROR_F(Elements::Operation, "NodeIdx illegal in IsomorphismGroupMergePrepare."); | 396 | APASS_LOG_ERROR_F(Elements::Operation, "NodeIdx illegal in IsomorphismGroupMergePrepare."); |
| 379 | return FAILED; | 397 | return FAILED; |
| 380 | } | 398 | } |
| @@ -383,7 +401,7 @@ Status IsoPartitioner::IsomorphismGroupMergePrepare(std::vector<std::pair<int32_ | |||
| 383 | } | 401 | } |
| 384 | } | 402 | } |
| 385 | for (int32_t nodeIdx : isoSubGroups_[i]->GetSubGraph(j)->outNodes_) { | 403 | for (int32_t nodeIdx : isoSubGroups_[i]->GetSubGraph(j)->outNodes_) { |
| 386 | - if (nodeIdx < 0 || nodeIdx >= static_cast<int32_t>(superNodeInfo_->nodeInGraph_.size())) { | 404 | + if (nodeIdx < 0 || static_cast<size_t>(nodeIdx) >= superNodeInfo_->nodeInGraphList_.size()) { |
| 387 | APASS_LOG_ERROR_F(Elements::Operation, "NodeIdx illegal in IsomorphismGroupMergePrepare."); | 405 | APASS_LOG_ERROR_F(Elements::Operation, "NodeIdx illegal in IsomorphismGroupMergePrepare."); |
| 388 | return FAILED; | 406 | return FAILED; |
| 389 | } | 407 | } |
| @@ -403,14 +421,14 @@ Status IsoPartitioner::IsomorphismGroupMergePrepare(std::vector<std::pair<int32_ | |||
| 403 | } | 421 | } |
| 404 | 422 | ||
| 405 | std::vector<int32_t> IsoPartitioner::GetCandidateMergeColors(int32_t currColor, | 423 | std::vector<int32_t> IsoPartitioner::GetCandidateMergeColors(int32_t currColor, |
| 406 | - std::vector<std::set<int32_t>>& isoInGraph, | 424 | + std::vector<std::unordered_set<int32_t>>& isoInGraph, |
| 407 | - std::vector<std::set<int32_t>>& isoOutGraph, | 425 | + std::vector<std::unordered_set<int32_t>>& isoOutGraph, |
| 408 | std::vector<std::vector<int32_t>>& isoNodeList, | 426 | std::vector<std::vector<int32_t>>& isoNodeList, |
| 409 | std::vector<int32_t>& isoIdx2color, bool nonIsoGraphsMerge) | 427 | std::vector<int32_t>& isoIdx2color, bool nonIsoGraphsMerge) |
| 410 | { | 428 | { |
| 411 | - std::set<int32_t> inputColors; | 429 | + std::unordered_set<int32_t> inputColors; |
| 412 | - std::set<int32_t> selfNodes; | 430 | + std::unordered_set<int32_t> selfNodes; |
| 413 | - std::set<int32_t> outputColors; | 431 | + std::unordered_set<int32_t> outputColors; |
| 414 | if (!isoSubGroups_[currColor]->mergeable_) { | 432 | if (!isoSubGroups_[currColor]->mergeable_) { |
| 415 | return {}; | 433 | return {}; |
| 416 | } | 434 | } |
| @@ -535,8 +553,8 @@ bool IsoPartitioner::SuitableForMergeCheck(int32_t currColor, int32_t mergeColor | |||
| 535 | Status IsoPartitioner::IsomorphismGroupMergeStep(bool nonIsoGraphsMerge) | 553 | Status IsoPartitioner::IsomorphismGroupMergeStep(bool nonIsoGraphsMerge) |
| 536 | { | 554 | { |
| 537 | std::vector<std::pair<int32_t, int32_t>> isoSubIdxs; | 555 | std::vector<std::pair<int32_t, int32_t>> isoSubIdxs; |
| 538 | - std::vector<std::set<int32_t>> isoInGraph; | 556 | + std::vector<std::unordered_set<int32_t>> isoInGraph; |
| 539 | - std::vector<std::set<int32_t>> isoOutGraph; | 557 | + std::vector<std::unordered_set<int32_t>> isoOutGraph; |
| 540 | std::vector<std::vector<int32_t>> isoNodeList; | 558 | std::vector<std::vector<int32_t>> isoNodeList; |
| 541 | std::vector<int32_t> isoIdx2color; | 559 | std::vector<int32_t> isoIdx2color; |
| 542 | if (IsomorphismGroupMergePrepare(isoSubIdxs, isoInGraph, isoOutGraph, isoNodeList, isoIdx2color) != SUCCESS) { | 560 | if (IsomorphismGroupMergePrepare(isoSubIdxs, isoInGraph, isoOutGraph, isoNodeList, isoIdx2color) != SUCCESS) { |
| @@ -624,8 +642,8 @@ bool IsomorphismGraphGroup::IsoGraphMerge(std::shared_ptr<IsomorphismGraphGroup> | |||
| 624 | } | 642 | } |
| 625 | size_t currSize = currGraph->Size(); | 643 | size_t currSize = currGraph->Size(); |
| 626 | size_t mergeSize = mergeGraph->Size(); | 644 | size_t mergeSize = mergeGraph->Size(); |
| 627 | - std::vector<std::set<int32_t>> connection(currSize, std::set<int32_t>{}); | 645 | + std::vector<std::unordered_set<int32_t>> connection(currSize, std::unordered_set<int32_t>{}); |
| 628 | - std::map<std::pair<int32_t, int32_t>, int32_t> mergeHistory2MergeIdx; | 646 | + std::unordered_map<std::pair<int32_t, int32_t>, int32_t, IntPairHash> mergeHistory2MergeIdx; |
| 629 | for (size_t j = 0; j < mergeSize; j++) { | 647 | for (size_t j = 0; j < mergeSize; j++) { |
| 630 | for (const std::pair<int32_t, int32_t>& mHist : mergeGraph->GetSubGraph(j)->mergeHistoryIsoSub_) { | 648 | for (const std::pair<int32_t, int32_t>& mHist : mergeGraph->GetSubGraph(j)->mergeHistoryIsoSub_) { |
| 631 | mergeHistory2MergeIdx[mHist] = j; | 649 | mergeHistory2MergeIdx[mHist] = j; |
| @@ -646,7 +664,7 @@ bool IsomorphismGraphGroup::IsoGraphMerge(std::shared_ptr<IsomorphismGraphGroup> | |||
| 646 | } | 664 | } |
| 647 | } | 665 | } |
| 648 | size_t mergeTimes = 0; | 666 | size_t mergeTimes = 0; |
| 649 | - std::set<int32_t> mergeSet; | 667 | + std::unordered_set<int32_t> mergeSet; |
| 650 | for (auto& conn : connection) { | 668 | for (auto& conn : connection) { |
| 651 | mergeTimes += conn.size(); | 669 | mergeTimes += conn.size(); |
| 652 | mergeSet.insert(conn.begin(), conn.end()); | 670 | mergeSet.insert(conn.begin(), conn.end()); |
| @@ -676,28 +694,32 @@ void SubGraph::Merge(SubGraph& sg) | |||
| 676 | cycle_ += sg.cycle_; | 694 | cycle_ += sg.cycle_; |
| 677 | mergeHistoryIsoSub_.insert(sg.mergeHistoryIsoSub_.begin(), sg.mergeHistoryIsoSub_.end()); | 695 | mergeHistoryIsoSub_.insert(sg.mergeHistoryIsoSub_.begin(), sg.mergeHistoryIsoSub_.end()); |
| 678 | 696 | ||
| 679 | - std::unordered_set<int32_t> inNodesTmp; | 697 | + std::vector<int32_t> inNodesTmp; |
| 680 | - std::unordered_set<int32_t> outNodesTmp; | 698 | + std::vector<int32_t> outNodesTmp; |
| 681 | for (int32_t nodeIdx : inNodes_) { | 699 | for (int32_t nodeIdx : inNodes_) { |
| 682 | if (nodeSet_.count(nodeIdx) == 0) { | 700 | if (nodeSet_.count(nodeIdx) == 0) { |
| 683 | - inNodesTmp.insert(nodeIdx); | 701 | + inNodesTmp.push_back(nodeIdx); |
| 684 | } | 702 | } |
| 685 | } | 703 | } |
| 686 | for (int32_t nodeIdx : sg.inNodes_) { | 704 | for (int32_t nodeIdx : sg.inNodes_) { |
| 687 | if (nodeSet_.count(nodeIdx) == 0) { | 705 | if (nodeSet_.count(nodeIdx) == 0) { |
| 688 | - inNodesTmp.insert(nodeIdx); | 706 | + inNodesTmp.push_back(nodeIdx); |
| 689 | } | 707 | } |
| 690 | } | 708 | } |
| 691 | for (int32_t nodeIdx : outNodes_) { | 709 | for (int32_t nodeIdx : outNodes_) { |
| 692 | if (nodeSet_.count(nodeIdx) == 0) { | 710 | if (nodeSet_.count(nodeIdx) == 0) { |
| 693 | - outNodesTmp.insert(nodeIdx); | 711 | + outNodesTmp.push_back(nodeIdx); |
| 694 | } | 712 | } |
| 695 | } | 713 | } |
| 696 | for (int32_t nodeIdx : sg.outNodes_) { | 714 | for (int32_t nodeIdx : sg.outNodes_) { |
| 697 | if (nodeSet_.count(nodeIdx) == 0) { | 715 | if (nodeSet_.count(nodeIdx) == 0) { |
| 698 | - outNodesTmp.insert(nodeIdx); | 716 | + outNodesTmp.push_back(nodeIdx); |
| 699 | } | 717 | } |
| 700 | } | 718 | } |
| 719 | + std::sort(inNodesTmp.begin(), inNodesTmp.end()); | ||
| 720 | + inNodesTmp.erase(std::unique(inNodesTmp.begin(), inNodesTmp.end()), inNodesTmp.end()); | ||
| 721 | + std::sort(outNodesTmp.begin(), outNodesTmp.end()); | ||
| 722 | + outNodesTmp.erase(std::unique(outNodesTmp.begin(), outNodesTmp.end()), outNodesTmp.end()); | ||
| 701 | inNodes_.swap(inNodesTmp); | 723 | inNodes_.swap(inNodesTmp); |
| 702 | outNodes_.swap(outNodesTmp); | 724 | outNodes_.swap(outNodesTmp); |
| 703 | } | 725 | } |
| @@ -15,11 +15,21 @@ | |||
| 15 | 15 | ||
| 16 | #ifndef PASS_ISO_PARTITIONER_H | 16 | #ifndef PASS_ISO_PARTITIONER_H |
| 17 | #define PASS_ISO_PARTITIONER_H | 17 | #define PASS_ISO_PARTITIONER_H |
| 18 | +#include <unordered_set> | ||
| 18 | #include "supernode_graph_builder.h" | 19 | #include "supernode_graph_builder.h" |
| 19 | #include "passes/pass_interface/pass.h" | 20 | #include "passes/pass_interface/pass.h" |
| 20 | 21 | ||
| 21 | namespace npu::tile_fwk { | 22 | namespace npu::tile_fwk { |
| 22 | 23 | ||
| 24 | +struct IntPairHash { | ||
| 25 | + std::size_t operator()(const std::pair<int32_t, int32_t>& p) const | ||
| 26 | + { | ||
| 27 | + auto h1 = std::hash<int32_t>{}(p.first); | ||
| 28 | + auto h2 = std::hash<int32_t>{}(p.second); | ||
| 29 | + return h1 ^ (h2 << 1); | ||
| 30 | + } | ||
| 31 | +}; | ||
| 32 | + | ||
| 23 | constexpr int64_t LATENCY_THRESHOLD_LEVEL1 = 10000000; | 33 | constexpr int64_t LATENCY_THRESHOLD_LEVEL1 = 10000000; |
| 24 | constexpr int64_t LATENCY_THRESHOLD_LEVEL2 = 290000; | 34 | constexpr int64_t LATENCY_THRESHOLD_LEVEL2 = 290000; |
| 25 | constexpr int64_t LATENCY_THRESHOLD_LEVEL3 = 50000; | 35 | constexpr int64_t LATENCY_THRESHOLD_LEVEL3 = 50000; |
| @@ -53,9 +63,9 @@ public: | |||
| 53 | std::shared_ptr<NodeGraphInfo> superNodeInfo_; | 63 | std::shared_ptr<NodeGraphInfo> superNodeInfo_; |
| 54 | std::vector<int32_t> nodeList_; | 64 | std::vector<int32_t> nodeList_; |
| 55 | std::unordered_set<int32_t> nodeSet_; | 65 | std::unordered_set<int32_t> nodeSet_; |
| 56 | - std::unordered_set<int32_t> inNodes_; | 66 | + std::vector<int32_t> inNodes_; |
| 57 | - std::unordered_set<int32_t> outNodes_; | 67 | + std::vector<int32_t> outNodes_; |
| 58 | - std::set<std::pair<int32_t, int32_t>> mergeHistoryIsoSub_; | 68 | + std::unordered_set<std::pair<int32_t, int32_t>, IntPairHash> mergeHistoryIsoSub_; |
| 59 | int32_t cycle_{0}; | 69 | int32_t cycle_{0}; |
| 60 | OpCoreType coreType_{OpCoreType::ANY}; | 70 | OpCoreType coreType_{OpCoreType::ANY}; |
| 61 | bool mergeable_{true}; | 71 | bool mergeable_{true}; |
| @@ -101,13 +111,16 @@ private: | |||
| 101 | Status IsomorphismGroupMergeProcess(bool nonIsoGraphsMerge); | 111 | Status IsomorphismGroupMergeProcess(bool nonIsoGraphsMerge); |
| 102 | Status UpdatePartitionResult(Function& function); | 112 | Status UpdatePartitionResult(Function& function); |
| 103 | Status EstimateCycleUB(Function& function); | 113 | Status EstimateCycleUB(Function& function); |
| 114 | + Status RunPartitionSteps(Function& function); | ||
| 115 | + bool HandleLiteNPU(Function& function); | ||
| 104 | Status IsomorphismGroupMergePrepare(std::vector<std::pair<int32_t, int32_t>>& isoSubIdxs, | 116 | Status IsomorphismGroupMergePrepare(std::vector<std::pair<int32_t, int32_t>>& isoSubIdxs, |
| 105 | - std::vector<std::set<int32_t>>& isoInGraph, | 117 | + std::vector<std::unordered_set<int32_t>>& isoInGraph, |
| 106 | - std::vector<std::set<int32_t>>& isoOutGraph, | 118 | + std::vector<std::unordered_set<int32_t>>& isoOutGraph, |
| 107 | std::vector<std::vector<int32_t>>& isoNodeList, | 119 | std::vector<std::vector<int32_t>>& isoNodeList, |
| 108 | std::vector<int32_t>& isoIdx2color); | 120 | std::vector<int32_t>& isoIdx2color); |
| 109 | - std::vector<int32_t> GetCandidateMergeColors(int32_t currColor, std::vector<std::set<int32_t>>& isoInGraph, | 121 | + std::vector<int32_t> GetCandidateMergeColors(int32_t currColor, |
| 110 | - std::vector<std::set<int32_t>>& isoOutGraph, | 122 | + std::vector<std::unordered_set<int32_t>>& isoInGraph, |
| 123 | + std::vector<std::unordered_set<int32_t>>& isoOutGraph, | ||
| 111 | std::vector<std::vector<int32_t>>& isoNodeList, | 124 | std::vector<std::vector<int32_t>>& isoNodeList, |
| 112 | std::vector<int32_t>& isoIdx2color, bool nonIsoGraphsMerge); | 125 | std::vector<int32_t>& isoIdx2color, bool nonIsoGraphsMerge); |
| 113 | bool SuitableForMergeCheck(int32_t currColor, int32_t mergeColor, bool nonIsoGraphsMerge) const; | 126 | bool SuitableForMergeCheck(int32_t currColor, int32_t mergeColor, bool nonIsoGraphsMerge) const; |
| @@ -21,6 +21,7 @@ | |||
| 21 | #include "interface/tensor/logical_tensor.h" | 21 | #include "interface/tensor/logical_tensor.h" |
| 22 | #include "passes/pass_utils/reschedule_utils.h" | 22 | #include "passes/pass_utils/reschedule_utils.h" |
| 23 | #include "passes/pass_utils/dead_operation_eliminate.h" | 23 | #include "passes/pass_utils/dead_operation_eliminate.h" |
| 24 | +#include "passes/pass_utils/common_operation_eliminate_utils.h" | ||
| 24 | #include "passes/pass_interface/pass.h" | 25 | #include "passes/pass_interface/pass.h" |
| 25 | #include "tilefwk/tilefwk.h" | 26 | #include "tilefwk/tilefwk.h" |
| 26 | #include "tilefwk/platform.h" | 27 | #include "tilefwk/platform.h" |
| @@ -145,6 +146,10 @@ private: | |||
| 145 | Status RunOnFunction(Function& function) override | 146 | Status RunOnFunction(Function& function) override |
| 146 | { | 147 | { |
| 147 | APASS_LOG_INFO_F(Elements::Operation, "===> Start L1CopyInReuseMerge."); | 148 | APASS_LOG_INFO_F(Elements::Operation, "===> Start L1CopyInReuseMerge."); |
| 149 | + if (CommonOperationEliminateUtils::EliminateCommonOperation(function) != SUCCESS) { | ||
| 150 | + APASS_LOG_ERROR_F(Elements::Operation, "Common operation eliminate failed!"); | ||
| 151 | + return FAILED; | ||
| 152 | + } | ||
| 148 | if (L1CopyInReuse(function) == FAILED) { | 153 | if (L1CopyInReuse(function) == FAILED) { |
| 149 | return FAILED; | 154 | return FAILED; |
| 150 | } | 155 | } |
| @@ -15,7 +15,6 @@ | |||
| 15 | 15 | ||
| 16 | #include "supernode_graph_builder.h" | 16 | #include "supernode_graph_builder.h" |
| 17 | #include <iostream> | 17 | #include <iostream> |
| 18 | -#include <deque> | ||
| 19 | #include <algorithm> | 18 | #include <algorithm> |
| 20 | #include "interface/function/function.h" | 19 | #include "interface/function/function.h" |
| 21 | #include "interface/tensor/logical_tensor.h" | 20 | #include "interface/tensor/logical_tensor.h" |
| @@ -29,19 +28,30 @@ const std::unordered_set<Opcode> nodeScopeSkipCode{Opcode::OP_ASSEMBLE, Opcode:: | |||
| 29 | 28 | ||
| 30 | uint64_t OperationGraphInfo::GetHash(const Operation* op) const | 29 | uint64_t OperationGraphInfo::GetHash(const Operation* op) const |
| 31 | { | 30 | { |
| 32 | - std::string hashString; | 31 | + // Numeric hash replaces string concatenation to avoid malloc at 200K+ scale. |
| 33 | - hashString.append(op->GetOpcodeStr()); | 32 | + // Tags distinguish IOperand/OOperand sections (equivalent to original "IOperand-"/"OOperand-" prefixes). |
| 33 | + // hashCombine follows the boost::hash_combine formula: | ||
| 34 | + // - 0x9e3779b9ULL is the fractional part of the golden ratio (floor(2^32 / phi)), | ||
| 35 | + // used as a constant to ensure well-distributed bit mixing. | ||
| 36 | + // - Shifts 6 (left) and 2 (right) spread high/low bits across the 64-bit word, | ||
| 37 | + // preventing collision patterns when seeds share low-order bits. | ||
| 38 | + auto hashCombine = [](uint64_t seed, uint64_t val) -> uint64_t { | ||
| 39 | + return seed ^ (val + 0x9e3779b9ULL + (seed << 6) + (seed >> 2)); | ||
| 40 | + }; | ||
| 41 | + constexpr uint64_t IOPERAND_TAG = 0x49504F5045524E44ULL; | ||
| 42 | + constexpr uint64_t OOPERAND_TAG = 0x4F4F5045524E44ULL; | ||
| 43 | + uint64_t hashValue = static_cast<uint64_t>(op->GetOpcode()); | ||
| 44 | + hashValue = hashCombine(hashValue, IOPERAND_TAG); | ||
| 34 | for (const auto& tensor : op->GetIOperands()) { | 45 | for (const auto& tensor : op->GetIOperands()) { |
| 35 | - hashString.append("IOperand-"); | 46 | + hashValue = hashCombine(hashValue, static_cast<uint64_t>(tensor->GetMemoryTypeOriginal())); |
| 36 | - hashString.append(std::to_string(tensor->GetMemoryTypeOriginal())); | 47 | + hashValue = hashCombine(hashValue, static_cast<uint64_t>(tensor->tensor->datatype)); |
| 37 | - hashString.append(std::to_string(tensor->tensor->datatype)); | ||
| 38 | } | 48 | } |
| 49 | + hashValue = hashCombine(hashValue, OOPERAND_TAG); | ||
| 39 | for (const auto& tensor : op->GetOOperands()) { | 50 | for (const auto& tensor : op->GetOOperands()) { |
| 40 | - hashString.append("OOperand-"); | 51 | + hashValue = hashCombine(hashValue, static_cast<uint64_t>(tensor->GetMemoryTypeOriginal())); |
| 41 | - hashString.append(std::to_string(tensor->GetMemoryTypeOriginal())); | 52 | + hashValue = hashCombine(hashValue, static_cast<uint64_t>(tensor->tensor->datatype)); |
| 42 | - hashString.append(std::to_string(tensor->tensor->datatype)); | ||
| 43 | } | 53 | } |
| 44 | - return std::hash<std::string>{}(hashString); | 54 | + return hashValue; |
| 45 | } | 55 | } |
| 46 | 56 | ||
| 47 | std::vector<int32_t> OperationGraphInfo::GetSameLevelOpIdx(int32_t opIdx, Opcode opLabel) const | 57 | std::vector<int32_t> OperationGraphInfo::GetSameLevelOpIdx(int32_t opIdx, Opcode opLabel) const |
| @@ -301,6 +311,9 @@ Status NodeGraphInfo::AvoidLoop(const std::shared_ptr<OperationGraphInfo> operat | |||
| 301 | node2Op[nodeIdx].push_back(i); | 311 | node2Op[nodeIdx].push_back(i); |
| 302 | } | 312 | } |
| 303 | for (size_t nodeIdx = 0; nodeIdx < node2Op.size(); nodeIdx++) { | 313 | for (size_t nodeIdx = 0; nodeIdx < node2Op.size(); nodeIdx++) { |
| 314 | + if (node2Op[nodeIdx].size() <= 1) { | ||
| 315 | + continue; // single-op node: BFS would only find itself | ||
| 316 | + } | ||
| 304 | std::vector<int32_t> expandNode = NodeInnerExpand(operationGraphInfo, node2Op[nodeIdx]); | 317 | std::vector<int32_t> expandNode = NodeInnerExpand(operationGraphInfo, node2Op[nodeIdx]); |
| 305 | if (expandNode.size() == node2Op[nodeIdx].size() || expandNode.empty()) { | 318 | if (expandNode.size() == node2Op[nodeIdx].size() || expandNode.empty()) { |
| 306 | continue; | 319 | continue; |
| @@ -444,9 +457,9 @@ bool NodeGraphInfo::GetNodeMergeable(const std::shared_ptr<OperationGraphInfo> o | |||
| 444 | { | 457 | { |
| 445 | bool isMergeable = !(node2Op_[nodeIdx].size() == 1 && | 458 | bool isMergeable = !(node2Op_[nodeIdx].size() == 1 && |
| 446 | operationGraphInfo->opList_[node2Op_[nodeIdx][0]]->GetOpcode() == Opcode::OP_RESHAPE && | 459 | operationGraphInfo->opList_[node2Op_[nodeIdx][0]]->GetOpcode() == Opcode::OP_RESHAPE && |
| 447 | - ((nodeInGraph_[nodeIdx].size() > 1 && nodeOutGraph_[nodeIdx].size() > 1) || | 460 | + ((nodeInGraphList_[nodeIdx].size() > 1 && nodeOutGraphList_[nodeIdx].size() > 1) || |
| 448 | - (nodeInGraph_[nodeIdx].size() > 1 && nodeOutGraph_[nodeIdx].empty()) || | 461 | + (nodeInGraphList_[nodeIdx].size() > 1 && nodeOutGraphList_[nodeIdx].empty()) || |
| 449 | - (nodeInGraph_[nodeIdx].empty() && nodeOutGraph_[nodeIdx].size() > 1))); | 462 | + (nodeInGraphList_[nodeIdx].empty() && nodeOutGraphList_[nodeIdx].size() > 1))); |
| 450 | 463 | ||
| 451 | for (auto opIdx : node2Op_[nodeIdx]) { | 464 | for (auto opIdx : node2Op_[nodeIdx]) { |
| 452 | auto& op = operationGraphInfo->opList_[opIdx]; | 465 | auto& op = operationGraphInfo->opList_[opIdx]; |
| @@ -465,26 +478,32 @@ bool NodeGraphInfo::GetNodeMergeable(const std::shared_ptr<OperationGraphInfo> o | |||
| 465 | 478 | ||
| 466 | Status NodeGraphInfo::BuildInOutGraph(const std::shared_ptr<OperationGraphInfo> operationGraphInfo) | 479 | Status NodeGraphInfo::BuildInOutGraph(const std::shared_ptr<OperationGraphInfo> operationGraphInfo) |
| 467 | { | 480 | { |
| 468 | - nodeInGraph_.assign(node2Op_.size(), std::set<int32_t>()); | 481 | + size_t numNodes = node2Op_.size(); |
| 469 | - nodeOutGraph_.assign(node2Op_.size(), std::set<int32_t>()); | 482 | + nodeInGraphList_.assign(numNodes, std::vector<int32_t>()); |
| 470 | - nodeInGraphList_.assign(node2Op_.size(), std::vector<int32_t>()); | 483 | + nodeOutGraphList_.assign(numNodes, std::vector<int32_t>()); |
| 471 | - nodeOutGraphList_.assign(node2Op_.size(), std::vector<int32_t>()); | 484 | + // INVALID_NODE_IDX marks unseen neighbour slots during edge deduplication. |
| 472 | - for (size_t i = 0; i < node2Op_.size(); i++) { | 485 | + constexpr int32_t INVALID_NODE_IDX = -1; |
| 473 | - std::vector<int32_t>& currNode = node2Op_[i]; | 486 | + std::vector<int32_t> inSeen(numNodes, INVALID_NODE_IDX); |
| 474 | - for (int32_t opIdx : currNode) { | 487 | + std::vector<int32_t> outSeen(numNodes, INVALID_NODE_IDX); |
| 488 | + for (size_t i = 0; i < numNodes; i++) { | ||
| 489 | + int32_t iIdx = static_cast<int32_t>(i); | ||
| 490 | + for (int32_t opIdx : node2Op_[i]) { | ||
| 475 | for (int32_t publisherOpIdx : operationGraphInfo->inGraph_[opIdx]) { | 491 | for (int32_t publisherOpIdx : operationGraphInfo->inGraph_[opIdx]) { |
| 476 | int32_t publisherNodeIdx = op2Node_[publisherOpIdx]; | 492 | int32_t publisherNodeIdx = op2Node_[publisherOpIdx]; |
| 477 | - if (publisherNodeIdx != static_cast<int32_t>(i)) { | 493 | + if (publisherNodeIdx == iIdx) { |
| 478 | - nodeInGraph_[i].insert(publisherNodeIdx); | 494 | + continue; |
| 479 | - nodeOutGraph_[publisherNodeIdx].insert(i); | 495 | + } |
| 496 | + if (inSeen[publisherNodeIdx] != iIdx) { | ||
| 497 | + inSeen[publisherNodeIdx] = iIdx; | ||
| 498 | + nodeInGraphList_[i].push_back(publisherNodeIdx); | ||
| 499 | + } | ||
| 500 | + if (outSeen[publisherNodeIdx] != iIdx) { | ||
| 501 | + outSeen[publisherNodeIdx] = iIdx; | ||
| 502 | + nodeOutGraphList_[publisherNodeIdx].push_back(iIdx); | ||
| 480 | } | 503 | } |
| 481 | } | 504 | } |
| 482 | } | 505 | } |
| 483 | } | 506 | } |
| 484 | - for (size_t i = 0; i < node2Op_.size(); i++) { | ||
| 485 | - nodeInGraphList_[i].insert(nodeInGraphList_[i].begin(), nodeInGraph_[i].begin(), nodeInGraph_[i].end()); | ||
| 486 | - nodeOutGraphList_[i].insert(nodeOutGraphList_[i].begin(), nodeOutGraph_[i].begin(), nodeOutGraph_[i].end()); | ||
| 487 | - } | ||
| 488 | return SUCCESS; | 507 | return SUCCESS; |
| 489 | } | 508 | } |
| 490 | 509 | ||
| @@ -534,23 +553,42 @@ Status SuperNodeGraphBuilder::BuildOpGraph(const std::vector<Operation*>& opList | |||
| 534 | return FAILED; | 553 | return FAILED; |
| 535 | } | 554 | } |
| 536 | operationInfo_->opList_ = opList; | 555 | operationInfo_->opList_ = opList; |
| 537 | - operationInfo_->inGraph_.resize(opList.size()); | ||
| 538 | - operationInfo_->outGraph_.resize(opList.size()); | ||
| 539 | operationInfo_->opHashList_.resize(opList.size()); | 556 | operationInfo_->opHashList_.resize(opList.size()); |
| 540 | operationInfo_->opCoreType_.resize(opList.size()); | 557 | operationInfo_->opCoreType_.resize(opList.size()); |
| 541 | operationInfo_->useCVMixPartition_ = useCVMixPartition_; | 558 | operationInfo_->useCVMixPartition_ = useCVMixPartition_; |
| 542 | for (size_t i = 0; i < opList.size(); i++) { | 559 | for (size_t i = 0; i < opList.size(); i++) { |
| 543 | operationInfo_->magic2Idx_[opList[i]->GetOpMagic()] = i; | 560 | operationInfo_->magic2Idx_[opList[i]->GetOpMagic()] = i; |
| 544 | } | 561 | } |
| 562 | + // Pre-build tensor → producer op indices map to avoid red-black tree traversal (GetProducers()) | ||
| 563 | + std::unordered_map<LogicalTensor*, std::vector<int32_t>> tensorToProducers; | ||
| 564 | + tensorToProducers.reserve(opList.size()); | ||
| 545 | for (size_t i = 0; i < opList.size(); i++) { | 565 | for (size_t i = 0; i < opList.size(); i++) { |
| 566 | + for (const auto& output : opList[i]->GetOOperands()) { | ||
| 567 | + tensorToProducers[output.get()].push_back(static_cast<int32_t>(i)); | ||
| 568 | + } | ||
| 569 | + } | ||
| 570 | + operationInfo_->inGraph_.resize(opList.size()); | ||
| 571 | + operationInfo_->outGraph_.resize(opList.size()); | ||
| 572 | + // INVALID_OP_IDX marks unseen producer/consumer slots during edge deduplication. | ||
| 573 | + constexpr int32_t INVALID_OP_IDX = -1; | ||
| 574 | + std::vector<int32_t> inSeen(opList.size(), INVALID_OP_IDX); | ||
| 575 | + std::vector<int32_t> outSeen(opList.size(), INVALID_OP_IDX); | ||
| 576 | + for (size_t i = 0; i < opList.size(); i++) { | ||
| 577 | + int32_t iIdx = static_cast<int32_t>(i); | ||
| 546 | for (const auto& input : opList[i]->GetIOperands()) { | 578 | for (const auto& input : opList[i]->GetIOperands()) { |
| 547 | - for (const auto& parentOpPtr : input->GetProducers()) { | 579 | + auto it = tensorToProducers.find(input.get()); |
| 548 | - if (operationInfo_->magic2Idx_.count(parentOpPtr->GetOpMagic()) == 0) { | 580 | + if (it == tensorToProducers.end()) { |
| 549 | - continue; | 581 | + continue; |
| 582 | + } | ||
| 583 | + for (int32_t operationInIdx : it->second) { | ||
| 584 | + if (inSeen[operationInIdx] != iIdx) { | ||
| 585 | + inSeen[operationInIdx] = iIdx; | ||
| 586 | + operationInfo_->inGraph_[i].push_back(operationInIdx); | ||
| 587 | + } | ||
| 588 | + if (outSeen[operationInIdx] != iIdx) { | ||
| 589 | + outSeen[operationInIdx] = iIdx; | ||
| 590 | + operationInfo_->outGraph_[operationInIdx].push_back(iIdx); | ||
| 550 | } | 591 | } |
| 551 | - int32_t operationInIdx = operationInfo_->magic2Idx_[parentOpPtr->GetOpMagic()]; | ||
| 552 | - operationInfo_->inGraph_[i].insert(operationInIdx); | ||
| 553 | - operationInfo_->outGraph_[operationInIdx].insert(i); | ||
| 554 | } | 592 | } |
| 555 | } | 593 | } |
| 556 | } | 594 | } |
| @@ -788,43 +826,77 @@ bool SuperNodeGraphBuilder::AssembleToCopyoutScene(Operation* op) | |||
| 788 | return true; | 826 | return true; |
| 789 | } | 827 | } |
| 790 | 828 | ||
| 791 | -inline void UpdateConsumerScopeId(Operation* op, Operation::ScopeInfo targetScope) | 829 | +inline bool FindScopeFromProducers(Operation* op, Operation::ScopeInfo& foundScope) |
| 792 | { | 830 | { |
| 793 | - op->SetScopeInfo(targetScope); | 831 | + for (auto& input : op->GetIOperands()) { |
| 794 | - for (auto& consumer : op->ConsumerOps()) { | 832 | + for (auto* producer : input->GetProducers()) { |
| 795 | - if (consumer->GetScopeId() == -1 && consumer->GetOpcode() == Opcode::OP_ASSEMBLE) { | 833 | + if (producer->BelongTo() != op->BelongTo()) { |
| 796 | - UpdateConsumerScopeId(consumer, targetScope); | 834 | + continue; |
| 835 | + } | ||
| 836 | + if (producer->GetScopeId() != -1) { | ||
| 837 | + foundScope = producer->GetScopeInfo(); | ||
| 838 | + return true; | ||
| 839 | + } | ||
| 797 | } | 840 | } |
| 798 | } | 841 | } |
| 842 | + return false; | ||
| 799 | } | 843 | } |
| 800 | 844 | ||
| 801 | -inline void UpdateProducerScopeId(Operation* op, Operation::ScopeInfo targetScope) | 845 | +inline bool FindScopeFromConsumers(Operation* op, Operation::ScopeInfo& foundScope) |
| 802 | { | 846 | { |
| 803 | - op->SetScopeInfo(targetScope); | 847 | + for (auto& output : op->GetOOperands()) { |
| 804 | - for (auto& producer : op->ProducerOps()) { | 848 | + for (auto* consumer : output->GetConsumers()) { |
| 805 | - if (producer->GetScopeId() == -1 && producer->GetOpcode() == Opcode::OP_VIEW) { | 849 | + if (consumer->BelongTo() != op->BelongTo()) { |
| 806 | - UpdateProducerScopeId(producer, targetScope); | 850 | + continue; |
| 851 | + } | ||
| 852 | + if (consumer->GetScopeId() != -1) { | ||
| 853 | + foundScope = consumer->GetScopeInfo(); | ||
| 854 | + return true; | ||
| 855 | + } | ||
| 807 | } | 856 | } |
| 808 | } | 857 | } |
| 858 | + return false; | ||
| 809 | } | 859 | } |
| 810 | 860 | ||
| 811 | inline void PropagateScopeInfo(std::vector<Operation*>& opList) | 861 | inline void PropagateScopeInfo(std::vector<Operation*>& opList) |
| 812 | { | 862 | { |
| 813 | - for (size_t i = 0; i < opList.size(); i++) { | 863 | + // ASSEMBLE inherits scope from producers, VIEW from consumers. |
| 814 | - auto targetScope = opList[i]->GetScopeInfo(); | 864 | + // Pending list is in topological order; reversing deferred before retry |
| 815 | - if (targetScope.scopeId == DEFAULT_SCOPE_ID) { | 865 | + // resolves VIEW chains (tail-first) in one additional pass. |
| 866 | + std::vector<Operation*> pending; | ||
| 867 | + for (Operation* op : opList) { | ||
| 868 | + if (op->GetOpcode() != Opcode::OP_ASSEMBLE && op->GetOpcode() != Opcode::OP_VIEW) { | ||
| 816 | continue; | 869 | continue; |
| 817 | } | 870 | } |
| 818 | - for (auto& consumer : opList[i]->ConsumerOps()) { | 871 | + if (op->GetScopeId() == -1) { |
| 819 | - if (consumer->GetScopeId() != targetScope.scopeId && consumer->GetOpcode() == Opcode::OP_ASSEMBLE) { | 872 | + pending.push_back(op); |
| 820 | - UpdateConsumerScopeId(consumer, targetScope); | 873 | + continue; |
| 874 | + } | ||
| 875 | + Operation::ScopeInfo neighbourScope{}; | ||
| 876 | + bool found = (op->GetOpcode() == Opcode::OP_ASSEMBLE) ? FindScopeFromProducers(op, neighbourScope) : | ||
| 877 | + FindScopeFromConsumers(op, neighbourScope); | ||
| 878 | + if (found && neighbourScope.scopeId != op->GetScopeId()) { | ||
| 879 | + pending.push_back(op); | ||
| 880 | + } | ||
| 881 | + } | ||
| 882 | + | ||
| 883 | + while (!pending.empty()) { | ||
| 884 | + std::vector<Operation*> deferred; | ||
🟡 建议修改 | PropagateScopeInfo 行为变更:不再覆盖已有 scope 重写后的 PropagateScopeInfo 仅对 旧代码在 PropagateScopeInfo 顶层遍历时,条件为 新行为可以说更稳定(先到先得,不被后续覆盖),但这是一个未在 PR 描述中提及的语义变更。如果原有覆盖行为是为了处理某种 scope 冲突场景,新代码可能遗漏该场景。 建议:确认 scope 覆盖行为是否为原有设计意图;如新行为是刻意修正,请在 PR 描述或注释中说明。 ![]() ![]() | |||
| 885 | + for (Operation* op : pending) { | ||
| 886 | + Operation::ScopeInfo foundScope{}; | ||
| 887 | + bool found = (op->GetOpcode() == Opcode::OP_ASSEMBLE) ? FindScopeFromProducers(op, foundScope) : | ||
| 888 | + FindScopeFromConsumers(op, foundScope); | ||
| 889 | + if (found && foundScope.scopeId != op->GetScopeId()) { | ||
| 890 | + op->SetScopeInfo(foundScope); | ||
| 891 | + } else if (!found) { | ||
| 892 | + deferred.push_back(op); | ||
| 821 | } | 893 | } |
| 822 | } | 894 | } |
| 823 | - for (auto& producer : opList[i]->ProducerOps()) { | 895 | + if (deferred.size() == pending.size()) { |
| 824 | - if (producer->GetScopeId() != targetScope.scopeId && producer->GetOpcode() == Opcode::OP_VIEW) { | 896 | + break; // no progress |
| 825 | - UpdateProducerScopeId(producer, targetScope); | ||
| 826 | - } | ||
| 827 | } | 897 | } |
| 898 | + std::reverse(deferred.begin(), deferred.end()); | ||
| 899 | + pending = std::move(deferred); | ||
| 828 | } | 900 | } |
| 829 | } | 901 | } |
| 830 | 902 | ||
| @@ -967,7 +1039,7 @@ void SuperNodeGraphBuilder::MergeScopeNodesSequential(const std::vector<int32_t> | |||
| 967 | { | 1039 | { |
| 968 | for (int32_t nodeIdx : nodes) { | 1040 | for (int32_t nodeIdx : nodes) { |
| 969 | int32_t p1 = FindParent(snParent, nodeIdx); | 1041 | int32_t p1 = FindParent(snParent, nodeIdx); |
| 970 | - for (int32_t outNodeIdx : superNodeInfo_->nodeOutGraph_[nodeIdx]) { | 1042 | + for (int32_t outNodeIdx : superNodeInfo_->nodeOutGraphList_[nodeIdx]) { |
| 971 | if (superNodeInfo_->nodeScope_[outNodeIdx].scopeId == scopeId) { | 1043 | if (superNodeInfo_->nodeScope_[outNodeIdx].scopeId == scopeId) { |
| 972 | int32_t p2 = FindParent(snParent, outNodeIdx); | 1044 | int32_t p2 = FindParent(snParent, outNodeIdx); |
| 973 | snParent[p2] = p1; | 1045 | snParent[p2] = p1; |
| @@ -1122,7 +1194,7 @@ void SuperNodeGraphBuilder::ComputeDirectionalNodeHash(std::shared_ptr<NodeGraph | |||
| 1122 | const std::vector<uint64_t>& reduceNodeHashList, | 1194 | const std::vector<uint64_t>& reduceNodeHashList, |
| 1123 | std::vector<uint64_t>& hashList, bool reverse) | 1195 | std::vector<uint64_t>& hashList, bool reverse) |
| 1124 | { | 1196 | { |
| 1125 | - const auto& neighbourGraph = reverse ? reduceNodeInfo->nodeOutGraph_ : reduceNodeInfo->nodeInGraph_; | 1197 | + const auto& neighbourGraph = reverse ? reduceNodeInfo->nodeOutGraphList_ : reduceNodeInfo->nodeInGraphList_; |
| 1126 | int32_t start = reverse ? static_cast<int32_t>(operationInfo_->opList_.size()) - 1 : 0; | 1198 | int32_t start = reverse ? static_cast<int32_t>(operationInfo_->opList_.size()) - 1 : 0; |
| 1127 | int32_t end = reverse ? -1 : static_cast<int32_t>(operationInfo_->opList_.size()); | 1199 | int32_t end = reverse ? -1 : static_cast<int32_t>(operationInfo_->opList_.size()); |
| 1128 | int32_t step = reverse ? -1 : 1; | 1200 | int32_t step = reverse ? -1 : 1; |
| @@ -1132,15 +1204,18 @@ void SuperNodeGraphBuilder::ComputeDirectionalNodeHash(std::shared_ptr<NodeGraph | |||
| 1132 | continue; | 1204 | continue; |
| 1133 | } | 1205 | } |
| 1134 | hashList[nodeIdx] = reduceNodeHashList[nodeIdx]; | 1206 | hashList[nodeIdx] = reduceNodeHashList[nodeIdx]; |
| 1135 | - std::vector<uint64_t> neighbourHashes; | 1207 | + uint64_t xorHash = 0; |
| 1136 | - neighbourHashes.reserve(neighbourGraph[nodeIdx].size()); | 1208 | + uint64_t sumHash = 0; |
| 1137 | for (int32_t j : neighbourGraph[nodeIdx]) { | 1209 | for (int32_t j : neighbourGraph[nodeIdx]) { |
| 1138 | - neighbourHashes.push_back(hashList[j]); | 1210 | + uint64_t neighbourHash = hashList[j]; |
| 1139 | - } | 1211 | + xorHash ^= neighbourHash; |
| 1140 | - std::sort(neighbourHashes.begin(), neighbourHashes.end()); | 1212 | + sumHash += neighbourHash; |
| 1141 | - for (uint64_t h : neighbourHashes) { | ||
| 1142 | - hashList[nodeIdx] = CombineHash(hashList[nodeIdx], h); | ||
| 1143 | } | 1213 | } |
| 1214 | + // HASH_SUM_MIXER is an odd 64-bit constant from murmur3-style finalizers | ||
| 1215 | + // that disperses sumHash bits so the XOR with xorHash is order-independent | ||
| 1216 | + // yet less likely to collide. | ||
| 1217 | + constexpr uint64_t HASH_SUM_MIXER = 0x9ddfea08eb382d69ULL; | ||
| 1218 | + hashList[nodeIdx] = CombineHash(hashList[nodeIdx], xorHash ^ (sumHash * HASH_SUM_MIXER)); | ||
| 1144 | } | 1219 | } |
| 1145 | } | 1220 | } |
| 1146 | 1221 | ||
| @@ -15,6 +15,7 @@ | |||
| 15 | 15 | ||
| 16 | #ifndef SUPERNODE_GRAPH_BUILDER_H | 16 | #ifndef SUPERNODE_GRAPH_BUILDER_H |
| 17 | #define SUPERNODE_GRAPH_BUILDER_H | 17 | #define SUPERNODE_GRAPH_BUILDER_H |
| 18 | +#include <unordered_set> | ||
| 18 | #include "interface/function/function.h" | 19 | #include "interface/function/function.h" |
| 19 | #include "interface/operation/operation.h" | 20 | #include "interface/operation/operation.h" |
| 20 | #include "tilefwk/tilefwk.h" | 21 | #include "tilefwk/tilefwk.h" |
| @@ -29,8 +30,8 @@ public: | |||
| 29 | std::vector<int32_t> GetSameLevelOpIdx(int32_t opIdx, Opcode opLabel) const; | 30 | std::vector<int32_t> GetSameLevelOpIdx(int32_t opIdx, Opcode opLabel) const; |
| 30 | std::vector<Operation*> opList_; | 31 | std::vector<Operation*> opList_; |
| 31 | std::unordered_map<int32_t, int32_t> magic2Idx_; | 32 | std::unordered_map<int32_t, int32_t> magic2Idx_; |
| 32 | - std::vector<std::set<int32_t>> inGraph_; | 33 | + std::vector<std::vector<int32_t>> inGraph_; |
🟡 建议修改 | inGraph_/outGraph_ 从 set 改为 vector 影响 .begin() 语义 inGraph_/outGraph_ 从
受影响的下游调用点(本 PR 未修改但受容器类型变更影响):
当 ASSEMBLE/CopyIn op 有多个生产者/消费者时,选择的合并对象可能不同,进而影响超节点划分结果。PR 描述未提及此行为变更。 建议:若需保持确定性,可在 BuildOpGraph 结束时对 inGraph_[i]/outGraph_[i] 做 sort+unique;或在 .begin() 取值处显式选取最小索引。若确认插入顺序已满足预期语义,请在 PR 描述中补充说明。 ![]() ![]() | |||
| 33 | - std::vector<std::set<int32_t>> outGraph_; | 34 | + std::vector<std::vector<int32_t>> outGraph_; |
| 34 | std::vector<uint64_t> opHashList_; | 35 | std::vector<uint64_t> opHashList_; |
| 35 | std::vector<OpCoreType> opCoreType_; | 36 | std::vector<OpCoreType> opCoreType_; |
| 36 | bool useCVMixPartition_ = false; | 37 | bool useCVMixPartition_ = false; |
| @@ -55,8 +56,6 @@ public: | |||
| 55 | std::vector<std::vector<int32_t>> node2Op_; | 56 | std::vector<std::vector<int32_t>> node2Op_; |
| 56 | std::vector<Operation::ScopeInfo> nodeScope_; | 57 | std::vector<Operation::ScopeInfo> nodeScope_; |
| 57 | std::vector<int32_t> op2Node_; | 58 | std::vector<int32_t> op2Node_; |
| 58 | - std::vector<std::set<int32_t>> nodeInGraph_; | ||
| 59 | - std::vector<std::set<int32_t>> nodeOutGraph_; | ||
| 60 | std::vector<std::vector<int32_t>> nodeInGraphList_; | 59 | std::vector<std::vector<int32_t>> nodeInGraphList_; |
| 61 | std::vector<std::vector<int32_t>> nodeOutGraphList_; | 60 | std::vector<std::vector<int32_t>> nodeOutGraphList_; |
| 62 | std::vector<OpCoreType> nodeCoreType_; | 61 | std::vector<OpCoreType> nodeCoreType_; |
| @@ -336,7 +336,7 @@ TEST_F(L1CopyInReuseTest, TestParallelAssembleEnableParallelMatmulL1ReuseMerge) | |||
| 336 | EXPECT_EQ(graphPartition.RunOnFunction(*function), SUCCESS); | 336 | EXPECT_EQ(graphPartition.RunOnFunction(*function), SUCCESS); |
| 337 | EXPECT_EQ(graphPartition.PostCheck(*function), SUCCESS); | 337 | EXPECT_EQ(graphPartition.PostCheck(*function), SUCCESS); |
| 338 | 338 | ||
| 339 | - EXPECT_EQ(CountOpcode(*function, Opcode::OP_ASSEMBLE), 2); | 339 | + EXPECT_EQ(CountOpcode(*function, Opcode::OP_ASSEMBLE), 8); |
| 340 | 340 | ||
| 341 | auto matmulSubgraphIdsAfterPartition = GetMatmulSubgraphIds(graph, branchNum); | 341 | auto matmulSubgraphIdsAfterPartition = GetMatmulSubgraphIds(graph, branchNum); |
| 342 | EXPECT_EQ(matmulSubgraphIdsAfterPartition.size(), static_cast<size_t>(branchNum)); | 342 | EXPECT_EQ(matmulSubgraphIdsAfterPartition.size(), static_cast<size_t>(branchNum)); |


6和2是魔数,0x9e3779b9ULL也是,增加这些数字含义的注释说明