| import torch |
| import torch.nn as nn |
| import torch.nn.functional as F |
| from transformers import AutoModel |
|
|
| class AdaLNZero(nn.Module): |
| def __init__(self, hidden_dim, cond_dim): |
| super().__init__() |
| self.norm = nn.LayerNorm(hidden_dim, elementwise_affine=False) |
| self.proj = nn.Linear(cond_dim, 3 * hidden_dim) |
| nn.init.zeros_(self.proj.weight) |
| nn.init.zeros_(self.proj.bias) |
|
|
| def forward(self, x, cond): |
| params = self.proj(cond).unsqueeze(1) |
| scale, shift, alpha = params.chunk(3, dim=-1) |
| normalized = self.norm(x) * (1 + scale) + shift |
| return normalized, alpha |
|
|
| class ModernBertLayerWithAdaLN(nn.Module): |
| def __init__(self, pretrained_layer, hidden_dim, cond_dim): |
| super().__init__() |
| self.pretrained_layer = pretrained_layer |
| self.cond_proj = nn.Linear(cond_dim, 6 * hidden_dim) |
| nn.init.zeros_(self.cond_proj.weight) |
| nn.init.zeros_(self.cond_proj.bias) |
| |
| |
| self.attn_module = getattr(pretrained_layer, 'self_attn', getattr(pretrained_layer, 'attn', None)) |
| self.mlp_module = getattr(pretrained_layer, 'mlp', getattr(pretrained_layer, 'feed_forward', None)) |
| self.attn_norm = getattr(pretrained_layer, 'input_layernorm', getattr(pretrained_layer, 'attn_norm', nn.Identity())) |
| self.mlp_norm = getattr(pretrained_layer, 'post_attention_layernorm', getattr(pretrained_layer, 'mlp_norm', nn.Identity())) |
|
|
| def forward(self, hidden_states, cond, position_ids=None, attention_mask=None, position_embeddings=None): |
| adaln_params = self.cond_proj(cond).unsqueeze(1) |
| scale_attn, shift_attn, alpha_attn, scale_mlp, shift_mlp, alpha_mlp = adaln_params.chunk(6, dim=-1) |
| |
| |
| normed_attn = self.attn_norm(hidden_states) * (1 + scale_attn) + shift_attn |
| |
| |
| kwargs = {} |
| if attention_mask is not None: kwargs['attention_mask'] = attention_mask |
| if position_ids is not None: kwargs['position_ids'] = position_ids |
| if position_embeddings is not None: kwargs['position_embeddings'] = position_embeddings |
| |
| attn_out = self.attn_module(normed_attn, **kwargs) |
| attn_output = attn_out[0] if isinstance(attn_out, tuple) else attn_out |
| hidden_states = hidden_states + (alpha_attn * attn_output) |
| |
| |
| normed_mlp = self.mlp_norm(hidden_states) * (1 + scale_mlp) + shift_mlp |
| mlp_out = self.mlp_module(normed_mlp) |
| mlp_output = mlp_out[0] if isinstance(mlp_out, tuple) else mlp_out |
| hidden_states = hidden_states + (alpha_mlp * mlp_output) |
| |
| return hidden_states |
|
|
| class ConditionalMDLM(nn.Module): |
| def __init__(self, config): |
| super().__init__() |
| mc = config["model"] |
| self.vocab_size = mc["vocab_size"] |
|
|
| print(f"Loading pretrained backbone from {mc['pretrained_token_embeddings']}...") |
| self.backbone = AutoModel.from_pretrained(mc["pretrained_token_embeddings"], trust_remote_code=True) |
| |
| |
| self.hidden_dim = self.backbone.config.hidden_size |
| self.cond_dim = mc["embedding_cond_dim"] |
| print(f"Dynamically mapped: hidden_dim={self.hidden_dim}, cond_dim={self.cond_dim}") |
|
|
| self.token_embed = self.backbone.get_input_embeddings() |
| self.embed_norm = getattr(self.backbone, 'norm', nn.Identity()) |
| |
| self.layers = nn.ModuleList([ |
| ModernBertLayerWithAdaLN(layer, self.hidden_dim, self.cond_dim) |
| for layer in self.backbone.layers |
| ]) |
| |
| self.final_adaln = AdaLNZero(self.hidden_dim, self.cond_dim) |
| self.output_proj = nn.Linear(self.hidden_dim, self.vocab_size, bias=False) |
| |
| if mc.get("tie_weights", True): |
| self.output_proj.weight = self.token_embed.weight |
|
|
| def _forward_impl(self, input_ids, cond_embedding, padding_mask, return_logits=False): |
| device = input_ids.device |
| batch_size, seq_len = input_ids.shape |
|
|
| |
| if padding_mask is not None: |
| |
| attn_mask = (~padding_mask).bool() |
| else: |
| attn_mask = torch.ones(batch_size, seq_len, dtype=torch.bool, device=device) |
| |
| |
| |
| attn_mask = attn_mask.unsqueeze(1).unsqueeze(2) |
|
|
| |
| pos_ids = torch.arange(seq_len, dtype=torch.long, device=device).unsqueeze(0).expand(batch_size, -1) |
|
|
| |
| pos_emb = None |
| if hasattr(self.backbone, 'rotary_emb'): |
| dummy_x = torch.empty(batch_size, seq_len, self.hidden_dim, device=device, dtype=torch.bfloat16) |
| try: |
| pos_emb = self.backbone.rotary_emb(dummy_x, pos_ids) |
| except Exception: |
| try: |
| pos_emb = self.backbone.rotary_emb(pos_ids) |
| except Exception: |
| pass |
|
|
| |
| x = self.token_embed(input_ids) |
| x = self.embed_norm(x) |
| |
| for layer in self.layers: |
| |
| x = layer(x, cond_embedding, position_ids=pos_ids, attention_mask=attn_mask, position_embeddings=pos_emb) |
| |
| x, _ = self.final_adaln(x, cond_embedding) |
| |
| if return_logits: |
| return self.output_proj(x) |
| return x |
|
|
| def forward(self, input_ids, cond_embedding, padding_mask=None): |
| return self._forward_impl(input_ids, cond_embedding, padding_mask, return_logits=True) |
|
|
| def forward_hidden(self, input_ids, cond_embedding, padding_mask=None): |
| return self._forward_impl(input_ids, cond_embedding, padding_mask, return_logits=False) |
|
|
| def count_params(self): |
| total = sum(p.numel() for p in self.parameters()) |
| trainable = sum(p.numel() for p in self.parameters() if p.requires_grad) |
| return total, trainable |
|
|
| def apply_mask(token_ids, mask_token_id, padding_mask=None): |
| B, L = token_ids.shape |
| device = token_ids.device |
| u = torch.rand(B, 1, device=device) |
| mask_ratio = (1 - (1 - 1e-3)**u).clamp(min=0.1, max=1.0) |
| rand_scores = torch.rand(B, L, device=device) |
| if padding_mask is not None: |
| rand_scores[padding_mask] = 2.0 |
| target_mask = rand_scores < mask_ratio |
| masked_ids = token_ids.clone() |
| masked_ids[target_mask] = mask_token_id |
| return masked_ids, target_mask, mask_ratio |