Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import torch
from torch import nn

from ... import initialization as init
from ...modeling_utils import PreTrainedModel
from ...processing_utils import Unpack
from ...utils import ModelOutput, TransformersKwargs, auto_docstring
Expand All @@ -44,21 +43,7 @@ class ColModernVBertPreTrainedModel(PreTrainedModel):

@torch.no_grad()
def _init_weights(self, module):
std = (
self.config.initializer_range
if hasattr(self.config, "initializer_range")
else self.config.vlm_config.text_config.initializer_range
)

if isinstance(module, (nn.Linear, nn.Conv2d)):
init.normal_(module.weight, mean=0.0, std=std)
if module.bias is not None:
init.zeros_(module.bias)
elif isinstance(module, nn.Embedding):
init.normal_(module.weight, mean=0.0, std=std)
# Here we need the check explicitly, as we slice the weight in the `zeros_` call, so it looses the flag
if module.padding_idx is not None and not getattr(module.weight, "_is_hf_initialized", False):
init.zeros_(module.weight[module.padding_idx])
super()._init_weights(module)


@auto_docstring(
Expand Down
17 changes: 1 addition & 16 deletions src/transformers/models/colqwen2/modeling_colqwen2.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

from transformers import AutoModel

from ... import initialization as init
from ...cache_utils import Cache
from ...modeling_utils import PreTrainedModel
from ...utils import ModelOutput, auto_docstring, can_return_tuple, is_torch_available
Expand All @@ -47,21 +46,7 @@ class ColQwen2PreTrainedModel(PreTrainedModel):

@torch.no_grad()
def _init_weights(self, module):
std = (
self.config.initializer_range
if hasattr(self.config, "initializer_range")
else self.config.vlm_config.text_config.initializer_range
)

if isinstance(module, (nn.Linear, nn.Conv2d)):
init.normal_(module.weight, mean=0.0, std=std)
if module.bias is not None:
init.zeros_(module.bias)
elif isinstance(module, nn.Embedding):
init.normal_(module.weight, mean=0.0, std=std)
# Here we need the check explicitly, as we slice the weight in the `zeros_` call, so it looses the flag
if module.padding_idx is not None and not getattr(module.weight, "_is_hf_initialized", False):
init.zeros_(module.weight[module.padding_idx])
super()._init_weights(module)


@auto_docstring(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ class ConditionalDetrPreTrainedModel(PreTrainedModel):

@torch.no_grad()
def _init_weights(self, module):
std = self.config.init_std
super()._init_weights(module)
xavier_std = self.config.init_xavier_std

if isinstance(module, ConditionalDetrMaskHeadSmallConv):
Expand All @@ -1068,18 +1068,6 @@ def _init_weights(self, module):
elif isinstance(module, ConditionalDetrLearnedPositionEmbedding):
init.uniform_(module.row_embeddings.weight)
init.uniform_(module.column_embeddings.weight)
elif isinstance(module, (nn.Linear, nn.Conv2d)):
init.normal_(module.weight, mean=0.0, std=std)
if module.bias is not None:
init.zeros_(module.bias)
elif isinstance(module, nn.Embedding):
init.normal_(module.weight, mean=0.0, std=std)
# Here we need the check explicitly, as we slice the weight in the `zeros_` call, so it looses the flag
if module.padding_idx is not None and not getattr(module.weight, "_is_hf_initialized", False):
init.zeros_(module.weight[module.padding_idx])
elif isinstance(module, (nn.LayerNorm, nn.GroupNorm)):
init.ones_(module.weight)
init.zeros_(module.bias)


class ConditionalDetrEncoder(ConditionalDetrPreTrainedModel):
Expand Down
13 changes: 3 additions & 10 deletions src/transformers/models/falcon_mamba/modeling_falcon_mamba.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from ...cache_utils import Cache, DynamicCache
from ...generation import GenerationMixin
from ...integrations import lazy_load_kernel
from ...integrations.accelerate import force_accelerate_hooks
from ...modeling_layers import GradientCheckpointingLayer
from ...modeling_utils import PreTrainedModel
from ...utils import ModelOutput, auto_docstring, logging
Expand Down Expand Up @@ -437,6 +438,7 @@ def combine_fn(left, right):
return contextualized_states
# fmt: on

@force_accelerate_hooks("conv1d")
def forward(
self,
hidden_states,
Expand Down Expand Up @@ -507,7 +509,7 @@ class FalconMambaPreTrainedModel(PreTrainedModel):
@torch.no_grad()
def _init_weights(self, module):
"""Initialize the weights."""
std = self.config.initializer_range
super()._init_weights(module)
if isinstance(module, FalconMambaMixer):
# S4D real initialization. These are not discretized!
# The core is to load them, compute the discrete states, then write the updated state. Keeps the memory bounded
Expand All @@ -532,15 +534,6 @@ def _init_weights(self, module):
p = module.out_proj.weight
p /= math.sqrt(self.config.num_hidden_layers)

if isinstance(module, nn.Linear):
init.normal_(module.weight, std=std)
if module.bias is not None:
init.zeros_(module.bias)
elif isinstance(module, FalconMambaRMSNorm):
init.ones_(module.weight)
elif isinstance(module, nn.Embedding):
init.normal_(module.weight, std=std)


@auto_docstring(
custom_intro="""
Expand Down
3 changes: 1 addition & 2 deletions src/transformers/models/glm_ocr/modeling_glm_ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,6 @@ def __init__(self, config) -> None:

head_dim = config.hidden_size // config.num_heads
self.rotary_pos_emb = GlmOcrVisionRotaryEmbedding(head_dim // 2)

self.blocks = nn.ModuleList([GlmOcrVisionBlock(config) for _ in range(config.depth)])
self.merger = GlmOcrVisionPatchMerger(
dim=config.out_hidden_size,
Expand Down Expand Up @@ -820,7 +819,7 @@ def __init__(self, config):
super().__init__(config)
self.visual = GlmOcrVisionModel._from_config(config.vision_config)
self.language_model = GlmOcrTextModel._from_config(config.text_config)
self.rope_deltas = None # cache rope_deltas here
self.rope_deltas = None

# Initialize weights and apply final processing
self.post_init()
Expand Down
13 changes: 12 additions & 1 deletion src/transformers/models/glm_ocr/modular_glm_ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class GlmOcrVisionMlp(Glm4VisionMlp):
def __init__(self, config, bias: bool = True):
super().__init__(config)
self.intermediate_size = config.intermediate_size
self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=bias)
self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=bias)
self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=bias)


@auto_docstring(checkpoint="zai-org/GLM-OCR")
Expand Down Expand Up @@ -228,6 +231,7 @@ def forward(
class GlmOcrVisionBlock(Glm4vVisionBlock):
def __init__(self, config) -> None:
super().__init__()
self.attn = GlmOcrVisionAttention(config)
self.mlp = GlmOcrVisionMlp(config, bias=config.attention_bias)


Expand All @@ -240,6 +244,7 @@ def __init__(self, config) -> None:
super().__init__(config)
del self.embeddings
del self.post_conv_layernorm
self.blocks = nn.ModuleList([GlmOcrVisionBlock(config) for _ in range(config.depth)])
self.merger = GlmOcrVisionPatchMerger(
dim=config.out_hidden_size,
context_dim=config.out_hidden_size * config.in_channels,
Expand Down Expand Up @@ -299,7 +304,13 @@ class GlmOcrTextModel(Glm4vTextModel):


class GlmOcrModel(Glm4vModel):
pass
def __init__(self, config):
super(Glm4vModel, self).__init__(config)
self.visual = GlmOcrVisionModel._from_config(config.vision_config)
self.language_model = GlmOcrTextModel._from_config(config.text_config)
self.rope_deltas = None
# Initialize weights and apply final processing
self.post_init()


class GlmOcrForConditionalGeneration(Glm4vForConditionalGeneration):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use_kernel_func_from_hub,
use_kernelized_func,
)
from ...integrations.accelerate import force_accelerate_hooks
from ...integrations.hub_kernels import lazy_load_kernel
from ...masking_utils import create_causal_mask, create_recurrent_attention_mask
from ...modeling_layers import GradientCheckpointingLayer
Expand Down Expand Up @@ -730,6 +731,7 @@ def torch_forward(
return contextualized_states
# fmt: on

@force_accelerate_hooks("conv1d")
def forward(
self,
hidden_states,
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/models/lfm2_moe/modeling_lfm2_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use_kernel_func_from_hub,
use_kernelized_func,
)
from ...integrations.accelerate import force_accelerate_hooks
from ...masking_utils import create_causal_mask, create_recurrent_attention_mask
from ...modeling_layers import GradientCheckpointingLayer
from ...modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, MoeModelOutputWithPast
Expand Down Expand Up @@ -490,6 +491,7 @@ def slow_forward(
y = self.out_proj(y)
return y

@force_accelerate_hooks("conv")
def forward(
self,
hidden_states: torch.Tensor,
Expand Down
14 changes: 1 addition & 13 deletions src/transformers/models/maskformer/modeling_maskformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ class MaskFormerDetrPreTrainedModel(PreTrainedModel):

@torch.no_grad()
def _init_weights(self, module):
std = self.config.init_std
super()._init_weights(module)
xavier_std = self.config.init_xavier_std

if isinstance(module, MaskFormerDetrMaskHeadSmallConv):
Expand All @@ -750,18 +750,6 @@ def _init_weights(self, module):
elif isinstance(module, MaskFormerDetrLearnedPositionEmbedding):
init.uniform_(module.row_embeddings.weight)
init.uniform_(module.column_embeddings.weight)
elif isinstance(module, (nn.Linear, nn.Conv2d)):
init.normal_(module.weight, mean=0.0, std=std)
if module.bias is not None:
init.zeros_(module.bias)
elif isinstance(module, nn.Embedding):
init.normal_(module.weight, mean=0.0, std=std)
# Here we need the check explicitly, as we slice the weight in the `zeros_` call, so it looses the flag
if module.padding_idx is not None and not getattr(module.weight, "_is_hf_initialized", False):
init.zeros_(module.weight[module.padding_idx])
elif isinstance(module, (nn.LayerNorm, nn.GroupNorm)):
init.ones_(module.weight)
init.zeros_(module.bias)


class MaskFormerDetrDecoder(MaskFormerDetrPreTrainedModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,7 @@ class MMGroundingDinoPreTrainedModel(PreTrainedModel):

@torch.no_grad()
def _init_weights(self, module):
std = self.config.init_std

super()._init_weights(module)
if isinstance(module, MMGroundingDinoLearnedPositionEmbedding):
init.uniform_(module.row_embeddings.weight)
init.uniform_(module.column_embeddings.weight)
Expand Down Expand Up @@ -537,18 +536,6 @@ def _init_weights(self, module):
elif isinstance(module, MMGroundingDinoFusionLayer):
init.constant_(module.vision_param, 1e-4)
init.constant_(module.text_param, 1e-4)
elif isinstance(module, (nn.Linear, nn.Conv2d)):
init.normal_(module.weight, mean=0.0, std=std)
if module.bias is not None:
init.zeros_(module.bias)
elif isinstance(module, (nn.LayerNorm, nn.GroupNorm)):
init.ones_(module.weight)
init.zeros_(module.bias)
elif isinstance(module, nn.Embedding):
init.normal_(module.weight, mean=0.0, std=std)
# Here we need the check explicitly, as we slice the weight in the `zeros_` call, so it looses the flag
if module.padding_idx is not None and not getattr(module.weight, "_is_hf_initialized", False):
init.zeros_(module.weight[module.padding_idx])
elif isinstance(module, MMGroundingDinoMLPPredictionHead):
init.constant_(module.layers[-1].weight, 0)
init.constant_(module.layers[-1].bias, 0)
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/models/nemotron_h/modeling_nemotron_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use_kernel_func_from_hub,
use_kernelized_func,
)
from ...integrations.accelerate import force_accelerate_hooks
from ...masking_utils import create_causal_mask, create_recurrent_attention_mask
from ...modeling_layers import GradientCheckpointingLayer
from ...modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast
Expand Down Expand Up @@ -585,6 +586,7 @@ def torch_forward(self, input_states, cache_params: Cache | None=None, attention
return contextualized_states
# fmt: on

@force_accelerate_hooks("conv1d")
def forward(
self,
hidden_states,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ class PPDocLayoutV2PreTrainedModel(PreTrainedModel):
@torch.no_grad()
def _init_weights(self, module):
"""Initialize the weights"""
super()._init_weights(module)
if isinstance(module, PPDocLayoutV2ForObjectDetection):
if module.model.decoder.class_embed is not None:
for layer in module.model.decoder.class_embed:
Expand Down Expand Up @@ -786,7 +787,7 @@ def _init_weights(self, module):
init.xavier_uniform_(module.enc_score_head.weight)
init.constant_(module.enc_score_head.bias, bias)

elif isinstance(module, (nn.Linear, nn.Conv2d, nn.BatchNorm2d)):
elif isinstance(module, nn.BatchNorm2d):
init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
if module.bias is not None:
init.zeros_(module.bias)
Expand All @@ -795,10 +796,6 @@ def _init_weights(self, module):
init.ones_(module.running_var)
init.zeros_(module.num_batches_tracked)

elif isinstance(module, nn.LayerNorm):
init.ones_(module.weight)
init.zeros_(module.bias)

if hasattr(module, "weight_embedding") and self.config.learn_initial_query:
init.xavier_uniform_(module.weight_embedding.weight)
if hasattr(module, "denoising_class_embed") and self.config.num_denoising > 0:
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/models/qwen3_5/modeling_qwen3_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from ...cache_utils import Cache, DynamicCache
from ...generation import GenerationMixin
from ...integrations import use_kernel_forward_from_hub
from ...integrations.accelerate import force_accelerate_hooks
from ...masking_utils import create_causal_mask, create_recurrent_attention_mask
from ...modeling_flash_attention_utils import FlashAttentionKwargs
from ...modeling_layers import (
Expand Down Expand Up @@ -436,6 +437,7 @@ def __init__(self, config: Qwen3_5Config, layer_idx: int):
self.in_proj_b = nn.Linear(self.hidden_size, self.num_v_heads, bias=False)
self.in_proj_a = nn.Linear(self.hidden_size, self.num_v_heads, bias=False)

@force_accelerate_hooks("conv1d")
def forward(
self,
hidden_states: torch.Tensor,
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/models/qwen3_5_moe/modeling_qwen3_5_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from ...cache_utils import Cache, DynamicCache
from ...generation import GenerationMixin
from ...integrations import use_experts_implementation, use_kernel_forward_from_hub
from ...integrations.accelerate import force_accelerate_hooks
from ...masking_utils import create_causal_mask, create_recurrent_attention_mask
from ...modeling_flash_attention_utils import FlashAttentionKwargs
from ...modeling_layers import GradientCheckpointingLayer
Expand Down Expand Up @@ -433,6 +434,7 @@ def __init__(self, config: Qwen3_5MoeConfig, layer_idx: int):
self.in_proj_b = nn.Linear(self.hidden_size, self.num_v_heads, bias=False)
self.in_proj_a = nn.Linear(self.hidden_size, self.num_v_heads, bias=False)

@force_accelerate_hooks("conv1d")
def forward(
self,
hidden_states: torch.Tensor,
Expand Down
4 changes: 1 addition & 3 deletions src/transformers/models/rf_detr/modeling_rf_detr.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,11 @@ class RfDetrDinov2PreTrainedModel(PreTrainedModel):
@torch.no_grad()
def _init_weights(self, module: nn.Linear | nn.Conv2d | nn.LayerNorm) -> None:
"""Initialize the weights"""
super()._init_weights(module)
if isinstance(module, (nn.Linear, nn.Conv2d)):
init.trunc_normal_(module.weight, mean=0.0, std=self.config.initializer_range)
if module.bias is not None:
init.zeros_(module.bias)
elif isinstance(module, nn.LayerNorm):
init.zeros_(module.bias)
init.ones_(module.weight)
elif isinstance(module, RfDetrDinov2Embeddings):
init.trunc_normal_(module.position_embeddings, mean=0.0, std=self.config.initializer_range)
init.trunc_normal_(module.cls_token, mean=0.0, std=self.config.initializer_range)
Expand Down
7 changes: 2 additions & 5 deletions src/transformers/models/rt_detr_v2/modeling_rt_detr_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ class RTDetrV2PreTrainedModel(PreTrainedModel):
@torch.no_grad()
def _init_weights(self, module):
"""Initialize the weights"""
super()._init_weights(module)
if isinstance(module, RTDetrV2ForObjectDetection):
if module.model.decoder.class_embed is not None:
for layer in module.model.decoder.class_embed:
Expand Down Expand Up @@ -488,7 +489,7 @@ def _init_weights(self, module):
init.xavier_uniform_(module.enc_score_head.weight)
init.constant_(module.enc_score_head.bias, bias)

elif isinstance(module, (nn.Linear, nn.Conv2d, nn.BatchNorm2d)):
elif isinstance(module, nn.BatchNorm2d):
init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
if module.bias is not None:
init.zeros_(module.bias)
Expand All @@ -497,10 +498,6 @@ def _init_weights(self, module):
init.ones_(module.running_var)
init.zeros_(module.num_batches_tracked)

elif isinstance(module, nn.LayerNorm):
init.ones_(module.weight)
init.zeros_(module.bias)

if hasattr(module, "weight_embedding") and self.config.learn_initial_query:
init.xavier_uniform_(module.weight_embedding.weight)
if hasattr(module, "denoising_class_embed") and self.config.num_denoising > 0:
Expand Down
Loading
Loading