1
1
# Copyright Allo authors. All Rights Reserved.
2
2
# SPDX-License-Identifier: Apache-2.0
3
3
4
+ import os
4
5
import allo
5
6
from allo .ir .types import int32 , float32 , bfloat16
6
7
import allo .dataflow as df
@@ -18,12 +19,20 @@ def top():
18
19
def core (A : Ty [M ], B : Ty [M ]):
19
20
B [:] = allo .add (A , 1 )
20
21
21
- mod = df .build (top , target = "aie" )
22
22
A = np .random .randint (0 , 100 , M ).astype (np .int32 )
23
+ if "MLIR_AIE_INSTALL_DIR" in os .environ :
24
+ mod = df .build (top , target = "aie" )
25
+ B = np .zeros (M ).astype (np .int32 )
26
+ mod (A , B )
27
+ np .testing .assert_allclose (B , A + 1 )
28
+ print ("PASSED!" )
29
+ else :
30
+ print ("MLIR_AIE_INSTALL_DIR unset. Skipping AIE backend test." )
31
+ sim_mod = df .build (top , target = "simulator" )
23
32
B = np .zeros (M ).astype (np .int32 )
24
- mod (A , B )
33
+ sim_mod (A , B )
25
34
np .testing .assert_allclose (B , A + 1 )
26
- print ("PASSED !" )
35
+ print ("Dataflow Simulator Passed !" )
27
36
28
37
29
38
def _test_vector_scalar_mul ():
@@ -37,12 +46,20 @@ def top():
37
46
def core (A : Ty [M ], B : Ty [M ]):
38
47
B [:] = allo .mul (A , 2 )
39
48
40
- mod = df .build (top , target = "aie" )
41
49
A = np .random .random (M ).astype (np .float32 )
50
+ if "MLIR_AIE_INSTALL_DIR" in os .environ :
51
+ mod = df .build (top , target = "aie" )
52
+ B = np .zeros (M ).astype (np .float32 )
53
+ mod (A , B )
54
+ np .testing .assert_allclose (B , A * 2 , rtol = 1e-5 )
55
+ print ("PASSED!" )
56
+ else :
57
+ print ("MLIR_AIE_INSTALL_DIR unset. Skipping AIE backend test." )
58
+ sim_mod = df .build (top , target = "simulator" )
42
59
B = np .zeros (M ).astype (np .float32 )
43
- mod (A , B )
60
+ sim_mod (A , B )
44
61
np .testing .assert_allclose (B , A * 2 , rtol = 1e-5 )
45
- print ("PASSED !" )
62
+ print ("Dataflow Simulator Passed !" )
46
63
47
64
48
65
def _test_vector_vector_add ():
@@ -56,13 +73,21 @@ def top():
56
73
def core (A : Ty [M ], B : Ty [M ], C : Ty [M ]):
57
74
C [:] = allo .add (A , B )
58
75
59
- mod = df .build (top , target = "aie" )
60
76
A = np .random .randint (0 , 100 , M ).astype (np .int32 )
61
77
B = np .random .randint (0 , 100 , M ).astype (np .int32 )
78
+ if "MLIR_AIE_INSTALL_DIR" in os .environ :
79
+ mod = df .build (top , target = "aie" )
80
+ C = np .zeros (M ).astype (np .int32 )
81
+ mod (A , B , C )
82
+ np .testing .assert_allclose (C , A + B )
83
+ print ("PASSED!" )
84
+ else :
85
+ print ("MLIR_AIE_INSTALL_DIR unset. Skipping AIE backend test." )
86
+ sim_mod = df .build (top , target = "simulator" )
62
87
C = np .zeros (M ).astype (np .int32 )
63
- mod (A , B , C )
88
+ sim_mod (A , B , C )
64
89
np .testing .assert_allclose (C , A + B )
65
- print ("PASSED !" )
90
+ print ("Dataflow Simulator Passed !" )
66
91
67
92
68
93
def _test_vector_vector_bf16_add ():
@@ -79,13 +104,23 @@ def core(A: Ty[M], B: Ty[M], C: Ty[M]):
79
104
80
105
A = np .random .random (M ).astype (np_bfloat16 )
81
106
B = np .random .random (M ).astype (np_bfloat16 )
82
- mod = df .build (top , target = "aie" )
107
+ if "MLIR_AIE_INSTALL_DIR" in os .environ :
108
+ mod = df .build (top , target = "aie" )
109
+ C = np .zeros (M ).astype (np_bfloat16 )
110
+ mod (A , B , C )
111
+ np .testing .assert_allclose (
112
+ C .astype (np .float32 ), (A + B ).astype (np .float32 ), rtol = 1e-2
113
+ )
114
+ print ("PASSED!" )
115
+ else :
116
+ print ("MLIR_AIE_INSTALL_DIR unset. Skipping AIE backend test." )
117
+ sim_mod = df .build (top , target = "simulator" )
83
118
C = np .zeros (M ).astype (np_bfloat16 )
84
- mod (A , B , C )
119
+ sim_mod (A , B , C )
85
120
np .testing .assert_allclose (
86
121
C .astype (np .float32 ), (A + B ).astype (np .float32 ), rtol = 1e-2
87
122
)
88
- print ("PASSED !" )
123
+ print ("Dataflow Simulator Passed !" )
89
124
90
125
91
126
def _test_vector_vector_mul ():
@@ -99,13 +134,23 @@ def top():
99
134
def core (A : Ty [M ], B : Ty [M ], C : Ty [M ]):
100
135
C [:] = allo .mul (A , B )
101
136
102
- mod = df .build (top , target = "aie" )
103
137
A = np .random .random (M ).astype (np .float32 )
104
138
B = np .random .random (M ).astype (np .float32 )
139
+
140
+ if "MLIR_AIE_INSTALL_DIR" in os .environ :
141
+ mod = df .build (top , target = "aie" )
142
+ C = np .zeros (M ).astype (np .float32 )
143
+ mod (A , B , C )
144
+ np .testing .assert_allclose (C , A * B , rtol = 1e-5 )
145
+ print ("PASSED!" )
146
+ else :
147
+ print ("MLIR_AIE_INSTALL_DIR unset. Skipping AIE backend test." )
148
+
149
+ sim_mod = df .build (top , target = "simulator" )
105
150
C = np .zeros (M ).astype (np .float32 )
106
- mod (A , B , C )
107
- np .testing .assert_allclose (C , A * B , rtol = 1e-5 )
108
- print ("PASSED !" )
151
+ sim_mod (A , B , C )
152
+ np .testing .assert_allclose (C , A * B , atol = 1e-5 )
153
+ print ("Dataflow Simulator Passed !" )
109
154
110
155
111
156
if __name__ == "__main__" :
0 commit comments