Skip to content

perf(j2k-metal): decode tiled images in bounded groups - #380

Merged
jcwal1516 merged 6 commits into
mainfrom
stage/15-metal-tiled-full
Dec 2, 2026
Merged

jcwal1516 merged 6 commits into
mainfrom
stage/15-metal-tiled-full

Conversation

@jcwal1516

@jcwal1516 jcwal1516 commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

What and why

Explicit Metal decodes of multi-tile JPEG 2000 and HTJ2K images currently
submit each sub-band of each tile separately. Large tiled outputs also fail
when one surface allocation would exceed 512 MiB.

This layer plans tiled full-image decodes once, dispatches bounded groups of
tiles, and stores outputs larger than one Metal allocation in row bands. A
tile that cannot fit within the device-work budget returns a typed error.
Auto keeps tiled half-size requests on the CPU because the routing evidence
covers single-tile inputs.

Impact

  • Changes decoded, encoded, or transcoded output bytes
  • Changes a public API or behavior callers can see (if breaking, add ! to the title)
    Large tiled surfaces may be banded, so single-buffer accessors return
    None; Surface::download_into reads every supported surface.
  • Changes how untrusted input is parsed, or how much memory or time it can use
    Tile groups and output bands are bounded by existing device-allocation
    limits, and checked row-copy offsets reject values beyond 32 bits.
  • Changes performance (command, hardware, and numbers under Testing)
    The saved M5 Max measurements reduce multi-tile decodes from one GPU trip
    per sub-band to bounded group submissions.
  • Adds a test image or fixture (small, license allows committing it, no patient
    data, source noted in a README and NOTICES.md)

Testing

  • cargo fmt --all -- --check

  • cargo check --locked -p j2k-metal --all-targets --all-features

  • The tiled Metal library suite at the final stack tip passes on an M5 Max,
    including grouped dispatch, device-budget, CPU-pixel parity, and session
    reuse cases.

  • The 512 MiB output and 538 MB group-fallback cases pass separately under
    real Metal; their exact commands and final-head hardware run are recorded
    in the top layer.

  • Exact-head CUDA and Metal validation: pending.

  • Bug fix: added tests that fail without the fixes

  • User-visible change: added a CHANGELOG.md entry under Unreleased

An explicit Metal full decode of a multi-tile codestream fell off the
single-tile direct route into the native decoder's hooks, which commit and
wait on a command buffer, with fresh buffers and a host readback, for each
sub-band, inverse transform and store of each tile. A 2048x2048 lossless
RGB8 image in 256x256 tiles took 11.2 s against 46 ms on the CPU.

Plan all tiles once with the referenced classic or HTJ2K planner, keep the
plan on the decoder, and encode the image into one command buffer. Reorder
each component's steps so the Tier-1 sub-bands of consecutive tiles form
one group, up to 16M coefficients, instead of one serial dispatch per
tile. The single grayscale executor now writes every tile store into one
surface, and the component-plane executor keeps later tiles' decoded bands
at a tile's store.

Classic RGB8 now decodes in 25 ms, HTJ2K RGB8 in 9 ms, byte-identical to
the CPU. Images the planners or kernels reject keep the per-tile route.
The routing measurements cover single-tile codestreams only, and Auto
already kept full RGB8 decodes of tiled images on the CPU. A half-size
request had no such check, so a tiled 2048x2048 RGB8 image went to Metal,
which decodes reduced requests of tiled images tile by tile: 6.8 s against
12 ms on the CPU.

Apply the single-tile check to half-size decodes as well. Full Gray8
decodes keep their routing, since a tiled full decode now runs as fast on
Metal as an untiled one.
One plan of every tile held the whole image's working set in one command
buffer, which grows without bound with the image. Split the tiles into
groups of whole rows, or runs of one row, whose plan, scratch and surface
fit the smaller of the per-allocation cap and the buffer pool's retention
cap. Each group is planned, checked exactly against the budget (and split
when its coded data makes it larger), decoded with its Tier-1 steps
merged, and copied into one output buffer before the next group, so its
scratch returns to the pool. An image that fits keeps the cached
whole-image plan and decodes straight into its surface. A tile that alone
exceeds the budget fails with BufferAllocationTooLarge.
The grouped tiled decode copied every group into one output buffer, and
one Metal allocation holds at most 512 MiB, so an explicit Metal decode
of a 16384 x 16384 RGB8 image (768 MiB) failed with
BufferAllocationTooLarge before decoding a tile.

Allocate such an output as bands of whole rows, each within one
allocation, and copy a group's rows into every band they reach. The
whole output stays within the device's recommended working set. The
returned surface is Metal-resident but in several buffers:
download_into reads it into the caller's memory, resident_metal_image,
metal_buffer and memory_range return None, and as_bytes returns an
error, since its bytes don't fit one host allocation.

The lossless 16384 x 16384 RGB8 image in 1024 x 1024 tiles now decodes
in 1.1 s (0.22 s for HTJ2K) on an Apple M5 Max, byte-identical to the
CPU.
@jcwal1516
jcwal1516 added this pull request to stack #368 December 2, 2026 04:06
@jcwal1516 jcwal1516 changed the title stage/15 metal tiled full perf(j2k-metal): decode tiled images in bounded groups Dec 2, 2026
@jcwal1516
jcwal1516 force-pushed the stage/15-metal-tiled-full branch from 70d3945 to 53c372f Compare December 2, 2026 19:01
@jcwal1516
jcwal1516 force-pushed the stage/15-metal-tiled-full branch from f98b8e3 to 8b4f142 Compare December 2, 2026 19:06
@jcwal1516
jcwal1516 removed this pull request from stack #368 December 2, 2026 20:01
Base automatically changed from stage/14-gpu-auto-routing to main December 2, 2026 20:01
@jcwal1516
jcwal1516 added this pull request to stack #387 December 2, 2026 20:01
@jcwal1516
jcwal1516 removed this pull request from stack #387 December 2, 2026 22:20
@jcwal1516 jcwal1516 closed this Dec 2, 2026
@jcwal1516 jcwal1516 reopened this Dec 2, 2026
@jcwal1516
jcwal1516 added this pull request to stack #390 December 2, 2026 22:21
@jcwal1516
jcwal1516 marked this pull request as ready for review December 2, 2026 23:48
@jcwal1516
jcwal1516 removed this pull request from stack #390 December 2, 2026 23:48
@jcwal1516
jcwal1516 added this pull request to stack #391 December 2, 2026 23:48
@jcwal1516
jcwal1516 merged commit 323ba2c into main Dec 2, 2026
52 of 59 checks passed
@jcwal1516
jcwal1516 deleted the stage/15-metal-tiled-full branch December 2, 2026 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant