fix: align RotaryEmbedding with Qwen2Moe pattern for transformers compat
#4
by kashif HF Staff - opened
- modeling_llada2_moe.py +37 -32
modeling_llada2_moe.py
CHANGED
|
@@ -28,9 +28,7 @@ from torch.nn import CrossEntropyLoss
|
|
| 28 |
|
| 29 |
from transformers.activations import ACT2FN
|
| 30 |
from transformers.cache_utils import Cache, DynamicCache
|
| 31 |
-
from transformers.
|
| 32 |
-
_prepare_4d_causal_attention_mask_for_sdpa,
|
| 33 |
-
)
|
| 34 |
from transformers.modeling_outputs import (
|
| 35 |
MoeModelOutputWithPast,
|
| 36 |
MoeCausalLMOutputWithPast,
|
|
@@ -92,24 +90,41 @@ ALL_LAYERNORM_LAYERS.append(LLaDA2MoeRMSNorm)
|
|
| 92 |
|
| 93 |
|
| 94 |
class LLaDA2MoeRotaryEmbedding(nn.Module):
|
|
|
|
|
|
|
| 95 |
def __init__(self, config: LLaDA2MoeConfig, device=None):
|
| 96 |
super().__init__()
|
| 97 |
-
# BC: "rope_type" was originally "type"
|
| 98 |
-
if hasattr(config, "rope_scaling") and config.rope_scaling is not None:
|
| 99 |
-
self.rope_type = config.rope_scaling.get(
|
| 100 |
-
"rope_type", config.rope_scaling.get("type")
|
| 101 |
-
)
|
| 102 |
-
else:
|
| 103 |
-
self.rope_type = "default"
|
| 104 |
self.max_seq_len_cached = config.max_position_embeddings
|
| 105 |
self.original_max_seq_len = config.max_position_embeddings
|
| 106 |
|
| 107 |
self.config = config
|
| 108 |
-
self.rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type]
|
| 109 |
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
| 112 |
-
self.original_inv_freq
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
@torch.no_grad()
|
| 115 |
@dynamic_rope_update # power user: used with advanced RoPE types (e.g. dynamic rope)
|
|
@@ -669,16 +684,12 @@ class LLaDA2MoePreTrainedModel(PreTrainedModel):
|
|
| 669 |
_supports_flex_attn = True
|
| 670 |
_supports_cache_class = True
|
| 671 |
|
|
|
|
| 672 |
def _init_weights(self, module):
|
|
|
|
| 673 |
std = self.config.initializer_range
|
| 674 |
-
if isinstance(module,
|
| 675 |
-
|
| 676 |
-
if module.bias is not None:
|
| 677 |
-
module.bias.data.zero_()
|
| 678 |
-
elif isinstance(module, nn.Embedding):
|
| 679 |
-
module.weight.data.normal_(mean=0.0, std=std)
|
| 680 |
-
if module.padding_idx is not None:
|
| 681 |
-
module.weight.data[module.padding_idx].zero_()
|
| 682 |
|
| 683 |
|
| 684 |
LLADA2MOE_INPUTS_DOCSTRING = r"""
|
|
@@ -863,17 +874,11 @@ class LLaDA2MoeModel(LLaDA2MoePreTrainedModel):
|
|
| 863 |
device=inputs_embeds.device,
|
| 864 |
)
|
| 865 |
position_ids = position_ids.unsqueeze(0)
|
| 866 |
-
|
| 867 |
-
|
| 868 |
-
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
past_seen_tokens,
|
| 872 |
-
)
|
| 873 |
-
else:
|
| 874 |
-
raise ValueError(
|
| 875 |
-
f"LLaDA2.0 only support block attention mask with shape: {(batch_size, 1, seq_length, seq_length)}, the input attention with shape {attention_mask.size()=}!"
|
| 876 |
-
)
|
| 877 |
# embed positions
|
| 878 |
hidden_states = inputs_embeds
|
| 879 |
|
|
|
|
| 28 |
|
| 29 |
from transformers.activations import ACT2FN
|
| 30 |
from transformers.cache_utils import Cache, DynamicCache
|
| 31 |
+
from transformers.masking_utils import create_bidirectional_mask
|
|
|
|
|
|
|
| 32 |
from transformers.modeling_outputs import (
|
| 33 |
MoeModelOutputWithPast,
|
| 34 |
MoeCausalLMOutputWithPast,
|
|
|
|
| 90 |
|
| 91 |
|
| 92 |
class LLaDA2MoeRotaryEmbedding(nn.Module):
|
| 93 |
+
inv_freq: torch.Tensor # fix linting for register_buffer
|
| 94 |
+
|
| 95 |
def __init__(self, config: LLaDA2MoeConfig, device=None):
|
| 96 |
super().__init__()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
self.max_seq_len_cached = config.max_position_embeddings
|
| 98 |
self.original_max_seq_len = config.max_position_embeddings
|
| 99 |
|
| 100 |
self.config = config
|
|
|
|
| 101 |
|
| 102 |
+
self.rope_type = self.config.rope_parameters["rope_type"]
|
| 103 |
+
rope_init_fn: Callable = self.compute_default_rope_parameters
|
| 104 |
+
if self.rope_type != "default":
|
| 105 |
+
rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type]
|
| 106 |
+
inv_freq, self.attention_scaling = rope_init_fn(self.config, device)
|
| 107 |
+
|
| 108 |
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
| 109 |
+
self.register_buffer("original_inv_freq", inv_freq.clone(), persistent=False)
|
| 110 |
+
|
| 111 |
+
@staticmethod
|
| 112 |
+
def compute_default_rope_parameters(
|
| 113 |
+
config: LLaDA2MoeConfig = None,
|
| 114 |
+
device=None,
|
| 115 |
+
seq_len: int = None,
|
| 116 |
+
):
|
| 117 |
+
base = config.rope_parameters["rope_theta"]
|
| 118 |
+
partial_rotary_factor = config.rope_parameters.get("partial_rotary_factor", 1.0)
|
| 119 |
+
head_dim = getattr(config, "head_dim", None) or config.hidden_size // config.num_attention_heads
|
| 120 |
+
dim = int(head_dim * partial_rotary_factor)
|
| 121 |
+
|
| 122 |
+
attention_factor = 1.0 # Unused in this type of RoPE
|
| 123 |
+
|
| 124 |
+
inv_freq = 1.0 / (
|
| 125 |
+
base ** (torch.arange(0, dim, 2, dtype=torch.int64).to(device=device, dtype=torch.float) / dim)
|
| 126 |
+
)
|
| 127 |
+
return inv_freq, attention_factor
|
| 128 |
|
| 129 |
@torch.no_grad()
|
| 130 |
@dynamic_rope_update # power user: used with advanced RoPE types (e.g. dynamic rope)
|
|
|
|
| 684 |
_supports_flex_attn = True
|
| 685 |
_supports_cache_class = True
|
| 686 |
|
| 687 |
+
@torch.no_grad()
|
| 688 |
def _init_weights(self, module):
|
| 689 |
+
super()._init_weights(module)
|
| 690 |
std = self.config.initializer_range
|
| 691 |
+
if isinstance(module, LLaDA2MoeGate):
|
| 692 |
+
nn.init.normal_(module.weight, mean=0.0, std=std)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 693 |
|
| 694 |
|
| 695 |
LLADA2MOE_INPUTS_DOCSTRING = r"""
|
|
|
|
| 874 |
device=inputs_embeds.device,
|
| 875 |
)
|
| 876 |
position_ids = position_ids.unsqueeze(0)
|
| 877 |
+
attention_mask = create_bidirectional_mask(
|
| 878 |
+
config=self.config,
|
| 879 |
+
inputs_embeds=inputs_embeds,
|
| 880 |
+
attention_mask=attention_mask,
|
| 881 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 882 |
# embed positions
|
| 883 |
hidden_states = inputs_embeds
|
| 884 |
|