Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load sub-configs from composite configs #34410

Merged
merged 14 commits into from
Nov 5, 2024

Conversation

zucchini-nlp
Copy link
Member

What does this PR do?

This PR removed overwritten from_pretrained in many configs that are part of a bigger config. Instead it moved the loading logic to the base config class through base_config_key. Additionally it adds a few more flags to the config, in a similar fashion to processors where each sub-config's class name is recorded. Currently those are needed only for tests to load the sub-config directly and compare with base_config's value

Weird thing is that config._is_composition is not an indicator of whether the config is composite or not. Several configs were missing the flag and operating good, because the flag is used only to separate out configs that can be init without params and those that require params. Those that require are flagged as is_composition, see more in #25237 description and in #7943 which introduced the flag.

I personally see only a few models that require input params (other configs) which are indeed composite, and all others don't need the flag so I removed it. But still I think the naming is quite misleading so maybe we should rename that in long-term or even remove that and find a better way to save configs when default params are overwritten.

@zucchini-nlp
Copy link
Member Author

zucchini-nlp commented Oct 25, 2024

Modular is trying to overwrite InstructBlip and remove logger, even though we do log.warning in configuration code, so I have no idea why. Same thing is happening in my other PR for porting Emu3

Other failing tests have no relation to the current PR

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

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

Very very nice! 🤗 Let's switch to a dict, always test and should be good to go

src/transformers/models/blip_2/configuration_blip_2.py Outdated Show resolved Hide resolved
tests/test_configuration_common.py Outdated Show resolved Hide resolved
src/transformers/configuration_utils.py Outdated Show resolved Hide resolved
@@ -110,6 +115,34 @@ def create_and_test_config_from_and_save_pretrained_subfolder(self):

self.parent.assertEqual(config_second.to_dict(), config_first.to_dict())

def create_and_test_config_from_and_save_pretrained_composite(self):
config = self.config_class(**self.inputs_dict)
Copy link
Collaborator

Choose a reason for hiding this comment

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

okay, as long as the attributes here are different than default we are testing good stuff

tests/models/x_clip/test_modeling_x_clip.py Outdated Show resolved Hide resolved
Comment on lines 106 to 109
sub_configs = ["text_config", "qformer_config", "vision_config"]
text_config_class = "AutoConfig"
qformer_config_class = "InstructBlipVideoQFormerConfig"
vision_config_class = "InstructBlipVideoVisionConfig"
Copy link
Contributor

Choose a reason for hiding this comment

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

Hi! Super nice work 🔥 I was wondering about this, for composite modular-based new models, could we add something in the converter to enforce this structure?

Copy link
Member Author

@zucchini-nlp zucchini-nlp Oct 28, 2024

Choose a reason for hiding this comment

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

Sounds good having it automated, I'll try to enable it through this PR if I can find a way. But I am wondering how would we automatically know which class does each sub_config map to if no info is given except for the code. Hmm, should we parse the code body then

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe with AST? we have typed args for the configs that inform of which config goes where, too

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, seems to be doable if you enforce config docstring to contain the sub-config class in type-hinting. Maybe we'll add it as the next PR so we can take time for making modular better. Also I've another PR on improving modular in general, might as well go there as a new feature

Copy link
Member Author

Choose a reason for hiding this comment

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

cc @Cyrilvallez since you are working closely with modular. Do you think this is smth we can add in your PR or maybe subsequent PRs?

Copy link
Member

Choose a reason for hiding this comment

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

Hey, sorry for the late reply! This is definitely do-able - it would require parsing the __init__ to check subconfig-specific assignments and add them as class attributes in the body.
However, it breaks a bit the "no magic" policy of the modular, as it means we automatically add stuff based on simple naming conventions.

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

Very very nice!
Thanks for iterating!

Comment on lines +1600 to +1607
for key in config.sub_configs.keys():
sub_config = getattr(config, key)
curr_attn_implementation = (
requested_attn_implementation
if not isinstance(requested_attn_implementation, dict)
else requested_attn_implementation.get(key, None)
)
sub_config._attn_implementation_internal = curr_attn_implementation
Copy link
Collaborator

Choose a reason for hiding this comment

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

vey nice!

@zucchini-nlp zucchini-nlp merged commit 893ad04 into huggingface:main Nov 5, 2024
26 checks passed
MagnusS0 added a commit to MagnusS0/transformers that referenced this pull request Nov 20, 2024
…SAM model tests

- Added attributes for sub_configs as per PR huggingface#34410.
- Enabled tests for configs, ensuring the composite model (SAM) has several sub-configs in the main config.
- Added class attribute _is_composite=True to the tester class
- test_sdpa_can_dispatch_composite_models added
BernardZach pushed a commit to BernardZach/transformers that referenced this pull request Dec 5, 2024
* save/load sub-configs

* nit forgot these

* fix copies

* move test to common

* use dict for sub-configs

* add load-save-laod test

* clean up modeling check

* oops this are correct keys

* fix some tests, missed some composite configs

* this model was missed
ArthurZucker pushed a commit that referenced this pull request Dec 17, 2024
* feat: add support for sdpa and gradient checkpointing

* fix: ruff format

* fix: config sdpa

* fix: sdpa layer naming convention

* fix: update test_eager_matches_sdpa_inference to handle vision_hidden_states

* test: skip incompatible tests and fix loading issue with sdpa

- Updated tests to skip cases flash and dynamic compile.
- Minor adjustment to ensure correct loading of model with sdpa for dispatch test.

* style: apply Ruff formatting

* ruff fix again after rebase

* [run-slow] sam

* [run-slow] sam

* refactor: Address review comments and improve sub-config handling in SAM model tests

- Added attributes for sub_configs as per PR #34410.
- Enabled tests for configs, ensuring the composite model (SAM) has several sub-configs in the main config.
- Added class attribute _is_composite=True to the tester class
- test_sdpa_can_dispatch_composite_models added

* [run-slow] sam

* style: ruff

* [run-slow] sam

* style: ruff again ...

* [run-slow] sam
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