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
23
24

class FlashLlama(FlashCausalLM):
    def __init__(
25
26
27
28
        self,
        model_id: str,
        revision: Optional[str] = None,
        quantize: Optional[str] = None,
29
        use_medusa: 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
        elif IS_XPU_SYSTEM:
            device = torch.device(f"xpu:{rank}")
            dtype = torch.float16 if dtype is None else dtype
40
41
42
        else:
            raise NotImplementedError("FlashLlama is only available on GPU")

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

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

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

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

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