Merged
fix(pass): Fix reshape and assemble logic error #7040
niulinkaiAdd on Oct 20
fix(pass): Fix reshape and assemble logic error #7040
Merged
Merged from2501_92924845/pyptosf1020intocann/pypto9.2.0
Total: 4 files changed+96-8
| @@ -1309,13 +1309,36 @@ Status RemoveRedundantAssemble::HanldeForSingleAssemble(Function& function, Logi | |||
| 1309 | { | 1309 | { |
| 1310 | auto producersBackup = input->GetProducers(); | 1310 | auto producersBackup = input->GetProducers(); |
| 1311 | auto& consumers = input->GetConsumers(); | 1311 | auto& consumers = input->GetConsumers(); |
| 1312 | - auto cons = *consumers.begin(); | 1312 | + // Only fold the additional COPY_OUT -> ASSEMBLE -> RESHAPE -> OUTCAST pattern. |
| 1313 | - if (consumers.size() == 1 && cons->GetOpcode() == Opcode::OP_ASSEMBLE) { | 1313 | + // The caller guarantees a single sibling ASSEMBLE. ReplaceTensor has already refreshed COPY_IN attributes. |
| 1314 | - PassTokenUtils::MoveTokenDependencyBeforeRemoveOp(function, *cons); | 1314 | + const auto& outputConsumers = output->GetConsumers(); |
| 1315 | - cons->SetAsDeleted(); | 1315 | + bool reshapeToOutcast = outputConsumers.size() == 1 && |
| 1316 | - if (HandleCopyOutToAssemble(*cons, output, producersBackup) != SUCCESS) { | 1316 | + (*outputConsumers.begin())->GetOpcode() == Opcode::OP_RESHAPE && |
| 1317 | - APASS_LOG_ERROR_F(Elements::Operation, "HandleCopyOutToAssemble for op:[%d] failed. %s", cons->GetOpMagic(), | 1317 | + FunctionUtils::GetNodeType(*(*outputConsumers.begin())->GetOOperands().front(), function) == |
| 1318 | - cons->GetOpcodeStr().c_str()); | 1318 | + NodeType::OUTCAST; |
| 1319 | + const auto& outputProducers = output->GetProducers(); | ||
| 1320 | + bool rewireCopyIns = reshapeToOutcast && outputProducers.size() == 1 && *outputProducers.begin() == &op && | ||
| 1321 | + consumers.size() > 1 && input->GetRawMagic() == output->GetRawMagic() && | ||
| 1322 | + std::all_of(producersBackup.begin(), producersBackup.end(), | ||
| 1323 | + [](Operation* producer) { return IsCopyOut(producer->GetOpcode()); }) && | ||
| 1324 | + std::all_of(consumers.begin(), consumers.end(), [&op](Operation* consumer) { | ||
| 1325 | + return consumer == &op || IsCopyIn(consumer->GetOpcode()); | ||
| 1326 | + }); | ||
| 1327 | + if (consumers.size() == 1 || rewireCopyIns) { | ||
| 1328 | + PassTokenUtils::MoveTokenDependencyBeforeRemoveOp(function, op); | ||
| 1329 | + if (rewireCopyIns) { | ||
| 1330 | + auto consumersBackup = consumers; | ||
| 1331 | + for (auto* consumer : consumersBackup) { | ||
| 1332 | + if (consumer != &op) { | ||
| 1333 | + // Preserve the absolute COPY_IN attributes refreshed by ReplaceTensor. | ||
| 1334 | + consumer->ReplaceInput(output, input); | ||
| 1335 | + } | ||
| 1336 | + } | ||
| 1337 | + } | ||
| 1338 | + op.SetAsDeleted(); | ||
| 1339 | + if (HandleCopyOutToAssemble(op, output, producersBackup) != SUCCESS) { | ||
| 1340 | + APASS_LOG_ERROR_F(Elements::Operation, "HandleCopyOutToAssemble for op:[%d] failed. %s", op.GetOpMagic(), | ||
| 1341 | + op.GetOpcodeStr().c_str()); | ||
| 1319 | return FAILED; | 1342 | return FAILED; |
| 1320 | } | 1343 | } |
| 1321 | HandleForAssembleFromInOut(function, op, producersBackup); | 1344 | HandleForAssembleFromInOut(function, op, producersBackup); |
| @@ -646,6 +646,10 @@ void RemoveUnalignedReshape::HandleNoCopyInConsumer(Function& function, Operatio | |||
| 646 | checkOverUbSize = true; | 646 | checkOverUbSize = true; |
| 647 | return; | 647 | return; |
| 648 | } | 648 | } |
| 649 | + // Empty consumers only need the UB check above; keep the existing bounded-tensor path unchanged. | ||
| 650 | + if (output->GetConsumers().empty()) { | ||
| 651 | + return; | ||
| 652 | + } | ||
| 649 | auto& newCopyInOp = PassOperationUtils::AddOperation( | 653 | auto& newCopyInOp = PassOperationUtils::AddOperation( |
| 650 | function, Opcode::OP_COPY_IN, {output}, {newCopyinTensorPtr}, [&output](Operation& newOp) { | 654 | function, Opcode::OP_COPY_IN, {output}, {newCopyinTensorPtr}, [&output](Operation& newOp) { |
| 651 | newOp.SetOpAttribute(std::make_shared<CopyOpAttribute>( | 655 | newOp.SetOpAttribute(std::make_shared<CopyOpAttribute>( |
| @@ -852,8 +856,8 @@ bool RemoveUnalignedReshape::HasMixedRawCopyOutAndAssembleProducers(Function& fu | |||
| 852 | */ | 856 | */ |
| 853 | bool RemoveUnalignedReshape::checkNonCopyInConsumerExists(LogicalTensorPtr tensor, std::vector<Operation*>& copyInOps) | 857 | bool RemoveUnalignedReshape::checkNonCopyInConsumerExists(LogicalTensorPtr tensor, std::vector<Operation*>& copyInOps) |
| 854 | { | 858 | { |
| 855 | - bool hasNoCopyInConsumer = false; | ||
| 856 | auto consumers = tensor->GetConsumers(); | 859 | auto consumers = tensor->GetConsumers(); |
| 860 | + bool hasNoCopyInConsumer = consumers.empty(); | ||
| 857 | for (auto* consumerOp : consumers) { | 861 | for (auto* consumerOp : consumers) { |
| 858 | auto opcode = consumerOp->GetOpcode(); | 862 | auto opcode = consumerOp->GetOpcode(); |
| 859 | if (opcode == Opcode::OP_COPY_IN) { | 863 | if (opcode == Opcode::OP_COPY_IN) { |
| @@ -2532,6 +2532,41 @@ TEST_F(PreGraphTest, maybeCycle) | |||
| 2532 | // check after pass | 2532 | // check after pass |
| 2533 | EXPECT_EQ(function->Operations().size(), opSize); | 2533 | EXPECT_EQ(function->Operations().size(), opSize); |
| 2534 | } | 2534 | } |
| 2535 | + | ||
| 2536 | +TEST_F(PreGraphTest, KeepParallelAssemblesWithSiblingCopyIn) | ||
| 2537 | +{ | ||
| 2538 | + ComputationalGraphBuilder graph; | ||
| 2539 | + graph.AddTensor(DT_FP32, {4, 4}, MEM_UB, "ub"); | ||
| 2540 | + graph.AddTensor(DT_FP32, {4, 4}, MEM_DEVICE_DDR, "gm"); | ||
| 2541 | + graph.AddTensor(DT_FP32, {8, 8}, MEM_DEVICE_DDR, "out1"); | ||
| 2542 | + graph.AddTensor(DT_FP32, {8, 8}, MEM_DEVICE_DDR, "out2"); | ||
| 2543 | + graph.AddTensor(DT_FP32, {4, 4}, MEM_UB, "read"); | ||
| 2544 | + graph.GetTensor("gm")->tensor = graph.GetTensor("out1")->tensor; | ||
| 2545 | + graph.GetTensor("out2")->tensor = graph.GetTensor("out1")->tensor; | ||
| 2546 | + graph.AddOp(Opcode::OP_COPY_OUT, {"ub"}, {"gm"}, "writer"); | ||
| 2547 | + auto copyAttr = std::make_shared<CopyOpAttribute>(MEM_UB, OpImmediate::Specified({0, 0}), | ||
| 2548 | + OpImmediate::Specified({4, 4}), OpImmediate::Specified({4, 4})); | ||
| 2549 | + graph.GetOp("writer")->SetOpAttribute(copyAttr); | ||
| 2550 | + graph.AddOp(Opcode::OP_ASSEMBLE, {"gm"}, {"out1"}, "assemble1"); | ||
| 2551 | + graph.AddOp(Opcode::OP_ASSEMBLE, {"gm"}, {"out2"}, "assemble2"); | ||
| 2552 | + graph.GetOp("assemble1") | ||
| 2553 | + ->SetOpAttribute(std::make_shared<AssembleOpAttribute>(Offset{2, 4}, CreateTestConstIntVector({2, 4}))); | ||
| 2554 | + graph.GetOp("assemble2") | ||
| 2555 | + ->SetOpAttribute(std::make_shared<AssembleOpAttribute>(Offset{4, 0}, CreateTestConstIntVector({4, 0}))); | ||
| 2556 | + graph.AddOp(Opcode::OP_COPY_IN, {"gm"}, {"read"}, "reader"); | ||
| 2557 | + graph.AddTensor(DT_FP32, {4, 16}, MEM_DEVICE_DDR, "final1"); | ||
| 2558 | + graph.AddTensor(DT_FP32, {4, 16}, MEM_DEVICE_DDR, "final2"); | ||
| 2559 | + graph.AddOp(Opcode::OP_RESHAPE, {"out1"}, {"final1"}, "reshape1"); | ||
| 2560 | + graph.AddOp(Opcode::OP_RESHAPE, {"out2"}, {"final2"}, "reshape2"); | ||
| 2561 | + graph.SetOutCast({"final1", "final2"}); | ||
| 2562 | + | ||
| 2563 | + RemoveRedundantAssemble pass; | ||
| 2564 | + ASSERT_EQ(pass.DeleteRedundantAssemble(*graph.GetFunction()), SUCCESS); | ||
| 2565 | + EXPECT_EQ(graph.GetFunction()->Operations().size(), 6); | ||
| 2566 | + EXPECT_EQ(graph.GetOp("reader")->GetIOperands().front(), graph.GetTensor("gm")); | ||
| 2567 | + CompareOpImmediateVector(copyAttr->GetToOffset(), {0, 0}); | ||
| 2568 | + CompareOpImmediateVector(copyAttr->GetRawShape(), {4, 4}); | ||
| 2569 | +} | ||
| 2535 | } // namespace tile_fwk | 2570 | } // namespace tile_fwk |
| 2536 | } // namespace npu | 2571 | } // namespace npu |
| 2537 | #undef private | 2572 | #undef private |
| @@ -1859,3 +1859,29 @@ TEST_F(FoldConsumerViewOffsetTest, FoldSkippedWithAllConstantOffsets) | |||
| 1859 | 1859 | ||
| 1860 | EXPECT_EQ(copyInOp.GetOOperands().front(), output) << "All-constant offsets should not trigger fold"; | 1860 | EXPECT_EQ(copyInOp.GetOOperands().front(), output) << "All-constant offsets should not trigger fold"; |
| 1861 | } | 1861 | } |
| 1862 | + | ||
| 1863 | +TEST_F(TestRemoveUnalignedReshapeOp, TestTerminalDynamicReshapeSkipsPairedConversion) | ||
| 1864 | +{ | ||
| 1865 | + auto function = std::make_shared<Function>(Program::GetInstance(), "TestMetadataReshape", "TestMetadataReshape", | ||
| 1866 | + nullptr); | ||
| 1867 | + IRBuilder builder; | ||
| 1868 | + auto ub = builder.CreateTensorVar(DT_FP32, {8, 8}, CreateTestConstIntVector({8, 8})); | ||
| 1869 | + ub->SetMemoryTypeBoth(MemoryType::MEM_UB, true); | ||
| 1870 | + auto gm = builder.CreateTensorVar(DT_FP32, {-1, 8}, {CreateTestScalarVar("rows"), SymbolicScalar(8)}); | ||
| 1871 | + gm->SetMemoryTypeBoth(MemoryType::MEM_DEVICE_DDR, true); | ||
| 1872 | + auto alias = builder.CreateTensorVar(*function, gm->GetRawTensor(), {0, 0}, {-1, 16}, | ||
| 1873 | + {CreateTestScalarVar("rows"), CreateTestScalarVar("cols")}); | ||
| 1874 | + alias->SetMemoryTypeBoth(MemoryType::MEM_DEVICE_DDR, true); | ||
| 1875 | + auto& copyOut = PassOperationUtils::AddOperation(*function, Opcode::OP_COPY_OUT, {ub}, {gm}); | ||
| 1876 | + PassOperationUtils::AddOperation(*function, Opcode::OP_RESHAPE, {gm}, {alias}); | ||
| 1877 | + function->outCasts_.push_back(alias); | ||
| 1878 | + auto readUb = builder.CreateTensorVar(DT_FP32, {8, 8}, CreateTestConstIntVector({8, 8})); | ||
| 1879 | + readUb->SetMemoryTypeBoth(MemoryType::MEM_UB, true); | ||
| 1880 | + auto& copyIn = PassOperationUtils::AddOperation(*function, Opcode::OP_COPY_IN, {gm}, {readUb}); | ||
| 1881 | + | ||
| 1882 | + RemoveUnalignedReshape pass; | ||
| 1883 | + EXPECT_EQ(pass.RunOnFunction(*function), SUCCESS); | ||
| 1884 | + EXPECT_EQ(function->Operations().size(), 3); | ||
| 1885 | + EXPECT_EQ(copyOut.GetOpcode(), Opcode::OP_COPY_OUT); | ||
| 1886 | + EXPECT_EQ(copyIn.GetOpcode(), Opcode::OP_COPY_IN); | ||
| 1887 | +} | ||