t5.py 2.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
import torch
import torch.distributed

from typing import List, Optional, Tuple

from transformers import (
    AutoTokenizer,
    AutoConfig,
)

11
from text_generation_server.models import Seq2SeqLM
12
13
14
from text_generation_server.models.custom_modeling.t5_modeling import (
    T5ForConditionalGeneration,
)
15
from text_generation_server.utils import (
16
17
    initialize_torch_distributed,
    weight_files,
18
    Weights,
19
20
21
22
23
)


class T5Sharded(Seq2SeqLM):
    def __init__(
24
25
26
27
        self,
        model_id: str,
        revision: Optional[str] = None,
        quantize: Optional[str] = None,
28
        trust_remote_code: bool = False,
29
    ):
30
        self.process_group, rank, world_size = initialize_torch_distributed()
31
        if torch.cuda.is_available():
32
            device = torch.device(f"cuda:{rank}")
33
            dtype = torch.float16
34
35
36
37
        else:
            device = torch.device("cpu")
            dtype = torch.float32

38
        config = AutoConfig.from_pretrained(
39
40
41
            model_id,
            revision=revision,
            trust_remote_code=trust_remote_code,
42
        )
43
        config.quantize = quantize
44

45
        tokenizer = AutoTokenizer.from_pretrained(
46
47
            model_id,
            revision=revision,
48
49
            padding_side="left",
            truncation_side="left",
50
            trust_remote_code=trust_remote_code,
51
52
53
54
55
        )
        tokenizer.bos_token_id = config.decoder_start_token_id

        torch.distributed.barrier(group=self.process_group)
        filenames = weight_files(model_id, revision=revision, extension=".safetensors")
56
57
58
        weights = Weights(
            filenames, device=device, dtype=dtype, process_group=self.process_group
        )
59

60
        model = T5ForConditionalGeneration(config, weights)
61
62
63

        torch.distributed.barrier(group=self.process_group)
        super(Seq2SeqLM, self).__init__(
64
            model=model,
65
            tokenizer=tokenizer,
66
67
            requires_padding=True,
            dtype=dtype,
68
            device=device,
69
70
            rank=rank,
            world_size=world_size,
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
        )

    def forward(
        self,
        input_ids,
        attention_mask,
        decoder_input_ids,
        decoder_attention_mask: Optional,
        encoder_last_hidden_state: Optional,
        past_key_values: Optional = None,
    ) -> Tuple[
        torch.Tensor,
        torch.Tensor,
        List[Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]],
    ]:
        # Model Forward
        outputs = self.model.forward(
            input_ids=input_ids,
            attention_mask=attention_mask,
            decoder_input_ids=decoder_input_ids,
            decoder_attention_mask=decoder_attention_mask,
            encoder_outputs=encoder_last_hidden_state,
            past_key_values=past_key_values,
            use_cache=True,
        )

        return (
98
            outputs.logits,
99
100
101
            outputs.encoder_last_hidden_state,
            outputs.past_key_values,
        )