Skip to content

🚨 Speed up detr image processing - #48066

Open
guarin wants to merge 10 commits into
huggingface:mainfrom
guarin:speed-up-detr-image-preprocessing
Open

guarin wants to merge 10 commits into
huggingface:mainfrom
guarin:speed-up-detr-image-preprocessing

Conversation

@guarin

@guarin guarin commented Aug 18, 2026

Copy link
Copy Markdown
Member

CPU CI GPU run-slow

What does this PR do?

Speeds up preprocessing on GPU for all detr models by using group_images_by_shape. With batch size 32 and ragged inputs between 512px to 2048px I got 27-38% speed up on an H100:

model baseline step ms speedup-branch step ms improvement
rtdetrv2 20.84 15.23 27% faster
rf-detr 25.85 16.37 37% faster
detr-panoptic 28.19 17.94 36% faster
conditional-detr 28.06 17.93 36% faster
dab-detr 28.77 17.97 38% faster
deformable-detr 28.16 18.01 36% faster
lw-detr 22.68 16.26 28% faster

Other minor changes:

Looks like a lot of changes but only detr, rt-detr, and rf-detr changed. The rest is auto-generated.

cc @molbap @zucchini-nlp

@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.

@guarin

guarin commented Aug 18, 2026

Copy link
Copy Markdown
Member Author

run-slow: conditional_detr, deformable_detr, detr, grounding_dino, rf_detr, rt_detr, yolos

@github-actions

Copy link
Copy Markdown
Contributor

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs:

models: ["models/conditional_detr", "models/deformable_detr", "models/detr", "models/grounding_dino", "models/rf_detr", "models/rt_detr", "models/yolos"]
quantizations: []

@github-actions

Copy link
Copy Markdown
Contributor

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN 9806cd79 workflow commit (merge commit)
PR 1f359f14 branch commit (from PR)
main f9b76f2d base commit (on main)

Model CI Report

2 new failed tests from this PR 😭

  • rf_detr:
    tests/models/rf_detr/test_modeling_rf_detr.py::RfDetrModelIntegrationTest::test_inference_object_detection (✅ ⟹ ❌)
    tests/models/rf_detr/test_modeling_rf_detr.py::RfDetrModelIntegrationTest::test_inference_segmentation (✅ ⟹ ❌)

@guarin

guarin commented Aug 19, 2026

Copy link
Copy Markdown
Member Author

run-slow: rf_detr

Comment thread tests/models/rf_detr/test_modeling_rf_detr.py
Comment on lines -469 to +470
("cuda", (8, 0)): [0.959521, 0.931229, 0.897797, 0.7286, 0.672863],
("xpu", None): [0.959521, 0.931229, 0.897797, 0.7286, 0.672863],
("cuda", (8, 0)): [0.960207, 0.934029, 0.896868, 0.718928, 0.707891],
("cpu", None): [0.960285, 0.934246, 0.896883, 0.718877, 0.707271],

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regenerated because of antialiasing change in preprocessing

@github-actions

Copy link
Copy Markdown
Contributor

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs:

models: ["models/rf_detr"]
quantizations: []

@github-actions

Copy link
Copy Markdown
Contributor

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN de878630 workflow commit (merge commit)
PR 8c1bf95b branch commit (from PR)
main e7e8b7fa base commit (on main)

✅ No failing test specific to this PR 🎉 👏 !

@zucchini-nlp zucchini-nlp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's great to see grouped iter (could be an mlinter rule 🤔 ). I am seeing a breaking changes, and not sure why are they added, left comments 👇🏻

Comment thread src/transformers/models/conditional_detr/image_processing_conditional_detr.py Outdated
annotations = reorder_images(grouped_annotations, grouped_images_index)
encoded_inputs["labels"] = [
BatchFeature(annotation, tensor_type=return_tensors) for annotation in annotations
]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the diff here from DetrImageProcessor._preprocess?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rfdetr does rescale then resize while detr does resize then rescale

Comment on lines +1040 to +1041
if paired_list is None
else {key: [item] for key, item in _iterate_items(paired_list, is_nested)}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why we had to return a list[item] instead of unsqueezing a new dim?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because with disable_grouping==False we always returned a list instead of unsqueeze. The reason is that paired values are not necessarily tensors.

)
image = resized_image
orig_size = stacked_images.shape[-2:]
stacked_images = self.resize(stacked_images, size=size, resample=resample, antialias=False)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is breaking, why we need to change antialias=False?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oke, can we mark PR as 🚨

Comment thread tests/models/rf_detr/test_modeling_rf_detr.py
@guarin guarin changed the title Speed up detr image processing 🚨 Speed up detr image processing Aug 24, 2026

@zucchini-nlp zucchini-nlp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, lets run slow tests for rf_detr before merging

Comment on lines +793 to -781
grouped_annotations[key] = stacked_annotations
processed_images = reorder_images(grouped_images, grouped_images_index)

if do_pad:
# depends on all resized image shapes so we need another loop
if pad_size is not None:
padded_size = (pad_size.height, pad_size.width)
else:
padded_size = get_max_height_width(images)

padded_images = []
padded_annotations = []
for image, annotation in zip(images, annotations if annotations is not None else [None] * len(images)):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do_pad can go under the same loop since we're reusing the rescaled images without "reorder then group again" code in between

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly not possible because of padded_size = get_max_height_width(images) which requires the first loop to be complete.

@guarin

guarin commented Sep 15, 2026

Copy link
Copy Markdown
Member Author

run-slow: conditional_detr, deformable_detr, detr, grounding_dino, rf_detr, rt_detr, yolos

@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: conditional_detr, deformable_detr, detr, grounding_dino, rf_detr, rt_detr, yolos

@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 34983156306:1
Result: success | Jobs: 16 | Tests: 96,401 | Failures: 0 | Duration: 9h 46m

@molbap molbap left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went through it! sounds good to me as well. Let's merge soon? maybe a quick core eye from @vasqu ?

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo just one design question but if you feel like it's not really appropriate feel free to ignore

Comment thread src/transformers/models/detr/image_processing_detr.py
Comment thread src/transformers/models/detr/image_processing_detr.py
Comment thread tests/models/rf_detr/test_modeling_rf_detr.py
@guarin

guarin commented Sep 21, 2026

Copy link
Copy Markdown
Member Author

run-slow: conditional_detr

@github-actions

Copy link
Copy Markdown
Contributor

Nvidia CI

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs on Nvidia:

models: ["models/conditional_detr"]
quantizations: []

@github-actions

Copy link
Copy Markdown
Contributor

AMD CI

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs on AMD:

models: ["models/conditional_detr"]

@github-actions

Copy link
Copy Markdown
Contributor

CI Results (AMD)

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN f3b86ec1 workflow commit (merge commit)
PR 50c18824 branch commit (from PR)
main d67c7293 base commit (on main)

✅ No failing test specific to this PR 🎉 👏 !

@github-actions

Copy link
Copy Markdown
Contributor

CI Results (Nvidia)

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN f3b86ec1 workflow commit (merge commit)
PR 50c18824 branch commit (from PR)
main d67c7293 base commit (on main)

✅ No failing test specific to this PR 🎉 👏 !

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.

5 participants