forked from fhamborg/NewsMTSC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFXBaseModel.py
More file actions
147 lines (110 loc) · 4.21 KB
/
Copy pathFXBaseModel.py
File metadata and controls
147 lines (110 loc) · 4.21 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
from copy import copy
import torch.nn as nn
from abc import abstractmethod
from transformers import XLNetModel, AlbertModel, BertModel, RobertaModel
from functools import wraps
from download import Download
class FXBaseModel(nn.Module):
def __init__(self):
super().__init__()
@staticmethod
@abstractmethod
def get_language_models():
return
@staticmethod
@abstractmethod
def get_input_field_ids():
return
def invoke_language_model(self, lm, input_ids, token_type_ids=None):
type_lm = type(lm)
if type_lm == XLNetModel:
last_hidden_state, mems, all_hidden_states = lm(
input_ids=input_ids,
token_type_ids=token_type_ids,
)
elif type_lm in [AlbertModel, BertModel, RobertaModel]:
if token_type_ids is None:
last_hidden_state, pooler_output, hidden_states = lm(
input_ids=input_ids,
)
else:
last_hidden_state, pooler_output, hidden_states = lm(
input_ids=input_ids,
token_type_ids=token_type_ids
)
else:
raise NotImplementedError
return last_hidden_state
def provide_pretrained(version, pretrained_url):
"""
Usage:
@provide_pretrained("v1.0.0", "https://example.com/link/to/state_dict")
class Example(nn.Module):
pass
"""
def decorator(model_class):
# The actual decorator to use before the class
wraps(model_class)
wrapper = __get_pretrained_wrapper_class(model_class)
wrapper._provide_pretrained_versions[version] = pretrained_url
return wrapper
return decorator
def default_pretrained(version):
"""
Set the version which should be used as the default version and will be used when running with --pretrained.
Usage:
@default_pretrained("v1.0.0")
@provide_pretrained("v1.0.0", "https://example.com/link/to/state_dict")
class Example(nn.Module):
pass
"""
def decorator(model_class):
# The actual decorator to use before the class
wraps(model_class)
wrapper = __get_pretrained_wrapper_class(model_class)
wrapper._provide_pretrained_default = version
return wrapper
return decorator
def model_includes_pretrained(model):
"""
Checks if a model-class includes the methods to load pretrained models.
Arguments:
model Model-class to check.
Returns:
True if it includes the functionality.
"""
return hasattr(model, 'has_pretrained_state_dict') and hasattr(model, 'get_pretrained_state_dict')
__pretrained_wrapper_classes = set()
def __get_pretrained_wrapper_class(base_class):
if base_class in __pretrained_wrapper_classes:
return base_class
class PretrainedWrapper(base_class):
_provide_pretrained_default = None
_provide_pretrained_versions = {}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@classmethod
def has_pretrained_state_dict(cls, version=None):
version = version or cls._provide_pretrained_default
return version in cls._provide_pretrained_versions
@classmethod
def get_pretrained_state_dict(cls, version=None, download_if_not_exists=True, **kwargs):
path = Download.model_path(cls, version)
if os.path.isfile(path):
if download_if_not_exists:
Download.download(cls, version, False)
else:
raise FileNotFoundError('State dict not found')
return cls.load_state_dict(path, **kwargs)
@classmethod
def get_pretrained_versions(cls):
return copy(cls._provide_pretrained_versions)
@classmethod
def get_pretrained_source(cls, version=None):
return cls._provide_pretrained_versions[version or cls._provide_pretrained_default]
@classmethod
def get_pretrained_default_version(cls):
return cls._provide_pretrained_default
__pretrained_wrapper_classes.add(PretrainedWrapper)
return PretrainedWrapper