-
Notifications
You must be signed in to change notification settings - Fork 27.6k
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
[model loading] don't init weights for pretrained models #11463
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a huge fan of "attaching" a new parameter to the
config
which is not really understandable by the user.Also, I think this could lead to problems -> lots of people initialize all weights except the final layer weights from a pre-trained BERT in, e.g. a
BertForSequenceClassification
. The logic would then not correctly initialize the final layer, but simply set everything to zero which would probably lead to a worse fine-tuning ofBertForSequenceClassification
.=> I would propose the following:
from_pretrained(...)
, we pass a new parameter tomodel = cls(config, *model_args, **model_kwargs)
by settingmodel_kwargs["init_weights"] = False
. This then sadly means that we have to replace all__init__(self, config)
functions in the modeling files by__init__(self, config, init_weights=True)
, but I think we can use a regex for this. This is a huge change in terms of files that need to be changed, but I think it's cleaner then creating a new"use_pretrained_weights"
config parameter that the user shouldn't have to learn about. Then, we also need to changeself.init_weights()
withself.init_weights()
because it would necessarly run through all modules and initialized them. So I think we should leverage themissing_keys()
list here to extract allnn.Modules(...)
that still need to be initialized and then runself._init_weights(m) for m in uninitialized_modules
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think ? @stas00
Also keen to hear @LysandreJik's and @sgugger's opinion here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
init_weights
kwarg by itself will not work as it doesn't deal with 2. As I said in my comment, the only one to properly deal with this is to pass anuninitalized_weights
kwargs (as done by themissing_keys
) which would then be used:and of course
init_weights
then needs to use a function different thanapply
that only applies_init_weights
to theunitialized_weights
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
init_weights
kwarg on its own won't work, but it's necessary to prevent each model from callingself.init_weights()
.The order of operations when doing
BertModel.from_pretrained(...)
is the following:Instantiate a random model:
cls(config, *model_args, **model_kwargs)
=> this command already callsself.init_weights(...)
(since in every model class we have aself.init_weights(...)
in__init__(config):. So in order to prevent this we need to pass a flag to
cls(config, *model_args, **model_kwargs)which I would do with
model_kwargs["init_weights"] = False`.Only after the model is instantiated (and the weights already have values), we can know which weights were missing & thus need to be randomely initialized. Here we can retrieve
uninitialized_weights
, but it would be better to actually retrieve allnn.Modules
that are randomely initialized since then we can reuse each model's_init_weights(...)
function.Having retrieved
uninitialized_modules
we can runself._init_weights(...)
on each module.