bloom.py 3.24 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
        dtype: Optional[torch.dtype] = None,
46
        trust_remote_code: bool = False,
47
    ):
48
        self.process_group, rank, world_size = initialize_torch_distributed()
49
        if torch.cuda.is_available():
50
            device = torch.device(f"cuda:{rank}")
51
            dtype = torch.float16 if dtype is None else dtype
52
        else:
53
            device = torch.device("cpu")
Wang, Yi's avatar
Wang, Yi committed
54
            dtype = torch.float32 if dtype is None else dtype
55

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

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

        torch.distributed.barrier(group=self.process_group)
75
        filenames = weight_files(model_id, revision=revision, extension=".safetensors")
76
77
78
        weights = Weights(
            filenames, device=device, dtype=dtype, process_group=self.process_group
        )
79
80
        if config.quantize == "gptq":
            weights._set_gptq_params(model_id)
81

82
        model = BloomForCausalLM(config, weights)
83
84

        torch.distributed.barrier(group=self.process_group)
85
        super(CausalLM, self).__init__(
86
            model=model,
87
88
89
90
            tokenizer=tokenizer,
            requires_padding=True,
            dtype=dtype,
            device=device,
91
92
            rank=rank,
            world_size=world_size,
93
        )
94

95
96
97
    @property
    def batch_type(self) -> Type[CausalLMBatch]:
        return BloomCausalLMBatch
98

99
100
101
    def forward(
        self, input_ids, attention_mask, position_ids, past_key_values: Optional = None
    ):
102
103
104
        outputs = self.model.forward(
            input_ids=input_ids,
            attention_mask=attention_mask,
105
            position_ids=position_ids,
106
107
108
109
            past_key_values=past_key_values,
            use_cache=True,
        )

110
        logits = outputs.logits
111
        return logits, outputs.past_key_values