flash_llama.py 2.94 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__)

Nicolas Patry's avatar
Nicolas Patry committed
20
from text_generation_server.utils.import_utils import SYSTEM
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
40
41
42
        else:
            raise NotImplementedError("FlashLlama is only available on GPU")

43
44
45
46
47
48
49
        tokenizer = AutoTokenizer.from_pretrained(
            model_id,
            revision=revision,
            padding_side="left",
            truncation_side="left",
            trust_remote_code=trust_remote_code,
        )
50
51
52
53
54
55
56
57
58
        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
59

60
        config = AutoConfig.from_pretrained(
61
            model_id, revision=revision, trust_remote_code=trust_remote_code
62
        )
63
        config.quantize = quantize
Nicolas Patry's avatar
Nicolas Patry committed
64
        config.speculator = speculator
65
66

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

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

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