| @@ -14,13 +14,21 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include "reschedule_utils.h" | 16 | #include "reschedule_utils.h" |
| 17 | -#include "interface/utils/common.h" | 17 | +#include <algorithm> |
| 18 | +#include <unordered_map> | ||
| 18 | 19 | ||
| 20 | +#include "interface/utils/common.h" | ||
| 19 | #include "passes/pass_log/pass_log.h" | 21 | #include "passes/pass_log/pass_log.h" |
| 20 | 22 | ||
| 21 | #define MODULE_NAME "RecheduleUtils" | 23 | #define MODULE_NAME "RecheduleUtils" |
| 22 | 24 | ||
| 23 | namespace npu::tile_fwk { | 25 | namespace npu::tile_fwk { |
| 26 | +namespace { | ||
| 27 | +constexpr uint64_t TOPO_HASH_BASE = 37; | ||
| 28 | +constexpr uint64_t TOPO_HASH_MOD = 0xFFFFFFFFFFFFF; | ||
| 29 | +constexpr uint64_t TOPO_HASH_XOR_SEED = 0x12345678; | ||
| 30 | +} // namespace | ||
| 31 | + | ||
| 24 | bool RescheduleUtils::isAllocOp(Operation* op) | 32 | bool RescheduleUtils::isAllocOp(Operation* op) |
| 25 | { | 33 | { |
| 26 | static std::unordered_set<Opcode> allocOpcodes = { | 34 | static std::unordered_set<Opcode> allocOpcodes = { |
| @@ -30,43 +38,148 @@ bool RescheduleUtils::isAllocOp(Operation* op) | |||
| 30 | return allocOpcodes.find(op->GetOpcode()) != allocOpcodes.end(); | 38 | return allocOpcodes.find(op->GetOpcode()) != allocOpcodes.end(); |
| 31 | } | 39 | } |
| 32 | 40 | ||
| 41 | +void RescheduleUtils::SortUnique(std::vector<int>& values) | ||
| 42 | +{ | ||
| 43 | + std::sort(values.begin(), values.end()); | ||
| 44 | + values.erase(std::unique(values.begin(), values.end()), values.end()); | ||
| 45 | +} | ||
| 46 | + | ||
| 47 | +RescheduleUtils::TensorDependencyMap RescheduleUtils::BuildTensorDependencyInfo(const OperationsViewer& opList, | ||
| 48 | + bool collectConsumerColors) | ||
| 49 | +{ | ||
| 50 | + TensorDependencyMap tensorDeps; | ||
| 51 | + tensorDeps.reserve(opList.size() * 2); | ||
| 52 | + for (size_t i = 0; i < opList.size(); i++) { | ||
| 53 | + const auto& op = opList[i]; | ||
| 54 | + int subgraphId = op.GetSubgraphID(); | ||
| 55 | + for (const auto& tensor : op.GetOOperands()) { | ||
| 56 | + if (tensor == nullptr) { | ||
| 57 | + continue; | ||
| 58 | + } | ||
| 59 | + auto& depInfo = tensorDeps[tensor.get()]; | ||
| 60 | + depInfo.producerOps.emplace_back(static_cast<int>(i)); | ||
| 61 | + if (subgraphId >= 0) { | ||
| 62 | + depInfo.producerColors.emplace_back(subgraphId); | ||
| 63 | + } | ||
| 64 | + } | ||
| 65 | + if (!collectConsumerColors || subgraphId < 0) { | ||
| 66 | + continue; | ||
| 67 | + } | ||
| 68 | + for (const auto& tensor : op.GetIOperands()) { | ||
| 69 | + if (tensor == nullptr) { | ||
| 70 | + continue; | ||
| 71 | + } | ||
| 72 | + tensorDeps[tensor.get()].consumerColors.emplace_back(subgraphId); | ||
| 73 | + } | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + for (auto& [tensor, depInfo] : tensorDeps) { | ||
| 77 | + (void)tensor; | ||
| 78 | + std::sort(depInfo.producerOps.begin(), depInfo.producerOps.end(), | ||
| 79 | + [&](int lhs, int rhs) { return opList[lhs].GetOpMagic() < opList[rhs].GetOpMagic(); }); | ||
| 80 | + depInfo.producerOps.erase( | ||
| 81 | + std::unique(depInfo.producerOps.begin(), depInfo.producerOps.end(), | ||
| 82 | + [&](int lhs, int rhs) { return opList[lhs].GetOpMagic() == opList[rhs].GetOpMagic(); }), | ||
| 83 | + depInfo.producerOps.end()); | ||
| 84 | + SortUnique(depInfo.producerColors); | ||
| 85 | + SortUnique(depInfo.consumerColors); | ||
| 86 | + } | ||
| 87 | + return tensorDeps; | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | +uint64_t RescheduleUtils::GetOpcodeHash(const std::string& op) | ||
| 91 | +{ | ||
| 92 | + uint64_t hash = 0; | ||
| 93 | + for (char c : op) { | ||
| 94 | + hash = (hash * TOPO_HASH_BASE + static_cast<uint64_t>(c)) % TOPO_HASH_MOD; | ||
| 95 | + } | ||
| 96 | + return hash; | ||
| 97 | +} | ||
| 98 | + | ||
| 99 | +RescheduleUtils::TopoHashContribution RescheduleUtils::BuildTopoHashContribution(const std::vector<int>& producerOps, | ||
| 100 | + const std::vector<uint64_t>& hashList) | ||
| 101 | +{ | ||
| 102 | + TopoHashContribution contribution; | ||
| 103 | + for (int producerIdx : producerOps) { | ||
| 104 | + contribution.polynomial = (contribution.polynomial * TOPO_HASH_BASE + | ||
| 105 | + (hashList[producerIdx] ^ TOPO_HASH_XOR_SEED)) % | ||
| 106 | + TOPO_HASH_MOD; | ||
| 107 | + contribution.power = (contribution.power * TOPO_HASH_BASE) % TOPO_HASH_MOD; | ||
| 108 | + } | ||
| 109 | + return contribution; | ||
| 110 | +} | ||
| 111 | + | ||
| 112 | +uint64_t RescheduleUtils::ApplyTopoHashContribution(uint64_t hash, const TopoHashContribution& contribution) | ||
| 113 | +{ | ||
| 114 | + return (hash * contribution.power + contribution.polynomial) % TOPO_HASH_MOD; | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +void RescheduleUtils::BuildInputTopoHashList(const OperationsViewer& opList, const TensorDependencyMap& tensorDeps, | ||
| 118 | + std::vector<uint64_t>& hashList) | ||
| 119 | +{ | ||
| 120 | + hashList.assign(opList.size(), 0); | ||
| 121 | + // opList must be topologically sorted so every producer hash is finalized before the first consumer caches it. | ||
| 122 | + std::unordered_map<const LogicalTensor*, TopoHashContribution> tensorHashCache; | ||
🟡 建议修改:tensorHashCache 隐含拓扑序依赖,建议加注释
原始实现(不缓存、每次实时读 hashList[j])也有同样的拓扑序假设,但缓存放大了错误传播范围。建议在 ![]() ![]() | |||
| 123 | + tensorHashCache.reserve(tensorDeps.size()); | ||
| 124 | + for (size_t i = 0; i < opList.size(); i++) { | ||
| 125 | + uint64_t hash = GetOpcodeHash(opList[i].GetOpcodeStr()); | ||
| 126 | + for (const auto& tensor : opList[i].GetIOperands()) { | ||
| 127 | + if (tensor == nullptr) { | ||
| 128 | + continue; | ||
| 129 | + } | ||
| 130 | + auto depIter = tensorDeps.find(tensor.get()); | ||
| 131 | + if (depIter == tensorDeps.end()) { | ||
| 132 | + continue; | ||
| 133 | + } | ||
| 134 | + const auto& depInfo = depIter->second; | ||
| 135 | + TopoHashContribution contribution; | ||
| 136 | + auto cacheIter = tensorHashCache.find(tensor.get()); | ||
| 137 | + if (cacheIter != tensorHashCache.end()) { | ||
| 138 | + contribution = cacheIter->second; | ||
| 139 | + } else { | ||
| 140 | + contribution = BuildTopoHashContribution(depInfo.producerOps, hashList); | ||
| 141 | + tensorHashCache.emplace(tensor.get(), contribution); | ||
| 142 | + } | ||
| 143 | + hash = ApplyTopoHashContribution(hash, contribution); | ||
| 144 | + } | ||
| 145 | + hashList[i] = hash; | ||
| 146 | + } | ||
| 147 | +} | ||
| 148 | + | ||
| 149 | +void RescheduleUtils::BuildInputTopoHashList(const OperationsViewer& opList, std::vector<uint64_t>& hashList) | ||
| 150 | +{ | ||
| 151 | + auto tensorDeps = BuildTensorDependencyInfo(opList, false); | ||
| 152 | + BuildInputTopoHashList(opList, tensorDeps, hashList); | ||
| 153 | +} | ||
| 154 | + | ||
| 33 | // vector size == 2 | 155 | // vector size == 2 |
| 34 | // 这里in_graph和out_graph只考虑了数据依赖,需要确认控制依赖是不是也要加入到in_graph和out_graph中 | 156 | // 这里in_graph和out_graph只考虑了数据依赖,需要确认控制依赖是不是也要加入到in_graph和out_graph中 |
| 35 | -std::vector<std::vector<std::vector<int>>> RescheduleUtils::GetInOutGraphs(const std::vector<Operation*>& opList, | 157 | +std::vector<std::vector<std::vector<int>>> RescheduleUtils::GetInOutGraphs(const std::vector<Operation*>& opList) |
| 36 | - int functionmagic) | ||
| 37 | { | 158 | { |
| 38 | - std::vector<std::vector<int>> inGraph; | 159 | + std::vector<std::vector<int>> inGraph(opList.size()); |
| 39 | - std::vector<std::vector<int>> outGraph; | 160 | + std::vector<std::vector<int>> outGraph(opList.size()); |
| 40 | - inGraph.resize(opList.size()); | ||
| 41 | - outGraph.resize(opList.size()); | ||
| 42 | std::map<int, size_t> magic2Index; | 161 | std::map<int, size_t> magic2Index; |
| 43 | for (size_t i = 0; i < opList.size(); i++) { | 162 | for (size_t i = 0; i < opList.size(); i++) { |
| 44 | magic2Index[opList[i]->GetOpMagic()] = i; | 163 | magic2Index[opList[i]->GetOpMagic()] = i; |
| 45 | } | 164 | } |
| 46 | - if (functionmagic != -1) { | 165 | + std::unordered_map<int, std::vector<size_t>> tensor2ProducerIdx; |
| 47 | - for (size_t i = 0; i < opList.size(); i++) { | 166 | + for (size_t i = 0; i < opList.size(); i++) { |
| 48 | - // 收集节点输出输出关系,沿着一个方向,如果沿着输入输出都收集,需要去重 | 167 | + for (auto& inTensor : opList[i]->GetIOperands()) { |
| 49 | - for (auto& inTensor : opList[i]->GetIOperands()) { | 168 | + int tensorMagic = inTensor->GetMagic(); |
| 50 | - for (auto& producer : inTensor->GetProducers()) { | 169 | + auto cacheIt = tensor2ProducerIdx.find(tensorMagic); |
| 51 | - // 只收集了当前func内的节点关联关系 | 170 | + if (cacheIt == tensor2ProducerIdx.end()) { |
| 171 | + std::vector<size_t> prodIdx; | ||
| 172 | + for (auto* producer : inTensor->GetProducers()) { | ||
| 52 | auto iter = magic2Index.find(producer->GetOpMagic()); | 173 | auto iter = magic2Index.find(producer->GetOpMagic()); |
| 53 | - if (iter == magic2Index.end()) { | 174 | + if (iter != magic2Index.end()) { |
| 54 | - continue; | 175 | + prodIdx.push_back(iter->second); |
| 55 | } | 176 | } |
| 56 | - inGraph[i].push_back(iter->second); | ||
| 57 | - outGraph[iter->second].push_back(i); | ||
| 58 | } | 177 | } |
| 178 | + cacheIt = tensor2ProducerIdx.emplace(tensorMagic, std::move(prodIdx)).first; | ||
| 59 | } | 179 | } |
| 60 | - } | 180 | + for (size_t prodIdx : cacheIt->second) { |
| 61 | - } else { | 181 | + inGraph[i].push_back(prodIdx); |
| 62 | - for (size_t i = 0; i < opList.size(); i++) { | 182 | + outGraph[prodIdx].push_back(i); |
| 63 | - // 收集节点输出输出关系,沿着一个方向,如果沿着输入输出都收集,需要去重 | ||
| 64 | - for (auto& inTensor : opList[i]->GetIOperands()) { | ||
| 65 | - for (auto& producer : inTensor->GetProducers()) { | ||
| 66 | - // 只收集了当前func内的节点关联关系 | ||
| 67 | - inGraph[i].push_back(magic2Index[producer->GetOpMagic()]); | ||
| 68 | - outGraph[magic2Index[producer->GetOpMagic()]].push_back(i); | ||
| 69 | - } | ||
| 70 | } | 183 | } |
| 71 | } | 184 | } |
| 72 | } | 185 | } |
| @@ -15,39 +15,42 @@ | |||
| 15 | 15 | ||
| 16 | #ifndef RESCHEDULE_UTILS_H_ | 16 | #ifndef RESCHEDULE_UTILS_H_ |
| 17 | #define RESCHEDULE_UTILS_H_ | 17 | #define RESCHEDULE_UTILS_H_ |
| 18 | + | ||
| 19 | +#include <unordered_map> | ||
| 20 | + | ||
| 18 | #include "interface/function/function.h" | 21 | #include "interface/function/function.h" |
| 19 | #include "interface/tensor/logical_tensor.h" | 22 | #include "interface/tensor/logical_tensor.h" |
| 20 | 23 | ||
| 21 | namespace npu::tile_fwk { | 24 | namespace npu::tile_fwk { |
| 22 | class RescheduleUtils { | 25 | class RescheduleUtils { |
| 23 | public: | 26 | public: |
| 27 | + struct TensorDependencyInfo { | ||
| 28 | + std::vector<int> producerOps; | ||
| 29 | + std::vector<int> producerColors; | ||
| 30 | + std::vector<int> consumerColors; | ||
| 31 | + }; | ||
| 32 | + using TensorDependencyMap = std::unordered_map<const LogicalTensor*, TensorDependencyInfo>; | ||
| 33 | + struct TopoHashContribution { | ||
| 34 | + uint64_t power{1}; | ||
| 35 | + uint64_t polynomial{0}; | ||
| 36 | + }; | ||
| 37 | + | ||
| 24 | static bool isAllocOp(Operation* op); | 38 | static bool isAllocOp(Operation* op); |
| 39 | + static void SortUnique(std::vector<int>& values); | ||
| 40 | + static TensorDependencyMap BuildTensorDependencyInfo(const OperationsViewer& opList, | ||
| 41 | + bool collectConsumerColors = true); | ||
| 42 | + static void BuildInputTopoHashList(const OperationsViewer& opList, const TensorDependencyMap& tensorDeps, | ||
| 43 | + std::vector<uint64_t>& hashList); | ||
| 44 | + static void BuildInputTopoHashList(const OperationsViewer& opList, std::vector<uint64_t>& hashList); | ||
| 45 | + static uint64_t GetOpcodeHash(const std::string& op); | ||
| 46 | + static TopoHashContribution BuildTopoHashContribution(const std::vector<int>& producerOps, | ||
| 47 | + const std::vector<uint64_t>& hashList); | ||
| 48 | + static uint64_t ApplyTopoHashContribution(uint64_t hash, const TopoHashContribution& contribution); | ||
| 25 | 49 | ||
| 26 | // vector size == 2 | 50 | // vector size == 2 |
| 27 | // 对于ingraph outgraph color | 51 | // 对于ingraph outgraph color |
| 28 | // colornodes等信息可以之后用一个单例管理起来,避免每个pass遍历图去重新收集这些信息,导致编译时间变长 | 52 | // colornodes等信息可以之后用一个单例管理起来,避免每个pass遍历图去重新收集这些信息,导致编译时间变长 |
| 29 | - static std::vector<std::vector<std::vector<int>>> GetInOutGraphs(const std::vector<Operation*>& opList, | 53 | + static std::vector<std::vector<std::vector<int>>> GetInOutGraphs(const std::vector<Operation*>& opList); |
| 30 | - int functionmagic = -1); | ||
| 31 | - | ||
| 32 | - // 按序列获取OP之间的依赖图连边。返回的边,以源op和目标op在opList中的Index表示。 | ||
| 33 | - // 读写关系通过tensor magic来判断。 | ||
| 34 | - static void GetInOutGraphs(const std::vector<Operation*>& opList, std::vector<std::vector<int>>& inGraph, | ||
| 35 | - std::vector<std::vector<int>>& outGraph, bool hasCtrlEdge = true); | ||
| 36 | - | ||
| 37 | - static void GetInOutGraphs(const std::vector<Operation*>& opList, | ||
| 38 | - std::unordered_map<Operation*, std::vector<Operation*>>& inGraph, | ||
| 39 | - std::unordered_map<Operation*, std::vector<Operation*>>& outGraph, | ||
| 40 | - bool hasCtrlEdge = true); | ||
| 41 | - | ||
| 42 | - // 按序列获取OP之间的依赖图连边。返回的边,以源op和目标op在opList中的Index表示。 | ||
| 43 | - // 读写关系通过每个tensor的memmap[subGraphId].memId来判断。 | ||
| 44 | - static void GetInOutGraphs(const std::vector<Operation*>& opList, std::vector<std::vector<int>>& inGraph, | ||
| 45 | - std::vector<std::vector<int>>& outGraph, int subGraphId, bool hasCtrlEdge = true); | ||
| 46 | - | ||
| 47 | - static void GetInOutGraphs(const std::vector<Operation*>& opList, | ||
| 48 | - std::unordered_map<Operation*, std::vector<Operation*>>& inGraph, | ||
| 49 | - std::unordered_map<Operation*, std::vector<Operation*>>& outGraph, int subGraphId, | ||
| 50 | - bool hasCtrlEdge = true); | ||
| 51 | 54 | ||
| 52 | static PipeType GetOpPipeType(const Operation* op); | 55 | static PipeType GetOpPipeType(const Operation* op); |
| 53 | 56 | ||
| @@ -14,6 +14,8 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include "l1_copy_reuse.h" | 16 | #include "l1_copy_reuse.h" |
| 17 | +#include <algorithm> | ||
| 18 | + | ||
| 17 | #include "interface/tensor/irbuilder.h" | 19 | #include "interface/tensor/irbuilder.h" |
| 18 | #include "passes/pass_utils/pass_utils.h" | 20 | #include "passes/pass_utils/pass_utils.h" |
| 19 | 21 | ||
| @@ -17,6 +17,7 @@ | |||
| 17 | #define PASS_L1_COPY_REUSE_H_ | 17 | #define PASS_L1_COPY_REUSE_H_ |
| 18 | 18 | ||
| 19 | #include <functional> | 19 | #include <functional> |
| 20 | + | ||
| 20 | #include "interface/function/function.h" | 21 | #include "interface/function/function.h" |
| 21 | #include "interface/tensor/logical_tensor.h" | 22 | #include "interface/tensor/logical_tensor.h" |
| 22 | #include "passes/pass_utils/reschedule_utils.h" | 23 | #include "passes/pass_utils/reschedule_utils.h" |
| @@ -41,7 +42,7 @@ | |||
| 41 | namespace npu::tile_fwk { | 42 | namespace npu::tile_fwk { |
| 42 | class L1CopyInReuseRunner { | 43 | class L1CopyInReuseRunner { |
| 43 | public: | 44 | public: |
| 44 | - explicit L1CopyInReuseRunner(const std::vector<std::vector<int>>& inGraph1) : inGraph_(inGraph1) {} | 45 | + L1CopyInReuseRunner() = default; |
| 45 | ~L1CopyInReuseRunner() {} | 46 | ~L1CopyInReuseRunner() {} |
| 46 | Status Run(Function& func, int color, std::vector<std::vector<int>>& colorNode); | 47 | Status Run(Function& func, int color, std::vector<std::vector<int>>& colorNode); |
| 47 | static bool CanReuse(const Operation& op); | 48 | static bool CanReuse(const Operation& op); |
| @@ -53,13 +54,14 @@ public: | |||
| 53 | const std::map<std::string, int64_t>& settingByFunc); | 54 | const std::map<std::string, int64_t>& settingByFunc); |
| 54 | static std::vector<int> GetCopyIn(const OperationsViewer& opOriList, int color, | 55 | static std::vector<int> GetCopyIn(const OperationsViewer& opOriList, int color, |
| 55 | std::vector<std::vector<int>>& colorNode); | 56 | std::vector<std::vector<int>>& colorNode); |
| 56 | - static void GetOriList(Function& func, std::vector<Operation*>& oriList); | ||
| 57 | 57 | ||
| 58 | private: | 58 | private: |
| 59 | - void GetOpHash(std::vector<uint64_t>& hashList, const std::string op, int idx); | 59 | + static void UpdateTopTwoColors(std::pair<int, int>& topTwoColors, int color); |
| 60 | - void GetColorHash(const Function& func, const OperationsViewer& opOriList, std::vector<uint64_t>& hashColor, | 60 | + static void BuildTopoHashAndMaxInputColors(const OperationsViewer& opList, std::vector<uint64_t>& hashList, |
| 61 | - const std::vector<std::vector<int>>& colorNode); | 61 | + std::vector<std::pair<int, int>>& maxInputColors); |
| 62 | - int GetMaxInColor(const std::vector<int>& nodes, const OperationsViewer& opOriList, int curColor); | 62 | + void GetColorHash(const Function& func, const OperationsViewer& opOriList, const std::vector<uint64_t>& hashTileOp, |
| 63 | + std::vector<uint64_t>& hashColor, const std::vector<std::vector<int>>& colorNode); | ||
| 64 | + int GetMaxInColor(const std::vector<int>& nodes, int curColor); | ||
| 63 | Status MergeDupL1CopyIn(Function& func, std::vector<std::vector<int>>& colorNode, int color); | 65 | Status MergeDupL1CopyIn(Function& func, std::vector<std::vector<int>>& colorNode, int color); |
| 64 | void MergeProcessIdUpdate(Function& func, std::vector<std::vector<int>>& colorNode, int color); | 66 | void MergeProcessIdUpdate(Function& func, std::vector<std::vector<int>>& colorNode, int color); |
| 65 | std::vector<int> GetOpInputFeature(const OperationsViewer& opOriList, const int opIdx, const int ioperandIdx); | 67 | std::vector<int> GetOpInputFeature(const OperationsViewer& opOriList, const int opIdx, const int ioperandIdx); |
| @@ -69,6 +71,13 @@ private: | |||
| 69 | std::vector<std::vector<int>>& replacedOutputs); | 71 | std::vector<std::vector<int>>& replacedOutputs); |
| 70 | Status Phase1(Function& func, int color, std::vector<std::vector<int>>& colorNode, std::vector<int>& colorCopyIn, | 72 | Status Phase1(Function& func, int color, std::vector<std::vector<int>>& colorNode, std::vector<int>& colorCopyIn, |
| 71 | std::vector<uint64_t>& hashColor); | 73 | std::vector<uint64_t>& hashColor); |
| 74 | + Status ResolveL1ReuseConfigLists(const Function& func, const OperationsViewer& opOriList, int color, | ||
| 75 | + const std::vector<uint64_t>& hashColor, std::vector<int>& numLRList, | ||
| 76 | + std::vector<int>& numLRSideList); | ||
| 77 | + Status ProcessL1ReuseCandidates(OperationsViewer& opOriList, int color, std::vector<std::vector<int>>& colorNode, | ||
| 78 | + std::vector<int>& colorCopyIn, std::vector<uint64_t>& hashColor, | ||
| 79 | + std::vector<int>& numLRList, std::vector<int>& numLRSideList); | ||
| 80 | + void LogSideMergeSummary(const std::vector<int>& mergedNum, const std::vector<int>& numLRSideList, int color) const; | ||
| 72 | void GetL1ReuseOpOrder(std::vector<std::pair<int, int>>& opOrder, std::map<uint64_t, int>& mgRem, | 81 | void GetL1ReuseOpOrder(std::vector<std::pair<int, int>>& opOrder, std::map<uint64_t, int>& mgRem, |
| 73 | std::vector<int>& numLRList, std::vector<uint64_t>& hashColor, int color); | 82 | std::vector<int>& numLRList, std::vector<uint64_t>& hashColor, int color); |
| 74 | bool GetMergedL1(int maxInColor, std::vector<int>& mergedNum, int maxMergeNum, int& tmpColor, int i, | 83 | bool GetMergedL1(int maxInColor, std::vector<int>& mergedNum, int maxMergeNum, int& tmpColor, int i, |
| @@ -107,17 +116,34 @@ private: | |||
| 107 | const std::string& configName); | 116 | const std::string& configName); |
| 108 | void HashUpdate(Function& func, int color, const std::vector<uint64_t>& hashColor, OperationsViewer& opOriList, | 117 | void HashUpdate(Function& func, int color, const std::vector<uint64_t>& hashColor, OperationsViewer& opOriList, |
| 109 | std::vector<std::vector<int>>& colorNode); | 118 | std::vector<std::vector<int>>& colorNode); |
| 119 | + void ConfigureRunSettings(Function& func); | ||
| 120 | + void LogL1ReuseHashOverview(const Function& func) const; | ||
| 121 | + Status RunL1ReusePhase(Function& func, int color, std::vector<std::vector<int>>& colorNode, | ||
| 122 | + std::vector<int>& colorCopyIn, std::vector<uint64_t>& hashColor, | ||
| 123 | + OperationsViewer& opOriList); | ||
| 124 | + Status RunCubeNBufferPhase(const Function& func, int color, std::vector<std::vector<int>>& colorNode, | ||
| 125 | + OperationsViewer& opOriList, std::vector<int>& colorCopyIn, | ||
| 126 | + const std::vector<uint64_t>& hashColor); | ||
| 127 | + Status ValidateSubgraphIds(Function& func) const; | ||
| 128 | + static Status CollectL1ReuseLabelOverrides(const OperationsViewer& opOriList, | ||
| 129 | + const std::map<std::string, int64_t>& labelMap, int color, | ||
| 130 | + std::map<int, int>& subgraphOverrides); | ||
| 131 | + static void ApplyL1ReuseLabelOverrides(const std::map<int, int>& subgraphOverrides, std::vector<int>& numLRList); | ||
| 132 | + Status CollectCubeNBufferLabelOverrides(const OperationsViewer& opOriList, const std::vector<uint64_t>& hashColor, | ||
| 133 | + int color, std::map<int, int>& labelOverrides) const; | ||
| 134 | + static void ApplyCubeNBufferLabelOverrides(const std::map<int, int>& labelOverrides, | ||
| 135 | + std::map<int, int>& hashMergeNumMap); | ||
| 110 | Status ApplySemanticLabelSettingsL1Reuse(const OperationsViewer& opOriList, | 136 | Status ApplySemanticLabelSettingsL1Reuse(const OperationsViewer& opOriList, |
| 111 | const std::map<std::string, int64_t>& labelMap, | 137 | const std::map<std::string, int64_t>& labelMap, |
| 112 | std::vector<int>& numLRList, const std::vector<uint64_t>& hashColor, | 138 | std::vector<int>& numLRList, const std::vector<uint64_t>& hashColor, |
| 113 | int color); | 139 | int color); |
| 114 | Status ApplySemanticLabelSettingsCubeNBuffer(const OperationsViewer& opOriList, std::map<int, int>& hashMergeNumMap, | 140 | Status ApplySemanticLabelSettingsCubeNBuffer(const OperationsViewer& opOriList, std::map<int, int>& hashMergeNumMap, |
| 115 | const std::vector<uint64_t>& hashColor, int color); | 141 | const std::vector<uint64_t>& hashColor, int color); |
| 116 | - const std::vector<std::vector<int>>& inGraph_; | ||
| 117 | std::unordered_map<int, int> replacedCopyMap_; | 142 | std::unordered_map<int, int> replacedCopyMap_; |
| 118 | std::unordered_map<int, int> tensormagic2Op_; | 143 | std::unordered_map<int, int> tensormagic2Op_; |
| 119 | std::unordered_map<uint64_t, std::vector<int>> hashMap_; | 144 | std::unordered_map<uint64_t, std::vector<int>> hashMap_; |
| 120 | std::map<uint64_t, int> hashOrder_; // Deterministic ordered map for compilation repeatability | 145 | std::map<uint64_t, int> hashOrder_; // Deterministic ordered map for compilation repeatability |
| 146 | + std::vector<std::pair<int, int>> maxInputColors_; | ||
| 121 | std::map<int64_t, int64_t> numLRMap_; | 147 | std::map<int64_t, int64_t> numLRMap_; |
| 122 | std::map<int64_t, int64_t> numDBMap_; | 148 | std::map<int64_t, int64_t> numDBMap_; |
| 123 | std::map<std::string, int64_t> numLRMapByFunc_; | 149 | std::map<std::string, int64_t> numLRMapByFunc_; |
| @@ -14,47 +14,233 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include "n_buffer_merge.h" | 16 | #include "n_buffer_merge.h" |
| 17 | -#include "passes/pass_utils/reschedule_utils.h" | 17 | +#include <algorithm> |
| 18 | -#include "passes/pass_utils/pass_utils.h" | ||
| 19 | - | ||
| 20 | -#include "passes/pass_utils/parallel_tool.h" | ||
| 21 | -#include "passes/pass_log/pass_log.h" | ||
| 22 | #include <climits> | 18 | #include <climits> |
| 19 | +#include <limits> | ||
| 20 | +#include <unordered_map> | ||
| 21 | + | ||
| 22 | +#include "passes/pass_log/pass_log.h" | ||
| 23 | +#include "passes/pass_utils/parallel_tool.h" | ||
| 24 | +#include "passes/pass_utils/pass_utils.h" | ||
| 25 | +#include "passes/pass_utils/reschedule_utils.h" | ||
| 23 | 26 | ||
| 24 | #define MODULE_NAME "NBufferMerge" | 27 | #define MODULE_NAME "NBufferMerge" |
| 25 | 28 | ||
| 26 | namespace npu::tile_fwk { | 29 | namespace npu::tile_fwk { |
| 30 | +namespace { | ||
| 31 | +constexpr int INVALID_COLOR = -1; | ||
| 32 | +} // namespace | ||
| 27 | 33 | ||
| 28 | -void NBufferMerge::GetOpHash(std::vector<uint64_t>& hashList, const std::string op, size_t idx) | 34 | +int NBufferMerge::GetMinDifferentColor(const std::vector<int>& colors, int color) |
| 29 | { | 35 | { |
| 30 | - uint64_t p = 37; | 36 | + if (colors.empty()) { |
| 31 | - const uint64_t mod = 0xFFFFFFFFFFFFF; | 37 | + return INVALID_COLOR; |
| 32 | - uint64_t hash = 0; | ||
| 33 | - for (char c : op) { | ||
| 34 | - hash = (hash * p + static_cast<uint64_t>(c)) % mod; | ||
| 35 | } | 38 | } |
| 36 | - uint64_t a = 0x12345678; | 39 | + if (colors[0] != color) { |
| 37 | - for (int j : inGraph_[idx]) { | 40 | + return colors[0]; |
| 38 | - hash = (hash * p + (hashList[j] ^ a)) % mod; | ||
| 39 | } | 41 | } |
| 40 | - hashList[idx] = hash; | 42 | + return colors.size() > 1 ? colors[1] : INVALID_COLOR; |
| 41 | } | 43 | } |
| 42 | 44 | ||
| 43 | -void NBufferMerge::GetOpHashReverse(std::vector<uint64_t>& hashList, const std::string op, int idx) | 45 | +void NBufferMerge::AddConsumerBarrier(const std::vector<int>& producerColors, const std::vector<int>& consumerColors, |
| 46 | + ColorTensorDependency& colorDep, std::vector<int>& pendingBarrierCount, | ||
| 47 | + bool& hasInterColorDependency) | ||
| 44 | { | 48 | { |
| 45 | - uint64_t a = 0x12345678; | 49 | + for (int consumerColor : consumerColors) { |
| 46 | - uint64_t p = 37; | 50 | + PASS_ASSERT(consumerColor >= 0 && consumerColor < static_cast<int>(pendingBarrierCount.size())) |
| 47 | - const uint64_t mod = 0xFFFFFFFFFFFFF; | 51 | + << "Consumer color is out of range: " << consumerColor; |
| 48 | - uint64_t hash = 0; | 52 | + bool isProducer = std::binary_search(producerColors.begin(), producerColors.end(), consumerColor); |
| 49 | - for (char c : op) { | 53 | + if (!isProducer) { |
| 50 | - hash = (hash * p + static_cast<uint64_t>(c)) % mod; | 54 | + colorDep.nonProducerConsumerColors.emplace_back(consumerColor); |
| 55 | + pendingBarrierCount[consumerColor]++; | ||
🟢 可选优化:pendingBarrierCount / producerColorToDeps 缺少 color 值边界检查
![]() ![]() | |||
| 56 | + hasInterColorDependency = true; | ||
| 57 | + continue; | ||
| 58 | + } | ||
| 59 | + if (producerColors.size() > 1) { | ||
| 60 | + pendingBarrierCount[consumerColor]++; | ||
| 61 | + hasInterColorDependency = true; | ||
| 62 | + } | ||
| 51 | } | 63 | } |
| 52 | - for (int j : outGraph_[idx]) { | ||
| 53 | - hash = (hash * p + (hashList[j] ^ a)) % mod; | ||
| 54 | - } | ||
| 55 | - hashList[idx] = hash; | ||
| 56 | } | 64 | } |
| 57 | 65 | ||
| 66 | +void NBufferMerge::BuildColorTensorDependencies(const RescheduleUtils::TensorDependencyMap& tensorDeps, int colorNum, | ||
| 67 | + std::vector<ColorTensorDependency>& colorTensorDeps, | ||
| 68 | + std::vector<std::vector<int>>& producerColorToDeps, | ||
| 69 | + std::vector<int>& pendingBarrierCount) | ||
| 70 | +{ | ||
| 71 | + producerColorToDeps.assign(colorNum, {}); | ||
| 72 | + pendingBarrierCount.assign(colorNum, 0); | ||
| 73 | + colorTensorDeps.clear(); | ||
| 74 | + colorTensorDeps.reserve(tensorDeps.size()); | ||
| 75 | + for (const auto& [tensor, depInfo] : tensorDeps) { | ||
| 76 | + (void)tensor; | ||
| 77 | + const auto& producerColors = depInfo.producerColors; | ||
| 78 | + const auto& consumerColors = depInfo.consumerColors; | ||
| 79 | + if (producerColors.empty() || consumerColors.empty()) { | ||
| 80 | + continue; | ||
| 81 | + } | ||
| 82 | + ColorTensorDependency colorDep; | ||
| 83 | + colorDep.consumerColors = consumerColors; | ||
| 84 | + colorDep.remainingProducerCount = static_cast<int>(producerColors.size()); | ||
| 85 | + for (int producerColor : producerColors) { | ||
| 86 | + colorDep.remainingProducerXor ^= producerColor; | ||
| 87 | + } | ||
| 88 | + bool hasInterColorDependency = false; | ||
| 89 | + AddConsumerBarrier(producerColors, consumerColors, colorDep, pendingBarrierCount, hasInterColorDependency); | ||
| 90 | + if (!hasInterColorDependency) { | ||
| 91 | + continue; | ||
| 92 | + } | ||
| 93 | + int depIndex = static_cast<int>(colorTensorDeps.size()); | ||
| 94 | + colorTensorDeps.emplace_back(std::move(colorDep)); | ||
| 95 | + for (int producerColor : producerColors) { | ||
| 96 | + PASS_ASSERT(producerColor >= 0 && producerColor < colorNum) | ||
| 97 | + << "Producer color is out of range: " << producerColor; | ||
| 98 | + producerColorToDeps[producerColor].emplace_back(depIndex); | ||
| 99 | + } | ||
| 100 | + } | ||
| 101 | +} | ||
| @@ -16,14 +16,17 @@ | |||
| 16 | #ifndef PASS_N_BUFFER_MERGE_H_ | 16 | #ifndef PASS_N_BUFFER_MERGE_H_ |
| 17 | #define PASS_N_BUFFER_MERGE_H_ | 17 | #define PASS_N_BUFFER_MERGE_H_ |
| 18 | 18 | ||
| 19 | +#include <functional> | ||
| 20 | +#include <queue> | ||
| 21 | + | ||
| 19 | #include "interface/function/function.h" | 22 | #include "interface/function/function.h" |
| 20 | #include "interface/tensor/logical_tensor.h" | 23 | #include "interface/tensor/logical_tensor.h" |
| 21 | #include "passes/pass_interface/pass.h" | 24 | #include "passes/pass_interface/pass.h" |
| 25 | +#include "passes/pass_utils/reschedule_utils.h" | ||
| 22 | #include "tilefwk/tilefwk.h" | 26 | #include "tilefwk/tilefwk.h" |
| 23 | #include "tilefwk/platform.h" | 27 | #include "tilefwk/platform.h" |
| 24 | #include "interface/inner/tilefwk.h" | 28 | #include "interface/inner/tilefwk.h" |
| 25 | #include "interface/program/program.h" | 29 | #include "interface/program/program.h" |
| 26 | -#include "passes/pass_utils/dfs_sort_utils.h" | ||
| 27 | #include "passes/tile_graph_pass/graph_partition/hash_order_utils.h" | 30 | #include "passes/tile_graph_pass/graph_partition/hash_order_utils.h" |
| 28 | namespace npu::tile_fwk { | 31 | namespace npu::tile_fwk { |
| 29 | 32 | ||
| @@ -38,12 +41,38 @@ public: | |||
| 38 | ~NBufferMerge() override = default; | 41 | ~NBufferMerge() override = default; |
| 39 | 42 | ||
| 40 | private: | 43 | private: |
| 44 | + // A tensor-level barrier compresses all producerColor -> consumerColor edges for one tensor. | ||
| 45 | + // This avoids expanding high fanin/fanout tensors into producer * consumer color edges. | ||
| 46 | + struct ColorTensorDependency { | ||
| 47 | + std::vector<int> consumerColors; | ||
| 48 | + std::vector<int> nonProducerConsumerColors; | ||
| 49 | + int remainingProducerCount{0}; | ||
| 50 | + int remainingProducerXor{0}; | ||
| 51 | + }; | ||
| 52 | + static int GetMinDifferentColor(const std::vector<int>& colors, int color); | ||
| 53 | + static void AddConsumerBarrier(const std::vector<int>& producerColors, const std::vector<int>& consumerColors, | ||
| 54 | + ColorTensorDependency& colorDep, std::vector<int>& pendingBarrierCount, | ||
| 55 | + bool& hasInterColorDependency); | ||
| 56 | + static void BuildColorTensorDependencies(const RescheduleUtils::TensorDependencyMap& tensorDeps, int colorNum, | ||
| 57 | + std::vector<ColorTensorDependency>& colorTensorDeps, | ||
| 58 | + std::vector<std::vector<int>>& producerColorToDeps, | ||
| 59 | + std::vector<int>& pendingBarrierCount); | ||
| 60 | + using ColorQueue = std::priority_queue<int, std::vector<int>, std::greater<int>>; | ||
| 61 | + static void ReleaseColor(int color, std::vector<int>& pendingBarrierCount, ColorQueue& colorQueue); | ||
| 62 | + static void UpdateDependencyAfterProducerReady(ColorTensorDependency& colorDep, int producerColor, | ||
| 63 | + std::vector<int>& pendingBarrierCount, ColorQueue& colorQueue); | ||
| 64 | + static Status RunColorBarrierTopo(int colorNum, const std::vector<std::vector<int>>& producerColorToDeps, | ||
| 65 | + std::vector<ColorTensorDependency>& colorTensorDeps, | ||
| 66 | + std::vector<int>& pendingBarrierCount, std::vector<int>& colorOrder); | ||
| 67 | + static void BuildColorDependencySummary(const OperationsViewer& opList, int colorNum, | ||
| 68 | + std::vector<std::vector<int>>& inColor, | ||
| 69 | + std::vector<std::vector<int>>& outColor); | ||
| 70 | + static Status BuildColorTopoOrder(const OperationsViewer& opList, int colorNum, std::vector<int>& colorOrder); | ||
| 71 | + static Status ApplyColorTopoOrder(OperationsViewer& opList, int colorNum); | ||
| 41 | Status RunOnFunction(Function& function) override; | 72 | Status RunOnFunction(Function& function) override; |
| 42 | Status NBufferMergeProcess(Function& func); | 73 | Status NBufferMergeProcess(Function& func); |
| 43 | Status Init(Function& func); | 74 | Status Init(Function& func); |
| 44 | void InitParam(OperationsViewer& opOriList); | 75 | void InitParam(OperationsViewer& opOriList); |
| 45 | - void GetOpHash(std::vector<uint64_t>& hashList, const std::string op, size_t idx); | ||
| 46 | - void GetOpHashReverse(std::vector<uint64_t>& hashList, const std::string op, int idx); | ||
| 47 | void GetColorHash(const Function& func, const OperationsViewer& opOriList, std::vector<uint64_t>& hashColor, | 76 | void GetColorHash(const Function& func, const OperationsViewer& opOriList, std::vector<uint64_t>& hashColor, |
| 48 | std::map<uint64_t, std::vector<int>>& hashMap); | 77 | std::map<uint64_t, std::vector<int>>& hashMap); |
| 49 | void SetHashOrderInfoOnOps(int funcMagic, const OperationsViewer& opOriList, | 78 | void SetHashOrderInfoOnOps(int funcMagic, const OperationsViewer& opOriList, |
| @@ -57,11 +86,14 @@ private: | |||
| 57 | std::vector<std::vector<int>> SortColorWithInput(std::vector<int>& colorValues) const; | 86 | std::vector<std::vector<int>> SortColorWithInput(std::vector<int>& colorValues) const; |
| 58 | Status MergeProcess(const OperationsViewer& opOriList, std::map<uint64_t, std::vector<int>>& hashMap, | 87 | Status MergeProcess(const OperationsViewer& opOriList, std::map<uint64_t, std::vector<int>>& hashMap, |
| 59 | std::map<uint64_t, size_t>& hashMergeNum, std::vector<uint64_t>& hashColor); | 88 | std::map<uint64_t, size_t>& hashMergeNum, std::vector<uint64_t>& hashColor); |
| 60 | - Status ColorTopo(int& color1, std::vector<std::vector<int>>& inputColor, std::vector<std::vector<int>>& outputColor, | ||
| 61 | - OperationsViewer& opOriList); | ||
| 62 | void MergePingPong(std::vector<std::vector<int>>& sortedColors, const OperationsViewer& opOriList, | 89 | void MergePingPong(std::vector<std::vector<int>>& sortedColors, const OperationsViewer& opOriList, |
| 63 | std::vector<uint64_t>& hashColor, size_t& numDBmerge, int hashOrder); | 90 | std::vector<uint64_t>& hashColor, size_t& numDBmerge, int hashOrder); |
| 64 | std::map<uint64_t, size_t> SetNumDB(const Function& func, std::map<uint64_t, std::vector<int>>& hashMap); | 91 | std::map<uint64_t, size_t> SetNumDB(const Function& func, std::map<uint64_t, std::vector<int>>& hashMap); |
| 92 | + Status BuildHashMergeNum(const Function& func, std::map<uint64_t, std::vector<int>>& hashMap, | ||
| 93 | + std::map<uint64_t, size_t>& hashMergeNum); | ||
| 94 | + Status RunMergeByMode(const OperationsViewer& opOriList, std::map<uint64_t, std::vector<int>>& hashMap, | ||
| 95 | + std::map<uint64_t, size_t>& hashMergeNum, std::vector<uint64_t>& hashColor); | ||
| 96 | + void LogHashOverview(const Function& func, const std::map<uint64_t, std::vector<int>>& hashMap) const; | ||
| 65 | void ApplyByFuncNumDB(int currentFuncMagic, std::map<uint64_t, size_t>& numDBMap, | 97 | void ApplyByFuncNumDB(int currentFuncMagic, std::map<uint64_t, size_t>& numDBMap, |
| 66 | std::map<uint64_t, std::vector<int>>& hashMap); | 98 | std::map<uint64_t, std::vector<int>>& hashMap); |
| 67 | void ApplyGlobalNumDB(std::map<uint64_t, size_t>& numDBMap, std::map<uint64_t, std::vector<int>>& hashMap); | 99 | void ApplyGlobalNumDB(std::map<uint64_t, size_t>& numDBMap, std::map<uint64_t, std::vector<int>>& hashMap); |
| @@ -70,18 +102,18 @@ private: | |||
| 70 | const std::map<uint64_t, std::vector<int>>& hashMap, | 102 | const std::map<uint64_t, std::vector<int>>& hashMap, |
| 71 | const std::map<uint64_t, size_t>& hashMergeNum, std::vector<uint64_t>& hashColor); | 103 | const std::map<uint64_t, size_t>& hashMergeNum, std::vector<uint64_t>& hashColor); |
| 72 | Status InitVecNBufferModeBySetting(); | 104 | Status InitVecNBufferModeBySetting(); |
| 105 | + Status CollectSemanticLabelOverrides(const OperationsViewer& opOriList, const std::vector<uint64_t>& hashColor, | ||
| 106 | + std::map<uint64_t, size_t>& labelOverrides) const; | ||
| 73 | Status ApplySemanticLabelSettings(const OperationsViewer& opOriList, std::map<uint64_t, size_t>& hashMergeNum, | 107 | Status ApplySemanticLabelSettings(const OperationsViewer& opOriList, std::map<uint64_t, size_t>& hashMergeNum, |
| 74 | const std::map<uint64_t, std::vector<int>>& hashMap, | 108 | const std::map<uint64_t, std::vector<int>>& hashMap, |
| 75 | const std::vector<uint64_t>& hashColor); | 109 | const std::vector<uint64_t>& hashColor); |
| 76 | 110 | ||
| 77 | private: | 111 | private: |
| 78 | int colorNum_{0}; | 112 | int colorNum_{0}; |
| 79 | - std::vector<std::vector<int>> inGraph_; | ||
| 80 | - std::vector<std::vector<int>> outGraph_; | ||
| 81 | std::vector<std::vector<int>> inColor_; | 113 | std::vector<std::vector<int>> inColor_; |
| 82 | std::vector<std::vector<int>> outColor_; | 114 | std::vector<std::vector<int>> outColor_; |
| 83 | std::vector<std::vector<int>> colorNode_; | 115 | std::vector<std::vector<int>> colorNode_; |
| 84 | - std::unordered_map<int, int> dfsColorOrder_; | 116 | + std::unordered_map<int, int> colorTopoOrder_; |
| 85 | std::vector<int> colorCycles_; | 117 | std::vector<int> colorCycles_; |
| 86 | int vecNBuffermode_; | 118 | int vecNBuffermode_; |
| 87 | int mgVecParallelLb_; | 119 | int mgVecParallelLb_; |


🟡 建议修改:producerOps 排序与去重语义不一致
producerOps按GetOpMagic()排序,但std::unique使用默认==(比较 index 值而非 magic)。若两个不同 op 拥有相同 magic,排序后相邻但 unique 不会合并,且std::sort不稳定导致相同 magic 的相对顺序不确定,进而影响下游BuildTopoHashContribution遍历 producerOps 时的 hash 确定性。建议:若 magic 在实践中保证唯一,加一行注释说明此假设;若需按 magic 去重,改用自定义谓词
std::unique(..., [&](int a, int b){ return opList[a].GetOpMagic() == opList[b].GetOpMagic(); })。