@@ -2320,6 +2320,79 @@ def test_iter_buffering_growinner():
23202320 assert_equal (i [0 ].size , a .size )
23212321
23222322
2323+ @pytest .mark .parametrize ("read_or_readwrite" , ["readonly" , "readwrite" ])
2324+ def test_iter_contig_flag_reduce_error (read_or_readwrite ):
2325+ # Test that a non-contiguous operand is rejected without buffering.
2326+ # NOTE: This is true even for a reduction, where we return a 0-stride
2327+ # below!
2328+ with pytest .raises (TypeError , match = "Iterator operand required buffering" ):
2329+ it = np .nditer (
2330+ (np .zeros (()),), flags = ["external_loop" , "reduce_ok" ],
2331+ op_flags = [(read_or_readwrite , "contig" ),], itershape = (10 ,))
2332+
2333+
2334+ @pytest .mark .parametrize ("arr" , [
2335+ lambda : np .zeros (()),
2336+ lambda : np .zeros ((20 , 1 ))[::20 ],
2337+ lambda : np .zeros ((1 , 20 ))[:, ::20 ]
2338+ ])
2339+ def test_iter_contig_flag_single_operand_strides (arr ):
2340+ """
2341+ Tests the strides with the contig flag for both broadcast and non-broadcast
2342+ operands in 3 cases where the logic is needed:
2343+ 1. When everything has a zero stride, the broadcast op needs to repeated
2344+ 2. When the reduce axis is the last axis (first to iterate).
2345+ 3. When the reduce axis is the first axis (last to iterate).
2346+
2347+ NOTE: The semantics of the cast flag are not clearly defined when
2348+ it comes to reduction. It is unclear that there are any users.
2349+ """
2350+ first_op = np .ones ((10 , 10 ))
2351+ broadcast_op = arr ()
2352+ red_op = arr ()
2353+ # Add a first operand to ensure no axis-reordering and the result shape.
2354+ iterator = np .nditer (
2355+ (first_op , broadcast_op , red_op ),
2356+ flags = ["external_loop" , "reduce_ok" , "buffered" , "delay_bufalloc" ],
2357+ op_flags = [("readonly" , "contig" )] * 2 + [("readwrite" , "contig" )])
2358+
2359+ with iterator :
2360+ iterator .reset ()
2361+ try :
2362+ for f , b , r in iterator :
2363+ # The first operand is contigouos, we should have a view
2364+ assert np .shares_memory (f , first_op )
2365+ # Although broadcast, the second op always has a contiguous stride
2366+ assert b .strides [0 ] == 8
2367+ assert not np .shares_memory (b , broadcast_op )
2368+ # The reduction has a contiguous stride or a 0 stride
2369+ if red_op .ndim == 0 or red_op .shape [- 1 ] == 1 :
2370+ assert r .strides [0 ] == 0
2371+ else :
2372+ # The stride is 8, although it was not originally:
2373+ assert r .strides [0 ] == 8
2374+ # If the reduce stride is 0, buffering makes no difference, but we
2375+ # do it anyway right now:
2376+ assert not np .shares_memory (r , red_op )
2377+ finally :
2378+ iterator .debug_print ()
2379+
2380+
2381+ @pytest .mark .xfail (reason = "The contig flag was always buggy." )
2382+ def test_iter_contig_flag_incorrect ():
2383+ # This case does the wrong thing...
2384+ iterator = np .nditer (
2385+ (np .ones ((10 , 10 )).T , np .ones ((1 , 10 ))),
2386+ flags = ["external_loop" , "reduce_ok" , "buffered" , "delay_bufalloc" ],
2387+ op_flags = [("readonly" , "contig" )] * 2 )
2388+
2389+ with iterator :
2390+ iterator .reset ()
2391+ for a , b in iterator :
2392+ assert a .strides == 8
2393+ assert b .strides == 8 # should be 8 but is 0 due to axis reorder
2394+
2395+
23232396@pytest .mark .slow
23242397def test_iter_buffered_reduce_reuse ():
23252398 # large enough array for all views, including negative strides.
@@ -3324,8 +3397,8 @@ def test_debug_print(capfd):
33243397 | BufIterEnd: 5
33253398 | BUFFER CoreSize: 5
33263399 | REDUCE Pos: 0
3327- | BUFFER Reduce outersize : 10
3328- | BUFFER OuterDim: 1
3400+ | REDUCE OuterSize : 10
3401+ | REDUCE OuterDim: 1
33293402 | Strides: 8 4
33303403 | Ptrs:
33313404 | REDUCE Outer Strides: 40 0
0 commit comments