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

from opentelemetry import trace
5
6
from transformers import AutoConfig, AutoTokenizer
from transformers.models.llama import LlamaTokenizer
7
from typing import Optional
8
9
10
11

from text_generation_server.models import FlashCausalLM
from text_generation_server.models.custom_modeling.flash_llama_modeling import (
    FlashLlamaForCausalLM,
12
    LlamaConfig,
13
14
15
16
)
from text_generation_server.utils import (
    initialize_torch_distributed,
    weight_files,
17
    Weights,
18
19
20
21
22
23
24
)

tracer = trace.get_tracer(__name__)


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
        else:
            raise NotImplementedError("FlashLlama is only available on GPU")

40
41
42
43
44
45
46
47
48
        try:
            tokenizer = LlamaTokenizer.from_pretrained(
                model_id,
                revision=revision,
                padding_side="left",
                truncation_side="left",
                trust_remote_code=trust_remote_code,
            )
        except Exception:
49
            tokenizer = AutoTokenizer.from_pretrained(
50
51
52
53
54
55
                model_id,
                revision=revision,
                padding_side="left",
                truncation_side="left",
                trust_remote_code=trust_remote_code,
            )
56

57
        config = LlamaConfig.from_pretrained(
58
            model_id, revision=revision, trust_remote_code=trust_remote_code
59
        )
60
        config.quantize = quantize
61
        config.use_medusa = use_medusa
62
63

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

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

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