flash_llama.py 3.31 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 transformers.models.llama import LlamaTokenizer
7
from typing import Optional
8
9
10
11
12
13
14
15

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,
16
    Weights,
17
18
19
20
)

tracer = trace.get_tracer(__name__)

21
from text_generation_server.utils.import_utils import IS_XPU_SYSTEM
22

Nicolas Patry's avatar
Nicolas Patry committed
23

24
25
class FlashLlama(FlashCausalLM):
    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
        elif IS_XPU_SYSTEM:
            device = torch.device(f"xpu:{rank}")
            dtype = torch.float16 if dtype is None else dtype
41
42
43
        else:
            raise NotImplementedError("FlashLlama is only available on GPU")

44
45
46
47
48
49
50
51
52
        try:
            tokenizer = LlamaTokenizer.from_pretrained(
                model_id,
                revision=revision,
                padding_side="left",
                truncation_side="left",
                trust_remote_code=trust_remote_code,
            )
        except Exception:
53
            tokenizer = AutoTokenizer.from_pretrained(
54
55
56
57
58
59
                model_id,
                revision=revision,
                padding_side="left",
                truncation_side="left",
                trust_remote_code=trust_remote_code,
            )
60
61
62
63
64
65
66
67
68
        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
69

70
        config = AutoConfig.from_pretrained(
71
            model_id, revision=revision, trust_remote_code=trust_remote_code
72
        )
73
        config.quantize = quantize
74
        config.use_medusa = use_medusa
75
76

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

78
        filenames = weight_files(model_id, revision=revision, extension=".safetensors")
79
        weights = Weights(filenames, device, dtype, process_group=self.process_group)
80
        if config.quantize in ["gptq", "awq"]:
OlivierDehaene's avatar
OlivierDehaene committed
81
            weights._set_gptq_params(model_id, revision)
82

83
84
        prefix = ""
        model = FlashLlamaForCausalLM(prefix, config, weights)
85
        torch.distributed.barrier(group=self.process_group)
86
        super(FlashLlama, self).__init__(
87
            model=model,
88
            tokenizer=tokenizer,
89
            num_layers=len(model.model.layers),
90
            num_kv_heads=model.model.num_key_value_heads,
91
            head_size=model.model.head_size,
92
            dtype=dtype,
93
            device=device,
94
95
            rank=rank,
            world_size=world_size,
96
        )