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

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

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

tracer = trace.get_tracer(__name__)


24
class FlashSantacoderSharded(FlashCausalLM):
25
    def __init__(
26
27
28
29
        self,
        model_id: str,
        revision: Optional[str] = None,
        quantize: Optional[str] = None,
30
        use_medusa: Optional[str] = None,
31
        dtype: Optional[torch.dtype] = None,
32
        trust_remote_code: bool = False,
33
    ):
34
        self.process_group, rank, world_size = initialize_torch_distributed()
35
        if torch.cuda.is_available():
36
            device = torch.device(f"cuda:{rank}")
37
            dtype = torch.float16 if dtype is None else dtype
38
39
40
41
        else:
            raise NotImplementedError("FlashSantacoderSharded is only available on GPU")

        tokenizer = AutoTokenizer.from_pretrained(
42
43
44
45
46
            model_id,
            revision=revision,
            padding_side="left",
            truncation_side="left",
            trust_remote_code=trust_remote_code,
47
48
        )

49
        config = AutoConfig.from_pretrained(
50
51
            model_id,
            revision=revision,
52
            trust_remote_code=True,
53
        )
54
        config.quantize = quantize
55
        config.use_medusa = use_medusa
56
        config.transpose = config.architectures[0].startswith("GPT2")
57
58
59

        torch.distributed.barrier(group=self.process_group)
        filenames = weight_files(model_id, revision=revision, extension=".safetensors")
60
        weights = Weights(
61
62
63
64
65
            filenames,
            device=device,
            dtype=dtype,
            process_group=self.process_group,
            aliases={"transformer.wte.weight": ["lm_head.weight"]},
66
        )
67
        if config.quantize == "gptq":
OlivierDehaene's avatar
OlivierDehaene committed
68
            weights._set_gptq_params(model_id, revision)
69

70
        model = FlashSantacoderForCausalLM(config, weights)
71
72

        torch.distributed.barrier(group=self.process_group)
73
        super(FlashSantacoderSharded, self).__init__(
74
            model=model.to(device),
75
            tokenizer=tokenizer,
76
77
78
            num_layers=len(model.transformer.h),
            num_kv_heads=1,
            head_size=model.transformer.head_size,
79
            dtype=dtype,
80
            device=device,
81
82
            rank=rank,
            world_size=world_size,
83
84
        )

85
86
87
88
89
    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
        )