flash_santacoder.py 3.11 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
from text_generation_server.utils.import_utils import IS_XPU_SYSTEM
22
23
24
tracer = trace.get_tracer(__name__)


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

        tokenizer = AutoTokenizer.from_pretrained(
46
47
48
49
50
            model_id,
            revision=revision,
            padding_side="left",
            truncation_side="left",
            trust_remote_code=trust_remote_code,
51
52
        )

53
        config = AutoConfig.from_pretrained(
54
55
            model_id,
            revision=revision,
56
            trust_remote_code=True,
57
        )
58
        config.quantize = quantize
59
        config.use_medusa = use_medusa
60
        config.transpose = config.architectures[0].startswith("GPT2")
61
62
63

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

74
        model = FlashSantacoderForCausalLM(config, weights)
75
76

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

89
90
91
92
93
    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
        )