1818import numpy as np
1919import mxnet as mx
2020
21- from mxnet .test_utils import rand_ndarray , assert_almost_equal , rand_coord_2d , default_context , create_vector
21+ from mxnet .test_utils import rand_ndarray , assert_almost_equal , rand_coord_2d , create_vector
2222from mxnet import gluon , nd
2323from tests .python .unittest .common import with_seed
2424
2525# dimension constants
2626LARGE_X = 5000000000
2727MEDIUM_X = 1000000000
28- LARGE_Y = 100000
29- SMALL_Y = 1
3028
3129
3230def test_slice ():
3331 a = nd .ones (LARGE_X )
3432 res = nd .slice (a , begin = (LARGE_X - MEDIUM_X ), end = LARGE_X )
33+ assert a [0 ] == 1
3534 assert res .shape [0 ] == MEDIUM_X
3635
3736
38- def test_gluon_embedding ():
39- m = gluon .nn .Embedding (1 , LARGE_Y )
40- m .initialize ()
41- a = nd .zeros ((LARGE_Y , 1 ))
42- b = m (a )
43- assert b .shape == (LARGE_Y , 1 , LARGE_Y )
44- assert b .asnumpy ().size == LARGE_X * 2
45-
46-
4737def test_ndarray_zeros ():
4838 a = nd .zeros (shape = LARGE_X )
4939 assert a [- 1 ] == 0
@@ -73,7 +63,7 @@ def test_ndarray_random_randint():
7363 a = nd .random .randint (low_large_value , high_large_value , dtype = np .int64 )
7464 low = mx .nd .array ([low_large_value ], dtype = 'int64' )
7565 high = mx .nd .array ([high_large_value ], dtype = 'int64' )
76- assert a . __gt__ ( low ) and a . __lt__ ( high )
66+ assert a > low and a < high
7767
7868
7969def test_ndarray_empty ():
@@ -93,36 +83,22 @@ def test_elementwise():
9383
9484
9585def test_reduce ():
96- a = nd .ones (shape = (LARGE_X , SMALL_Y ))
86+ a = nd .ones (shape = (LARGE_X , 1 ))
9787 assert nd .sum (a ).asnumpy () == a .shape [0 ] * a .shape [1 ]
9888
9989
100- def test_broadcast ():
101- a = nd .ones (shape = (LARGE_X , SMALL_Y * 2 ))
102- b = nd .arange (0 , LARGE_X ).reshape (LARGE_X , 1 )
103- res = nd .broadcast_to (b , shape = (b .shape [0 ], SMALL_Y * 2 ))
104- assert np .sum (res [- 1 ].asnumpy () == LARGE_X ) == res .shape [1 ]
105- res = mx .nd .broadcast_like (b , a )
106- assert np .sum (res [- 1 ].asnumpy () == LARGE_X ) == res .shape [1 ]
107-
108-
10990def test_clip ():
110- a = nd . arange ( 0 , LARGE_X )
91+ a = create_vector ( LARGE_X )
11192 res = nd .clip (a , a_min = 100 , a_max = 1000 )
11293 assert np .sum (res [- 1 ].asnumpy () == 1000 ) == 1
11394
11495
11596def test_argmin ():
116- a = nd .arange (0 , LARGE_X )
97+ a = create_vector (LARGE_X , dtype = np .float32 )
98+ assert a [0 ] == 0
11799 idx = mx .nd .argmin (a , axis = 0 )
118- assert idx .shape [0 ] == SMALL_Y
119-
120-
121- def test_tile ():
122- a = nd .arange (0 , LARGE_X )
123- b = nd .tile (a , reps = (1 ,2 ))
124- assert b [0 ][LARGE_X ] == b [0 ][0 ]
125- assert b [0 ][LARGE_X - 1 ] == b [0 ][- 1 ]
100+ assert idx [0 ] == 0
101+ assert idx .shape [0 ] == 1
126102
127103
128104def test_take ():
@@ -169,114 +145,6 @@ def test_Dense(ctx=mx.cpu(0)):
169145 assert res .shape == (LARGE_X , 2 )
170146
171147
172- def test_pick ():
173- a = mx .nd .ones (shape = (LARGE_X , 2 ))
174- b = mx .nd .ones (shape = LARGE_X )
175- res = mx .nd .pick (a , b )
176- assert res .shape == b .shape
177-
178-
179- def test_depthtospace ():
180- def numpy_depth_to_space (x , blocksize ):
181- b , c , h , w = x .shape [0 ], x .shape [1 ], x .shape [2 ], x .shape [3 ]
182- tmp = np .reshape (x , [b , blocksize , blocksize , c // (blocksize ** 2 ), h , w ])
183- tmp = np .transpose (tmp , [0 , 3 , 4 , 1 , 5 , 2 ])
184- y = np .reshape (tmp , [b , c // (blocksize ** 2 ), h * blocksize , w * blocksize ])
185- return y
186-
187- shape_inp = (LARGE_X , 4 , 1 , 1 )
188- data = rand_ndarray (shape_inp , 'default' )
189- data_np = data .asnumpy ()
190- expected = numpy_depth_to_space (data_np , 2 )
191- output = mx .nd .depth_to_space (data , 2 )
192- assert_almost_equal (output .asnumpy (), expected , atol = 1e-3 , rtol = 1e-3 )
193-
194-
195- def test_spacetodepth ():
196- def numpy_space_to_depth (x , blocksize ):
197- b , c , h , w = x .shape [0 ], x .shape [1 ], x .shape [2 ], x .shape [3 ]
198- tmp = np .reshape (x , [b , c , h // blocksize , blocksize , w // blocksize , blocksize ])
199- tmp = np .transpose (tmp , [0 , 3 , 5 , 1 , 2 , 4 ])
200- y = np .reshape (tmp , [b , c * (blocksize ** 2 ), h // blocksize , w // blocksize ])
201- return y
202-
203- shape_inp = (LARGE_X , 1 , 2 , 2 )
204- data = rand_ndarray (shape_inp , 'default' )
205- data_np = data .asnumpy ()
206- expected = numpy_space_to_depth (data_np , 2 )
207- output = mx .nd .space_to_depth (data , 2 )
208- assert_almost_equal (output .asnumpy (), expected , atol = 1e-3 , rtol = 1e-3 )
209-
210- @with_seed ()
211- def test_diag ():
212- a_np = np .random .random ((LARGE_X , 2 )).astype (np .float32 )
213- a = mx .nd .array (a_np )
214-
215- # k == 0
216- r = mx .nd .diag (a )
217- assert_almost_equal (r .asnumpy (), np .diag (a_np ))
218-
219- # k == 1
220- k = 1
221- r = mx .nd .diag (a , k = k )
222- assert_almost_equal (r .asnumpy (), np .diag (a_np , k = k ))
223-
224- # k == -1
225- k = - 1
226- r = mx .nd .diag (a , k = k )
227- assert_almost_equal (r .asnumpy (), np .diag (a_np , k = k ))
228-
229-
230- @with_seed ()
231- def test_ravel_multi_index ():
232- x1 , y1 = rand_coord_2d ((LARGE_X - 100 ), LARGE_X , SMALL_Y , 4 )
233- x2 , y2 = rand_coord_2d ((LARGE_X - 200 ), LARGE_X , SMALL_Y , 3 )
234- x3 , y3 = rand_coord_2d ((LARGE_X - 300 ), LARGE_X , SMALL_Y , 2 )
235- indices_2d = [[x1 , x2 , x3 ], [y1 , y2 , y3 ]]
236- idx = mx .nd .ravel_multi_index (mx .nd .array (indices_2d , dtype = np .int64 ), shape = (LARGE_X , 5 ))
237- idx_numpy = np .ravel_multi_index (indices_2d , (LARGE_X , 5 ))
238- assert np .sum (1 for i in range (idx .size ) if idx [i ] == idx_numpy [i ]) == 3
239-
240-
241- @with_seed ()
242- def test_unravel_index ():
243- x1 , y1 = rand_coord_2d ((LARGE_X - 100 ), LARGE_X , SMALL_Y , 4 )
244- x2 , y2 = rand_coord_2d ((LARGE_X - 200 ), LARGE_X , SMALL_Y , 3 )
245- x3 , y3 = rand_coord_2d ((LARGE_X - 300 ), LARGE_X , SMALL_Y , 2 )
246- original_2d_indices = [[x1 , x2 , x3 ], [y1 , y2 , y3 ]]
247- idx_numpy = np .ravel_multi_index (original_2d_indices , (LARGE_X , 5 ))
248- indices_2d = mx .nd .unravel_index (mx .nd .array (idx_numpy , dtype = np .int64 ), shape = (LARGE_X , 5 ))
249- assert (indices_2d .asnumpy () == np .array (original_2d_indices )).all ()
250-
251-
252- def test_transpose ():
253- b = nd .arange (0 , LARGE_X , dtype = np .int64 ).reshape (1 , LARGE_X )
254- t = b .T
255- assert t .shape == (LARGE_X , 1 )
256- assert t [- 1 , 0 ].asnumpy () == (LARGE_X - 1 )
257-
258-
259- def test_swapaxes ():
260- b = nd .arange (0 , LARGE_X , dtype = np .int64 ).reshape (LARGE_X , 1 )
261- t = nd .swapaxes (b , dim1 = 0 , dim2 = 1 )
262- assert t .shape == (1 , LARGE_X )
263- assert t [0 , - 1 ].asnumpy () == (LARGE_X - 1 )
264-
265-
266- def test_flip ():
267- b = nd .arange (0 , LARGE_X , dtype = np .int64 ).reshape (1 , LARGE_X )
268- t = nd .flip (b , axis = 1 )
269- assert t .shape == (1 , LARGE_X )
270- assert t [- 1 , - 1 ].asnumpy () == 0
271-
272-
273- def test_softmax ():
274- input_data = nd .ones ((2 , LARGE_X ))
275- output = nd .softmax (input_data , axis = 0 )
276- assert output [0 ][0 ] == 0.5
277- assert output [- 1 ][- 1 ] == 0.5
278-
279-
280148def test_argsort ():
281149 b = create_vector (size = LARGE_X )
282150 s = nd .argsort (b , axis = 0 , is_ascend = False , dtype = np .int64 )
@@ -287,19 +155,19 @@ def test_argsort():
287155def test_sort ():
288156 b = create_vector (size = LARGE_X )
289157 s = nd .sort (b , axis = 0 , is_ascend = False )
290- assert np .sum (s [- 1 ][ SMALL_Y // 2 : SMALL_Y ] .asnumpy () == 0 ).all ()
158+ assert np .sum (s [- 1 ].asnumpy () == 0 ).all ()
291159 s = nd .sort (b , is_ascend = True )
292160 assert np .sum (s [0 ].asnumpy () == 0 ).all ()
293161
294162
295163def test_topk ():
296164 b = create_vector (size = LARGE_X )
297- k = nd .topk (b , k = 10 , axis = 0 , dtype = np .int64 )
298- assert np .sum (k .asnumpy () == (LARGE_X - 1 )) == SMALL_Y
165+ ind = nd .topk (b , k = 10 , axis = 0 , dtype = np .int64 )
166+ assert np .sum (ind .asnumpy () == (LARGE_X - 1 )) == 1
299167 ind , val = mx .nd .topk (b , k = 3 , axis = 0 , dtype = np .int64 , ret_typ = "both" , is_ascend = False )
300168 assert np .all (ind == val )
301- l = nd .topk (b , k = 1 , axis = 0 , dtype = np .int64 , ret_typ = "value" )
302- assert l .sum () == (LARGE_X - 1 )
169+ val = nd .topk (b , k = 1 , axis = 0 , dtype = np .int64 , ret_typ = "value" )
170+ assert val .sum () == (LARGE_X - 1 )
303171
304172
305173if __name__ == '__main__' :
0 commit comments