KEMBAR78
[modular] Classes can now be defined and referenced in arbitrary order (without bringing unwanted dependencies) by Cyrilvallez · Pull Request #40507 · huggingface/transformers · GitHub
Skip to content

Conversation

@Cyrilvallez
Copy link
Member

@Cyrilvallez Cyrilvallez commented Aug 27, 2025

What does this PR do?

As per the title. Until now, if a class was ever referenced in the modular file BEFORE being redefined in it later on in the file, it could match a class from another modeling file, thus adding wrong dependencies to the generated files.
This PR fixes it, by making sure all classes redefined in modular are removed from the dependency graph of another class if present (they will be added later on with their own dependencies)

So the order of class definition and class reference in modular files can now be arbitrary 🎉(even though to keep clean code, we always want classes to be defined before they are used somewhere, so that someone can read the file "naturally", it is sometimes not practical)

As an example, consider modular_modernbert_decoder which is being modified, where I previously had to use a check on string name instead of isinstance(class) because the 2 classes are defined afterwards in modular, and would thus bring erroneous dependencies.
A few other models had classes at the wrong location due to it, or even some classes that were imported but never used anywhere
This is now all fixed

@Cyrilvallez Cyrilvallez changed the title [modular] Remove future classes from dependency graph [modular] Classes can now be referenced before their definition without bringing erroneous dependencies Aug 27, 2025
@HuggingFaceDocBuilderDev

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
Contributor

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

run-slow: d_fine, instructblipvideo, llava_onevision, metaclip_2, mm_grounding_dino, modernbert_decoder, rt_detr_v2, sam_hq, siglip2

@Cyrilvallez Cyrilvallez changed the title [modular] Classes can now be referenced before their definition without bringing erroneous dependencies [modular] Classes can now be defined and referenced in arbitrary order (without bringing unwanted dependencies) Aug 27, 2025
@Cyrilvallez Cyrilvallez merged commit 98289c5 into main Aug 27, 2025
19 checks passed
@Cyrilvallez Cyrilvallez deleted the modular-lookahead branch August 27, 2025 21:06
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