@@ -1875,15 +1875,16 @@ def reduce_slices( # noqa: C901
18751875 cslice = step_handler (cslice , _slice )
18761876 chunks_ = tuple (s .stop - s .start for s in cslice )
18771877 unit_steps = np .all ([s .step == 1 for s in cslice ])
1878+ # Get the starts for the slice (needed for offset calculations when intra-chunk slicing)
1879+ starts = [s .start if s .start is not None else 0 for s in cslice ]
18781880 if _slice == () and fast_path and unit_steps :
18791881 # Fast path
18801882 full_chunk = chunks_ == chunks
18811883 fill_chunk_operands (
18821884 operands , cslice , chunks_ , full_chunk , aligned , nchunk , iter_disk , chunk_operands , reduc = True
18831885 )
18841886 else :
1885- # Get the starts and stops for the slice
1886- starts = [s .start if s .start is not None else 0 for s in cslice ]
1887+ # Get the stops for the slice
18871888 stops = [s .stop if s .stop is not None else sh for s , sh in zip (cslice , chunks_ , strict = True )]
18881889 # Get the slice of each operand
18891890 for key , value in operands .items ():
@@ -1968,12 +1969,15 @@ def reduce_slices( # noqa: C901
19681969 )
19691970 if reduce_args ["axis" ] is None : # indexing into flattened array
19701971 result_val = result_val [np .unravel_index (result , shape = result_val .shape )]
1971- result += np .ravel_multi_index (offset , shape )
1972+ idx_within_cslice = np .unravel_index (result , shape = chunks_ )
1973+ result = np .ravel_multi_index (
1974+ tuple (o + i for o , i in zip (starts , idx_within_cslice , strict = True )), shape
1975+ )
19721976 else : # axis is an integer
19731977 result_val = np .take_along_axis (
19741978 result_val , np .expand_dims (result , axis = reduce_args ["axis" ]), axis = reduce_args ["axis" ]
19751979 )
1976- result += offset [reduce_args ["axis" ]]
1980+ result += starts [reduce_args ["axis" ]]
19771981 else :
19781982 result = reduce_op .value .reduce (result , ** reduce_args )
19791983
0 commit comments