forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_memory_dag.cpp
138 lines (113 loc) · 3.71 KB
/
test_memory_dag.cpp
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
#include <gtest/gtest.h>
#include <torch/csrc/jit/ir/ir.h>
#include <torch/csrc/jit/passes/utils/memory_dag.h>
namespace torch {
namespace jit {
TEST(MemoryDAGTest, Basic) {
auto graph = std::make_shared<Graph>();
const Value* aValue = graph->addInput();
const Value* bValue = graph->addInput();
const Value* cValue = graph->addInput();
const Value* dValue = graph->addInput();
const Value* eValue = graph->addInput();
const Value* fValue = graph->addInput();
const Value* gValue = graph->addInput();
{
// a <- b <- c
// b <- d
// a <- e
// f <- e
// g is by itself
auto t = std::make_unique<MemoryDAGBuilder>();
auto a = t->makeFreshValue(aValue);
auto b = t->makeFreshValue(bValue);
auto c = t->makeFreshValue(cValue);
auto d = t->makeFreshValue(dValue);
auto e = t->makeFreshValue(eValue);
auto f = t->makeFreshValue(fValue);
auto g = t->makeFreshValue(gValue);
t->makePointerTo(b, a);
t->makePointerTo(c, b);
t->makePointerTo(d, b);
t->makePointerTo(e, a);
t->makePointerTo(e, f);
auto dag = std::move(*t).createMemoryDAG();
/**
* Test mayAlias()
*/
// Values should alias themselves
EXPECT_TRUE(dag->mayAlias(a, a));
EXPECT_TRUE(dag->mayAlias(g, g));
// Values that point to the same location should alias
EXPECT_TRUE(dag->mayAlias(a, b));
EXPECT_TRUE(dag->mayAlias(a, c));
EXPECT_TRUE(dag->mayAlias(c, d));
// e may point to a OR f
EXPECT_TRUE(dag->mayAlias(e, a));
EXPECT_TRUE(dag->mayAlias(e, f));
// But a and f don't alias
EXPECT_FALSE(dag->mayAlias(a, f));
}
{
// x(y) -> x contains y
// b(a)
// c(a)
auto t = std::make_unique<MemoryDAGBuilder>();
auto a = t->makeFreshValue(aValue);
auto b = t->makeFreshValue(bValue);
t->addToContainedElements(a, b);
auto c = t->makeFreshValue(cValue);
t->addToContainedElements(a, c);
auto dag = std::move(*t).createMemoryDAG();
EXPECT_TRUE(dag->mayContainAlias(a, b));
EXPECT_TRUE(dag->mayContainAlias(b, a));
EXPECT_TRUE(dag->mayContainAlias(a, c));
EXPECT_TRUE(dag->mayContainAlias(c, a));
EXPECT_TRUE(dag->mayContainAlias(b, c));
EXPECT_TRUE(dag->mayContainAlias(c, b));
// containers contain an element in themselves
EXPECT_TRUE(dag->mayContainAlias(b, b));
EXPECT_TRUE(dag->mayContainAlias(c, c));
EXPECT_TRUE(dag->mayContainAlias(a, a));
}
{
// b(a)
// c(a)
// d(b(a))
auto t = std::make_unique<MemoryDAGBuilder>();
auto a = t->makeFreshValue(aValue);
auto b = t->makeFreshValue(bValue);
t->addToContainedElements(a, b);
auto c = t->makeFreshValue(cValue);
t->addToContainedElements(a, c);
auto d = t->makeFreshValue(dValue);
t->addToContainedElements(b, d);
auto dag = std::move(*t).createMemoryDAG();
EXPECT_TRUE(dag->mayContainAlias(b, d));
EXPECT_TRUE(dag->mayContainAlias(d, b));
EXPECT_TRUE(dag->mayContainAlias(c, d));
EXPECT_TRUE(dag->mayContainAlias(d, c));
EXPECT_TRUE(dag->mayContainAlias(a, d));
}
{
// f(e)
auto t = std::make_unique<MemoryDAGBuilder>();
auto a = t->makeFreshValue(aValue);
auto b = t->makeFreshValue(bValue);
t->addToContainedElements(a, b);
auto c = t->makeFreshValue(cValue);
t->addToContainedElements(a, c);
auto d = t->makeFreshValue(dValue);
t->addToContainedElements(b, d);
auto f = t->makeFreshValue(aValue);
auto e = t->makeFreshValue(bValue);
t->addToContainedElements(f, e);
auto dag = std::move(*t).createMemoryDAG();
for (auto elem : {a, b, c, d}) {
EXPECT_FALSE(dag->mayContainAlias(f, elem));
EXPECT_FALSE(dag->mayContainAlias(e, elem));
}
}
}
} // namespace jit
} // namespace torch