| @@ -0,0 +1,118 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025-2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file cluster_list_sort.h | ||
| 13 | + * \brief Cluster-based List Schedule: static clustering + ready-cluster queue + three-way compare, | ||
| 14 | + * targeting vec, not online-memory aware. Splits the op graph into maximal single-in-single-out | ||
| 15 | + * chains (clusters), maintains a ready-cluster queue, and picks one op per step via comparator | ||
| 16 | + * (peak -> end -> net-release -> secondaryId). Ops with net memory growth are deferred via | ||
| 17 | + * break_op_queue with three-way comparison. Allocs are excluded and reinserted at first use; | ||
| 18 | + * L0 memory is handled by the base class ExecuteOp(). | ||
| 19 | + */ | ||
| 20 | + | ||
| 21 | +#ifndef PASS_CLUSTER_LIST_SORT_H | ||
| 22 | +#define PASS_CLUSTER_LIST_SORT_H | ||
| 23 | + | ||
| 24 | +#include "optimize_sort.h" | ||
| 25 | + | ||
| 26 | +#include <cstdint> | ||
| 27 | +#include <deque> | ||
| 28 | +#include <unordered_map> | ||
| 29 | +#include <unordered_set> | ||
| 30 | +#include <vector> | ||
| 31 | + | ||
| 32 | +namespace npu::tile_fwk { | ||
| 33 | + | ||
| 34 | +class ClusterListSort : public OptimizeSort { | ||
| 35 | +public: | ||
| 36 | + using OptimizeSort::OptimizeSort; | ||
| 37 | + | ||
| 38 | +private: | ||
| 39 | + Status DoSortOps() override; | ||
| 40 | + | ||
| 41 | + // === 静态量 / 内存量 === | ||
| 42 | + int64_t TensorBytes(int memId) const; | ||
| 43 | + int64_t ReleaseBytes(Operation* op) const; // 该 op 执行后 refcount 归零的输入字节 | ||
| 44 | + int64_t OccupyBytes(Operation* op) const; // 该 op 新写出的本地输出字节 | ||
| 45 | + int64_t NetOf(Operation* op) const { return OccupyBytes(op) - ReleaseBytes(op); } | ||
| 46 | + void DecRemainCons(Operation* op); // op 输入 memId 的本 list 内剩余消费者数各 −1(同 op 内去重) | ||
| 47 | + | ||
| 48 | + // === 建簇:极大单入单出链 === | ||
| 49 | + Status BuildClusters(); | ||
| 50 | + void ComputeDegrees(); // 填 producersByMem_/consumersByMem_(按 memId 聚合) | ||
| 51 | + bool IsChainStart(Operation* op); // 链起点判据(查 depManager 非 const) | ||
| 52 | + bool IsChainEnd(Operation* op); // 链终点判据(同上) | ||
| 53 | + void LinkAdjacent(std::unordered_map<Operation*, Operation*>& nextOf, | ||
| 54 | + std::unordered_map<Operation*, Operation*>& prevOf); | ||
| 55 | + Status WalkChains(const std::unordered_map<Operation*, Operation*>& nextOf, | ||
| 56 | + const std::unordered_map<Operation*, Operation*>& prevOf); // 走链建簇 + 对称性校验 | ||
| 57 | + void AssignSecondaryIds(); // 分支簇并查集合并 | ||
| 58 | + int64_t ComputeBasePeak(const std::vector<Operation*>& clusterOps) const; // 簇内静态峰值 | ||
| 59 | + | ||
| 60 | + // === 主排序 / 调度 === | ||
| 61 | + bool ClusterStaticLess(int a, int b) const; // 簇静态键:(basePeak, end, secondaryId, id) | ||
| 62 | + void TryAddReady(int cid); // 簇头前驱已齐 → 入 readyClusters_(去重) | ||
| 63 | + void Commit(Operation* op); // 执行一个 op:入序 / refcount− / 游标+ / 解锁后继 | ||
| 64 | + std::vector<Operation*> SelectReadyHeads() const; // 取可执行的 ready 簇头 | ||
| 65 | + std::vector<Operation*> SortedReadyHeads(); // 取 ready 簇头(插入时已有序) | ||
| 66 | + std::pair<Operation*, int64_t> TryStickyCluster(); // 黏簇尝试,失败清 currentCid_ | ||
| 67 | + std::pair<Operation*, int64_t> PickTiedCandidate(const std::vector<Operation*>& heads) const; // 打平组选净涨最小 | ||
| 68 | + void ThreeWayChoose(const std::vector<Operation*>& heads, Operation* cand, int64_t candNet); // 三方净涨比较 | ||
| 69 | + Status RunScheduleLoop(); | ||
| 70 | + | ||
| 71 | + // === alloc 回插 === | ||
| 72 | + std::vector<Operation*> CollectLeftoverAllocs(const std::vector<Operation*>& allocOps, | ||
| 73 | + const std::unordered_set<int>& allocated) const; | ||
| 74 | + std::vector<Operation*> ReinsertAllocs(const std::vector<Operation*>& allocOps) const; | ||
| 75 | + | ||
| 76 | + // === DoSortOps 子步骤 === | ||
| 77 | + std::vector<Operation*> InitRunState(); // 分离 alloc/normals + 重置工作态 + tensorRemainCons_ | ||
| 78 | + void InitUnmetPreds(); // unmetPreds_ 初始化(只算 normals_ 内依赖) | ||
| 79 | + | ||
| 80 | + // === 簇(簇级状态收进一个对象,不写进 IR) === | ||
| 81 | + struct Cluster { | ||
| 82 | + int id = -1; | ||
| 83 | + std::vector<Operation*> ops; // 建链序 == 簇内执行序(intraOrder 即下标) | ||
| 84 | + int64_t basePeak = 0; // 簇内静态峰值 | ||
| 85 | + int64_t end = 0; // 簇末保留的本地输出字节 | ||
| 86 | + int secondaryId = -1; // 喂同一聚点的分支簇共享 | ||
| 87 | + size_t cursor = 0; // 下一个待执行 op 的下标 | ||
| 88 | + bool inReady = false; // 是否已入 readyClusters_(去重,防重复 push) | ||
| 89 | + Operation* CurOp() const { return cursor < ops.size() ? ops[cursor] : nullptr; } | ||
| 90 | + bool Done() const { return cursor >= ops.size(); } | ||
| 91 | + }; | ||
| 92 | + | ||
| 93 | + // === 簇结构 === | ||
| 94 | + std::vector<Cluster> clusters_; // 按 clusterId 下标索引 | ||
| 95 | + std::unordered_map<Operation*, int> cluster_; // op -> clusterId(反查) | ||
| 96 | + | ||
| 97 | + // === 建簇工作态(ComputeDegrees 内填,只统计 normals_,按 memId 聚合 view) === | ||
| 98 | + // memId -> 本 list 内产/读该 buffer 的 op 们。度数=size();唯一者=size()==1 时的 [0]。 | ||
| 99 | + std::unordered_map<int, std::vector<Operation*>> producersByMem_; | ||
| 100 | + std::unordered_map<int, std::vector<Operation*>> consumersByMem_; | ||
| 101 | + | ||
| 102 | + // === DoSortOps 单次运行的工作态(开头重置) === | ||
| 103 | + std::vector<Operation*> normals_; // 非 alloc op | ||
| 104 | + std::unordered_set<Operation*> normalSet_; // normals_ 的集合视图 | ||
| 105 | + std::unordered_map<int, int> | ||
| 106 | + tensorRemainCons_; // memId -> 本 list 内剩余未执行消费者 op 数(非全局 refcount,不含 alloc) | ||
| 107 | + std::unordered_map<Operation*, int> unmetPreds_; // op -> 未满足前驱数(只算 normals_ 内) | ||
| 108 | + std::deque<Operation*> breakQ_; // 押后的 op,尾进头出 | ||
| 109 | + std::unordered_set<Operation*> inBreakQ_; // 快速判定 + 冻结其簇 | ||
| 110 | + std::vector<int> readyClusters_; // ready 簇 id,按 ClusterStaticLess 有序 | ||
| 111 | + std::unordered_map<int, size_t> readyPos_; // clusterId → readyClusters_ 下标(O(1) 定位删除位置) | ||
| 112 | + int lastSecondaryId_ = -1; // 上一个 Commit 的簇的 secondaryId(net 打平时黏在同组) | ||
| 113 | + int currentCid_ = -1; // 黏簇:当前正在推进的簇,-1 表示需重选 | ||
| 114 | + std::vector<Operation*> orderedNormals_; // 最终执行序(不含 alloc) | ||
| 115 | +}; | ||
| 116 | + | ||
| 117 | +} // namespace npu::tile_fwk | ||
| 118 | +#endif // PASS_CLUSTER_LIST_SORT_H | ||
| @@ -18,7 +18,7 @@ | |||
| 18 | #include <queue> | 18 | #include <queue> |
| 19 | 19 | ||
| 20 | #include "prior_dfs_sort.h" | 20 | #include "prior_dfs_sort.h" |
| 21 | -#include <queue> | 21 | +#include "cluster_list_sort.h" |
| 22 | #include "passes/pass_log/pass_log.h" | 22 | #include "passes/pass_log/pass_log.h" |
| 23 | 23 | ||
| 24 | namespace npu::tile_fwk { | 24 | namespace npu::tile_fwk { |
| @@ -548,6 +548,7 @@ void OptimizeSort::AllocAhead() | |||
| 548 | 548 | ||
| 549 | const std::unordered_map<std::string, OptimizeSort::Factory> OptimizeSort::SORT_ALGOS = { | 549 | const std::unordered_map<std::string, OptimizeSort::Factory> OptimizeSort::SORT_ALGOS = { |
| 550 | {"PriorDFS", [](std::vector<Operation*> ops, Function& f) { return std::make_unique<PriorDFSSort>(ops, f); }}, | 550 | {"PriorDFS", [](std::vector<Operation*> ops, Function& f) { return std::make_unique<PriorDFSSort>(ops, f); }}, |
| 551 | + {"ClusterList", [](std::vector<Operation*> ops, Function& f) { return std::make_unique<ClusterListSort>(ops, f); }}, | ||
| 551 | }; | 552 | }; |
| 552 | 553 | ||
| 553 | std::string OptimizeSort::ResolveOooSortMode(const std::vector<Operation*>& ops, const ParamConfigs& pc) | 554 | std::string OptimizeSort::ResolveOooSortMode(const std::vector<Operation*>& ops, const ParamConfigs& pc) |
| @@ -0,0 +1,425 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025-2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file test_cluster_list_sort.cpp | ||
| 13 | + * \brief Unit test for ClusterListSort. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include <gtest/gtest.h> | ||
| 17 | + | ||
| 18 | +#include <string> | ||
| 19 | +#include <vector> | ||
| 20 | + | ||
| 21 | +#include "symbolic_scalar_test_utils.h" | ||
| 22 | +#include "interface/function/function.h" | ||
| 23 | +#include "interface/inner/tilefwk.h" | ||
| 24 | +#include "interface/tensor/irbuilder.h" | ||
| 25 | +#include "tilefwk/tilefwk.h" | ||
| 26 | +#define private public | ||
| 27 | +#include "computational_graph_builder.h" | ||
| 28 | +#include "passes/block_graph_pass/schedule_ooo/pre_schedule/cluster_list_sort.h" | ||
| 29 | + | ||
| 30 | +namespace npu::tile_fwk { | ||
| 31 | + | ||
| 32 | +class ClusterListSortTest : public ::testing::Test { | ||
| 33 | +public: | ||
| 34 | + void SetUp() override { Program::GetInstance().Reset(); } | ||
| 35 | + void TearDown() override {} | ||
| 36 | +}; | ||
| 37 | + | ||
| 38 | +static bool Before(const std::vector<Operation*>& ops, Operation* a, Operation* b) | ||
| 39 | +{ | ||
| 40 | + size_t ia = ops.size(); | ||
| 41 | + size_t ib = ops.size(); | ||
| 42 | + for (size_t i = 0; i < ops.size(); ++i) { | ||
| 43 | + if (ops[i] == a) { | ||
| 44 | + ia = i; | ||
| 45 | + } | ||
| 46 | + if (ops[i] == b) { | ||
| 47 | + ib = i; | ||
| 48 | + } | ||
| 49 | + } | ||
| 50 | + return ia < ib; | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +// 用例 1:单簇直行。copy_in -> add -> copy_out,验证建簇/黏簇/alloc 回插。 | ||
| 54 | +TEST_F(ClusterListSortTest, SingleClusterStraightRun) | ||
| 55 | +{ | ||
| 56 | + ComputationalGraphBuilder b; | ||
| 57 | + std::vector<std::string> tensorNames{"t1", "t2", "t3", "t4"}; | ||
| 58 | + std::vector<MemoryType> memTypes{MEM_DEVICE_DDR, MEM_UB, MEM_UB, MEM_DEVICE_DDR}; | ||
| 59 | + EXPECT_TRUE(b.AddTensors(DT_FP32, {64, 64}, memTypes, tensorNames, 0)); | ||
| 60 | + std::vector<Opcode> opcodes{Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, Opcode::OP_COPY_IN, Opcode::OP_ADD, | ||
| 61 | + Opcode::OP_COPY_OUT}; | ||
| 62 | + std::vector<std::vector<std::string>> ins{{}, {}, {"t1"}, {"t2"}, {"t3"}}; | ||
| 63 | + std::vector<std::vector<std::string>> outs{{"t2"}, {"t3"}, {"t2"}, {"t3"}, {"t4"}}; | ||
| 64 | + std::vector<std::string> opNames{"Alloc1", "Alloc2", "CopyIn", "Add", "CopyOut"}; | ||
| 65 | + EXPECT_TRUE(b.AddOps(opcodes, ins, outs, opNames, true)); | ||
| 66 | + Function* func = b.GetFunction(); | ||
| 67 | + ASSERT_NE(func, nullptr); | ||
| 68 | + | ||
| 69 | + auto ops = func->Operations().DuplicatedOpList(); | ||
| 70 | + ClusterListSort sorter(ops, *func); | ||
| 71 | + sorter.state_.Init(ops); | ||
| 72 | + EXPECT_EQ(sorter.DoSortOps(), SUCCESS); | ||
| 73 | + | ||
| 74 | + ASSERT_EQ(sorter.clusters_.size(), 1U); | ||
| 75 | + ASSERT_EQ(sorter.clusters_[0].ops.size(), 3U); | ||
| 76 | + EXPECT_EQ(sorter.clusters_[0].ops[0], b.GetOp("CopyIn")); | ||
| 77 | + EXPECT_EQ(sorter.clusters_[0].ops[1], b.GetOp("Add")); | ||
| 78 | + EXPECT_EQ(sorter.clusters_[0].ops[2], b.GetOp("CopyOut")); | ||
| 79 | + EXPECT_EQ(sorter.cluster_[b.GetOp("CopyIn")], 0); | ||
| 80 | + EXPECT_EQ(sorter.cluster_[b.GetOp("Add")], 0); | ||
| 81 | + EXPECT_EQ(sorter.cluster_[b.GetOp("CopyOut")], 0); | ||
| 82 | + EXPECT_TRUE(Before(sorter.operations, b.GetOp("CopyIn"), b.GetOp("Add"))); | ||
| 83 | + EXPECT_TRUE(Before(sorter.operations, b.GetOp("Add"), b.GetOp("CopyOut"))); | ||
| 84 | + EXPECT_TRUE(Before(sorter.operations, b.GetOp("Alloc2"), b.GetOp("Add"))); | ||
| 85 | +} | ||
| 86 | + | ||
| 87 | +// 用例 2:菱形分簇 + secondaryId。验证 fan-out/join 断簇、并查集合组。 | ||
| 88 | +TEST_F(ClusterListSortTest, DiamondFanOutJoinSecondaryId) | ||
| 89 | +{ | ||
| 90 | + ComputationalGraphBuilder b; | ||
| 91 | + std::vector<std::string> tensorNames{"t1", "t2", "t3", "t5", "t6", "t7", "t8"}; | ||
| 92 | + std::vector<MemoryType> memTypes{MEM_DEVICE_DDR, MEM_UB, MEM_UB, MEM_UB, MEM_UB, MEM_UB, MEM_DEVICE_DDR}; | ||
| 93 | + EXPECT_TRUE(b.AddTensors(DT_FP32, {64, 64}, memTypes, tensorNames, 0)); | ||
| 94 | + std::vector<Opcode> opcodes{Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, | ||
| 95 | + Opcode::OP_UB_ALLOC, Opcode::OP_COPY_IN, Opcode::OP_ADD, Opcode::OP_ADD, | ||
| 96 | + Opcode::OP_ADD, Opcode::OP_ADD, Opcode::OP_COPY_OUT}; | ||
| 97 | + std::vector<std::vector<std::string>> ins{{}, {}, {}, {}, {}, {"t1"}, {"t2"}, {"t3"}, {"t3"}, {"t5", "t6"}, {"t7"}}; | ||
| 98 | + std::vector<std::vector<std::string>> outs{{"t2"}, {"t3"}, {"t5"}, {"t6"}, {"t7"}, {"t2"}, | ||
| 99 | + {"t3"}, {"t5"}, {"t6"}, {"t7"}, {"t8"}}; | ||
| 100 | + std::vector<std::string> opNames{"Alloc1", "Alloc2", "Alloc3", "Alloc4", "Alloc5", "CopyIn", | ||
| 101 | + "Add0", "Add1", "Add2", "Add3", "CopyOut"}; | ||
| 102 | + EXPECT_TRUE(b.AddOps(opcodes, ins, outs, opNames, true)); | ||
| 103 | + Function* func = b.GetFunction(); | ||
| 104 | + ASSERT_NE(func, nullptr); | ||
| 105 | + | ||
| 106 | + auto ops = func->Operations().DuplicatedOpList(); | ||
| 107 | + ClusterListSort sorter(ops, *func); | ||
| 108 | + sorter.state_.Init(ops); | ||
| 109 | + EXPECT_EQ(sorter.DoSortOps(), SUCCESS); | ||
| 110 | + | ||
| 111 | + ASSERT_EQ(sorter.clusters_.size(), 4U); | ||
| 112 | + ASSERT_EQ(sorter.clusters_[0].ops.size(), 2U); | ||
| 113 | + EXPECT_EQ(sorter.clusters_[0].ops[0], b.GetOp("CopyIn")); | ||
| 114 | + EXPECT_EQ(sorter.clusters_[0].ops[1], b.GetOp("Add0")); | ||
| 115 | + ASSERT_EQ(sorter.clusters_[3].ops.size(), 2U); | ||
| 116 | + EXPECT_EQ(sorter.clusters_[3].ops[0], b.GetOp("Add3")); | ||
| 117 | + EXPECT_EQ(sorter.clusters_[3].ops[1], b.GetOp("CopyOut")); | ||
| 118 | + EXPECT_EQ(sorter.clusters_[1].secondaryId, sorter.clusters_[2].secondaryId); | ||
| 119 | + EXPECT_TRUE(Before(sorter.operations, b.GetOp("CopyIn"), b.GetOp("Add0"))); | ||
| 120 | + EXPECT_TRUE(Before(sorter.operations, b.GetOp("Add0"), b.GetOp("Add1"))); | ||
| 121 | + EXPECT_TRUE(Before(sorter.operations, b.GetOp("Add0"), b.GetOp("Add2"))); | ||
| 122 | + EXPECT_TRUE(Before(sorter.operations, b.GetOp("Add1"), b.GetOp("Add3"))); | ||
| 123 | + EXPECT_TRUE(Before(sorter.operations, b.GetOp("Add2"), b.GetOp("Add3"))); | ||
| 124 | + EXPECT_TRUE(Before(sorter.operations, b.GetOp("Add3"), b.GetOp("CopyOut"))); | ||
| 125 | +} | ||
| 126 | + | ||
| 127 | +// 用例 3a:三方比较换簇。A 首个 op 无条件执行;第二 op net>0,B 头 net 更小 -> 跳转 B。 | ||
| 128 | +// 簇 A: copy_inA(t1->t2, 4KB) -> addA(t2->t3, 24KB) -> copy_outA(t3->t4 DDR) | ||
| 129 | +// basePeak=28KB。copy_inA 无条件执行,addA net=+20KB>0 -> 三方比较。 | ||
| 130 | +// 簇 B: copy_inB(t5->t7, 16KB) -> addB(t7->t6, 16KB),basePeak=32KB。 | ||
| 131 | +// B 头=copy_inB net=+16KB < 20KB -> 胜出,跳转 B。addB net=0 -> 黏簇直行。 | ||
| 132 | +// 期望 orderedNormals_ = [copy_inA, copy_inB, addB, addA, copy_outA]。 | ||
| 133 | +TEST_F(ClusterListSortTest, ThreeWayChooseSwitchToB) | ||
| 134 | +{ | ||
| 135 | + ComputationalGraphBuilder b; | ||
| 136 | + std::vector<std::string> tensorNames{"t1", "t2", "t3", "t4", "t5", "t6", "t7"}; | ||
| 137 | + std::vector<MemoryType> memTypes{MEM_DEVICE_DDR, MEM_UB, MEM_UB, MEM_DEVICE_DDR, MEM_DEVICE_DDR, MEM_UB, MEM_UB}; | ||
| 138 | + EXPECT_TRUE(b.AddTensors(DT_FP32, {64, 64}, memTypes, tensorNames, 0)); | ||
| 139 | + // t2=4KB(32x32), t3=24KB(96x64), t7=16KB(默认64x64), t6=16KB(默认64x64) | ||
| 140 | + auto t2 = b.GetTensor("t2"); | ||
| 141 | + t2->shape = {32, 32}; | ||
【🟡 建议修改】测试在 AddTensors 后直接修改 shape/rawshape,依赖 state_.Init 刷新 size
如果 建议:确认 ![]() ![]() | |||
| 142 | + t2->tensor->rawshape = {32, 32}; | ||
| 143 | + auto t3 = b.GetTensor("t3"); | ||
| 144 | + t3->shape = {96, 64}; | ||
| 145 | + t3->tensor->rawshape = {96, 64}; | ||
| 146 | + | ||
| 147 | + std::vector<Opcode> opcodes{Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, | ||
| 148 | + Opcode::OP_UB_ALLOC, Opcode::OP_COPY_IN, Opcode::OP_ADD, | ||
| 149 | + Opcode::OP_COPY_OUT, Opcode::OP_COPY_IN, Opcode::OP_ADD}; | ||
| 150 | + std::vector<std::vector<std::string>> ins{{}, {}, {}, {}, {"t1"}, {"t2"}, {"t3"}, {"t5"}, {"t7"}}; | ||
| 151 | + std::vector<std::vector<std::string>> outs{{"t2"}, {"t3"}, {"t6"}, {"t7"}, {"t2"}, {"t3"}, {"t4"}, {"t7"}, {"t6"}}; | ||
| 152 | + std::vector<std::string> opNames{"AllocA1", "AllocA2", "AllocB1", "AllocB2", "CopyInA", | ||
| 153 | + "AddA", "CopyOutA", "CopyInB", "AddB"}; | ||
| 154 | + EXPECT_TRUE(b.AddOps(opcodes, ins, outs, opNames, true)); | ||
| 155 | + Function* func = b.GetFunction(); | ||
| 156 | + ASSERT_NE(func, nullptr); | ||
| 157 | + | ||
| 158 | + auto ops = func->Operations().DuplicatedOpList(); | ||
| 159 | + ClusterListSort sorter(ops, *func); | ||
| 160 | + sorter.state_.Init(ops); | ||
| 161 | + EXPECT_EQ(sorter.DoSortOps(), SUCCESS); | ||
| 162 | + | ||
| 163 | + ASSERT_EQ(sorter.clusters_.size(), 2U); | ||
| 164 | + ASSERT_EQ(sorter.orderedNormals_.size(), 5U); | ||
| 165 | + EXPECT_EQ(sorter.orderedNormals_[0], b.GetOp("CopyInA")); // A 新簇首 op 无条件执行 | ||
| 166 | + // addA net=+20KB, copy_inB net=+16KB<20KB -> ThreeWayChoose 跳转 copy_inB | ||
| 167 | + EXPECT_EQ(sorter.orderedNormals_[1], b.GetOp("CopyInB")); // 胜出执行 | ||
| 168 | + EXPECT_EQ(sorter.orderedNormals_[2], b.GetOp("AddB")); // 黏簇直行 net=0 | ||
| 169 | + EXPECT_EQ(sorter.orderedNormals_[3], b.GetOp("AddA")); // breakQ pop 后执行 | ||
| 170 | + EXPECT_EQ(sorter.orderedNormals_[4], b.GetOp("CopyOutA")); // 黏簇直行 | ||
| 171 | +} | ||
| 172 | + | ||
| 173 | +// 用例 3b:三方比较不换簇。A 第二 op net>0,但 B 头 net 更大 -> A 不跳转,继续执行。 | ||
| 174 | +// 簇 A: copy_inA(t1->t2, 4KB) -> addA(t2->t3, 16KB) -> copy_outA(t3->t4 DDR),basePeak=20KB。 | ||
| 175 | +// addA net=+12KB>0 -> 三方比较。 | ||
| 176 | +// 簇 B: copy_inB(t5->t7, 32KB) -> addB(t7->t6, 4KB),basePeak=36KB。 | ||
| 177 | +// B 头=copy_inB net=+32KB > 12KB -> 不跳转,A 继续。A 完成后才轮到 B。 | ||
| 178 | +// 期望 orderedNormals_ = [copy_inA, addA, copy_outA, copy_inB, addB]。 | ||
| 179 | +TEST_F(ClusterListSortTest, ThreeWayChooseNoSwitch) | ||
| 180 | +{ | ||
| 181 | + ComputationalGraphBuilder b; | ||
| 182 | + std::vector<std::string> tensorNames{"t1", "t2", "t3", "t4", "t5", "t6", "t7"}; | ||
| 183 | + std::vector<MemoryType> memTypes{MEM_DEVICE_DDR, MEM_UB, MEM_UB, MEM_DEVICE_DDR, MEM_DEVICE_DDR, MEM_UB, MEM_UB}; | ||
| 184 | + EXPECT_TRUE(b.AddTensors(DT_FP32, {64, 64}, memTypes, tensorNames, 0)); | ||
| 185 | + // t2=4KB(32x32), t3=16KB(默认64x64), t7=32KB(128x64), t6=4KB(32x32) | ||
| 186 | + auto t2 = b.GetTensor("t2"); | ||
| 187 | + t2->shape = {32, 32}; | ||
| 188 | + t2->tensor->rawshape = {32, 32}; | ||
| 189 | + auto t7 = b.GetTensor("t7"); | ||
| 190 | + t7->shape = {128, 64}; | ||
| 191 | + t7->tensor->rawshape = {128, 64}; | ||
| 192 | + auto t6 = b.GetTensor("t6"); | ||
| 193 | + t6->shape = {32, 32}; | ||
| 194 | + t6->tensor->rawshape = {32, 32}; | ||
| 195 | + | ||
| 196 | + std::vector<Opcode> opcodes{Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, | ||
| 197 | + Opcode::OP_UB_ALLOC, Opcode::OP_COPY_IN, Opcode::OP_ADD, | ||
| 198 | + Opcode::OP_COPY_OUT, Opcode::OP_COPY_IN, Opcode::OP_ADD}; | ||
| 199 | + std::vector<std::vector<std::string>> ins{{}, {}, {}, {}, {"t1"}, {"t2"}, {"t3"}, {"t5"}, {"t7"}}; | ||
| 200 | + std::vector<std::vector<std::string>> outs{{"t2"}, {"t3"}, {"t6"}, {"t7"}, {"t2"}, {"t3"}, {"t4"}, {"t7"}, {"t6"}}; | ||
| 201 | + std::vector<std::string> opNames{"AllocA1", "AllocA2", "AllocB1", "AllocB2", "CopyInA", | ||
| 202 | + "AddA", "CopyOutA", "CopyInB", "AddB"}; | ||
| 203 | + EXPECT_TRUE(b.AddOps(opcodes, ins, outs, opNames, true)); | ||
| 204 | + Function* func = b.GetFunction(); | ||
| 205 | + ASSERT_NE(func, nullptr); | ||
| 206 | + | ||
| 207 | + auto ops = func->Operations().DuplicatedOpList(); | ||
| 208 | + ClusterListSort sorter(ops, *func); | ||
| 209 | + sorter.state_.Init(ops); | ||
| 210 | + EXPECT_EQ(sorter.DoSortOps(), SUCCESS); | ||
| 211 | + | ||
| 212 | + ASSERT_EQ(sorter.clusters_.size(), 2U); | ||
| 213 | + ASSERT_EQ(sorter.orderedNormals_.size(), 5U); | ||
| 214 | + EXPECT_EQ(sorter.orderedNormals_[0], b.GetOp("CopyInA")); // A 新簇首 op 无条件执行 | ||
| 215 | + // addA net=+12KB, copy_inB net=+32KB>12KB -> 不跳转,A 继续 | ||
| 216 | + EXPECT_EQ(sorter.orderedNormals_[1], b.GetOp("AddA")); | ||
| 217 | + EXPECT_EQ(sorter.orderedNormals_[2], b.GetOp("CopyOutA")); // 黏簇直行 net=-16KB | ||
| 218 | + // A 完成后才轮到 B | ||
| 219 | + EXPECT_EQ(sorter.orderedNormals_[3], b.GetOp("CopyInB")); // B 新簇首 op 无条件执行 | ||
| 220 | + EXPECT_EQ(sorter.orderedNormals_[4], b.GetOp("AddB")); // 黏簇直行 net=-28KB | ||
| 221 | +} | ||
| 222 | + | ||
| 223 | +// 用例 4:alloc 回插(多生产者写不同 memId + 未引用 alloc 放最前)。 | ||
| 224 | +// 图:t1(DDR) -> CopyIn1 -> t2(UB, 16KB) | ||
| 225 | +// -> Add -> t4(UB) -> CopyOut -> t5(DDR) | ||
| 226 | +// t3(DDR) -> CopyIn2 -> t6(UB, 32KB) | ||
| 227 | +// Alloc1->t2, Alloc2->t4, Alloc3->t9(未引用), Alloc4->t6。t2 < t6 使 CopyIn1 簇排在 CopyIn2 前。 | ||
| 228 | +TEST_F(ClusterListSortTest, ReinsertAllocsMultiProducerAndLeftover) | ||
| 229 | +{ | ||
| 230 | + ComputationalGraphBuilder b; | ||
| 231 | + std::vector<std::string> tensorNames{"t1", "t2", "t3", "t4", "t5", "t6", "t9"}; | ||
| 232 | + std::vector<MemoryType> memTypes{MEM_DEVICE_DDR, MEM_UB, MEM_DEVICE_DDR, MEM_UB, MEM_DEVICE_DDR, MEM_UB, MEM_UB}; | ||
| 233 | + EXPECT_TRUE(b.AddTensors(DT_FP32, {64, 64}, memTypes, tensorNames, 0)); | ||
| 234 | + // t2=16KB(64x64 默认), t6=32KB(128x64) -> CopyIn1 簇 basePeak 小排前面 | ||
| 235 | + auto t6 = b.GetTensor("t6"); | ||
| 236 | + t6->shape = {128, 64}; | ||
| 237 | + t6->tensor->rawshape = {128, 64}; | ||
| 238 | + std::vector<Opcode> opcodes{Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, | ||
| 239 | + Opcode::OP_COPY_IN, Opcode::OP_COPY_IN, Opcode::OP_ADD, Opcode::OP_COPY_OUT}; | ||
| 240 | + std::vector<std::vector<std::string>> ins{{}, {}, {}, {}, {"t1"}, {"t3"}, {"t2", "t6"}, {"t4"}}; | ||
| 241 | + std::vector<std::vector<std::string>> outs{{"t2"}, {"t4"}, {"t9"}, {"t6"}, {"t2"}, {"t6"}, {"t4"}, {"t5"}}; | ||
| 242 | + std::vector<std::string> opNames{"Alloc1", "Alloc2", "Alloc3", "Alloc4", "CopyIn1", "CopyIn2", "Add", "CopyOut"}; | ||
| 243 | + EXPECT_TRUE(b.AddOps(opcodes, ins, outs, opNames, true)); | ||
| 244 | + Function* func = b.GetFunction(); | ||
| 245 | + ASSERT_NE(func, nullptr); | ||
| 246 | + | ||
| 247 | + auto ops = func->Operations().DuplicatedOpList(); | ||
| 248 | + ClusterListSort sorter(ops, *func); | ||
| 249 | + sorter.state_.Init(ops); | ||
| 250 | + EXPECT_EQ(sorter.DoSortOps(), SUCCESS); | ||
| 251 | + | ||
| 252 | + // 建簇:CopyIn1(t2 16KB) / CopyIn2(t6 32KB) 各独立成簇,Add+CopyOut 合一簇 | ||
| 253 | + ASSERT_EQ(sorter.clusters_.size(), 3U); | ||
| 254 | + // 按 ops 内容找到对应簇 id(cluster id 按 normals_ 遍历顺序分配,不按 basePeak) | ||
| 255 | + auto FindCid = [&](Operation* op) -> int { | ||
| 256 | + auto it = sorter.cluster_.find(op); | ||
| 257 | + return it == sorter.cluster_.end() ? -1 : it->second; | ||
| 258 | + }; | ||
| 259 | + int cidCopyIn1 = FindCid(b.GetOp("CopyIn1")); | ||
| 260 | + int cidCopyIn2 = FindCid(b.GetOp("CopyIn2")); | ||
| 261 | + int cidAdd = FindCid(b.GetOp("Add")); | ||
| 262 | + EXPECT_GE(cidCopyIn1, 0); | ||
| 263 | + EXPECT_GE(cidCopyIn2, 0); | ||
| 264 | + EXPECT_GE(cidAdd, 0); | ||
| 265 | + auto& clCopyIn1 = sorter.clusters_[static_cast<size_t>(cidCopyIn1)]; | ||
| 266 | + auto& clCopyIn2 = sorter.clusters_[static_cast<size_t>(cidCopyIn2)]; | ||
| 267 | + auto& clAddCopyOut = sorter.clusters_[static_cast<size_t>(cidAdd)]; | ||
| 268 | + ASSERT_EQ(clCopyIn1.ops.size(), 1U); | ||
| 269 | + EXPECT_EQ(clCopyIn1.ops[0], b.GetOp("CopyIn1")); | ||
| 270 | + ASSERT_EQ(clCopyIn2.ops.size(), 1U); | ||
| 271 | + EXPECT_EQ(clCopyIn2.ops[0], b.GetOp("CopyIn2")); | ||
| 272 | + ASSERT_EQ(clAddCopyOut.ops.size(), 2U); | ||
| 273 | + EXPECT_EQ(clAddCopyOut.ops[0], b.GetOp("Add")); | ||
| 274 | + // t2(16KB) < t6(32KB) -> CopyIn1 簇 basePeak < CopyIn2 簇 basePeak -> 调度时 CopyIn1 先 | ||
| 275 | + EXPECT_LT(clCopyIn1.basePeak, clCopyIn2.basePeak); | ||
| 276 | + | ||
| 277 | + auto& result = sorter.operations; | ||
| 278 | + ASSERT_EQ(result.size(), 8U); | ||
| 279 | + // 未引用 alloc 放最前 | ||
| 280 | + EXPECT_EQ(result[0], b.GetOp("Alloc3")); | ||
| 281 | + // alloc_t2 在 copy_in1 前 | ||
| 282 | + EXPECT_TRUE(Before(result, b.GetOp("Alloc1"), b.GetOp("CopyIn1"))); | ||
| 283 | + // alloc_t4 在 add 前 | ||
| 284 | + EXPECT_TRUE(Before(result, b.GetOp("Alloc2"), b.GetOp("Add"))); | ||
| 285 | + // copy_in1 在 copy_in2 前(t2 basePeak < t6 basePeak) | ||
| 286 | + EXPECT_TRUE(Before(result, b.GetOp("CopyIn1"), b.GetOp("CopyIn2"))); | ||
| 287 | +} | ||
| 288 | + | ||
| 289 | +// 用例 5:空输入边界。 | ||
| 290 | +TEST_F(ClusterListSortTest, EmptyInput) | ||
【🟡 建议修改】UT 覆盖存在缺口:缺少 FAILED 路径与多输出算子测试 当前 5 个用例覆盖了单簇直行、菱形分簇、三方比较换/不换簇、alloc 回插、空输入,核心路径覆盖良好。但以下关键场景缺失:
建议:补充 (1) 构造环形依赖图验证 FAILED 返回;(2) 含多输出 op 的图验证簇边界正确;(3) 50+ op 的图验证调度收敛。 ![]() ![]() | |||
| 291 | +{ | ||
| 292 | + ComputationalGraphBuilder b; | ||
| 293 | + Function* func = b.GetFunction(); | ||
| 294 | + ASSERT_NE(func, nullptr); | ||
| 295 | + auto ops = func->Operations().DuplicatedOpList(); | ||
| 296 | + ClusterListSort sorter(ops, *func); | ||
| 297 | + sorter.state_.Init(ops); | ||
| 298 | + EXPECT_EQ(sorter.DoSortOps(), SUCCESS); | ||
| 299 | + EXPECT_TRUE(sorter.operations.empty()); | ||
| 300 | +} | ||
| 301 | + | ||
| 302 | +// 用例 6:多输出算子(fan-out)断簇。Add 写 2 个 UB tensor → IsChainEnd=true,应为簇末。 | ||
| 303 | +TEST_F(ClusterListSortTest, MultiOutputOpBreaksCluster) | ||
| 304 | +{ | ||
| 305 | + ComputationalGraphBuilder b; | ||
| 306 | + std::vector<std::string> tensorNames{"t1", "t2", "t3", "t4", "t5", "t6"}; | ||
| 307 | + std::vector<MemoryType> memTypes{MEM_DEVICE_DDR, MEM_UB, MEM_UB, MEM_UB, MEM_DEVICE_DDR, MEM_DEVICE_DDR}; | ||
| 308 | + EXPECT_TRUE(b.AddTensors(DT_FP32, {64, 64}, memTypes, tensorNames, 0)); | ||
| 309 | + std::vector<Opcode> opcodes{Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, Opcode::OP_COPY_IN, | ||
| 310 | + Opcode::OP_ADD, Opcode::OP_COPY_OUT, Opcode::OP_COPY_OUT}; | ||
| 311 | + std::vector<std::vector<std::string>> ins{{}, {}, {}, {"t1"}, {"t2"}, {"t3"}, {"t4"}}; | ||
| 312 | + std::vector<std::vector<std::string>> outs{{"t2"}, {"t3"}, {"t4"}, {"t2"}, {"t3", "t4"}, {"t5"}, {"t6"}}; | ||
| 313 | + std::vector<std::string> opNames{"Alloc1", "Alloc2", "Alloc3", "CopyIn", "FanOutAdd", "CopyOut1", "CopyOut2"}; | ||
| 314 | + EXPECT_TRUE(b.AddOps(opcodes, ins, outs, opNames, true)); | ||
| 315 | + Function* func = b.GetFunction(); | ||
| 316 | + ASSERT_NE(func, nullptr); | ||
| 317 | + | ||
| 318 | + auto ops = func->Operations().DuplicatedOpList(); | ||
| 319 | + ClusterListSort sorter(ops, *func); | ||
| 320 | + sorter.state_.Init(ops); | ||
| 321 | + EXPECT_EQ(sorter.DoSortOps(), SUCCESS); | ||
| 322 | + | ||
| 323 | + auto FindCid = [&](Operation* op) -> int { | ||
| 324 | + auto it = sorter.cluster_.find(op); | ||
| 325 | + return it == sorter.cluster_.end() ? -1 : it->second; | ||
| 326 | + }; | ||
| 327 | + int cidCopyIn = FindCid(b.GetOp("CopyIn")); | ||
| 328 | + int cidFanOut = FindCid(b.GetOp("FanOutAdd")); | ||
| 329 | + int cidCopyOut1 = FindCid(b.GetOp("CopyOut1")); | ||
| 330 | + int cidCopyOut2 = FindCid(b.GetOp("CopyOut2")); | ||
| 331 | + EXPECT_GE(cidCopyIn, 0); | ||
| 332 | + EXPECT_GE(cidFanOut, 0); | ||
| 333 | + EXPECT_GE(cidCopyOut1, 0); | ||
| 334 | + EXPECT_GE(cidCopyOut2, 0); | ||
| 335 | + EXPECT_EQ(cidCopyIn, cidFanOut); | ||
| 336 | + EXPECT_NE(cidCopyIn, cidCopyOut1); | ||
| 337 | + EXPECT_NE(cidCopyIn, cidCopyOut2); | ||
| 338 | + auto& clFanOut = sorter.clusters_[static_cast<size_t>(cidFanOut)]; | ||
| 339 | + ASSERT_GE(clFanOut.ops.size(), 2U); | ||
| 340 | + EXPECT_EQ(clFanOut.ops.front(), b.GetOp("CopyIn")); | ||
| 341 | + EXPECT_EQ(clFanOut.ops.back(), b.GetOp("FanOutAdd")); | ||
| 342 | + EXPECT_TRUE(Before(sorter.operations, b.GetOp("CopyIn"), b.GetOp("FanOutAdd"))); | ||
| 343 | + EXPECT_TRUE(Before(sorter.operations, b.GetOp("FanOutAdd"), b.GetOp("CopyOut1"))); | ||
| 344 | + EXPECT_TRUE(Before(sorter.operations, b.GetOp("FanOutAdd"), b.GetOp("CopyOut2"))); | ||
| 345 | +} | ||
| 346 | + | ||
| 347 | +// 用例 7:环形依赖 → RunScheduleLoop 返回 FAILED。 | ||
| 348 | +// 构造正常图后手动设置 unmetPreds_ 模拟环形依赖(所有 op 互相依赖 → 无 ready 簇头)。 | ||
| 349 | +TEST_F(ClusterListSortTest, CycleDependencyReturnsFailed) | ||
| 350 | +{ | ||
| 351 | + ComputationalGraphBuilder b; | ||
| 352 | + std::vector<std::string> tensorNames{"t1", "t2", "t3", "t4"}; | ||
| 353 | + std::vector<MemoryType> memTypes{MEM_DEVICE_DDR, MEM_UB, MEM_UB, MEM_DEVICE_DDR}; | ||
| 354 | + EXPECT_TRUE(b.AddTensors(DT_FP32, {64, 64}, memTypes, tensorNames, 0)); | ||
| 355 | + std::vector<Opcode> opcodes{Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, Opcode::OP_COPY_IN, Opcode::OP_ADD, | ||
| 356 | + Opcode::OP_COPY_OUT}; | ||
| 357 | + std::vector<std::vector<std::string>> ins{{}, {}, {"t1"}, {"t2"}, {"t3"}}; | ||
| 358 | + std::vector<std::vector<std::string>> outs{{"t2"}, {"t3"}, {"t2"}, {"t3"}, {"t4"}}; | ||
| 359 | + std::vector<std::string> opNames{"Alloc1", "Alloc2", "CopyIn", "Add", "CopyOut"}; | ||
| 360 | + EXPECT_TRUE(b.AddOps(opcodes, ins, outs, opNames, true)); | ||
| 361 | + Function* func = b.GetFunction(); | ||
| 362 | + ASSERT_NE(func, nullptr); | ||
| 363 | + | ||
| 364 | + auto ops = func->Operations().DuplicatedOpList(); | ||
| 365 | + ClusterListSort sorter(ops, *func); | ||
| 366 | + sorter.state_.Init(ops); | ||
| 367 | + sorter.InitRunState(); | ||
| 368 | + ASSERT_EQ(sorter.BuildClusters(), SUCCESS); | ||
| 369 | + sorter.InitUnmetPreds(); | ||
| 370 | + for (auto& [op, cnt] : sorter.unmetPreds_) { | ||
| 371 | + cnt = 1; | ||
| 372 | + } | ||
| 373 | + EXPECT_EQ(sorter.RunScheduleLoop(), FAILED); | ||
| 374 | +} | ||
| 375 | + | ||
| 376 | +// 用例 8:breakQ 队头胜出。addA 进 breakQ 后,下一轮 breakQ 队头 net 最小 → 从 breakQ 弹出执行。 | ||
| 377 | +// 簇 A: copy_inA(t1->t2, 4KB) -> addA(t2->t3, 32KB) -> copy_outA(t3->t4 DDR),addA net=+28KB。 | ||
| 378 | +// 簇 D: copy_inD(t5->t6, 4KB) -> addD(t6->t7, 64KB),copy_inD net=+4KB,addD net=+60KB。 | ||
| 379 | +// 调度: copy_inA 执行 → addA net>0, next head=copy_inD(4)<28 → addA 进 breakQ, copy_inD 胜出 → | ||
| 380 | +// addD net>0, breakQ 队头=addA(28)<60 → addA 从 breakQ 胜出执行。 | ||
| 381 | +TEST_F(ClusterListSortTest, ThreeWayChooseBreakQHeadWins) | ||
| 382 | +{ | ||
| 383 | + ComputationalGraphBuilder b; | ||
| 384 | + std::vector<std::string> tensorNames{"t1", "t2", "t3", "t4", "t5", "t6", "t7"}; | ||
| 385 | + std::vector<MemoryType> memTypes{MEM_DEVICE_DDR, MEM_UB, MEM_UB, MEM_DEVICE_DDR, MEM_DEVICE_DDR, MEM_UB, MEM_UB}; | ||
| 386 | + EXPECT_TRUE(b.AddTensors(DT_FP32, {64, 64}, memTypes, tensorNames, 0)); | ||
| 387 | + auto t2 = b.GetTensor("t2"); | ||
| 388 | + t2->shape = {32, 32}; | ||
| 389 | + t2->tensor->rawshape = {32, 32}; | ||
| 390 | + auto t3 = b.GetTensor("t3"); | ||
| 391 | + t3->shape = {128, 64}; | ||
| 392 | + t3->tensor->rawshape = {128, 64}; | ||
| 393 | + auto t6 = b.GetTensor("t6"); | ||
| 394 | + t6->shape = {32, 32}; | ||
| 395 | + t6->tensor->rawshape = {32, 32}; | ||
| 396 | + auto t7 = b.GetTensor("t7"); | ||
| 397 | + t7->shape = {256, 64}; | ||
| 398 | + t7->tensor->rawshape = {256, 64}; | ||
| 399 | + | ||
| 400 | + std::vector<Opcode> opcodes{Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, | ||
| 401 | + Opcode::OP_UB_ALLOC, Opcode::OP_COPY_IN, Opcode::OP_ADD, | ||
| 402 | + Opcode::OP_COPY_OUT, Opcode::OP_COPY_IN, Opcode::OP_ADD}; | ||
| 403 | + std::vector<std::vector<std::string>> ins{{}, {}, {}, {}, {"t1"}, {"t2"}, {"t3"}, {"t5"}, {"t6"}}; | ||
| 404 | + std::vector<std::vector<std::string>> outs{{"t2"}, {"t3"}, {"t6"}, {"t7"}, {"t2"}, {"t3"}, {"t4"}, {"t6"}, {"t7"}}; | ||
| 405 | + std::vector<std::string> opNames{"AllocA1", "AllocA2", "AllocD1", "AllocD2", "CopyInA", | ||
| 406 | + "AddA", "CopyOutA", "CopyInD", "AddD"}; | ||
| 407 | + EXPECT_TRUE(b.AddOps(opcodes, ins, outs, opNames, true)); | ||
| 408 | + Function* func = b.GetFunction(); | ||
| 409 | + ASSERT_NE(func, nullptr); | ||
| 410 | + | ||
| 411 | + auto ops = func->Operations().DuplicatedOpList(); | ||
| 412 | + ClusterListSort sorter(ops, *func); | ||
| 413 | + sorter.state_.Init(ops); | ||
| 414 | + EXPECT_EQ(sorter.DoSortOps(), SUCCESS); | ||
| 415 | + | ||
| 416 | + ASSERT_EQ(sorter.clusters_.size(), 2U); | ||
| 417 | + ASSERT_EQ(sorter.orderedNormals_.size(), 5U); | ||
| 418 | + EXPECT_EQ(sorter.orderedNormals_[0], b.GetOp("CopyInA")); | ||
| 419 | + EXPECT_EQ(sorter.orderedNormals_[1], b.GetOp("CopyInD")); | ||
| 420 | + EXPECT_EQ(sorter.orderedNormals_[2], b.GetOp("AddA")); | ||
| 421 | + EXPECT_EQ(sorter.orderedNormals_[3], b.GetOp("CopyOutA")); | ||
| 422 | + EXPECT_EQ(sorter.orderedNormals_[4], b.GetOp("AddD")); | ||
| 423 | +} | ||
| 424 | + | ||
| 425 | +} // namespace npu::tile_fwk | ||
| @@ -31,7 +31,9 @@ | |||
| 31 | #include "computational_graph_builder.h" | 31 | #include "computational_graph_builder.h" |
| 32 | #include "passes/block_graph_pass/schedule_ooo/common/iso_matcher.h" | 32 | #include "passes/block_graph_pass/schedule_ooo/common/iso_matcher.h" |
| 33 | #include "passes/block_graph_pass/schedule_ooo/post_schedule/buffer_rearrange.h" | 33 | #include "passes/block_graph_pass/schedule_ooo/post_schedule/buffer_rearrange.h" |
| 34 | +#include "passes/block_graph_pass/schedule_ooo/pre_schedule/cluster_list_sort.h" | ||
| 34 | #include "passes/block_graph_pass/schedule_ooo/pre_schedule/core_assign.h" | 35 | #include "passes/block_graph_pass/schedule_ooo/pre_schedule/core_assign.h" |
| 36 | +#include "passes/block_graph_pass/schedule_ooo/pre_schedule/prior_dfs_sort.h" | ||
| 35 | #include "passes/block_graph_pass/schedule_ooo/schedule_ooo.h" | 37 | #include "passes/block_graph_pass/schedule_ooo/schedule_ooo.h" |
| 36 | #include "passes/tile_graph_pass/graph_constraint/infer_dyn_shape.h" | 38 | #include "passes/tile_graph_pass/graph_constraint/infer_dyn_shape.h" |
| 37 | 39 | ||
| @@ -3010,6 +3012,8 @@ TEST_F(ScheduleOoOTest, IsoMatch_IsoMatchChains_RootSignatureMismatch) | |||
| 3010 | EXPECT_EQ(res.pairs.size(), 0u); | 3012 | EXPECT_EQ(res.pairs.size(), 0u); |
| 3011 | } | 3013 | } |
| 3012 | 3014 | ||
| 3015 | +// === upstream/master: ModifyAllocOrder tests === | ||
| 3016 | + | ||
| 3013 | static int IndexOf(const std::vector<Operation*>& opList, Operation* op) | 3017 | static int IndexOf(const std::vector<Operation*>& opList, Operation* op) |
| 3014 | { | 3018 | { |
| 3015 | for (size_t i = 0; i < opList.size(); i++) { | 3019 | for (size_t i = 0; i < opList.size(); i++) { |
| @@ -3121,4 +3125,104 @@ TEST_F(ScheduleOoOTest, TestModifyAllocOrderInsertsAllocOnceForInPlaceOp) | |||
| 3121 | EXPECT_EQ(IndexOf(opList, subGraph.GetOp("AddInPlace")), 1); | 3125 | EXPECT_EQ(IndexOf(opList, subGraph.GetOp("AddInPlace")), 1); |
| 3122 | } | 3126 | } |
| 3123 | 3127 | ||
| 3128 | +// === vecSortAlgo: OptimizeSort factory interface tests === | ||
| 3129 | + | ||
| 3130 | +// 用按 mode 实例化正确的 sorter 子类。 | ||
| 3131 | +TEST_F(ScheduleOoOTest, OptimizeSortCreateFactory) | ||
| 3132 | +{ | ||
| 3133 | + auto rootFuncPtr = std::make_shared<Function>(Program::GetInstance(), "TestParams", "TestParams", nullptr); | ||
| 3134 | + rootFuncPtr->rootFunc_ = rootFuncPtr.get(); | ||
| 3135 | + auto currFunctionPtr = std::make_shared<Function>(Program::GetInstance(), "TestCreate", "TestCreate", | ||
| 3136 | + rootFuncPtr.get()); | ||
| 3137 | + ASSERT_NE(currFunctionPtr, nullptr); | ||
| 3138 | + | ||
| 3139 | + std::vector<Operation*> emptyOps; | ||
| 3140 | + auto priordfs = OptimizeSort::Create(emptyOps, *currFunctionPtr, "PriorDFS"); | ||
| 3141 | + EXPECT_NE(priordfs, nullptr); | ||
| 3142 | + EXPECT_NE(dynamic_cast<PriorDFSSort*>(priordfs.get()), nullptr); | ||
| 3143 | + | ||
| 3144 | + auto clusterList = OptimizeSort::Create(emptyOps, *currFunctionPtr, "ClusterList"); | ||
| 3145 | + EXPECT_NE(clusterList, nullptr); | ||
| 3146 | + EXPECT_NE(dynamic_cast<ClusterListSort*>(clusterList.get()), nullptr); | ||
| 3147 | + | ||
| 3148 | + auto unknown = OptimizeSort::Create(emptyOps, *currFunctionPtr, "UnknownMode"); | ||
| 3149 | + EXPECT_EQ(unknown, nullptr); | ||
| 3150 | +} | ||
| 3151 | + | ||
| 3152 | +// SortOps 端到端——oooSortModeAiv="ClusterList" + 纯 AIV 图 → SUCCESS。 | ||
| 3153 | +TEST_F(ScheduleOoOTest, SortOpsClusterListMode) | ||
| 3154 | +{ | ||
| 3155 | + ComputationalGraphBuilder subGraph; | ||
| 3156 | + std::vector<std::string> tensorNames{"t1", "t2", "t3", "t4"}; | ||
| 3157 | + std::vector<MemoryType> tensorMemTypes{MemoryType::MEM_DEVICE_DDR, MemoryType::MEM_UB, MemoryType::MEM_UB, | ||
| 3158 | + MemoryType::MEM_DEVICE_DDR}; | ||
| 3159 | + std::vector<Opcode> opCodes{Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, Opcode::OP_COPY_IN, Opcode::OP_ADD, | ||
| 3160 | + Opcode::OP_COPY_OUT}; | ||
| 3161 | + std::vector<std::vector<std::string>> ioperands{{}, {}, {"t1"}, {"t2"}, {"t3"}}; | ||
| 3162 | + std::vector<std::vector<std::string>> ooperands{{"t2"}, {"t3"}, {"t2"}, {"t3"}, {"t4"}}; | ||
| 3163 | + std::vector<std::string> opNames{"Alloc1", "Alloc2", "Copyin1", "Add1", "Copyout1"}; | ||
| 3164 | + EXPECT_EQ(subGraph.AddTensors(DataType::DT_FP32, {64, 64}, tensorMemTypes, tensorNames, 0), true); | ||
| 3165 | + EXPECT_EQ(subGraph.AddOps(opCodes, ioperands, ooperands, opNames, true), true); | ||
| 3166 | + Function* function = subGraph.GetFunction(); | ||
| 3167 | + ASSERT_NE(function, nullptr); | ||
| 3168 | + function->paramConfigs_.oooSortModeAiv = "ClusterList"; | ||
| 3169 | + | ||
| 3170 | + OptimizeSort sort(function->Operations().DuplicatedOpList(), *function); | ||
| 3171 | + EXPECT_EQ(sort.SortOps(), SUCCESS); | ||
| 3172 | + auto result = sort.GetOperations(); | ||
| 3173 | + EXPECT_EQ(result.size(), 5U); | ||
| 3174 | + Operation* copyin = subGraph.GetOp("Copyin1"); | ||
| 3175 | + Operation* add = subGraph.GetOp("Add1"); | ||
| 3176 | + Operation* copyout = subGraph.GetOp("Copyout1"); | ||
| 3177 | + auto pos = [&](Operation* target) { | ||
| 3178 | + for (size_t i = 0; i < result.size(); ++i) { | ||
| 3179 | + if (result[i] == target) { | ||
| 3180 | + return i; | ||
| 3181 | + } | ||
| 3182 | + } | ||
| 3183 | + return result.size(); | ||
| 3184 | + }; | ||
| 3185 | + EXPECT_LT(pos(copyin), pos(add)); | ||
| 3186 | + EXPECT_LT(pos(add), pos(copyout)); | ||
| 3187 | +} | ||
| 3188 | + | ||
| 3189 | +// SortOps 端到端——oooSortModeAiv="" + 纯 AIV 图 → SUCCESS(默认走 PriorDFS)。 | ||
| 3190 | +TEST_F(ScheduleOoOTest, SortOpsDefaultPriorDFSMode) | ||
| 3191 | +{ | ||
| 3192 | + ComputationalGraphBuilder subGraph; | ||
| 3193 | + std::vector<std::string> tensorNames{"t1", "t2", "t3", "t4"}; | ||
| 3194 | + std::vector<MemoryType> tensorMemTypes{MemoryType::MEM_DEVICE_DDR, MemoryType::MEM_UB, MemoryType::MEM_UB, | ||
| 3195 | + MemoryType::MEM_DEVICE_DDR}; | ||
| 3196 | + std::vector<Opcode> opCodes{Opcode::OP_UB_ALLOC, Opcode::OP_UB_ALLOC, Opcode::OP_COPY_IN, Opcode::OP_ADD, | ||
| 3197 | + Opcode::OP_COPY_OUT}; | ||
| 3198 | + std::vector<std::vector<std::string>> ioperands{{}, {}, {"t1"}, {"t2"}, {"t3"}}; | ||
| 3199 | + std::vector<std::vector<std::string>> ooperands{{"t2"}, {"t3"}, {"t2"}, {"t3"}, {"t4"}}; | ||
| 3200 | + std::vector<std::string> opNames{"Alloc1", "Alloc2", "Copyin1", "Add1", "Copyout1"}; | ||
| 3201 | + EXPECT_EQ(subGraph.AddTensors(DataType::DT_FP32, {64, 64}, tensorMemTypes, tensorNames, 0), true); | ||
| 3202 | + EXPECT_EQ(subGraph.AddOps(opCodes, ioperands, ooperands, opNames, true), true); | ||
| 3203 | + Function* function = subGraph.GetFunction(); | ||
| 3204 | + ASSERT_NE(function, nullptr); | ||
| 3205 | + function->paramConfigs_.oooSortModeAiv = ""; | ||
| 3206 | + | ||
| 3207 | + OptimizeSort sort(function->Operations().DuplicatedOpList(), *function); | ||
| 3208 | + EXPECT_EQ(sort.SortOps(), SUCCESS); | ||
| 3209 | + auto result = sort.GetOperations(); | ||
| 3210 | + EXPECT_EQ(result.size(), 5U); | ||
| 3211 | +} | ||
| 3212 | + | ||
| 3213 | +// SortOps 端到端——空 operations → SUCCESS。 | ||
| 3214 | +TEST_F(ScheduleOoOTest, SortOpsEmptyInput) | ||
| 3215 | +{ | ||
| 3216 | + auto rootFuncPtr = std::make_shared<Function>(Program::GetInstance(), "TestParams", "TestParams", nullptr); | ||
| 3217 | + rootFuncPtr->rootFunc_ = rootFuncPtr.get(); | ||
| 3218 | + auto currFunctionPtr = std::make_shared<Function>(Program::GetInstance(), "TestEmpty", "TestEmpty", | ||
| 3219 | + rootFuncPtr.get()); | ||
| 3220 | + ASSERT_NE(currFunctionPtr, nullptr); | ||
| 3221 | + | ||
| 3222 | + std::vector<Operation*> emptyOps; | ||
| 3223 | + OptimizeSort sort(emptyOps, *currFunctionPtr); | ||
| 3224 | + EXPECT_EQ(sort.SortOps(), SUCCESS); | ||
| 3225 | + EXPECT_TRUE(sort.GetOperations().empty()); | ||
| 3226 | +} | ||
| 3227 | + | ||
| 3124 | } // namespace npu::tile_fwk | 3228 | } // namespace npu::tile_fwk |


【🟡 建议修改】新增排序算法为性能优化,PR 缺少精度回归证据
本 PR 新增 ClusterListSort 排序算法,PR 描述提到"rope 算子调优 spill 次数 5→4"的性能收益,但未附行为等价性证据。
Pass 专项 §10 / 已知缺陷模式 5 要求:纯性能 PR(合轴/融合/布局/tile 形状/merge 条件/调度顺序改变)必须附行为等价性证据——至少包含同名算子精度回归(优化前后同输入输出一致)、覆盖不同内存布局场景(GM stride 分行、L1、UB、非连续)、动态 shape / 尾块 / 广播等边界场景。
虽然 OoO 调度顺序改变理论上不影响数值结果,但调度顺序变化可能影响 buffer 复用路径和 L0 内存分配,间接引入数据错误(历史教训:性能优化 PR #3925 改 tile 布局导致 GM 交互数据错误 #2393)。
建议:在 PR 描述中补充:(1) 至少一个算子的优化前后精度回归对比结果;(2) 覆盖 rope 及其他典型 vec 算子的 ST 验证结果。