bloom.py 3.06 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
        trust_remote_code: bool = False,
46
    ):
47
        self.process_group, rank, world_size = initialize_torch_distributed()
48
        if torch.cuda.is_available():
49
            device = torch.device(f"cuda:{rank}")
50
            dtype = torch.float16
51
        else:
52
            device = torch.device("cpu")
53
54
            dtype = torch.float32

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

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

        torch.distributed.barrier(group=self.process_group)
74
        filenames = weight_files(model_id, revision=revision, extension=".safetensors")
75
76
77
        weights = Weights(
            filenames, device=device, dtype=dtype, process_group=self.process_group
        )
78

79
        model = BloomForCausalLM(config, weights)
80
81

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

92
93
94
    @property
    def batch_type(self) -> Type[CausalLMBatch]:
        return BloomCausalLMBatch
95

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

107
        logits = outputs.logits
108
        return logits, outputs.past_key_values