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

from opentelemetry import trace
from transformers import AutoConfig
6
from transformers.models.llama import LlamaTokenizer, LlamaTokenizerFast
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
21
22
23
)

tracer = trace.get_tracer(__name__)


class FlashLlama(FlashCausalLM):
    def __init__(
24
25
26
27
        self,
        model_id: str,
        revision: Optional[str] = None,
        quantize: Optional[str] = None,
28
        trust_remote_code: bool = False,
29
    ):
30
        self.process_group, rank, world_size = initialize_torch_distributed()
31
        if torch.cuda.is_available():
32
            device = torch.device(f"cuda:{rank}")
33
            dtype = torch.float16
34
35
36
        else:
            raise NotImplementedError("FlashLlama is only available on GPU")

37
38
39
40
41
42
43
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:
            tokenizer = LlamaTokenizerFast.from_pretrained(
                model_id,
                revision=revision,
                padding_side="left",
                truncation_side="left",
                trust_remote_code=trust_remote_code,
            )
53
54

        config = AutoConfig.from_pretrained(
55
            model_id, revision=revision, trust_remote_code=trust_remote_code
56
57
58
        )

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

60
        filenames = weight_files(model_id, revision=revision, extension=".safetensors")
61
        weights = Weights(filenames, device, dtype, process_group=self.process_group)
62

63
64
        config.quantize = quantize
        model = FlashLlamaForCausalLM(config, weights)
65
66
67

        torch.distributed.barrier(group=self.process_group)
        super(FlashCausalLM, self).__init__(
68
            model=model,
69
            tokenizer=tokenizer,
70
71
            requires_padding=False,
            dtype=dtype,
72
            device=device,
73
74
            rank=rank,
            world_size=world_size,
75
        )