opt.py 2.61 KB
Newer Older
1
2
3
import torch
import torch.distributed

4
from typing import Optional
5
6
7
8
9

from transformers import (
    AutoTokenizer,
    AutoConfig,
)
10
from text_generation_server.models.custom_modeling.opt_modeling import OPTForCausalLM
11
12
13
14
from text_generation_server.models import CausalLM
from text_generation_server.utils import (
    initialize_torch_distributed,
    weight_files,
15
    Weights,
16
17
18
)


19
class OPTSharded(CausalLM):
20
    def __init__(
21
22
23
24
        self,
        model_id: str,
        revision: Optional[str] = None,
        quantize: Optional[str] = None,
Nicolas Patry's avatar
Nicolas Patry committed
25
        speculator: Optional[str] = None,
26
        dtype: Optional[torch.dtype] = 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 if dtype is None else dtype
33
34
        else:
            device = torch.device("cpu")
Wang, Yi's avatar
Wang, Yi committed
35
            dtype = torch.float32 if dtype is None else dtype
36
37

        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
47
48
            model_id,
            revision=revision,
            trust_remote_code=trust_remote_code,
49
        )
50
        config.quantize = quantize
Nicolas Patry's avatar
Nicolas Patry committed
51
        config.speculator = speculator
52
53
54
55
        tokenizer.pad_token_id = config.pad_token_id

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

62
        model = OPTForCausalLM(config, weights)
63
64
65

        torch.distributed.barrier(group=self.process_group)
        super(CausalLM, self).__init__(
66
            model=model,
67
            tokenizer=tokenizer,
68
69
            requires_padding=True,
            dtype=dtype,
70
            device=device,
71
72
            rank=rank,
            world_size=world_size,
73
74
75
76
77
        )

    def forward(
        self, input_ids, attention_mask, position_ids, past_key_values: Optional = None
    ):
78
        outputs, speculative_logits = self.model.forward(
79
80
81
82
83
84
            input_ids=input_ids,
            attention_mask=attention_mask,
            past_key_values=past_key_values,
            use_cache=True,
        )

85
        return outputs.logits, speculative_logits, outputs.past_key_values