8
8
assert_allclose ,
9
9
assert_almost_equal ,
10
10
assert_array_equal ,
11
+ assert_equal ,
11
12
assert_raises ,
12
13
assert_raises_regex ,
13
14
suppress_warnings ,
24
25
has_support_aspect64 ,
25
26
is_cpu_device ,
26
27
is_cuda_device ,
28
+ numpy_version ,
27
29
)
28
30
from .third_party .cupy import testing
29
31
@@ -2074,9 +2076,6 @@ def test_matrix_transpose():
2074
2076
2075
2077
2076
2078
class TestNorm :
2077
- def setup_method (self ):
2078
- numpy .random .seed (42 )
2079
-
2080
2079
@pytest .mark .usefixtures ("suppress_divide_numpy_warnings" )
2081
2080
@pytest .mark .parametrize (
2082
2081
"shape" , [(0 ,), (5 , 0 ), (2 , 0 , 3 )], ids = ["(0,)" , "(5, 0)" , "(2, 0, 3)" ]
@@ -2087,29 +2086,36 @@ def setup_method(self):
2087
2086
def test_empty (self , shape , ord , axis , keepdims ):
2088
2087
a = numpy .empty (shape )
2089
2088
ia = dpnp .array (a )
2089
+ kwarg = {"ord" : ord , "axis" : axis , "keepdims" : keepdims }
2090
+
2090
2091
if axis is None and a .ndim > 1 and ord in [0 , 3 ]:
2091
2092
# Invalid norm order for matrices (a.ndim == 2) or
2092
2093
# Improper number of dimensions to norm (a.ndim>2)
2093
- with pytest . raises (ValueError ):
2094
- dpnp .linalg .norm ( ia , ord = ord , axis = axis , keepdims = keepdims )
2094
+ assert_raises (ValueError , dpnp . linalg . norm , ia , ** kwarg )
2095
+ assert_raises ( ValueError , numpy .linalg .norm , a , ** kwarg )
2095
2096
elif axis is None and a .ndim > 2 and ord is not None :
2096
2097
# Improper number of dimensions to norm
2097
- with pytest . raises (ValueError ):
2098
- dpnp .linalg .norm ( ia , ord = ord , axis = axis , keepdims = keepdims )
2098
+ assert_raises (ValueError , dpnp . linalg . norm , ia , ** kwarg )
2099
+ assert_raises ( ValueError , numpy .linalg .norm , a , ** kwarg )
2099
2100
elif (
2100
2101
axis is None
2101
2102
and ord is not None
2102
2103
and a .ndim != 1
2103
2104
and a .shape [- 1 ] == 0
2104
2105
):
2105
- # reduction cannot be performed over zero-size axes
2106
- with pytest .raises (ValueError ):
2107
- dpnp .linalg .norm (ia , ord = ord , axis = axis , keepdims = keepdims )
2106
+ if ord in [- 2 , - 1 , 0 , 3 ]:
2107
+ # reduction cannot be performed over zero-size axes
2108
+ assert_raises (ValueError , dpnp .linalg .norm , ia , ** kwarg )
2109
+ assert_raises (ValueError , numpy .linalg .norm , a , ** kwarg )
2110
+ else :
2111
+ # TODO: when similar changes in numpy are available, instead
2112
+ # of assert_equal with zero, we should compare with numpy
2113
+ # ord in [None, 1, 2]
2114
+ assert_equal (dpnp .linalg .norm (ia , ** kwarg ), 0 )
2115
+ assert_raises (ValueError , numpy .linalg .norm , a , ** kwarg )
2108
2116
else :
2109
- result = dpnp .linalg .norm (ia , ord = ord , axis = axis , keepdims = keepdims )
2110
- expected = numpy .linalg .norm (
2111
- a , ord = ord , axis = axis , keepdims = keepdims
2112
- )
2117
+ result = dpnp .linalg .norm (ia , ** kwarg )
2118
+ expected = numpy .linalg .norm (a , ** kwarg )
2113
2119
assert_dtype_allclose (result , expected )
2114
2120
2115
2121
@pytest .mark .parametrize (
@@ -2121,11 +2127,11 @@ def test_0D(self, ord, axis):
2121
2127
ia = dpnp .array (a )
2122
2128
if axis is None and ord is not None :
2123
2129
# Improper number of dimensions to norm
2124
- with pytest . raises (ValueError ):
2125
- dpnp .linalg .norm ( ia , ord = ord , axis = axis )
2130
+ assert_raises (ValueError , dpnp . linalg . norm , ia , ord = ord , axis = axis )
2131
+ assert_raises ( ValueError , numpy .linalg .norm , a , ord = ord , axis = axis )
2126
2132
elif axis is not None :
2127
- with pytest . raises ( AxisError ):
2128
- dpnp .linalg .norm ( ia , ord = ord , axis = axis )
2133
+ assert_raises ( IndexError , dpnp . linalg . norm , ia , ord = ord , axis = axis )
2134
+ assert_raises ( AxisError , numpy .linalg .norm , a , ord = ord , axis = axis )
2129
2135
else :
2130
2136
result = dpnp .linalg .norm (ia , ord = ord , axis = axis )
2131
2137
expected = numpy .linalg .norm (a , ord = ord , axis = axis )
@@ -2158,24 +2164,21 @@ def test_1D(self, dtype, ord, axis, keepdims):
2158
2164
def test_2D (self , dtype , ord , axis , keepdims ):
2159
2165
a = generate_random_numpy_array ((3 , 5 ), dtype )
2160
2166
ia = dpnp .array (a )
2167
+ kwarg = {"ord" : ord , "axis" : axis , "keepdims" : keepdims }
2168
+
2161
2169
if (axis in [- 1 , 0 , 1 ] and ord in ["nuc" , "fro" ]) or (
2162
2170
(isinstance (axis , tuple ) or axis is None ) and ord == 3
2163
2171
):
2164
2172
# Invalid norm order for vectors
2165
- with pytest . raises (ValueError ):
2166
- dpnp .linalg .norm ( ia , ord = ord , axis = axis , keepdims = keepdims )
2173
+ assert_raises (ValueError , dpnp . linalg . norm , ia , ** kwarg )
2174
+ assert_raises ( ValueError , numpy .linalg .norm , a , ** kwarg )
2167
2175
else :
2168
- result = dpnp .linalg .norm (ia , ord = ord , axis = axis , keepdims = keepdims )
2169
- expected = numpy .linalg .norm (
2170
- a , ord = ord , axis = axis , keepdims = keepdims
2171
- )
2176
+ result = dpnp .linalg .norm (ia , ** kwarg )
2177
+ expected = numpy .linalg .norm (a , ** kwarg )
2172
2178
assert_dtype_allclose (result , expected )
2173
2179
2174
2180
@pytest .mark .usefixtures ("suppress_divide_numpy_warnings" )
2175
- @pytest .mark .parametrize (
2176
- "dtype" ,
2177
- get_all_dtypes (no_none = True ),
2178
- )
2181
+ @pytest .mark .parametrize ("dtype" , get_all_dtypes (no_none = True ))
2179
2182
@pytest .mark .parametrize (
2180
2183
"ord" , [None , - dpnp .inf , - 2 , - 1 , 1 , 2 , 3 , dpnp .inf , "fro" , "nuc" ]
2181
2184
)
@@ -2188,21 +2191,21 @@ def test_2D(self, dtype, ord, axis, keepdims):
2188
2191
def test_ND (self , dtype , ord , axis , keepdims ):
2189
2192
a = generate_random_numpy_array ((2 , 3 , 4 , 5 ), dtype )
2190
2193
ia = dpnp .array (a )
2194
+ kwarg = {"ord" : ord , "axis" : axis , "keepdims" : keepdims }
2195
+
2191
2196
if (axis in [- 1 , 0 , 1 ] and ord in ["nuc" , "fro" ]) or (
2192
2197
isinstance (axis , tuple ) and ord == 3
2193
2198
):
2194
2199
# Invalid norm order for vectors
2195
- with pytest . raises (ValueError ):
2196
- dpnp .linalg .norm ( ia , ord = ord , axis = axis , keepdims = keepdims )
2200
+ assert_raises (ValueError , dpnp . linalg . norm , ia , ** kwarg )
2201
+ assert_raises ( ValueError , numpy .linalg .norm , a , ** kwarg )
2197
2202
elif axis is None and ord is not None :
2198
2203
# Improper number of dimensions to norm
2199
- with pytest . raises (ValueError ):
2200
- dpnp .linalg .norm ( ia , ord = ord , axis = axis , keepdims = keepdims )
2204
+ assert_raises (ValueError , dpnp . linalg . norm , ia , ** kwarg )
2205
+ assert_raises ( ValueError , numpy .linalg .norm , a , ** kwarg )
2201
2206
else :
2202
- result = dpnp .linalg .norm (ia , ord = ord , axis = axis , keepdims = keepdims )
2203
- expected = numpy .linalg .norm (
2204
- a , ord = ord , axis = axis , keepdims = keepdims
2205
- )
2207
+ result = dpnp .linalg .norm (ia , ** kwarg )
2208
+ expected = numpy .linalg .norm (a , ** kwarg )
2206
2209
assert_dtype_allclose (result , expected )
2207
2210
2208
2211
@pytest .mark .usefixtures ("suppress_divide_numpy_warnings" )
@@ -2219,21 +2222,21 @@ def test_ND(self, dtype, ord, axis, keepdims):
2219
2222
def test_usm_ndarray (self , dtype , ord , axis , keepdims ):
2220
2223
a = generate_random_numpy_array ((2 , 3 , 4 , 5 ), dtype )
2221
2224
ia = dpt .asarray (a )
2225
+ kwarg = {"ord" : ord , "axis" : axis , "keepdims" : keepdims }
2226
+
2222
2227
if (axis in [- 1 , 0 , 1 ] and ord in ["nuc" , "fro" ]) or (
2223
2228
isinstance (axis , tuple ) and ord == 3
2224
2229
):
2225
2230
# Invalid norm order for vectors
2226
- with pytest . raises (ValueError ):
2227
- dpnp .linalg .norm ( ia , ord = ord , axis = axis , keepdims = keepdims )
2231
+ assert_raises (ValueError , dpnp . linalg . norm , ia , ** kwarg )
2232
+ assert_raises ( ValueError , numpy .linalg .norm , a , ** kwarg )
2228
2233
elif axis is None and ord is not None :
2229
2234
# Improper number of dimensions to norm
2230
- with pytest . raises (ValueError ):
2231
- dpnp .linalg .norm ( ia , ord = ord , axis = axis , keepdims = keepdims )
2235
+ assert_raises (ValueError , dpnp . linalg . norm , ia , ** kwarg )
2236
+ assert_raises ( ValueError , numpy .linalg .norm , a , ** kwarg )
2232
2237
else :
2233
- result = dpnp .linalg .norm (ia , ord = ord , axis = axis , keepdims = keepdims )
2234
- expected = numpy .linalg .norm (
2235
- a , ord = ord , axis = axis , keepdims = keepdims
2236
- )
2238
+ result = dpnp .linalg .norm (ia , ** kwarg )
2239
+ expected = numpy .linalg .norm (a , ** kwarg )
2237
2240
assert_dtype_allclose (result , expected )
2238
2241
2239
2242
@pytest .mark .parametrize ("stride" , [3 , - 1 , - 5 ])
@@ -2257,8 +2260,7 @@ def test_strided_2D(self, axis, stride):
2257
2260
A = numpy .random .rand (20 , 30 )
2258
2261
B = dpnp .asarray (A )
2259
2262
slices = tuple (slice (None , None , stride [i ]) for i in range (A .ndim ))
2260
- a = A [slices ]
2261
- b = B [slices ]
2263
+ a , b = A [slices ], B [slices ]
2262
2264
2263
2265
result = dpnp .linalg .norm (b , axis = axis )
2264
2266
expected = numpy .linalg .norm (a , axis = axis )
@@ -2278,8 +2280,7 @@ def test_strided_ND(self, axis, stride):
2278
2280
A = numpy .random .rand (12 , 16 , 20 , 24 )
2279
2281
B = dpnp .asarray (A )
2280
2282
slices = tuple (slice (None , None , stride [i ]) for i in range (A .ndim ))
2281
- a = A [slices ]
2282
- b = B [slices ]
2283
+ a , b = A [slices ], B [slices ]
2283
2284
2284
2285
result = dpnp .linalg .norm (b , axis = axis )
2285
2286
expected = numpy .linalg .norm (a , axis = axis )
@@ -2299,6 +2300,49 @@ def test_matrix_norm(self, ord, keepdims):
2299
2300
expected = numpy .linalg .matrix_norm (a , ord = ord , keepdims = keepdims )
2300
2301
assert_dtype_allclose (result , expected )
2301
2302
2303
+ @pytest .mark .parametrize (
2304
+ "xp" ,
2305
+ [
2306
+ dpnp ,
2307
+ pytest .param (
2308
+ numpy ,
2309
+ marks = pytest .mark .skipif (
2310
+ numpy_version () < "2.3.0" ,
2311
+ reason = "numpy raises an error" ,
2312
+ ),
2313
+ ),
2314
+ ],
2315
+ )
2316
+ @pytest .mark .parametrize ("dtype" , [dpnp .float32 , dpnp .int32 ])
2317
+ @pytest .mark .parametrize (
2318
+ "shape_axis" , [[(2 , 0 ), None ], [(2 , 0 ), (0 , 1 )], [(0 , 2 ), (0 , 1 )]]
2319
+ )
2320
+ @pytest .mark .parametrize ("ord" , [None , "fro" , "nuc" , 1 , 2 , dpnp .inf ])
2321
+ def test_matrix_norm_empty (self , xp , dtype , shape_axis , ord ):
2322
+ shape , axis = shape_axis [0 ], shape_axis [1 ]
2323
+ x = xp .zeros (shape , dtype = dtype )
2324
+ assert_equal (xp .linalg .norm (x , axis = axis , ord = ord ), 0 )
2325
+
2326
+ @pytest .mark .parametrize (
2327
+ "xp" ,
2328
+ [
2329
+ dpnp ,
2330
+ pytest .param (
2331
+ numpy ,
2332
+ marks = pytest .mark .skipif (
2333
+ numpy_version () < "2.3.0" ,
2334
+ reason = "numpy raises an error" ,
2335
+ ),
2336
+ ),
2337
+ ],
2338
+ )
2339
+ @pytest .mark .parametrize ("dtype" , [dpnp .float32 , dpnp .int32 ])
2340
+ @pytest .mark .parametrize ("axis" , [None , 0 ])
2341
+ @pytest .mark .parametrize ("ord" , [None , 1 , 2 , dpnp .inf ])
2342
+ def test_vector_norm_empty (self , xp , dtype , axis , ord ):
2343
+ x = xp .zeros (0 , dtype = dtype )
2344
+ assert_equal (xp .linalg .vector_norm (x , axis = axis , ord = ord ), 0 )
2345
+
2302
2346
@testing .with_requires ("numpy>=2.0" )
2303
2347
@pytest .mark .parametrize (
2304
2348
"ord" , [None , - dpnp .inf , - 2 , - 1 , 0 , 1 , 2 , 3.5 , dpnp .inf ]
@@ -2320,13 +2364,10 @@ def test_vector_norm_0D(self, ord):
2320
2364
def test_vector_norm_1D (self , ord , axis , keepdims ):
2321
2365
a = generate_random_numpy_array (10 )
2322
2366
ia = dpnp .array (a )
2367
+ kwarg = {"ord" : ord , "axis" : axis , "keepdims" : keepdims }
2323
2368
2324
- result = dpnp .linalg .vector_norm (
2325
- ia , ord = ord , axis = axis , keepdims = keepdims
2326
- )
2327
- expected = numpy .linalg .vector_norm (
2328
- a , ord = ord , axis = axis , keepdims = keepdims
2329
- )
2369
+ result = dpnp .linalg .vector_norm (ia , ** kwarg )
2370
+ expected = numpy .linalg .vector_norm (a , ** kwarg )
2330
2371
assert_dtype_allclose (result , expected )
2331
2372
2332
2373
@testing .with_requires ("numpy>=2.0" )
@@ -2343,29 +2384,26 @@ def test_vector_norm_1D(self, ord, axis, keepdims):
2343
2384
def test_vector_norm_ND (self , ord , axis , keepdims ):
2344
2385
a = numpy .arange (120 ).reshape (2 , 3 , 4 , 5 )
2345
2386
ia = dpnp .array (a )
2387
+ kwarg = {"ord" : ord , "axis" : axis , "keepdims" : keepdims }
2346
2388
2347
- result = dpnp .linalg .vector_norm (
2348
- ia , ord = ord , axis = axis , keepdims = keepdims
2349
- )
2350
- expected = numpy .linalg .vector_norm (
2351
- a , ord = ord , axis = axis , keepdims = keepdims
2352
- )
2389
+ result = dpnp .linalg .vector_norm (ia , ** kwarg )
2390
+ expected = numpy .linalg .vector_norm (a , ** kwarg )
2353
2391
assert_dtype_allclose (result , expected )
2354
2392
2355
2393
def test_error (self ):
2356
- ia = dpnp .arange (120 ).reshape (2 , 3 , 4 , 5 )
2394
+ a = numpy .arange (120 ).reshape (2 , 3 , 4 , 5 )
2395
+ ia = dpnp .array (a )
2357
2396
2358
2397
# Duplicate axes given
2359
- with pytest . raises ( ValueError ):
2360
- dpnp .linalg .norm ( ia , axis = (2 , 2 ))
2398
+ assert_raises ( ValueError , dpnp . linalg . norm , ia , axis = ( 2 , 2 ))
2399
+ assert_raises ( ValueError , numpy .linalg .norm , a , axis = (2 , 2 ))
2361
2400
2362
2401
#'axis' must be None, an integer or a tuple of integers
2363
- with pytest . raises (TypeError ):
2364
- dpnp .linalg .norm ( ia , axis = [2 ])
2402
+ assert_raises (TypeError , dpnp . linalg . norm , ia , axis = [ 2 ])
2403
+ assert_raises ( TypeError , numpy .linalg .norm , a , axis = [2 ])
2365
2404
2366
2405
# Invalid norm order for vectors
2367
- with pytest .raises (ValueError ):
2368
- dpnp .linalg .norm (ia , axis = 1 , ord = [3 ])
2406
+ assert_raises (ValueError , dpnp .linalg .norm , ia , axis = 1 , ord = [3 ])
2369
2407
2370
2408
2371
2409
class TestQr :
0 commit comments