__init__.py 2.06 KB
Newer Older
1
2
import torch

3
4
5
from transformers import AutoConfig
from typing import Optional

6
from text_generation.models.model import Model
7
from text_generation.models.causal_lm import CausalLM
8
from text_generation.models.bloom import BLOOM, BLOOMSharded
9
from text_generation.models.seq2seq_lm import Seq2SeqLM
10
from text_generation.models.galactica import Galactica, GalacticaSharded
11
from text_generation.models.santacoder import SantaCoder
12
from text_generation.models.gpt_neox import GPTNeox, GPTNeoxSharded
13
14
15
16
17
18
19
20
21
22
23
24
25
26

__all__ = [
    "Model",
    "BLOOM",
    "BLOOMSharded",
    "CausalLM",
    "Seq2SeqLM",
    "SantaCoder",
    "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
27

28
29
# The flag below controls whether to allow TF32 on cuDNN. This flag defaults to True.
torch.backends.cudnn.allow_tf32 = True
30
31


32
33
34
35
36
37
38
39
40
41
42
def get_model(
    model_name: str, revision: Optional[str], sharded: bool, quantize: bool
) -> Model:
    config = AutoConfig.from_pretrained(model_name)

    if config.model_type == "bloom":
        if sharded:
            return BLOOMSharded(model_name, revision, quantize=quantize)
        else:
            return BLOOM(model_name, revision, quantize=quantize)
    elif config.model_type == "gpt_neox":
43
        if sharded:
44
            return GPTNeoxSharded(model_name, revision, quantize=quantize)
45
        else:
46
            return GPTNeox(model_name, revision, quantize=quantize)
47
48
    elif model_name.startswith("facebook/galactica"):
        if sharded:
49
            return GalacticaSharded(model_name, revision, quantize=quantize)
50
        else:
51
            return Galactica(model_name, revision, quantize=quantize)
52
    elif "santacoder" in model_name:
53
        return SantaCoder(model_name, revision, quantize)
54
    else:
55
56
        if sharded:
            raise ValueError("sharded is not supported for AutoModel")
57
        try:
58
            return CausalLM(model_name, revision, quantize=quantize)
59
        except Exception:
60
            return Seq2SeqLM(model_name, revision, quantize=quantize)