model.py 21.4 KB
Newer Older
helloyongyang's avatar
helloyongyang committed
1
2
3
4
# Modified from transformers.models.t5.modeling_t5
# Copyright 2024-2025 The Alibaba Wan Team Authors. All rights reserved.
import math
import os
PengGao's avatar
PengGao committed
5

helloyongyang's avatar
helloyongyang committed
6
7
8
import torch
import torch.nn as nn
import torch.nn.functional as F
root's avatar
root committed
9
from loguru import logger
10

11
from lightx2v.models.input_encoders.hf.q_linear import Q8FQuantLinearFp8, Q8FQuantLinearInt8, SglQuantLinearFp8, TorchaoQuantLinearInt8, VllmQuantLinearInt8
12
from lightx2v.utils.envs import *
gushiqiao's avatar
gushiqiao committed
13
from lightx2v.utils.utils import load_weights
PengGao's avatar
PengGao committed
14
15

from .tokenizer import HuggingfaceTokenizer
helloyongyang's avatar
helloyongyang committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

__all__ = [
    "T5Model",
    "T5Encoder",
    "T5Decoder",
    "T5EncoderModel",
]


def fp16_clamp(x):
    if x.dtype == torch.float16 and torch.isinf(x).any():
        clamp = torch.finfo(x.dtype).max - 1000
        x = torch.clamp(x, min=-clamp, max=clamp)
    return x


gushiqiao's avatar
gushiqiao committed
32
33
34
35
36
37
38
39
def optimize_memory_usage():
    if torch.cuda.is_available():
        torch.cuda.empty_cache()
    import gc

    gc.collect()


helloyongyang's avatar
helloyongyang committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def init_weights(m):
    if isinstance(m, T5LayerNorm):
        nn.init.ones_(m.weight)
    elif isinstance(m, T5Model):
        nn.init.normal_(m.token_embedding.weight, std=1.0)
    elif isinstance(m, T5FeedForward):
        nn.init.normal_(m.gate[0].weight, std=m.dim**-0.5)
        nn.init.normal_(m.fc1.weight, std=m.dim**-0.5)
        nn.init.normal_(m.fc2.weight, std=m.dim_ffn**-0.5)
    elif isinstance(m, T5Attention):
        nn.init.normal_(m.q.weight, std=(m.dim * m.dim_attn) ** -0.5)
        nn.init.normal_(m.k.weight, std=m.dim**-0.5)
        nn.init.normal_(m.v.weight, std=m.dim**-0.5)
        nn.init.normal_(m.o.weight, std=(m.num_heads * m.dim_attn) ** -0.5)
    elif isinstance(m, T5RelativeEmbedding):
Dongz's avatar
Dongz committed
55
        nn.init.normal_(m.embedding.weight, std=(2 * m.num_buckets * m.num_heads) ** -0.5)
helloyongyang's avatar
helloyongyang committed
56
57
58
59


class GELU(nn.Module):
    def forward(self, x):
Dongz's avatar
Dongz committed
60
        return 0.5 * x * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi) * (x + 0.044715 * torch.pow(x, 3.0))))
helloyongyang's avatar
helloyongyang committed
61
62
63


class T5LayerNorm(nn.Module):
gushiqiao's avatar
gushiqiao committed
64
    def __init__(self, dim, eps=1e-6, dtype=torch.float16):
helloyongyang's avatar
helloyongyang committed
65
66
67
        super(T5LayerNorm, self).__init__()
        self.dim = dim
        self.eps = eps
gushiqiao's avatar
gushiqiao committed
68
        self.weight = nn.Parameter(torch.ones(dim, dtype=dtype))
helloyongyang's avatar
helloyongyang committed
69
70
71
72
73
74
75
76
77

    def forward(self, x):
        x = x * torch.rsqrt(x.float().pow(2).mean(dim=-1, keepdim=True) + self.eps)
        if self.weight.dtype in [torch.float16, torch.bfloat16]:
            x = x.type_as(self.weight)
        return self.weight * x


class T5Attention(nn.Module):
gushiqiao's avatar
gushiqiao committed
78
    def __init__(self, dim, dim_attn, num_heads, dropout=0.1, quantized=False, quant_scheme=None, dtype=torch.bfloat16):
helloyongyang's avatar
helloyongyang committed
79
80
81
82
83
84
85
        assert dim_attn % num_heads == 0
        super(T5Attention, self).__init__()
        self.dim = dim
        self.dim_attn = dim_attn
        self.num_heads = num_heads
        self.head_dim = dim_attn // num_heads

86
        if quantized:
gushiqiao's avatar
gushiqiao committed
87
            if quant_scheme in ["int8", "int8-vllm"]:
gushiqiao's avatar
gushiqiao committed
88
                linear_cls = VllmQuantLinearInt8
gushiqiao's avatar
gushiqiao committed
89
            elif quant_scheme in ["fp8", "fp8-sgl"]:
90
                linear_cls = SglQuantLinearFp8
gushiqiao's avatar
gushiqiao committed
91
92
            elif quant_scheme == "int8-torchao":
                linear_cls = TorchaoQuantLinearInt8
gushiqiao's avatar
gushiqiao committed
93
94
95
96
            elif quant_scheme == "int8-q8f":
                linear_cls = Q8FQuantLinearInt8
            elif quant_scheme == "fp8-q8f":
                linear_cls = Q8FQuantLinearFp8
gushiqiao's avatar
gushiqiao committed
97
98
            else:
                NotImplementedError(f"Unsupported T5 quant scheme: {quant_scheme}")
99
100
101
        else:
            linear_cls = nn.Linear

helloyongyang's avatar
helloyongyang committed
102
        # layers
gushiqiao's avatar
gushiqiao committed
103
104
105
106
        self.q = linear_cls(dim, dim_attn, bias=False, dtype=dtype)
        self.k = linear_cls(dim, dim_attn, bias=False, dtype=dtype)
        self.v = linear_cls(dim, dim_attn, bias=False, dtype=dtype)
        self.o = linear_cls(dim_attn, dim, bias=False, dtype=dtype)
helloyongyang's avatar
helloyongyang committed
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
        self.dropout = nn.Dropout(dropout)

    def forward(self, x, context=None, mask=None, pos_bias=None):
        """
        x:          [B, L1, C].
        context:    [B, L2, C] or None.
        mask:       [B, L2] or [B, L1, L2] or None.
        """
        # check inputs
        context = x if context is None else context
        b, n, c = x.size(0), self.num_heads, self.head_dim

        # compute query, key, value
        q = self.q(x).view(b, -1, n, c)
        k = self.k(context).view(b, -1, n, c)
        v = self.v(context).view(b, -1, n, c)

        # attention bias
        attn_bias = x.new_zeros(b, n, q.size(1), k.size(1))
        if pos_bias is not None:
            attn_bias += pos_bias
        if mask is not None:
            assert mask.ndim in [2, 3]
            mask = mask.view(b, 1, 1, -1) if mask.ndim == 2 else mask.unsqueeze(1)
            attn_bias.masked_fill_(mask == 0, torch.finfo(x.dtype).min)

        # compute attention (T5 does not use scaling)
        attn = torch.einsum("binc,bjnc->bnij", q, k) + attn_bias
gushiqiao's avatar
gushiqiao committed
135
136
137

        if hasattr(self, "cpu_offload") and self.cpu_offload:
            del attn_bias
138
        attn = F.softmax(attn.float(), dim=-1).type_as(attn)
helloyongyang's avatar
helloyongyang committed
139
140
        x = torch.einsum("bnij,bjnc->binc", attn, v)

gushiqiao's avatar
gushiqiao committed
141
142
        if hasattr(self, "cpu_offload") and self.cpu_offload:
            del attn
helloyongyang's avatar
helloyongyang committed
143
144
145
146
147
148
149
        x = x.reshape(b, -1, n * c)
        x = self.o(x)
        x = self.dropout(x)
        return x


class T5FeedForward(nn.Module):
gushiqiao's avatar
gushiqiao committed
150
    def __init__(self, dim, dim_ffn, dropout=0.1, quantized=False, quant_scheme=None, dtype=torch.bfloat16):
helloyongyang's avatar
helloyongyang committed
151
152
153
154
        super(T5FeedForward, self).__init__()
        self.dim = dim
        self.dim_ffn = dim_ffn

155
        if quantized:
gushiqiao's avatar
gushiqiao committed
156
            if quant_scheme in ["int8", "int8-vllm"]:
gushiqiao's avatar
gushiqiao committed
157
                linear_cls = VllmQuantLinearInt8
gushiqiao's avatar
gushiqiao committed
158
            elif quant_scheme in ["fp8", "fp8-sgl"]:
159
                linear_cls = SglQuantLinearFp8
gushiqiao's avatar
gushiqiao committed
160
161
            elif quant_scheme == "int8-torchao":
                linear_cls = TorchaoQuantLinearInt8
gushiqiao's avatar
gushiqiao committed
162
163
164
165
            elif quant_scheme == "int8-q8f":
                linear_cls = Q8FQuantLinearInt8
            elif quant_scheme == "fp8-q8f":
                linear_cls = Q8FQuantLinearFp8
gushiqiao's avatar
gushiqiao committed
166
167
            else:
                NotImplementedError(f"Unsupported T5 quant scheme: {quant_scheme}")
168
169
        else:
            linear_cls = nn.Linear
helloyongyang's avatar
helloyongyang committed
170
        # layers
gushiqiao's avatar
gushiqiao committed
171
172
173
        self.gate = nn.Sequential(linear_cls(dim, dim_ffn, bias=False, dtype=dtype), GELU())
        self.fc1 = linear_cls(dim, dim_ffn, bias=False, dtype=dtype)
        self.fc2 = linear_cls(dim_ffn, dim, bias=False, dtype=dtype)
helloyongyang's avatar
helloyongyang committed
174
175
176
        self.dropout = nn.Dropout(dropout)

    def forward(self, x):
gushiqiao's avatar
gushiqiao committed
177
178
179
180
181
182
183
184
        if hasattr(self, "cpu_offload") and self.cpu_offload:
            gate_out = self.gate(x)
            fc1_out = self.fc1(x)
            x = fc1_out * gate_out
            del gate_out, fc1_out
        else:
            x = self.fc1(x) * self.gate(x)

helloyongyang's avatar
helloyongyang committed
185
186
187
188
189
190
191
        x = self.dropout(x)
        x = self.fc2(x)
        x = self.dropout(x)
        return x


class T5SelfAttention(nn.Module):
gushiqiao's avatar
gushiqiao committed
192
    def __init__(self, dim, dim_attn, dim_ffn, num_heads, num_buckets, shared_pos=True, dropout=0.1, quantized=False, quant_scheme=None, dtype=torch.bfloat16):
helloyongyang's avatar
helloyongyang committed
193
194
195
196
197
198
199
200
201
        super(T5SelfAttention, self).__init__()
        self.dim = dim
        self.dim_attn = dim_attn
        self.dim_ffn = dim_ffn
        self.num_heads = num_heads
        self.num_buckets = num_buckets
        self.shared_pos = shared_pos

        # layers
gushiqiao's avatar
gushiqiao committed
202
203
204
205
206
        self.norm1 = T5LayerNorm(dim, dtype=dtype)
        self.attn = T5Attention(dim, dim_attn, num_heads, dropout, quantized, quant_scheme, dtype)
        self.norm2 = T5LayerNorm(dim, dtype=dtype)
        self.ffn = T5FeedForward(dim, dim_ffn, dropout, quantized, quant_scheme, dtype=dtype)
        self.pos_embedding = None if shared_pos else T5RelativeEmbedding(num_buckets, num_heads, bidirectional=True, dtype=dtype)
helloyongyang's avatar
helloyongyang committed
207
208
209

    def forward(self, x, mask=None, pos_bias=None):
        e = pos_bias if self.shared_pos else self.pos_embedding(x.size(1), x.size(1))
gushiqiao's avatar
gushiqiao committed
210
211
212
213
214
215
216
217
218
219
220
221
222

        if hasattr(self, "cpu_offload") and self.cpu_offload:
            attn_out = self.attn(self.norm1(x), mask=mask, pos_bias=e)
            x = fp16_clamp(x + attn_out)
            del attn_out

            ffn_out = self.ffn(self.norm2(x))
            x = fp16_clamp(x + ffn_out)
            del ffn_out
        else:
            x = fp16_clamp(x + self.attn(self.norm1(x), mask=mask, pos_bias=e))
            x = fp16_clamp(x + self.ffn(self.norm2(x)))

helloyongyang's avatar
helloyongyang committed
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
        return x


class T5CrossAttention(nn.Module):
    def __init__(
        self,
        dim,
        dim_attn,
        dim_ffn,
        num_heads,
        num_buckets,
        shared_pos=True,
        dropout=0.1,
    ):
        super(T5CrossAttention, self).__init__()
        self.dim = dim
        self.dim_attn = dim_attn
        self.dim_ffn = dim_ffn
        self.num_heads = num_heads
        self.num_buckets = num_buckets
        self.shared_pos = shared_pos

        # layers
        self.norm1 = T5LayerNorm(dim)
        self.self_attn = T5Attention(dim, dim_attn, num_heads, dropout)
        self.norm2 = T5LayerNorm(dim)
        self.cross_attn = T5Attention(dim, dim_attn, num_heads, dropout)
        self.norm3 = T5LayerNorm(dim)
        self.ffn = T5FeedForward(dim, dim_ffn, dropout)
Dongz's avatar
Dongz committed
252
        self.pos_embedding = None if shared_pos else T5RelativeEmbedding(num_buckets, num_heads, bidirectional=False)
helloyongyang's avatar
helloyongyang committed
253

Dongz's avatar
Dongz committed
254
    def forward(self, x, mask=None, encoder_states=None, encoder_mask=None, pos_bias=None):
helloyongyang's avatar
helloyongyang committed
255
256
        e = pos_bias if self.shared_pos else self.pos_embedding(x.size(1), x.size(1))
        x = fp16_clamp(x + self.self_attn(self.norm1(x), mask=mask, pos_bias=e))
Dongz's avatar
Dongz committed
257
        x = fp16_clamp(x + self.cross_attn(self.norm2(x), context=encoder_states, mask=encoder_mask))
helloyongyang's avatar
helloyongyang committed
258
259
260
261
262
        x = fp16_clamp(x + self.ffn(self.norm3(x)))
        return x


class T5RelativeEmbedding(nn.Module):
gushiqiao's avatar
gushiqiao committed
263
    def __init__(self, num_buckets, num_heads, bidirectional, dtype=torch.bfloat16, max_dist=128):
helloyongyang's avatar
helloyongyang committed
264
265
266
267
268
269
270
        super(T5RelativeEmbedding, self).__init__()
        self.num_buckets = num_buckets
        self.num_heads = num_heads
        self.bidirectional = bidirectional
        self.max_dist = max_dist

        # layers
gushiqiao's avatar
gushiqiao committed
271
        self.embedding = nn.Embedding(num_buckets, num_heads, dtype=dtype)
helloyongyang's avatar
helloyongyang committed
272
273
274
275
276

    def forward(self, lq, lk):
        device = self.embedding.weight.device
        # rel_pos = torch.arange(lk).unsqueeze(0).to(device) - \
        #     torch.arange(lq).unsqueeze(1).to(device)
Dongz's avatar
Dongz committed
277
        rel_pos = torch.arange(lk, device=device).unsqueeze(0) - torch.arange(lq, device=device).unsqueeze(1)
helloyongyang's avatar
helloyongyang committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
        rel_pos = self._relative_position_bucket(rel_pos)
        rel_pos_embeds = self.embedding(rel_pos)
        rel_pos_embeds = rel_pos_embeds.permute(2, 0, 1).unsqueeze(0)  # [1, N, Lq, Lk]
        return rel_pos_embeds.contiguous()

    def _relative_position_bucket(self, rel_pos):
        # preprocess
        if self.bidirectional:
            num_buckets = self.num_buckets // 2
            rel_buckets = (rel_pos > 0).long() * num_buckets
            rel_pos = torch.abs(rel_pos)
        else:
            num_buckets = self.num_buckets
            rel_buckets = 0
            rel_pos = -torch.min(rel_pos, torch.zeros_like(rel_pos))

        # embeddings for small and large positions
        max_exact = num_buckets // 2
Dongz's avatar
Dongz committed
296
297
        rel_pos_large = max_exact + (torch.log(rel_pos.float() / max_exact) / math.log(self.max_dist / max_exact) * (num_buckets - max_exact)).long()
        rel_pos_large = torch.min(rel_pos_large, torch.full_like(rel_pos_large, num_buckets - 1))
helloyongyang's avatar
helloyongyang committed
298
299
300
301
302
        rel_buckets += torch.where(rel_pos < max_exact, rel_pos, rel_pos_large)
        return rel_buckets


class T5Encoder(nn.Module):
gushiqiao's avatar
gushiqiao committed
303
    def __init__(self, dtype, vocab, dim, dim_attn, dim_ffn, num_heads, num_layers, num_buckets, shared_pos=True, dropout=0.1, cpu_offload=False, quantized=False, quant_scheme=None):
helloyongyang's avatar
helloyongyang committed
304
        super(T5Encoder, self).__init__()
305

306
        self.cpu_offload = cpu_offload
helloyongyang's avatar
helloyongyang committed
307
308
309
310
311
312
313
        self.dim = dim
        self.dim_attn = dim_attn
        self.dim_ffn = dim_ffn
        self.num_heads = num_heads
        self.num_layers = num_layers
        self.num_buckets = num_buckets
        self.shared_pos = shared_pos
314
        self.quant_scheme = quant_scheme
helloyongyang's avatar
helloyongyang committed
315
316

        # layers
gushiqiao's avatar
gushiqiao committed
317
318
        self.token_embedding = vocab.to(dtype) if isinstance(vocab, nn.Embedding) else nn.Embedding(vocab, dim, dtype=dtype)
        self.pos_embedding = T5RelativeEmbedding(num_buckets, num_heads, bidirectional=True, dtype=dtype) if shared_pos else None
helloyongyang's avatar
helloyongyang committed
319
        self.dropout = nn.Dropout(dropout)
gushiqiao's avatar
gushiqiao committed
320
        self.blocks = nn.ModuleList([T5SelfAttention(dim, dim_attn, dim_ffn, num_heads, num_buckets, shared_pos, dropout, quantized, quant_scheme, dtype) for _ in range(num_layers)])
gushiqiao's avatar
gushiqiao committed
321
322
323
324
325
326

        if cpu_offload:
            for block in self.blocks:
                block.cpu_offload = cpu_offload
                block.attn.cpu_offload = cpu_offload
                block.ffn.cpu_offload = cpu_offload
gushiqiao's avatar
gushiqiao committed
327
        self.norm = T5LayerNorm(dim, dtype=dtype)
helloyongyang's avatar
helloyongyang committed
328
329

        # initialize weights
330
        # self.apply(init_weights)
helloyongyang's avatar
helloyongyang committed
331
332

    def forward(self, ids, mask=None):
333
334
        if self.cpu_offload:
            self.token_embedding = self.token_embedding.cuda()
helloyongyang's avatar
helloyongyang committed
335
        x = self.token_embedding(ids)
336
337
        if self.cpu_offload:
            self.token_embedding = self.token_embedding.cpu()
gushiqiao's avatar
gushiqiao committed
338
            optimize_memory_usage()
helloyongyang's avatar
helloyongyang committed
339
        x = self.dropout(x)
gushiqiao's avatar
gushiqiao committed
340

341
342
        if self.cpu_offload and self.pos_embedding is not None:
            self.pos_embedding = self.pos_embedding.cuda()
helloyongyang's avatar
helloyongyang committed
343
        e = self.pos_embedding(x.size(1), x.size(1)) if self.shared_pos else None
344
345
        if self.cpu_offload and self.pos_embedding is not None:
            self.pos_embedding = self.pos_embedding.cpu()
gushiqiao's avatar
gushiqiao committed
346
347
348
            optimize_memory_usage()

        for i, block in enumerate(self.blocks):
349
350
            if self.cpu_offload:
                block = block.cuda()
helloyongyang's avatar
helloyongyang committed
351
            x = block(x, mask, pos_bias=e)
352
353
            if self.cpu_offload:
                block = block.cpu()
gushiqiao's avatar
gushiqiao committed
354
355
356
                del block
                optimize_memory_usage()

357
358
        if self.cpu_offload:
            self.norm = self.norm.cuda()
helloyongyang's avatar
helloyongyang committed
359
        x = self.norm(x)
360
361
        if self.cpu_offload:
            self.norm = self.norm.cpu()
gushiqiao's avatar
gushiqiao committed
362
363
            optimize_memory_usage()

helloyongyang's avatar
helloyongyang committed
364
        x = self.dropout(x)
365
        return x.to(GET_DTYPE())
helloyongyang's avatar
helloyongyang committed
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


class T5Decoder(nn.Module):
    def __init__(
        self,
        vocab,
        dim,
        dim_attn,
        dim_ffn,
        num_heads,
        num_layers,
        num_buckets,
        shared_pos=True,
        dropout=0.1,
    ):
        super(T5Decoder, self).__init__()
        self.dim = dim
        self.dim_attn = dim_attn
        self.dim_ffn = dim_ffn
        self.num_heads = num_heads
        self.num_layers = num_layers
        self.num_buckets = num_buckets
        self.shared_pos = shared_pos

        # layers
Dongz's avatar
Dongz committed
391
392
        self.token_embedding = vocab if isinstance(vocab, nn.Embedding) else nn.Embedding(vocab, dim)
        self.pos_embedding = T5RelativeEmbedding(num_buckets, num_heads, bidirectional=False) if shared_pos else None
helloyongyang's avatar
helloyongyang committed
393
        self.dropout = nn.Dropout(dropout)
Dongz's avatar
Dongz committed
394
        self.blocks = nn.ModuleList([T5CrossAttention(dim, dim_attn, dim_ffn, num_heads, num_buckets, shared_pos, dropout) for _ in range(num_layers)])
helloyongyang's avatar
helloyongyang committed
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
        self.norm = T5LayerNorm(dim)

        # initialize weights
        self.apply(init_weights)

    def forward(self, ids, mask=None, encoder_states=None, encoder_mask=None):
        b, s = ids.size()

        # causal mask
        if mask is None:
            mask = torch.tril(torch.ones(1, s, s).to(ids.device))
        elif mask.ndim == 2:
            mask = torch.tril(mask.unsqueeze(1).expand(-1, s, -1))

        # layers
        x = self.token_embedding(ids)
        x = self.dropout(x)
        e = self.pos_embedding(x.size(1), x.size(1)) if self.shared_pos else None
        for block in self.blocks:
            x = block(x, mask, encoder_states, encoder_mask, pos_bias=e)
        x = self.norm(x)
        x = self.dropout(x)
        return x


class T5Model(nn.Module):
    def __init__(
        self,
        vocab_size,
        dim,
        dim_attn,
        dim_ffn,
        num_heads,
        encoder_layers,
        decoder_layers,
        num_buckets,
        shared_pos=True,
        dropout=0.1,
    ):
        super(T5Model, self).__init__()
        self.vocab_size = vocab_size
        self.dim = dim
        self.dim_attn = dim_attn
        self.dim_ffn = dim_ffn
        self.num_heads = num_heads
        self.encoder_layers = encoder_layers
        self.decoder_layers = decoder_layers
        self.num_buckets = num_buckets

        # layers
        self.token_embedding = nn.Embedding(vocab_size, dim)
        self.encoder = T5Encoder(
            self.token_embedding,
            dim,
            dim_attn,
            dim_ffn,
            num_heads,
            encoder_layers,
            num_buckets,
            shared_pos,
            dropout,
        )
        self.decoder = T5Decoder(
            self.token_embedding,
            dim,
            dim_attn,
            dim_ffn,
            num_heads,
            decoder_layers,
            num_buckets,
            shared_pos,
            dropout,
        )
        self.head = nn.Linear(dim, vocab_size, bias=False)

        # initialize weights
        self.apply(init_weights)

    def forward(self, encoder_ids, encoder_mask, decoder_ids, decoder_mask):
        x = self.encoder(encoder_ids, encoder_mask)
        x = self.decoder(decoder_ids, decoder_mask, x, encoder_mask)
        x = self.head(x)
        return x


def _t5(
    name,
    encoder_only=False,
    decoder_only=False,
    return_tokenizer=False,
    tokenizer_kwargs={},
    dtype=torch.float32,
    device="cpu",
    **kwargs,
):
    # sanity check
    assert not (encoder_only and decoder_only)

    # params
    if encoder_only:
        model_cls = T5Encoder
        kwargs["vocab"] = kwargs.pop("vocab_size")
        kwargs["num_layers"] = kwargs.pop("encoder_layers")
        _ = kwargs.pop("decoder_layers")
    elif decoder_only:
        model_cls = T5Decoder
        kwargs["vocab"] = kwargs.pop("vocab_size")
        kwargs["num_layers"] = kwargs.pop("decoder_layers")
        _ = kwargs.pop("encoder_layers")
    else:
        model_cls = T5Model

    # init model
    with torch.device(device):
gushiqiao's avatar
gushiqiao committed
509
        model = model_cls(dtype=dtype, **kwargs)
helloyongyang's avatar
helloyongyang committed
510
511

    # set device
gushiqiao's avatar
gushiqiao committed
512
    model = model.to(device=device)
513
    return model
helloyongyang's avatar
helloyongyang committed
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


def umt5_xxl(**kwargs):
    cfg = dict(
        vocab_size=256384,
        dim=4096,
        dim_attn=4096,
        dim_ffn=10240,
        num_heads=64,
        encoder_layers=24,
        decoder_layers=24,
        num_buckets=32,
        shared_pos=False,
        dropout=0.1,
    )
    cfg.update(**kwargs)
    return _t5("umt5-xxl", **cfg)


class T5EncoderModel:
    def __init__(
        self,
        text_len,
        dtype=torch.bfloat16,
        device=torch.cuda.current_device(),
        checkpoint_path=None,
        tokenizer_path=None,
        shard_fn=None,
542
543
        cpu_offload=False,
        offload_granularity="model",
544
545
546
        t5_quantized=False,
        t5_quantized_ckpt=None,
        quant_scheme=None,
547
        load_from_rank0=False,
helloyongyang's avatar
helloyongyang committed
548
549
550
551
    ):
        self.text_len = text_len
        self.dtype = dtype
        self.device = device
552
553
554
555
        if t5_quantized_ckpt is not None and t5_quantized:
            self.checkpoint_path = t5_quantized_ckpt
        else:
            self.checkpoint_path = checkpoint_path
helloyongyang's avatar
helloyongyang committed
556
        self.tokenizer_path = tokenizer_path
557
558
559
560
561
562
        self.offload_granularity = offload_granularity

        # sync cpu offload
        self.cpu_offload = cpu_offload
        if self.cpu_offload:
            assert self.offload_granularity in ["block", "model"]
helloyongyang's avatar
helloyongyang committed
563

564
565
566
567
568
569
        model = (
            umt5_xxl(
                encoder_only=True,
                return_tokenizer=False,
                dtype=dtype,
                device=device,
570
571
572
                cpu_offload=(cpu_offload if self.offload_granularity == "block" else False),
                quantized=t5_quantized,
                quant_scheme=quant_scheme,
573
574
575
576
            )
            .eval()
            .requires_grad_(False)
        )
577

578
        weights_dict = load_weights(self.checkpoint_path, cpu_offload=cpu_offload, load_from_rank0=load_from_rank0)
gushiqiao's avatar
gushiqiao committed
579
        model.load_state_dict(weights_dict)
gushiqiao's avatar
gushiqiao committed
580

helloyongyang's avatar
helloyongyang committed
581
582
583
584
585
586
        self.model = model
        if shard_fn is not None:
            self.model = shard_fn(self.model, sync_module_states=False)
        else:
            self.model.to(self.device)
        # init tokenizer
Dongz's avatar
Dongz committed
587
        self.tokenizer = HuggingfaceTokenizer(name=tokenizer_path, seq_len=text_len, clean="whitespace")
helloyongyang's avatar
helloyongyang committed
588

TorynCurtis's avatar
TorynCurtis committed
589
590
591
592
593
594
    def to_cpu(self):
        self.model = self.model.to("cpu")

    def to_cuda(self):
        self.model = self.model.to("cuda")

gushiqiao's avatar
gushiqiao committed
595
596
597
598
    def optimize_memory(self):
        """优化内存使用"""
        optimize_memory_usage()

599
600
    def infer(self, texts):
        if self.cpu_offload and self.offload_granularity == "model":
TorynCurtis's avatar
TorynCurtis committed
601
602
            self.to_cuda()

helloyongyang's avatar
helloyongyang committed
603
604
605
606
        ids, mask = self.tokenizer(texts, return_mask=True, add_special_tokens=True)
        ids = ids.cuda()
        mask = mask.cuda()
        seq_lens = mask.gt(0).sum(dim=1).long()
gushiqiao's avatar
gushiqiao committed
607
608
609

        with torch.no_grad():
            context = self.model(ids, mask)
TorynCurtis's avatar
TorynCurtis committed
610

611
        if self.cpu_offload and self.offload_granularity == "model":
TorynCurtis's avatar
TorynCurtis committed
612
            self.to_cpu()
gushiqiao's avatar
gushiqiao committed
613
614
615
616
617
            optimize_memory_usage()

        del ids, mask
        if self.cpu_offload:
            optimize_memory_usage()
TorynCurtis's avatar
TorynCurtis committed
618

helloyongyang's avatar
helloyongyang committed
619
620
621
622
        return [u[:v] for u, v in zip(context, seq_lens)]


if __name__ == "__main__":
623
    checkpoint_dir = ""
helloyongyang's avatar
helloyongyang committed
624
625
626
627
628
629
630
631
632
633
634
635
    t5_checkpoint = "models_t5_umt5-xxl-enc-bf16.pth"
    t5_tokenizer = "google/umt5-xxl"
    model = T5EncoderModel(
        text_len=512,
        dtype=torch.bfloat16,
        device=torch.device("cuda"),
        checkpoint_path=os.path.join(checkpoint_dir, t5_checkpoint),
        tokenizer_path=os.path.join(checkpoint_dir, t5_tokenizer),
        shard_fn=None,
    )
    text = "Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage."
    outputs = model.infer(text)
root's avatar
root committed
636
    logger.info(outputs)