kashif HF Staff commited on
Commit
ce6a36f
·
verified ·
1 Parent(s): 2deebcd

fix: align RotaryEmbedding and _init_weights with Qwen2Moe for transformers compat

Browse files

- RotaryEmbedding: use compute_default_rope_parameters for default rope_type (removed from ROPE_INIT_FUNCTIONS in newer transformers)
- _init_weights: call super()._init_weights() for proper weight loading in transformers v5

Files changed (1) hide show
  1. modeling_llada2_moe.py +32 -19
modeling_llada2_moe.py CHANGED
@@ -20,7 +20,7 @@
20
 
21
  import math
22
  import warnings
23
- from typing import List, Optional, Tuple, Union
24
 
25
  import torch
26
  import torch.nn.functional as F
@@ -111,24 +111,41 @@ ALL_LAYERNORM_LAYERS.append(LLaDA2MoeRMSNorm)
111
 
112
 
113
  class LLaDA2MoeRotaryEmbedding(nn.Module):
 
 
114
  def __init__(self, config: LLaDA2MoeConfig, device=None):
115
  super().__init__()
116
- # BC: "rope_type" was originally "type"
117
- if hasattr(config, "rope_scaling") and config.rope_scaling is not None:
118
- self.rope_type = config.rope_scaling.get(
119
- "rope_type", config.rope_scaling.get("type")
120
- )
121
- else:
122
- self.rope_type = "default"
123
  self.max_seq_len_cached = config.max_position_embeddings
124
  self.original_max_seq_len = config.max_position_embeddings
125
 
126
  self.config = config
127
- self.rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type]
128
 
129
- inv_freq, self.attention_scaling = self.rope_init_fn(self.config, device)
 
 
 
 
 
130
  self.register_buffer("inv_freq", inv_freq, persistent=False)
131
- self.original_inv_freq = self.inv_freq
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
  @torch.no_grad()
134
  @dynamic_rope_update # power user: used with advanced RoPE types (e.g. dynamic rope)
@@ -1052,16 +1069,12 @@ class LLaDA2MoePreTrainedModel(PreTrainedModel):
1052
  _supports_sdpa = True
1053
  _supports_cache_class = True
1054
 
 
1055
  def _init_weights(self, module):
 
1056
  std = self.config.initializer_range
1057
- if isinstance(module, nn.Linear):
1058
- module.weight.data.normal_(mean=0.0, std=std)
1059
- if module.bias is not None:
1060
- module.bias.data.zero_()
1061
- elif isinstance(module, nn.Embedding):
1062
- module.weight.data.normal_(mean=0.0, std=std)
1063
- if module.padding_idx is not None:
1064
- module.weight.data[module.padding_idx].zero_()
1065
 
1066
 
1067
  LLADA2MOE_INPUTS_DOCSTRING = r"""
 
20
 
21
  import math
22
  import warnings
23
+ from typing import Callable, List, Optional, Tuple, Union
24
 
25
  import torch
26
  import torch.nn.functional as F
 
111
 
112
 
113
  class LLaDA2MoeRotaryEmbedding(nn.Module):
114
+ inv_freq: torch.Tensor # fix linting for register_buffer
115
+
116
  def __init__(self, config: LLaDA2MoeConfig, device=None):
117
  super().__init__()
 
 
 
 
 
 
 
118
  self.max_seq_len_cached = config.max_position_embeddings
119
  self.original_max_seq_len = config.max_position_embeddings
120
 
121
  self.config = config
 
122
 
123
+ self.rope_type = self.config.rope_parameters["rope_type"]
124
+ rope_init_fn: Callable = self.compute_default_rope_parameters
125
+ if self.rope_type != "default":
126
+ rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type]
127
+ inv_freq, self.attention_scaling = rope_init_fn(self.config, device)
128
+
129
  self.register_buffer("inv_freq", inv_freq, persistent=False)
130
+ self.register_buffer("original_inv_freq", inv_freq.clone(), persistent=False)
131
+
132
+ @staticmethod
133
+ def compute_default_rope_parameters(
134
+ config: LLaDA2MoeConfig = None,
135
+ device=None,
136
+ seq_len: int = None,
137
+ ):
138
+ base = config.rope_parameters["rope_theta"]
139
+ partial_rotary_factor = config.rope_parameters.get("partial_rotary_factor", 1.0)
140
+ head_dim = getattr(config, "head_dim", None) or config.hidden_size // config.num_attention_heads
141
+ dim = int(head_dim * partial_rotary_factor)
142
+
143
+ attention_factor = 1.0 # Unused in this type of RoPE
144
+
145
+ inv_freq = 1.0 / (
146
+ base ** (torch.arange(0, dim, 2, dtype=torch.int64).to(device=device, dtype=torch.float) / dim)
147
+ )
148
+ return inv_freq, attention_factor
149
 
150
  @torch.no_grad()
151
  @dynamic_rope_update # power user: used with advanced RoPE types (e.g. dynamic rope)
 
1069
  _supports_sdpa = True
1070
  _supports_cache_class = True
1071
 
1072
+ @torch.no_grad()
1073
  def _init_weights(self, module):
1074
+ super()._init_weights(module)
1075
  std = self.config.initializer_range
1076
+ if isinstance(module, LLaDA2MoeGate):
1077
+ nn.init.normal_(module.weight, mean=0.0, std=std)
 
 
 
 
 
 
1078
 
1079
 
1080
  LLADA2MOE_INPUTS_DOCSTRING = r"""