Skip to content

Fix RT-DETR indexing error when num_feature_levels exceeds backbone o…#46833

Open
c1prk wants to merge 3 commits into
huggingface:mainfrom
c1prk:fix/rt-detr-num-feature-levels-indexing
Open

Fix RT-DETR indexing error when num_feature_levels exceeds backbone o…#46833
c1prk wants to merge 3 commits into
huggingface:mainfrom
c1prk:fix/rt-detr-num-feature-levels-indexing

Conversation

@c1prk

@c1prk c1prk commented Jun 23, 2026

Copy link
Copy Markdown

What does this PR do?

Fixes a TypeError in RTDetrModel.forward that occurs when num_feature_levels is set higher than the number of backbone output levels (default is 3).

When extra decoder feature levels need to be synthesized, the first extra level passed the entire list of encoder feature maps to a Conv2d layer and indexed [-1] on the result, instead of indexing [-1] on the input list first to select a single tensor. The corrected line now matches the pattern already used correctly on the very next line of the same block.

# Before (buggy): indexes the conv2d output, passing a list into conv2d
sources.append(self.decoder_input_proj[_len_sources](encoder_outputs.last_hidden_state)[-1])

# After (correct): indexes the input list first, passing a single tensor
sources.append(self.decoder_input_proj[_len_sources](encoder_outputs.last_hidden_state[-1]))

The change is made in modular_rt_detr.py (the source of truth) and modeling_rt_detr.py is regenerated from it. The default config (num_feature_levels=3) is unaffected, so no existing behavior changes. A regression test was added that builds the model with num_feature_levels=4 and confirms the forward pass returns logits of the expected shape.

AI assistance was used to help diagnose and draft this fix; I reviewed and verified every changed line and reproduced both the bug and the fix myself.

Fixes #46832

Code Agent Policy

  • I confirm that this is not a pure code agent PR.

Before submitting

Test results

python -m pytest tests/models/rt_detr/test_modeling_rt_detr.py -k num_feature_levels -v

(passes locally)

Who can review?

@yonigozlan @molbap

@github-actions

Copy link
Copy Markdown
Contributor

CI Dashboard: View test results in Grafana

@c1prk c1prk marked this pull request as ready for review June 24, 2026 00:05
@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: d_fine, pp_doclayout_v2, rt_detr, rt_detr_v2

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.

[RT-DETR] TypeError in RTDetrModel.forward when num_feature_levels > len(backbone outputs)

1 participant