flash_neox.py 2.35 KB
Newer Older
1
2
3
4
import torch
import torch.distributed

from opentelemetry import trace
5
from transformers import AutoTokenizer, AutoConfig
6
from typing import Optional
7

8
9
from text_generation_server.models import FlashCausalLM
from text_generation_server.models.custom_modeling.flash_neox_modeling import (
10
11
12
13
14
    FlashGPTNeoXForCausalLM,
)
from text_generation_server.utils import (
    initialize_torch_distributed,
    weight_files,
15
    Weights,
16
17
18
19
20
)

tracer = trace.get_tracer(__name__)


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

        tokenizer = AutoTokenizer.from_pretrained(
39
40
41
42
43
            model_id,
            revision=revision,
            padding_side="left",
            truncation_side="left",
            trust_remote_code=trust_remote_code,
44
45
46
        )

        config = AutoConfig.from_pretrained(
47
            model_id, revision=revision, trust_remote_code=trust_remote_code
48
        )
49
        config.quantize = quantize
50
        config.use_medusa = use_medusa
51
52
53

        torch.distributed.barrier(group=self.process_group)
        filenames = weight_files(model_id, revision=revision, extension=".safetensors")
54
55
56
        weights = Weights(
            filenames, device=device, dtype=dtype, process_group=self.process_group
        )
57
        if config.quantize == "gptq":
OlivierDehaene's avatar
OlivierDehaene committed
58
            weights._set_gptq_params(model_id, revision)
59

60
        model = FlashGPTNeoXForCausalLM(config, weights)
61
62

        torch.distributed.barrier(group=self.process_group)
63
        super(FlashNeoXSharded, self).__init__(
64
            model=model.to(device),
65
            tokenizer=tokenizer,
66
67
68
            num_layers=len(model.gpt_neox.layers),
            num_kv_heads=model.gpt_neox.num_heads,
            head_size=model.gpt_neox.head_size,
69
            dtype=dtype,
70
            device=device,
71
72
            rank=rank,
            world_size=world_size,
73
        )