@@ -8079,6 +8079,130 @@ def forward(self, a):
80798079 assert_almost_equal (mx_out [i ].asnumpy (), np_out [i ], rtol = 1e-3 , atol = 1e-5 )
80808080
80818081
8082+ @use_np
8083+ @pytest .mark .parametrize ('shape,index,inverse,counts' , [
8084+ ((), True , True , True ),
8085+ ((1 , ), True , True , True ),
8086+ ((5 , ), True , True , True ),
8087+ ((5 , ), True , True , True ),
8088+ ((5 , 4 ), True , True , True ),
8089+ ((5 , 0 , 4 ), True , True , True ),
8090+ ((0 , 0 , 0 ), True , True , True ),
8091+ ((5 , 3 , 4 ), True , True , True ),
8092+ ])
8093+ @pytest .mark .parametrize ('dtype' , ['float32' , 'float64' , 'int8' , 'uint8' , 'int32' , 'int64' ])
8094+ @pytest .mark .parametrize ('hybridize' , [False , True ])
8095+ def test_np_unique_all (shape , index , inverse , counts , dtype , hybridize ):
8096+ class TestUniqueAll (HybridBlock ):
8097+ def __init__ (self ):
8098+ super (TestUniqueAll , self ).__init__ ()
8099+
8100+ def forward (self , a ):
8101+ return np .unique_all (a )
8102+
8103+ test_unique = TestUniqueAll ()
8104+ if hybridize :
8105+ test_unique .hybridize ()
8106+ x = onp .random .uniform (- 8.0 , 8.0 , size = shape )
8107+ x = np .array (x , dtype = dtype )
8108+ np_out = onp .unique (x .asnumpy (), return_index = index , return_inverse = inverse , return_counts = counts )
8109+ mx_out = test_unique (x )
8110+ for i in range (len (mx_out )):
8111+ assert mx_out [i ].shape == np_out [i ].shape
8112+ assert_almost_equal (mx_out [i ].asnumpy (), np_out [i ], rtol = 1e-3 , atol = 1e-5 )
8113+
8114+ # Test imperative once again
8115+ mx_out = np .unique_all (x )
8116+ np_out = onp .unique (x .asnumpy (), return_index = index , return_inverse = inverse , return_counts = counts )
8117+ assert mx_out .values .shape == np_out [0 ].shape
8118+ assert_almost_equal (mx_out .values .asnumpy (), np_out [0 ], rtol = 1e-3 , atol = 1e-5 )
8119+ assert mx_out .indices .shape == np_out [1 ].shape
8120+ assert_almost_equal (mx_out .indices .asnumpy (), np_out [1 ], rtol = 1e-3 , atol = 1e-5 )
8121+ assert mx_out .inverse_indices .shape == np_out [2 ].shape
8122+ assert_almost_equal (mx_out .inverse_indices .asnumpy (), np_out [2 ], rtol = 1e-3 , atol = 1e-5 )
8123+ assert mx_out .counts .shape == np_out [3 ].shape
8124+ assert_almost_equal (mx_out .counts .asnumpy (), np_out [3 ], rtol = 1e-3 , atol = 1e-5 )
8125+
8126+
8127+ @use_np
8128+ @pytest .mark .parametrize ('shape,index,inverse,counts' , [
8129+ ((), False , True , False ),
8130+ ((1 , ), False , True , False ),
8131+ ((5 , ), False , True , False ),
8132+ ((5 , ), False , True , False ),
8133+ ((5 , 4 ), False , True , False ),
8134+ ((5 , 0 , 4 ), False , True , False ),
8135+ ((0 , 0 , 0 ), False , True , False ),
8136+ ((5 , 3 , 4 ), False , True , False ),
8137+ ])
8138+ @pytest .mark .parametrize ('dtype' , ['float32' , 'float64' , 'int8' , 'uint8' , 'int32' , 'int64' ])
8139+ @pytest .mark .parametrize ('hybridize' , [False , True ])
8140+ def test_np_unique_inverse (shape , index , inverse , counts , dtype , hybridize ):
8141+ class TestUniqueInverse (HybridBlock ):
8142+ def __init__ (self ):
8143+ super (TestUniqueInverse , self ).__init__ ()
8144+
8145+ def forward (self , a ):
8146+ return np .unique_inverse (a )
8147+
8148+ test_unique = TestUniqueInverse ()
8149+ if hybridize :
8150+ test_unique .hybridize ()
8151+ x = onp .random .uniform (- 8.0 , 8.0 , size = shape )
8152+ x = np .array (x , dtype = dtype )
8153+ np_out = onp .unique (x .asnumpy (), return_index = index , return_inverse = inverse , return_counts = counts )
8154+ mx_out = test_unique (x )
8155+ for i in range (len (mx_out )):
8156+ assert mx_out [i ].shape == np_out [i ].shape
8157+ assert_almost_equal (mx_out [i ].asnumpy (), np_out [i ], rtol = 1e-3 , atol = 1e-5 )
8158+
8159+ # Test imperative once again
8160+ mx_out = np .unique_inverse (x )
8161+ np_out = onp .unique (x .asnumpy (), return_index = index , return_inverse = inverse , return_counts = counts )
8162+ assert mx_out .values .shape == np_out [0 ].shape
8163+ assert_almost_equal (mx_out .values .asnumpy (), np_out [0 ], rtol = 1e-3 , atol = 1e-5 )
8164+ assert mx_out .inverse_indices .shape == np_out [1 ].shape
8165+ assert_almost_equal (mx_out .inverse_indices .asnumpy (), np_out [1 ], rtol = 1e-3 , atol = 1e-5 )
8166+
8167+
8168+ @use_np
8169+ @pytest .mark .parametrize ('shape,index,inverse,counts' , [
8170+ ((), False , False , False ),
8171+ ((1 , ), False , False , False ),
8172+ ((5 , ), False , False , False ),
8173+ ((5 , ), False , False , False ),
8174+ ((5 , 4 ), False , False , False ),
8175+ ((5 , 0 , 4 ), False , False , False ),
8176+ ((0 , 0 , 0 ), False , False , False ),
8177+ ((5 , 3 , 4 ), False , False , False ),
8178+ ])
8179+ @pytest .mark .parametrize ('dtype' , ['float32' , 'float64' , 'int8' , 'uint8' , 'int32' , 'int64' ])
8180+ @pytest .mark .parametrize ('hybridize' , [False , True ])
8181+ def test_np_unique_values (shape , index , inverse , counts , dtype , hybridize ):
8182+ class TestUniqueValues (HybridBlock ):
8183+ def __init__ (self ):
8184+ super (TestUniqueValues , self ).__init__ ()
8185+
8186+ def forward (self , a ):
8187+ return np .unique_values (a )
8188+
8189+ test_unique = TestUniqueValues ()
8190+ if hybridize :
8191+ test_unique .hybridize ()
8192+ x = onp .random .uniform (- 8.0 , 8.0 , size = shape )
8193+ x = np .array (x , dtype = dtype )
8194+ np_out = onp .unique (x .asnumpy (), return_index = index , return_inverse = inverse , return_counts = counts )
8195+ mx_out = test_unique (x )
8196+ assert mx_out .shape == np_out .shape
8197+ assert_almost_equal (mx_out .asnumpy (), np_out , rtol = 1e-3 , atol = 1e-5 )
8198+
8199+ # Test imperative once again
8200+ mx_out = np .unique_values (x )
8201+ np_out = onp .unique (x .asnumpy (), return_index = index , return_inverse = inverse , return_counts = counts )
8202+ assert mx_out .shape == np_out .shape
8203+ assert_almost_equal (mx_out .asnumpy (), np_out , rtol = 1e-3 , atol = 1e-5 )
8204+
8205+
80828206@use_np
80838207def test_np_take ():
80848208 configs = [
0 commit comments