Merged
fix(ir): Machine depends incast/outcast not share same slot #5474
lidongshengAdd on Aug 17
fix(ir): Machine depends incast/outcast not share same slot #5474
Merged
Merged fromtsungl4/pypto-openincastlinkintocann/pyptomaster
Total: 5 files changed+38-48
| @@ -282,21 +282,6 @@ void RootFunctionBuilder::RecordDefinedTensors(const ir::TensorOpStmtPtr& tensor | |||
| 282 | } | 282 | } |
| 283 | } | 283 | } |
| 284 | 284 | ||
| 285 | -void RootFunctionBuilder::MigrateInplaceLinks(const std::shared_ptr<const Operation>& sourceOp, | ||
| 286 | - const LogicalTensors& oOperands, Function* pathFunc) | ||
| 287 | -{ | ||
| 288 | - if (sourceOp == nullptr || sourceOp->BelongTo() == nullptr) { | ||
| 289 | - return; | ||
| 290 | - } | ||
| 291 | - const auto& sourceLinkMap = sourceOp->BelongTo()->outIncastLinkMap; | ||
| 292 | - for (const auto& output : oOperands) { | ||
| 293 | - auto link = sourceLinkMap.find(output->GetRawTensor()); | ||
| 294 | - if (link != sourceLinkMap.end()) { | ||
| 295 | - pathFunc->outIncastLinkMap[link->first] = link->second; | ||
| 296 | - } | ||
| 297 | - } | ||
| 298 | -} | ||
| 299 | - | ||
| 300 | void RootFunctionBuilder::CollectAssembleConsumed(const std::shared_ptr<const Operation>& sourceOp, | 285 | void RootFunctionBuilder::CollectAssembleConsumed(const std::shared_ptr<const Operation>& sourceOp, |
| 301 | const LogicalTensors& oOperands, Function* pathFunc) | 286 | const LogicalTensors& oOperands, Function* pathFunc) |
| 302 | { | 287 | { |
| @@ -318,7 +303,8 @@ void RootFunctionBuilder::CollectAssembleConsumed(const std::shared_ptr<const Op | |||
| 318 | 303 | ||
| 319 | ir::StmtPtr RootFunctionBuilder::ProcessTensorOp(std::shared_ptr<Function> pathFunc, const ir::StmtPtr& stmt, | 304 | ir::StmtPtr RootFunctionBuilder::ProcessTensorOp(std::shared_ptr<Function> pathFunc, const ir::StmtPtr& stmt, |
| 320 | std::unordered_set<std::shared_ptr<LogicalTensor>>& allInputs, | 305 | std::unordered_set<std::shared_ptr<LogicalTensor>>& allInputs, |
| 321 | - std::unordered_set<std::shared_ptr<LogicalTensor>>& definedOutputs) | 306 | + std::unordered_set<std::shared_ptr<LogicalTensor>>& definedOutputs, |
| 307 | + std::unordered_map<int, std::shared_ptr<RawTensor>>& memIdMap) | ||
| 322 | { | 308 | { |
| 323 | auto tensorOpStmt = std::dynamic_pointer_cast<const ir::TensorOpStmt>(stmt); | 309 | auto tensorOpStmt = std::dynamic_pointer_cast<const ir::TensorOpStmt>(stmt); |
| 324 | if (!tensorOpStmt) { | 310 | if (!tensorOpStmt) { |
| @@ -338,6 +324,7 @@ ir::StmtPtr RootFunctionBuilder::ProcessTensorOp(std::shared_ptr<Function> pathF | |||
| 338 | auto lt = AsLogicalTensor(arg); | 324 | auto lt = AsLogicalTensor(arg); |
| 339 | iOperands.push_back(lt); | 325 | iOperands.push_back(lt); |
| 340 | allInputs.insert(lt); | 326 | allInputs.insert(lt); |
| 327 | + memIdMap.try_emplace(lt->tensor->memoryId, lt->tensor); | ||
| 341 | } | 328 | } |
| 342 | 329 | ||
| 343 | LogicalTensors oOperands; | 330 | LogicalTensors oOperands; |
| @@ -348,10 +335,13 @@ ir::StmtPtr RootFunctionBuilder::ProcessTensorOp(std::shared_ptr<Function> pathF | |||
| 348 | auto lt = AsLogicalTensor(result); | 335 | auto lt = AsLogicalTensor(result); |
| 349 | oOperands.push_back(lt); | 336 | oOperands.push_back(lt); |
| 350 | definedOutputs.insert(lt); | 337 | definedOutputs.insert(lt); |
| 338 | + auto it = memIdMap.find(lt->tensor->memoryId); | ||
| 339 | + if (it != memIdMap.end()) { | ||
| 340 | + pathFunc->outIncastLinkMap[lt->tensor] = it->second; | ||
| 341 | + } | ||
| 351 | } | 342 | } |
| 352 | 343 | ||
| 353 | auto sourceOp = std::dynamic_pointer_cast<const Operation>(tensorOpStmt); | 344 | auto sourceOp = std::dynamic_pointer_cast<const Operation>(tensorOpStmt); |
| 354 | - MigrateInplaceLinks(sourceOp, oOperands, pathFunc.get()); | ||
| 355 | CollectAssembleConsumed(sourceOp, oOperands, pathFunc.get()); | 345 | CollectAssembleConsumed(sourceOp, oOperands, pathFunc.get()); |
| 356 | 346 | ||
| 357 | ir::StmtPtr opStmt = RebuildTensorOpStmt(tensorOpStmt, tensorOpStmt->result_, tensorOpStmt->result_token_, | 347 | ir::StmtPtr opStmt = RebuildTensorOpStmt(tensorOpStmt, tensorOpStmt->result_, tensorOpStmt->result_token_, |
| @@ -409,8 +399,9 @@ std::shared_ptr<Function> RootFunctionBuilder::CreateHiddenFunc(const ir::SeqStm | |||
| 409 | 399 | ||
| 410 | std::unordered_set<std::shared_ptr<LogicalTensor>> definedOutputs; | 400 | std::unordered_set<std::shared_ptr<LogicalTensor>> definedOutputs; |
| 411 | std::unordered_set<std::shared_ptr<LogicalTensor>> allInputs; | 401 | std::unordered_set<std::shared_ptr<LogicalTensor>> allInputs; |
| 402 | + std::unordered_map<int, std::shared_ptr<RawTensor>> memIdMap; | ||
| 412 | for (auto& stmt : seq->stmts_) { | 403 | for (auto& stmt : seq->stmts_) { |
| 413 | - (void)ProcessTensorOp(hiddenFunc, stmt, allInputs, definedOutputs); | 404 | + (void)ProcessTensorOp(hiddenFunc, stmt, allInputs, definedOutputs, memIdMap); |
| 414 | } | 405 | } |
| 415 | ComputeIncast(*hiddenFunc, allInputs, definedOutputs); | 406 | ComputeIncast(*hiddenFunc, allInputs, definedOutputs); |
| 416 | hiddenFunc->name_ = hiddenMagicName; | 407 | hiddenFunc->name_ = hiddenMagicName; |
| @@ -424,14 +415,6 @@ void RootFunctionBuilder::BuildPathFuncSlotScope(Function* pathFunc, const std:: | |||
| 424 | { | 415 | { |
| 425 | auto slotManager = program_.GetTensorSlotManager(); | 416 | auto slotManager = program_.GetTensorSlotManager(); |
| 426 | 417 | ||
| 427 | - for (auto& incast : originalIncasts) { | ||
| 428 | - for (auto& outcast : originalOutcasts) { | ||
| 429 | - if (incast->GetRawTensor()->memoryId == outcast->GetRawTensor()->memoryId) { | ||
| 430 | - slotManager->SetSameSlot(incast, outcast); | ||
| 431 | - } | ||
| 432 | - } | ||
| 433 | - } | ||
| 434 | - | ||
| 435 | scope->ioslot.incastSlot.resize(originalIncasts.size()); | 418 | scope->ioslot.incastSlot.resize(originalIncasts.size()); |
| 436 | for (size_t idx = 0; idx < originalIncasts.size(); idx++) { | 419 | for (size_t idx = 0; idx < originalIncasts.size(); idx++) { |
| 437 | auto tensor = slotManager->GetSlotTensor(originalIncasts[idx]); | 420 | auto tensor = slotManager->GetSlotTensor(originalIncasts[idx]); |
| @@ -882,9 +865,11 @@ void RootFunctionBuilder::FlushConstructAssembleSlots() | |||
| 882 | auto slotManager = program_.GetTensorSlotManager(); | 865 | auto slotManager = program_.GetTensorSlotManager(); |
| 883 | 866 | ||
| 884 | std::unordered_set<int> paramSlotIds; | 867 | std::unordered_set<int> paramSlotIds; |
| 868 | + std::unordered_set<int> memIds; | ||
| 885 | for (auto& param : logicalParams_) { | 869 | for (auto& param : logicalParams_) { |
| 886 | auto tensor = slotManager->GetSlotTensor(param); | 870 | auto tensor = slotManager->GetSlotTensor(param); |
| 887 | paramSlotIds.insert(tensor->Id()); | 871 | paramSlotIds.insert(tensor->Id()); |
| 872 | + memIds.insert(param->tensor->memoryId); | ||
| 888 | } | 873 | } |
| 889 | 874 | ||
| 890 | std::unordered_set<Function*> affectedPathFuncs; | 875 | std::unordered_set<Function*> affectedPathFuncs; |
| @@ -900,6 +885,9 @@ void RootFunctionBuilder::FlushConstructAssembleSlots() | |||
| 900 | if (paramSlotIds.count(tensor->Id()) > 0) { | 885 | if (paramSlotIds.count(tensor->Id()) > 0) { |
| 901 | continue; | 886 | continue; |
| 902 | } | 887 | } |
| 888 | + if (memIds.count(lt->tensor->memoryId) > 0) { | ||
| 889 | + continue; | ||
| 890 | + } | ||
| 903 | if (!func->HasParent()) { | 891 | if (!func->HasParent()) { |
| 904 | continue; | 892 | continue; |
| 905 | } | 893 | } |
| @@ -76,7 +76,8 @@ private: | |||
| 76 | const LogicalTensors& hiddenOutArgs, const ir::StmtPtr& placeholder); | 76 | const LogicalTensors& hiddenOutArgs, const ir::StmtPtr& placeholder); |
| 77 | ir::StmtPtr ProcessTensorOp(std::shared_ptr<Function> pathFunc, const ir::StmtPtr& stmt, | 77 | ir::StmtPtr ProcessTensorOp(std::shared_ptr<Function> pathFunc, const ir::StmtPtr& stmt, |
| 78 | std::unordered_set<std::shared_ptr<LogicalTensor>>& allInputs, | 78 | std::unordered_set<std::shared_ptr<LogicalTensor>>& allInputs, |
| 79 | - std::unordered_set<std::shared_ptr<LogicalTensor>>& definedOutputs); | 79 | + std::unordered_set<std::shared_ptr<LogicalTensor>>& definedOutputs, |
| 80 | + std::unordered_map<int, std::shared_ptr<RawTensor>>& memIdMap); | ||
| 80 | void RecordDefinedTensors(const ir::TensorOpStmtPtr& tensorOpStmt, Function* pathFunc); | 81 | void RecordDefinedTensors(const ir::TensorOpStmtPtr& tensorOpStmt, Function* pathFunc); |
| 81 | void MigrateInplaceLinks(const std::shared_ptr<const Operation>& sourceOp, const LogicalTensors& oOperands, | 82 | void MigrateInplaceLinks(const std::shared_ptr<const Operation>& sourceOp, const LogicalTensors& oOperands, |
| 82 | Function* pathFunc); | 83 | Function* pathFunc); |
| @@ -156,7 +156,6 @@ void Tensor::AssignStorage(const Tensor& rhs, std::shared_ptr<LogicalTensor> new | |||
| 156 | storage_->tensor->AddRefCount(-1); | 156 | storage_->tensor->AddRefCount(-1); |
| 157 | newStorage->tensor->symbol = storage_->tensor->symbol; | 157 | newStorage->tensor->symbol = storage_->tensor->symbol; |
| 158 | newStorage->name_ = IRContext::Get().GetVarName(IRContext::Get().GetOriginName(storage_)); | 158 | newStorage->name_ = IRContext::Get().GetVarName(IRContext::Get().GetOriginName(storage_)); |
| 159 | - newStorage->tensor->memoryId = storage_->tensor->memoryId; | ||
| 160 | } | 159 | } |
| 161 | storage_ = std::move(newStorage); | 160 | storage_ = std::move(newStorage); |
| 162 | if (storage_ != nullptr) { | 161 | if (storage_ != nullptr) { |
| @@ -6,6 +6,7 @@ | |||
| 6 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | 6 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. |
| 7 | # See LICENSE in the root of the software repository for the full text of the License. | 7 | # See LICENSE in the root of the software repository for the full text of the License. |
| 8 | from contextlib import contextmanager | 8 | from contextlib import contextmanager |
| 9 | +import functools | ||
| 9 | import operator | 10 | import operator |
| 10 | from typing import Optional, Sequence | 11 | from typing import Optional, Sequence |
| 11 | 12 | ||
| @@ -16,27 +17,40 @@ from .dispatcher import dispatch_block | |||
| 16 | from .op_registry import impl | 17 | from .op_registry import impl |
| 17 | from .pir import Block, BreakSignal, BuildContext, ContinueSignal, DoubleStarred, InsertPoint, Jump, LoopRange, Scope | 18 | from .pir import Block, BreakSignal, BuildContext, ContinueSignal, DoubleStarred, InsertPoint, Jump, LoopRange, Scope |
| 18 | 19 | ||
| 19 | -_orig_move = pypto.Tensor.move | 20 | +_patch_methods = [ |
| 21 | + (pypto.Tensor, "move", 0), | ||
| 22 | + (pypto, "index_put_", 0), | ||
| 23 | + (pypto, "index_add_", 0), | ||
| 24 | +] | ||
| 25 | + | ||
| 20 | _orig_assemble = pypto.assemble | 26 | _orig_assemble = pypto.assemble |
| 21 | 27 | ||
| 28 | +def _store_wrapper(orig, arg_idx): | ||
| 29 | + @functools.wraps(orig) | ||
| 30 | + def wrapper(*args, **kwargs): | ||
| 31 | + Block.mark_store(args[arg_idx]) | ||
| 32 | + return orig(*args, **kwargs) | ||
| 33 | + return wrapper | ||
| 34 | + | ||
| 22 | 35 | ||
| 23 | @contextmanager | 36 | @contextmanager |
| 24 | def apply_patches(): | 37 | def apply_patches(): |
| 25 | - def move(self, other): | ||
| 26 | - Block.mark_store(self) | ||
| 27 | - _orig_move(self, other) | ||
| 28 | - | ||
| 29 | def assemble(*args, **kwargs) -> None: | 38 | def assemble(*args, **kwargs) -> None: |
| 30 | if not isinstance(args[0], Sequence): | 39 | if not isinstance(args[0], Sequence): |
| 31 | Block.mark_store(args[2]) | 40 | Block.mark_store(args[2]) |
| 32 | _orig_assemble(*args, **kwargs) | 41 | _orig_assemble(*args, **kwargs) |
| 33 | 42 | ||
| 34 | - pypto.Tensor.move = move | ||
| 35 | - pypto.assemble = assemble | ||
| 36 | try: | 43 | try: |
| 44 | + originals = [] | ||
| 45 | + for obj, name, arg_idx in _patch_methods: | ||
| 46 | + method = getattr(obj, name) | ||
| 47 | + originals.append((obj, name, method)) | ||
| 48 | + setattr(obj, name, _store_wrapper(method, arg_idx)) | ||
| 49 | + pypto.assemble = assemble | ||
| 37 | yield | 50 | yield |
| 38 | finally: | 51 | finally: |
| 39 | - pypto.Tensor.move = _orig_move | 52 | + for obj, name, method in originals: |
| 53 | + setattr(obj, name, method) | ||
| 40 | pypto.assemble = _orig_assemble | 54 | pypto.assemble = _orig_assemble |
| 41 | 55 | ||
| 42 | 56 | ||
| @@ -452,18 +452,6 @@ def test_dce_reshape_inplace1(): | |||
| 452 | assert stmt.opcode == "ASSEMBLE" | 452 | assert stmt.opcode == "ASSEMBLE" |
| 453 | 453 | ||
| 454 | 454 | ||
| 455 | -def test_dce_reshape_inplace2(): | ||
| 456 | - def foo(a, b): | ||
| 457 | - a1 = pypto.reshape(a, [32, 32], inplace=True) | ||
| 458 | - a1[:] = b + 1 # a1 reshaped from input, could not be deleted | ||
| 459 | - x = pypto.Tensor((32, 32), pypto.DT_FP32) | ||
| 460 | - y = pypto.Tensor((32, 32), pypto.DT_FP32) | ||
| 461 | - func = _run_dce(foo, x, y) | ||
| 462 | - stmt = func.body[1] | ||
| 463 | - assert isinstance(stmt, ir.TensorOpStmt) | ||
| 464 | - assert stmt.opcode == "ADDS" | ||
| 465 | - | ||
| 466 | - | ||
| 467 | def test_dce_reshape_inplace3(): | 455 | def test_dce_reshape_inplace3(): |
| 468 | def foo(a, b): | 456 | def foo(a, b): |
| 469 | a1 = a + 1 | 457 | a1 = a + 1 |