flash_santacoder.py 2.67 KB
Newer Older
1
2
3
4
import torch
import torch.distributed

from opentelemetry import trace
5
from transformers import AutoTokenizer, AutoConfig
6
7
8
9
from typing import Optional, List

from text_generation_server.models import FlashCausalLM
from text_generation_server.models.custom_modeling.flash_santacoder_modeling import (
10
    FlashSantacoderForCausalLM,
11
12
)
from text_generation_server.utils import (
13
    initialize_torch_distributed,
14
    weight_files,
15
    Weights,
16
17
18
19
20
)

tracer = trace.get_tracer(__name__)


21
class FlashSantacoderSharded(FlashCausalLM):
22
    def __init__(
23
24
25
26
        self,
        model_id: str,
        revision: Optional[str] = None,
        quantize: Optional[str] = None,
27
        dtype: Optional[torch.dtype] = 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 if dtype is None else dtype
34
35
36
37
        else:
            raise NotImplementedError("FlashSantacoderSharded is only available on GPU")

        tokenizer = AutoTokenizer.from_pretrained(
38
39
40
41
42
            model_id,
            revision=revision,
            padding_side="left",
            truncation_side="left",
            trust_remote_code=trust_remote_code,
43
44
        )

45
        config = AutoConfig.from_pretrained(
46
47
            model_id,
            revision=revision,
48
            trust_remote_code=True,
49
        )
50
51
        config.quantize = quantize
        config.transpose = config.architectures[0].startswith("GPT2")
52
53
54

        torch.distributed.barrier(group=self.process_group)
        filenames = weight_files(model_id, revision=revision, extension=".safetensors")
55
        weights = Weights(
56
57
58
59
60
            filenames,
            device=device,
            dtype=dtype,
            process_group=self.process_group,
            aliases={"transformer.wte.weight": ["lm_head.weight"]},
61
        )
62

63
        model = FlashSantacoderForCausalLM(config, weights)
64
65

        torch.distributed.barrier(group=self.process_group)
66
        super(FlashSantacoderSharded, self).__init__(
67
            model=model.to(device),
68
            tokenizer=tokenizer,
69
70
71
            num_layers=len(model.transformer.h),
            num_kv_heads=1,
            head_size=model.transformer.head_size,
72
            dtype=dtype,
73
            device=device,
74
75
            rank=rank,
            world_size=world_size,
76
77
        )

78
79
80
81
82
    def decode(self, generated_ids: List[int]) -> str:
        # Do not skip special tokens as they are used for custom parsing rules of the generated text
        return self.tokenizer.decode(
            generated_ids, skip_special_tokens=False, clean_up_tokenization_spaces=False
        )