Commit 2031c04
peng.li24
fix(reduce): align mean_axis/norm_axis accumulation with numpy (issue #1)
Root cause
----------
numpy's np.mean(a, axis=k) / np.sum(a, axis=k) use two different
accumulation algorithms depending on whether the reduced axis is
memory-contiguous:
axis_stride == 1 (last / contiguous axis in C order)
→ pairwise_sum: same 3-tier (sequential-base / 8-accumulator /
recursive) algorithm numpy uses for flat 1-D reductions
axis_stride > 1 (non-contiguous axis, e.g. axis=0 of a C-order array)
→ sequential left-fold: numpy processes the array row-by-row,
accumulating each output element one source element at a time
The previous mean_axis / norm_axis always used pairwise_sum regardless
of stride. For n ≥ 8 on a non-contiguous axis (the common case: axis=0
of a (N, M) C-order array) this produced a result that differed from
numpy by up to several ULP, causing downstream argmin / norm pipelines
to select different indices.
The specific report (issue #1):
(4, 2) float32 polygon → mean_axis(axis=0) → (2,) centre
→ norm(subpath - centre) → argmin → different cumsum reward
Fix
---
Add sequential_sum() helper (simple left-fold from -0.0).
In mean_axis and norm_axis, select the algorithm per fiber based on
the axis memory stride that axis_reduce_impl exposes as parameter 'as':
as == 1 → pairwise_sum (contiguous axis, matches numpy 1-D path)
as > 1 → sequential_sum (strided axis, matches numpy row-by-row path)
Whole-array sum() / mean() (axis=None, flat 1-D path) are unchanged —
they continue to use pairwise_sum, which matches numpy's 1-D behaviour.
Tests added (tests/test_all.py)
--------------------------------
- test_mean_axis_polygon_center_f32 exact (4,2) polygon scenario
- test_mean_axis_polygon_center_rounding_f32 near-2^23 rounding boundary
- test_mean_axis_large_fiber[n_axis] n ∈ {8,9,16,17,100,128,129} for
both axis=0 (stride>1) and axis=1
(stride=1), float32 + float64
- test_mean_axis_n8_boundary_f32 2^24 sentinel values that expose
pairwise vs sequential difference
All 917 tests pass.1 parent 1cbc1e7 commit 2031c04
2 files changed
Lines changed: 132 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
30 | 59 | | |
31 | 60 | | |
32 | 61 | | |
| |||
48 | 77 | | |
49 | 78 | | |
50 | 79 | | |
| 80 | + | |
51 | 81 | | |
52 | 82 | | |
53 | 83 | | |
| |||
195 | 225 | | |
196 | 226 | | |
197 | 227 | | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
198 | 240 | | |
199 | 241 | | |
200 | 242 | | |
201 | 243 | | |
202 | 244 | | |
203 | 245 | | |
204 | 246 | | |
205 | | - | |
206 | | - | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
207 | 251 | | |
208 | 252 | | |
209 | 253 | | |
210 | 254 | | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
211 | 259 | | |
212 | 260 | | |
213 | 261 | | |
| |||
217 | 265 | | |
218 | 266 | | |
219 | 267 | | |
220 | | - | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
221 | 272 | | |
222 | 273 | | |
223 | 274 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
791 | 791 | | |
792 | 792 | | |
793 | 793 | | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
794 | 867 | | |
795 | 868 | | |
796 | 869 | | |
| |||
0 commit comments