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

from opentelemetry import trace
from transformers import AutoConfig
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
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
37
38
39
40
41
        else:
            raise NotImplementedError("FlashLlama is only available on GPU")

        tokenizer = LlamaTokenizer.from_pretrained(
            model_id,
            revision=revision,
            padding_side="left",
            truncation_side="left",
42
            trust_remote_code=trust_remote_code,
43
44
45
        )

        config = AutoConfig.from_pretrained(
46
            model_id, revision=revision, trust_remote_code=trust_remote_code
47
48
49
        )

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

51
        filenames = weight_files(model_id, revision=revision, extension=".safetensors")
52
        weights = Weights(filenames, device, dtype, process_group=self.process_group)
53

54
55
        config.quantize = quantize
        model = FlashLlamaForCausalLM(config, weights)
56
57
58

        torch.distributed.barrier(group=self.process_group)
        super(FlashCausalLM, self).__init__(
59
            model=model,
60
            tokenizer=tokenizer,
61
62
            requires_padding=False,
            dtype=dtype,
63
            device=device,
64
65
            rank=rank,
            world_size=world_size,
66
        )