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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
"""Rose X1 model implementation for Hugging Face transformers.

Architecture (T-X4 family with XSA refresh gate):
  RoPE (half-split) + RMSNorm + SwiGLU + grouped-query attention with
  per-head QK-norm, plus an XSA *refresh gate* that re-injects the original
  token embedding through a gated depthwise-causal-conv path on a subset of
  layers (``config.refresh_gate_inject_layers``).

Cache design (after the T-X4 reference implementation):
  KV cache uses HF's ``DynamicCache``.  The refresh gate's conv history
  (last ``kernel-1`` timesteps of the normalised attention output) is stored
  in a plain dict monkey-patched onto the same ``DynamicCache`` object as
  ``_refresh_conv_state``, so both share one lifetime and no custom Cache
  subclass is needed.
"""
from typing import Optional

import torch
import torch.nn as nn
from torch.nn import functional as F
from transformers import PreTrainedModel
from transformers.cache_utils import DynamicCache
from transformers.generation.utils import GenerationMixin
from transformers.modeling_outputs import CausalLMOutputWithPast

try:
    from .configuration_rose_x1 import RoseX1Config
except ImportError:
    from configuration_rose_x1 import RoseX1Config


# ═══════════════════════════════════════════════════════════════════════════
#  Primitives
# ═══════════════════════════════════════════════════════════════════════════

class RMSNorm(nn.Module):
    """RMSNorm with fp32 internal computation, cast back to input dtype."""
    def __init__(self, dim: int, eps: float = 1e-5):
        super().__init__()
        self.eps = eps
        self.weight = nn.Parameter(torch.ones(dim))

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        in_dtype = x.dtype
        xf = x.float()
        out = xf * torch.rsqrt(xf.pow(2).mean(-1, keepdim=True) + self.eps)
        return (out * self.weight.float()).to(in_dtype)


def precompute_rope_cos_sin(head_dim: int, seq_len: int, theta: float = 100000.0):
    """Precompute RoPE cos/sin tables.  Returns (cos, sin) each (seq_len, head_dim//2)."""
    freqs = 1.0 / (theta ** (torch.arange(0, head_dim, 2, dtype=torch.float32) / head_dim))
    t = torch.arange(seq_len, dtype=torch.float32)
    angles = torch.outer(t, freqs)                       # (seq_len, head_dim//2)
    return angles.cos(), angles.sin()


def apply_rotary_emb(q: torch.Tensor, k: torch.Tensor,
                     cos: torch.Tensor, sin: torch.Tensor):
    """Half-split RoPE (matches the trainer's ``apply_rope``).

    cos / sin: (T, head_dim//2)  β€” already sliced to the right positions.
    q, k:      (B, H, T, head_dim)
    """
    cos = cos.unsqueeze(0).unsqueeze(0).to(q.dtype)      # (1,1,T,d//2)
    sin = sin.unsqueeze(0).unsqueeze(0).to(q.dtype)
    d2 = q.shape[-1] // 2

    q1, q2 = q[..., :d2], q[..., d2:]
    k1, k2 = k[..., :d2], k[..., d2:]

    q_out = torch.cat([q1 * cos - q2 * sin, q2 * cos + q1 * sin], dim=-1)
    k_out = torch.cat([k1 * cos - k2 * sin, k2 * cos + k1 * sin], dim=-1)
    return q_out, k_out


# ═══════════════════════════════════════════════════════════════════════════
#  Attention  (GQA + QK-norm + RoPE)
# ═══════════════════════════════════════════════════════════════════════════

class RoseX1Attention(nn.Module):
    def __init__(self, config: RoseX1Config, layer_idx: int):
        super().__init__()
        self.layer_idx = layer_idx
        self.n_head = config.num_attention_heads
        self.n_kv_heads = config.num_key_value_heads
        self.head_dim = config.head_dim
        self.n_rep = self.n_head // self.n_kv_heads

        self.q_proj = nn.Linear(config.hidden_size, self.n_head * self.head_dim, bias=False)
        self.k_proj = nn.Linear(config.hidden_size, self.n_kv_heads * self.head_dim, bias=False)
        self.v_proj = nn.Linear(config.hidden_size, self.n_kv_heads * self.head_dim, bias=False)
        self.o_proj = nn.Linear(self.n_head * self.head_dim, config.hidden_size, bias=False)

        # QK-norm: per-head RMSNorm on Q & K, applied BEFORE RoPE
        self.use_qk_norm = bool(getattr(config, "use_qk_norm", False))
        if self.use_qk_norm:
            self.q_norm = RMSNorm(self.head_dim, eps=config.rms_norm_eps)
            self.k_norm = RMSNorm(self.head_dim, eps=config.rms_norm_eps)

    def forward(self, x, rope_cos, rope_sin,
                past_key_value: Optional[DynamicCache] = None,
                use_cache: bool = False,
                attention_mask: Optional[torch.Tensor] = None):
        B, T, _ = x.size()

        q = self.q_proj(x).view(B, T, self.n_head, self.head_dim).transpose(1, 2)
        k = self.k_proj(x).view(B, T, self.n_kv_heads, self.head_dim).transpose(1, 2)
        v = self.v_proj(x).view(B, T, self.n_kv_heads, self.head_dim).transpose(1, 2)

        # ── QK-norm BEFORE RoPE (== trainer) ──────────────────────────────
        if self.use_qk_norm:
            q = self.q_norm(q)
            k = self.k_norm(k)

        # ── RoPE (half-split, cos/sin already sliced to current positions) ─
        q, k = apply_rotary_emb(q, k, rope_cos, rope_sin)

        # ── KV cache (DynamicCache.update handles concat internally) ──────
        if past_key_value is not None:
            k, v = past_key_value.update(k, v, self.layer_idx)

        S = k.size(2)

        # ── GQA expansion ─────────────────────────────────────────────────
        k = k.unsqueeze(2).expand(B, self.n_kv_heads, self.n_rep, S, self.head_dim) \
             .reshape(B, self.n_head, S, self.head_dim)
        v = v.unsqueeze(2).expand(B, self.n_kv_heads, self.n_rep, S, self.head_dim) \
             .reshape(B, self.n_head, S, self.head_dim)

        # ── Attention mask ────────────────────────────────────────────────
        # is_causal=True only for prefill (no cache) with T>1 and no padding
        # mask.  For decode (T=1) causal is trivially satisfied.
        is_causal = (past_key_value is None
                     or past_key_value.get_seq_length(self.layer_idx) == T)
        attn_mask = None
        if attention_mask is not None:
            key_pad = attention_mask.to(torch.bool)[:, None, None, :]   # (B,1,1,S)
            if is_causal and T > 1:
                causal = torch.ones(T, S, dtype=torch.bool, device=x.device) \
                             .tril(diagonal=S - T)
                attn_mask = key_pad & causal[None, None, :, :]
            else:
                attn_mask = key_pad.expand(B, 1, T, S)
            is_causal = False

        y = F.scaled_dot_product_attention(q, k, v,
                                           attn_mask=attn_mask,
                                           is_causal=is_causal)
        y = y.transpose(1, 2).contiguous().view(B, T, self.n_head * self.head_dim)
        return self.o_proj(y)


# ═══════════════════════════════════════════════════════════════════════════
#  MLP  (SwiGLU)
# ═══════════════════════════════════════════════════════════════════════════

class RoseX1MLP(nn.Module):
    def __init__(self, config: RoseX1Config):
        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: torch.Tensor) -> torch.Tensor:
        return self.down_proj(F.silu(self.gate_proj(x)) * self.up_proj(x))


# ═══════════════════════════════════════════════════════════════════════════
#  XSA Refresh Gate
# ═══════════════════════════════════════════════════════════════════════════

class RoseX1RefreshGate(nn.Module):
    """Re-injects the original token embedding (e0) into the residual stream,
    gated by a causal depthwise conv over the (detached) attention output.

    Conv history for cached generation is read/written via ``conv_state``
    (a plain dict living on the DynamicCache object).
    """
    def __init__(self, config: RoseX1Config):
        super().__init__()
        H = config.hidden_size
        self.kernel_size = int(getattr(config, "refresh_gate_kernel_size", 9))

        self.attn_norm  = RMSNorm(H, eps=config.rms_norm_eps)
        self.emb_norm   = RMSNorm(H, eps=config.rms_norm_eps)
        self.gate_proj  = nn.Linear(H, H, bias=False)
        self.value_proj = nn.Linear(H, H, bias=False)
        self.out_proj   = nn.Linear(H, H, bias=False)
        self.out_norm   = RMSNorm(H, eps=config.rms_norm_eps)

        # padding attribute documents intent; forward uses F.conv1d(padding=0)
        # with manual left-pad so the same weight works for cached & non-cached.
        self.causal_conv = nn.Conv1d(H, H, self.kernel_size,
                                     groups=H, bias=False,
                                     padding=self.kernel_size - 1)
        self.alpha = nn.Parameter(torch.tensor(0.1))

    def forward(self, h, attn_out, e0, conv_state=None, layer_idx=None):
        a = self.attn_norm(attn_out.detach())
        e = self.emb_norm(e0)

        k = self.kernel_size
        B, T, D = a.shape

        if conv_state is not None:
            # ── cached generation: prepend stored history ─────────────────
            prev = conv_state.get(layer_idx)
            if prev is None or prev.size(0) != B:
                prev = a.new_zeros(B, k - 1, D)
            a_ext = torch.cat([prev, a], dim=1)                    # (B, k-1+T, D)
            conv_state[layer_idx] = a_ext[:, -(k - 1):, :].detach()
        else:
            # ── no cache (training / full recompute): left-pad zeros ──────
            a_ext = F.pad(a, (0, 0, k - 1, 0))                    # (B, k-1+T, D)

        # Manual left-pad + padding=0 conv  (== trainer's CausalDepthwiseConv1d)
        c = F.conv1d(a_ext.transpose(1, 2),
                     self.causal_conv.weight,
                     bias=None, padding=0, groups=D)
        c = c.transpose(1, 2)                                      # (B, T, D)

        gate  = self.gate_proj(a) + c
        value = self.value_proj(e)
        z = self.out_norm(self.out_proj(F.silu(gate) * value))
        return h + self.alpha * z


# ═══════════════════════════════════════════════════════════════════════════
#  Decoder layer
# ═══════════════════════════════════════════════════════════════════════════

class RoseX1DecoderLayer(nn.Module):
    def __init__(self, config: RoseX1Config, layer_idx: int):
        super().__init__()
        self.layer_idx = layer_idx
        self.input_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
        self.self_attn = RoseX1Attention(config, layer_idx)
        self.post_attention_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
        self.mlp = RoseX1MLP(config)

        inject = list(getattr(config, "refresh_gate_inject_layers", []) or [])
        self.has_refresh = (bool(getattr(config, "refresh_gate_enabled", False))
                            and layer_idx in inject)
        if self.has_refresh:
            self.refresh_gate = RoseX1RefreshGate(config)

    def forward(self, x, e0, rope_cos, rope_sin,
                past_key_value=None, use_cache=False,
                attention_mask=None, conv_state=None):
        attn_out = self.self_attn(self.input_layernorm(x), rope_cos, rope_sin,
                                  past_key_value, use_cache, attention_mask)
        x = x + attn_out
        # Refresh gate fires AFTER attention residual, BEFORE FFN (== trainer)
        if self.has_refresh:
            x = self.refresh_gate(x, attn_out, e0,
                                  conv_state=conv_state,
                                  layer_idx=self.layer_idx)
        x = x + self.mlp(self.post_attention_layernorm(x))
        return x


# ═══════════════════════════════════════════════════════════════════════════
#  Base / backbone / head
# ═══════════════════════════════════════════════════════════════════════════

class RoseX1PreTrainedModel(PreTrainedModel):
    config_class = RoseX1Config
    base_model_prefix = "model"
    supports_gradient_checkpointing = False
    _no_split_modules = ["RoseX1DecoderLayer"]
    _supports_sdpa = True

    def _init_weights(self, module):
        std = self.config.initializer_range
        if isinstance(module, nn.Linear):
            nn.init.normal_(module.weight, mean=0.0, std=std)
            if module.bias is not None:
                nn.init.zeros_(module.bias)
        elif isinstance(module, nn.Embedding):
            nn.init.normal_(module.weight, mean=0.0, std=std)
        elif isinstance(module, nn.Conv1d):
            nn.init.normal_(module.weight, mean=0.0, std=std)
        elif isinstance(module, RMSNorm):
            nn.init.ones_(module.weight)


class RoseX1Model(nn.Module):
    """Backbone: embed β†’ N Γ— decoder layer β†’ final norm."""
    def __init__(self, config: RoseX1Config):
        super().__init__()
        self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size)
        self.dropout = nn.Dropout(config.attention_dropout)
        self.layers = nn.ModuleList(
            [RoseX1DecoderLayer(config, i) for i in range(config.num_hidden_layers)]
        )
        self.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)

    def forward(self, input_ids, e0, rope_cos, rope_sin,
                past_key_value=None, use_cache=False,
                attention_mask=None, conv_state=None):
        x = self.dropout(self.embed_tokens(input_ids))
        for layer in self.layers:
            x = layer(x, e0, rope_cos, rope_sin,
                      past_key_value, use_cache, attention_mask, conv_state)
        return self.norm(x)


class RoseX1ForCausalLM(RoseX1PreTrainedModel, GenerationMixin):
    # Dict format required by modern transformers' get_expanded_tied_weights_keys.
    # Tells HF: "lm_head.weight is tied to model.embed_tokens.weight β€” if it's
    # missing from the checkpoint, fill it from the embedding, don't warn."
    _tied_weights_keys = {"lm_head.weight": "model.embed_tokens.weight"}

    def __init__(self, config: RoseX1Config):
        super().__init__(config)
        self.model = RoseX1Model(config)

        # Always create lm_head; tie it when configured (standard HF pattern).
        self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
        if config.tie_word_embeddings:
            self.lm_head.weight = self.model.embed_tokens.weight

        self._rope_cache = None          # (cos, sin) cached on device
        self.post_init()

    # ── Embedding accessors (used by tie_weights / resize) ────────────────
    def get_input_embeddings(self):
        return self.model.embed_tokens

    def set_input_embeddings(self, value):
        self.model.embed_tokens = value

    def get_output_embeddings(self):
        return self.lm_head

    def set_output_embeddings(self, new_embeddings):
        self.lm_head = new_embeddings

    # ── RoPE cache ────────────────────────────────────────────────────────
    def _get_rope(self, seq_len: int, device: torch.device):
        cache = self._rope_cache
        if (cache is None
                or cache[0].device != device
                or cache[0].size(0) < seq_len):
            cos, sin = precompute_rope_cos_sin(
                self.config.head_dim, seq_len, self.config.rope_theta)
            cache = (cos.to(device), sin.to(device))
            self._rope_cache = cache
        return cache[0][:seq_len], cache[1][:seq_len]

    # ── Generation plumbing ───────────────────────────────────────────────
    def prepare_inputs_for_generation(self, input_ids,
                                      past_key_values=None,
                                      attention_mask=None, **kwargs):
        # When a cache with content exists, feed only the newest token.
        if past_key_values is not None and past_key_values.get_seq_length() > 0:
            input_ids = input_ids[:, -1:]
        return {
            "input_ids": input_ids,
            "attention_mask": attention_mask,
            "past_key_values": past_key_values,
            "use_cache": True,
        }

    # ── Forward ───────────────────────────────────────────────────────────
    def forward(
        self,
        input_ids: torch.Tensor,
        attention_mask: Optional[torch.Tensor] = None,
        labels: Optional[torch.Tensor] = None,
        past_key_values: Optional[DynamicCache] = None,
        use_cache: bool = False,
        **kwargs,
    ) -> CausalLMOutputWithPast:
        B, T = input_ids.size()

        # ── Conv-state cache for the refresh gate ─────────────────────────
        # Monkey-patched onto the DynamicCache so it shares the cache's
        # lifetime.  No custom Cache subclass needed.
        conv_state = None
        if use_cache:
            if past_key_values is None:
                past_key_values = DynamicCache()
            if not hasattr(past_key_values, "_refresh_conv_state"):
                past_key_values._refresh_conv_state = {}
            conv_state = past_key_values._refresh_conv_state

        # ── Position from cache length (no explicit position_ids needed) ──
        past_len = (past_key_values.get_seq_length()
                    if past_key_values is not None else 0)

        # ── Embeddings ────────────────────────────────────────────────────
        e0 = self.model.embed_tokens(input_ids)     # original embedding for refresh gate

        # ── RoPE: precompute up to past_len+T, slice to current positions ─
        cos, sin = self._get_rope(past_len + T, input_ids.device)
        cos, sin = cos[past_len:], sin[past_len:]   # (T, head_dim//2)

        # ── Backbone ──────────────────────────────────────────────────────
        hidden = self.model(
            input_ids, e0, cos, sin,
            past_key_values if use_cache else None,
            use_cache, attention_mask, conv_state,
        )

        # ── Head ──────────────────────────────────────────────────────────
        logits = self.lm_head(hidden).float()       # fp32 for stable logprobs

        loss = None
        if labels is not None:
            loss = F.cross_entropy(
                logits[..., :-1, :].contiguous().view(-1, self.config.vocab_size),
                labels[..., 1:].contiguous().view(-1),
                ignore_index=-100,
            )

        return CausalLMOutputWithPast(
            loss=loss,
            logits=logits,
            past_key_values=past_key_values if use_cache else None,
        )


# ── Optional registration (lets model_type="rose_x1" resolve without auto_map) ──
try:
    from transformers import AutoConfig, AutoModelForCausalLM
    try:
        AutoConfig.register("rose_x1", RoseX1Config)
    except Exception:
        pass
    try:
        AutoModelForCausalLM.register(RoseX1Config, RoseX1ForCausalLM)
    except Exception:
        pass
except Exception:
    pass