Repository navigation
perf(j2k-metal): decode tiled images in bounded groups - #380
Merged
Merged
Conversation
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
force-pushed
the
stage/15-metal-tiled-full
branch
from
December 2, 2026 19:01
70d3945 to
53c372f
Compare
jcwal1516
force-pushed
the
stage/15-metal-tiled-full
branch
from
December 2, 2026 19:06
f98b8e3 to
8b4f142
Compare
jcwal1516
marked this pull request as ready for review
December 2, 2026 23:48
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
Autokeeps tiled half-size requests on the CPU because the routing evidencecovers single-tile inputs.
Impact
!to the title)Large tiled surfaces may be banded, so single-buffer accessors return
None;Surface::download_intoreads every supported surface.Tile groups and output bands are bounded by existing device-allocation
limits, and checked row-copy offsets reject values beyond 32 bits.
The saved M5 Max measurements reduce multi-tile decodes from one GPU trip
per sub-band to bounded group submissions.
data, source noted in a README and NOTICES.md)
Testing
cargo fmt --all -- --checkcargo check --locked -p j2k-metal --all-targets --all-featuresThe 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.mdentry underUnreleased