15
15
from __future__ import print_function
16
16
17
17
import unittest
18
- from tests .op_test import OpTest
19
18
20
19
import numpy as np
21
- import paddle .base as base
22
20
import paddle
21
+ import paddle .base as base
23
22
from paddle .base import Program , program_guard
23
+ from tests .op_test import OpTest
24
+
25
+ with paddle .pir_utils .OldIrGuard ():
26
+ from paddle .base import program_guard as old_program_guard , Program as OldProgram
24
27
25
28
26
29
def create_test_class (op_type , typename , callback ):
27
30
class Cls (OpTest ):
28
31
def setUp (self ):
29
- self .set_mlu ()
30
- self .place = paddle .CustomPlace ("mlu" , 0 )
31
- x = np .random .random (size = (10 , 7 )).astype (typename )
32
- y = np .random .random (size = (10 , 7 )).astype (typename )
33
- out = callback (x , y )
32
+ self .device = "mlu:0"
33
+ self .op_type = op_type
34
+ self .python_api = eval ("paddle." + op_type )
35
+ self .set_device ()
36
+
37
+ def init_input_output (self , shape1 , shape2 ):
38
+ x = np .random .random (size = shape1 ).astype (typename )
39
+ y = np .random .random (size = shape2 ).astype (typename )
34
40
self .inputs = {"X" : x , "Y" : y }
41
+ out = callback (x , y )
35
42
self .outputs = {"Out" : out }
36
- self .op_type = op_type
37
43
38
- def set_mlu (self ):
44
+ def set_device (self ):
39
45
self .__class__ .use_custom_device = True
46
+ self .place = paddle .CustomPlace (
47
+ self .device .split (":" )[0 ], int (self .device .split (":" )[1 ])
48
+ )
49
+ paddle .set_device (self .device )
40
50
41
51
def test_output (self ):
52
+ self .init_input_output ((10 , 7 ), (10 , 7 ))
53
+ self .check_output_with_place (place = self .place )
54
+
55
+ def test_output1 (self ):
56
+ self .init_input_output ((8192 ,), (8192 ,))
57
+ self .check_output_with_place (place = self .place )
58
+
59
+ def test_output2 (self ):
60
+ self .init_input_output ((2 , 4096 , 1 ), (2 , 4096 , 1 ))
42
61
self .check_output_with_place (place = self .place )
43
62
44
63
def test_errors (self ):
45
64
paddle .enable_static ()
46
- with program_guard (Program (), Program ()):
47
- a = paddle .static .data (name = "a" , shape = [- 1 , 2 ], dtype = "float32" )
48
- b = paddle .static .data (name = "b" , shape = [- 1 , 2 ], dtype = "float32" )
49
- c = paddle .static .data (name = "c" , shape = [- 1 , 2 ], dtype = "int16" )
50
- d = base .create_lod_tensor (np .array ([[- 1 ]]), [[1 ]], self .place )
51
-
52
- op = eval ("paddle.%s" % self .op_type )
53
- self .assertRaises (TypeError , op , x = a , y = b , axis = True )
54
- self .assertRaises (TypeError , op , x = a , y = b , force_cpu = 1 )
55
- self .assertRaises (TypeError , op , x = a , y = b , cond = 1 )
56
- self .assertRaises (TypeError , op , x = a , y = c )
57
- self .assertRaises (TypeError , op , x = c , y = a )
58
- self .assertRaises (TypeError , op , x = a , y = d )
59
- self .assertRaises (TypeError , op , x = d , y = a )
60
- self .assertRaises (TypeError , op , x = c , y = d )
65
+ op = eval ("paddle." + self .op_type )
66
+
67
+ # TODO(LittleHeroZZZX): Remove after CI switched to PIR
68
+ if not paddle .get_flags (["FLAGS_enable_pir_api" ])["FLAGS_enable_pir_api" ]:
69
+ cases = [
70
+ {"x" : "a" , "y" : "b" , "args" : {"axis" : True }},
71
+ {"x" : "a" , "y" : "b" , "args" : {"force_cpu" : 1 }},
72
+ {"x" : "a" , "y" : "b" , "args" : {"cond" : 1 }},
73
+ {"x" : "a" , "y" : "c" , "args" : {}},
74
+ {"x" : "c" , "y" : "a" , "args" : {}},
75
+ {"x" : "a" , "y" : "d" , "args" : {}},
76
+ {"x" : "d" , "y" : "a" , "args" : {}},
77
+ {"x" : "c" , "y" : "d" , "args" : {}},
78
+ ]
79
+
80
+ def build_op (case ):
81
+ with old_program_guard (OldProgram (), OldProgram ()):
82
+ a = paddle .static .data (name = "a" , shape = [- 1 , 2 ], dtype = "float32" )
83
+ b = paddle .static .data (name = "b" , shape = [- 1 , 2 ], dtype = "float32" )
84
+ c = paddle .static .data (name = "c" , shape = [- 1 , 2 ], dtype = "int16" )
85
+ d = base .create_lod_tensor (np .array ([[- 1 ]]), [[1 ]], self .place )
86
+ inputs = {"a" : a , "b" : b , "c" : c , "d" : d }
87
+
88
+ op (x = inputs [case ["x" ]], y = inputs [case ["y" ]], ** case ["args" ])
89
+ exe = paddle .static .Executor (self .place )
90
+
91
+ exe .run (paddle .static .default_startup_program ())
92
+ exe .run (paddle .static .default_main_program ())
93
+
94
+ for case in cases :
95
+ self .assertRaises (TypeError , build_op , case )
96
+ else :
97
+ with program_guard (Program (), Program ()):
98
+ a = paddle .static .data (name = "a" , shape = [- 1 , 2 ], dtype = "float32" )
99
+ b = paddle .static .data (name = "b" , shape = [- 1 , 2 ], dtype = "float32" )
100
+ c = paddle .static .data (name = "c" , shape = [- 1 , 2 ], dtype = "int16" )
101
+ d = base .create_lod_tensor (np .array ([[- 1 ]]), [[1 ]], self .place )
102
+
103
+ self .assertRaises (TypeError , op , x = a , y = b , axis = True )
104
+ self .assertRaises (TypeError , op , x = a , y = b , force_cpu = 1 )
105
+ self .assertRaises (TypeError , op , x = a , y = b , cond = 1 )
106
+ self .assertRaises (TypeError , op , x = a , y = c )
107
+ self .assertRaises (TypeError , op , x = c , y = a )
108
+ self .assertRaises (TypeError , op , x = a , y = d )
109
+ self .assertRaises (TypeError , op , x = d , y = a )
110
+ self .assertRaises (TypeError , op , x = c , y = d )
111
+ paddle .disable_static ()
61
112
62
113
def test_dynamic_api (self ):
63
114
paddle .disable_static ()
64
- paddle .set_device ("mlu" )
65
115
x = np .random .random (size = (10 , 7 )).astype (typename )
66
116
y = np .random .random (size = (10 , 7 )).astype (typename )
67
117
real_result = callback (x , y )
@@ -71,9 +121,22 @@ def test_dynamic_api(self):
71
121
out = op (x , y )
72
122
self .assertEqual ((out .numpy () == real_result ).all (), True )
73
123
124
+ def test_dynamic_api_different_type (self ):
125
+ if op_type != "equal" :
126
+ return
127
+ paddle .disable_static ()
128
+ y = np .random .random (size = (10 , 7 )).astype ("int32" )
129
+ x = np .random .random (size = (10 , 7 )).astype (typename )
130
+ real_result = callback (x , y )
131
+ x = paddle .to_tensor (x , dtype = typename ).cast ("float32" )
132
+ y = paddle .to_tensor (y , dtype = "float32" )
133
+ op = eval ("paddle.%s" % (self .op_type ))
134
+ out = op (x , y )
135
+
136
+ self .assertEqual ((out .numpy () == real_result ).all (), True )
137
+
74
138
def test_broadcast_api_1 (self ):
75
139
paddle .enable_static ()
76
- paddle .set_device ("mlu" )
77
140
with program_guard (Program (), Program ()):
78
141
x = paddle .static .data (name = "x" , shape = [1 , 2 , 1 , 3 ], dtype = typename )
79
142
y = paddle .static .data (name = "y" , shape = [1 , 2 , 3 ], dtype = typename )
@@ -88,7 +151,6 @@ def test_broadcast_api_1(self):
88
151
89
152
def test_broadcast_api_2 (self ):
90
153
paddle .enable_static ()
91
- paddle .set_device ("mlu" )
92
154
with program_guard (Program (), Program ()):
93
155
x = paddle .static .data (name = "x" , shape = [1 , 2 , 3 ], dtype = typename )
94
156
y = paddle .static .data (name = "y" , shape = [1 , 2 , 1 , 3 ], dtype = typename )
@@ -103,7 +165,6 @@ def test_broadcast_api_2(self):
103
165
104
166
def test_broadcast_api_3 (self ):
105
167
paddle .enable_static ()
106
- paddle .set_device ("mlu" )
107
168
with program_guard (Program (), Program ()):
108
169
x = paddle .static .data (name = "x" , shape = [5 ], dtype = typename )
109
170
y = paddle .static .data (name = "y" , shape = [3 , 1 ], dtype = typename )
@@ -118,20 +179,20 @@ def test_broadcast_api_3(self):
118
179
119
180
def test_attr_name (self ):
120
181
paddle .enable_static ()
121
- paddle .set_device ( "mlu" )
122
- with program_guard ( Program (), Program ()):
123
- x = paddle .static .data (name = "x" , shape = [- 1 , 4 ], dtype = typename )
124
- y = paddle .static .data (name = "y" , shape = [- 1 , 4 ], dtype = typename )
125
- op = eval ("paddle.%s" % (self .op_type ))
126
- out = op (x = x , y = y , name = "name_%s" % (self .op_type ))
127
- self .assertEqual ("name_%s" % (self .op_type ) in out .name , True )
182
+ with paddle .pir_utils . OldIrGuard ():
183
+ with old_program_guard ( OldProgram (), OldProgram ()):
184
+ x = paddle .static .data (name = "x" , shape = [- 1 , 4 ], dtype = typename )
185
+ y = paddle .static .data (name = "y" , shape = [- 1 , 4 ], dtype = typename )
186
+ op = eval ("paddle.%s" % (self .op_type ))
187
+ out = op (x = x , y = y , name = "name_%s" % (self .op_type ))
188
+ self .assertEqual ("name_%s" % (self .op_type ) in out .name , True )
128
189
129
190
cls_name = "{0}_{1}" .format (op_type , typename )
130
191
Cls .__name__ = cls_name
131
192
globals ()[cls_name ] = Cls
132
193
133
194
134
- for _type_name in {"float16" , "float32" , "int32" , "bool " , "int64 " }:
195
+ for _type_name in {"float16" , "float32" , "int32" , "int64 " , "bool " }:
135
196
create_test_class ("equal" , _type_name , lambda _a , _b : _a == _b )
136
197
create_test_class ("not_equal" , _type_name , lambda _a , _b : _a != _b )
137
198
create_test_class ("less_than" , _type_name , lambda _a , _b : _a < _b )
0 commit comments