Fix missing initializations for models created in 2022#39772
Conversation
| nn.init.trunc_normal_(module.cls_token, std=std) | ||
| nn.init.trunc_normal_(module.position_embeddings, std=std) |
There was a problem hiding this comment.
| nn.init.trunc_normal_(module.cls_token, std=std) | ||
| nn.init.trunc_normal_(module.position_embeddings, std=std) |
There was a problem hiding this comment.
| if isinstance(module, YolosEmbeddings): | ||
| nn.init.trunc_normal_(module.cls_token, std=std) | ||
| nn.init.trunc_normal_(module.position_embeddings, std=std) | ||
| nn.init.trunc_normal_(module.detection_tokens, std=std) |
There was a problem hiding this comment.
| nn.init.trunc_normal_(module.position_embeddings, std=std) | ||
| nn.init.trunc_normal_(module.detection_tokens, std=std) | ||
| elif isinstance(module, YolosEncoder): | ||
| nn.init.trunc_normal_(module.mid_position_embeddings, std=std) |
There was a problem hiding this comment.
| elif isinstance(module, GroupViTVisionEmbeddings): | ||
| nn.init.trunc_normal_(module.position_embeddings, std=factor * 0.02) |
There was a problem hiding this comment.
| elif isinstance(module, GroupViTStage): | ||
| if module.num_group_token > 0: | ||
| # only zero init group token if we have a projection | ||
| if module.group_projector is not None: | ||
| module.group_token.data.zero_() | ||
| else: | ||
| nn.init.trunc_normal_(module.group_token, std=factor * 0.02) |
| nn.init.normal_(module.position_embedding, std=self.config.initializer_factor) | ||
| nn.init.normal_(module.position_embedding, std=factor * 0.02) |
There was a problem hiding this comment.
| module.weight.data.normal_(mean=0.0, std=self.config.initializer_factor) | ||
| module.weight.data.normal_(mean=0.0, std=factor * 0.02) |
There was a problem hiding this comment.
| elif isinstance(module, VideoMAEForPreTraining): | ||
| module.mask_token.data.normal_(mean=0.0, std=std) |
There was a problem hiding this comment.
|
Hi @Cyrilvallez ! I'm encountering a blocker while trying to fix the missing initializations for the def _init_weights(self, module: nn.Module):
"""Initialize the weights"""
if isinstance(module, Data2VecAudioFeatureProjection):
k = math.sqrt(1 / module.projection.in_features)
nn.init.uniform_(module.projection.weight, a=-k, b=k)
nn.init.uniform_(module.projection.bias, a=-k, b=k)
elif isinstance(module, Data2VecAudioForXVector):
nn.init.normal_(module.objective.weight)
elif isinstance(module, Data2VecAudioPositionalConvLayer):
nn.init.constant_(module.conv.bias, 0)
elif isinstance(module, Data2VecAudioModel):
# model only needs masking vector if mask prob is > 0.0
if self.config.mask_time_prob > 0.0 or self.config.mask_feature_prob > 0.0:
nn.init.uniform_(module.masked_spec_embed)
elif isinstance(module, nn.Linear):
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
if module.bias is not None:
module.bias.data.zero_()
elif isinstance(module, nn.LayerNorm):
if module.bias is not None:
module.bias.data.zero_()
if module.weight is not None:
module.weight.data.fill_(1.0)
elif isinstance(module, nn.Conv1d):
nn.init.kaiming_normal_(module.weight)
if module.bias is not None:
k = math.sqrt(module.groups / (module.in_channels * module.kernel_size[0]))
nn.init.uniform_(module.bias, a=-k, b=k)When generating the modeling file from the modular file, I run into a code verification error during the src/transformers/models/data2vec/modeling_data2vec_audio.py:797:35: F821 Undefined name `Data2VecAudioPreTrainedModel`
|
795 | """
796 | )
797 | class Data2VecAudioForPreTraining(Data2VecAudioPreTrainedModel):
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ F821
798 | def __init__(self, config: Data2VecAudioConfig):
799 | super().__init__(config)
|
Found 1 error.
The error occurs because |
|
Hey @bvantuan! Sorry for the delay! The issue comes from the fact that I'll try to look into it, for now we can just skip this model, or perform the checks in a way which does not use those module name (e.g. if the actual module name is unique, we can simply use |
Cyrilvallez
left a comment
There was a problem hiding this comment.
Hey @bvantuan! Sorry for being late on this! Very happy to see this new batch! 🤗 Still some points to fix though, especially I'm not sure why you added 0.02 factors (apparently) randomly at some places? Did I miss something?
| module.weight.data.normal_(mean=0.0, std=self.config.initializer_factor) | ||
| module.weight.data.normal_(mean=0.0, std=factor * 0.02) |
There was a problem hiding this comment.
Why add a random 0.02 here???
| module.weight.data.normal_(mean=0.0, std=self.config.initializer_factor) | ||
| module.weight.data.normal_(mean=0.0, std=factor * 0.02) |
| fc_std = (2 * self.config.hidden_size) ** -0.5 | ||
| for block in module.transformer.resblocks: | ||
| nn.init.normal_(block.attn.in_proj_weight, std=attn_std * std) | ||
| nn.init.normal_(block.attn.in_proj_weight, std=attn_std * factor) |
There was a problem hiding this comment.
Let's not change files only to rename a variable, which had a perfectly fine name before 🙂
There was a problem hiding this comment.
By convention, it is better to use factor = self.config.initializer_factor
| if isinstance(module, nn.Linear) and module.bias is not None: | ||
| module.bias.data.zero_() | ||
| if isinstance(module, (nn.Linear, nn.ConvTranspose2d)): | ||
| module.weight.data.normal_(mean=0.0, std=factor * 0.02) | ||
| if module.bias is not None: | ||
| module.bias.data.zero_() |
There was a problem hiding this comment.
This would overwrite most of the others, so need to make it coherent here
There was a problem hiding this comment.
I didn’t see the nn.ConvTranspose2d initialization code in the original implementation, so I applied the same initialization approach as used for Linear.
| class EsmForProteinFolding(EsmPreTrainedModel): | ||
| class EsmForProteinFolding(EsmFoldPreTrainedModel): |
| nn.init.xavier_uniform_(module.get_submodule("0").weight, gain=xavier_std) | ||
| nn.init.constant_(module.get_submodule("0").bias, 0) |
There was a problem hiding this comment.
Let's initialize the correct module directly instead of that
There was a problem hiding this comment.
Does that mean keeping the original code?
| module.weight.data.fill_(1.0) | ||
| if isinstance(module, nn.Linear): | ||
| module.weight.data.normal_(mean=0.0, std=factor) | ||
| module.weight.data.normal_(mean=0.0, std=factor * 0.02) |
| module.weight.data.fill_(1.0) | ||
| if isinstance(module, nn.Linear): | ||
| module.weight.data.normal_(mean=0.0, std=factor) | ||
| module.weight.data.normal_(mean=0.0, std=factor * 0.02) |
| nn.init.normal_(module.position_embedding, std=self.config.initializer_factor) | ||
| nn.init.normal_(module.position_embedding, std=factor * 0.02) |
| module.weight.data.normal_(mean=0.0, std=self.config.initializer_factor) | ||
| module.weight.data.normal_(mean=0.0, std=factor * 0.02) |
|
Hi @Cyrilvallez ! Thank you so much for reviewing and responding to my issue, as always😊. Yes, indeed, when using For the term transformers/src/transformers/models/clvp/modeling_clvp.py Lines 797 to 802 in e446372 The original implementation of |
|
[For maintainers] Suggested jobs to run (before merge) run-slow: altclip, bert, blip, bridgetower, camembert, chinese_clip, clip, clipseg, conditional_detr, cvt, data2vec, deformable_detr, detr, ernie, esm, groupvit |
|
Yeah but unfortunately, once again if those old models were badly implemented at the time, it's hard to change it now. So this would be another PR, which would be quite breaking, so need to be separate. Happy to review such a PR in the future if you are open to it! In the meantime, this PR should keep its focus on adding missing initializations only, i.e. modules that are not taken into account - it should not change the default std of existing inits 🤗 |
What does this PR do?
Fixes missing weight initializations for models created in 2022.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
@Cyrilvallez