Skip to content

fix(j2k-metal): fall back one rejected tile group through hooks - #382

Merged
jcwal1516 merged 3 commits into
mainfrom
stage/17-metal-tiled-fallback
Dec 2, 2026
Merged

jcwal1516 merged 3 commits into
mainfrom
stage/17-metal-tiled-fallback

Conversation

@jcwal1516

@jcwal1516 jcwal1516 commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

What and why

When one group of a tiled Metal decode is rejected by the device kernels, the
grouped route currently gives up on the whole image. The full native-hook
fallback can exceed the host decode limit even though decoding only the
rejected tile group would fit.

This layer decodes only the rejected group through the existing native hooks
and copies it into the grouped output. Other groups stay on the Metal path.
Caller-owned destination requests keep their documented strict direct-fallback
error. The release-Metal command now includes the large tiled hardware tests.

Impact

  • Changes decoded, encoded, or transcoded output bytes
  • Changes a public API or behavior callers can see (if breaking, add ! to the title)
    Supported tiled surface requests can complete when one group needs the
    slower hooks; caller-owned direct destinations keep returning a typed
    direct-fallback error.
  • Changes how untrusted input is parsed, or how much memory or time it can use
    The fallback is limited to the rejected group rectangle instead of keeping
    every component of a large image on the host.
  • Changes performance (command, hardware, and numbers under Testing)
    Only rejected groups take the slower hook path.
  • 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

  • On a real M5 Max, 26 focused tiled tests passed. They cover full, reduced,
    region, RGBA8, destination, batch, budgets, classic/HT, and CPU parity.

  • The ignored 538 MB rejected-group test and 512 MiB output test both passed
    separately with J2K_REQUIRE_METAL_RUNTIME=1.

  • 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/17 metal tiled fallback fix(j2k-metal): fall back one rejected tile group through hooks Dec 2, 2026
@jcwal1516
jcwal1516 force-pushed the stage/17-metal-tiled-fallback branch from 1b49ed8 to c837089 Compare December 2, 2026 04:14
@jcwal1516
jcwal1516 force-pushed the stage/16-metal-tiled-operations branch from f62efab to ec2ad5e Compare December 2, 2026 04:14
@jcwal1516
jcwal1516 force-pushed the stage/17-metal-tiled-fallback branch 2 times, most recently from dcafebe to 97a8131 Compare December 2, 2026 19:04
@jcwal1516
jcwal1516 force-pushed the stage/16-metal-tiled-operations branch from 8a97b7f to f885233 Compare December 2, 2026 19:04
…rough the hooks

When the Metal kernels rejected the code blocks of one group of a tiled
decode (for example a block with more coding passes than its bit planes
allow, from a damaged packet header), the grouped route gave up on the
whole image, which then decoded through the native decoder's hooks. That
route holds every component of the image on the host, so a large image
failed with AllocationTooLarge (native decoder context retained
components, 512 MiB cap) where the CPU decodes it: the malformed-input
harness found it on 9472 x 9472 12-bit RGB images in 1024 x 1024 tiles.

Decode only the rejected group's rectangle through the hooks, on the
(reduced) image the groups are planned on, and copy it into the output;
the other groups keep the grouped route. A caller's destination still
takes only the direct route, so decode_into_metal returns
MetalDirectFallback as documented.

tiled_decodes_take_the_hooks_for_a_rejected_group_alone makes one group
of full, region, reduced and RGBA8 requests fail as rejected through a
test hook and checks the pixels and that only that group took the hooks;
it failed before (the whole image took them). The ignored
tiled_decode_over_the_native_limit_takes_the_hooks_for_a_rejected_group_alone
decodes such an image over the native limit; it failed with
AllocationTooLarge. tiled_decode_into_metal_of_a_rejected_group_is_a_direct_fallback
pins the destination behaviour.
14bd0040 and the tile-group fix added two ignored correctness tests, a tiled decode whose output is over 512 MiB and one over the native decoder's working-memory limit, without listing them, so release-metal's ignored-test inventory check failed. Both are now required, so release-metal runs them.
@jcwal1516
jcwal1516 force-pushed the stage/16-metal-tiled-operations branch from f885233 to eac5193 Compare December 2, 2026 19:06
@jcwal1516
jcwal1516 force-pushed the stage/17-metal-tiled-fallback branch from 97a8131 to 0c66df0 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 removed this pull request from stack #390 December 2, 2026 23:48
Base automatically changed from stage/16-metal-tiled-operations to main December 2, 2026 23:48
@jcwal1516
jcwal1516 marked this pull request as ready for review December 2, 2026 23:56
@jcwal1516
jcwal1516 merged commit 5f7ced2 into main Dec 2, 2026
43 of 62 checks passed
@jcwal1516
jcwal1516 deleted the stage/17-metal-tiled-fallback branch December 2, 2026 23:56
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