Skip to content

[WIP] Support custom kernels for processing ops#46771

Open
molbap wants to merge 5 commits into
mainfrom
processing_kernels
Open

[WIP] Support custom kernels for processing ops#46771
molbap wants to merge 5 commits into
mainfrom
processing_kernels

Conversation

@molbap

@molbap molbap commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator

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=True swaps 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.

from transformers import AutoImageProcessor

processor = AutoImageProcessor.from_pretrained("google/siglip2-base-patch16-224", use_kernels=True)
pixel_values = processor(images, return_tensors="pt")["pixel_values"]

For vision-language models the image processor is wrapped in a processor, so the flag passes through [AutoProcessor] to the inner image processor.

from transformers import AutoProcessor

processor = AutoProcessor.from_pretrained("google/gemma-3-4b-it", use_kernels=True)
inputs = processor(text=prompt, images=images, return_tensors="pt")

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 :)

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

@github-actions

Copy link
Copy Markdown
Contributor

CI Dashboard: View test results in Grafana

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.

2 participants