flash_neox.py 1.99 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
        trust_remote_code: bool = False,
28
    ):
29
        self.process_group, rank, world_size = initialize_torch_distributed()
30
        if torch.cuda.is_available():
31
            device = torch.device(f"cuda:{rank}")
32
            dtype = torch.float16
33
34
35
36
        else:
            raise NotImplementedError("FlashNeoX is only available on GPU")

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

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

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

55
        model = FlashGPTNeoXForCausalLM(config, weights)
56
57

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