File size: 28,823 Bytes
d9a2e19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d117d0
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
import math
import os
import torch
from modules.Attention import Attention
from modules.Device import Device
from modules.SD15 import SDClip, SDToken
from modules.cond import cast
from transformers import T5TokenizerFast

activations = {
    "gelu_pytorch_tanh": lambda a: torch.nn.functional.gelu(a, approximate="tanh"),
    "relu": torch.nn.functional.relu,
}

class T5DenseGatedActDense(torch.nn.Module):
    """#### Dense Gated Activation Layer"""
    def __init__(self, model_dim: int, ff_dim: int, ff_activation: str, dtype: torch.dtype, device: torch.device, operations):
        """#### Initialize Dense Gated Activation Layer



        #### Args:

            - `model_dim` (int): Model dimension.

            - `ff_dim` (int): Feedforward dimension.

            - `ff_activation` (str): Feedforward activation function.

            - `dtype` (torch.dtype): Data type.

            - `device` (torch.device): Device.

            - `operations` (Operations): Operations.

        """
        super().__init__()
        self.wi_0 = operations.Linear(
            model_dim, ff_dim, bias=False, dtype=dtype, device=device
        )
        self.wi_1 = operations.Linear(
            model_dim, ff_dim, bias=False, dtype=dtype, device=device
        )
        self.wo = operations.Linear(
            ff_dim, model_dim, bias=False, dtype=dtype, device=device
        )
        # self.dropout = nn.Dropout(config.dropout_rate)
        self.act = activations[ff_activation]

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """#### Forward Pass

        

        #### Args:

            - `x` (torch.Tensor): Input tensor.

            

        #### Returns:

            - `torch.Tensor`: Output tensor.

        """
        hidden_gelu = self.act(self.wi_0(x))
        hidden_linear = self.wi_1(x)
        x = hidden_gelu * hidden_linear
        # x = self.dropout(x)
        x = self.wo(x)
        return x


class T5LayerFF(torch.nn.Module):
    """#### Feedforward Layer"""
    def __init__(

        self, model_dim: int, ff_dim: int, ff_activation: str, gated_act: bool, dtype: torch.dtype, device: torch.device, operations

    ):
        """#### Initialize Feedforward Layer

        

        #### Args:

            - `model_dim` (int): Model dimension.

            - `ff_dim` (int): Feedforward dimension.

            - `ff_activation` (str): Feedforward activation function.

            - `gated_act` (bool): Whether to use gated activation.

            - `dtype` (torch.dtype): Data type.

            - `device` (torch.device): Device.

            - `operations` (Operations): Operations.

        """
        super().__init__()
        if gated_act:
            self.DenseReluDense = T5DenseGatedActDense(
                model_dim, ff_dim, ff_activation, dtype, device, operations
            )

        self.layer_norm = T5LayerNorm(
            model_dim, dtype=dtype, device=device, operations=operations
        )
        # self.dropout = nn.Dropout(config.dropout_rate)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """#### Forward Pass

        

        #### Args:

            - `x` (torch.Tensor): Input tensor.

            

        #### Returns:

            - `torch.Tensor`: Output tensor.

        """
        forwarded_states = self.layer_norm(x)
        forwarded_states = self.DenseReluDense(forwarded_states)
        # x = x + self.dropout(forwarded_states)
        x += forwarded_states
        return x


class T5Attention(torch.nn.Module):
    """#### Attention Layer"""
    def __init__(

        self,

        model_dim: int,

        inner_dim: int,

        num_heads: int,

        relative_attention_bias: bool,

        dtype: torch.dtype,

        device: torch.device,

        operations,

    ):
        """#### Initialize Attention Layer

        

        #### Args:

            - `model_dim` (int): Model dimension.

            - `inner_dim` (int): Inner dimension.

            - `num_heads` (int): Number of attention heads.

            - `relative_attention_bias` (bool): Whether to use relative attention bias.

            - `dtype` (torch.dtype): Data type.

            - `device` (torch.device): Device.

            - `operations` (Operations): Operations.

        """
        super().__init__()

        # Mesh TensorFlow initialization to avoid scaling before softmax
        self.q = operations.Linear(
            model_dim, inner_dim, bias=False, dtype=dtype, device=device
        )
        self.k = operations.Linear(
            model_dim, inner_dim, bias=False, dtype=dtype, device=device
        )
        self.v = operations.Linear(
            model_dim, inner_dim, bias=False, dtype=dtype, device=device
        )
        self.o = operations.Linear(
            inner_dim, model_dim, bias=False, dtype=dtype, device=device
        )
        self.num_heads = num_heads

        self.relative_attention_bias = None
        if relative_attention_bias:
            self.relative_attention_num_buckets = 32
            self.relative_attention_max_distance = 128
            self.relative_attention_bias = operations.Embedding(
                self.relative_attention_num_buckets,
                self.num_heads,
                device=device,
                dtype=dtype,
            )

    @staticmethod
    def _relative_position_bucket(

        relative_position: torch.Tensor, bidirectional: bool = True, num_buckets: int = 32, max_distance: int = 128

    ) -> torch.Tensor:
        """

        Adapted from Mesh Tensorflow:

        https://github.com/tensorflow/mesh/blob/0cb87fe07da627bf0b7e60475d59f95ed6b5be3d/mesh_tensorflow/transformer/transformer_layers.py#L593



        Translate relative position to a bucket number for relative attention. The relative position is defined as

        memory_position - query_position, i.e. the distance in tokens from the attending position to the attended-to

        position. If bidirectional=False, then positive relative positions are invalid. We use smaller buckets for

        small absolute relative_position and larger buckets for larger absolute relative_positions. All relative

        positions >=max_distance map to the same bucket. All relative positions <=-max_distance map to the same bucket.

        This should allow for more graceful generalization to longer sequences than the model has been trained on



        #### Args:

            - `relative_position` (torch.Tensor): Relative position tensor.

            - `bidirectional` (bool): Whether the attention is bidirectional.

            - `num_buckets` (int): Number of buckets.

            - `max_distance` (int): Maximum distance.



        #### Returns:

            - `torch.Tensor`: Bucketed relative positions.

        """
        relative_buckets = 0
        if bidirectional:
            num_buckets //= 2
            relative_buckets += (relative_position > 0).to(torch.long) * num_buckets
            relative_position = torch.abs(relative_position)
        else:
            relative_position = -torch.min(
                relative_position, torch.zeros_like(relative_position)
            )
        # now relative_position is in the range [0, inf)

        # half of the buckets are for exact increments in positions
        max_exact = num_buckets // 2
        is_small = relative_position < max_exact

        # The other half of the buckets are for logarithmically bigger bins in positions up to max_distance
        relative_position_if_large = max_exact + (
            torch.log(relative_position.float() / max_exact)
            / math.log(max_distance / max_exact)
            * (num_buckets - max_exact)
        ).to(torch.long)
        relative_position_if_large = torch.min(
            relative_position_if_large,
            torch.full_like(relative_position_if_large, num_buckets - 1),
        )

        relative_buckets += torch.where(
            is_small, relative_position, relative_position_if_large
        )
        return relative_buckets

    def compute_bias(self, query_length: int, key_length: int, device: torch.device, dtype: torch.dtype) -> torch.Tensor:
        """#### Compute binned relative position bias

        

        #### Args:

            - `query_length` (int): Length of the query.

            - `key_length` (int): Length of the key.

            - `device` (torch.device): Device.

            - `dtype` (torch.dtype): Data type.

            

        #### Returns:

            - `torch.Tensor`: Computed bias.

        """
        context_position = torch.arange(query_length, dtype=torch.long, device=device)[
            :, None
        ]
        memory_position = torch.arange(key_length, dtype=torch.long, device=device)[
            None, :
        ]
        relative_position = (
            memory_position - context_position
        )  # shape (query_length, key_length)
        relative_position_bucket = self._relative_position_bucket(
            relative_position,  # shape (query_length, key_length)
            bidirectional=True,
            num_buckets=self.relative_attention_num_buckets,
            max_distance=self.relative_attention_max_distance,
        )
        values = self.relative_attention_bias(
            relative_position_bucket, out_dtype=dtype
        )  # shape (query_length, key_length, num_heads)
        values = values.permute([2, 0, 1]).unsqueeze(
            0
        )  # shape (1, num_heads, query_length, key_length)
        return values

    def forward(self, x: torch.Tensor, mask: torch.Tensor = None, past_bias: torch.Tensor = None, optimized_attention = None) -> torch.Tensor:
        """#### Forward Pass

        

        #### Args:

            - `x` (torch.Tensor): Input tensor.

            - `mask` (torch.Tensor, optional): Attention mask. Defaults to None.

            - `past_bias` (torch.Tensor, optional): Past bias. Defaults to None.

            - `optimized_attention` (callable, optional): Optimized attention function. Defaults to None.

            

        #### Returns:

            - `torch.Tensor`: Output tensor.

        """
        q = self.q(x)
        k = self.k(x)
        v = self.v(x)
        if self.relative_attention_bias is not None:
            past_bias = self.compute_bias(x.shape[1], x.shape[1], x.device, x.dtype)

        if past_bias is not None:
            if mask is not None:
                mask = mask + past_bias
            else:
                mask = past_bias

        out = optimized_attention(
            q, k * ((k.shape[-1] / self.num_heads) ** 0.5), v, self.num_heads, mask
        )
        return self.o(out), past_bias


class T5LayerSelfAttention(torch.nn.Module):
    """#### Self-Attention Layer"""
    def __init__(

        self,

        model_dim: int,

        inner_dim: int,

        ff_dim: int,

        num_heads: int,

        relative_attention_bias: bool,

        dtype: torch.dtype,

        device: torch.device,

        operations,

    ):
        """#### Initialize Self-Attention Layer

        

        #### Args:

            - `model_dim` (int): Model dimension.

            - `inner_dim` (int): Inner dimension.

            - `ff_dim` (int): Feedforward dimension.

            - `num_heads` (int): Number of attention heads.

            - `relative_attention_bias` (bool): Whether to use relative attention bias.

            - `dtype` (torch.dtype): Data type.

            - `device` (torch.device): Device.

            - `operations` (Operations): Operations.

        """
        super().__init__()
        self.SelfAttention = T5Attention(
            model_dim,
            inner_dim,
            num_heads,
            relative_attention_bias,
            dtype,
            device,
            operations,
        )
        self.layer_norm = T5LayerNorm(
            model_dim, dtype=dtype, device=device, operations=operations
        )
        # self.dropout = nn.Dropout(config.dropout_rate)

    def forward(self, x: torch.Tensor, mask: torch.Tensor = None, past_bias: torch.Tensor = None, optimized_attention = None) -> torch.Tensor:
        """#### Forward Pass

        

        #### Args:

            - `x` (torch.Tensor): Input tensor.

            - `mask` (torch.Tensor, optional): Attention mask. Defaults to None.

            - `past_bias` (torch.Tensor, optional): Past bias. Defaults to None.

            - `optimized_attention` (callable, optional): Optimized attention function. Defaults to None.

            

        #### Returns:

            - `torch.Tensor`: Output tensor.

        """
        self.layer_norm(x)
        output, past_bias = self.SelfAttention(
            self.layer_norm(x),
            mask=mask,
            past_bias=past_bias,
            optimized_attention=optimized_attention,
        )
        # x = x + self.dropout(attention_output)
        x += output
        return x, past_bias


class T5Block(torch.nn.Module):
    """#### T5 Block"""
    def __init__(

        self,

        model_dim: int,

        inner_dim: int,

        ff_dim: int,

        ff_activation: str,

        gated_act: bool,

        num_heads: int,

        relative_attention_bias: bool,

        dtype: torch.dtype,

        device: torch.device,

        operations,

    ):
        """#### Initialize T5 Block

        

        #### Args:

            - `model_dim` (int): Model dimension.

            - `inner_dim` (int): Inner dimension.

            - `ff_dim` (int): Feedforward dimension.

            - `ff_activation` (str): Feedforward activation function.

            - `gated_act` (bool): Whether to use gated activation.

            - `num_heads` (int): Number of attention heads.

            - `relative_attention_bias` (bool): Whether to use relative attention bias.

            - `dtype` (torch.dtype): Data type.

            - `device` (torch.device): Device.

            - `operations` (Operations): Operations.

        """
        super().__init__()
        self.layer = torch.nn.ModuleList()
        self.layer.append(
            T5LayerSelfAttention(
                model_dim,
                inner_dim,
                ff_dim,
                num_heads,
                relative_attention_bias,
                dtype,
                device,
                operations,
            )
        )
        self.layer.append(
            T5LayerFF(
                model_dim, ff_dim, ff_activation, gated_act, dtype, device, operations
            )
        )

    def forward(self, x: torch.Tensor, mask: torch.Tensor = None, past_bias: torch.Tensor = None, optimized_attention = None) -> torch.Tensor:
        """#### Forward Pass

        

        #### Args:

            - `x` (torch.Tensor): Input tensor.

            - `mask` (torch.Tensor, optional): Attention mask. Defaults to None.

            - `past_bias` (torch.Tensor, optional): Past bias. Defaults to None.

            - `optimized_attention` (callable, optional): Optimized attention function. Defaults to None.

            

        #### Returns:

            - `torch.Tensor`: Output tensor.

        """
        x, past_bias = self.layer[0](x, mask, past_bias, optimized_attention)
        x = self.layer[-1](x)
        return x, past_bias


class T5Stack(torch.nn.Module):
    """#### T5 Stack"""
    def __init__(

        self,

        num_layers: int,

        model_dim: int,

        inner_dim: int,

        ff_dim: int,

        ff_activation: str,

        gated_act: bool,

        num_heads: int,

        relative_attention: bool,

        dtype: torch.dtype,

        device: torch.device,

        operations,

    ):
        """#### Initialize T5 Stack

        

        #### Args:

            - `num_layers` (int): Number of layers.

            - `model_dim` (int): Model dimension.

            - `inner_dim` (int): Inner dimension.

            - `ff_dim` (int): Feedforward dimension.

            - `ff_activation` (str): Feedforward activation function.

            - `gated_act` (bool): Whether to use gated activation.

            - `num_heads` (int): Number of attention heads.

            - `relative_attention` (bool): Whether to use relative attention.

            - `dtype` (torch.dtype): Data type.

            - `device` (torch.device): Device.

            - `operations` (Operations): Operations.

        """
        super().__init__()

        self.block = torch.nn.ModuleList(
            [
                T5Block(
                    model_dim,
                    inner_dim,
                    ff_dim,
                    ff_activation,
                    gated_act,
                    num_heads,
                    relative_attention_bias=((not relative_attention) or (i == 0)),
                    dtype=dtype,
                    device=device,
                    operations=operations,
                )
                for i in range(num_layers)
            ]
        )
        self.final_layer_norm = T5LayerNorm(
            model_dim, dtype=dtype, device=device, operations=operations
        )
        # self.dropout = nn.Dropout(config.dropout_rate)

    def forward(

        self,

        x: torch.Tensor,

        attention_mask: torch.Tensor = None,

        intermediate_output: int = None,

        final_layer_norm_intermediate: bool = True,

        dtype: torch.dtype = None,

    ) -> torch.Tensor:
        """#### Forward Pass

        

        #### Args:

            - `x` (torch.Tensor): Input tensor.

            - `attention_mask` (torch.Tensor, optional): Attention mask. Defaults to None.

            - `intermediate_output` (int, optional): Intermediate output index. Defaults to None.

            - `final_layer_norm_intermediate` (bool, optional): Whether to apply final layer norm to intermediate output. Defaults to True.

            - `dtype` (torch.dtype, optional): Data type. Defaults to None.

            

        #### Returns:

            - `torch.Tensor`: Output tensor.

        """
        mask = None
        if attention_mask is not None:
            mask = 1.0 - attention_mask.to(x.dtype).reshape(
                (attention_mask.shape[0], 1, -1, attention_mask.shape[-1])
            ).expand(
                attention_mask.shape[0],
                1,
                attention_mask.shape[-1],
                attention_mask.shape[-1],
            )
            mask = mask.masked_fill(mask.to(torch.bool), float("-inf"))

        intermediate = None
        optimized_attention = Attention.optimized_attention_for_device()
        past_bias = None
        for i, l in enumerate(self.block):
            x, past_bias = l(x, mask, past_bias, optimized_attention)
            if i == intermediate_output:
                intermediate = x.clone()
        x = self.final_layer_norm(x)
        if intermediate is not None and final_layer_norm_intermediate:
            intermediate = self.final_layer_norm(intermediate)
        return x, intermediate

class T5(torch.nn.Module):
    def __init__(self, config_dict, dtype, device, operations):
        """#### Initialize T5 Model



        #### Args:

            - `config_dict` (dict): Configuration dictionary.

            - `dtype` (torch.dtype): Data type.

            - `device` (torch.device): Device.

            - `operations` (Operations): Operations.

        """
        super().__init__()
        self.num_layers = config_dict["num_layers"]
        model_dim = config_dict["d_model"]

        self.encoder = T5Stack(
            self.num_layers,
            model_dim,
            model_dim,
            config_dict["d_ff"],
            config_dict["dense_act_fn"],
            config_dict["is_gated_act"],
            config_dict["num_heads"],
            config_dict["model_type"] != "umt5",
            dtype,
            device,
            operations,
        )
        self.dtype = dtype
        self.shared = operations.Embedding(
            config_dict["vocab_size"], model_dim, device=device, dtype=dtype
        )

    def get_input_embeddings(self) -> torch.nn.Embedding:
        """#### Get input embeddings



        #### Returns:

            - `torch.nn.Embedding`: The input embeddings.

        """
        return self.shared

    def set_input_embeddings(self, embeddings: torch.nn.Embedding) -> None:
        """#### Set input embeddings



        #### Args:

            - `embeddings` (torch.nn.Embedding): The input embeddings.

        """
        self.shared = embeddings

    def forward(self, input_ids: torch.Tensor, *args, **kwargs) -> torch.Tensor:
        """#### Forward pass



        #### Args:

            - `input_ids` (torch.Tensor): Input tensor.

            - `*args`: Additional arguments.

            - `**kwargs`: Additional keyword arguments.



        #### Returns:

            - `torch.Tensor`: Output tensor.

        """
        x = self.shared(input_ids, out_dtype=kwargs.get("dtype", torch.float32))
        if self.dtype not in [torch.float32, torch.float16, torch.bfloat16]:
            x = torch.nan_to_num(x)  # Fix for fp8 T5 base
        return self.encoder(x, *args, **kwargs)

class T5XXLModel(SDClip.SDClipModel):
    def __init__(

        self, device="cpu", layer="last", layer_idx=None, dtype=None, model_options={}

    ):
        """#### Initialize T5XXL Model



        #### Args:

            - `device` (str, optional): Device. Defaults to "cpu".

            - `layer` (str, optional): Layer. Defaults to "last".

            - `layer_idx` (int, optional): Layer index. Defaults to None.

            - `dtype` (torch.dtype, optional): Data type. Defaults to None.

            - `model_options` (dict, optional): Model options. Defaults to {}.

        """
        textmodel_json_config = os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            "./clip/t5_config_xxl.json",
        )
        super().__init__(
            device=device,
            layer=layer,
            layer_idx=layer_idx,
            textmodel_json_config=textmodel_json_config,
            dtype=dtype,
            special_tokens={"end": 1, "pad": 0},
            model_class=T5,
            model_options=model_options,
        )

class T5XXLTokenizer(SDToken.SDTokenizer):
    def __init__(self, embedding_directory=None, tokenizer_data={}):
        """#### Initialize T5XXL Tokenizer



        #### Args:

            - `embedding_directory` (str, optional): Embedding directory. Defaults to None.

            - `tokenizer_data` (dict, optional): Tokenizer data. Defaults to {}.

        """
        tokenizer_path = os.path.join(
            os.path.dirname(os.path.realpath(__file__)), "./clip/t5_tokenizer"
        )
        super().__init__(
            tokenizer_path,
            pad_with_end=False,
            embedding_size=4096,
            embedding_key="t5xxl",
            tokenizer_class=T5TokenizerFast,
            has_start_token=False,
            pad_to_max_length=False,
            max_length=99999999,
            min_length=256,
        )

class T5LayerNorm(torch.nn.Module):
    def __init__(self, hidden_size, eps=1e-6, dtype=None, device=None, operations=None):
        """#### Initialize T5 Layer Normalization



        #### Args:

            - `hidden_size` (int): Hidden size.

            - `eps` (float, optional): Epsilon. Defaults to 1e-6.

            - `dtype` (torch.dtype, optional): Data type. Defaults to None.

            - `device` (torch.device, optional): Device. Defaults to None.

            - `operations` (Operations, optional): Operations. Defaults to None.

        """
        super().__init__()
        self.weight = torch.nn.Parameter(
            torch.empty(hidden_size, dtype=dtype, device=device)
        )
        self.variance_epsilon = eps

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """#### Forward pass



        #### Args:

            - `x` (torch.Tensor): Input tensor.



        #### Returns:

            - `torch.Tensor`: Output tensor.

        """
        variance = x.pow(2).mean(-1, keepdim=True)
        x = x * torch.rsqrt(variance + self.variance_epsilon)
        return cast.cast_to_input(self.weight, x) * x

class FluxTokenizer:
    def __init__(self, embedding_directory=None, tokenizer_data={}):
        """#### Initialize Flux Tokenizer



        #### Args:

            - `embedding_directory` (str, optional): Embedding directory. Defaults to None.

            - `tokenizer_data` (dict, optional): Tokenizer data. Defaults to {}.

        """
        clip_l_tokenizer_class = tokenizer_data.get(
            "clip_l_tokenizer_class", SDToken.SDTokenizer
        )
        self.clip_l = clip_l_tokenizer_class(embedding_directory=embedding_directory)
        self.t5xxl = T5XXLTokenizer(embedding_directory=embedding_directory)

    def tokenize_with_weights(self, text: str, return_word_ids=False) -> dict:
        """#### Tokenize text with weights



        #### Args:

            - `text` (str): Text to tokenize.

            - `return_word_ids` (bool, optional): Whether to return word IDs. Defaults to False.



        #### Returns:

            - `dict`: Tokenized text with weights.

        """
        out = {}
        out["l"] = self.clip_l.tokenize_with_weights(text, return_word_ids)
        out["t5xxl"] = self.t5xxl.tokenize_with_weights(text, return_word_ids)
        return out

class FluxClipModel(torch.nn.Module):
    def __init__(self, dtype_t5=None, device="cpu", dtype=None, model_options={}):
        """#### Initialize FluxClip Model



        #### Args:

            - `dtype_t5` (torch.dtype, optional): T5 data type. Defaults to None.

            - `device` (str, optional): Device. Defaults to "cpu".

            - `dtype` (torch.dtype, optional): Data type. Defaults to None.

            - `model_options` (dict, optional): Model options. Defaults to {}.

        """
        super().__init__()
        dtype_t5 = Device.pick_weight_dtype(dtype_t5, dtype, device)
        clip_l_class = model_options.get("clip_l_class", SDClip.SDClipModel)
        self.clip_l = clip_l_class(
            device=device,
            dtype=dtype,
            return_projected_pooled=False,
            model_options=model_options,
        )
        self.t5xxl = T5XXLModel(
            device=device, dtype=dtype_t5, model_options=model_options
        )
        self.dtypes = set([dtype, dtype_t5])

    def reset_clip_options(self) -> None:
        """#### Reset CLIP options"""
        self.clip_l.reset_clip_options()
        self.t5xxl.reset_clip_options()

    def encode_token_weights(self, token_weight_pairs: dict) -> tuple:
        """#### Encode token weights



        #### Args:

            - `token_weight_pairs` (dict): Token weight pairs.



        #### Returns:

            - `tuple`: Encoded token weights.

        """
        token_weight_pairs_l = token_weight_pairs["l"]
        token_weight_pairs_t5 = token_weight_pairs["t5xxl"]

        t5_out, t5_pooled = self.t5xxl.encode_token_weights(token_weight_pairs_t5)
        l_out, l_pooled = self.clip_l.encode_token_weights(token_weight_pairs_l)
        return t5_out, l_pooled

    def load_sd(self, sd: dict) -> None:
        """#### Load state dictionary



        #### Args:

            - `sd` (dict): State dictionary.

        """
        if "text_model.encoder.layers.1.mlp.fc1.weight" in sd:
            return self.clip_l.load_sd(sd)
        else:
            return self.t5xxl.load_sd(sd)

def flux_clip(dtype_t5=None):
    """#### Create FluxClip Model



    #### Args:

        - `dtype_t5` (torch.dtype, optional): T5 data type. Defaults to None.



    #### Returns:

        - `FluxClipModel`: FluxClip Model class.

    """
    class FluxClipModel_(FluxClipModel):
        def __init__(self, device="cpu", dtype=None, model_options={}):
            """#### Initialize FluxClip Model



            #### Args:

                - `device` (str, optional): Device. Defaults to "cpu".

                - `dtype` (torch.dtype, optional): Data type. Defaults to None.

                - `model_options` (dict, optional): Model options. Defaults to {}.

            """
            super().__init__(
                dtype_t5=dtype_t5,
                device=device,
                dtype=dtype,
                model_options=model_options,
            )

    return FluxClipModel_