flash_neox.py 2.18 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
        dtype: Optional[torch.dtype] = 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 if dtype is None else dtype
34
35
36
37
        else:
            raise NotImplementedError("FlashNeoX is only available on GPU")

        tokenizer = AutoTokenizer.from_pretrained(
38
39
40
41
42
            model_id,
            revision=revision,
            padding_side="left",
            truncation_side="left",
            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
        config.quantize = quantize
49
50
51

        torch.distributed.barrier(group=self.process_group)
        filenames = weight_files(model_id, revision=revision, extension=".safetensors")
52
53
54
        weights = Weights(
            filenames, device=device, dtype=dtype, process_group=self.process_group
        )
55

56
        model = FlashGPTNeoXForCausalLM(config, weights)
57
58

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