"docs/vscode:/vscode.git/clone" did not exist on "1f22c9882020cbe2cc08acfee54fab553bbb5678"
bloom.py 3.44 KB
Newer Older
1
2
3
import torch
import torch.distributed

4
from typing import Optional, Type
5

6
7
8
9
10
from transformers import (
    AutoTokenizer,
    AutoConfig,
    PreTrainedTokenizerBase,
)
11

12
13
14
from text_generation_server.models.custom_modeling.bloom_modeling import (
    BloomForCausalLM,
)
15
16
17
18
from text_generation_server.models import CausalLM
from text_generation_server.models.causal_lm import CausalLMBatch
from text_generation_server.pb import generate_pb2
from text_generation_server.utils import (
19
20
    initialize_torch_distributed,
    weight_files,
21
    Weights,
22
23
24
)


25
26
27
class BloomCausalLMBatch(CausalLMBatch):
    @classmethod
    def from_pb(
28
29
30
        cls,
        pb: generate_pb2.Batch,
        tokenizer: PreTrainedTokenizerBase,
31
        dtype: torch.dtype,
32
        device: torch.device,
33
    ) -> "CausalLMBatch":
34
        batch = super().from_pb(pb=pb, tokenizer=tokenizer, dtype=dtype, device=device)
35
36
37
38
        batch.keys_head_dim_last = False
        return batch


39
class BLOOMSharded(CausalLM):
40
    def __init__(
41
42
43
44
        self,
        model_id: str,
        revision: Optional[str] = None,
        quantize: Optional[str] = None,
45
        use_medusa: Optional[str] = None,
46
        dtype: Optional[torch.dtype] = None,
47
        trust_remote_code: bool = False,
48
    ):
49
        self.process_group, rank, world_size = initialize_torch_distributed()
50
        if torch.cuda.is_available():
51
            device = torch.device(f"cuda:{rank}")
52
            dtype = torch.float16 if dtype is None else dtype
53
        else:
54
            device = torch.device("cpu")
Wang, Yi's avatar
Wang, Yi committed
55
            dtype = torch.float32 if dtype is None else dtype
56

57
        tokenizer = AutoTokenizer.from_pretrained(
58
59
60
61
62
            model_id,
            revision=revision,
            padding_side="left",
            truncation_side="left",
            trust_remote_code=trust_remote_code,
63
        )
64
65

        config = AutoConfig.from_pretrained(
66
67
68
69
70
            model_id,
            revision=revision,
            slow_but_exact=False,
            tp_parallel=True,
            trust_remote_code=trust_remote_code,
71
72
        )
        config.pad_token_id = 3
73
        config.quantize = quantize
74
        config.use_medusa = use_medusa
75
76

        torch.distributed.barrier(group=self.process_group)
77
        filenames = weight_files(model_id, revision=revision, extension=".safetensors")
78
        weights = Weights(
OlivierDehaene's avatar
OlivierDehaene committed
79
80
81
82
83
            filenames,
            device=device,
            dtype=dtype,
            process_group=self.process_group,
            prefix="transformer",
84
        )
85
        if config.quantize == "gptq":
OlivierDehaene's avatar
OlivierDehaene committed
86
            weights._set_gptq_params(model_id, revision)
87

88
        model = BloomForCausalLM(config, weights)
89
90

        torch.distributed.barrier(group=self.process_group)
91
        super(CausalLM, self).__init__(
92
            model=model,
93
94
95
96
            tokenizer=tokenizer,
            requires_padding=True,
            dtype=dtype,
            device=device,
97
98
            rank=rank,
            world_size=world_size,
99
        )
100

101
102
103
    @property
    def batch_type(self) -> Type[CausalLMBatch]:
        return BloomCausalLMBatch
104

105
106
107
    def forward(
        self, input_ids, attention_mask, position_ids, past_key_values: Optional = None
    ):
108
        outputs, speculative_logits = self.model.forward(
109
110
            input_ids=input_ids,
            attention_mask=attention_mask,
111
            position_ids=position_ids,
112
113
114
115
            past_key_values=past_key_values,
            use_cache=True,
        )

116
        logits = outputs.logits
117
        return logits, speculative_logits, outputs.past_key_values