Skip to content

fix(gpu): re-measure Auto routing and keep unsupported inputs on CPU - #376

Merged
jcwal1516 merged 7 commits into
stage/13-crate-licensesfrom
stage/14-gpu-auto-routing
Dec 2, 2026
Merged

jcwal1516 merged 7 commits into
stage/13-crate-licensesfrom
stage/14-gpu-auto-routing

Conversation

@jcwal1516

@jcwal1516 jcwal1516 commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

What and why

The CUDA and Metal Auto thresholds predated the current CPU kernels and
Metal plan cache. They sent several shapes to a GPU after the CPU had become
faster.

This layer records fresh routing evidence, regenerates the CUDA and Metal
promotion tables, keeps CUDA requests whose components differ in sampling on
the CPU under Auto, and keeps explicit device requests strict. It also makes a
manifest with no qualifying lossy Metal encode representable and gates Metal
evidence constants off non-macOS builds.

This layer is based on the license-packaging PR below it.

Impact

  • Changes decoded, encoded, or transcoded output bytes
  • Changes a public API or behavior callers can see (if breaking, add ! to the title)
    Auto may choose the CPU for shapes where the GPU is slower or unsupported.
  • Changes how untrusted input is parsed, or how much memory or time it can use
  • Changes performance (command, hardware, and numbers under Testing)
    The generated routing tables use the measured CUDA and Metal crossover cells.
  • Adds a test image or fixture (small, license allows committing it, no patient
    data, source noted in a README and NOTICES.md)

Testing

  • Saved evidence covers an RTX 4070 SUPER and an Apple M4. The CUDA confirmation
    at 533cbba7 kept all 58 route decisions; Metal thresholds were updated for
    the newer CPU and cache code.

  • Generated-table, manifest, routed-shape, and subsampled-Auto tests are
    included.

  • cargo xtask promotion-codegen, 16 CUDA routing tests, and 37 Metal routing
    tests passed; two Metal runtime tests remain for hardware validation.

  • The focused mixed-sampling metadata and Auto CPU-fallback tests passed.
    Uniform shared-sampling support remains with the later component-grid layer.

  • Exact-head CUDA and Metal routing validation: pending.

  • Bug fix: added tests that fail with stale or unsupported routing

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

The CUDA routing cells dated from August and sent classic images as
small as 256 x 149 to CUDA, which is now slower than the CPU for them.
They were re-measured at 0c61cb2 on an RTX 4070 SUPER with Part 1
(openjpeg-data) and Part 15 (T.803) inputs. A single classic image now
goes to CUDA only from 2592 x 1944 (RGB8 lossless from 640 x 480), and
a region or half-size decode only from a 1296 x 972 output. Batches of
one repeated image and the HTJ2K and JPH routes still go to CUDA. CUDA
encode stayed slower than the CPU everywhere.

Updates xtask/routing-promotion-evidence.json, the generated table, the
tests that pin routed shapes, book/performance.md and the corpus recipe
in contributor-book/benchmarks.md.

Provisional: the measurements predate the performance commits before
this one. CUDA and Metal routing still need re-measuring at a commit
that includes them, and these cells, tests and numbers redoing.

(cherry picked from commit 517b13bc3d13e8e5eb5f1a37746708259eafc197)
…hold

The Metal lossy RGB8 encode gate required a nonzero pixel threshold, so a
measurement in which no lossy encode was faster on Metal could not be
recorded. `metal_lossy_rgb8` may now be `null`; the key stays required,
and the generated `auto_lossy_rgb8_encode_qualifies` then returns false.

`HOST_OUTPUT_CELLS` is now rendered the way rustfmt lays it out at every
length: on one line, wrapped after `=`, or one cell per line. Before, a
table of one or two cells failed `cargo fmt --check`.

(cherry picked from commit 2b4705392e41bd916d8667d5de22ab4280b3f331)
The Metal cells came from September runs on an M4 Pro, before the CPU
speedups and the faster Metal plan cache. They were re-measured at
57779fc2 on a Mac mini (Apple M4, 10 GPU cores) with 49 inputs built from
two openjpeg-data photographs: 175 cells, 46 faster on Metal by the
verifier's rule. Artifact 6f2b9815, manifest 64178573.

- Single images go to Metal from 1024 x 1024 for lossless RGB8 (HTJ2K or
  Part 1) and at 2048 x 2048 for lossy RGB8 and Gray8 HTJ2K, instead of
  HTJ2K from 640 x 480, which is now slower on this GPU.
- Half-size RGB8 decodes go to Metal from a 1024 x 1024 output for HTJ2K
  and Part 1 lossless; before, none did.
- Repeated RGB8 batches go to Metal from 256 x 256 for every kind. Gray8
  HTJ2K batches need 2048 x 2048 and Part 1 lossless 1024 x 1024; Part 1
  lossy gray batches stay on the CPU.
- Lossless HTJ2K host-output encode goes to Metal only at 2048 x 2048, and
  no lossy encode does: Metal took 2.5 to 21 times as long.

Updates the tests that pin routed shapes, book/performance.md, book/gpu.md,
the changelog, and the Metal corpus recipe in contributor-book/benchmarks.md,
which now also says how each backend runs the repeated-input batch cell.

(cherry picked from commit 1a50c5350cdf7a63d7f1586d2d499d941b84118f)
The CUDA cells from 904c2ed6 were measured at 0c61cb2, before the CPU
and Metal performance commits. Re-measured at 533cbba7 on the same RTX
4070 SUPER with the same Part 1 and Part 15 manifests, the verifier
reached the same decision for every one of the 58 cells: 28 decode cells
go to CUDA and every encode stays on the CPU. Only the artifact hashes
change (part1 6e522137, part15 fbca5ff2), so the generated table differs
in those two constants and no routed shape or test changes.

book/performance.md now quotes the new numbers and says that the CUDA
batch cell plans, uploads, and decodes all 16 copies, while the CPU
decodes them one after another.
The scheduler test hooks, the cache digest helpers, and the Metal promotion
evidence constants are only read by macOS code and macOS-only tests, so gate
them on `target_os = "macos"` like the code that uses them. The promotion
codegen now writes that gate on the Metal evidence while no lossy RGB8
threshold reads it on other targets.
Auto sends a decode to CUDA when its shape matches a measured routing
cell. The decision never looked at component sampling, and CUDA's direct
plans decline every component that isn't sampled (1, 1). So an RGB8 image
with subsampled chroma that matched a cell, for example a lossless
704x512 one, went to CUDA and failed with the native planner's
unsupported error. Auto has no CPU retry after it picks a device, so the
caller got an error for an image the CPU decodes.

The decoder now records whether any component is subsampled, from the
parsed support metadata, and Auto keeps such images on the CPU before any
CUDA work, for single decodes and for the repeated tile batch. That also
holds for images whose components all share one sampling: no routing
measurement covers them. An explicit CUDA request is unchanged.

Tests: the_decoder_knows_whether_any_component_is_subsampled runs without
a device. auto_keeps_subsampled_images_on_the_cpu decodes a mixed-sampling
and a shared-sampling RGB8 image of routed sizes under Auto and expects a
CPU surface with the CPU decode's samples, and with a device expects the
explicit CUDA request for the mixed image to be an unsupported error; on a
machine without CUDA its Auto half passes before and after, so it needs
the PC to show the failure this fixes.
@jcwal1516
jcwal1516 added this pull request to stack #368 December 2, 2026 03:30
@jcwal1516 jcwal1516 changed the title stage/14 gpu auto routing fix(gpu): re-measure Auto routing and keep unsupported inputs on CPU Dec 2, 2026
@jcwal1516
jcwal1516 force-pushed the stage/14-gpu-auto-routing branch 2 times, most recently from 5d7c1c9 to d92d44a Compare December 2, 2026 19:04
@jcwal1516
jcwal1516 marked this pull request as ready for review December 2, 2026 19:08
@jcwal1516
jcwal1516 removed this pull request from stack #368 December 2, 2026 20:01
@jcwal1516
jcwal1516 added this pull request to stack #386 December 2, 2026 20:01
@jcwal1516
jcwal1516 merged commit e8f8555 into main Dec 2, 2026
57 of 71 checks passed
@jcwal1516
jcwal1516 deleted the stage/14-gpu-auto-routing branch December 2, 2026 20:01
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