22
22
23
23
24
24
class TestDLPack (unittest .TestCase ):
25
-
26
25
def func_test_dlpack_dygraph (self ):
27
26
paddle .disable_static ()
28
27
tensor = paddle .to_tensor (np .array ([1 , 2 , 3 , 4 ]).astype ('int' ))
29
28
dlpack = paddle .utils .dlpack .to_dlpack (tensor )
30
29
out_from_dlpack = paddle .utils .dlpack .from_dlpack (dlpack )
31
30
if paddle .fluid .framework .in_dygraph_mode ():
32
31
self .assertTrue (
33
- isinstance (out_from_dlpack , paddle .fluid .core .eager .Tensor ))
32
+ isinstance (out_from_dlpack , paddle .fluid .core .eager .Tensor )
33
+ )
34
34
else :
35
35
self .assertTrue (isinstance (out_from_dlpack , paddle .Tensor ))
36
- np .testing .assert_array_equal (np .array (out_from_dlpack ),
37
- np .array ([1 , 2 , 3 , 4 ]).astype ('int' ))
36
+ np .testing .assert_array_equal (
37
+ np .array (out_from_dlpack ), np .array ([1 , 2 , 3 , 4 ]).astype ('int' )
38
+ )
38
39
39
40
def test_dlpack_dygraph (self ):
40
41
with _test_eager_guard ():
@@ -58,26 +59,32 @@ def test_dlpack_tensor_larger_than_2dim(self):
58
59
def test_dlpack_static (self ):
59
60
paddle .enable_static ()
60
61
tensor = fluid .create_lod_tensor (
61
- np .array ([[1 ], [2 ], [3 ], [4 ]]).astype ('int' ), [[1 , 3 ]],
62
- fluid .CPUPlace ())
62
+ np .array ([[1 ], [2 ], [3 ], [4 ]]).astype ('int' ),
63
+ [[1 , 3 ]],
64
+ fluid .CPUPlace (),
65
+ )
63
66
dlpack = paddle .utils .dlpack .to_dlpack (tensor )
64
67
out_from_dlpack = paddle .utils .dlpack .from_dlpack (dlpack )
65
68
self .assertTrue (isinstance (out_from_dlpack , fluid .core .Tensor ))
66
69
np .testing .assert_array_equal (
67
70
np .array (out_from_dlpack ),
68
- np .array ([[1 ], [2 ], [3 ], [4 ]]).astype ('int' ))
71
+ np .array ([[1 ], [2 ], [3 ], [4 ]]).astype ('int' ),
72
+ )
69
73
70
74
# when build with cuda
71
75
if core .is_compiled_with_cuda ():
72
76
gtensor = fluid .create_lod_tensor (
73
- np .array ([[1 ], [2 ], [3 ], [4 ]]).astype ('int' ), [[1 , 3 ]],
74
- fluid .CUDAPlace (0 ))
77
+ np .array ([[1 ], [2 ], [3 ], [4 ]]).astype ('int' ),
78
+ [[1 , 3 ]],
79
+ fluid .CUDAPlace (0 ),
80
+ )
75
81
gdlpack = paddle .utils .dlpack .to_dlpack (gtensor )
76
82
gout_from_dlpack = paddle .utils .dlpack .from_dlpack (gdlpack )
77
83
self .assertTrue (isinstance (gout_from_dlpack , fluid .core .Tensor ))
78
84
np .testing .assert_array_equal (
79
85
np .array (gout_from_dlpack ),
80
- np .array ([[1 ], [2 ], [3 ], [4 ]]).astype ('int' ))
86
+ np .array ([[1 ], [2 ], [3 ], [4 ]]).astype ('int' ),
87
+ )
81
88
82
89
def func_test_dlpack_dtype_conversion (self ):
83
90
paddle .disable_static ()
@@ -104,7 +111,8 @@ def func_test_dlpack_dtype_conversion(self):
104
111
for dtype in complex_dtypes :
105
112
x = paddle .to_tensor (
106
113
[[1 + 6j , 2 + 5j , 3 + 4j ], [4 + 3j , 5 + 2j , 6 + 1j ]],
107
- dtype = dtype )
114
+ dtype = dtype ,
115
+ )
108
116
dlpack = paddle .utils .dlpack .to_dlpack (x )
109
117
o = paddle .utils .dlpack .from_dlpack (dlpack )
110
118
self .assertEqual (x .dtype , o .dtype )
@@ -115,12 +123,18 @@ def test_dlpack_dtype_conversion(self):
115
123
self .func_test_dlpack_dtype_conversion ()
116
124
self .func_test_dlpack_dtype_conversion ()
117
125
126
+ def test_to_dlpack_for_loop (self ):
127
+ # See Paddle issue 50120
128
+ for i in range (10 ):
129
+ x = paddle .rand ([3 , 5 ])
130
+ dlpack = paddle .utils .dlpack .to_dlpack (x )
118
131
119
- class TestRaiseError (unittest .TestCase ):
120
132
133
+ class TestRaiseError (unittest .TestCase ):
121
134
def func_test_from_dlpack_raise_type_error (self ):
122
- self .assertRaises (TypeError , paddle .utils .dlpack .from_dlpack ,
123
- np .zeros (5 ))
135
+ self .assertRaises (
136
+ TypeError , paddle .utils .dlpack .from_dlpack , np .zeros (5 )
137
+ )
124
138
125
139
def test_from_dlpack_raise_type_error (self ):
126
140
with _test_eager_guard ():
0 commit comments