Merged
feat(ir): Support versioned assemble outputs #5787
赵俊涵Add on Aug 29
feat(ir): Support versioned assemble outputs #5787
Merged
Total: 12 files changed+359-37
| @@ -86,6 +86,7 @@ struct ViewKey { | |||
| 86 | struct TensorDependency { | 86 | struct TensorDependency { |
| 87 | std::vector<int> producers; | 87 | std::vector<int> producers; |
| 88 | int pendingConsumerCount{0}; | 88 | int pendingConsumerCount{0}; |
| 89 | + bool hasSelfConsumer{false}; | ||
| 89 | }; | 90 | }; |
| 90 | 91 | ||
| 91 | struct SortContext { | 92 | struct SortContext { |
| @@ -93,14 +94,20 @@ struct SortContext { | |||
| 93 | std::unordered_map<const LogicalTensor*, size_t> tensorToDep; | 94 | std::unordered_map<const LogicalTensor*, size_t> tensorToDep; |
| 94 | std::vector<TensorDependency> tensorDeps; | 95 | std::vector<TensorDependency> tensorDeps; |
| 95 | std::vector<std::vector<size_t>> tensorDepsByConsumer; | 96 | std::vector<std::vector<size_t>> tensorDepsByConsumer; |
| 97 | + std::vector<std::vector<int>> tokenProducersByConsumer; | ||
| 96 | std::vector<int> outDegree; | 98 | std::vector<int> outDegree; |
| 97 | }; | 99 | }; |
| 98 | 100 | ||
| 99 | class LightweightOperationSorter { | 101 | class LightweightOperationSorter { |
| 100 | public: | 102 | public: |
| 101 | LightweightOperationSorter(const Function& function, const std::vector<std::shared_ptr<Operation>>& operations, | 103 | LightweightOperationSorter(const Function& function, const std::vector<std::shared_ptr<Operation>>& operations, |
| 102 | - const LogicalTensors& inCasts, const LogicalTensors& outCasts) | 104 | + const LogicalTensors& inCasts, const LogicalTensors& outCasts, |
| 103 | - : function_(function), operations_(operations), inCasts_(inCasts), outCasts_(outCasts) | 105 | + bool preserveOriginalOrder) |
| 106 | + : function_(function), | ||
| 107 | + operations_(operations), | ||
| 108 | + inCasts_(inCasts), | ||
| 109 | + outCasts_(outCasts), | ||
| 110 | + preserveOriginalOrder_(preserveOriginalOrder) | ||
| 104 | {} | 111 | {} |
| 105 | 112 | ||
| 106 | std::vector<std::shared_ptr<Operation>> Sort() | 113 | std::vector<std::shared_ptr<Operation>> Sort() |
| @@ -110,6 +117,7 @@ public: | |||
| 110 | // producer-list scans during lightweight sorting. | 117 | // producer-list scans during lightweight sorting. |
| 111 | BuildOpIndex(); | 118 | BuildOpIndex(); |
| 112 | BuildTensorDeps(); | 119 | BuildTensorDeps(); |
| 120 | + BuildTokenDeps(); | ||
| 113 | BuildOutDegree(); | 121 | BuildOutDegree(); |
| 114 | auto sortedOperations = RunTopologicalSort(); | 122 | auto sortedOperations = RunTopologicalSort(); |
| 115 | CheckSortedOperations(sortedOperations); | 123 | CheckSortedOperations(sortedOperations); |
| @@ -158,6 +166,9 @@ private: | |||
| 158 | if (dep.producers.empty()) { | 166 | if (dep.producers.empty()) { |
| 159 | return; | 167 | return; |
| 160 | } | 168 | } |
| 169 | + if (std::find(dep.producers.begin(), dep.producers.end(), consumerIdx) != dep.producers.end()) { | ||
| 170 | + dep.hasSelfConsumer = true; | ||
| 171 | + } | ||
| 161 | dep.pendingConsumerCount++; | 172 | dep.pendingConsumerCount++; |
| 162 | context_.tensorDepsByConsumer[consumerIdx].emplace_back(depIndex); | 173 | context_.tensorDepsByConsumer[consumerIdx].emplace_back(depIndex); |
| 163 | } | 174 | } |
| @@ -186,41 +197,87 @@ private: | |||
| 186 | } | 197 | } |
| 187 | } | 198 | } |
| 188 | 199 | ||
| 200 | + void BuildTokenDeps() | ||
| 201 | + { | ||
| 202 | + context_.tokenProducersByConsumer.resize(operations_.size()); | ||
| 203 | + for (size_t idx = 0; idx < operations_.size(); idx++) { | ||
| 204 | + auto& producerIndexes = context_.tokenProducersByConsumer[idx]; | ||
| 205 | + for (auto* producer : operations_[idx]->ProducerOpsByToken()) { | ||
| 206 | + if (producer == operations_[idx].get()) { | ||
| 207 | + continue; | ||
| 208 | + } | ||
| 209 | + auto producerIter = context_.opToIndex.find(producer); | ||
| 210 | + if (producerIter != context_.opToIndex.end()) { | ||
| 211 | + producerIndexes.emplace_back(producerIter->second); | ||
| 212 | + } | ||
| 213 | + } | ||
| 214 | + std::sort(producerIndexes.begin(), producerIndexes.end()); | ||
| 215 | + producerIndexes.erase(std::unique(producerIndexes.begin(), producerIndexes.end()), producerIndexes.end()); | ||
| 216 | + } | ||
| 217 | + } | ||
| 218 | + | ||
| 189 | void BuildOutDegree() | 219 | void BuildOutDegree() |
| 190 | { | 220 | { |
| 191 | context_.outDegree.assign(operations_.size(), 0); | 221 | context_.outDegree.assign(operations_.size(), 0); |
| 192 | for (auto& dep : context_.tensorDeps) { | 222 | for (auto& dep : context_.tensorDeps) { |
| 193 | - if (dep.producers.empty() || dep.pendingConsumerCount == 0) { | 223 | + if (dep.producers.empty() || dep.pendingConsumerCount == 0 || dep.hasSelfConsumer) { |
| 194 | continue; | 224 | continue; |
| 195 | } | 225 | } |
| 196 | for (int producerIdx : dep.producers) { | 226 | for (int producerIdx : dep.producers) { |
| 197 | context_.outDegree[producerIdx]++; | 227 | context_.outDegree[producerIdx]++; |
| 198 | } | 228 | } |
| 199 | } | 229 | } |
| 230 | + for (size_t consumerIdx = 0; consumerIdx < context_.tensorDepsByConsumer.size(); ++consumerIdx) { | ||
| 231 | + for (size_t depIndex : context_.tensorDepsByConsumer[consumerIdx]) { | ||
| 232 | + const auto& dep = context_.tensorDeps[depIndex]; | ||
| 233 | + if (!dep.hasSelfConsumer) { | ||
| 234 | + continue; | ||
| 235 | + } | ||
| 236 | + for (int producerIdx : dep.producers) { | ||
| 237 | + if (producerIdx != static_cast<int>(consumerIdx)) { | ||
| 238 | + context_.outDegree[producerIdx]++; | ||
| 239 | + } | ||
| 240 | + } | ||
| 241 | + } | ||
| 242 | + } | ||
| 243 | + for (const auto& producerIndexes : context_.tokenProducersByConsumer) { | ||
| 244 | + for (int producerIdx : producerIndexes) { | ||
| 245 | + context_.outDegree[producerIdx]++; | ||
| 246 | + } | ||
| 247 | + } | ||
| 200 | } | 248 | } |
| 201 | 249 | ||
| 202 | std::vector<std::shared_ptr<Operation>> RunTopologicalSort() | 250 | std::vector<std::shared_ptr<Operation>> RunTopologicalSort() |
| 203 | { | 251 | { |
| 204 | - // Sort initial ready ops by opmagic to ensure deterministic topological order. | ||
| 205 | std::vector<int> readyList; | 252 | std::vector<int> readyList; |
| 206 | for (size_t idx = 0; idx < operations_.size(); idx++) { | 253 | for (size_t idx = 0; idx < operations_.size(); idx++) { |
| 207 | if (context_.outDegree[idx] == 0) { | 254 | if (context_.outDegree[idx] == 0) { |
| 208 | readyList.push_back(static_cast<int>(idx)); | 255 | readyList.push_back(static_cast<int>(idx)); |
| 209 | } | 256 | } |
| 210 | } | 257 | } |
| 211 | - std::sort(readyList.begin(), readyList.end(), | 258 | + if (!preserveOriginalOrder_) { |
| 212 | - [this](int a, int b) { return operations_[a]->GetOpMagic() < operations_[b]->GetOpMagic(); }); | 259 | + // Sort initial ready ops by opmagic to ensure deterministic topological order. |
| 213 | - std::queue<int> readyOps; | 260 | + std::sort(readyList.begin(), readyList.end(), |
| 214 | - for (int idx : readyList) { | 261 | + [this](int a, int b) { return operations_[a]->GetOpMagic() < operations_[b]->GetOpMagic(); }); |
| 215 | - readyOps.emplace(idx); | ||
| 216 | } | 262 | } |
| 217 | - auto releasePrevOp = [this, &readyOps](int prevOpIndex) { | 263 | + for (int idx : readyList) { |
| 264 | + PushReadyOp(idx); | ||
| 265 | + } | ||
| 266 | + auto releasePrevOp = [this](int prevOpIndex) { | ||
| 218 | if (--context_.outDegree[prevOpIndex] == 0) { | 267 | if (--context_.outDegree[prevOpIndex] == 0) { |
| 219 | - readyOps.emplace(prevOpIndex); | 268 | + PushReadyOp(prevOpIndex); |
| 220 | } | 269 | } |
| 221 | }; | 270 | }; |
| 222 | - auto releaseTensorDep = [this, &releasePrevOp](size_t depIndex) { | 271 | + auto releaseTensorDep = [this, &releasePrevOp](size_t depIndex, int consumerIdx) { |
| 223 | auto& dep = context_.tensorDeps[depIndex]; | 272 | auto& dep = context_.tensorDeps[depIndex]; |
| 273 | + if (dep.hasSelfConsumer) { | ||
| 274 | + for (int producerIdx : dep.producers) { | ||
| 275 | + if (producerIdx != consumerIdx) { | ||
| 276 | + releasePrevOp(producerIdx); | ||
| 277 | + } | ||
| 278 | + } | ||
| 279 | + return; | ||
| 280 | + } | ||
| 224 | if (--dep.pendingConsumerCount == 0) { | 281 | if (--dep.pendingConsumerCount == 0) { |
| 225 | for (int producerIdx : dep.producers) { | 282 | for (int producerIdx : dep.producers) { |
| 226 | releasePrevOp(producerIdx); | 283 | releasePrevOp(producerIdx); |
| @@ -229,17 +286,45 @@ private: | |||
| 229 | }; | 286 | }; |
| 230 | std::vector<std::shared_ptr<Operation>> sortedOperations; | 287 | std::vector<std::shared_ptr<Operation>> sortedOperations; |
| 231 | sortedOperations.reserve(operations_.size()); | 288 | sortedOperations.reserve(operations_.size()); |
| 232 | - while (!readyOps.empty()) { | 289 | + while (HasReadyOp()) { |
| 233 | - int currIndex = readyOps.front(); | 290 | + int currIndex = PopReadyOp(); |
| 234 | - readyOps.pop(); | ||
| 235 | sortedOperations.emplace_back(operations_[currIndex]); | 291 | sortedOperations.emplace_back(operations_[currIndex]); |
| 236 | for (size_t depIndex : context_.tensorDepsByConsumer[currIndex]) { | 292 | for (size_t depIndex : context_.tensorDepsByConsumer[currIndex]) { |
| 237 | - releaseTensorDep(depIndex); | 293 | + releaseTensorDep(depIndex, currIndex); |
| 294 | + } | ||
| 295 | + for (int producerIdx : context_.tokenProducersByConsumer[currIndex]) { | ||
| 296 | + releasePrevOp(producerIdx); | ||
| 238 | } | 297 | } |
| 239 | } | 298 | } |
| 240 | return sortedOperations; | 299 | return sortedOperations; |
| 241 | } | 300 | } |
| 242 | 301 | ||
| 302 | + void PushReadyOp(int opIndex) | ||
| 303 | + { | ||
| 304 | + if (preserveOriginalOrder_) { | ||
| 305 | + stableReadyOps_.push(opIndex); | ||
| 306 | + } else { | ||
| 307 | + readyOps_.push(opIndex); | ||
| 308 | + } | ||
| 309 | + } | ||
| 310 | + | ||
| 311 | + [[nodiscard]] bool HasReadyOp() const | ||
| 312 | + { | ||
| 313 | + return preserveOriginalOrder_ ? !stableReadyOps_.empty() : !readyOps_.empty(); | ||
| 314 | + } | ||
| 315 | + | ||
| 316 | + int PopReadyOp() | ||
| 317 | + { | ||
| 318 | + if (preserveOriginalOrder_) { | ||
| 319 | + int opIndex = stableReadyOps_.top(); | ||
| 320 | + stableReadyOps_.pop(); | ||
| 321 | + return opIndex; | ||
| 322 | + } | ||
| 323 | + int opIndex = readyOps_.front(); | ||
| 324 | + readyOps_.pop(); | ||
| 325 | + return opIndex; | ||
| 326 | + } | ||
| 327 | + | ||
| 243 | void CheckSortedOperations(const std::vector<std::shared_ptr<Operation>>& sortedOperations) const | 328 | void CheckSortedOperations(const std::vector<std::shared_ptr<Operation>>& sortedOperations) const |
| 244 | { | 329 | { |
| 245 | for (auto& op : operations_) { | 330 | for (auto& op : operations_) { |
| @@ -255,6 +340,9 @@ private: | |||
| 255 | const std::vector<std::shared_ptr<Operation>>& operations_; | 340 | const std::vector<std::shared_ptr<Operation>>& operations_; |
| 256 | const LogicalTensors& inCasts_; | 341 | const LogicalTensors& inCasts_; |
| 257 | const LogicalTensors& outCasts_; | 342 | const LogicalTensors& outCasts_; |
| 343 | + bool preserveOriginalOrder_; | ||
| 344 | + std::queue<int> readyOps_; | ||
| 345 | + std::priority_queue<int> stableReadyOps_; | ||
| 258 | SortContext context_; | 346 | SortContext context_; |
| 259 | }; | 347 | }; |
| 260 | 348 | ||
| @@ -1316,6 +1404,15 @@ std::vector<std::shared_ptr<Operation>> Function::GetSortedOperations() const | |||
| 1316 | addProd(op.get(), outCasts_[index]); | 1404 | addProd(op.get(), outCasts_[index]); |
| 1317 | } | 1405 | } |
| 1318 | } | 1406 | } |
| 1407 | + for (auto* producer : op->ProducerOpsByToken()) { | ||
| 1408 | + if (producer == op.get() || producer->BelongTo() != this) { | ||
| 1409 | + continue; | ||
| 1410 | + } | ||
| 1411 | + auto producerIter = opToIndex.find(producer); | ||
| 1412 | + FE_ASSERT(FeError::NOT_EXIST, producerIter != opToIndex.end()) | ||
| 1413 | + << "Token producer not found in opToIndex: " << producer->Dump(); | ||
| 1414 | + outDegree[producerIter->second]++; | ||
| 1415 | + } | ||
| 1319 | } | 1416 | } |
| 1320 | for (auto& opGroup : operationGroups_) { | 1417 | for (auto& opGroup : operationGroups_) { |
| 1321 | for (size_t idx = 1; idx < opGroup.size(); idx++) { | 1418 | for (size_t idx = 1; idx < opGroup.size(); idx++) { |
| @@ -1366,6 +1463,17 @@ std::vector<std::shared_ptr<Operation>> Function::GetSortedOperations() const | |||
| 1366 | visit(op.get(), outCasts_[index]); | 1463 | visit(op.get(), outCasts_[index]); |
| 1367 | } | 1464 | } |
| 1368 | } | 1465 | } |
| 1466 | + for (auto* producer : op->ProducerOpsByToken()) { | ||
| 1467 | + if (producer == op.get() || producer->BelongTo() != this) { | ||
| 1468 | + continue; | ||
| 1469 | + } | ||
| 1470 | + auto producerIter = opToIndex.find(producer); | ||
| 1471 | + FE_ASSERT(FeError::NOT_EXIST, producerIter != opToIndex.end()) | ||
| 1472 | + << "Token producer not found in opToIndex: " << producer->Dump(); | ||
| 1473 | + if (--outDegree[producerIter->second] == 0) { | ||
| 1474 | + q.emplace(producerIter->second); | ||
| 1475 | + } | ||
| 1476 | + } | ||
| 1369 | } | 1477 | } |
| 1370 | for (auto& op : operations_) { | 1478 | for (auto& op : operations_) { |
| 1371 | FE_ASSERT(FeError::OP_DEPENDENCY_CYCLE, outDegree[opToIndex[op.get()]] == 0) | 1479 | FE_ASSERT(FeError::OP_DEPENDENCY_CYCLE, outDegree[opToIndex[op.get()]] == 0) |
| @@ -1378,10 +1486,10 @@ std::vector<std::shared_ptr<Operation>> Function::GetSortedOperations() const | |||
| 1378 | return sortedOperations; | 1486 | return sortedOperations; |
| 1379 | } | 1487 | } |
| 1380 | 1488 | ||
| 1381 | -std::vector<std::shared_ptr<Operation>> Function::GetLightweightSortedOperations() const | 1489 | +std::vector<std::shared_ptr<Operation>> Function::GetLightweightSortedOperations(bool preserveOriginalOrder) const |
| 1382 | { | 1490 | { |
| 1383 | FE_ASSERT(operationGroups_.empty()) << "Lightweight sort does not support operationGroups_."; | 1491 | FE_ASSERT(operationGroups_.empty()) << "Lightweight sort does not support operationGroups_."; |
| 1384 | - LightweightOperationSorter sorter(*this, operations_, inCasts_, outCasts_); | 1492 | + LightweightOperationSorter sorter(*this, operations_, inCasts_, outCasts_, preserveOriginalOrder); |
| 1385 | return sorter.Sort(); | 1493 | return sorter.Sort(); |
| 1386 | } | 1494 | } |
| 1387 | 1495 | ||
| @@ -1395,6 +1503,9 @@ void Function::SortOperations(SortOperationsMode mode) | |||
| 1395 | case SortOperationsMode::LIGHTWEIGHT: | 1503 | case SortOperationsMode::LIGHTWEIGHT: |
| 1396 | sortedOperations = GetLightweightSortedOperations(); | 1504 | sortedOperations = GetLightweightSortedOperations(); |
| 1397 | break; | 1505 | break; |
| 1506 | + case SortOperationsMode::LIGHTWEIGHT_STABLE: | ||
| 1507 | + sortedOperations = GetLightweightSortedOperations(true); | ||
| 1508 | + break; | ||
| 1398 | default: | 1509 | default: |
| 1399 | FE_ASSERT(FeError::INVALID_VAL, false) << "Invalid sort operations mode."; | 1510 | FE_ASSERT(FeError::INVALID_VAL, false) << "Invalid sort operations mode."; |
| 1400 | } | 1511 | } |
| @@ -2058,7 +2169,7 @@ void Function::AddOriginOutcast(const std::shared_ptr<LogicalTensor>& tensor) | |||
| 2058 | originOutCasts_.push_back(tensor); | 2169 | originOutCasts_.push_back(tensor); |
| 2059 | } | 2170 | } |
| 2060 | 2171 | ||
| 2061 | -void Function::SetSameMemId(const LogicalTensorPtr& operand, LogicalTensorPtr& dst) | 2172 | +void Function::SetSameMemId(const LogicalTensorPtr& operand, const LogicalTensorPtr& dst) |
| 2062 | { | 2173 | { |
| 2063 | FE_ASSERT(FeError::INVALID_TYPE, operand->Datatype() == dst->Datatype()) << "Check Dtype failed!"; | 2174 | FE_ASSERT(FeError::INVALID_TYPE, operand->Datatype() == dst->Datatype()) << "Check Dtype failed!"; |
| 2064 | 2175 | ||
| @@ -71,6 +71,7 @@ enum class EndFuncReturnParam { INPUT = 0, OUTPUT, ARGS }; | |||
| 71 | enum class SortOperationsMode { | 71 | enum class SortOperationsMode { |
| 72 | GENERAL, | 72 | GENERAL, |
| 73 | LIGHTWEIGHT, | 73 | LIGHTWEIGHT, |
| 74 | + LIGHTWEIGHT_STABLE, | ||
| 74 | }; | 75 | }; |
| 75 | 76 | ||
| 76 | enum class MixResourceType { | 77 | enum class MixResourceType { |
| @@ -570,6 +571,7 @@ class Function : public ir::Function { | |||
| 570 | public: | 571 | public: |
| 571 | friend class ExpandFunction; | 572 | friend class ExpandFunction; |
| 572 | friend class VFFusionPass; | 573 | friend class VFFusionPass; |
| 574 | + friend class TokenUtils; | ||
| 573 | 575 | ||
| 574 | std::vector<std::shared_ptr<LogicalTensor>> inCasts_; // Input tensors | 576 | std::vector<std::shared_ptr<LogicalTensor>> inCasts_; // Input tensors |
| 575 | std::vector<std::shared_ptr<LogicalTensor>> outCasts_; // Output tensors | 577 | std::vector<std::shared_ptr<LogicalTensor>> outCasts_; // Output tensors |
| @@ -644,7 +646,7 @@ public: | |||
| 644 | ir::Span span = ir::Span::Unknown()); | 646 | ir::Span span = ir::Span::Unknown()); |
| 645 | 647 | ||
| 646 | std::map<std::shared_ptr<RawTensor>, std::shared_ptr<RawTensor>> outIncastLinkMap; // 记录outcast 共享地址的 incast | 648 | std::map<std::shared_ptr<RawTensor>, std::shared_ptr<RawTensor>> outIncastLinkMap; // 记录outcast 共享地址的 incast |
| 647 | - void SetSameMemId(const LogicalTensorPtr& operand, LogicalTensorPtr& dst); | 649 | + void SetSameMemId(const LogicalTensorPtr& operand, const LogicalTensorPtr& dst); |
| 648 | void UpdateLinkMap(const std::shared_ptr<LogicalTensor>& oriLogicalTensor, | 650 | void UpdateLinkMap(const std::shared_ptr<LogicalTensor>& oriLogicalTensor, |
| 649 | const std::shared_ptr<LogicalTensor>& newLogicalTensor, const bool isOutCast = false); | 651 | const std::shared_ptr<LogicalTensor>& newLogicalTensor, const bool isOutCast = false); |
| 650 | 652 | ||
| @@ -1036,7 +1038,7 @@ private: | |||
| 1036 | std::unordered_map<LogicalTensorPtr, bool> outcastNeedAllocMap_; | 1038 | std::unordered_map<LogicalTensorPtr, bool> outcastNeedAllocMap_; |
| 1037 | 1039 | ||
| 1038 | private: | 1040 | private: |
| 1039 | - std::vector<std::shared_ptr<Operation>> GetLightweightSortedOperations() const; | 1041 | + std::vector<std::shared_ptr<Operation>> GetLightweightSortedOperations(bool preserveOriginalOrder = false) const; |
| 1040 | 1042 | ||
| 1041 | unsigned long ComputeHashOrderless() const; | 1043 | unsigned long ComputeHashOrderless() const; |
| 1042 | void OpValidCheck(Operation& op) const; | 1044 | void OpValidCheck(Operation& op) const; |
| @@ -21,6 +21,8 @@ | |||
| 21 | #include <vector> | 21 | #include <vector> |
| 22 | 22 | ||
| 23 | #include "interface/tensor/ir.h" | 23 | #include "interface/tensor/ir.h" |
| 24 | +#include "interface/tensor/ir_tensor_op_rebuild.h" | ||
| 25 | +#include "interface/tensor/irbuilder.h" | ||
| 24 | #include "ir/expr.h" | 26 | #include "ir/expr.h" |
| 25 | #include "ir/kind_traits.h" | 27 | #include "ir/kind_traits.h" |
| 26 | #include "ir/stmt.h" | 28 | #include "ir/stmt.h" |
| @@ -104,9 +106,13 @@ void FindLiveRootsRecursiveImpl(const std::vector<StmtPtr>& stmts, const Removab | |||
| 104 | if (is_leaf && !is_removable(stmt)) { | 106 | if (is_leaf && !is_removable(stmt)) { |
| 105 | auto all_refs = CollectStmtVarRefs(stmt); | 107 | auto all_refs = CollectStmtVarRefs(stmt); |
| 106 | // Do not keep an unconsumed result token alive just because its TensorOp is non-removable. | 108 | // Do not keep an unconsumed result token alive just because its TensorOp is non-removable. |
| 107 | - if (auto tensor_op = std::dynamic_pointer_cast<const TensorOpStmt>(stmt)) { | 109 | + // When the assemble-new-logical-tensor mechanism is active, result tokens are inferred by |
| 108 | - for (const auto& token : tensor_op->result_token_) { | 110 | + // InferTokenPass and must survive DCE so that RemoveRedundantTokenPass can analyse them. |
| 109 | - all_refs.erase(token.get()); | 111 | + if (!npu::tile_fwk::IRContext::Get().AssembleNewLogicalTensor()) { |
| 112 | + if (auto tensor_op = std::dynamic_pointer_cast<const TensorOpStmt>(stmt)) { | ||
| 113 | + for (const auto& token : tensor_op->result_token_) { | ||
| 114 | + all_refs.erase(token.get()); | ||
| 115 | + } | ||
| 110 | } | 116 | } |
| 111 | } | 117 | } |
| 112 | live.insert(all_refs.begin(), all_refs.end()); | 118 | live.insert(all_refs.begin(), all_refs.end()); |
| @@ -180,7 +186,25 @@ std::vector<StmtPtr> FilterDeadCodeImpl(const std::vector<StmtPtr>& stmts, const | |||
| 180 | if (!any_live) | 186 | if (!any_live) |
| 181 | continue; | 187 | continue; |
| 182 | } | 188 | } |
| 183 | - result.push_back(stmt); | 189 | + std::vector<VarPtr> liveTokens; |
| 190 | + if (npu::tile_fwk::IRContext::Get().AssembleNewLogicalTensor()) { | ||
| 191 | + // Preserve all result tokens so RemoveRedundantTokenPass can analyse the full | ||
| 192 | + // token dependency graph. Only tensor results determine statement liveness. | ||
| 193 | + liveTokens = tensor_op->result_token_; | ||
| 194 | + } else { | ||
| 195 | + for (const auto& token : tensor_op->result_token_) { | ||
| 196 | + if (live.count(token.get())) { | ||
| 197 | + liveTokens.push_back(token); | ||
| 198 | + } | ||
| 199 | + } | ||
| 200 | + } | ||
| 201 | + if (liveTokens.size() != tensor_op->result_token_.size()) { | ||
| 202 | + result.push_back(npu::tile_fwk::RebuildTensorOpStmt(tensor_op, tensor_op->result_, | ||
| 203 | + std::move(liveTokens), tensor_op->args_, | ||
| 204 | + tensor_op->tokens_, tensor_op->span_)); | ||
| 205 | + } else { | ||
| 206 | + result.push_back(stmt); | ||
| 207 | + } | ||
| 184 | } else if (auto scalar_op = std::dynamic_pointer_cast<const ScalarOpStmt>(stmt)) { | 208 | } else if (auto scalar_op = std::dynamic_pointer_cast<const ScalarOpStmt>(stmt)) { |
| 185 | if (is_removable(stmt) && !live.count(scalar_op->result_.get())) | 209 | if (is_removable(stmt) && !live.count(scalar_op->result_.get())) |
| 186 | continue; | 210 | continue; |
| @@ -1565,6 +1565,10 @@ REGISTER_INFER_SHAPE_FUNC(OP_SLICE, Opcode::OP_SLICE, ViewInferFunc); | |||
| 1565 | 1565 | ||
| 1566 | void AssembleInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) | 1566 | void AssembleInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) |
| 1567 | { | 1567 | { |
| 1568 | + if (op == nullptr || op->GetIOperands().empty() || op->GetOOperands().empty()) { | ||
| 1569 | + VECTOR_LOGW("AssembleInferFunc: op or its operands are invalid, skip infer shape."); | ||
| 1570 | + return; | ||
| 1571 | + } | ||
| 1568 | auto assembleOpAttribute = std::dynamic_pointer_cast<AssembleOpAttribute>(op->GetOpAttribute()); | 1572 | auto assembleOpAttribute = std::dynamic_pointer_cast<AssembleOpAttribute>(op->GetOpAttribute()); |
| 1569 | if (assembleOpAttribute != nullptr) { | 1573 | if (assembleOpAttribute != nullptr) { |
| 1570 | auto fromValidShape = op->GetIOperands()[0]->GetDynValidShape(); | 1574 | auto fromValidShape = op->GetIOperands()[0]->GetDynValidShape(); |
| @@ -1585,10 +1589,13 @@ void AssembleInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& | |||
| 1585 | std::vector<SymbolicScalar> outShape; | 1589 | std::vector<SymbolicScalar> outShape; |
| 1586 | for (size_t i = 0U; i < inputShapes.size(); i++) { | 1590 | for (size_t i = 0U; i < inputShapes.size(); i++) { |
| 1587 | SymbolicScalar actualDim; | 1591 | SymbolicScalar actualDim; |
| 1588 | - if (offset[i] == 0) { | 1592 | + // offset/outDynShape 维度可能小于 inputShapes(pass 改写后畸形 op),越界时按 0 处理避免崩溃 |
| 1589 | - actualDim = std::max(outDynShape[i], inputShapes[i]); | 1593 | + int64_t curOffset = (i < offset.size()) ? offset[i] : 0; |
| 1594 | + SymbolicScalar curOutDim = (i < outDynShape.size()) ? outDynShape[i] : SymbolicScalar(0); | ||
| 1595 | + if (curOffset == 0) { | ||
| 1596 | + actualDim = std::max(curOutDim, inputShapes[i]); | ||
| 1590 | } else { | 1597 | } else { |
| 1591 | - actualDim = std::max(outDynShape[i], (inputShapes[i] + offset[i]) * (inputShapes[i] != 0)); | 1598 | + actualDim = std::max(curOutDim, (inputShapes[i] + curOffset) * (inputShapes[i] != 0)); |
| 1592 | } | 1599 | } |
| 1593 | outShape.push_back(actualDim); | 1600 | outShape.push_back(actualDim); |
| 1594 | } | 1601 | } |
| @@ -715,6 +715,20 @@ void Operation::LoadExtraInfoFromJson(const Json& opDump) | |||
| 715 | SetAIVCore(core); | 715 | SetAIVCore(core); |
| 716 | } | 716 | } |
| 717 | } | 717 | } |
| 718 | + if (opDump.count("otokens") && !opDump["otokens"].empty()) { | ||
| 719 | + result_token_.clear(); | ||
| 720 | + for (const auto& name : opDump["otokens"]) { | ||
| 721 | + result_token_.push_back( | ||
| 722 | + std::make_shared<ir::Var>(name.get<std::string>(), ir::GetTokenType(), ir::Span::Unknown())); | ||
| 723 | + } | ||
| 724 | + } | ||
| 725 | + if (opDump.count("itokens") && !opDump["itokens"].empty()) { | ||
| 726 | + tokens_.clear(); | ||
| 727 | + for (const auto& name : opDump["itokens"]) { | ||
| 728 | + tokens_.push_back( | ||
| 729 | + std::make_shared<ir::Var>(name.get<std::string>(), ir::GetTokenType(), ir::Span::Unknown())); | ||
| 730 | + } | ||
| 731 | + } | ||
| 718 | } | 732 | } |
| 719 | 733 | ||
| 720 | std::shared_ptr<Operation> Operation::LoadJson( | 734 | std::shared_ptr<Operation> Operation::LoadJson( |
| @@ -13,6 +13,7 @@ | |||
| 13 | #include <string> | 13 | #include <string> |
| 14 | #include <map> | 14 | #include <map> |
| 15 | #include <unordered_set> | 15 | #include <unordered_set> |
| 16 | +#include <vector> | ||
| 16 | 17 | ||
| 17 | #include "ir/scalar_expr.h" | 18 | #include "ir/scalar_expr.h" |
| 18 | #include "ir/stmt.h" | 19 | #include "ir/stmt.h" |
| @@ -371,15 +371,28 @@ void RootFunctionBuilder::ComputeIncast(Function& pathFunc, | |||
| 371 | 371 | ||
| 372 | void RootFunctionBuilder::ComputeOutcast(Function& pathFunc) | 372 | void RootFunctionBuilder::ComputeOutcast(Function& pathFunc) |
| 373 | { | 373 | { |
| 374 | + LogicalTensors outcasts; | ||
| 375 | + std::unordered_map<int, size_t> rawMagicToOutcastIndex; | ||
| 374 | for (auto& op : pathFunc.Operations(false)) { | 376 | for (auto& op : pathFunc.Operations(false)) { |
| 375 | for (auto& oOperand : op.GetOOperands()) { | 377 | for (auto& oOperand : op.GetOOperands()) { |
| 376 | bool neededByConsumer = consumedTensors_.count(oOperand) > 0; | 378 | bool neededByConsumer = consumedTensors_.count(oOperand) > 0; |
| 377 | bool isFuncOutput = paramTensors_.count(oOperand) > 0; | 379 | bool isFuncOutput = paramTensors_.count(oOperand) > 0; |
| 378 | - if (neededByConsumer || isFuncOutput || oOperand->IsGetTensorDataOutcast()) { | 380 | + if (!neededByConsumer && !isFuncOutput && !oOperand->IsGetTensorDataOutcast()) { |
| 379 | - pathFunc.AddOriginOutcast(oOperand); | 381 | + continue; |
| 382 | + } | ||
| 383 | + | ||
| 384 | + int rawMagic = oOperand->GetRawMagic(); | ||
| 385 | + auto [iter, inserted] = rawMagicToOutcastIndex.emplace(rawMagic, outcasts.size()); | ||
| 386 | + if (inserted) { | ||
| 387 | + outcasts.push_back(oOperand); | ||
| 388 | + } else { | ||
| 389 | + outcasts[iter->second] = oOperand; | ||
| 380 | } | 390 | } |
| 381 | } | 391 | } |
| 382 | } | 392 | } |
| 393 | + for (const auto& outcast : outcasts) { | ||
| 394 | + pathFunc.AddOriginOutcast(outcast); | ||
| 395 | + } | ||
| 383 | } | 396 | } |
| 384 | 397 | ||
| 385 | std::shared_ptr<Function> RootFunctionBuilder::CreateHiddenFunc(const ir::SeqStmtsPtr& seq, | 398 | std::shared_ptr<Function> RootFunctionBuilder::CreateHiddenFunc(const ir::SeqStmtsPtr& seq, |
| @@ -174,11 +174,19 @@ ir::VarPtr LogicalTensor::Clone(bool shareRawTensor) const | |||
| 174 | LogicalTensorPtr LogicalTensor::NextVersion(Function& func, std::vector<ir::VarPtr>& tokens) const | 174 | LogicalTensorPtr LogicalTensor::NextVersion(Function& func, std::vector<ir::VarPtr>& tokens) const |
| 175 | { | 175 | { |
| 176 | (void)tokens; | 176 | (void)tokens; |
| 177 | - IRBuilder builder; | 177 | + auto newTensor = std::make_shared<LogicalTensor>(func, tensor, offset, shape, dynValidShape_); |
| 178 | - | 178 | + newTensor->oriShape = oriShape; |
| 179 | - auto zeroOffset = std::vector<int64_t>(shape.size(), 0); | 179 | + newTensor->dynOffset_ = dynOffset_; |
| 180 | - auto newTensor = builder.CreateTensorVar(func, tensor, zeroOffset, shape, dynValidShape_); | 180 | + newTensor->storageShape = storageShape; |
| 181 | - | 181 | + newTensor->storage_ = storage_; |
| 182 | + newTensor->storageOffset_ = storageOffset_; | ||
| 183 | + newTensor->memoryrange = memoryrange; | ||
| 184 | + newTensor->memoryTypeOriginal_ = memoryTypeOriginal_; | ||
| 185 | + newTensor->memoryTypeToBe_ = memoryTypeToBe_; | ||
| 186 | + newTensor->readyTime_ = readyTime_; | ||
| 187 | + newTensor->remainingTime_ = remainingTime_; | ||
| 188 | + newTensor->GetAllAttr() = GetAllAttr(); | ||
| 189 | + func.GetTensorMap().Insert(newTensor, false); | ||
| 182 | return newTensor; | 190 | return newTensor; |
| 183 | } | 191 | } |
| 184 | 192 | ||
| @@ -81,10 +81,11 @@ struct IrFuncSetup { | |||
| 81 | return lt; | 81 | return lt; |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | - Operation& AddDassemble(const LogicalTensorPtr& src, const LogicalTensorPtr& dst) | 84 | + Operation& AddDassemble(const LogicalTensorPtr& src, const LogicalTensorPtr& dst, |
| 85 | + const Offset& offset = Offset{TILE, TILE}) | ||
| 85 | { | 86 | { |
| 86 | auto& op = fwkFunc->AddRawOperation(Opcode::OP_ASSEMBLE, {src}, {dst}, Sp()); | 87 | auto& op = fwkFunc->AddRawOperation(Opcode::OP_ASSEMBLE, {src}, {dst}, Sp()); |
| 87 | - op.SetOpAttribute(std::make_shared<AssembleOpAttribute>(Offset{TILE, TILE})); | 88 | + op.SetOpAttribute(std::make_shared<AssembleOpAttribute>(offset)); |
| 88 | op.SetAttribute("dassemble", true); | 89 | op.SetAttribute("dassemble", true); |
| 89 | stmts.push_back(std::static_pointer_cast<const ir::Stmt>(op.shared_from_this())); | 90 | stmts.push_back(std::static_pointer_cast<const ir::Stmt>(op.shared_from_this())); |
| 90 | return op; | 91 | return op; |
| @@ -268,6 +269,58 @@ TEST_F(IrFuncBuilderTest, TestConstructAssembleSlotList_DedupSameSlot) | |||
| 268 | EXPECT_EQ(slots.size(), 1u) << "Expected 1 slot (deduplicated), got " << slots.size(); | 269 | EXPECT_EQ(slots.size(), 1u) << "Expected 1 slot (deduplicated), got " << slots.size(); |
| 269 | } | 270 | } |
| 270 | 271 | ||
| 272 | +TEST_F(IrFuncBuilderTest, TestConstructAssembleSlotList_ContinuationBranchKeepsExistingSlot) | ||
| 273 | +{ | ||
| 274 | + IrFuncSetup setup("ContinuationBranchKeepsExistingSlot"); | ||
| 275 | + | ||
| 276 | + auto input = setup.MakeParam("input"); | ||
| 277 | + auto local = setup.MakeLocal("local"); | ||
| 278 | + setup.AddDassemble(input, local, Offset{0, 0}); | ||
| 279 | + auto initBody = std::make_shared<ir::SeqStmts>(setup.stmts, Sp()); | ||
| 280 | + setup.stmts.clear(); | ||
| 281 | + | ||
| 282 | + auto updated = setup.builder.CreateTensorVar(*setup.fwkFunc, local->GetRawTensor(), local->GetOffset(), | ||
| 283 | + local->GetShape(), local->GetDynValidShape()); | ||
| 284 | + auto& updateOp = setup.fwkFunc->AddRawOperation(Opcode::OP_ADD, {local, input}, {updated}, Sp()); | ||
| 285 | + setup.stmts.push_back(std::static_pointer_cast<const ir::Stmt>(updateOp.shared_from_this())); | ||
| 286 | + auto localVersion = setup.builder.CreateTensorVar(*setup.fwkFunc, local->GetRawTensor(), local->GetOffset(), | ||
| 287 | + local->GetShape(), local->GetDynValidShape()); | ||
| 288 | + setup.AddDassemble(updated, localVersion, Offset{0, 0}); | ||
| 289 | + auto continuationBody = std::make_shared<ir::SeqStmts>(setup.stmts, Sp()); | ||
| 290 | + setup.stmts.clear(); | ||
| 291 | + | ||
| 292 | + auto cond = std::make_shared<ir::ConstInt>(1, ir::DataType::INT64, Sp()); | ||
| 293 | + setup.stmts.push_back(std::make_shared<ir::IfStmt>(cond, initBody, std::optional<ir::SeqStmtsPtr>{continuationBody}, | ||
| 294 | + std::vector<ir::VarPtr>{}, Sp())); | ||
| 295 | + | ||
| 296 | + auto irFunc = setup.BuildIrFunction("ContinuationBranchKeepsExistingSlot"); | ||
| 297 | + auto irProg = std::make_shared<ir::Program>(std::vector<ir::FunctionPtr>{irFunc}, "test", Sp()); | ||
| 298 | + | ||
| 299 | + auto createRoot = pypto::ir::pass::CreateRootFunctions(); | ||
| 300 | + (void)createRoot(irProg); | ||
| 301 | + | ||
| 302 | + auto hiddenFuncs = FindHiddenFuncs(); | ||
| 303 | + ASSERT_EQ(hiddenFuncs.size(), 2u); | ||
| 304 | + auto localSlot = Program::GetInstance().GetTensorSlotManager()->GetSlotTensor(local)->Id(); | ||
| 305 | + for (auto* hiddenFunc : hiddenFuncs) { | ||
| 306 | + auto scope = hiddenFunc->Parent().GetSlotScope(); | ||
| 307 | + ASSERT_NE(scope, nullptr); | ||
| 308 | + bool isContinuation = false; | ||
| 309 | + for (auto& op : hiddenFunc->Operations(false)) { | ||
| 310 | + isContinuation = isContinuation || op.GetOpcode() == Opcode::OP_ADD; | ||
| 311 | + } | ||
| 312 | + if (isContinuation) { | ||
| 313 | + EXPECT_TRUE(std::any_of(scope->ioslot.incastSlot.begin(), scope->ioslot.incastSlot.end(), | ||
| 314 | + [localSlot](const auto& slots) { | ||
| 315 | + return std::find(slots.begin(), slots.end(), localSlot) != slots.end(); | ||
| 316 | + })); | ||
| 317 | + EXPECT_TRUE(scope->constructAssembleSlotList.empty()); | ||
| 318 | + } else { | ||
| 319 | + EXPECT_EQ(scope->constructAssembleSlotList, std::vector<int>{localSlot}); | ||
| 320 | + } | ||
| 321 | + } | ||
| 322 | +} | ||
| 323 | + | ||
| 271 | // ============================================================================ | 324 | // ============================================================================ |
| 272 | // Mixed: one dassemble to function param (excluded) + one to intermediate (kept) | 325 | // Mixed: one dassemble to function param (excluded) + one to intermediate (kept) |
| 273 | // => constructAssembleSlotList should contain exactly 1 slot | 326 | // => constructAssembleSlotList should contain exactly 1 slot |
| @@ -296,6 +349,32 @@ TEST_F(IrFuncBuilderTest, TestConstructAssembleSlotList_MixedParamAndIntermediat | |||
| 296 | EXPECT_EQ(slots.size(), 1u) << "Expected 1 slot (aux only, out excluded), got " << slots.size(); | 349 | EXPECT_EQ(slots.size(), 1u) << "Expected 1 slot (aux only, out excluded), got " << slots.size(); |
| 297 | } | 350 | } |
| 298 | 351 | ||
| 352 | +TEST_F(IrFuncBuilderTest, TestNonInplaceStorageAliasDoesNotUseParamSlot) | ||
| 353 | +{ | ||
| 354 | + IrFuncSetup setup("NonInplaceStorageAlias"); | ||
| 355 | + | ||
| 356 | + auto output = setup.MakeParam("output"); | ||
| 357 | + auto alias = setup.MakeLocal("alias"); | ||
| 358 | + auto& cast = setup.fwkFunc->AddRawOperation(Opcode::OP_CAST, {output}, {alias}, Sp()); | ||
| 359 | + setup.fwkFunc->SetSameMemId(output, alias); | ||
| 360 | + setup.stmts.push_back(std::static_pointer_cast<const ir::Stmt>(cast.shared_from_this())); | ||
| 361 | + | ||
| 362 | + auto irFunc = setup.BuildIrFunction("NonInplaceStorageAlias"); | ||
| 363 | + auto irProg = std::make_shared<ir::Program>(std::vector<ir::FunctionPtr>{irFunc}, "test", Sp()); | ||
| 364 | + | ||
| 365 | + auto createRoot = pypto::ir::pass::CreateRootFunctions(); | ||
| 366 | + auto rootProg = createRoot(irProg); | ||
| 367 | + | ||
| 368 | + auto hiddenFuncs = FindHiddenFuncs(); | ||
| 369 | + ASSERT_EQ(hiddenFuncs.size(), 1u); | ||
| 370 | + EXPECT_TRUE(hiddenFuncs[0]->GetOutcast().empty()); | ||
| 371 | + auto slotManager = Program::GetInstance().GetTensorSlotManager(); | ||
| 372 | + EXPECT_NE(slotManager->GetSlotTensor(output)->Id(), slotManager->GetSlotTensor(alias)->Id()); | ||
| 373 | + auto dynFunc = std::static_pointer_cast<const npu::tile_fwk::Function>( | ||
| 374 | + rootProg->functions_.at("NonInplaceStorageAlias")); | ||
| 375 | + EXPECT_TRUE(dynFunc->GetOriginOutcast().empty()); | ||
| 376 | +} | ||
| 377 | + | ||
| 299 | // ============================================================================ | 378 | // ============================================================================ |
| 300 | // ForStmt with "unroll_times" attr => TransformStmts reads it via std::any_cast<int> | 379 | // ForStmt with "unroll_times" attr => TransformStmts reads it via std::any_cast<int> |
| 301 | // and appends "_Unroll<N>" to the loop-var-derived path suffix baked into the | 380 | // and appends "_Unroll<N>" to the loop-var-derived path suffix baked into the |
| @@ -122,3 +122,30 @@ TEST_F(TestLogicalTensor, UpdateGetTensorDataIOIndexNonImmediateIOTypeIndexThrow | |||
| 122 | SymbolicScalar scalar = MakeGetTensorDataCall(true, false); | 122 | SymbolicScalar scalar = MakeGetTensorDataCall(true, false); |
| 123 | EXPECT_THROW(UpdateGetTensorDataIOIndex(0, 1, scalar), std::exception); | 123 | EXPECT_THROW(UpdateGetTensorDataIOIndex(0, 1, scalar), std::exception); |
| 124 | } | 124 | } |
| 125 | + | ||
| 126 | +TEST_F(TestLogicalTensor, NextVersionPreservesLogicalTensorMetadata) | ||
| 127 | +{ | ||
| 128 | + Tensor input(DT_FP32, {16, 16}, "input"); | ||
| 129 | + auto* function = Program::GetInstance().GetCurrentFunction(); | ||
| 130 | + ASSERT_NE(function, nullptr); | ||
| 131 | + | ||
| 132 | + auto storage = input.GetStorage(false)->View(*function, {8, 8}, {4, 4}); | ||
| 133 | + storage->UpdateOffset(TensorOffset({4, 4}, {SymbolicScalar(4), SymbolicScalar(4)})); | ||
| 134 | + storage->SetMemoryTypeBoth(MEM_UB, true); | ||
| 135 | + storage->SetAttr("version_metadata", true); | ||
| 136 | + | ||
| 137 | + std::vector<ir::VarPtr> tokens; | ||
| 138 | + auto next = storage->NextVersion(*function, tokens); | ||
| 139 | + | ||
| 140 | + ASSERT_NE(next, nullptr); | ||
| 141 | + EXPECT_NE(next, storage); | ||
| 142 | + EXPECT_EQ(next->GetRawTensor(), storage->GetRawTensor()); | ||
| 143 | + EXPECT_EQ(next->GetOffset(), storage->GetOffset()); | ||
| 144 | + EXPECT_EQ(next->GetDynOffset(), storage->GetDynOffset()); | ||
| 145 | + EXPECT_EQ(next->GetShape(), storage->GetShape()); | ||
| 146 | + EXPECT_EQ(next->GetDynValidShape(), storage->GetDynValidShape()); | ||
| 147 | + EXPECT_EQ(next->GetMemoryTypeOriginal(), storage->GetMemoryTypeOriginal()); | ||
| 148 | + EXPECT_EQ(next->GetMemoryTypeToBe(), storage->GetMemoryTypeToBe()); | ||
| 149 | + EXPECT_TRUE(next->HasAttr("version_metadata")); | ||
| 150 | + EXPECT_EQ(function->GetTensorMap().GetTensorByMagic(next->GetMagic()), next); | ||
| 151 | +} | ||
| @@ -0,0 +1,20 @@ | |||
| 1 | +program #entry(foo) { | ||
| 2 | + function foo incast(v0_logical_tensor %src@0 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]), v0_logical_tensor %out@1 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16])) outcast() #type(Orchestration) #entry(true) { | ||
| 3 | + v0_logical_tensor %out_5@1 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]) = !10000 CALL(%src@0) token(); | ||
| 4 | + return %src@0, %out_5@1; | ||
| 5 | + } | ||
| 6 | + function foo_PATH0_6 incast(v0_logical_tensor %src_3@8 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]), v0_logical_tensor %out_9@10 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16])) outcast() #type(InCore) #entry(false) { | ||
| 7 | + v0_logical_tensor %INCAST_LOCAL_BUF0_0@9 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]) = !10001 VIEW(%src_3@8) token() #fromOffset(Unsupported) #toValidShape(Unsupported); | ||
| 8 | + v0_logical_tensor %OUTCAST_LOCAL_BUF0@11 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]) = !10000 CALL(%INCAST_LOCAL_BUF0_0@9) token(); | ||
| 9 | + v0_logical_tensor %out_9@10 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]) = !10002 ASSEMBLE(%OUTCAST_LOCAL_BUF0@11) token() #toOffset(Unsupported); | ||
| 10 | + } | ||
| 11 | + function foo_PATH0_hiddenfunc_4 incast(v0_logical_tensor %src_1@5 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]), v0_logical_tensor %out_7@7 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16])) outcast() #type(InCore) #entry(false) { | ||
| 12 | + v0_logical_tensor %INCAST_LOCAL_BUF0@6 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]) = !10006 VIEW(%src_1@5) token() #fromOffset(Unsupported) #toValidShape(Unsupported); | ||
| 13 | + v0_logical_tensor %$0@2 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]) = !10000 ADDS(%INCAST_LOCAL_BUF0@6) token(); | ||
| 14 | + v0_logical_tensor %out_1@7 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]) = !10001 ASSEMBLE(%$0@2) token() #toOffset(Unsupported); | ||
| 15 | + v0_logical_tensor %$1@3 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]) = !10002 ADDS(%INCAST_LOCAL_BUF0@6) token(); | ||
| 16 | + v0_logical_tensor %out_3@7 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]) = !10003 ASSEMBLE(%$1@3) token() #toOffset(Unsupported); | ||
| 17 | + v0_logical_tensor %$2@4 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]) = !10004 ADDS(%INCAST_LOCAL_BUF0@6) token(); | ||
| 18 | + v0_logical_tensor %out_7@7 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]) = !10005 ASSEMBLE(%$2@4) token() #toOffset(Unsupported); | ||
| 19 | + } | ||
| 20 | +} | ||
| @@ -18,6 +18,7 @@ from ..test_common import _ssa_verify, check_snapshot | |||
| 18 | _GOLDEN_DIR = Path(__file__).parent | 18 | _GOLDEN_DIR = Path(__file__).parent |
| 19 | 19 | ||
| 20 | IR = _GOLDEN_DIR / "test_assemble_outcast_versions.pypto" | 20 | IR = _GOLDEN_DIR / "test_assemble_outcast_versions.pypto" |
| 21 | +LATEST_OUTCAST_IR = _GOLDEN_DIR / "test_assemble_latest_outcast.pypto" | ||
| 21 | 22 | ||
| 22 | 23 | ||
| 23 | def _run_root_builder(func, *args): | 24 | def _run_root_builder(func, *args): |
| @@ -50,3 +51,18 @@ def test_assemble_outcast_versions_share_raw_tensor(): | |||
| 50 | program = _run_root_builder(foo, a, aux, out) | 51 | program = _run_root_builder(foo, a, aux, out) |
| 51 | 52 | ||
| 52 | check_snapshot(program, IR) | 53 | check_snapshot(program, IR) |
| 54 | + | ||
| 55 | + | ||
| 56 | +def test_repeated_assemble_uses_single_latest_outcast(): | ||
| 57 | + """ComputeOutcast deduplicates versions by RawTensor and keeps the latest one.""" | ||
| 58 | + | ||
| 59 | + def foo(src, out): | ||
| 60 | + pypto.assemble(src + 1.0, [0, 0], out) | ||
| 61 | + pypto.assemble(src + 2.0, [0, 0], out) | ||
| 62 | + pypto.assemble(src + 3.0, [0, 0], out) | ||
| 63 | + | ||
| 64 | + src = pypto.Tensor([16, 16], pypto.DT_FP32, name="src") | ||
| 65 | + out = pypto.Tensor([16, 16], pypto.DT_FP32, name="out") | ||
| 66 | + program = _run_root_builder(foo, src, out) | ||
| 67 | + | ||
| 68 | + check_snapshot(program, LATEST_OUTCAST_IR) | ||