flash_neox.py 2.69 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
39
40
        elif SYSTEM == "ipex":
            if hasattr(torch, "xpu") and torch.xpu.is_available():
                device = torch.device(f"xpu:{rank}")
            else:
                device = torch.device("cpu")
41
            dtype = torch.float16 if dtype is None else dtype
42
43
44
45
        else:
            raise NotImplementedError("FlashNeoX is only available on GPU")

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

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

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

67
        model = FlashGPTNeoXForCausalLM(config, weights)
68
69

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