forked from ROCm/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ir_verifier.cpp
191 lines (178 loc) · 6.56 KB
/
test_ir_verifier.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
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
183
184
185
186
187
188
189
190
191
#include <gtest/gtest.h>
#include <stdexcept>
#include "test/cpp/tensorexpr/test_base.h"
#include <torch/csrc/jit/tensorexpr/expr.h>
#include <torch/csrc/jit/tensorexpr/ir.h>
#include <torch/csrc/jit/tensorexpr/ir_verifier.h>
#include <torch/csrc/jit/tensorexpr/loopnest.h>
#include <torch/csrc/jit/tensorexpr/tensor.h>
#include <torch/csrc/jit/testing/file_check.h>
#include <sstream>
namespace torch {
namespace jit {
using namespace torch::jit::tensorexpr;
TEST(IRVerifier, BitwiseOps) {
VarPtr X = alloc<Var>("x", kInt);
VarPtr Y = alloc<Var>("y", kFloat);
{
auto a = alloc<And>(X, Y);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_ANY_THROW(verify(a));
}
{
auto a = alloc<Or>(X, Y);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_ANY_THROW(verify(a));
}
{
auto a = alloc<Xor>(X, Y);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_ANY_THROW(verify(a));
}
{
auto a = alloc<Lshift>(X, Y);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_ANY_THROW(verify(a));
}
{
auto a = alloc<Rshift>(X, Y);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_ANY_THROW(verify(a));
}
}
TEST(IRVerifier, CompareSelect) {
ExprPtr X = alloc<IntImm>(1);
ExprPtr Y = alloc<FloatImm>(3.14f);
{
auto a = alloc<CompareSelect>(X, X, X, Y, kEQ);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_ANY_THROW(verify(a));
}
{
auto a = alloc<CompareSelect>(X, Y, X, X, kEQ);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_ANY_THROW(verify(a));
}
}
TEST(IRVerifier, Ramp) {
VarPtr I = alloc<Var>("i", kInt);
VarPtr J = alloc<Var>("j", kFloat);
{
auto a = alloc<Ramp>(I, J, 4);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_ANY_THROW(verify(a));
}
}
TEST(IRVerifier, Load) {
VarPtr I = alloc<Var>("i", kInt);
VarPtr J = alloc<Var>("j", kLong);
VarPtr K = alloc<Var>("k", kFloat);
BufPtr B = alloc<Buf>(
"b",
std::vector<ExprPtr>({alloc<IntImm>(10), alloc<IntImm>(20)}),
kFloat);
{
// Indices with different int dtypes (kInt, kLong) are ok
auto a = alloc<Load>(B, std::vector<ExprPtr>({I, J}));
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_NO_THROW(verify(a));
}
{
// Float index
auto a = alloc<Load>(B, std::vector<ExprPtr>({K, K}));
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_ANY_THROW(verify(a));
}
{
// Multilanes are only allowed in flattened indices
auto multilane_index = alloc<Ramp>(I, alloc<IntImm>(1), 4);
auto a = alloc<Load>(B, std::vector<ExprPtr>({I, multilane_index}));
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_ANY_THROW(verify(a));
}
}
TEST(IRVerifier, IfThenElse) {
VarPtr I = alloc<Var>("i", kInt);
VarPtr J = alloc<Var>("j", kLong);
VarPtr K = alloc<Var>("k", kFloat);
{
// Condition must be integral
auto a = alloc<IfThenElse>(K, I, I);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_ANY_THROW(verify(a));
}
{
// Dtypes of true and false exprs must match
auto a = alloc<IfThenElse>(I, I, J);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_ANY_THROW(verify(a));
}
{
// Can't have multiple lanes in condition expr
auto a = alloc<IfThenElse>(alloc<Broadcast>(I, 4), I, I);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_ANY_THROW(verify(a));
}
}
TEST(IRVerifier, For) {
VarPtr I = alloc<Var>("i", kInt);
VarPtr J = alloc<Var>("j", kInt);
StmtPtr body = alloc<Block>(std::vector<StmtPtr>({}));
{
// Can't have nullptr as a Var
auto a = alloc<For>(nullptr, I, J, body);
// NOLINTNEXTLINE(hicpp-avoid-goto,cppcoreguidelines-avoid-goto)
EXPECT_ANY_THROW(verify(a));
}
}
TEST(IRVerifier, Block) {
VarPtr I = alloc<Var>("i", kInt);
BufPtr B = alloc<Buf>("B", std::vector<ExprPtr>({alloc<IntImm>(10)}), kInt);
{
StmtPtr store = alloc<Store>(B, std::vector<ExprPtr>({I}), I);
// NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
StmtPtr block1 = alloc<Block>(std::vector<StmtPtr>({store}));
// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
StmtPtr block2 = alloc<Block>(std::vector<StmtPtr>({store}));
// Stmt can't have multiple parents, thus inserting it into several blocks
// is illegal
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_ANY_THROW(verify(block2));
}
}
TEST(IRVerifier, Store) {
VarPtr I = alloc<Var>("i", kInt);
VarPtr J = alloc<Var>("j", kLong);
VarPtr K = alloc<Var>("k", kFloat);
BufPtr B = alloc<Buf>(
"b",
std::vector<ExprPtr>({alloc<IntImm>(10), alloc<IntImm>(20)}),
kFloat);
{
// Indices with different int dtypes (kInt, kLong) are ok
auto a = alloc<Store>(B, std::vector<ExprPtr>({I, J}), K);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_NO_THROW(verify(a));
}
{
// Float index
auto a = alloc<Store>(B, std::vector<ExprPtr>({K, K}), K);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_ANY_THROW(verify(a));
}
{
// Multilanes are only allowed in flattened indices
auto multilane_index = alloc<Ramp>(I, alloc<IntImm>(1), 4);
auto a = alloc<Store>(B, std::vector<ExprPtr>({I, multilane_index}), K);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_ANY_THROW(verify(a));
}
{
// Value and buf dtypes mismatch
auto a = alloc<Store>(B, std::vector<ExprPtr>({I}), I);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto,clang-analyzer-cplusplus.NewDeleteLeaks)
EXPECT_ANY_THROW(verify(a));
}
}
} // namespace jit
} // namespace torch