__init__.py 5.51 KB
Newer Older
1
2
import torch

3
from loguru import logger
4
from transformers import AutoConfig
5
from transformers.models.auto import modeling_auto
6
7
from typing import Optional

8
9
from text_generation_server.models.model import Model
from text_generation_server.models.causal_lm import CausalLM
10
from text_generation_server.models.flash_causal_lm import FlashCausalLM
11
12
from text_generation_server.models.bloom import BLOOM, BLOOMSharded
from text_generation_server.models.seq2seq_lm import Seq2SeqLM
13
from text_generation_server.models.opt import OPT, OPTSharded
14
15
from text_generation_server.models.galactica import Galactica, GalacticaSharded
from text_generation_server.models.santacoder import SantaCoder
16
from text_generation_server.models.gpt_neox import GPTNeoxSharded
17
from text_generation_server.models.t5 import T5Sharded
18

19
try:
20
21
22
23
24
25
26
27
    if torch.cuda.is_available():
        major, minor = torch.cuda.get_device_capability()
        is_sm75 = major == 7 and minor == 5
        is_sm8x = major == 8 and minor >= 0
        is_sm90 = major == 9 and minor == 0

        supported = is_sm75 or is_sm8x or is_sm90
        if not supported:
28
29
30
31
32
33
34
35
36
37
38
39
40
41
            raise ImportError(
                f"GPU with CUDA capability {major} {minor} is not supported"
            )

        from text_generation_server.models.flash_neox import FlashNeoX, FlashNeoXSharded
        from text_generation_server.models.flash_llama import (
            FlashLlama,
            FlashLlamaSharded,
        )
        from text_generation_server.models.flash_santacoder import (
            FlashSantacoder,
            FlashSantacoderSharded,
        )

42
43
44
        FLASH_ATTENTION = True
    else:
        FLASH_ATTENTION = False
45
except ImportError:
46
47
48
    logger.opt(exception=True).warning(
        "Could not import Flash Attention enabled models"
    )
49
    FLASH_ATTENTION = False
50

51
52
53
54
55
__all__ = [
    "Model",
    "BLOOM",
    "BLOOMSharded",
    "CausalLM",
56
    "FlashCausalLM",
57
58
59
    "Galactica",
    "GalacticaSharded",
    "GPTNeoxSharded",
60
    "Seq2SeqLM",
61
62
    "Galactica",
    "GalacticaSharded",
63
    "SantaCoder",
64
65
    "OPT",
    "OPTSharded",
66
    "T5Sharded",
67
68
69
    "get_model",
]

70
if FLASH_ATTENTION:
71
72
    __all__.append(FlashNeoX)
    __all__.append(FlashNeoXSharded)
73
    __all__.append(FlashSantacoder)
74
    __all__.append(FlashSantacoderSharded)
75
76
77
    __all__.append(FlashLlama)
    __all__.append(FlashLlamaSharded)

78
79
80
81
82
FLASH_ATT_ERROR_MESSAGE = (
    "{} requires Flash Attention CUDA kernels to be installed.\n"
    "Use the official Docker image (ghcr.io/huggingface/text-generation-inference:latest) "
    "or install flash attention with `cd server && make install install-flash-attention`"
)
83

84
85
86
# The flag below controls whether to allow TF32 on matmul. This flag defaults to False
# in PyTorch 1.12 and later.
torch.backends.cuda.matmul.allow_tf32 = True
87

88
89
# The flag below controls whether to allow TF32 on cuDNN. This flag defaults to True.
torch.backends.cudnn.allow_tf32 = True
90

91
92
93
# Disable gradients
torch.set_grad_enabled(False)

94

95
def get_model(
96
    model_id: str, revision: Optional[str], sharded: bool, quantize: bool
97
) -> Model:
98
    if "facebook/galactica" in model_id:
99
100
101
102
103
        if sharded:
            return GalacticaSharded(model_id, revision, quantize=quantize)
        else:
            return Galactica(model_id, revision, quantize=quantize)

104
    if "bigcode" in model_id:
105
        if sharded:
106
107
108
109
            if not FLASH_ATTENTION:
                raise NotImplementedError(
                    FLASH_ATT_ERROR_MESSAGE.format(f"Sharded Santacoder")
                )
110
            return FlashSantacoderSharded(model_id, revision, quantize=quantize)
111
112
        else:
            santacoder_cls = FlashSantacoder if FLASH_ATTENTION else SantaCoder
113
            return santacoder_cls(model_id, revision, quantize=quantize)
114

115
    config = AutoConfig.from_pretrained(model_id, revision=revision)
116
    model_type = config.model_type
117

118
    if model_type == "bloom":
119
        if sharded:
120
            return BLOOMSharded(model_id, revision, quantize=quantize)
121
        else:
122
            return BLOOM(model_id, revision, quantize=quantize)
123

124
    if model_type == "gpt_neox":
125
        if sharded:
126
            neox_cls = FlashNeoXSharded if FLASH_ATTENTION else GPTNeoxSharded
127
            return neox_cls(model_id, revision, quantize=quantize)
128
        else:
129
            neox_cls = FlashNeoX if FLASH_ATTENTION else CausalLM
130
            return neox_cls(model_id, revision, quantize=quantize)
131

132
133
134
135
    if model_type == "llama":
        if sharded:
            if FLASH_ATTENTION:
                return FlashLlamaSharded(model_id, revision, quantize=quantize)
136
            raise NotImplementedError(FLASH_ATT_ERROR_MESSAGE.format(f"Sharded Llama"))
137
138
139
140
        else:
            llama_cls = FlashLlama if FLASH_ATTENTION else CausalLM
            return llama_cls(model_id, revision, quantize=quantize)

141
142
143
144
145
146
    if config.model_type == "opt":
        if sharded:
            return OPTSharded(model_id, revision, quantize=quantize)
        else:
            return OPT(model_id, revision, quantize=quantize)

147
    if model_type == "t5":
148
149
150
151
        if sharded:
            return T5Sharded(model_id, revision, quantize=quantize)
        else:
            return Seq2SeqLM(model_id, revision, quantize=quantize)
152
153
154

    if sharded:
        raise ValueError("sharded is not supported for AutoModel")
155
156

    if model_type in modeling_auto.MODEL_FOR_CAUSAL_LM_MAPPING_NAMES:
157
        return CausalLM(model_id, revision, quantize=quantize)
158
    if model_type in modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES:
159
        return Seq2SeqLM(model_id, revision, quantize=quantize)
160
161

    raise ValueError(f"Unsupported model type {model_type}")