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

add create_reference_model #61

Merged
merged 5 commits into from
Dec 30, 2022
Merged

add create_reference_model #61

merged 5 commits into from
Dec 30, 2022

Conversation

lvwerra
Copy link
Member

@lvwerra lvwerra commented Dec 29, 2022

This PR adds the create_reference_model function. It can be used to create a static reference model from an existing model:

ref_model = create_reference_model(model)

The reference model can also share layers with the original model:

ref_model = create_reference_model(model, share_layers=3)

In that case the first three layers are frozen for both models and the remaining layers can be updated in the active model.

The layers are identified via string matching of their names. This works for GPT2/BLOOM/OPT/GPT-neo. If a custom pattern is necessary one could use the pattern keyword.

Copy link
Contributor

@younesbelkada younesbelkada left a comment

Choose a reason for hiding this comment

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

Thanks a bunch for working on this 🔥 Can't wait to try it on the trainer! Left few minor comments :D

"""

parameter_names = [n for n, _ in model.named_parameters()]
ref_model = deepcopy(model)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think this can blow up the memory if manipulating very large models?
I was thinking of leveraging accelerate.init_empty_weights context manager and initialize an empty model and populating it step by step. This can be boilerplate a bit so maybe let's leave it in a follow up PR

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good - let's see how it works and maybe adapt it to init_empty_weights in a follow up :)

Co-authored-by: Younes Belkada <[email protected]>
@younesbelkada younesbelkada mentioned this pull request Dec 29, 2022
26 tasks
Copy link
Member

@lewtun lewtun left a comment

Choose a reason for hiding this comment

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

Nice feature @lvwerra 🚀 ! I left some nits and a question about how the indexing of share_layers is counted :)

ref_model = deepcopy(model)

# if no layers are shared, return copy of model
if share_layers is None:
Copy link
Member

Choose a reason for hiding this comment

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

do we need to extend this logic to handle the case when share_layers=0 or does the indexing start with 0?

Copy link
Member Author

Choose a reason for hiding this comment

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

indeed indexing starts at 0 so this should be fine

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Dec 30, 2022

The documentation is not available anymore as the PR was closed or merged.

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.

4 participants