[WIP] Support custom kernels for processing ops#46771
Open
molbap wants to merge 5 commits into
Open
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Contributor
|
CI Dashboard: View test results in Grafana |
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 does this PR do?
TL;DR Preprocessing and postprocessing ops can be offloaded to Hub kernels the same way model layers currently are. This PR allows this and goes with a first simple kernel that is common to many image processors.
use_kernels=Trueswaps a processor op for a registered Hub kernel and falls back to the default implementation when none applies. So custom or hard-to-vectorize ops (beyond resize or normalize: patchify, NMS, mask decoding, ...) can move onto the GPU without rewriting the processor or the call site.However the adapter is uglier than for modeling components, mainly because there's no same-signature forward to swap and no kernelize pass over processors. Open to ideas here!
I pushed a fast image processors' resize + normalize that is automatic for processors using the base fast-processing path others fall back and can opt in later. The kernel batches the resize over a ragged set of images and folds rescale and normalize into the same pass.
For vision-language models the image processor is wrapped in a processor, so the flag passes through [
AutoProcessor] to the inner image processor.Some benchmarks for processors of vision models showing the speedup:
processor() resize+normalize, use_kernels off -> on: siglip2-base-patch16-224 3.92 -> 1.30 ms 3.0x pixΔ 3.9e-3 paligemma2-3b-pt-224 4.26 -> 1.36 ms 3.1x pixΔ 3.9e-3 clip-vit-base-patch32 7.30 -> 1.41 ms 5.2x pixΔ 1.8e-2 dinov2-small 7.26 -> 1.42 ms 5.1x pixΔ 2.5e-1 standalone resize stage (siglip-so400m, bicubic+AA): torchvision eager loop 2.95 ms torch.compile (dynamic) 5.65 ms (slower; ~2.6s warmup) fused triton (2D) 11.53 ms separable triton 1.37 ms -> 2.9x the real processor (3.96 ms)Need to move the kernel from
Molbap/<kernel_name>and open this to the community. Feedback welcome :)