Merged
perf(pass): Use LIGHTWEIGHT sort and skip unnecessary DCE for tile graph passes #5011
CatherieAdd on Jul 22
perf(pass): Use LIGHTWEIGHT sort and skip unnecessary DCE for tile graph passes #5011
Merged
Merged fromCatherie/pyptoperf/lightweight-sort-dceintocann/pyptomaster
Total: 8 files changed+117-90
| @@ -456,11 +456,11 @@ Function::Function(const Program& belongTo, const std::string& funcMagicName, co | |||
| 456 | opSeed_ = FUNCTION_MAX_INCASTS; | 456 | opSeed_ = FUNCTION_MAX_INCASTS; |
| 457 | } | 457 | } |
| 458 | 458 | ||
| 459 | -OperationsViewer Function::Operations(bool sorted) | 459 | +OperationsViewer Function::Operations(bool sorted, SortOperationsMode mode) |
| 460 | { | 460 | { |
| 461 | if (!sorted_ && sorted) { | 461 | if (!sorted_ && sorted) { |
| 462 | sorted_ = true; | 462 | sorted_ = true; |
| 463 | - SortOperations(); | 463 | + SortOperations(mode); |
| 464 | } | 464 | } |
| 465 | return OperationsViewer(operations_, opPosition_); | 465 | return OperationsViewer(operations_, opPosition_); |
| 466 | } | 466 | } |
| @@ -1637,7 +1637,7 @@ unsigned long Function::ComputeHashOrderless() const | |||
| 1637 | return result; | 1637 | return result; |
| 1638 | } | 1638 | } |
| 1639 | 1639 | ||
| 1640 | -void Function::EraseOperations(bool eraseRelatedTensor, bool sorted) | 1640 | +void Function::EraseOperations(bool eraseRelatedTensor, bool sorted, SortOperationsMode mode) |
| 1641 | { | 1641 | { |
| 1642 | std::unordered_set<LogicalTensorPtr> inOutCastSet(inCasts_.begin(), inCasts_.end()); | 1642 | std::unordered_set<LogicalTensorPtr> inOutCastSet(inCasts_.begin(), inCasts_.end()); |
| 1643 | inOutCastSet.insert(outCasts_.begin(), outCasts_.end()); | 1643 | inOutCastSet.insert(outCasts_.begin(), outCasts_.end()); |
| @@ -1669,25 +1669,32 @@ void Function::EraseOperations(bool eraseRelatedTensor, bool sorted) | |||
| 1669 | operations_ = operations; | 1669 | operations_ = operations; |
| 1670 | 1670 | ||
| 1671 | if (eraseRelatedTensor) { | 1671 | if (eraseRelatedTensor) { |
| 1672 | - for (auto tensorPtr : removeCandidiateTensor) { | 1672 | + EraseRelatedTensors(removeCandidiateTensor, removeProducerTensor, inOutCastSet); |
| 1673 | - if (inOutCastSet.count(tensorPtr) != 0) { | 1673 | + } |
| 1674 | - continue; | 1674 | + if (sorted) { |
| 1675 | - } | 1675 | + SortOperations(mode); |
| 1676 | - if (tensorPtr->GetProducers().empty() && tensorPtr->GetConsumers().empty()) { | 1676 | + } |
| 1677 | - GetTensorMap().Erase(tensorPtr); | 1677 | +} |
| 1678 | - } else if (removeProducerTensor.count(tensorPtr) > 0 && tensorPtr->GetProducers().empty()) { | 1678 | + |
| 1679 | - GetTensorMap().Erase(tensorPtr); | 1679 | +void Function::EraseRelatedTensors(const std::unordered_set<std::shared_ptr<LogicalTensor>>& removeCandidateTensor, |
| 1680 | - for (auto& consumer : tensorPtr->GetConsumers()) { | 1680 | + const std::unordered_set<std::shared_ptr<LogicalTensor>>& removeProducerTensor, |
| 1681 | - if (consumer->BelongTo() == this) { | 1681 | + const std::unordered_set<LogicalTensorPtr>& inOutCastSet) |
| 1682 | - consumer->EraseInput(tensorPtr); | 1682 | +{ |
| 1683 | - } | 1683 | + for (auto tensorPtr : removeCandidateTensor) { |
| 1684 | + if (inOutCastSet.count(tensorPtr) != 0) { | ||
| 1685 | + continue; | ||
| 1686 | + } | ||
| 1687 | + if (tensorPtr->GetProducers().empty() && tensorPtr->GetConsumers().empty()) { | ||
| 1688 | + GetTensorMap().Erase(tensorPtr); | ||
| 1689 | + } else if (removeProducerTensor.count(tensorPtr) > 0 && tensorPtr->GetProducers().empty()) { | ||
| 1690 | + GetTensorMap().Erase(tensorPtr); | ||
| 1691 | + for (auto& consumer : tensorPtr->GetConsumers()) { | ||
| 1692 | + if (consumer->BelongTo() == this) { | ||
| 1693 | + consumer->EraseInput(tensorPtr); | ||
| 1684 | } | 1694 | } |
| 1685 | } | 1695 | } |
| 1686 | } | 1696 | } |
| 1687 | } | 1697 | } |
| 1688 | - if (sorted) { | ||
| 1689 | - SortOperations(); | ||
| 1690 | - } | ||
| 1691 | } | 1698 | } |
| 1692 | 1699 | ||
| 1693 | void Function::EraseOperations(const OperationDeleter& deleter) | 1700 | void Function::EraseOperations(const OperationDeleter& deleter) |
| @@ -626,7 +626,7 @@ public: | |||
| 626 | FunctionHash ComputeHash(); | 626 | FunctionHash ComputeHash(); |
| 627 | void BuildTensorMap(); | 627 | void BuildTensorMap(); |
| 628 | std::vector<std::shared_ptr<Operation>> GetSortedOperations() const; | 628 | std::vector<std::shared_ptr<Operation>> GetSortedOperations() const; |
| 629 | - OperationsViewer Operations(bool sorted = true); | 629 | + OperationsViewer Operations(bool sorted = true, SortOperationsMode mode = SortOperationsMode::GENERAL); |
| 630 | OperationsViewer OperationsAfterOOO(); | 630 | OperationsViewer OperationsAfterOOO(); |
| 631 | void RecordOOOSeq(); | 631 | void RecordOOOSeq(); |
| 632 | // 这个LeafOperations写法破坏封装性,但是是针对LeafFunction特有的,后续在Function按类拆分的时候会将其只放到LeafFunction中 | 632 | // 这个LeafOperations写法破坏封装性,但是是针对LeafFunction特有的,后续在Function按类拆分的时候会将其只放到LeafFunction中 |
| @@ -634,7 +634,8 @@ public: | |||
| 634 | void SetProgramOp(const std::vector<OperationPtr>& operations); | 634 | void SetProgramOp(const std::vector<OperationPtr>& operations); |
| 635 | void SortOperations(SortOperationsMode mode = SortOperationsMode::GENERAL); | 635 | void SortOperations(SortOperationsMode mode = SortOperationsMode::GENERAL); |
| 636 | void ScheduleBy(const std::vector<Operation*>& newList, bool needRefresh = false); | 636 | void ScheduleBy(const std::vector<Operation*>& newList, bool needRefresh = false); |
| 637 | - void EraseOperations(bool eraseRelatedTensor = true, bool sorted = true); | 637 | + void EraseOperations(bool eraseRelatedTensor = true, bool sorted = true, |
| 638 | + SortOperationsMode mode = SortOperationsMode::GENERAL); | ||
| 638 | void EraseOperations(const OperationDeleter& deleter); | 639 | void EraseOperations(const OperationDeleter& deleter); |
| 639 | void AddGlobalTensor(std::shared_ptr<LogicalTensor> tensor) { globalTensors_.emplace(tensor); }; | 640 | void AddGlobalTensor(std::shared_ptr<LogicalTensor> tensor) { globalTensors_.emplace(tensor); }; |
| 640 | void AddOperationGroup(std::vector<Operation*> operationGroup); | 641 | void AddOperationGroup(std::vector<Operation*> operationGroup); |
| @@ -1092,6 +1093,9 @@ private: | |||
| 1092 | 1093 | ||
| 1093 | void RefreshOpPosition(); | 1094 | void RefreshOpPosition(); |
| 1094 | auto AnnotateOperation(); | 1095 | auto AnnotateOperation(); |
| 1096 | + void EraseRelatedTensors(const std::unordered_set<std::shared_ptr<LogicalTensor>>& removeCandidateTensor, | ||
| 1097 | + const std::unordered_set<std::shared_ptr<LogicalTensor>>& removeProducerTensor, | ||
| 1098 | + const std::unordered_set<LogicalTensorPtr>& inOutCastSet); | ||
| 1095 | 1099 | ||
| 1096 | void FillOriginInOutCast(std::vector<Operation*>& operationList); | 1100 | void FillOriginInOutCast(std::vector<Operation*>& operationList); |
| 1097 | void SetCallOpSlot(); | 1101 | void SetCallOpSlot(); |
| @@ -140,10 +140,13 @@ Status CommonOperationEliminateUtils::EliminateCommonOperation(Function& functio | |||
| 140 | 140 | ||
| 141 | Status CommonOperationEliminateUtils::Process(Function& function) | 141 | Status CommonOperationEliminateUtils::Process(Function& function) |
| 142 | { | 142 | { |
| 143 | + function.SortOperations(SortOperationsMode::LIGHTWEIGHT); | ||
| 143 | std::vector<LogicalTensorPtr> sequence; | 144 | std::vector<LogicalTensorPtr> sequence; |
| 144 | - auto tensorProducerMap = GetTensorProducers(function, sequence); | 145 | + std::unordered_map<LogicalTensorPtr, std::vector<Operation*>> tensorProducerMap; |
| 146 | + tensorProducerMap = GetTensorProducers(function, sequence); | ||
| 145 | mixSubgraphIds_ = GetMixSubgraphIds(function); | 147 | mixSubgraphIds_ = GetMixSubgraphIds(function); |
| 146 | std::unordered_set<Operation*> cacheProducers; | 148 | std::unordered_set<Operation*> cacheProducers; |
| 149 | + bool anyDeleted = false; | ||
| 147 | for (auto& orderedTensor : sequence) { | 150 | for (auto& orderedTensor : sequence) { |
| 148 | auto& producerGroup = tensorProducerMap[orderedTensor]; | 151 | auto& producerGroup = tensorProducerMap[orderedTensor]; |
| 149 | if (producerGroup.empty() || | 152 | if (producerGroup.empty() || |
| @@ -157,13 +160,16 @@ Status CommonOperationEliminateUtils::Process(Function& function) | |||
| 157 | if (!cacheProducers.count(op)) { | 160 | if (!cacheProducers.count(op)) { |
| 158 | APASS_LOG_DEBUG_F(Elements::Operation, "Operation[%d] was set as deleted.", op->GetOpMagic()); | 161 | APASS_LOG_DEBUG_F(Elements::Operation, "Operation[%d] was set as deleted.", op->GetOpMagic()); |
| 159 | op->SetAsDeleted(); | 162 | op->SetAsDeleted(); |
| 163 | + anyDeleted = true; | ||
| 160 | } | 164 | } |
| 161 | } | 165 | } |
| 162 | } | 166 | } |
| 163 | - function.EraseOperations(); | 167 | + if (anyDeleted) { |
| 164 | - if (DeadOperationEliminator::EliminateDeadOperation(function) != SUCCESS) { | 168 | + function.EraseOperations(true, false); |
| 165 | - APASS_LOG_ERROR_F(Elements::Operation, "Eliminate dead operation failed in CommonOperationEliminateUtils."); | 169 | + if (DeadOperationEliminator::EliminateDeadOperation(function) != SUCCESS) { |
| 166 | - return FAILED; | 170 | + APASS_LOG_ERROR_F(Elements::Operation, "Eliminate dead operation failed in CommonOperationEliminateUtils."); |
| 171 | + return FAILED; | ||
| 172 | + } | ||
| 167 | } | 173 | } |
| 168 | return SUCCESS; | 174 | return SUCCESS; |
| 169 | } | 175 | } |
| @@ -30,7 +30,7 @@ Status DeadOperationEliminator::EliminateDeadOperation(Function& function) | |||
| 30 | // Delete Operation without oOperand | 30 | // Delete Operation without oOperand |
| 31 | void DeadOperationEliminator::EliminateDeadOperationBackward(Function& function) | 31 | void DeadOperationEliminator::EliminateDeadOperationBackward(Function& function) |
| 32 | { | 32 | { |
| 33 | - for (auto& op : function.Operations()) { | 33 | + for (auto& op : function.Operations(false)) { |
| 34 | op.SetAsNotDeleted(); | 34 | op.SetAsNotDeleted(); |
| 35 | } | 35 | } |
| 36 | EliminateOperation(function); | 36 | EliminateOperation(function); |
| @@ -59,7 +59,7 @@ void DeadOperationEliminator::EliminateOperation(Function& function, bool useSor | |||
| 59 | std::queue<Operation*> q; | 59 | std::queue<Operation*> q; |
| 60 | std::unordered_set<Operation*> visited; | 60 | std::unordered_set<Operation*> visited; |
| 61 | std::unordered_set<std::shared_ptr<LogicalTensor>> visitedOperands; | 61 | std::unordered_set<std::shared_ptr<LogicalTensor>> visitedOperands; |
| 62 | - for (auto& op : function.Operations(useSortedOperations)) { | 62 | + for (auto& op : function.Operations(useSortedOperations, SortOperationsMode::LIGHTWEIGHT)) { |
| 63 | bool dontTouch = op.GetBoolAttribute(OpAttributeKey::dontTouch); | 63 | bool dontTouch = op.GetBoolAttribute(OpAttributeKey::dontTouch); |
| 64 | if (dontTouch) { | 64 | if (dontTouch) { |
| 65 | visited.emplace(&op); | 65 | visited.emplace(&op); |
| @@ -88,11 +88,11 @@ void DeadOperationEliminator::EliminateOperation(Function& function, bool useSor | |||
| 88 | q.emplace(producerOp); | 88 | q.emplace(producerOp); |
| 89 | } | 89 | } |
| 90 | } | 90 | } |
| 91 | - for (auto& op : function.Operations(useSortedOperations)) { | 91 | + for (auto& op : function.Operations(false)) { |
| 92 | if (visited.count(&op) == 0) { | 92 | if (visited.count(&op) == 0) { |
| 93 | op.SetAsDeleted(); | 93 | op.SetAsDeleted(); |
| 94 | } | 94 | } |
| 95 | } | 95 | } |
| 96 | - function.EraseOperations(false, sortAfterErase); | 96 | + function.EraseOperations(false, sortAfterErase, SortOperationsMode::LIGHTWEIGHT); |
| 97 | } | 97 | } |
| 98 | } // namespace npu::tile_fwk | 98 | } // namespace npu::tile_fwk |
| @@ -14,7 +14,6 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include "infer_discontinuous_input.h" | 16 | #include "infer_discontinuous_input.h" |
| 17 | -#include <queue> | ||
| 18 | #include "interface/tensor/irbuilder.h" | 17 | #include "interface/tensor/irbuilder.h" |
| 19 | #include "passes/pass_log/pass_log.h" | 18 | #include "passes/pass_log/pass_log.h" |
| 20 | #include "passes/pass_check/infer_discontinuous_input_checker.h" | 19 | #include "passes/pass_check/infer_discontinuous_input_checker.h" |
| @@ -52,8 +51,8 @@ Status InferDiscontinuousInput::RunOnFunction(Function& function) | |||
| 52 | 51 | ||
| 53 | std::vector<std::pair<LogicalTensorPtr, Operation*>> GetInplacedTileTensors(LogicalTensorPtr targetTensor) | 52 | std::vector<std::pair<LogicalTensorPtr, Operation*>> GetInplacedTileTensors(LogicalTensorPtr targetTensor) |
| 54 | { | 53 | { |
| 55 | - std::unordered_set<Opcode> inplaceNodes{Opcode::OP_VIEW, Opcode::OP_ASSEMBLE, Opcode::OP_RESHAPE, | 54 | + static const std::unordered_set<Opcode> inplaceNodes{Opcode::OP_VIEW, Opcode::OP_ASSEMBLE, Opcode::OP_RESHAPE, |
| 56 | - Opcode::OP_INDEX_OUTCAST}; | 55 | + Opcode::OP_INDEX_OUTCAST}; |
| 57 | std::vector<std::pair<LogicalTensorPtr, Operation*>> inplacedTensor; | 56 | std::vector<std::pair<LogicalTensorPtr, Operation*>> inplacedTensor; |
| 58 | for (auto& producer : targetTensor->GetProducers()) { | 57 | for (auto& producer : targetTensor->GetProducers()) { |
| 59 | if (inplaceNodes.count(producer->GetOpcode()) == 0) { | 58 | if (inplaceNodes.count(producer->GetOpcode()) == 0) { |
| @@ -244,44 +243,30 @@ std::vector<std::pair<LogicalTensorPtr, Operation*>> InferDiscontinuousInput::Fi | |||
| 244 | 243 | ||
| 245 | void InferDiscontinuousInput::Init(Function& function) | 244 | void InferDiscontinuousInput::Init(Function& function) |
| 246 | { | 245 | { |
| 247 | - auto opList = function.Operations().DuplicatedOpList(); | 246 | + auto opList = function.Operations(true, SortOperationsMode::LIGHTWEIGHT).DuplicatedOpList(); |
| 248 | - for (size_t i = 0; i < opList.size(); ++i) { | 247 | + tensorProducers_.reserve(opList.size()); |
| 249 | - opInputDegree_.emplace(opList[i], opList[i]->ProducerOps().size()); | 248 | + for (auto currOp : opList) { |
| 250 | - for (auto outTensor : opList[i]->GetOOperands()) { | 249 | + for (auto outTensor : currOp->GetOOperands()) { |
| 251 | tensorProducers_[outTensor] = outTensor->GetProducers().size(); | 250 | tensorProducers_[outTensor] = outTensor->GetProducers().size(); |
| 252 | } | 251 | } |
| 253 | } | 252 | } |
| 254 | } | 253 | } |
| 255 | 254 | ||
| 256 | -// 从INCAST出发,按DFS做前向推导 | 255 | +// 从INCAST出发,按前向推导 |
| 257 | Status InferDiscontinuousInput::InferFromIncast(Function& function) | 256 | Status InferDiscontinuousInput::InferFromIncast(Function& function) |
| 258 | { | 257 | { |
| 259 | - std::queue<Operation*> procOpQueue; | 258 | + auto opList = function.Operations(true, SortOperationsMode::LIGHTWEIGHT).DuplicatedOpList(); |
| 260 | - for (auto& opInputDegree : opInputDegree_) { | 259 | + insertCopys_.reserve(opList.size()); |
| 261 | - if (opInputDegree.second == 0) { | 260 | + for (auto currOp : opList) { |
| 262 | - procOpQueue.push(opInputDegree.first); | 261 | + for (auto& outputTensor : currOp->GetOOperands()) { |
| 263 | - } | 262 | + auto& producerCnt = tensorProducers_[outputTensor]; |
| 264 | - } | 263 | + producerCnt--; |
| 265 | - std::set<Operation*> visitedOps; | 264 | + if (producerCnt != 0) { |
| 266 | - while (!procOpQueue.empty()) { | ||
| 267 | - auto currentOp = procOpQueue.front(); | ||
| 268 | - procOpQueue.pop(); | ||
| 269 | - visitedOps.insert(currentOp); | ||
| 270 | - for (auto outOp : currentOp->ConsumerOps()) { | ||
| 271 | - opInputDegree_[outOp]--; | ||
| 272 | - if (opInputDegree_[outOp] == 0) { | ||
| 273 | - procOpQueue.push(outOp); | ||
| 274 | - } | ||
| 275 | - } | ||
| 276 | - for (auto& outputTensor : currentOp->GetOOperands()) { | ||
| 277 | - tensorProducers_[outputTensor]--; | ||
| 278 | - std::vector<std::pair<LogicalTensorPtr, Operation*>> filterdTensor; | ||
| 279 | - if (tensorProducers_[outputTensor] != 0) { | ||
| 280 | continue; | 265 | continue; |
| 281 | } | 266 | } |
| 282 | auto inplacedTensor = GetInplacedTileTensors(outputTensor); | 267 | auto inplacedTensor = GetInplacedTileTensors(outputTensor); |
| 283 | - filterdTensor = FilterCopyScenes(function, inplacedTensor); | 268 | + auto filterdTensor = FilterCopyScenes(function, inplacedTensor); |
| 284 | - insertCopys_.emplace(outputTensor, filterdTensor); | 269 | + insertCopys_.emplace(outputTensor, std::move(filterdTensor)); |
| 285 | } | 270 | } |
| 286 | } | 271 | } |
| 287 | return SUCCESS; | 272 | return SUCCESS; |
| @@ -341,9 +326,11 @@ void InferDiscontinuousInput::InsertCopyOp(Function& function, LogicalTensorPtr | |||
| 341 | InsertViewOp(function, newTensor, oOperand); | 326 | InsertViewOp(function, newTensor, oOperand); |
| 342 | } | 327 | } |
| 343 | 328 | ||
| 344 | -inline void DDRTensorAssignUB(Function& function, std::map<LogicalTensorPtr, std::set<Operation*>>& insertedNodes) | 329 | +inline void DDRTensorAssignUB(Function& function, |
| 330 | + std::unordered_map<LogicalTensorPtr, std::unordered_set<Operation*>>& insertedNodes) | ||
| 345 | { | 331 | { |
| 346 | - auto opList = function.Operations().DuplicatedOpList(); | 332 | + auto opList = function.Operations(true, SortOperationsMode::LIGHTWEIGHT).DuplicatedOpList(); |
| 333 | + insertedNodes.reserve(opList.size()); | ||
| 347 | for (size_t i = 0; i < opList.size(); ++i) { | 334 | for (size_t i = 0; i < opList.size(); ++i) { |
| 348 | Operation* currOp = opList[i]; | 335 | Operation* currOp = opList[i]; |
| 349 | if (currOp->GetOpcode() != Opcode::OP_ASSEMBLE) { | 336 | if (currOp->GetOpcode() != Opcode::OP_ASSEMBLE) { |
| @@ -381,18 +368,17 @@ inline void DDRTensorAssignUB(Function& function, std::map<LogicalTensorPtr, std | |||
| 381 | } | 368 | } |
| 382 | Status InferDiscontinuousInput::InsertTensorCopy(Function& function) | 369 | Status InferDiscontinuousInput::InsertTensorCopy(Function& function) |
| 383 | { | 370 | { |
| 384 | - std::map<LogicalTensorPtr, std::set<Operation*>> insertedNodes; | 371 | + std::unordered_map<LogicalTensorPtr, std::unordered_set<Operation*>> insertedNodes; |
| 385 | DDRTensorAssignUB(function, insertedNodes); | 372 | DDRTensorAssignUB(function, insertedNodes); |
| 386 | for (auto& copyInserts : insertCopys_) { | 373 | for (auto& copyInserts : insertCopys_) { |
| 387 | auto& inplaceNodes = copyInserts.second; | 374 | auto& inplaceNodes = copyInserts.second; |
| 388 | for (auto& inplaceNode : inplaceNodes) { | 375 | for (auto& inplaceNode : inplaceNodes) { |
| 389 | auto& inputTensor = inplaceNode.first; | 376 | auto& inputTensor = inplaceNode.first; |
| 390 | - if (insertedNodes.find(inputTensor) != insertedNodes.end()) { | 377 | + auto& nodeSet = insertedNodes[inputTensor]; |
| 391 | - if (insertedNodes[inputTensor].count(inplaceNode.second) != 0U) { | 378 | + if (nodeSet.count(inplaceNode.second) != 0U) { |
| 392 | - continue; | 379 | + continue; |
| 393 | - } | ||
| 394 | } | 380 | } |
| 395 | - insertedNodes[inputTensor].insert(inplaceNode.second); | 381 | + nodeSet.insert(inplaceNode.second); |
| 396 | std::shared_ptr<RawTensor> newRawTensor = std::make_shared<RawTensor>( | 382 | std::shared_ptr<RawTensor> newRawTensor = std::make_shared<RawTensor>( |
| 397 | inputTensor->Datatype(), inputTensor->GetShape(), inputTensor->Format()); | 383 | inputTensor->Datatype(), inputTensor->GetShape(), inputTensor->Format()); |
| 398 | Offset newOffset(inputTensor->GetShape().size(), 0); | 384 | Offset newOffset(inputTensor->GetShape().size(), 0); |
| @@ -18,6 +18,7 @@ | |||
| 18 | 18 | ||
| 19 | #include <vector> | 19 | #include <vector> |
| 20 | #include <unordered_map> | 20 | #include <unordered_map> |
| 21 | +#include <unordered_set> | ||
| 21 | 22 | ||
| 22 | #include "passes/pass_interface/pass.h" | 23 | #include "passes/pass_interface/pass.h" |
| 23 | #include "interface/inner/tilefwk.h" | 24 | #include "interface/inner/tilefwk.h" |
| @@ -42,9 +43,8 @@ private: | |||
| 42 | void Init(Function& function); | 43 | void Init(Function& function); |
| 43 | std::vector<std::pair<LogicalTensorPtr, Operation*>> FilterCopyScenes( | 44 | std::vector<std::pair<LogicalTensorPtr, Operation*>> FilterCopyScenes( |
| 44 | Function& function, const std::vector<std::pair<LogicalTensorPtr, Operation*>>&); | 45 | Function& function, const std::vector<std::pair<LogicalTensorPtr, Operation*>>&); |
| 45 | - std::map<LogicalTensorPtr, std::vector<std::pair<LogicalTensorPtr, Operation*>>> insertCopys_; | 46 | + std::unordered_map<LogicalTensorPtr, std::vector<std::pair<LogicalTensorPtr, Operation*>>> insertCopys_; |
| 46 | - std::map<Operation*, size_t> opInputDegree_; | 47 | + std::unordered_map<LogicalTensorPtr, size_t> tensorProducers_; |
| 47 | - std::map<LogicalTensorPtr, size_t> tensorProducers_; | ||
| 48 | std::vector<Operation*> newOps; | 48 | std::vector<Operation*> newOps; |
| 49 | IRBuilder irBuilder_; | 49 | IRBuilder irBuilder_; |
| 50 | }; | 50 | }; |
| @@ -86,7 +86,7 @@ Status ProcessAtomic::RunOnFunction(Function& function) | |||
| 86 | 86 | ||
| 87 | Status ProcessAtomic::CheckAtomicRMWUnsupportedMode(Function& function) | 87 | Status ProcessAtomic::CheckAtomicRMWUnsupportedMode(Function& function) |
| 88 | { | 88 | { |
| 89 | - for (const auto& op : function.Operations()) { | 89 | + for (const auto& op : function.Operations(true, SortOperationsMode::LIGHTWEIGHT)) { |
| 90 | if (op.GetOpcode() == Opcode::OP_ATOMIC_RMW) { | 90 | if (op.GetOpcode() == Opcode::OP_ATOMIC_RMW) { |
| 91 | int rmwModeValue = op.GetIntAttribute(OpAttributeKey::rmwMode); | 91 | int rmwModeValue = op.GetIntAttribute(OpAttributeKey::rmwMode); |
| 92 | AtomicRMWMode rmwMode = static_cast<AtomicRMWMode>(rmwModeValue); | 92 | AtomicRMWMode rmwMode = static_cast<AtomicRMWMode>(rmwModeValue); |
| @@ -105,7 +105,8 @@ Status ProcessAtomic::CheckAtomicRMWUnsupportedMode(Function& function) | |||
| 105 | 105 | ||
| 106 | Status ProcessAtomic::EliminateReduceAcc(Function& function) | 106 | Status ProcessAtomic::EliminateReduceAcc(Function& function) |
| 107 | { | 107 | { |
| 108 | - for (auto& op : function.Operations()) { | 108 | + bool anyDeleted = false; |
| 109 | + for (auto& op : function.Operations(true, SortOperationsMode::LIGHTWEIGHT)) { | ||
| 109 | if (op.GetOpcode() == Opcode::OP_REDUCE_ACC) { | 110 | if (op.GetOpcode() == Opcode::OP_REDUCE_ACC) { |
| 110 | APASS_LOG_INFO_F(Elements::Operation, "ATOMIC_ADD, opmagic: %d", op.GetOpMagic()); | 111 | APASS_LOG_INFO_F(Elements::Operation, "ATOMIC_ADD, opmagic: %d", op.GetOpMagic()); |
| 111 | auto reduceOut = op.GetOOperands().front(); | 112 | auto reduceOut = op.GetOOperands().front(); |
| @@ -119,15 +120,18 @@ Status ProcessAtomic::EliminateReduceAcc(Function& function) | |||
| 119 | } | 120 | } |
| 120 | } | 121 | } |
| 121 | op.SetAsDeleted(); | 122 | op.SetAsDeleted(); |
| 123 | + anyDeleted = true; | ||
| 122 | APASS_LOG_DEBUG_F(Elements::Operation, "%s[%d] will be deleted.", op.GetOpcodeStr().c_str(), | 124 | APASS_LOG_DEBUG_F(Elements::Operation, "%s[%d] will be deleted.", op.GetOpcodeStr().c_str(), |
| 123 | op.GetOpMagic()); | 125 | op.GetOpMagic()); |
| 124 | } | 126 | } |
| 125 | } | 127 | } |
| 126 | - function.EraseOperations(true); | 128 | + if (anyDeleted) { |
| 127 | - if (DeadOperationEliminator::EliminateDeadOperation(function) != SUCCESS) { | 129 | + function.EraseOperations(true, true, SortOperationsMode::LIGHTWEIGHT); |
| 128 | - APASS_LOG_ERROR_F(Elements::Function, | 130 | + if (DeadOperationEliminator::EliminateDeadOperation(function) != SUCCESS) { |
| 129 | - "Eliminate dead operation failed for ReduceAcc in CommonOperationEliminate."); | 131 | + APASS_LOG_ERROR_F(Elements::Function, |
| 130 | - return FAILED; | 132 | + "Eliminate dead operation failed for ReduceAcc in CommonOperationEliminate."); |
| 133 | + return FAILED; | ||
| 134 | + } | ||
| 131 | } | 135 | } |
| 132 | return SUCCESS; | 136 | return SUCCESS; |
| 133 | } | 137 | } |
| @@ -135,14 +139,18 @@ Status ProcessAtomic::EliminateReduceAcc(Function& function) | |||
| 135 | Status ProcessAtomic::EliminateAtomicRMW(Function& function) | 139 | Status ProcessAtomic::EliminateAtomicRMW(Function& function) |
| 136 | { | 140 | { |
| 137 | std::vector<Operation*> atomicRmwOps; | 141 | std::vector<Operation*> atomicRmwOps; |
| 138 | - for (auto& op : function.Operations()) { | 142 | + for (auto& op : function.Operations(true, SortOperationsMode::LIGHTWEIGHT)) { |
| 139 | if (op.GetOpcode() == Opcode::OP_ATOMIC_RMW) { | 143 | if (op.GetOpcode() == Opcode::OP_ATOMIC_RMW) { |
| 140 | atomicRmwOps.emplace_back(&op); | 144 | atomicRmwOps.emplace_back(&op); |
| 141 | } | 145 | } |
| 142 | } | 146 | } |
| 147 | + if (atomicRmwOps.empty()) { | ||
| 148 | + return SUCCESS; | ||
| 149 | + } | ||
| 143 | if (PrepareAtomicRMWSharedInputs(function, atomicRmwOps) != SUCCESS) { | 150 | if (PrepareAtomicRMWSharedInputs(function, atomicRmwOps) != SUCCESS) { |
| 144 | return FAILED; | 151 | return FAILED; |
| 145 | } | 152 | } |
| 153 | + bool anyDeleted = false; | ||
| 146 | for (auto* op : atomicRmwOps) { | 154 | for (auto* op : atomicRmwOps) { |
| 147 | if (op == nullptr || op->IsDeleted()) { | 155 | if (op == nullptr || op->IsDeleted()) { |
| 148 | continue; | 156 | continue; |
| @@ -150,12 +158,15 @@ Status ProcessAtomic::EliminateAtomicRMW(Function& function) | |||
| 150 | if (ProcessSingleAtomicRMW(*op) != SUCCESS) { | 158 | if (ProcessSingleAtomicRMW(*op) != SUCCESS) { |
| 151 | return FAILED; | 159 | return FAILED; |
| 152 | } | 160 | } |
| 161 | + anyDeleted = true; | ||
| 153 | } | 162 | } |
| 154 | - function.EraseOperations(true); | 163 | + if (anyDeleted) { |
| 155 | - if (DeadOperationEliminator::EliminateDeadOperation(function) != SUCCESS) { | 164 | + function.EraseOperations(true, true, SortOperationsMode::LIGHTWEIGHT); |
| 156 | - APASS_LOG_ERROR_F(Elements::Function, | 165 | + if (DeadOperationEliminator::EliminateDeadOperation(function) != SUCCESS) { |
| 157 | - "Eliminate dead operation failed for AtomicRMW in CommonOperationEliminate."); | 166 | + APASS_LOG_ERROR_F(Elements::Function, |
| 158 | - return FAILED; | 167 | + "Eliminate dead operation failed for AtomicRMW in CommonOperationEliminate."); |
| 168 | + return FAILED; | ||
| 169 | + } | ||
| 159 | } | 170 | } |
| 160 | return SUCCESS; | 171 | return SUCCESS; |
| 161 | } | 172 | } |
| @@ -736,7 +747,7 @@ Status ProcessAtomic::EliminateVecDupBranch(Function& function, bool& hasReduceA | |||
| 736 | return SUCCESS; | 747 | return SUCCESS; |
| 737 | } | 748 | } |
| 738 | APASS_LOG_INFO_F(Elements::Function, "EliminateVecDupBranch removed VecDup assemble input branch."); | 749 | APASS_LOG_INFO_F(Elements::Function, "EliminateVecDupBranch removed VecDup assemble input branch."); |
| 739 | - function.EraseOperations(true); | 750 | + function.EraseOperations(true, true, SortOperationsMode::LIGHTWEIGHT); |
| 740 | if (DeadOperationEliminator::EliminateDeadOperation(function) != SUCCESS) { | 751 | if (DeadOperationEliminator::EliminateDeadOperation(function) != SUCCESS) { |
| 741 | APASS_LOG_ERROR_F(Elements::Function, "Eliminate dead operation failed for VecDup branch."); | 752 | APASS_LOG_ERROR_F(Elements::Function, "Eliminate dead operation failed for VecDup branch."); |
| 742 | return FAILED; | 753 | return FAILED; |
| @@ -108,6 +108,7 @@ bool RemoveRedundantOp::ProcessRedundantOpWithoutDynShape(Operation& op) const | |||
| 108 | 108 | ||
| 109 | Status RemoveRedundantOp::RemoveDummyOp(Function& function) | 109 | Status RemoveRedundantOp::RemoveDummyOp(Function& function) |
| 110 | { | 110 | { |
| 111 | + bool anyRemoved = false; | ||
| 111 | for (auto& op : function.Operations()) { | 112 | for (auto& op : function.Operations()) { |
| 112 | bool canRemove = false; | 113 | bool canRemove = false; |
| 113 | if (matchOpcodeWithDynshape.find(op.GetOpcode()) != matchOpcodeWithDynshape.end()) { | 114 | if (matchOpcodeWithDynshape.find(op.GetOpcode()) != matchOpcodeWithDynshape.end()) { |
| @@ -117,10 +118,13 @@ Status RemoveRedundantOp::RemoveDummyOp(Function& function) | |||
| 117 | } | 118 | } |
| 118 | if (canRemove) { | 119 | if (canRemove) { |
| 119 | operationUpdated = true; | 120 | operationUpdated = true; |
| 121 | + anyRemoved = true; | ||
| 120 | function.UpdateOperandBeforeRemoveOp(op, false); | 122 | function.UpdateOperandBeforeRemoveOp(op, false); |
| 121 | } | 123 | } |
| 122 | } | 124 | } |
| 123 | - DeadOperationEliminator::EliminateDeadOperation(function); | 125 | + if (anyRemoved) { |
| 126 | + DeadOperationEliminator::EliminateDeadOperation(function); | ||
| 127 | + } | ||
| 124 | return SUCCESS; | 128 | return SUCCESS; |
| 125 | } | 129 | } |
| 126 | 130 | ||
| @@ -130,6 +134,7 @@ Status RemoveRedundantOp::RunOnFunction(Function& function) | |||
| 130 | operationUpdated = true; | 134 | operationUpdated = true; |
| 131 | iterTime = 0U; | 135 | iterTime = 0U; |
| 132 | newOps_.clear(); | 136 | newOps_.clear(); |
| 137 | + function.SortOperations(SortOperationsMode::LIGHTWEIGHT); | ||
| 133 | while (operationUpdated) { | 138 | while (operationUpdated) { |
| 134 | operationUpdated = false; | 139 | operationUpdated = false; |
| 135 | if (RemoveDummyOps(function) != SUCCESS) { | 140 | if (RemoveDummyOps(function) != SUCCESS) { |
| @@ -188,6 +193,7 @@ Status RemoveRedundantOp::RemoveDummyOps(Function& function) | |||
| 188 | Status RemoveRedundantOp::ProcessViewAssemble(Function& function) | 193 | Status RemoveRedundantOp::ProcessViewAssemble(Function& function) |
| 189 | { | 194 | { |
| 190 | auto opList = function.Operations().DuplicatedOpList(); | 195 | auto opList = function.Operations().DuplicatedOpList(); |
| 196 | + bool anyRemoved = false; | ||
| 191 | for (auto& op : opList) { | 197 | for (auto& op : opList) { |
| 192 | auto opcode = op->GetOpcode(); | 198 | auto opcode = op->GetOpcode(); |
| 193 | if (opcode != Opcode::OP_VIEW) { | 199 | if (opcode != Opcode::OP_VIEW) { |
| @@ -224,6 +230,7 @@ Status RemoveRedundantOp::ProcessViewAssemble(Function& function) | |||
| 224 | "CASE1: Process OP_VIEW[%d]'s input and OP_ASSEMBLE[%d]'s output perfectMatch.", | 230 | "CASE1: Process OP_VIEW[%d]'s input and OP_ASSEMBLE[%d]'s output perfectMatch.", |
| 225 | op->opmagic, consumer->GetOpMagic()); | 231 | op->opmagic, consumer->GetOpMagic()); |
| 226 | ProcessPerfectMatch(function, startTensor, endTensor); | 232 | ProcessPerfectMatch(function, startTensor, endTensor); |
| 233 | + anyRemoved = true; | ||
| 227 | } else { | 234 | } else { |
| 228 | // 对于输入输出存在动轴的场景无法做级联的冗余删除 | 235 | // 对于输入输出存在动轴的场景无法做级联的冗余删除 |
| 229 | if ((function.IsFromInCast(startTensor) && CommonUtils::ContainsNegativeOne(startTensor->GetShape())) || | 236 | if ((function.IsFromInCast(startTensor) && CommonUtils::ContainsNegativeOne(startTensor->GetShape())) || |
| @@ -238,10 +245,13 @@ Status RemoveRedundantOp::ProcessViewAssemble(Function& function) | |||
| 238 | "CASE2: Process OP_VIEW[%d]'s input is a part of OP_ASSEMBLE[%d]'s output.", | 245 | "CASE2: Process OP_VIEW[%d]'s input is a part of OP_ASSEMBLE[%d]'s output.", |
| 239 | op->opmagic, consumer->GetOpMagic()); | 246 | op->opmagic, consumer->GetOpMagic()); |
| 240 | GenerateNewView(function, *op, startTensor, endTensor); | 247 | GenerateNewView(function, *op, startTensor, endTensor); |
| 248 | + anyRemoved = true; | ||
| 241 | } | 249 | } |
| 242 | } | 250 | } |
| 243 | } | 251 | } |
| 244 | - DeadOperationEliminator::EliminateDeadOperation(function); | 252 | + if (anyRemoved) { |
| 253 | + DeadOperationEliminator::EliminateDeadOperation(function); | ||
| 254 | + } | ||
| 245 | return SUCCESS; | 255 | return SUCCESS; |
| 246 | } | 256 | } |
| 247 | 257 | ||
| @@ -463,7 +473,7 @@ void RemoveRedundantOp::GenerateNewView(Function& function, Operation& op, Logic | |||
| 463 | 473 | ||
| 464 | Status RemoveRedundantOp::ProcessReshape(Function& function) | 474 | Status RemoveRedundantOp::ProcessReshape(Function& function) |
| 465 | { | 475 | { |
| 466 | - bool canRemove; | 476 | + bool anyRemoved = false; |
| 467 | for (auto& op : function.Operations()) { | 477 | for (auto& op : function.Operations()) { |
| 468 | auto opcode = op.GetOpcode(); | 478 | auto opcode = op.GetOpcode(); |
| 469 | if (opcode != Opcode::OP_RESHAPE) { | 479 | if (opcode != Opcode::OP_RESHAPE) { |
| @@ -472,7 +482,7 @@ Status RemoveRedundantOp::ProcessReshape(Function& function) | |||
| 472 | } | 482 | } |
| 473 | auto in = op.GetIOperands().front(); | 483 | auto in = op.GetIOperands().front(); |
| 474 | auto out = op.GetOOperands().front(); | 484 | auto out = op.GetOOperands().front(); |
| 475 | - canRemove = false; | 485 | + bool canRemove = false; |
| 476 | if (in->shape == out->shape && !CommonUtils::ContainsNegativeOne(in->GetShape()) && | 486 | if (in->shape == out->shape && !CommonUtils::ContainsNegativeOne(in->GetShape()) && |
| 477 | !CommonUtils::ContainsNegativeOne(out->GetShape())) { | 487 | !CommonUtils::ContainsNegativeOne(out->GetShape())) { |
| 478 | APASS_LOG_DEBUG_F(Elements::Operation, "op[%d]'s in->shape == out->shape.", op.GetOpMagic()); | 488 | APASS_LOG_DEBUG_F(Elements::Operation, "op[%d]'s in->shape == out->shape.", op.GetOpMagic()); |
| @@ -493,9 +503,12 @@ Status RemoveRedundantOp::ProcessReshape(Function& function) | |||
| 493 | if (canRemove) { | 503 | if (canRemove) { |
| 494 | function.UpdateOperandBeforeRemoveOp(op, false); | 504 | function.UpdateOperandBeforeRemoveOp(op, false); |
| 495 | operationUpdated = true; | 505 | operationUpdated = true; |
| 506 | + anyRemoved = true; | ||
| 496 | } | 507 | } |
| 497 | } | 508 | } |
| 498 | - DeadOperationEliminator::EliminateDeadOperation(function); | 509 | + if (anyRemoved) { |
| 510 | + DeadOperationEliminator::EliminateDeadOperation(function); | ||
| 511 | + } | ||
| 499 | return SUCCESS; | 512 | return SUCCESS; |
| 500 | } | 513 | } |
| 501 | } // namespace tile_fwk | 514 | } // namespace tile_fwk |