Spaces:
Running on Zero

File size: 30,369 Bytes
878a616
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
# Local compatibility shim for loading T5Gemma 2 text encoders under
# transformers 4.57.x, which does not ship the t5gemma2 architecture.


import torch
from torch import nn
from transformers import PretrainedConfig
from transformers.activations import ACT2FN
from transformers.modeling_layers import GradientCheckpointingLayer
from transformers.modeling_outputs import BaseModelOutput
from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel


class T5Gemma2TextConfig(PretrainedConfig):
    model_type = "t5gemma2_text"
    keys_to_ignore_at_inference = ["past_key_values"]

    def __init__(
        self,
        vocab_size: int = 262208,
        hidden_size: int = 2304,
        intermediate_size: int = 9216,
        num_hidden_layers: int = 26,
        num_attention_heads: int = 8,
        num_key_value_heads: int = 4,
        head_dim: int = 256,
        hidden_activation: str = "gelu_pytorch_tanh",
        max_position_embeddings: int = 131072,
        initializer_range: float = 0.02,
        rms_norm_eps: float = 1e-6,
        use_cache: bool = True,
        rope_parameters: dict | None = None,
        attention_bias: bool = False,
        attention_dropout: float = 0.0,
        dropout_rate: float = 0.0,
        query_pre_attn_scalar: int = 256,
        sliding_window: int | None = 4096,
        layer_types: list[str] | None = None,
        final_logit_softcapping: float | None = None,
        attn_logit_softcapping: float | None = None,
        eoi_token_index: int = 256000,
        pad_token_id: int | None = 0,
        eos_token_id: int | None = 1,
        bos_token_id: int | None = 2,
        tie_word_embeddings: bool = True,
        **kwargs,
    ):
        sliding_window_pattern = kwargs.pop(
            "_sliding_window_pattern", kwargs.pop("sliding_window_pattern", 6)
        )
        rope_scaling = kwargs.pop("rope_scaling", None)
        super().__init__(
            pad_token_id=pad_token_id,
            eos_token_id=eos_token_id,
            bos_token_id=bos_token_id,
            tie_word_embeddings=tie_word_embeddings,
            **kwargs,
        )

        self.vocab_size = vocab_size
        self.hidden_size = hidden_size
        self.intermediate_size = intermediate_size
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads
        self.num_key_value_heads = num_key_value_heads
        self.head_dim = head_dim
        self.hidden_activation = hidden_activation
        self.max_position_embeddings = max_position_embeddings
        self.initializer_range = initializer_range
        self.rms_norm_eps = rms_norm_eps
        self.use_cache = use_cache
        self.attention_bias = attention_bias
        self.attention_dropout = attention_dropout
        self.dropout_rate = dropout_rate
        self.query_pre_attn_scalar = query_pre_attn_scalar
        self.sliding_window = sliding_window
        self.final_logit_softcapping = final_logit_softcapping
        self.attn_logit_softcapping = attn_logit_softcapping
        self.eoi_token_index = eoi_token_index

        if layer_types is None:
            layer_types = [
                "sliding_attention"
                if (i + 1) % sliding_window_pattern
                else "full_attention"
                for i in range(num_hidden_layers)
            ]
        self.layer_types = layer_types
        self.rope_parameters = self._normalize_rope_parameters(
            rope_parameters, rope_scaling
        )

    @staticmethod
    def _normalize_rope_parameters(
        rope_parameters: dict | None, rope_scaling: dict | None
    ) -> dict:
        params = {
            "sliding_attention": {"rope_type": "default", "rope_theta": 10000.0},
            "full_attention": {"rope_type": "default", "rope_theta": 1000000.0},
        }
        if rope_parameters:
            for key, value in rope_parameters.items():
                params[key] = (
                    dict(value) if value is not None else {"rope_type": "default"}
                )
        if rope_scaling:
            params["full_attention"].update(rope_scaling)
        params["sliding_attention"].setdefault("rope_type", "default")
        params["sliding_attention"].setdefault("rope_theta", 10000.0)
        params["full_attention"].setdefault("rope_type", "default")
        params["full_attention"].setdefault("rope_theta", 1000000.0)
        return params


class T5Gemma2RMSNorm(nn.Module):
    def __init__(self, dim: int, eps: float = 1e-6):
        super().__init__()
        self.eps = eps
        self.weight = nn.Parameter(torch.zeros(dim))

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        output = x.float() * torch.rsqrt(
            x.float().pow(2).mean(-1, keepdim=True) + self.eps
        )
        output = output * (1.0 + self.weight.float())
        return output.type_as(x)


class T5Gemma2MLP(nn.Module):
    def __init__(self, config: T5Gemma2TextConfig):
        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
        )
        self.act_fn = ACT2FN[config.hidden_activation]
        self.dropout = nn.Dropout(config.dropout_rate)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        hidden_states = self.act_fn(self.gate_proj(x)) * self.up_proj(x)
        hidden_states = self.dropout(hidden_states)
        return self.down_proj(hidden_states)


class T5Gemma2RotaryEmbedding(nn.Module):
    def __init__(self, config: T5Gemma2TextConfig, device=None):
        super().__init__()
        self.config = config
        self.layer_types = sorted(set(config.layer_types))
        for layer_type in self.layer_types:
            inv_freq, attention_scaling = self._compute_inv_freq(
                config, layer_type, device=device
            )
            self.register_buffer(f"{layer_type}_inv_freq", inv_freq, persistent=False)
            setattr(self, f"{layer_type}_attention_scaling", attention_scaling)

    @staticmethod
    def _compute_inv_freq(
        config: T5Gemma2TextConfig, layer_type: str, device=None
    ) -> tuple[torch.Tensor, float]:
        rope_params = config.rope_parameters[layer_type]
        rope_type = rope_params.get("rope_type", "default")
        base = rope_params["rope_theta"]
        dim = (
            getattr(config, "head_dim", None)
            or config.hidden_size // config.num_attention_heads
        )
        inv_freq = 1.0 / (
            base ** (torch.arange(0, dim, 2, device=device, dtype=torch.float32) / dim)
        )
        if rope_type == "linear":
            inv_freq = inv_freq / float(rope_params["factor"])
        elif rope_type != "default":
            raise ValueError(
                f"Unsupported T5Gemma2 rope_type in local compat shim: {rope_type}"
            )
        return inv_freq, 1.0

    def forward(self, x: torch.Tensor, position_ids: torch.LongTensor, layer_type: str):
        inv_freq = getattr(self, f"{layer_type}_inv_freq")
        attention_scaling = getattr(self, f"{layer_type}_attention_scaling")
        inv_freq_expanded = (
            inv_freq[None, :, None]
            .float()
            .expand(position_ids.shape[0], -1, 1)
            .to(x.device)
        )
        position_ids_expanded = position_ids[:, None, :].float()

        device_type = (
            x.device.type
            if isinstance(x.device.type, str) and x.device.type != "mps"
            else "cpu"
        )
        with torch.autocast(device_type=device_type, enabled=False):
            freqs = (
                inv_freq_expanded.float() @ position_ids_expanded.float()
            ).transpose(1, 2)
            emb = torch.cat((freqs, freqs), dim=-1)
            cos = emb.cos() * attention_scaling
            sin = emb.sin() * attention_scaling

        return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype)


def rotate_half(x: torch.Tensor) -> torch.Tensor:
    x1 = x[..., : x.shape[-1] // 2]
    x2 = x[..., x.shape[-1] // 2 :]
    return torch.cat((-x2, x1), dim=-1)


def apply_rotary_pos_emb(
    q: torch.Tensor,
    k: torch.Tensor,
    cos: torch.Tensor,
    sin: torch.Tensor,
    unsqueeze_dim=1,
):
    cos = cos.unsqueeze(unsqueeze_dim)
    sin = sin.unsqueeze(unsqueeze_dim)
    q_embed = (q * cos) + (rotate_half(q) * sin)
    k_embed = (k * cos) + (rotate_half(k) * sin)
    return q_embed, k_embed


def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
    batch, num_key_value_heads, slen, head_dim = hidden_states.shape
    if n_rep == 1:
        return hidden_states
    hidden_states = hidden_states[:, :, None, :, :].expand(
        batch, num_key_value_heads, n_rep, slen, head_dim
    )
    return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)


def eager_attention_forward(
    module: nn.Module,
    query: torch.Tensor,
    key: torch.Tensor,
    value: torch.Tensor,
    attention_mask: torch.Tensor | None,
    dropout: float = 0.0,
    scaling: float | None = None,
    softcap: float | None = None,
    **kwargs,
):
    if scaling is None:
        scaling = module.head_dim**-0.5
    key_states = repeat_kv(key, module.num_key_value_groups)
    value_states = repeat_kv(value, module.num_key_value_groups)
    attn_weights = torch.matmul(query, key_states.transpose(2, 3)) * scaling
    if softcap is not None:
        attn_weights = torch.tanh(attn_weights / softcap) * softcap
    if attention_mask is not None:
        attn_weights = attn_weights + attention_mask[:, :, :, : key_states.shape[-2]]
    attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(
        query.dtype
    )
    attn_weights = nn.functional.dropout(
        attn_weights, p=dropout, training=module.training
    )
    attn_output = torch.matmul(attn_weights, value_states)
    attn_output = attn_output.transpose(1, 2).contiguous()
    return attn_output, attn_weights


def _padding_mask_to_additive_mask(
    attention_mask: torch.Tensor | None,
    query: torch.Tensor,
    layer_type: str,
    sliding_window: int | None,
) -> torch.Tensor | None:
    batch_size, _, seq_len, _ = query.shape
    device = query.device
    dtype = query.dtype

    has_padding = attention_mask is not None and not bool(
        attention_mask.to(torch.bool).all()
    )
    if layer_type == "full_attention" and not has_padding:
        return None

    if attention_mask is None:
        valid_kv = torch.ones(batch_size, seq_len, device=device, dtype=torch.bool)
    else:
        valid_kv = attention_mask.to(device=device, dtype=torch.bool)
    allowed = valid_kv[:, None, None, :].expand(batch_size, 1, seq_len, seq_len)

    if layer_type == "sliding_attention":
        if sliding_window is None:
            raise ValueError(
                "T5Gemma2 sliding_attention requires config.sliding_window"
            )
        q_idx = torch.arange(seq_len, device=device)[:, None]
        kv_idx = torch.arange(seq_len, device=device)[None, :]
        left_window_size = (sliding_window + 1) // 2
        right_window_size = sliding_window // 2 + 1
        dist = q_idx - kv_idx
        local = ((dist >= 0) & (dist < left_window_size)) | (
            (dist < 0) & (-dist < right_window_size)
        )
        allowed = allowed & local[None, None, :, :]

    additive_mask = torch.zeros(
        (batch_size, 1, seq_len, seq_len), device=device, dtype=dtype
    )
    additive_mask.masked_fill_(~allowed, torch.finfo(dtype).min)
    return additive_mask


def t5gemma2_flash_attention_forward(
    module: nn.Module,
    query: torch.Tensor,
    key: torch.Tensor,
    value: torch.Tensor,
    attention_mask: torch.Tensor | None,
    dropout: float = 0.0,
    scaling: float | None = None,
    sliding_window: int | None = None,
    softcap: float | None = None,
    **kwargs,
):
    if query.device.type != "cuda" or query.dtype not in (
        torch.float16,
        torch.bfloat16,
    ):
        additive_mask = _padding_mask_to_additive_mask(
            attention_mask,
            query,
            module.layer_type,
            module.config.sliding_window,
        )
        return eager_attention_forward(
            module,
            query,
            key,
            value,
            additive_mask,
            dropout=dropout,
            scaling=scaling,
            softcap=softcap,
            **kwargs,
        )

    try:
        from flash_attn import flash_attn_func, flash_attn_varlen_func
        from flash_attn.bert_padding import pad_input, unpad_input
    except ImportError as exc:
        raise ImportError(
            "flash_attn is required for T5Gemma2 flash_attention_2"
        ) from exc

    query = query.transpose(1, 2).contiguous()
    key = key.transpose(1, 2).contiguous()
    value = value.transpose(1, 2).contiguous()
    batch_size, seq_len = query.shape[:2]
    dropout_p = dropout if module.training else 0.0
    softcap_value = 0.0 if softcap is None else softcap

    window_size = (-1, -1)
    if module.layer_type == "sliding_attention":
        if sliding_window is None:
            raise ValueError(
                "T5Gemma2 sliding_attention requires config.sliding_window"
            )
        left_window_size = (sliding_window + 1) // 2
        right_window_size = sliding_window // 2 + 1
        window_size = (left_window_size - 1, right_window_size - 1)

    has_padding = attention_mask is not None and not bool(
        attention_mask.to(torch.bool).all()
    )
    if has_padding:
        attention_mask = attention_mask.to(device=query.device, dtype=torch.bool)
        query_unpad, indices_q, cu_seqlens_q, max_seqlen_q, _ = unpad_input(
            query, attention_mask
        )
        key_unpad, _, cu_seqlens_k, max_seqlen_k, _ = unpad_input(key, attention_mask)
        value_unpad, _, _, _, _ = unpad_input(value, attention_mask)
        attn_output_unpad = flash_attn_varlen_func(
            query_unpad,
            key_unpad,
            value_unpad,
            cu_seqlens_q,
            cu_seqlens_k,
            max_seqlen_q,
            max_seqlen_k,
            dropout_p=dropout_p,
            softmax_scale=scaling,
            causal=False,
            window_size=window_size,
            softcap=softcap_value,
        )
        attn_output = pad_input(attn_output_unpad, indices_q, batch_size, seq_len)
    else:
        attn_output = flash_attn_func(
            query,
            key,
            value,
            dropout_p=dropout_p,
            softmax_scale=scaling,
            causal=False,
            window_size=window_size,
            softcap=softcap_value,
        )

    return attn_output, None


class T5Gemma2SelfAttention(nn.Module):
    def __init__(self, config: T5Gemma2TextConfig, layer_idx: int):
        super().__init__()
        self.config = config
        self.layer_idx = layer_idx
        self.layer_type = config.layer_types[layer_idx]
        self.head_dim = getattr(
            config, "head_dim", config.hidden_size // config.num_attention_heads
        )
        self.num_key_value_groups = (
            config.num_attention_heads // config.num_key_value_heads
        )
        self.scaling = config.query_pre_attn_scalar**-0.5
        self.attention_dropout = config.attention_dropout
        self.is_causal = False
        self.sliding_window = (
            config.sliding_window if self.layer_type == "sliding_attention" else None
        )

        self.q_proj = nn.Linear(
            config.hidden_size,
            config.num_attention_heads * self.head_dim,
            bias=config.attention_bias,
        )
        self.k_proj = nn.Linear(
            config.hidden_size,
            config.num_key_value_heads * self.head_dim,
            bias=config.attention_bias,
        )
        self.v_proj = nn.Linear(
            config.hidden_size,
            config.num_key_value_heads * self.head_dim,
            bias=config.attention_bias,
        )
        self.o_proj = nn.Linear(
            config.num_attention_heads * self.head_dim,
            config.hidden_size,
            bias=config.attention_bias,
        )
        self.q_norm = T5Gemma2RMSNorm(dim=self.head_dim, eps=config.rms_norm_eps)
        self.k_norm = T5Gemma2RMSNorm(dim=self.head_dim, eps=config.rms_norm_eps)

    def forward(
        self,
        hidden_states: torch.Tensor,
        position_embeddings: tuple[torch.Tensor, torch.Tensor],
        attention_mask: torch.Tensor | None = None,
        **kwargs,
    ):
        input_shape = hidden_states.shape[:-1]
        hidden_shape = (*input_shape, -1, self.head_dim)

        query_states = self.q_proj(hidden_states).view(hidden_shape).transpose(1, 2)
        key_states = self.k_proj(hidden_states).view(hidden_shape).transpose(1, 2)
        value_states = self.v_proj(hidden_states).view(hidden_shape).transpose(1, 2)

        query_states = self.q_norm(query_states)
        key_states = self.k_norm(key_states)

        cos, sin = position_embeddings
        query_states, key_states = apply_rotary_pos_emb(
            query_states, key_states, cos, sin
        )

        attn_impl = getattr(self.config, "_attn_implementation", None) or getattr(
            self.config,
            "preferred_attn_implementation",
            "flash_attention_2",
        )
        if attn_impl == "flash_attention_2":
            attention_interface = t5gemma2_flash_attention_forward
        else:
            attention_interface = (
                eager_attention_forward
                if attn_impl == "eager"
                else ALL_ATTENTION_FUNCTIONS[attn_impl]
            )
        attn_output, attn_weights = attention_interface(
            self,
            query_states,
            key_states,
            value_states,
            attention_mask,
            dropout=self.attention_dropout if self.training else 0.0,
            scaling=self.scaling,
            sliding_window=self.sliding_window,
            softcap=self.config.attn_logit_softcapping,
            **kwargs,
        )

        attn_output = attn_output.reshape(*input_shape, -1).contiguous()
        attn_output = self.o_proj(attn_output)
        return attn_output, attn_weights


class T5Gemma2EncoderLayer(GradientCheckpointingLayer):
    def __init__(self, config: T5Gemma2TextConfig, layer_idx: int):
        super().__init__()
        self.attention_type = config.layer_types[layer_idx]
        self.self_attn = T5Gemma2SelfAttention(config=config, layer_idx=layer_idx)
        self.pre_self_attn_layernorm = T5Gemma2RMSNorm(
            config.hidden_size, eps=config.rms_norm_eps
        )
        self.post_self_attn_layernorm = T5Gemma2RMSNorm(
            config.hidden_size, eps=config.rms_norm_eps
        )
        self.mlp = T5Gemma2MLP(config)
        self.pre_feedforward_layernorm = T5Gemma2RMSNorm(
            config.hidden_size, eps=config.rms_norm_eps
        )
        self.post_feedforward_layernorm = T5Gemma2RMSNorm(
            config.hidden_size, eps=config.rms_norm_eps
        )
        self.dropout = nn.Dropout(config.dropout_rate)

    def forward(
        self,
        hidden_states: torch.Tensor,
        position_embeddings: tuple[torch.Tensor, torch.Tensor],
        attention_mask: torch.Tensor | None = None,
        **kwargs,
    ) -> torch.Tensor:
        residual = hidden_states
        hidden_states = self.pre_self_attn_layernorm(hidden_states)
        hidden_states, _ = self.self_attn(
            hidden_states=hidden_states,
            position_embeddings=position_embeddings,
            attention_mask=attention_mask,
            **kwargs,
        )
        hidden_states = self.post_self_attn_layernorm(hidden_states)
        hidden_states = residual + self.dropout(hidden_states)

        residual = hidden_states
        hidden_states = self.pre_feedforward_layernorm(hidden_states)
        hidden_states = self.mlp(hidden_states)
        hidden_states = self.post_feedforward_layernorm(hidden_states)
        hidden_states = residual + self.dropout(hidden_states)
        return hidden_states


class T5Gemma2TextScaledWordEmbedding(nn.Embedding):
    def __init__(
        self,
        num_embeddings: int,
        embedding_dim: int,
        padding_idx: int | None,
        embed_scale: float = 1.0,
        eoi_token_index: int = 256000,
        device=None,
        dtype=None,
    ):
        super().__init__(
            num_embeddings, embedding_dim, padding_idx, device=device, dtype=dtype
        )
        self.scalar_embed_scale = embed_scale
        self.register_buffer("embed_scale", torch.tensor(embed_scale), persistent=False)
        self.eoi_token_index = eoi_token_index
        self.eoi_embedding = nn.Parameter(
            torch.zeros(self.embedding_dim, device=device, dtype=dtype)
        )

    def forward(self, input_ids: torch.Tensor):
        input_embeddings = super().forward(input_ids) * self.embed_scale.to(
            self.weight.dtype
        )
        eoi_mask = input_ids == self.eoi_token_index
        return torch.where(
            eoi_mask.unsqueeze(-1),
            self.eoi_embedding.to(input_embeddings.dtype),
            input_embeddings,
        )


class T5Gemma2TextEncoder(PreTrainedModel):
    config_class = T5Gemma2TextConfig
    base_model_prefix = "model"
    supports_gradient_checkpointing = True
    _no_split_modules = ["T5Gemma2EncoderLayer"]
    _supports_flash_attn = True
    _supports_sdpa = True
    _supports_attention_backend = True

    def __init__(self, config: T5Gemma2TextConfig):
        super().__init__(config)
        self.padding_idx = config.pad_token_id
        self.vocab_size = config.vocab_size
        self.embed_tokens = T5Gemma2TextScaledWordEmbedding(
            config.vocab_size,
            config.hidden_size,
            self.padding_idx,
            embed_scale=config.hidden_size**0.5,
            eoi_token_index=config.eoi_token_index,
        )
        self.norm = T5Gemma2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
        self.layers = nn.ModuleList(
            [
                T5Gemma2EncoderLayer(config, layer_idx)
                for layer_idx in range(config.num_hidden_layers)
            ]
        )
        self.dropout = nn.Dropout(config.dropout_rate)
        self.rotary_emb = T5Gemma2RotaryEmbedding(config)
        self.gradient_checkpointing = False
        self.post_init()

    def get_input_embeddings(self):
        return self.embed_tokens

    def set_input_embeddings(self, new_embeddings):
        self.embed_tokens = new_embeddings
        self.vocab_size = new_embeddings.num_embeddings

    def _get_resized_embeddings(
        self,
        old_embeddings: nn.Embedding,
        new_num_tokens: int | None = None,
        pad_to_multiple_of: int | None = None,
        mean_resizing: bool = True,
    ) -> nn.Embedding:
        if new_num_tokens is None:
            return old_embeddings
        if pad_to_multiple_of is not None:
            new_num_tokens = (
                (new_num_tokens + pad_to_multiple_of - 1) // pad_to_multiple_of
            ) * pad_to_multiple_of

        old_num_tokens, old_embedding_dim = old_embeddings.weight.shape
        if old_num_tokens == new_num_tokens:
            return old_embeddings
        padding_idx = old_embeddings.padding_idx
        if padding_idx is not None and padding_idx >= new_num_tokens:
            padding_idx = None
        new_embeddings = T5Gemma2TextScaledWordEmbedding(
            new_num_tokens,
            old_embedding_dim,
            padding_idx,
            embed_scale=getattr(
                old_embeddings, "scalar_embed_scale", self.config.hidden_size**0.5
            ),
            eoi_token_index=getattr(
                old_embeddings, "eoi_token_index", self.config.eoi_token_index
            ),
            device=old_embeddings.weight.device,
            dtype=old_embeddings.weight.dtype,
        )
        self._init_weights(new_embeddings)
        num_tokens_to_copy = min(old_num_tokens, new_num_tokens)
        new_embeddings.weight.data[:num_tokens_to_copy] = old_embeddings.weight.data[
            :num_tokens_to_copy
        ]
        if hasattr(old_embeddings, "eoi_embedding"):
            new_embeddings.eoi_embedding.data.copy_(old_embeddings.eoi_embedding.data)
        return new_embeddings

    def _init_weights(self, module):
        std = self.config.initializer_range
        if isinstance(module, nn.Linear):
            module.weight.data.normal_(mean=0.0, std=std)
            if module.bias is not None:
                module.bias.data.zero_()
        elif isinstance(module, T5Gemma2TextScaledWordEmbedding):
            module.weight.data.normal_(mean=0.0, std=std)
            if module.padding_idx is not None:
                module.weight.data[module.padding_idx].zero_()
            module.eoi_embedding.data.zero_()
            module.embed_scale.fill_(module.scalar_embed_scale)
        elif isinstance(module, T5Gemma2RMSNorm):
            module.weight.data.zero_()

    @staticmethod
    def _build_additive_attention_mask(
        attention_mask: torch.Tensor | None,
        inputs_embeds: torch.Tensor,
        layer_type: str,
        sliding_window: int | None,
    ) -> torch.Tensor | None:
        batch_size, seq_len = inputs_embeds.shape[:2]
        device = inputs_embeds.device

        has_padding = attention_mask is not None
        if layer_type == "full_attention" and not has_padding:
            return None

        if attention_mask is None:
            valid_kv = torch.ones(batch_size, seq_len, device=device, dtype=torch.bool)
        else:
            valid_kv = attention_mask.to(device=device, dtype=torch.bool)
        allowed = valid_kv[:, None, None, :].expand(batch_size, 1, seq_len, seq_len)

        if layer_type == "sliding_attention":
            if sliding_window is None:
                raise ValueError(
                    "T5Gemma2 sliding_attention requires config.sliding_window"
                )
            q_idx = torch.arange(seq_len, device=device)[:, None]
            kv_idx = torch.arange(seq_len, device=device)[None, :]
            left_window_size = (sliding_window + 1) // 2
            right_window_size = sliding_window // 2 + 1
            dist = q_idx - kv_idx
            local = ((dist >= 0) & (dist < left_window_size)) | (
                (dist < 0) & (-dist < right_window_size)
            )
            allowed = allowed & local[None, None, :, :]

        min_value = torch.finfo(inputs_embeds.dtype).min
        additive_mask = torch.zeros(
            (batch_size, 1, seq_len, seq_len), device=device, dtype=inputs_embeds.dtype
        )
        additive_mask.masked_fill_(~allowed, min_value)
        return additive_mask

    def forward(
        self,
        input_ids: torch.LongTensor | None = None,
        attention_mask: torch.Tensor | None = None,
        position_ids: torch.LongTensor | None = None,
        inputs_embeds: torch.FloatTensor | None = None,
        output_hidden_states: bool | None = None,
        return_dict: bool | None = None,
        token_type_ids: torch.Tensor | None = None,
        **kwargs,
    ) -> BaseModelOutput:
        if (input_ids is None) == (inputs_embeds is None):
            raise ValueError(
                "You must specify exactly one of input_ids or inputs_embeds"
            )
        kwargs.pop("past_key_values", None)
        kwargs.pop("use_cache", None)

        output_hidden_states = (
            output_hidden_states
            if output_hidden_states is not None
            else self.config.output_hidden_states
        )
        return_dict = (
            return_dict if return_dict is not None else self.config.use_return_dict
        )

        if inputs_embeds is None:
            inputs_embeds = self.embed_tokens(input_ids)
        if position_ids is None:
            position_ids = torch.arange(
                0, inputs_embeds.shape[1], device=inputs_embeds.device
            ).unsqueeze(0)

        hidden_states = self.dropout(inputs_embeds)
        all_hidden_states = () if output_hidden_states else None

        position_embeddings = {
            layer_type: self.rotary_emb(hidden_states, position_ids, layer_type)
            for layer_type in set(self.config.layer_types)
        }
        attn_impl = getattr(self.config, "_attn_implementation", None) or getattr(
            self.config,
            "preferred_attn_implementation",
            "flash_attention_2",
        )
        if attn_impl == "flash_attention_2":
            padding_mask = None
            if getattr(self, "_force_static_attention_mask", False) or attention_mask is not None and not bool(
                attention_mask.to(torch.bool).all()
            ):
                padding_mask = attention_mask
            attn_masks = {
                layer_type: padding_mask for layer_type in set(self.config.layer_types)
            }
        else:
            attn_masks = {
                layer_type: self._build_additive_attention_mask(
                    attention_mask,
                    inputs_embeds,
                    layer_type,
                    self.config.sliding_window,
                )
                for layer_type in set(self.config.layer_types)
            }

        for layer_module in self.layers[: self.config.num_hidden_layers]:
            if output_hidden_states:
                all_hidden_states += (hidden_states,)
            hidden_states = layer_module(
                hidden_states,
                position_embeddings[layer_module.attention_type],
                attn_masks[layer_module.attention_type],
                output_attentions=False,
                **kwargs,
            )

        hidden_states = self.norm(hidden_states)
        hidden_states = self.dropout(hidden_states)
        if output_hidden_states:
            all_hidden_states += (hidden_states,)

        if not return_dict:
            return tuple(v for v in [hidden_states, all_hidden_states] if v is not None)
        return BaseModelOutput(
            last_hidden_state=hidden_states, hidden_states=all_hidden_states
        )