"README_ORIGIN.md" did not exist on "e3b318b28e4187f8e0c41afc2a6c80f92c678390"
__init__.py 4.98 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
20
try:
    from text_generation_server.models.flash_neox import FlashNeoX, FlashNeoXSharded
21
    from text_generation_server.models.flash_llama import FlashLlama, FlashLlamaSharded
22
23
24
25
    from text_generation_server.models.flash_santacoder import (
        FlashSantacoder,
        FlashSantacoderSharded,
    )
26

27
    FLASH_ATTENTION = torch.cuda.is_available()
28
except ImportError:
29
    logger.opt(exception=True).warning("Could not import Flash Attention enabled models")
30
    FLASH_ATTENTION = False
31

32
33
34
35
36
__all__ = [
    "Model",
    "BLOOM",
    "BLOOMSharded",
    "CausalLM",
37
    "FlashCausalLM",
38
39
40
    "Galactica",
    "GalacticaSharded",
    "GPTNeoxSharded",
41
    "Seq2SeqLM",
42
43
    "Galactica",
    "GalacticaSharded",
44
    "SantaCoder",
45
46
    "OPT",
    "OPTSharded",
47
    "T5Sharded",
48
49
50
    "get_model",
]

51
if FLASH_ATTENTION:
52
53
    __all__.append(FlashNeoX)
    __all__.append(FlashNeoXSharded)
54
    __all__.append(FlashSantacoder)
55
    __all__.append(FlashSantacoderSharded)
56
57
58
    __all__.append(FlashLlama)
    __all__.append(FlashLlamaSharded)

59
60
61
62
63
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`"
)
64

65
66
67
# 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
68

69
70
# The flag below controls whether to allow TF32 on cuDNN. This flag defaults to True.
torch.backends.cudnn.allow_tf32 = True
71

72
73
74
# Disable gradients
torch.set_grad_enabled(False)

75

76
def get_model(
77
    model_id: str, revision: Optional[str], sharded: bool, quantize: bool
78
) -> Model:
79
    if "facebook/galactica" in model_id:
80
81
82
83
84
        if sharded:
            return GalacticaSharded(model_id, revision, quantize=quantize)
        else:
            return Galactica(model_id, revision, quantize=quantize)

85
    if "bigcode" in model_id:
86
        if sharded:
87
88
89
90
91
            if not FLASH_ATTENTION:
                raise NotImplementedError(
                    FLASH_ATT_ERROR_MESSAGE.format(f"Sharded Santacoder")
                )
            return FlashSantacoderSharded(model_id, revision=revision)
92
93
94
        else:
            santacoder_cls = FlashSantacoder if FLASH_ATTENTION else SantaCoder
            return santacoder_cls(model_id, revision, quantize)
95

96
    config = AutoConfig.from_pretrained(model_id, revision=revision)
97
    model_type = config.model_type
98

99
    if model_type == "bloom":
100
        if sharded:
101
            return BLOOMSharded(model_id, revision, quantize=quantize)
102
        else:
103
            return BLOOM(model_id, revision, quantize=quantize)
104

105
    if model_type == "gpt_neox":
106
        if sharded:
107
            neox_cls = FlashNeoXSharded if FLASH_ATTENTION else GPTNeoxSharded
108
            return neox_cls(model_id, revision, quantize=quantize)
109
        else:
110
            neox_cls = FlashNeoX if FLASH_ATTENTION else CausalLM
111
            return neox_cls(model_id, revision, quantize=quantize)
112

113
114
115
116
    if model_type == "llama":
        if sharded:
            if FLASH_ATTENTION:
                return FlashLlamaSharded(model_id, revision, quantize=quantize)
117
            raise NotImplementedError(FLASH_ATT_ERROR_MESSAGE.format(f"Sharded Llama"))
118
119
120
121
        else:
            llama_cls = FlashLlama if FLASH_ATTENTION else CausalLM
            return llama_cls(model_id, revision, quantize=quantize)

122
123
124
125
126
127
    if config.model_type == "opt":
        if sharded:
            return OPTSharded(model_id, revision, quantize=quantize)
        else:
            return OPT(model_id, revision, quantize=quantize)

128
    if model_type == "t5":
129
130
131
132
        if sharded:
            return T5Sharded(model_id, revision, quantize=quantize)
        else:
            return Seq2SeqLM(model_id, revision, quantize=quantize)
133
134
135

    if sharded:
        raise ValueError("sharded is not supported for AutoModel")
136
137

    if model_type in modeling_auto.MODEL_FOR_CAUSAL_LM_MAPPING_NAMES:
138
        return CausalLM(model_id, revision, quantize=quantize)
139
    if model_type in modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES:
140
        return Seq2SeqLM(model_id, revision, quantize=quantize)
141
142

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