-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_operator_overloading.py
215 lines (192 loc) · 5.39 KB
/
test_operator_overloading.py
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from textwrap import dedent
import pytest
import mlir.extras.types as T
from mlir.extras.dialects.ext.arith import constant, Scalar
from mlir.extras.dialects.ext.tensor import Tensor, empty
# noinspection PyUnresolvedReferences
from mlir.extras.testing import mlir_ctx as ctx, filecheck, MLIRContext
# needed since the fix isn't defined here nor conftest.py
pytest.mark.usefixtures("ctx")
def test_arithmetic(ctx: MLIRContext):
one = constant(1)
two = constant(2)
one + two
one - two
one / two
one // two
one % two
one = constant(1.0)
two = constant(2.0)
one + two
one - two
one / two
try:
one // two
except ValueError as e:
assert (
str(e)
== "floordiv not supported for lhs=Scalar(%cst = arith.constant 1.000000e+00 : f32)"
)
one % two
ctx.module.operation.verify()
filecheck(
dedent(
"""\
module {
%c1_i32 = arith.constant 1 : i32
%c2_i32 = arith.constant 2 : i32
%0 = arith.addi %c1_i32, %c2_i32 : i32
%1 = arith.subi %c1_i32, %c2_i32 : i32
%2 = arith.divsi %c1_i32, %c2_i32 : i32
%3 = arith.floordivsi %c1_i32, %c2_i32 : i32
%4 = arith.remsi %c1_i32, %c2_i32 : i32
%cst = arith.constant 1.000000e+00 : f32
%cst_0 = arith.constant 2.000000e+00 : f32
%5 = arith.addf %cst, %cst_0 : f32
%6 = arith.subf %cst, %cst_0 : f32
%7 = arith.divf %cst, %cst_0 : f32
%8 = arith.remf %cst, %cst_0 : f32
}
"""
),
ctx.module,
)
def test_tensor_arithmetic(ctx: MLIRContext):
one = constant(1)
assert isinstance(one, Scalar)
two = constant(2)
assert isinstance(two, Scalar)
three = one + two
assert isinstance(three, Scalar)
ten1 = empty(10, 10, 10, T.f32())
assert isinstance(ten1, Tensor)
ten2 = empty(10, 10, 10, T.f32())
assert isinstance(ten2, Tensor)
ten3 = ten1 + ten2
assert isinstance(ten3, Tensor)
ctx.module.operation.verify()
filecheck(
dedent(
"""\
module {
%c1_i32 = arith.constant 1 : i32
%c2_i32 = arith.constant 2 : i32
%0 = arith.addi %c1_i32, %c2_i32 : i32
%1 = tensor.empty() : tensor<10x10x10xf32>
%2 = tensor.empty() : tensor<10x10x10xf32>
%3 = arith.addf %1, %2 : tensor<10x10x10xf32>
}
"""
),
ctx.module,
)
def test_r_arithmetic(ctx: MLIRContext):
one = constant(1)
two = constant(2)
one - two
two - one
ctx.module.operation.verify()
filecheck(
dedent(
"""\
module {
%c1_i32 = arith.constant 1 : i32
%c2_i32 = arith.constant 2 : i32
%0 = arith.subi %c1_i32, %c2_i32 : i32
%1 = arith.subi %c2_i32, %c1_i32 : i32
}
"""
),
ctx.module,
)
def test_arith_cmp(ctx: MLIRContext):
one = constant(1)
two = constant(2)
one < two
one <= two
one > two
one >= two
one == two
one != two
one & two
one | two
assert one._ne(two)
assert not one._eq(two)
one = constant(1.0)
two = constant(2.0)
one < two
one <= two
one > two
one >= two
one == two
one != two
assert one._ne(two)
assert not one._eq(two)
ctx.module.operation.verify()
filecheck(
dedent(
"""\
module {
%c1_i32 = arith.constant 1 : i32
%c2_i32 = arith.constant 2 : i32
%0 = arith.cmpi ult, %c1_i32, %c2_i32 : i32
%1 = arith.cmpi ule, %c1_i32, %c2_i32 : i32
%2 = arith.cmpi ugt, %c1_i32, %c2_i32 : i32
%3 = arith.cmpi uge, %c1_i32, %c2_i32 : i32
%4 = arith.cmpi eq, %c1_i32, %c2_i32 : i32
%5 = arith.cmpi ne, %c1_i32, %c2_i32 : i32
%6 = arith.andi %c1_i32, %c2_i32 : i32
%7 = arith.ori %c1_i32, %c2_i32 : i32
%cst = arith.constant 1.000000e+00 : f32
%cst_0 = arith.constant 2.000000e+00 : f32
%8 = arith.cmpf olt, %cst, %cst_0 : f32
%9 = arith.cmpf ole, %cst, %cst_0 : f32
%10 = arith.cmpf ogt, %cst, %cst_0 : f32
%11 = arith.cmpf oge, %cst, %cst_0 : f32
%12 = arith.cmpf oeq, %cst, %cst_0 : f32
%13 = arith.cmpf one, %cst, %cst_0 : f32
}
"""
),
ctx.module,
)
def test_scalar_promotion(ctx: MLIRContext):
one = constant(1)
one + 2
one - 2
one / 2
one // 2
one % 2
one = constant(1.0)
one + 2.0
one - 2.0
one / 2.0
one % 2.0
ctx.module.operation.verify()
correct = dedent(
"""\
module {
%c1_i32 = arith.constant 1 : i32
%c2_i32 = arith.constant 2 : i32
%0 = arith.addi %c1_i32, %c2_i32 : i32
%c2_i32_0 = arith.constant 2 : i32
%1 = arith.subi %c1_i32, %c2_i32_0 : i32
%c2_i32_1 = arith.constant 2 : i32
%2 = arith.divsi %c1_i32, %c2_i32_1 : i32
%c2_i32_2 = arith.constant 2 : i32
%3 = arith.floordivsi %c1_i32, %c2_i32_2 : i32
%c2_i32_3 = arith.constant 2 : i32
%4 = arith.remsi %c1_i32, %c2_i32_3 : i32
%cst = arith.constant 1.000000e+00 : f32
%cst_4 = arith.constant 2.000000e+00 : f32
%5 = arith.addf %cst, %cst_4 : f32
%cst_5 = arith.constant 2.000000e+00 : f32
%6 = arith.subf %cst, %cst_5 : f32
%cst_6 = arith.constant 2.000000e+00 : f32
%7 = arith.divf %cst, %cst_6 : f32
%cst_7 = arith.constant 2.000000e+00 : f32
%8 = arith.remf %cst, %cst_7 : f32
}
"""
)
filecheck(correct, ctx.module)