You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
_box_reduce float64 accumulator dominates large-frame set_data — float32 accumulate for the float path? #64
Profiling 8192² float32 frames through the tiled path, _make_overview's _box_reduce is the dominant cost of every set_data: ~170 ms per call on two different machines (~156–172 ms), which is essentially the entire cost of showing an 8k frame (encode + ship measure under 5 ms with binary transport).
The float path accumulates with blk.sum(axis=(1,3), dtype=np.float64) — ~537 MB of accumulator traffic to reduce a float32 frame whose overview is immediately quantised to 8-bit tile bytes. The integer path beside it was deliberately built to avoid exactly this widening cast, so the float path seems to have inherited the safe-by-default dtype rather than needed it: a float32 accumulate (or a two-stage float32 pairwise reduce) would roughly halve memory traffic for an output that cannot express the difference after quantisation.
Happy to PR this if the direction is acceptable. Measured on 0.7.3.
Thanks @TheDrOnos — good catch, and the direction is fine. Two corrections to the
mechanism, and the profiling turned up something worse sitting next to it. #65 has
the fix.
The float64 accumulator is not doing what you think.sum(dtype=np.float64)
over a float32 region does not materialise a float64 copy — numpy buffers the cast
in chunks, so the accumulator is the 1024² output grid (8 MB), not 537 MB. Which is
why float32 on its own only buys ~1.1x, not the ~2x you were expecting:
variant (8192² float32 → 1024² overview)
sum(axis=(1,3), dtype=float64) — today
64.6 ms
sum(axis=(1,3), dtype=float32) — your proposal
58.5 ms
two-stage rows-first, float64
44.1 ms
two-stage rows-first, float32
15.7 ms
The real cost is the reduction order.sum(axis=(1, 3)) collapses a strided
axis and a contiguous one in one pass, which defeats numpy's fast inner loops.
Collapsing whole rows first, then the contiguous column blocks, walks memory in
order both times. The two changes compound rather than add — once the access
pattern is contiguous the cast becomes the bottleneck, and vice versa.
Your precision argument holds, for the record: float32 accumulation costs 2.4e-7
relative error against an output quantised to 1/255. The overview genuinely cannot
express the difference.
The thing I actually went looking for the perf and found instead — the integer
path you describe as "deliberately built to avoid the widening cast" is not safe. np.uint32 if is_int else np.float64 wraps every signed frame:
On a non-divisible grid the same frame raises _UFuncOutputCastingError out of the
ragged accumulate instead, and uint32 overflows anything past 16 bits regardless
of sign (int64 3e9 → 4.7e7). test_tile_backend.py only ever covered uint16 and
float32, which is why it has been sitting there since the tiled path landed. So the
accumulator is now sized to the data rather than assumed, which fixes that and gets
your speedup in the same change.
Measured after, on Apple silicon / numpy 2.4.4 — your ~170 ms suggests the absolute
numbers travel less well than the ratios, so I would be glad of a second reading
from one of your machines:
Sorry to take the PR off you after you offered — the signed-int bug made it a
correctness fix that wanted to ship on its own timeline. Review on #65 very welcome,
particularly if you have int16 or float64 frames to point at it.
Activity
CSSFrancis commentedon Aug 31, 2026
Thanks @TheDrOnos — good catch, and the direction is fine. Two corrections to the
mechanism, and the profiling turned up something worse sitting next to it. #65 has
the fix.
The float64 accumulator is not doing what you think.
sum(dtype=np.float64)over a float32 region does not materialise a float64 copy — numpy buffers the cast
in chunks, so the accumulator is the 1024² output grid (8 MB), not 537 MB. Which is
why float32 on its own only buys ~1.1x, not the ~2x you were expecting:
sum(axis=(1,3), dtype=float64)— todaysum(axis=(1,3), dtype=float32)— your proposalThe real cost is the reduction order.
sum(axis=(1, 3))collapses a stridedaxis and a contiguous one in one pass, which defeats numpy's fast inner loops.
Collapsing whole rows first, then the contiguous column blocks, walks memory in
order both times. The two changes compound rather than add — once the access
pattern is contiguous the cast becomes the bottleneck, and vice versa.
Your precision argument holds, for the record: float32 accumulation costs 2.4e-7
relative error against an output quantised to 1/255. The overview genuinely cannot
express the difference.
The thing I actually went looking for the perf and found instead — the integer
path you describe as "deliberately built to avoid the widening cast" is not safe.
np.uint32 if is_int else np.float64wraps every signed frame:On a non-divisible grid the same frame raises
_UFuncOutputCastingErrorout of theragged accumulate instead, and
uint32overflows anything past 16 bits regardlessof sign (int64 3e9 → 4.7e7).
test_tile_backend.pyonly ever covered uint16 andfloat32, which is why it has been sitting there since the tiled path landed. So the
accumulator is now sized to the data rather than assumed, which fixes that and gets
your speedup in the same change.
Measured after, on Apple silicon / numpy 2.4.4 — your ~170 ms suggests the absolute
numbers travel less well than the ratios, so I would be glad of a second reading
from one of your machines:
Sorry to take the PR off you after you offered — the signed-int bug made it a
correctness fix that wanted to ship on its own timeline. Review on #65 very welcome,
particularly if you have int16 or float64 frames to point at it.
fix(plot2d): size the box-reduce accumulator to the data, not to uint32