flash_llama.py 3.09 KB
Newer Older
1
2
3
4
import torch
import torch.distributed

from opentelemetry import trace
5
from transformers import AutoConfig, AutoTokenizer, GenerationConfig
6
from typing import Optional
7
8
9
10
11
12
13
14

from text_generation_server.models import FlashCausalLM
from text_generation_server.models.custom_modeling.flash_llama_modeling import (
    FlashLlamaForCausalLM,
)
from text_generation_server.utils import (
    initialize_torch_distributed,
    weight_files,
15
    Weights,
16
17
18
19
)

tracer = trace.get_tracer(__name__)

Wang, Yi's avatar
Wang, Yi committed
20
from text_generation_server.utils.import_utils import SYSTEM, IPEX_AVAIL
21

Nicolas Patry's avatar
Nicolas Patry committed
22

23
24
class FlashLlama(FlashCausalLM):
    def __init__(
25
26
27
28
        self,
        model_id: str,
        revision: Optional[str] = None,
        quantize: Optional[str] = None,
Nicolas Patry's avatar
Nicolas Patry committed
29
        speculator: 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
Nicolas Patry's avatar
Nicolas Patry committed
37
        elif SYSTEM == "xpu":
38
39
            device = torch.device(f"xpu:{rank}")
            dtype = torch.float16 if dtype is None else dtype
Wang, Yi's avatar
Wang, Yi committed
40
41
42
        elif IPEX_AVAIL:
            device = torch.device("cpu")
            dtype = torch.bfloat16 if dtype is None else dtype
43
44
45
        else:
            raise NotImplementedError("FlashLlama is only available on GPU")

46
47
48
49
50
51
52
        tokenizer = AutoTokenizer.from_pretrained(
            model_id,
            revision=revision,
            padding_side="left",
            truncation_side="left",
            trust_remote_code=trust_remote_code,
        )
53
54
55
56
57
58
59
60
61
        try:
            generation_config = GenerationConfig.from_pretrained(
                model_id, revision=revision, trust_remote_code=trust_remote_code
            )
            if isinstance(generation_config.eos_token_id, (list, set)):
                # TODO Huge hack
                tokenizer._eos_token_ids = set(generation_config.eos_token_id)
        except Exception:
            pass
62

63
        config = AutoConfig.from_pretrained(
64
            model_id, revision=revision, trust_remote_code=trust_remote_code
65
        )
66
        config.quantize = quantize
Nicolas Patry's avatar
Nicolas Patry committed
67
        config.speculator = speculator
68
69

        torch.distributed.barrier(group=self.process_group)
70

71
        filenames = weight_files(model_id, revision=revision, extension=".safetensors")
72
        weights = Weights(filenames, device, dtype, process_group=self.process_group)
73
        if config.quantize in ["awq", "exl2", "gptq", "marlin"]:
OlivierDehaene's avatar
OlivierDehaene committed
74
            weights._set_gptq_params(model_id, revision)
75

76
77
        prefix = ""
        model = FlashLlamaForCausalLM(prefix, config, weights)
78
        torch.distributed.barrier(group=self.process_group)
79
        super(FlashLlama, self).__init__(
80
            model=model,
81
            tokenizer=tokenizer,
82
            num_layers=len(model.model.layers),
83
            num_kv_heads=model.model.num_key_value_heads,
84
            head_size=model.model.head_size,
85
            dtype=dtype,
86
            device=device,
87
88
            rank=rank,
            world_size=world_size,
89
        )