|
| 1 | +#include <gtest/gtest.h> |
| 2 | + |
| 3 | +#include <fmt/format.h> |
| 4 | + |
| 5 | +#include <torch/nativert/executor/memory/AliasAnalyzer.h> |
| 6 | +#include <torch/nativert/graph/Graph.h> |
| 7 | + |
| 8 | +#include <torch/nativert/executor/Executor.h> |
| 9 | +#include <torch/nativert/kernels/KernelFactory.h> |
| 10 | + |
| 11 | +using namespace ::testing; |
| 12 | +using namespace torch::nativert; |
| 13 | + |
| 14 | +using AliasTestCase = std::tuple< |
| 15 | + std::string /* value */, |
| 16 | + AllocationLifetime, |
| 17 | + bool /* is_alias */, |
| 18 | + bool /* is_storage_associated_with_output */, |
| 19 | + c10::FastSet<std::string> /* source(s) */>; |
| 20 | + |
| 21 | +class AliasAnalyzerTests : public testing::Test { |
| 22 | + void SetUp() override {} |
| 23 | + |
| 24 | + void TearDown() override { |
| 25 | + test_cases.clear(); |
| 26 | + model.clear(); |
| 27 | + } |
| 28 | + |
| 29 | + public: |
| 30 | + void setTestCases(std::vector<AliasTestCase> cases) { |
| 31 | + test_cases = std::move(cases); |
| 32 | + } |
| 33 | + |
| 34 | + void setModel(std::string m) { |
| 35 | + model = std::move(m); |
| 36 | + } |
| 37 | + |
| 38 | + void run() { |
| 39 | + EXPECT_FALSE(test_cases.empty()); |
| 40 | + EXPECT_FALSE(model.empty()); |
| 41 | + |
| 42 | + ExecutorConfig cfg; |
| 43 | + cfg.enableStaticCPUKernels = true; |
| 44 | + |
| 45 | + auto graph = stringToGraph(model); |
| 46 | + auto kernels = KernelFactory().initializeNodeKernels( |
| 47 | + *graph, nullptr, cfg, {}, nullptr); |
| 48 | + auto kernelSchemas = Executor::getKernelSchemas(kernels.nodeKernels); |
| 49 | + |
| 50 | + AliasAnalyzer analyzer(*graph, kernelSchemas); |
| 51 | + |
| 52 | + for ( |
| 53 | + auto& [value, lifetime, is_alias, is_storage_associated_with_output, srcs] : |
| 54 | + test_cases) { |
| 55 | + LOG(INFO) << fmt::format( |
| 56 | + "running test: value={}, lifetime=({}, {}), is_alias={}, is_storage_associated_with_output={}, src={}", |
| 57 | + value, |
| 58 | + lifetime.start, |
| 59 | + lifetime.end, |
| 60 | + is_alias, |
| 61 | + is_storage_associated_with_output, |
| 62 | + srcs.empty() ? "{}" |
| 63 | + : std::accumulate( |
| 64 | + srcs.begin(), |
| 65 | + srcs.end(), |
| 66 | + std::string{}, |
| 67 | + [](std::string cur, const std::string& src) { |
| 68 | + cur.append(","); |
| 69 | + cur.append(src); |
| 70 | + return cur; |
| 71 | + })); |
| 72 | + auto* v = graph->getValue(value); |
| 73 | + EXPECT_EQ(analyzer.lifetime(v), lifetime); |
| 74 | + EXPECT_EQ(analyzer.is_alias(v), is_alias); |
| 75 | + EXPECT_EQ( |
| 76 | + analyzer.is_storage_associated_with_output(v), |
| 77 | + is_storage_associated_with_output); |
| 78 | + const auto* resolved_srcs = analyzer.get_sources_of_alias(v); |
| 79 | + if (resolved_srcs /* ensure set equality between *resolved_srcs and srcs */) { |
| 80 | + EXPECT_FALSE(srcs.empty()); |
| 81 | + EXPECT_EQ(resolved_srcs->size(), srcs.size()); |
| 82 | + for (const auto& resolved_src : *resolved_srcs) { |
| 83 | + EXPECT_TRUE(srcs.erase(std::string(resolved_src->name())) == 1); |
| 84 | + } |
| 85 | + EXPECT_TRUE(srcs.empty()); |
| 86 | + } else { |
| 87 | + EXPECT_TRUE(srcs.empty()); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private: |
| 93 | + std::string model; |
| 94 | + std::vector<AliasTestCase> test_cases; |
| 95 | +}; |
| 96 | + |
| 97 | +TEST_F(AliasAnalyzerTests, TestNoAlias) { |
| 98 | + setModel(R"( |
| 99 | + graph(%y0, %y1): |
| 100 | + %out_t = torch.ops.aten.matmul.default(self=%y0, other=%y1) |
| 101 | + %res = torch.ops.aten.clone.default(self=%out_t, memory_format=None) |
| 102 | + return (%res))"); |
| 103 | + |
| 104 | + setTestCases({ |
| 105 | + {"out_t", AllocationLifetime(1, 2), false, false, {}}, |
| 106 | + {"res", AllocationLifetime(2, 3), false, true, {}}, |
| 107 | + }); |
| 108 | + |
| 109 | + run(); |
| 110 | +} |
| 111 | + |
| 112 | +TEST_F(AliasAnalyzerTests, TestSimpleAlias) { |
| 113 | + setModel(R"( |
| 114 | + graph(%y0, %y1): |
| 115 | + %out_t = torch.ops.aten.matmul.default(self=%y0, other=%y1) |
| 116 | + %res = torch.ops.aten.slice.Tensor(self=%out_t, dim=1, start=0, end=0, step=1) |
| 117 | + return (%res))"); |
| 118 | + |
| 119 | + setTestCases({ |
| 120 | + {"out_t", AllocationLifetime(1, 3), false, true, {}}, |
| 121 | + {"res", AllocationLifetime(2, 3), true, false, {"out_t"}}, |
| 122 | + }); |
| 123 | + |
| 124 | + run(); |
| 125 | +} |
| 126 | + |
| 127 | +TEST_F(AliasAnalyzerTests, TestDeepAlias) { |
| 128 | + setModel(R"( |
| 129 | + graph(%y0, %y1): |
| 130 | + %out_t = torch.ops.aten.matmul.default(self=%y0, other=%y1) |
| 131 | + %a1 = torch.ops.aten.slice.Tensor(self=%out_t, dim=1, start=0, end=0, step=1) |
| 132 | + %res = torch.ops.aten.slice.Tensor(self=%a1, dim=1, start=0, end=0, step=1) |
| 133 | + return (%res))"); |
| 134 | + |
| 135 | + setTestCases({ |
| 136 | + {"out_t", AllocationLifetime(1, 4), false, true, {}}, |
| 137 | + {"a1", AllocationLifetime(2, 4), true, false, {"out_t"}}, |
| 138 | + {"res", AllocationLifetime(3, 4), true, false, {"out_t"}}, |
| 139 | + }); |
| 140 | + |
| 141 | + run(); |
| 142 | +} |
| 143 | + |
| 144 | +TEST_F(AliasAnalyzerTests, TestPackedListUnpack) { |
| 145 | + setModel(R"( |
| 146 | + graph(%a, %b, %c, %d): |
| 147 | + %input_list[] = prim.ListPack(l0=%a, l1=%b, l2=%c, l3=%d) |
| 148 | + %x0, %x1, %x2, %x3 = prim.ListUnpack(input=%input_list) |
| 149 | + return (%x1, %x3))"); |
| 150 | + |
| 151 | + setTestCases({ |
| 152 | + {"a", AllocationLifetime(0, 2), false, false, {}}, |
| 153 | + {"x0", AllocationLifetime(2, 2), true, false, {"a"}}, |
| 154 | + {"b", AllocationLifetime(0, 3), false, true, {}}, |
| 155 | + {"x1", AllocationLifetime(2, 3), true, false, {"b"}}, |
| 156 | + {"c", AllocationLifetime(0, 2), false, false, {}}, |
| 157 | + {"x2", AllocationLifetime(2, 2), true, false, {"c"}}, |
| 158 | + {"d", AllocationLifetime(0, 3), false, true, {}}, |
| 159 | + {"x3", AllocationLifetime(2, 3), true, false, {"d"}}, |
| 160 | + }); |
| 161 | + |
| 162 | + run(); |
| 163 | +} |
| 164 | + |
| 165 | +TEST_F(AliasAnalyzerTests, TestAmbiguousSourceOfAlias) { |
| 166 | + setModel(R"( |
| 167 | + graph(%y0, %y1): |
| 168 | + %out_t = torch.ops.aten.matmul.default(self=%y0, other=%y1) |
| 169 | + %out_t2 = torch.ops.aten.matmul.default(self=%y0, other=%y1) |
| 170 | + %a1 = prim.VarStack(l0=%out_t, l1=%out_t2) |
| 171 | + %res = torch.ops.aten.slice.Tensor(self=%a1, dim=1, start=0, end=0, step=1) |
| 172 | + return (%res))"); |
| 173 | + |
| 174 | + setTestCases({ |
| 175 | + {"out_t", AllocationLifetime(1, 5), false, true, {}}, |
| 176 | + {"out_t2", AllocationLifetime(2, 5), false, true, {}}, |
| 177 | + {"a1", AllocationLifetime(3, 5), true, false, {"out_t", "out_t2"}}, |
| 178 | + {"res", AllocationLifetime(4, 5), true, false, {"out_t", "out_t2"}}, |
| 179 | + }); |
| 180 | + |
| 181 | + run(); |
| 182 | +} |
0 commit comments