"vscode:/vscode.git/clone" did not exist on "bc2ea0597b1afbe40375449882577bb132000480"
flash_santacoder.py 3.25 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
)

Nicolas Patry's avatar
Nicolas Patry committed
21
from text_generation_server.utils.import_utils import SYSTEM
Nicolas Patry's avatar
Nicolas Patry committed
22

23
24
25
tracer = trace.get_tracer(__name__)


26
class FlashSantacoderSharded(FlashCausalLM):
27
    def __init__(
28
29
30
31
        self,
        model_id: str,
        revision: Optional[str] = None,
        quantize: Optional[str] = None,
Nicolas Patry's avatar
Nicolas Patry committed
32
        speculator: Optional[str] = None,
33
        dtype: Optional[torch.dtype] = None,
34
        trust_remote_code: bool = False,
35
    ):
36
        self.process_group, rank, world_size = initialize_torch_distributed()
37
        if torch.cuda.is_available():
38
            device = torch.device(f"cuda:{rank}")
39
            dtype = torch.float16 if dtype is None else dtype
Nicolas Patry's avatar
Nicolas Patry committed
40
41
42
43
44
        elif SYSTEM == "ipex":
            if hasattr(torch, "xpu") and torch.xpu.is_available():
                device = torch.device(f"xpu:{rank}")
            else:
                device = torch.device("cpu")
45
            dtype = torch.float16 if dtype is None else dtype
46
47
48
49
        else:
            raise NotImplementedError("FlashSantacoderSharded is only available on GPU")

        tokenizer = AutoTokenizer.from_pretrained(
50
51
52
53
54
            model_id,
            revision=revision,
            padding_side="left",
            truncation_side="left",
            trust_remote_code=trust_remote_code,
55
56
        )

57
        config = AutoConfig.from_pretrained(
58
59
            model_id,
            revision=revision,
60
            trust_remote_code=True,
61
        )
62
        config.quantize = quantize
Nicolas Patry's avatar
Nicolas Patry committed
63
        config.speculator = speculator
64
        config.transpose = config.architectures[0].startswith("GPT2")
65
66
67

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

78
        model = FlashSantacoderForCausalLM(config, weights)
79
80

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

93
94
95
96
97
    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
        )