__init__.py 2.43 KB
Newer Older
1
2
import torch

3
4
5
from transformers import AutoConfig
from typing import Optional

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

__all__ = [
    "Model",
    "BLOOM",
    "BLOOMSharded",
    "CausalLM",
20
21
22
    "Galactica",
    "GalacticaSharded",
    "GPTNeoxSharded",
23
24
    "Seq2SeqLM",
    "SantaCoder",
25
    "T5Sharded",
26
27
28
29
30
31
    "get_model",
]

# 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
32

33
34
# The flag below controls whether to allow TF32 on cuDNN. This flag defaults to True.
torch.backends.cudnn.allow_tf32 = True
35

36
37
38
# Disable gradients
torch.set_grad_enabled(False)

39

40
def get_model(
41
    model_id: str, revision: Optional[str], sharded: bool, quantize: bool
42
) -> Model:
43
    if "facebook/galactica" in model_id:
44
45
46
47
48
49
50
51
        if sharded:
            return GalacticaSharded(model_id, revision, quantize=quantize)
        else:
            return Galactica(model_id, revision, quantize=quantize)

    if "santacoder" in model_id:
        return SantaCoder(model_id, revision, quantize)

52
    config = AutoConfig.from_pretrained(model_id, revision=revision)
53
54
55

    if config.model_type == "bloom":
        if sharded:
56
            return BLOOMSharded(model_id, revision, quantize=quantize)
57
        else:
58
            return BLOOM(model_id, revision, quantize=quantize)
59
60

    if config.model_type == "gpt_neox":
61
        if sharded:
62
            return GPTNeoxSharded(model_id, revision, quantize=quantize)
63
        else:
64
            return CausalLM(model_id, revision, quantize=quantize)
65
66

    if config.model_type == "t5":
67
68
69
70
        if sharded:
            return T5Sharded(model_id, revision, quantize=quantize)
        else:
            return Seq2SeqLM(model_id, revision, quantize=quantize)
71
72
73
74
75
76
77

    if sharded:
        raise ValueError("sharded is not supported for AutoModel")
    try:
        return CausalLM(model_id, revision, quantize=quantize)
    except Exception:
        return Seq2SeqLM(model_id, revision, quantize=quantize)