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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
"""
MetaDiffusion: Convert AR LLMs to Masked Diffusion LLMs
Based on Supra-1.5-50M-Base-exp architecture
"""

import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from dataclasses import dataclass, field
from typing import Optional, Tuple


@dataclass
class MetaDiffusionConfig:
    # Architecture (matching Supra-1.5-50M)
    hidden_size: int = 512
    intermediate_size: int = 1408
    num_hidden_layers: int = 12
    num_attention_heads: int = 8
    num_key_value_heads: int = 4
    head_dim: int = 64
    vocab_size: int = 32000          # original vocab
    mask_vocab_size: int = 32001     # vocab + [MASK] token
    max_position_embeddings: int = 5120
    rope_theta: float = 10000.0
    rms_norm_eps: float = 1e-6
    hidden_act: str = "silu"

    # Diffusion-specific
    timestep_emb_hidden: int = 512
    mask_token_id: int = 32000       # index of [MASK] in embedding table
    pad_token_id: int = 1            # Supra pad token

    # Masking strategy
    mask_ratio_min: float = 0.0
    mask_ratio_max: float = 1.0

    # Training
    dtype: torch.dtype = torch.float32
    tie_word_embeddings: bool = True

class RotaryEmbedding(nn.Module):
    """RoPE - position embeddings for the attention layers."""

    def __init__(self, dim, max_position_embeddings=5120, base=10000.0, device=None):
        super().__init__()
        self.dim = dim
        self.max_position_embeddings = max_position_embeddings
        self.base = base

        inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2, device=device).float() / dim))
        self.register_buffer("inv_freq", inv_freq, persistent=False)

    @torch.no_grad()
    def forward(self, x, position_ids):
        # x: (batch, seq, hidden) - used for dtype/device only
        # position_ids: (batch, seq)
        inv_freq_expanded = self.inv_freq[None, :, None].float().expand(
            position_ids.shape[0], -1, 1
        )
        position_ids_expanded = position_ids[:, None, :].float()
        freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2)
        emb = torch.cat((freqs, freqs), dim=-1)
        cos = emb.cos()
        sin = emb.sin()
        return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype)


def rotate_half(x):
    x1, x2 = x.chunk(2, dim=-1)
    return torch.cat((-x2, x1), dim=-1)


def apply_rotary_pos_emb(q, k, cos, sin):
    cos = cos.unsqueeze(1)  # (batch, 1, seq, dim)
    sin = sin.unsqueeze(1)
    q_embed = (q * cos) + (rotate_half(q) * sin)
    k_embed = (k * cos) + (rotate_half(k) * sin)
    return q_embed, k_embed


class TimestepEmbedding(nn.Module):
    """Sinusoidal timestep embedding with learned projection."""

    def __init__(self, hidden_size):
        super().__init__()
        self.hidden_size = hidden_size
        self.mlp = nn.Sequential(
            nn.Linear(hidden_size, hidden_size * 4),
            nn.SiLU(),
            nn.Linear(hidden_size * 4, hidden_size),
        )

    def forward(self, t):
        # t: (batch,) timesteps in [0, 1]
        half_dim = self.hidden_size // 2
        emb = math.log(10000.0) / (half_dim - 1)
        emb = torch.exp(
            torch.arange(half_dim, device=t.device, dtype=torch.float32) * -emb
        )
        emb = t[:, None].float() * emb[None, :]
        emb = torch.cat([emb.sin(), emb.cos()], dim=-1)  # (batch, hidden_size)
        return self.mlp(emb).to(t.dtype)  # (batch, hidden_size)


class TimestepResidual(nn.Module):
    """Add timestep embedding to hidden state at each block.
    Initialized to zero so it starts as identity (no disruption to pretrained weights)."""

    def __init__(self, hidden_size):
        super().__init__()
        self.proj = nn.Linear(hidden_size, hidden_size)
        nn.init.zeros_(self.proj.weight)
        nn.init.zeros_(self.proj.bias)

    def forward(self, x, emb):
        # x: (batch, seq, hidden)
        # emb: (batch, hidden)
        return x + self.proj(emb)[:, None, :]


class RMSNorm(nn.Module):
    """Llama-style RMSNorm."""

    def __init__(self, hidden_size, eps=1e-6):
        super().__init__()
        self.weight = nn.Parameter(torch.ones(hidden_size))
        self.eps = eps

    def forward(self, x):
        var = x.pow(2).mean(-1, keepdim=True)
        x = x * torch.rsqrt(var + self.eps)
        return self.weight * x


class SelfAttention(nn.Module):
    """Multi-head attention with GQA and RoPE.
    BIDIRECTIONAL (no causal mask) - this is the key difference from AR."""

    def __init__(self, config):
        super().__init__()
        self.config = config
        self.hidden_size = config.hidden_size
        self.num_heads = config.num_attention_heads
        self.num_kv_heads = config.num_key_value_heads
        self.head_dim = config.head_dim
        self.num_kv_groups = self.num_heads // self.num_kv_heads

        self.q_proj = nn.Linear(
            config.hidden_size, self.num_heads * config.head_dim, bias=False
        )
        self.k_proj = nn.Linear(
            config.hidden_size, self.num_kv_heads * config.head_dim, bias=False
        )
        self.v_proj = nn.Linear(
            config.hidden_size, self.num_kv_heads * config.head_dim, bias=False
        )
        self.o_proj = nn.Linear(
            self.num_heads * config.head_dim, config.hidden_size, bias=False
        )
        self.rotary_emb = RotaryEmbedding(
            config.head_dim,
            max_position_embeddings=config.max_position_embeddings,
            base=config.rope_theta,
        )

    def forward(self, x, attention_mask=None, position_ids=None):
        batch, seq, _ = x.shape

        q = self.q_proj(x).view(batch, seq, self.num_heads, self.head_dim).transpose(1, 2)
        k = self.k_proj(x).view(batch, seq, self.num_kv_heads, self.head_dim).transpose(1, 2)
        v = self.v_proj(x).view(batch, seq, self.num_kv_heads, self.head_dim).transpose(1, 2)

        cos, sin = self.rotary_emb(x, position_ids)
        q, k = apply_rotary_pos_emb(q, k, cos, sin)

        # GQA: repeat KV heads to match Q heads
        if self.num_kv_groups > 1:
            k = k.repeat_interleave(self.num_kv_groups, dim=1)
            v = v.repeat_interleave(self.num_kv_groups, dim=1)

        # Bidirectional attention - no causal mask!
        out = F.scaled_dot_product_attention(q, k, v, attn_mask=attention_mask)
        out = out.transpose(1, 2).contiguous().view(batch, seq, -1)
        return self.o_proj(out)


class MLP(nn.Module):
    """Llama-style gated FFN (SwiGLU)."""

    def __init__(self, config):
        super().__init__()
        self.gate_proj = nn.Linear(
            config.hidden_size, config.intermediate_size, bias=False
        )
        self.up_proj = nn.Linear(
            config.hidden_size, config.intermediate_size, bias=False
        )
        self.down_proj = nn.Linear(
            config.intermediate_size, config.hidden_size, bias=False
        )

    def forward(self, x):
        return self.down_proj(F.silu(self.gate_proj(x)) * self.up_proj(x))


class TransformerBlock(nn.Module):
    """Llama transformer block adapted for diffusion.
    - Pre-norm architecture
    - Bidirectional attention
    - Timestep conditioning via residual addition
    """

    def __init__(self, config):
        super().__init__()
        self.input_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
        self.self_attn = SelfAttention(config)
        self.post_attention_layernorm = RMSNorm(
            config.hidden_size, eps=config.rms_norm_eps
        )
        self.mlp = MLP(config)
        self.timestep_residual = TimestepResidual(config.hidden_size)

    def forward(self, x, timestep_emb, attention_mask=None, position_ids=None):
        # Pre-norm + attention + residual + timestep
        residual = x
        x = self.input_layernorm(x)
        x = self.self_attn(x, attention_mask, position_ids)
        x = residual + x
        x = self.timestep_residual(x, timestep_emb)

        # Pre-norm + FFN + residual + timestep
        residual = x
        x = self.post_attention_layernorm(x)
        x = self.mlp(x)
        x = residual + x
        x = self.timestep_residual(x, timestep_emb)

        return x

class MetaDiffusionLM(nn.Module):
    """Masked Diffusion Language Model.

    Converts an AR Llama-style model to a masked-diffusion LM.
    Transfers: embeddings, all transformer blocks, RoPE, norms.
    New: timestep embedding, [MASK] token.
    Output head is tied with embeddings by default (set tie_word_embeddings=False to untie).
    """

    def __init__(self, config: MetaDiffusionConfig):
        super().__init__()
        self.config = config

        # Embeddings (vocab + 1 for [MASK])
        self.embed_tokens = nn.Embedding(
            config.mask_vocab_size, config.hidden_size, padding_idx=config.pad_token_id
        )

        # Timestep conditioning
        self.timestep_emb = TimestepEmbedding(config.timestep_emb_hidden)

        # Transformer stack
        self.layers = nn.ModuleList(
            [TransformerBlock(config) for _ in range(config.num_hidden_layers)]
        )

        # Final norm
        self.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)

        # Output projection (can be tied with embeddings for parameter efficiency)
        if config.tie_word_embeddings:
            self.lm_head = None  # Will use embed_tokens.weight in forward
        else:
            self.lm_head = nn.Linear(
                config.hidden_size, config.mask_vocab_size, bias=False
            )

        self.post_init()

    def post_init(self):
        if self.lm_head is not None:
            nn.init.normal_(self.lm_head.weight, std=0.02)

    def forward(self, input_ids, timesteps, attention_mask=None):
        """
        Forward pass for training.

        Args:
            input_ids: (batch, seq) - tokens with masked positions replaced by mask_token_id
            timesteps: (batch,) - diffusion timestep in [0, 1]
            attention_mask: optional (batch, seq) - 1 for real tokens, 0 for padding

        Returns:
            logits: (batch, seq, mask_vocab_size)
        """
        batch, seq = input_ids.shape

        # Position IDs (0, 1, 2, ...)
        position_ids = (
            torch.arange(seq, device=input_ids.device).unsqueeze(0).expand(batch, -1)
        )

        # Embed tokens
        x = self.embed_tokens(input_ids)

        # Get timestep embedding
        t_emb = self.timestep_emb(timesteps)

        # Convert attention mask for SDPA (0 -> keep, -inf -> mask out)
        attn_mask = None
        if attention_mask is not None:
            attn_mask = ((1.0 - attention_mask[:, None, None, :].float()) * -1e9).to(
                x.dtype
            )

        # Pass through transformer blocks
        for layer in self.layers:
            x = layer(x, t_emb, attn_mask, position_ids)

        # Final norm + project to vocab
        x = self.norm(x)
        if self.lm_head is not None:
            logits = self.lm_head(x)
        else:
            # Tied embeddings: use embed_tokens.weight transposed
            logits = F.linear(x, self.embed_tokens.weight)

        return logits

    def compute_loss(self, logits, labels, mask_positions, pad_token_id=None):
        """
        Compute cross-entropy loss on masked positions only.

        Args:
            logits: (batch, seq, vocab_size)
            labels: (batch, seq) - original token IDs (before masking)
            mask_positions: (batch, seq) - bool tensor, True where token was masked
            pad_token_id: ignore these positions in loss

        Returns:
            loss: scalar
            num_masked: number of positions in loss
        """
        logits_masked = logits[mask_positions]
        labels_masked = labels[mask_positions]

        # Filter out padding tokens
        if pad_token_id is not None:
            valid = labels_masked != pad_token_id
            logits_masked = logits_masked[valid]
            labels_masked = labels_masked[valid]

        if labels_masked.numel() == 0:
            return torch.tensor(0.0, device=logits.device), 0

        loss = F.cross_entropy(logits_masked, labels_masked)
        return loss, labels_masked.numel()

    @torch.no_grad()
    def generate(self, batch_size, seq_len, num_steps=256, device="cuda"):
        """
        Iterative denoising generation (LLaDA-style).

        Starts from all-mask tokens and progressively unmaskes the most confident predictions.

        Args:
            batch_size: number of sequences to generate
            seq_len: length of each sequence
            num_steps: number of denoising iterations

        Returns:
            tokens: (batch, seq) - generated token IDs
        """
        mask_token_id = self.config.mask_token_id
        x = torch.full(
            (batch_size, seq_len), mask_token_id, device=device, dtype=torch.long
        )

        # Linear schedule from t=1 (all mask) to t=0 (no mask)
        timesteps = torch.linspace(1.0, 0.0, num_steps + 1, device=device)

        for i in range(num_steps):
            t = timesteps[i]
            t_next = timesteps[i + 1]
            t_batch = torch.full((batch_size,), t, device=device)

            # Get predictions
            logits = self.forward(x, t_batch)
            pred_tokens = logits.argmax(dim=-1)

            # Confidence of predicted tokens
            probs = F.softmax(logits, dim=-1)
            confidence = probs.gather(-1, pred_tokens.unsqueeze(-1)).squeeze(-1)

            # Number of tokens to unmask this step
            num_unmask = max(1, int(seq_len * (t - t_next)))

            # Only consider currently-masked positions
            is_mask = x == mask_token_id
            confidence_masked = confidence.clone()
            confidence_masked[~is_mask] = -1.0

            # Unmask the most confident predictions
            _, top_indices = confidence_masked.topk(num_unmask, dim=-1)
            batch_idx = (
                torch.arange(batch_size, device=device).unsqueeze(-1).expand_as(top_indices)
            )
            x[batch_idx, top_indices] = pred_tokens[batch_idx, top_indices]

        return x

    @classmethod
    def from_pretrained_ar(
        cls,
        model_name_or_path: str,
        **kwargs,
    ):
        """
        Initialize a MetaDiffusionLM from a pretrained AR Llama model.

        Transfers all AR weights and initializes new diffusion components.
        """
        from transformers import LlamaForCausalLM

        print(f"Loading AR model: {model_name_or_path}")
        ar_model = LlamaForCausalLM.from_pretrained(model_name_or_path)
        ar_config = ar_model.config

        # Build diffusion config from AR config
        head_dim = getattr(
            ar_config,
            "head_dim",
            ar_config.hidden_size // ar_config.num_attention_heads,
        )
        config = MetaDiffusionConfig(
            hidden_size=ar_config.hidden_size,
            intermediate_size=ar_config.intermediate_size,
            num_hidden_layers=ar_config.num_hidden_layers,
            num_attention_heads=ar_config.num_attention_heads,
            num_key_value_heads=ar_config.num_key_value_heads,
            head_dim=head_dim,
            vocab_size=ar_config.vocab_size,
            mask_vocab_size=ar_config.vocab_size + 1,
            max_position_embeddings=ar_config.max_position_embeddings,
            rope_theta=getattr(ar_config, "rope_theta", 10000.0),
            rms_norm_eps=ar_config.rms_norm_eps,
            mask_token_id=ar_config.vocab_size,
            pad_token_id=getattr(ar_config, "pad_token_id", 1),
            **kwargs,
        )

        model = cls(config)

        # Build state dict mapping
        state_dict = ar_model.state_dict()
        new_state_dict = {}

        # --- Embeddings ---
        # Copy original vocab embeddings
        ar_embeds = state_dict["model.embed_tokens.weight"]
        mask_embed = ar_embeds.mean(dim=0, keepdim=True)  # [MASK] = mean of all embeds
        new_state_dict["embed_tokens.weight"] = torch.cat(
            [ar_embeds, mask_embed], dim=0
        )

        # --- Transformer blocks ---
        for i in range(config.num_hidden_layers):
            ar_prefix = f"model.layers.{i}"
            new_prefix = f"layers.{i}"

            # Attention
            new_state_dict[f"{new_prefix}.self_attn.q_proj.weight"] = state_dict[
                f"{ar_prefix}.self_attn.q_proj.weight"
            ]
            new_state_dict[f"{new_prefix}.self_attn.k_proj.weight"] = state_dict[
                f"{ar_prefix}.self_attn.k_proj.weight"
            ]
            new_state_dict[f"{new_prefix}.self_attn.v_proj.weight"] = state_dict[
                f"{ar_prefix}.self_attn.v_proj.weight"
            ]
            new_state_dict[f"{new_prefix}.self_attn.o_proj.weight"] = state_dict[
                f"{ar_prefix}.self_attn.o_proj.weight"
            ]

            # MLP
            new_state_dict[f"{new_prefix}.mlp.gate_proj.weight"] = state_dict[
                f"{ar_prefix}.mlp.gate_proj.weight"
            ]
            new_state_dict[f"{new_prefix}.mlp.up_proj.weight"] = state_dict[
                f"{ar_prefix}.mlp.up_proj.weight"
            ]
            new_state_dict[f"{new_prefix}.mlp.down_proj.weight"] = state_dict[
                f"{ar_prefix}.mlp.down_proj.weight"
            ]

            # Layer norms
            new_state_dict[f"{new_prefix}.input_layernorm.weight"] = state_dict[
                f"{ar_prefix}.input_layernorm.weight"
            ]
            new_state_dict[
                f"{new_prefix}.post_attention_layernorm.weight"
            ] = state_dict[f"{ar_prefix}.post_attention_layernorm.weight"]

        # --- Final norm ---
        new_state_dict["norm.weight"] = state_dict["model.norm.weight"]

        # --- Output head ---
        # Initialize from AR embeddings (since AR used tied embeddings, E^T was the output)
        if not config.tie_word_embeddings:
            new_state_dict["lm_head.weight"] = torch.cat(
                [ar_embeds, torch.zeros(1, config.hidden_size, device=ar_embeds.device)],
                dim=0,
            ).clone()

        # Load with strict=False for new diffusion params
        missing, unexpected = model.load_state_dict(new_state_dict, strict=False)

        print(f"Weights transferred from AR model")
        print(f"  Missing (new diffusion params): {len(missing)}")
        print(f"  Unexpected: {len(unexpected)}")
        if missing:
            for k in missing:
                print(f"    NEW: {k}")

        return model