flash_santacoder.py 2.81 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
        dtype: Optional[torch.dtype] = None,
31
        trust_remote_code: bool = False,
32
    ):
33
        self.process_group, rank, world_size = initialize_torch_distributed()
34
        if torch.cuda.is_available():
35
            device = torch.device(f"cuda:{rank}")
36
            dtype = torch.float16 if dtype is None else dtype
37
38
39
40
        else:
            raise NotImplementedError("FlashSantacoderSharded is only available on GPU")

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

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

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

68
        model = FlashSantacoderForCausalLM(config, weights)
69
70

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

83
84
85
86
87
    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
        )