-
Notifications
You must be signed in to change notification settings - Fork 34.7k
Fix missing initializations for models created in 2022 #39772
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
base: main
Are you sure you want to change the base?
Changes from all commits
6c117b4
5c7c025
9f020a0
8a9dd35
d5c608a
1a89491
3ae1d23
62d6219
de3d656
38c9853
0f70e77
0a8b3bf
e72fac7
198942d
b1f0b47
8746788
e63912b
07a6443
c58c3c8
b954115
40c7240
da184dc
4f63715
c6f3b32
97e3b04
a586d99
b7d93f7
48b81fe
0220b9d
7dd274e
91fa7d2
c7e70f8
ab653bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -815,48 +815,44 @@ class AltCLIPPreTrainedModel(PreTrainedModel): | |
| supports_gradient_checkpointing = True | ||
| _no_split_module = [] | ||
|
|
||
| def _init_weights(self, module): | ||
| def _init_weights(self, module: nn.Module): | ||
| """Initialize the weights""" | ||
| factor = self.config.initializer_factor | ||
| if isinstance(module, AltCLIPVisionEmbeddings): | ||
| factor = self.config.initializer_factor | ||
| nn.init.normal_(module.class_embedding, mean=0.0, std=module.embed_dim**-0.5 * factor) | ||
| nn.init.normal_(module.class_embedding, std=module.embed_dim**-0.5 * factor) | ||
| nn.init.normal_(module.patch_embedding.weight, std=module.config.initializer_range * factor) | ||
| nn.init.normal_(module.position_embedding.weight, std=module.config.initializer_range * factor) | ||
| elif isinstance(module, AltCLIPAttention): | ||
| factor = self.config.initializer_factor | ||
| in_proj_std = (module.embed_dim**-0.5) * ((2 * module.config.num_hidden_layers) ** -0.5) * factor | ||
| out_proj_std = (module.embed_dim**-0.5) * factor | ||
| nn.init.normal_(module.q_proj.weight, std=in_proj_std) | ||
| nn.init.normal_(module.k_proj.weight, std=in_proj_std) | ||
| nn.init.normal_(module.v_proj.weight, std=in_proj_std) | ||
| nn.init.normal_(module.out_proj.weight, std=out_proj_std) | ||
| elif isinstance(module, AltCLIPMLP): | ||
| factor = self.config.initializer_factor | ||
| in_proj_std = (module.config.hidden_size**-0.5) * ((2 * module.config.num_hidden_layers) ** -0.5) * factor | ||
| fc_std = (2 * module.config.hidden_size) ** -0.5 * factor | ||
| nn.init.normal_(module.fc1.weight, std=fc_std) | ||
| nn.init.normal_(module.fc2.weight, std=in_proj_std) | ||
| elif isinstance(module, AltCLIPModel): | ||
| nn.init.normal_( | ||
| module.text_projection.weight, | ||
| std=module.text_embed_dim**-0.5 * self.config.initializer_factor, | ||
| std=module.text_embed_dim**-0.5 * factor, | ||
| ) | ||
| module.text_projection._is_hf_initialized = True | ||
| nn.init.normal_( | ||
| module.visual_projection.weight, | ||
| std=module.vision_embed_dim**-0.5 * self.config.initializer_factor, | ||
| std=module.vision_embed_dim**-0.5 * factor, | ||
| ) | ||
| module.visual_projection._is_hf_initialized = True | ||
| module.logit_scale.data.fill_(self.config.logit_scale_init_value) | ||
| elif isinstance(module, nn.LayerNorm): | ||
| module.bias.data.zero_() | ||
| module.weight.data.fill_(1.0) | ||
| elif isinstance(module, nn.Linear): | ||
| module.weight.data.normal_(mean=0.0, std=self.config.initializer_factor) | ||
| module.weight.data.normal_(mean=0.0, std=factor * 0.02) | ||
| if module.bias is not None: | ||
| module.bias.data.zero_() | ||
| elif isinstance(module, nn.Embedding): | ||
| module.weight.data.normal_(mean=0.0, std=self.config.initializer_factor) | ||
| module.weight.data.normal_(mean=0.0, std=factor * 0.02) | ||
|
Comment on lines
-859
to
+855
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here?? |
||
| if module.padding_idx is not None: | ||
| module.weight.data[module.padding_idx].zero_() | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -943,22 +943,22 @@ class BridgeTowerPreTrainedModel(PreTrainedModel): | |
| _skip_keys_device_placement = "past_key_values" | ||
|
|
||
| def _init_weights(self, module: nn.Module): | ||
| std = self.config.initializer_factor | ||
| factor = self.config.initializer_factor | ||
| if isinstance(module, BridgeTowerVisionTransformer): | ||
| proj_std = (self.config.hidden_size**-0.5) * ((2 * self.config.num_hidden_layers) ** -0.5) | ||
| attn_std = self.config.hidden_size**-0.5 | ||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not change files only to rename a variable, which had a perfectly fine name before 🙂
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By convention, it is better to use |
||
| block.attn.in_proj_bias.data.zero_() | ||
| nn.init.normal_(block.attn.out_proj.weight, std=proj_std * std) | ||
| nn.init.normal_(block.mlp.c_fc.weight, std=fc_std * std) | ||
| nn.init.normal_(block.mlp.c_proj.weight, std=proj_std * std) | ||
| nn.init.normal_(block.attn.out_proj.weight, std=proj_std * factor) | ||
| nn.init.normal_(block.mlp.c_fc.weight, std=fc_std * factor) | ||
| nn.init.normal_(block.mlp.c_proj.weight, std=proj_std * factor) | ||
|
|
||
| nn.init.normal_(module.embeddings.class_embedding, std=attn_std * std) | ||
| nn.init.normal_(module.embeddings.position_embedding.weight, std=attn_std * std) | ||
| nn.init.normal_(module.embeddings.class_embedding, std=attn_std * factor) | ||
| nn.init.normal_(module.embeddings.position_embedding.weight, std=attn_std * factor) | ||
| elif isinstance(module, (nn.Linear, nn.Conv2d, nn.Embedding)): | ||
| module.weight.data.normal_(mean=0.0, std=0.05 * std) | ||
| module.weight.data.normal_(mean=0.0, std=0.05 * factor) | ||
| elif isinstance(module, nn.LayerNorm): | ||
| module.bias.data.zero_() | ||
| module.weight.data.fill_(1.0) | ||
|
|
||
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.
Why add a random 0.02 here???