1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import ctypes
1516import unittest
1617
18+ import numpy as np
19+
1720import paddle
1821from paddle .device import xpu
1922
@@ -56,7 +59,6 @@ def test_xpu_stream_synchronize(self):
5659 e2 = paddle .device .xpu .Event ()
5760
5861 e1 .record (s )
59- print ("1111" )
6062 e1 .query ()
6163 tensor1 = paddle .to_tensor (paddle .rand ([1000 , 1000 ]))
6264 tensor2 = paddle .matmul (tensor1 , tensor1 )
@@ -78,7 +80,85 @@ def test_xpu_stream_wait_event_and_record_event(self):
7880 s2 .wait_event (e1 )
7981 s2 .synchronize ()
8082
81- self .assertTrue (e1 .query () and s1 .query () and s2 .query ())
83+ self .assertTrue (e1 .query ())
84+
85+
86+ class TestXPUEvent (unittest .TestCase ):
87+ def test_xpu_event (self ):
88+ if paddle .is_compiled_with_xpu ():
89+ e = paddle .device .xpu .Event ()
90+ self .assertIsNotNone (e )
91+ s = paddle .device .xpu .current_stream ()
92+
93+ def test_xpu_event_methods (self ):
94+ if paddle .is_compiled_with_xpu ():
95+ e = paddle .device .xpu .Event ()
96+ s = paddle .device .xpu .current_stream ()
97+ event_query_1 = e .query ()
98+ tensor1 = paddle .to_tensor (paddle .rand ([1000 , 1000 ]))
99+ tensor2 = paddle .matmul (tensor1 , tensor1 )
100+ s .record_event (e )
101+ e .synchronize ()
102+ event_query_2 = e .query ()
103+
104+ self .assertTrue (event_query_1 )
105+ self .assertTrue (event_query_2 )
106+
107+
108+ class TestStreamGuard (unittest .TestCase ):
109+ '''
110+ Note:
111+ The asynchronous execution property of XPU Stream can only be tested offline.
112+ '''
113+
114+ def test_stream_guard_normal (self ):
115+ if paddle .is_compiled_with_xpu ():
116+ s = paddle .device .Stream ()
117+ a = paddle .to_tensor (np .array ([0 , 2 , 4 ], dtype = "int32" ))
118+ b = paddle .to_tensor (np .array ([1 , 3 , 5 ], dtype = "int32" ))
119+ c = a + b
120+ with paddle .device .stream_guard (s ):
121+ d = a + b
122+ s .synchronize ()
123+
124+ np .testing .assert_array_equal (np .array (c ), np .array (d ))
125+
126+ def test_stream_guard_default_stream (self ):
127+ if paddle .is_compiled_with_xpu ():
128+ s1 = paddle .device .current_stream ()
129+ with paddle .device .stream_guard (s1 ):
130+ pass
131+ s2 = paddle .device .current_stream ()
132+
133+ self .assertTrue (id (s1 .stream_base ) == id (s2 .stream_base ))
134+
135+ def test_set_current_stream_default_stream (self ):
136+ if paddle .is_compiled_with_xpu ():
137+ cur_stream = paddle .device .current_stream ()
138+ new_stream = paddle .device .set_stream (cur_stream )
139+
140+ self .assertTrue (
141+ id (cur_stream .stream_base ) == id (new_stream .stream_base )
142+ )
143+
144+ def test_stream_guard_raise_error (self ):
145+ if paddle .is_compiled_with_xpu ():
146+
147+ def test_not_correct_stream_guard_input ():
148+ tmp = np .zeros (5 )
149+ with paddle .device .stream_guard (tmp ):
150+ pass
151+
152+ self .assertRaises (TypeError , test_not_correct_stream_guard_input )
153+
154+
155+ class TestRawStream (unittest .TestCase ):
156+ def test_xpu_stream (self ):
157+ if paddle .is_compiled_with_xpu ():
158+ xpu_stream = paddle .device .xpu .current_stream ().xpu_stream
159+ print (xpu_stream )
160+ self .assertTrue (type (xpu_stream ) is int )
161+ ptr = ctypes .c_void_p (xpu_stream )
82162
83163
84164if __name__ == "__main__" :
0 commit comments