Skip to content

_box_reduce float64 accumulator dominates large-frame set_data — float32 accumulate for the float path? #64

Closed

Description

@TheDrOnos

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.

Activity

  1. CSSFrancis commented on Aug 31, 2026

    @CSSFrancis
    Owner

    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:

    a = np.full((2048, 2048), -100, np.int16)
    ax.imshow(a)          # overview reads 1073741696.0, silently

    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:

    float32  8192² -> 1024²    64.6ms -> 15.7ms   4.13x
    float64  8192² -> 1024²    57.0ms -> 26.3ms   2.17x
    uint16   8192² -> 1024²    66.3ms -> 41.6ms   1.59x
    

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions