Skip to content

perf(j2k-metal): batch tiled operations within the device budget - #381

Merged
jcwal1516 merged 7 commits into
stage/15-metal-tiled-fullfrom
stage/16-metal-tiled-operations
Dec 2, 2026
Merged

jcwal1516 merged 7 commits into
stage/15-metal-tiled-fullfrom
stage/16-metal-tiled-operations

Conversation

@jcwal1516

@jcwal1516 jcwal1516 commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

What and why

Metal region, scaled, RGBA8, destination, and batch requests for tiled images
still submit tiles independently or decline the grouped route.

This layer extends bounded tile groups to those request types, merges Tier-1
work across tiled images in a batch, and lets classic tiled grayscale decode
share Tier-1 work with the CPU pool. MetalBatchDecoder splits submissions so
each run stays within the tiled device-memory budget.

Impact

  • Changes decoded, encoded, or transcoded output bytes
  • Changes a public API or behavior callers can see (if breaking, add ! to the title)
    Explicit tiled destination requests now run on Metal when their grouped
    request is supported; explicit GPU requests remain strict.
  • Changes how untrusted input is parsed, or how much memory or time it can use
    Batch groups are partitioned by checked device-memory estimates, and one
    oversized tile fails cleanly.
  • Changes performance (command, hardware, and numbers under Testing)
    Region, scaled, RGBA8, destination, and multi-image tiled requests share
    GPU dispatches instead of submitting one sub-band or tile at a time.
  • 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

  • Real M5 Max tests at the final stack tip cover region, scaled, RGBA8,
    destination, classic grayscale, merged Tier-1, batch grouping, and every
    resident/destination entry point under the device budget.

  • A mixed single-tile/multi-tile exact-operation group fails with the typed
    allocation-size error before submitting work when its unsplittable plan
    exceeds the group's device budget.

  • 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

@jcwal1516
jcwal1516 added this pull request to stack #368 December 2, 2026 04:06
@jcwal1516 jcwal1516 changed the title stage/16 metal tiled operations perf(j2k-metal): batch tiled operations within the device budget Dec 2, 2026
@jcwal1516
jcwal1516 force-pushed the stage/16-metal-tiled-operations branch 3 times, most recently from 8a97b7f to f885233 Compare December 2, 2026 19:04
…s of tiled images in groups

Only full Gray and RGB decodes of a tiled image took the grouped plan
route. Region and reduced-resolution requests, RGBA8 output of an RGB
image, and decode_into_metal fell back to the native decoder's hooks,
which commit and wait on a command buffer for each sub-band, transform
and store of each tile, or, for decode_into_metal, failed with
MetalDirectFallback.

Generalize the grouped route to a request: the tiles of the region on
the reduced image, clipped to it, grouped under the device budget, each
group planned for its rectangle and pruned to what its stores read. RGBA8
decodes RGB8 groups and makes them opaque in the copy into the output,
and a destination receives the groups' copies in place of a new output.
A sole group that needs no copy still decodes straight into its surface.

On an Apple M5 Max, for a lossless 2048 x 2048 RGB8 image in 256 x 256
tiles: half size 4.3 s -> 13 ms (HTJ2K 1.2 s -> 7 ms), a region 1.6 s ->
9 ms, RGBA8 6.7 s -> 30 ms, decode_into_metal 23 ms. A half-size decode
of a 16384 x 16384 Gray8 image in 1024 x 1024 tiles went from 16 s to
0.2 s. Pixels match the CPU.
MetalBatchDecoder planned a tiled image as one plan of all its tiles, but
each tile kept its own Tier-1 dispatch per component, which the GPU runs
one after another; a lossless 2048 x 2048 RGB8 image in 256 x 256 tiles
took 360 ms. Merging the tiles' Tier-1 steps, as the single-image route
does, failed the group: the stacked executor dropped every band at each
tile's store, including those the merged group had already decoded for
later tiles.

Keep the bands above a store's input band at the store, per stacked image,
and merge the tiles' Tier-1 steps of every batch plan after pruning. On an
Apple M5 Max that image now decodes in 65 ms (HTJ2K 16 ms -> 4 ms), a
batch of four in 100 ms instead of 560 ms, and the 512 x 512 tile version
in 43 ms instead of 106 ms. Pixels match the CPU.
…the CPU pool

A tiled classic Gray8 image decoded slower on Metal than on the CPU: a
lossless 16384 x 16384 image in 1024 x 1024 tiles took 570 ms against
490 ms. Timing the groups showed planning at 1 ms and copies at 12 ms of
the 570; the rest was the 16 groups' execution, in which the GPU decoded
every classic code block alone. A colour decode already shares each
component's first classic group with the CPU pool, the finest sub-bands
going to the pool up to the runtime's measured share.

Run that split for the grayscale plans of tiled groups too, whose merged
first group covers the group's tiles, and resume the single grayscale
executor after the group the split decoded. The image now decodes in
0.34 s, at half size in 0.15 s (was 0.2 s), and a region in 0.11 s (was
0.16 s), byte-identical to the CPU.

The tiled tests derived their group budgets from the bytes a command
buffer held, which the CPU share now lowers by a varying amount, so a
budget could fall below one tile's charge. Derive them from the bytes
the groups are charged, which groups are split by.
An explicit GPU request never runs on the CPU, but Metal already hands part
of classic JPEG 2000 block decoding to CPU threads, with the inverse
transform, color conversion and output on the GPU. Write that down in
AGENTS.md, CONTRIBUTING.md and the GPU chapter so the rule matches the
code.
The row copy kernels compute byte indices in 32 bits. A tiled decode's
copy of a group's rows into its output checked that the window's first
offset fit 32 bits but not its last byte, so rows that start below 4 GiB
of a caller's destination and end past it wrapped and overwrote the
buffer's first rows. Check the end of the source and destination ranges
too, so such a copy fails with an error instead.
…device budget

A batch group decodes all its images in one command buffer, which holds
every image's working set until it completes, and a tiled image's plan
covers all its tiles. On an Apple M5 Max a lossless 16384 x 16384 Gray8
image in 1024 x 1024 tiles held 2.9 GiB of device memory in a batch
(1.9 GiB for HTJ2K) and two of them 5.4 GiB, against the 512 MiB the
single-image route keeps each command buffer to. Decoded into a caller's
buffer, the RGB8 image failed with a buffer-pool allocation error part way
through.

When a group has a tiled image and its images don't fit that budget
together, decode the group a run at a time, each run complete before the
next starts: consecutive images whose plans fit, into their part of the
group's output, or one tiled image over the budget a group of its tiles at
a time. Its tile groups are planned as the single-image route plans them,
merging their tiles' Tier-1, decoded through the batch's own store into the
batch layout and sample format, and their rows (or each plane's, for NCHW)
copied into place. The group's submission is then an empty command buffer
committed with the requested consumer ordering. An image over the budget
builds no whole-image plan, the cooperative split leaves such groups to
this route, and a tile that alone is over the budget fails its group with
BufferAllocationTooLarge.

The Gray8 image now takes 0.67 GiB (0.71 GiB for HTJ2K) however many share
its batch, in the same time, and the RGB8 one decodes into a caller's
buffer with 0.75 GiB besides it. A batch of four lossless 2048 x 2048 RGB8
images in 256 x 256 tiles takes 0.6 GiB instead of 1 GiB, in two runs, and
31 ms instead of 21 ms. Groups without a tiled image are unchanged, and
pixels match the CPU.
@jcwal1516
jcwal1516 force-pushed the stage/16-metal-tiled-operations branch from f885233 to eac5193 Compare December 2, 2026 19:06
@jcwal1516
jcwal1516 removed this pull request from stack #368 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 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
53 of 72 checks passed
@jcwal1516
jcwal1516 deleted the stage/16-metal-tiled-operations 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