Skip to content

Modernize ConvNextV2, ResNet, RegNet, RTDetrResnet, SwinV2, Swin2SR - #48378

Open
sbucaille wants to merge 39 commits into
huggingface:mainfrom
sbucaille:refactor/modernize-cnn-backbones
Open

sbucaille wants to merge 39 commits into
huggingface:mainfrom
sbucaille:refactor/modernize-cnn-backbones

Conversation

@sbucaille

@sbucaille sbucaille commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

🤗 Transformers CI

What does this PR do?

Modernize several vision arch to last library standards : ConvNextV2, ResNet, RegNet, RTDetrResnet, SwinV2, Swin2SR
Moved to modular and save overall approx. 1600 LoC

I stopped there because it would end up being a big PR but I can continue onto other arch in the same PR if you want (such as DETR archs)

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline and the
    Pull Request checks?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes according to the guidelines?
  • Did you write any new necessary tests?

Who can review?

@molbap @guarin

🤖 mlinter review state

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Model linter — first pass

transformers-mlinter found 1 item(s) in the model files this PR touches. These are structural conventions a maintainer would otherwise flag by hand.

This is automated and advisory — it does not block merging.

rule count what it checks
TRF013 1 PreTrainedModel init must call self.post_init().

@molbap
molbap self-requested a review August 28, 2026 09:29
@sbucaille

Copy link
Copy Markdown
Contributor Author

@molbap I added some low hanging fruits archs to the branch as well : efficientnet, poolformer, mobilenet v1 and v2, mobilevit v1 and v2

@sbucaille
sbucaille force-pushed the refactor/modernize-cnn-backbones branch from e5ab17b to 5b6ee2a Compare September 16, 2026 19:22

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Model linter — first pass

transformers-mlinter found 5 item(s) in the model files this PR touches. These are structural conventions a maintainer would otherwise flag by hand.

This is automated and advisory — it does not block merging.

rule count what it checks
TRF040 5 @can_return_tuple must not be combined with @capture_outputs.

# Initialize weights and apply final processing
self.post_init()

@can_return_tuple

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

TRF040 — TRF040: forward combines @can_return_tuple with @capture_outputs. @capture_outputs already handles returning tuples, drop @can_return_tuple.

Why this matters

Both decorators pop return_dict, so only the outermost one sees the true value. @capture_outputs already handles the to_tuple conversion, making @can_return_tuple redundant.

-@can_return_tuple
 @merge_with_config_defaults
 @capture_outputs
 @auto_docstring
 def forward(self, x):
     return AcmeModelOutput(last_hidden_state=x)

Suppress with # trf-ignore: TRF040 if intentional.

# Initialize weights and apply final processing
self.post_init()

@can_return_tuple

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

TRF040 — TRF040: forward combines @can_return_tuple with @capture_outputs. @capture_outputs already handles returning tuples, drop @can_return_tuple.

Why this matters

Both decorators pop return_dict, so only the outermost one sees the true value. @capture_outputs already handles the to_tuple conversion, making @can_return_tuple redundant.

-@can_return_tuple
 @merge_with_config_defaults
 @capture_outputs
 @auto_docstring
 def forward(self, x):
     return AcmeModelOutput(last_hidden_state=x)

Suppress with # trf-ignore: TRF040 if intentional.

# Initialize weights and apply final processing
self.post_init()

@can_return_tuple

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

TRF040 — TRF040: forward combines @can_return_tuple with @capture_outputs. @capture_outputs already handles returning tuples, drop @can_return_tuple.

Why this matters

Both decorators pop return_dict, so only the outermost one sees the true value. @capture_outputs already handles the to_tuple conversion, making @can_return_tuple redundant.

-@can_return_tuple
 @merge_with_config_defaults
 @capture_outputs
 @auto_docstring
 def forward(self, x):
     return AcmeModelOutput(last_hidden_state=x)

Suppress with # trf-ignore: TRF040 if intentional.

# Initialize weights and apply final processing
self.post_init()

@can_return_tuple

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

TRF040 — TRF040: forward combines @can_return_tuple with @capture_outputs. @capture_outputs already handles returning tuples, drop @can_return_tuple.

Why this matters

Both decorators pop return_dict, so only the outermost one sees the true value. @capture_outputs already handles the to_tuple conversion, making @can_return_tuple redundant.

-@can_return_tuple
 @merge_with_config_defaults
 @capture_outputs
 @auto_docstring
 def forward(self, x):
     return AcmeModelOutput(last_hidden_state=x)

Suppress with # trf-ignore: TRF040 if intentional.

# Initialize weights and apply final processing
self.post_init()

@can_return_tuple

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

TRF040 — TRF040: forward combines @can_return_tuple with @capture_outputs. @capture_outputs already handles returning tuples, drop @can_return_tuple.

Why this matters

Both decorators pop return_dict, so only the outermost one sees the true value. @capture_outputs already handles the to_tuple conversion, making @can_return_tuple redundant.

-@can_return_tuple
 @merge_with_config_defaults
 @capture_outputs
 @auto_docstring
 def forward(self, x):
     return AcmeModelOutput(last_hidden_state=x)

Suppress with # trf-ignore: TRF040 if intentional.

@sbucaille
sbucaille force-pushed the refactor/modernize-cnn-backbones branch from 55f3c16 to db0acbd Compare September 16, 2026 22:10
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution 🤗!

CI Security Gate — automatic approval blocked

This PR was not automatically approved for CI because the security gate failed.

Possible reasons:

  • The PR touches 50 or more files — only PRs with fewer than 50 changed files are automatically approved
  • A changed file is outside the allowed directories (src/, tests/, docs/, utils/), has a disallowed extension (only .py, .txt, .md permitted outside tests/ and docs/), or is not .md/.yml inside docs/ — this covers files the PR deletes or renames, not only the ones it edits
  • A new high-severity security issue was detected in the changed Python files (Bandit check)
  • The PR touches a path this repository protects from untrusted PRs, such as the file that decides who reviews it — a maintainer must make that change in a separate PR

See the workflow run for the exact violations.

A maintainer can review and manually approve CI if a finding is a false positive.

@sbucaille
sbucaille force-pushed the refactor/modernize-cnn-backbones branch from db0acbd to 470689f Compare September 16, 2026 22:28
@sbucaille

Copy link
Copy Markdown
Contributor Author

@molbap bumping the PR with small changes. Tests not passing are not relevant to the PR

sbucaille and others added 21 commits September 17, 2026 13:22
Move ResNetEncoder to PreTrainedModel, record stage hidden states via
_can_record_outputs, and drop manual return_dict / hidden-state loops.
Add modular_regnet as source of truth, generate modeling_regnet, and drop
Copied-from blocks in favor of modern capture_outputs patterns.
Add modular_rt_detr_resnet, keep base_model_prefix=resnet for checkpoint BC,
and modernize backbone forward while preserving RT-DETR stem/shortcut rules.
Add modular_swinv2 with Swin inheritance, cosine attention via attention
interface, SDPA support, and regenerate modeling_swinv2.
Emit modern attention/mlp keys from the original conversion script.
Add modular_swin2sr reusing the SwinV2 attention stack, RSTB stages,
prefixed upsamplers, and regenerate modeling_swin2sr.
Align the original convert script and add swinv2/swin2sr WeightRenaming
entries for Bert-style attention and MLP keys.
Add headdim helper, flash-dispatch skip, greyscale classifier coverage,
and loosen fp16 integration tolerance.
Add headdim config helper and skip flash dispatch for dense CRPB masks.
Collapse duplicate SwinBackbone guards in check_hidden_states_output.
- drop unused torch_int import in modular_swinv2
- ruff-format convert_swin2sr qkv split
- regenerate regnet/rt_detr encoders with post_init from resnet
- restore convnextv2 _init_weights skip comment from modular
align encoder/model/backbone with capture_outputs and can_return_tuple
use can_return_tuple and kwargs while keeping manual encoder hidden states
introduce PoolFormerStage, capture-based encoder hs, and weight key mapping
use can_return_tuple and kwargs while keeping manual hidden states length 26
use can_return_tuple and kwargs while preserving segmentation last-hs path
use can_return_tuple and kwargs while preserving segmentation last-hs path
thin max-inherit modular source and drop copied-from generated modeling
`nn.DataParallel` cannot replicate or gather the forward patterns and
output types modern Transformers models use, so this common test measures
PyTorch's legacy DP path rather than model correctness.

DataParallel is a Trainer feature, not a per-model contract, and it keeps
its coverage there (tests/trainer/test_trainer.py). This removes the common
test and all 66 overrides; no `src/` behavior changes.
@sbucaille
sbucaille force-pushed the refactor/modernize-cnn-backbones branch from 7f84e1e to ec60bee Compare September 18, 2026 15:05
@sbucaille

Copy link
Copy Markdown
Contributor Author

@molbap bumping this here, I merged with main to update the branch, failures seem to come from main

@guarin

guarin commented Oct 13, 2026

Copy link
Copy Markdown
Member

Thanks, will have a look!

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

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

run-slow: bit, convnextv2, efficientnet, mobilenet_v1, mobilenet_v2, mobilevit, mobilevitv2, poolformer, regnet, resnet, rt_detr, swin, swin2sr, swinv2

This branch has not been deployed

No deployments
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.

3 participants