File: test_alias_analyzer.cpp

package info (click to toggle)
pytorch 2.9.1%2Bdfsg-1~exp2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 180,096 kB
  • sloc: python: 1,473,255; cpp: 942,030; ansic: 79,796; asm: 7,754; javascript: 2,502; java: 1,962; sh: 1,809; makefile: 628; xml: 8
file content (182 lines) | stat: -rw-r--r-- 5,714 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <gtest/gtest.h>

#include <fmt/format.h>

#include <torch/nativert/executor/memory/AliasAnalyzer.h>
#include <torch/nativert/graph/Graph.h>

#include <torch/nativert/executor/Executor.h>
#include <torch/nativert/kernels/KernelFactory.h>

using namespace ::testing;
using namespace torch::nativert;

using AliasTestCase = std::tuple<
    std::string /* value */,
    AllocationLifetime,
    bool /* is_alias */,
    bool /* is_storage_associated_with_output */,
    c10::FastSet<std::string> /* source(s) */>;

class AliasAnalyzerTests : public testing::Test {
  void SetUp() override {}

  void TearDown() override {
    test_cases.clear();
    model.clear();
  }

 public:
  void setTestCases(std::vector<AliasTestCase> cases) {
    test_cases = std::move(cases);
  }

  void setModel(std::string m) {
    model = std::move(m);
  }

  void run() {
    EXPECT_FALSE(test_cases.empty());
    EXPECT_FALSE(model.empty());

    ExecutorConfig cfg;
    cfg.enableStaticCPUKernels = true;

    auto graph = stringToGraph(model);
    auto kernels =
        KernelFactory().initializeNodeKernels(*graph, nullptr, cfg, nullptr);
    auto kernelSchemas = Executor::getKernelSchemas(kernels.nodeKernels);

    AliasAnalyzer analyzer(*graph, kernelSchemas);

    for (
        auto& [value, lifetime, is_alias, is_storage_associated_with_output, srcs] :
        test_cases) {
      LOG(INFO) << fmt::format(
          "running test: value={}, lifetime=({}, {}), is_alias={}, is_storage_associated_with_output={}, src={}",
          value,
          lifetime.start,
          lifetime.end,
          is_alias,
          is_storage_associated_with_output,
          srcs.empty() ? "{}"
                       : std::accumulate(
                             srcs.begin(),
                             srcs.end(),
                             std::string{},
                             [](std::string cur, const std::string& src) {
                               cur.append(",");
                               cur.append(src);
                               return cur;
                             }));
      auto* v = graph->getValue(value);
      EXPECT_EQ(analyzer.lifetime(v), lifetime);
      EXPECT_EQ(analyzer.is_alias(v), is_alias);
      EXPECT_EQ(
          analyzer.is_storage_associated_with_output(v),
          is_storage_associated_with_output);
      const auto* resolved_srcs = analyzer.get_sources_of_alias(v);
      if (resolved_srcs /* ensure set equality between *resolved_srcs and srcs */) {
        EXPECT_FALSE(srcs.empty());
        EXPECT_EQ(resolved_srcs->size(), srcs.size());
        for (const auto& resolved_src : *resolved_srcs) {
          EXPECT_TRUE(srcs.erase(std::string(resolved_src->name())) == 1);
        }
        EXPECT_TRUE(srcs.empty());
      } else {
        EXPECT_TRUE(srcs.empty());
      }
    }
  }

 private:
  std::string model;
  std::vector<AliasTestCase> test_cases;
};

TEST_F(AliasAnalyzerTests, TestNoAlias) {
  setModel(R"(
    graph(%y0, %y1):
      %out_t = torch.ops.aten.matmul.default(self=%y0, other=%y1)
      %res = torch.ops.aten.clone.default(self=%out_t, memory_format=None)
  return (%res))");

  setTestCases({
      {"out_t", AllocationLifetime(1, 2), false, false, {}},
      {"res", AllocationLifetime(2, 3), false, true, {}},
  });

  run();
}

TEST_F(AliasAnalyzerTests, TestSimpleAlias) {
  setModel(R"(
    graph(%y0, %y1):
      %out_t = torch.ops.aten.matmul.default(self=%y0, other=%y1)
      %res = torch.ops.aten.slice.Tensor(self=%out_t, dim=1, start=0, end=0, step=1)
  return (%res))");

  setTestCases({
      {"out_t", AllocationLifetime(1, 3), false, true, {}},
      {"res", AllocationLifetime(2, 3), true, false, {"out_t"}},
  });

  run();
}

TEST_F(AliasAnalyzerTests, TestDeepAlias) {
  setModel(R"(
    graph(%y0, %y1):
      %out_t = torch.ops.aten.matmul.default(self=%y0, other=%y1)
      %a1 = torch.ops.aten.slice.Tensor(self=%out_t, dim=1, start=0, end=0, step=1)
      %res = torch.ops.aten.slice.Tensor(self=%a1, dim=1, start=0, end=0, step=1)
  return (%res))");

  setTestCases({
      {"out_t", AllocationLifetime(1, 4), false, true, {}},
      {"a1", AllocationLifetime(2, 4), true, false, {"out_t"}},
      {"res", AllocationLifetime(3, 4), true, false, {"out_t"}},
  });

  run();
}

TEST_F(AliasAnalyzerTests, TestPackedListUnpack) {
  setModel(R"(
    graph(%a, %b, %c, %d):
  %input_list[] = prim.ListPack(l0=%a, l1=%b, l2=%c, l3=%d)
  %x0, %x1, %x2, %x3 = prim.ListUnpack(input=%input_list)
  return (%x1, %x3))");

  setTestCases({
      {"a", AllocationLifetime(0, 2), false, false, {}},
      {"x0", AllocationLifetime(2, 2), true, false, {"a"}},
      {"b", AllocationLifetime(0, 3), false, true, {}},
      {"x1", AllocationLifetime(2, 3), true, false, {"b"}},
      {"c", AllocationLifetime(0, 2), false, false, {}},
      {"x2", AllocationLifetime(2, 2), true, false, {"c"}},
      {"d", AllocationLifetime(0, 3), false, true, {}},
      {"x3", AllocationLifetime(2, 3), true, false, {"d"}},
  });

  run();
}

TEST_F(AliasAnalyzerTests, TestAmbiguousSourceOfAlias) {
  setModel(R"(
    graph(%y0, %y1):
      %out_t = torch.ops.aten.matmul.default(self=%y0, other=%y1)
      %out_t2 = torch.ops.aten.matmul.default(self=%y0, other=%y1)
      %a1 = prim.VarStack(l0=%out_t, l1=%out_t2)
      %res = torch.ops.aten.slice.Tensor(self=%a1, dim=1, start=0, end=0, step=1)
  return (%res))");

  setTestCases({
      {"out_t", AllocationLifetime(1, 5), false, true, {}},
      {"out_t2", AllocationLifetime(2, 5), false, true, {}},
      {"a1", AllocationLifetime(3, 5), true, false, {"out_t", "out_t2"}},
      {"res", AllocationLifetime(4, 5), true, false, {"out_t", "out_t2"}},
  });

  run();
}