flash_neox.py 2.79 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
)
Nicolas Patry's avatar
Nicolas Patry committed
17
from text_generation_server.utils.import_utils import SYSTEM
Nicolas Patry's avatar
Nicolas Patry committed
18

19
20
21
tracer = trace.get_tracer(__name__)


22
class FlashNeoXSharded(FlashCausalLM):
23
    def __init__(
24
25
26
27
        self,
        model_id: str,
        revision: Optional[str] = None,
        quantize: Optional[str] = None,
Nicolas Patry's avatar
Nicolas Patry committed
28
        speculator: Optional[str] = None,
29
        dtype: Optional[torch.dtype] = None,
30
        trust_remote_code: bool = False,
31
    ):
32
        self.process_group, rank, world_size = initialize_torch_distributed()
33
        if torch.cuda.is_available():
34
            device = torch.device(f"cuda:{rank}")
35
            dtype = torch.float16 if dtype is None else dtype
Nicolas Patry's avatar
Nicolas Patry committed
36
37
38
        elif SYSTEM == "ipex":
            if hasattr(torch, "xpu") and torch.xpu.is_available():
                device = torch.device(f"xpu:{rank}")
Wang, Yi's avatar
Wang, Yi committed
39
                dtype = torch.float16 if dtype is None else dtype
Nicolas Patry's avatar
Nicolas Patry committed
40
41
            else:
                device = torch.device("cpu")
Wang, Yi's avatar
Wang, Yi committed
42
                dtype = torch.bfloat16 if dtype is None else dtype
43
44
45
46
        else:
            raise NotImplementedError("FlashNeoX is only available on GPU")

        tokenizer = AutoTokenizer.from_pretrained(
47
48
49
50
51
            model_id,
            revision=revision,
            padding_side="left",
            truncation_side="left",
            trust_remote_code=trust_remote_code,
52
53
54
        )

        config = AutoConfig.from_pretrained(
55
            model_id, revision=revision, trust_remote_code=trust_remote_code
56
        )
57
        config.quantize = quantize
Nicolas Patry's avatar
Nicolas Patry committed
58
        config.speculator = speculator
59
60
61

        torch.distributed.barrier(group=self.process_group)
        filenames = weight_files(model_id, revision=revision, extension=".safetensors")
62
63
64
        weights = Weights(
            filenames, device=device, dtype=dtype, process_group=self.process_group
        )
65
        if config.quantize in ["gptq", "marlin"]:
OlivierDehaene's avatar
OlivierDehaene committed
66
            weights._set_gptq_params(model_id, revision)
67

68
        model = FlashGPTNeoXForCausalLM(config, weights)
69
70

        torch.distributed.barrier(group=self.process_group)
71
        super(FlashNeoXSharded, self).__init__(
drbh's avatar
drbh committed
72
            model_id=model_id,
73
            model=model.to(device),
74
            tokenizer=tokenizer,
75
76
77
            num_layers=len(model.gpt_neox.layers),
            num_kv_heads=model.gpt_neox.num_heads,
            head_size=model.gpt_neox.head_size,
78
            dtype=dtype,
79
            device=device,
80
81
            rank=rank,
            world_size=world_size,
82
        )