forked from dmlc/gluon-nlp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
70 lines (61 loc) · 2.36 KB
/
Copy path__init__.py
File metadata and controls
70 lines (61 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from typing import Tuple, List
from . import albert
from . import bert
from . import electra
from . import mobilebert
from . import roberta
from . import transformer
from . import transformer_xl
from . import xlmr
from ..base import get_model_zoo_home_dir
from ..registry import BACKBONE_REGISTRY
from ..data.tokenizers import BaseTokenizer
__all__ = ['list_backbone_names', 'get_backbone']
def list_backbone_names():
all_keys = []
for backbone_type in BACKBONE_REGISTRY.list_keys():
all_keys.extend(BACKBONE_REGISTRY.get(backbone_type)[-1]())
return all_keys
def get_backbone(model_name: str,
root: str = get_model_zoo_home_dir(),
**kwargs) -> Tuple['Block', str, BaseTokenizer, str, List]:
"""Get the backbone network
Parameters
----------
model_name
The name of the pretrained model
root
Downloaded directory of the model zoo
Returns
-------
model_cls
The class to construct the backbone network
cfg
Path to the config file of the backbone
tokenizer
The tokenizer that is bound to the backbone model
backbone_param_path
The path to the pretrained backbone weights
others
The other items returned by the create function.
Will be wrapped into a list
Examples
--------
>>> from gluonnlp.models import get_backbone
>>> model_cls, tokenizer, cfg, backbone_param_path = get_backbone('google_en_cased_bert_base')
>>> model = model_cls.from_cfg(cfg)
>>> model.load_parameters(backbone_param_path)
"""
model_cls, local_create_fn = None, None
for backbone_type in BACKBONE_REGISTRY.list_keys():
ele_model_cls, ele_local_create_fn, list_key_fn = BACKBONE_REGISTRY.get(backbone_type)
if model_name in list_key_fn():
model_cls = ele_model_cls
local_create_fn = ele_local_create_fn
if model_cls is None or local_create_fn is None:
raise KeyError('The backbone model "{}" is not found! '
'Here are all available backbone models = {}'
.format(model_name, list_backbone_names()))
cfg, tokenizer, local_params_path, *others = local_create_fn(model_name=model_name, root=root,
**kwargs)
return model_cls, cfg, tokenizer, local_params_path, others