forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ir_verifier.cpp
181 lines (168 loc) · 4.58 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
#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) {
KernelScope kernel_scope;
const Var* X = new Var("x", kInt);
const Var* Y = new Var("y", kFloat);
{
auto a = new And(X, Y);
EXPECT_ANY_THROW(verify(a));
}
{
auto a = new Or(X, Y);
EXPECT_ANY_THROW(verify(a));
}
{
auto a = new Xor(X, Y);
EXPECT_ANY_THROW(verify(a));
}
{
auto a = new Lshift(X, Y);
EXPECT_ANY_THROW(verify(a));
}
{
auto a = new Rshift(X, Y);
EXPECT_ANY_THROW(verify(a));
}
}
TEST(IRVerifier, CompareSelect) {
KernelScope kernel_scope;
const Expr* X = new IntImm(1);
const Expr* Y = new FloatImm(3.14f);
{
auto a = new CompareSelect(X, X, X, Y, kEQ);
EXPECT_ANY_THROW(verify(a));
}
{
auto a = new CompareSelect(X, Y, X, X, kEQ);
EXPECT_ANY_THROW(verify(a));
}
}
TEST(IRVerifier, Ramp) {
KernelScope kernel_scope;
const Var* I = new Var("i", kInt);
const Var* J = new Var("j", kFloat);
{
auto a = new Ramp(I, J, 4);
EXPECT_ANY_THROW(verify(a));
}
}
TEST(IRVerifier, Load) {
KernelScope kernel_scope;
const Var* I = new Var("i", kInt);
const Var* J = new Var("j", kLong);
const Var* K = new Var("k", kFloat);
const Buf* B = new Buf("b", {new IntImm(10), new IntImm(20)}, kFloat);
{
// Indices with different int dtypes (kInt, kLong) are ok
auto a = new Load(B, {I, J}, new IntImm(1));
EXPECT_NO_THROW(verify(a));
}
{
// Float index
auto a = new Load(B, {K, K}, new IntImm(1));
EXPECT_ANY_THROW(verify(a));
}
{
// Multilanes are only allowed in flattened indices
auto multilane_index = new Ramp(I, new IntImm(1), 4);
auto a = new Load(B, {I, multilane_index}, new IntImm(1));
EXPECT_ANY_THROW(verify(a));
}
{
// Lane number mismatch in indices and mask
auto a = new Load(B, {I}, new Broadcast(new IntImm(1), 4));
EXPECT_ANY_THROW(verify(a));
}
}
TEST(IRVerifier, IfThenElse) {
KernelScope kernel_scope;
const Var* I = new Var("i", kInt);
const Var* J = new Var("j", kLong);
const Var* K = new Var("k", kFloat);
{
// Condition must be integral
auto a = new IfThenElse(K, I, I);
EXPECT_ANY_THROW(verify(a));
}
{
// Dtypes of true and false exprs must match
auto a = new IfThenElse(I, I, J);
EXPECT_ANY_THROW(verify(a));
}
{
// Can't have multiple lanes in condition expr
auto a = new IfThenElse(new Broadcast(I, 4), I, I);
EXPECT_ANY_THROW(verify(a));
}
}
TEST(IRVerifier, For) {
KernelScope kernel_scope;
const Var* I = new Var("i", kInt);
const Var* J = new Var("j", kInt);
Stmt* body = new Block({});
{
// Can't have nullptr as a Var
auto a = new For(nullptr, I, J, body);
EXPECT_ANY_THROW(verify(a));
}
}
TEST(IRVerifier, Block) {
KernelScope kernel_scope;
const Var* I = new Var("i", kInt);
const Buf* B = new Buf("B", {new IntImm(10)}, kInt);
{
Stmt* store = new Store(B, {I}, I, new IntImm(1));
Stmt* block1 = new Block({store});
Stmt* block2 = new Block({store});
// Stmt can't have multiple parrents, thus inserting it into several blocks
// is illegal
EXPECT_ANY_THROW(verify(block2));
}
}
TEST(IRVerifier, Store) {
KernelScope kernel_scope;
const Var* I = new Var("i", kInt);
const Var* J = new Var("j", kLong);
const Var* K = new Var("k", kFloat);
const Buf* B = new Buf("b", {new IntImm(10), new IntImm(20)}, kFloat);
{
// Indices with different int dtypes (kInt, kLong) are ok
auto a = new Store(B, {I, J}, K, new IntImm(1));
EXPECT_NO_THROW(verify(a));
}
{
// Float index
auto a = new Store(B, {K, K}, K, new IntImm(1));
EXPECT_ANY_THROW(verify(a));
}
{
// Multilanes are only allowed in flattened indices
auto multilane_index = new Ramp(I, new IntImm(1), 4);
auto a = new Store(B, {I, multilane_index}, K, new IntImm(1));
EXPECT_ANY_THROW(verify(a));
}
{
// Lane number mismatch in indices and mask
auto a = new Store(B, {I}, K, new Broadcast(new IntImm(1), 4));
EXPECT_ANY_THROW(verify(a));
}
{
// Value and buf dtypes mismatch
auto a = new Store(B, {I}, I, new Broadcast(new IntImm(1), 4));
EXPECT_ANY_THROW(verify(a));
}
}
} // namespace jit
} // namespace torch