t5.py 3.33 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
        use_medusa: Optional[str] = None,
29
        dtype: Optional[torch.dtype] = None,
30
        trust_remote_code: bool = False,
31
    ):
32
        self.process_group, rank, world_size = initialize_torch_distributed()
33
        if torch.cuda.is_available():
34
            device = torch.device(f"cuda:{rank}")
35
            dtype = torch.float16 if dtype is None else dtype
36
37
        else:
            device = torch.device("cpu")
Wang, Yi's avatar
Wang, Yi committed
38
            dtype = torch.float32 if dtype is None else dtype
39

40
        config = AutoConfig.from_pretrained(
41
42
43
            model_id,
            revision=revision,
            trust_remote_code=trust_remote_code,
44
        )
45
        config.quantize = quantize
46
        config.use_medusa = use_medusa
47

48
        tokenizer = AutoTokenizer.from_pretrained(
49
50
            model_id,
            revision=revision,
51
52
            padding_side="left",
            truncation_side="left",
53
            trust_remote_code=trust_remote_code,
54
55
56
57
58
        )
        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")
59
        weights = Weights(
60
61
62
63
64
65
66
67
68
69
            filenames,
            device=device,
            dtype=dtype,
            process_group=self.process_group,
            aliases={
                "shared.weight": [
                    "encoder.embed_tokens.weight",
                    "decoder.embed_tokens.weight",
                ]
            },
70
        )
71

72
        model = T5ForConditionalGeneration(config, weights)
73
74
75

        torch.distributed.barrier(group=self.process_group)
        super(Seq2SeqLM, self).__init__(
76
            model=model,
77
            tokenizer=tokenizer,
78
79
            requires_padding=True,
            dtype=dtype,
80
            device=device,
81
82
            rank=rank,
            world_size=world_size,
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        )

    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
99
        outputs, speculative_logits = self.model.forward(
100
101
102
103
104
105
106
107
108
109
            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 (
110
            outputs.logits,
111
            speculative_logits,
112
113
114
            outputs.encoder_last_hidden_state,
            outputs.past_key_values,
        )