idefics.py 3.54 KB
Newer Older
1
2
3
import torch
import torch.distributed

4
from typing import Optional
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20


from text_generation_server.models.custom_modeling.idefics_config import IdeficsConfig
from text_generation_server.models.custom_modeling.idefics_processing import (
    IdeficsProcessor,
)
from transformers import LlamaTokenizerFast
from text_generation_server.models.custom_modeling.idefics_modeling import (
    IdeficsForVisionText2Text,
)
from text_generation_server.models.idefics_causal_lm import IdeficsCausalLM
from text_generation_server.utils import (
    initialize_torch_distributed,
    weight_files,
    Weights,
)
21
from text_generation_server.utils.quantization import get_loader
22

23
24
from text_generation_server.utils.import_utils import SYSTEM

25
26
27
28
29
30
31

class IDEFICSSharded(IdeficsCausalLM):
    def __init__(
        self,
        model_id: str,
        revision: Optional[str] = None,
        quantize: Optional[str] = None,
Nicolas Patry's avatar
Nicolas Patry committed
32
        speculator: Optional[str] = None,
33
34
35
        dtype: Optional[torch.dtype] = None,
        trust_remote_code: bool = False,
    ):
Nicolas Patry's avatar
Nicolas Patry committed
36
        self.quantize = quantize
37
38
39
40
41
        self.process_group, rank, world_size = initialize_torch_distributed()
        if torch.cuda.is_available():
            device = torch.device(f"cuda:{rank}")
            # 9b seems to work correctly enough in float16, but 80b seems
            # to be really saturating for f16.
Nicolas Patry's avatar
Nicolas Patry committed
42
            dtype = torch.float16 if dtype is None else dtype
43
44
45
46
47
48
49
50
        elif SYSTEM == "ipex":
            if hasattr(torch, "xpu") and torch.xpu.is_available():
                device = torch.device(f"xpu:{rank}")
                dtype = torch.float16 if dtype is None else dtype
            else:
                device = torch.device("cpu")
                # Float16 doesn't exist on target.
                dtype = torch.bfloat16 if dtype is None else dtype
51
52
        else:
            device = torch.device("cpu")
Wang, Yi's avatar
Wang, Yi committed
53
            dtype = torch.float32 if dtype is None else dtype
54
55
56
57
58
59
60
61
        self.device, self.dtype = device, dtype

        config = IdeficsConfig.from_pretrained(
            model_id,
            revision=revision,
            trust_remote_code=trust_remote_code,
        )
        config.quantize = quantize
Nicolas Patry's avatar
Nicolas Patry committed
62
        config.speculator = speculator
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
        config.vision_config.quantize = quantize

        tokenizer = LlamaTokenizerFast.from_pretrained(
            model_id,
            revision=revision,
            padding_side="left",
            truncation_side="left",
            trust_remote_code=trust_remote_code,
        )
        self.processor = IdeficsProcessor.from_pretrained(
            model_id,
            revision=revision,
            padding_side="left",
            truncation_side="left",
            trust_remote_code=trust_remote_code,
        )

80
81
82
        weights_loader = get_loader(
            quantize=quantize, model_id=model_id, revision=revision
        )
83
84
85
86
87
88
89
        torch.distributed.barrier(group=self.process_group)
        filenames = weight_files(model_id, revision=revision, extension=".safetensors")
        weights = Weights(
            filenames,
            device=device,
            dtype=dtype,
            process_group=self.process_group,
90
            weights_loader=weights_loader,
91
92
93
94
95
96
        )

        model = IdeficsForVisionText2Text(config, weights)

        torch.distributed.barrier(group=self.process_group)
        super(IdeficsCausalLM, self).__init__(
drbh's avatar
drbh committed
97
            model_id=model_id,
98
99
100
101
102
103
104
105
            model=model,
            tokenizer=tokenizer,
            requires_padding=True,
            dtype=dtype,
            device=device,
            rank=rank,
            world_size=world_size,
        )