Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6c117b4
fix MaskFormer
bvantuan Jul 30, 2025
5c7c025
fix ConditionalDetr and Detr
bvantuan Jul 30, 2025
9f020a0
fix Wav2Vec2Conformer and Wav2Vec2
bvantuan Jul 30, 2025
8a9dd35
fix Levit
bvantuan Jul 30, 2025
d5c608a
fix ChineseCLIP
bvantuan Jul 30, 2025
1a89491
fix CLIPSeg
bvantuan Jul 30, 2025
3ae1d23
fix Cvt
bvantuan Jul 30, 2025
62d6219
fix Vilt
bvantuan Jul 30, 2025
de3d656
fix Yolos
bvantuan Jul 31, 2025
38c9853
fix GroupViT
bvantuan Jul 31, 2025
0f70e77
fix Swin2SR
bvantuan Jul 31, 2025
0a8b3bf
fix Mvp
bvantuan Jul 31, 2025
e72fac7
fix DeformableDetr
bvantuan Jul 31, 2025
198942d
fix Nystromformer
bvantuan Jul 31, 2025
b1f0b47
fix RegNet and ResNet
bvantuan Jul 31, 2025
8746788
fix Timesformer
bvantuan Jul 31, 2025
e63912b
fix AltCLIP and CLIP
bvantuan Jul 31, 2025
07a6443
fix XCLIP
bvantuan Jul 31, 2025
c58c3c8
fix VideoMAE
bvantuan Jul 31, 2025
b954115
fix TimeSeriesTransformer
bvantuan Jul 31, 2025
40c7240
fix Blip
bvantuan Jul 31, 2025
da184dc
fix Ernie
bvantuan Jul 31, 2025
4f63715
fix OwlViTForObjectDetection
bvantuan Aug 1, 2025
c6f3b32
fix Blip
bvantuan Aug 1, 2025
97e3b04
Merge branch 'main' into fix-missing-initializations-2022
bvantuan Aug 1, 2025
a586d99
fix MaskFormerSwin
bvantuan Aug 1, 2025
b7d93f7
fix Mgpstr
bvantuan Aug 1, 2025
48b81fe
fix Vilt
bvantuan Aug 1, 2025
0220b9d
fix Esm
bvantuan Aug 1, 2025
7dd274e
Merge branch 'main' into fix-missing-initializations-2022
bvantuan Aug 8, 2025
91fa7d2
fix Data2VecAudio
bvantuan Aug 14, 2025
c7e70f8
Merge branch 'main' into fix-missing-initializations-2022
bvantuan Aug 14, 2025
ab653bc
Merge branch 'main' into fix-missing-initializations-2022
bvantuan Aug 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions src/transformers/models/altclip/modeling_altclip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines -855 to +851

Copy link
Copy Markdown
Member

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???

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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_()

Expand Down
7 changes: 4 additions & 3 deletions src/transformers/models/bert/modeling_bert.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,16 +794,17 @@ class BertPreTrainedModel(PreTrainedModel):
supports_gradient_checkpointing = True
_supports_sdpa = True

def _init_weights(self, module):
def _init_weights(self, module: nn.Module):
"""Initialize the weights"""
std = self.config.initializer_range
if isinstance(module, nn.Linear):
# Slightly different from the TF version which uses truncated_normal for initialization
# cf https://github.com/pytorch/pytorch/pull/5617
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
module.weight.data.normal_(mean=0.0, std=std)
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_range)
module.weight.data.normal_(mean=0.0, std=std)
if module.padding_idx is not None:
module.weight.data[module.padding_idx].zero_()
elif isinstance(module, nn.LayerNorm):
Expand Down
24 changes: 8 additions & 16 deletions src/transformers/models/blip/modeling_blip.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,29 +440,21 @@ class BlipPreTrainedModel(PreTrainedModel):
_no_split_modules = ["BlipEncoderLayer", "BlipTextEmbeddings"]
_skip_keys_device_placement = ["past_key_values"]

def _init_weights(self, module):
def _init_weights(self, module: nn.Module):
"""Initialize the weights"""
factor = self.config.initializer_range
std = self.config.initializer_range
if isinstance(module, (nn.Conv2d, nn.Embedding, nn.Linear)):
module.weight.data.normal_(mean=0.0, std=factor)
module.weight.data.normal_(mean=0.0, std=std)
if hasattr(module, "bias") and module.bias is not None:
module.bias.data.zero_()

if isinstance(module, BlipVisionEmbeddings):
if hasattr(self.config, "vision_config"):
factor = self.config.vision_config.initializer_range
nn.init.trunc_normal_(
module.position_embedding,
mean=0.0,
std=factor,
)

nn.init.trunc_normal_(
module.class_embedding,
mean=0.0,
std=factor,
)

std = self.config.vision_config.initializer_range
nn.init.trunc_normal_(module.position_embedding, std=std)
nn.init.trunc_normal_(module.class_embedding, std=std)
elif isinstance(module, BlipModel):
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)
Expand Down
4 changes: 2 additions & 2 deletions src/transformers/models/blip/modeling_blip_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ class BlipTextPreTrainedModel(PreTrainedModel):
base_model_prefix = "bert"
_no_split_modules = []

def _init_weights(self, module):
def _init_weights(self, module: nn.Module):
"""Initialize the weights"""
if isinstance(module, (nn.Linear, nn.Embedding)):
# Slightly different from the TF version which uses truncated_normal for initialization
Expand All @@ -592,7 +592,7 @@ def _init_weights(self, module):
elif isinstance(module, nn.LayerNorm):
module.bias.data.zero_()
module.weight.data.fill_(1.0)
if isinstance(module, nn.Linear) and module.bias is not None:
if isinstance(module, (nn.Linear, BlipTextLMPredictionHead)) and module.bias is not None:
module.bias.data.zero_()


Expand Down
16 changes: 8 additions & 8 deletions src/transformers/models/bridgetower/modeling_bridgetower.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

By convention, it is better to use factor = self.config.initializer_factor

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)
Expand Down
7 changes: 4 additions & 3 deletions src/transformers/models/camembert/modeling_camembert.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,16 +680,17 @@ class CamembertPreTrainedModel(PreTrainedModel):
_supports_sdpa = True

# Copied from transformers.models.bert.modeling_bert.BertPreTrainedModel._init_weights with BertLMPredictionHead->CamembertLMHead
def _init_weights(self, module):
def _init_weights(self, module: nn.Module):
"""Initialize the weights"""
std = self.config.initializer_range
if isinstance(module, nn.Linear):
# Slightly different from the TF version which uses truncated_normal for initialization
# cf https://github.com/pytorch/pytorch/pull/5617
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
module.weight.data.normal_(mean=0.0, std=std)
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_range)
module.weight.data.normal_(mean=0.0, std=std)
if module.padding_idx is not None:
module.weight.data[module.padding_idx].zero_()
elif isinstance(module, nn.LayerNorm):
Expand Down
21 changes: 10 additions & 11 deletions src/transformers/models/chinese_clip/modeling_chinese_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,50 +592,49 @@ class ChineseCLIPPreTrainedModel(PreTrainedModel):
base_model_prefix = "chinese_clip"
supports_gradient_checkpointing = True

def _init_weights(self, module):
def _init_weights(self, module: nn.Module):
"""Initialize the weights"""
factor = self.config.initializer_factor
std = self.config.initializer_range
if isinstance(module, ChineseCLIPVisionEmbeddings):
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, ChineseCLIPTextEmbeddings):
nn.init.normal_(module.word_embeddings.weight, mean=0.0, std=self.config.initializer_range)
nn.init.normal_(module.position_embeddings.weight, mean=0.0, std=self.config.initializer_range)
nn.init.normal_(module.token_type_embeddings.weight, mean=0.0, std=self.config.initializer_range)
nn.init.normal_(module.word_embeddings.weight, std=std)
nn.init.normal_(module.position_embeddings.weight, std=std)
nn.init.normal_(module.token_type_embeddings.weight, std=std)
for embedding in [module.word_embeddings, module.position_embeddings, module.token_type_embeddings]:
if embedding.padding_idx is not None:
embedding.weight.data[embedding.padding_idx].zero_()
elif isinstance(module, ChineseCLIPVisionAttention):
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, ChineseCLIPVisionMLP):
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, ChineseCLIPModel):
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,
)
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.logit_scale.data.fill_(self.config.logit_scale_init_value)

if isinstance(module, nn.LayerNorm):
module.bias.data.zero_()
module.weight.data.fill_(1.0)
if isinstance(module, nn.Linear):
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
module.weight.data.normal_(mean=0.0, std=std)
if module.bias is not None:
module.bias.data.zero_()

Expand Down
18 changes: 8 additions & 10 deletions src/transformers/models/clip/modeling_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,54 +432,52 @@ class CLIPPreTrainedModel(PreTrainedModel):
_supports_flex_attn = True
_supports_attention_backend = True

def _init_weights(self, module):
def _init_weights(self, module: nn.Module):
"""Initialize the weights"""
factor = self.config.initializer_factor
if isinstance(module, CLIPTextEmbeddings):
module.token_embedding.weight.data.normal_(mean=0.0, std=factor * 0.02)
module.position_embedding.weight.data.normal_(mean=0.0, std=factor * 0.02)
elif isinstance(module, CLIPVisionEmbeddings):
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, CLIPAttention):
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, CLIPMLP):
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, CLIPModel):
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,
)
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.logit_scale.data.fill_(self.config.logit_scale_init_value)
elif isinstance(module, CLIPVisionModelWithProjection):
nn.init.normal_(
module.visual_projection.weight,
std=self.config.hidden_size**-0.5 * self.config.initializer_factor,
std=self.config.hidden_size**-0.5 * factor,
)
elif isinstance(module, CLIPTextModelWithProjection):
nn.init.normal_(
module.text_projection.weight,
std=self.config.hidden_size**-0.5 * self.config.initializer_factor,
std=self.config.hidden_size**-0.5 * factor,
)
elif isinstance(module, CLIPForImageClassification):
nn.init.normal_(
module.classifier.weight,
std=self.config.vision_config.hidden_size**-0.5 * self.config.initializer_factor,
std=self.config.vision_config.hidden_size**-0.5 * factor,
)

if isinstance(module, nn.LayerNorm):
Expand Down
Loading