Merged
fix(pass): Avoid null pointer errors. #7445
wangqun23Add on 19 days ago
fix(pass): Avoid null pointer errors. #7445
Merged
Merged fromwangqun23/pypto_preGraphUTmasterintocann/pyptomaster
Total: 5 files changed+143-21
| @@ -47,7 +47,7 @@ public: | |||
| 47 | if (it != inferShapeFuncs_.end()) { | 47 | if (it != inferShapeFuncs_.end()) { |
| 48 | it->second(op, outValidShapes); | 48 | it->second(op, outValidShapes); |
| 49 | } else { | 49 | } else { |
| 50 | - PASS_LOGW("Infer shape failed, opcode [%s] doesn't support infer shape.", op->GetOpcodeStr().c_str()); | 50 | + PASS_LOGD("Infer shape failed, opcode [%s] doesn't support infer shape.", op->GetOpcodeStr().c_str()); |
| 51 | // 如果op infershape未注册,那么validshape设置成shape | 51 | // 如果op infershape未注册,那么validshape设置成shape |
| 52 | for (auto output : op->GetOOperands()) { | 52 | for (auto output : op->GetOOperands()) { |
| 53 | auto immShape = OpImmediate::Specified(output->GetShape()); | 53 | auto immShape = OpImmediate::Specified(output->GetShape()); |
| @@ -51,8 +51,8 @@ void LargeBitmap::ResizeBits(const size_t newSize) | |||
| 51 | void LargeBitmap::ClearBit(const size_t bitIdx) | 51 | void LargeBitmap::ClearBit(const size_t bitIdx) |
| 52 | { | 52 | { |
| 53 | if (bitIdx >= size_) { | 53 | if (bitIdx >= size_) { |
| 54 | - APASS_LOG_WARN_F(Elements::Function, | 54 | + APASS_LOG_DEBUG_F(Elements::Function, |
| 55 | - "Func LargeBitmap::ClearBit bitIdx %zu is not valid, total size is %zu bits.", bitIdx, size_); | 55 | + "Func LargeBitmap::ClearBit bitIdx %zu is not valid, total size is %zu bits.", bitIdx, size_); |
| 56 | return; | 56 | return; |
| 57 | } | 57 | } |
| 58 | bits_[bitIdx >> RIGHT_SHIFT_SIZE] &= ~(1UL << (bitIdx % BITS_EACH_VALUE)); | 58 | bits_[bitIdx >> RIGHT_SHIFT_SIZE] &= ~(1UL << (bitIdx % BITS_EACH_VALUE)); |
| @@ -1165,18 +1165,28 @@ std::unordered_map<LogicalTensorPtr, int> ReplaceTensor::BuildTensorOrderIndexMa | |||
| 1165 | { | 1165 | { |
| 1166 | std::unordered_map<LogicalTensorPtr, int> tensorToOrderIndex; | 1166 | std::unordered_map<LogicalTensorPtr, int> tensorToOrderIndex; |
| 1167 | int index = 0; | 1167 | int index = 0; |
| 1168 | + auto addTensor = [&tensorToOrderIndex, &index](const LogicalTensorPtr& tensor) { | ||
| 1169 | + // Skip null entries and retain the first-seen order used by base selection. | ||
| 1170 | + if (tensor != nullptr && !tensorToOrderIndex.count(tensor)) { | ||
| 1171 | + tensorToOrderIndex[tensor] = index++; | ||
| 1172 | + } | ||
| 1173 | + }; | ||
| 1168 | for (const auto& op : function.Operations()) { | 1174 | for (const auto& op : function.Operations()) { |
| 1169 | for (const auto& inTensor : op.GetIOperands()) { | 1175 | for (const auto& inTensor : op.GetIOperands()) { |
| 1170 | - if (!tensorToOrderIndex.count(inTensor)) { | 1176 | + addTensor(inTensor); |
| 1171 | - tensorToOrderIndex[inTensor] = index++; | ||
| 1172 | - } | ||
| 1173 | } | 1177 | } |
| 1174 | for (const auto& outTensor : op.GetOOperands()) { | 1178 | for (const auto& outTensor : op.GetOOperands()) { |
| 1175 | - if (!tensorToOrderIndex.count(outTensor)) { | 1179 | + addTensor(outTensor); |
| 1176 | - tensorToOrderIndex[outTensor] = index++; | ||
| 1177 | - } | ||
| 1178 | } | 1180 | } |
| 1179 | } | 1181 | } |
| 1182 | + // tensors may be disconnected from all ops while sharing rawmagic with an Assemble output. | ||
| 1183 | + // Pre-register them because UnionFind::Find requires every tensor to have an initialized root. | ||
| 1184 | + for (const auto& tensor : function.GetIncast()) { | ||
| 1185 | + addTensor(tensor); | ||
| 1186 | + } | ||
| 1187 | + for (const auto& tensor : function.GetOutcast()) { | ||
| 1188 | + addTensor(tensor); | ||
| 1189 | + } | ||
| 1180 | return tensorToOrderIndex; | 1190 | return tensorToOrderIndex; |
| 1181 | } | 1191 | } |
| 1182 | 1192 | ||
| @@ -72,9 +72,14 @@ class UnionFind { | |||
| 72 | public: | 72 | public: |
| 73 | explicit UnionFind(std::unordered_map<LogicalTensorPtr, int>& tensorToOrderIndex) | 73 | explicit UnionFind(std::unordered_map<LogicalTensorPtr, int>& tensorToOrderIndex) |
| 74 | { | 74 | { |
| 75 | - for (auto it = tensorToOrderIndex.begin(); it != tensorToOrderIndex.end(); it++) { | 75 | + for (const auto& entry : tensorToOrderIndex) { |
| 76 | - parentMap[it->first] = it->first; | 76 | + const auto& tensor = entry.first; |
| 77 | - rankMap[it->first] = 1; | 77 | + if (tensor == nullptr) { |
| 78 | + // Null operands are not valid union-find vertices. | ||
| 79 | + continue; | ||
| 80 | + } | ||
| 81 | + parentMap.emplace(tensor, tensor); | ||
| 82 | + rankMap.emplace(tensor, 1); | ||
| 78 | } | 83 | } |
| 79 | } | 84 | } |
| 80 | 85 | ||
| @@ -85,21 +90,25 @@ public: | |||
| 85 | if (xRoot == yRoot) { | 90 | if (xRoot == yRoot) { |
| 86 | return; | 91 | return; |
| 87 | } | 92 | } |
| 88 | - if (rankMap[xRoot] < rankMap[yRoot]) { | 93 | + if (rankMap.at(xRoot) < rankMap.at(yRoot)) { |
| 89 | parentMap[xRoot] = yRoot; | 94 | parentMap[xRoot] = yRoot; |
| 90 | - } else if (rankMap[xRoot] > rankMap[yRoot]) { | 95 | + } else if (rankMap.at(xRoot) > rankMap.at(yRoot)) { |
| 91 | parentMap[yRoot] = xRoot; | 96 | parentMap[yRoot] = xRoot; |
| 92 | } else { | 97 | } else { |
| 93 | parentMap[yRoot] = xRoot; | 98 | parentMap[yRoot] = xRoot; |
| 94 | - rankMap[xRoot]++; | 99 | + rankMap.at(xRoot)++; |
| 95 | } | 100 | } |
| 96 | } | 101 | } |
| 97 | 102 | ||
| 98 | - std::vector<LogicalTensors> GetGroups() const | 103 | + std::vector<LogicalTensors> GetGroups() |
| 99 | { | 104 | { |
| 100 | std::unordered_map<LogicalTensorPtr, LogicalTensors> rootToGroup; | 105 | std::unordered_map<LogicalTensorPtr, LogicalTensors> rootToGroup; |
| 101 | for (const auto& pair : parentMap) { | 106 | for (const auto& pair : parentMap) { |
| 102 | LogicalTensorPtr obj = pair.first; | 107 | LogicalTensorPtr obj = pair.first; |
| 108 | + if (obj == nullptr) { | ||
| 109 | + // Keep any legacy null key out of the returned tensor groups. | ||
| 110 | + continue; | ||
| 111 | + } | ||
| 103 | LogicalTensorPtr root = Find(obj); | 112 | LogicalTensorPtr root = Find(obj); |
| 104 | rootToGroup[root].push_back(obj); | 113 | rootToGroup[root].push_back(obj); |
| 105 | } | 114 | } |
| @@ -111,14 +120,19 @@ public: | |||
| 111 | } | 120 | } |
| 112 | 121 | ||
| 113 | private: | 122 | private: |
| 114 | - LogicalTensorPtr Find(const LogicalTensorPtr& x) const | 123 | + LogicalTensorPtr Find(const LogicalTensorPtr& x) |
| 115 | { | 124 | { |
| 116 | - if (parentMap[x] != x) { | 125 | + if (x == nullptr) { |
| 117 | - parentMap[x] = Find(parentMap[x]); | 126 | + // A null tensor has no representative in the disjoint set. |
| 127 | + return nullptr; | ||
| 118 | } | 128 | } |
| 119 | - return parentMap[x]; | 129 | + auto it = parentMap.find(x); |
| 130 | + if (it->second != x) { | ||
| 131 | + it->second = Find(it->second); | ||
| 132 | + } | ||
| 133 | + return it->second; | ||
| 120 | } | 134 | } |
| 121 | - mutable std::unordered_map<LogicalTensorPtr, LogicalTensorPtr> parentMap; | 135 | + std::unordered_map<LogicalTensorPtr, LogicalTensorPtr> parentMap; |
| 122 | std::unordered_map<LogicalTensorPtr, int> rankMap; | 136 | std::unordered_map<LogicalTensorPtr, int> rankMap; |
| 123 | }; | 137 | }; |
| 124 | 138 | ||
| @@ -2193,5 +2193,103 @@ TEST_F(ReplaceTensorTest, MemoryIdLinkedAliasWithExistingActualRawMagicIsNotOver | |||
| 2193 | EXPECT_EQ(graph.originalRaw->actualRawmagic, -1); | 2193 | EXPECT_EQ(graph.originalRaw->actualRawmagic, -1); |
| 2194 | } | 2194 | } |
| 2195 | 2195 | ||
| 2196 | +/* | ||
| 2197 | + * connectedIncast -> COPY_IN(offset=0,0) -> ASSEMBLE(offset=0,0) -> output1 [outcast, rawmagic R] | ||
| 2198 | + * \-> COPY_IN(offset=8,0) -> ASSEMBLE(offset=0,0) -> output2 [outcast, rawmagic R] | ||
| 2199 | + * unconnectedIncast [incast, no op connection] shares rawmagic R. | ||
| 2200 | + * output1 -> MULS -> COPY_OUT(offset=0,0) --+ | ||
| 2201 | + * output2 -> MULS -> COPY_OUT(offset=8,0) --+-> outcast | ||
| 2202 | + * The first ASSEMBLE produces a token consumed by the second ASSEMBLE. | ||
| 2203 | + */ | ||
| 2204 | +inline void ConstructUnconnectedIncastGraph(std::shared_ptr<Function>& function) | ||
| 2205 | +{ | ||
| 2206 | + Shape tileShape = {kNumEight, 32}, inputShape = {kNumSixteen, 32}; | ||
| 2207 | + std::vector<Offset> tileOffsets = {{kNumZero, kNumZero}, {kNumEight, kNumZero}}; | ||
| 2208 | + auto makeTensor = [](const Shape& shape, MemoryType memoryType, std::shared_ptr<RawTensor> raw = nullptr) { | ||
| 2209 | + if (raw == nullptr) { | ||
| 2210 | + raw = std::make_shared<RawTensor>(DT_FP32, shape); | ||
| 2211 | + } | ||
| 2212 | + auto tensor = IRBuilder().CreateTensorVar(raw, Offset(shape.size(), kNumZero), shape, | ||
| 2213 | + CreateTestConstIntVector(shape)); | ||
| 2214 | + tensor->SetMemoryTypeBoth(memoryType, true); | ||
| 2215 | + return tensor; | ||
| 2216 | + }; | ||
| 2217 | + auto connectedIncast = makeTensor(inputShape, MEM_DEVICE_DDR); | ||
| 2218 | + auto sharedRaw = std::make_shared<RawTensor>(DT_FP32, tileShape); | ||
| 2219 | + auto unconnectedIncast = makeTensor(tileShape, MEM_DEVICE_DDR, sharedRaw); | ||
| 2220 | + auto copyInShape = OpImmediate::Specified(tileShape); | ||
| 2221 | + LogicalTensors assembleOutputs; | ||
| 2222 | + std::vector<Operation*> assembleOps; | ||
| 2223 | + for (size_t i = 0; i < tileOffsets.size(); ++i) { | ||
| 2224 | + auto input = makeTensor(tileShape, MEM_UB); | ||
| 2225 | + auto output = makeTensor(tileShape, MEM_UB, sharedRaw); | ||
| 2226 | + auto& copyIn = PassOperationUtils::AddOperation(*function, Opcode::OP_COPY_IN, {connectedIncast}, {input}); | ||
| 2227 | + copyIn.SetOpAttribute(std::make_shared<CopyOpAttribute>(OpImmediate::Specified(tileOffsets[i]), MEM_UB, | ||
| 2228 | + copyInShape, copyInShape)); | ||
| 2229 | + auto& assemble = PassOperationUtils::AddOperation(*function, Opcode::OP_ASSEMBLE, {input}, {output}); | ||
| 2230 | + assemble.SetOpAttribute(std::make_shared<AssembleOpAttribute>(MEM_UB, Offset(tileShape.size(), kNumZero))); | ||
| 2231 | + assembleOutputs.push_back(output); | ||
| 2232 | + assembleOps.push_back(&assemble); | ||
| 2233 | + } | ||
| 2234 | + auto outcast = makeTensor(inputShape, MEM_DEVICE_DDR); | ||
| 2235 | + function->inCasts_ = {connectedIncast, unconnectedIncast}; | ||
| 2236 | + function->outCasts_ = {assembleOutputs[0], outcast, assembleOutputs[1]}; | ||
| 2237 | + auto copyOutShape = OpImmediate::Specified(tileShape); | ||
| 2238 | + auto copyOutRawShape = OpImmediate::Specified(inputShape); | ||
| 2239 | + for (size_t i = 0; i < tileOffsets.size(); ++i) { | ||
| 2240 | + auto output = makeTensor(tileShape, MEM_UB); | ||
| 2241 | + auto& muls = PassOperationUtils::AddOperation(*function, Opcode::OP_MULS, {assembleOutputs[i]}, {output}); | ||
| 2242 | + muls.SetAttribute(OpAttributeKey::scalar, Element(DT_FP32, 5.0f)); | ||
| 2243 | + auto& copyOut = PassOperationUtils::AddOperation(*function, Opcode::OP_COPY_OUT, {output}, {outcast}); | ||
| 2244 | + copyOut.SetOpAttribute(std::make_shared<CopyOpAttribute>(MEM_UB, OpImmediate::Specified(tileOffsets[i]), | ||
| 2245 | + copyOutShape, copyOutRawShape)); | ||
| 2246 | + } | ||
| 2247 | + auto token = IRBuilder().CreateTokenVar(ir::Span::Unknown()); | ||
| 2248 | + assembleOps[0]->result_token_ = {token}; | ||
| 2249 | + assembleOps[1]->tokens_ = {token}; | ||
| 2250 | + function->GetVarDependency().AddProducer(token, ToStmt(*assembleOps[0])); | ||
| 2251 | + function->GetVarDependency().AddConsumer(token, ToStmt(*assembleOps[1])); | ||
| 2252 | +} | ||
| 2253 | + | ||
| 2254 | +TEST_F(ReplaceTensorTest, CheckUnconnectedIncast) | ||
| 2255 | +{ | ||
| 2256 | + auto function = std::make_shared<Function>(Program::GetInstance(), "IncastSibling", "IncastSibling", nullptr); | ||
| 2257 | + ConstructUnconnectedIncastGraph(function); | ||
| 2258 | + auto sharedRaw = function->inCasts_[1]->GetRawTensor(); | ||
| 2259 | + auto unconnectedIncast = function->inCasts_[1]; | ||
| 2260 | + std::vector<Operation*> assembleOps; | ||
| 2261 | + for (auto& op : function->Operations()) { | ||
| 2262 | + if (op.GetOpcode() == Opcode::OP_ASSEMBLE) { | ||
| 2263 | + assembleOps.push_back(&op); | ||
| 2264 | + } | ||
| 2265 | + } | ||
| 2266 | + ASSERT_EQ(assembleOps.size(), 2U); | ||
| 2267 | + ASSERT_NE(assembleOps[0], nullptr); | ||
| 2268 | + ASSERT_NE(assembleOps[1], nullptr); | ||
| 2269 | + auto token = assembleOps[0]->result_token_.front(); | ||
| 2270 | + auto firstMulsConsumers = assembleOps[0]->GetOutputOperand(0)->GetConsumers(); | ||
| 2271 | + ASSERT_FALSE(firstMulsConsumers.empty()); | ||
| 2272 | + auto* firstMuls = *firstMulsConsumers.begin(); | ||
| 2273 | + ASSERT_NE(firstMuls, nullptr); | ||
| 2274 | + auto secondCopyInProducers = assembleOps[1]->GetInputOperand(0)->GetProducers(); | ||
| 2275 | + ASSERT_FALSE(secondCopyInProducers.empty()); | ||
| 2276 | + auto* secondCopyIn = *secondCopyInProducers.begin(); | ||
| 2277 | + ASSERT_NE(secondCopyIn, nullptr); | ||
| 2278 | + ASSERT_EQ(firstMuls->GetOpcode(), Opcode::OP_MULS); | ||
| 2279 | + ASSERT_EQ(secondCopyIn->GetOpcode(), Opcode::OP_COPY_IN); | ||
| 2280 | + ReplaceTensor pass; | ||
| 2281 | + ASSERT_EQ(pass.RunOnFunction(*function), SUCCESS); | ||
【🟡 建议修改】后置断言使用了 pass 运行前捕获的 用例在 建议:只记录 ![]() ![]() | |||
| 2282 | + for (auto* assemble : assembleOps) { | ||
| 2283 | + EXPECT_EQ(assemble->GetOutputOperand(0)->GetRawMagic(), sharedRaw->rawmagic); | ||
| 2284 | + } | ||
| 2285 | + EXPECT_EQ(unconnectedIncast->GetRawMagic(), sharedRaw->rawmagic); | ||
| 2286 | + ASSERT_EQ(firstMuls->result_token_.size(), 1U); | ||
| 2287 | + ASSERT_EQ(secondCopyIn->tokens_.size(), 1U); | ||
| 2288 | + EXPECT_NE(firstMuls->result_token_.front(), token); | ||
| 2289 | + EXPECT_EQ(secondCopyIn->tokens_.front(), firstMuls->result_token_.front()); | ||
| 2290 | + EXPECT_FALSE(function->GetVarDependency().HasDependency(token)); | ||
| 2291 | + CheckTokenDependencies(*function); | ||
| 2292 | +} | ||
| 2293 | + | ||
| 2196 | } // namespace tile_fwk | 2294 | } // namespace tile_fwk |
| 2197 | } // namespace npu | 2295 | } // namespace npu |


【🟢 可选优化】
Find具备插入能力,但GetGroups在遍历parentMap时调用它Find(本行起)在 key 缺失时会parentMap.emplace/rankMap.emplace,而GetGroups(header 第 107-124 行)在for (const auto& pair : parentMap)的循环体里调用Find(obj)。当前确实不会插入:循环取出的 key 一定已在
parentMap中,递归的it->second也都是Unite里经Find取得并登记过的根,所以不变式成立、不会 rehash。但「遍历unordered_map期间调用的函数具备插入能力」这个组合是隐患:一旦后续改动让Find的入参来自parentMap之外(或递归链上出现未登记的父节点),插入触发 rehash 会使 range-for 的迭代器失效。建议:
GetGroups先拷贝 key 到std::vector再遍历,或在Find里用ASSERT(parentMap.count(x))把「递归父节点必已登记」的不变式固化下来。