-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel_loader_extra.py
141 lines (112 loc) · 5.05 KB
/
model_loader_extra.py
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
from transformers import AutoModelForMaskedLM, RobertaConfig
from modeling_xlmr_extra import *
from modeling_roberta_extra import *
from torch import nn
import numpy as np
import torch
def get_embedding_path(base_model_type, dim, only_eng_vocab=False, random_initialization=False):
assert base_model_type in ['roberta-base', 'xlm-roberta-base', 'xlm-roberta-large']
# if we use random_initialization we have to make sure the model is in full dimension
if random_initialization:
assert dim == 768 or dim == 1024
embedding_path = ''
if base_model_type == 'roberta-base':
embedding_path += 'roberta'
elif base_model_type == 'xlm-roberta-base':
embedding_path += 'xlm'
else:
embedding_path += 'xlm_large'
if random_initialization:
embedding_path += '_rand'
else:
if only_eng_vocab:
embedding_path += '_eng'
else:
embedding_path += '_all'
embedding_path += f"_{str(dim)}"
return embedding_path
def load_assembled_model(base_model_type, dim, only_eng_vocab=False,
path='/mounts/data/proj/yihong/newhome/OFA/stored_factorization/updated',
random_initialization=False):
assert base_model_type in ['roberta-base', 'xlm-roberta-base', 'xlm-roberta-large']
# if we use random_initialization we have to make sure the model is in full dimension
if random_initialization:
assert dim == 768 or dim == 1024
factorize = True
# all available reduced dimension
if base_model_type != 'xlm-roberta-large':
assert dim in [100, 200, 400, 768]
if dim == 768:
# for this we do not perform dimension reduction
factorize = False
else:
assert dim in [100, 200, 400, 800, 1024]
if dim == 1024:
factorize = False
embedding_path = ''
# loading the base model
if base_model_type == 'roberta-base':
embedding_path += 'roberta'
model = RobertaForMaskedLMUpdated.from_pretrained('roberta-base')
config = RobertaConfig.from_pretrained('roberta-base')
elif base_model_type == 'xlm-roberta-base':
embedding_path += 'xlm'
model = XLMRobertaForMaskedLMUpdated.from_pretrained('xlm-roberta-base')
config = XLMRobertaConfig.from_pretrained('xlm-roberta-base')
else:
embedding_path += 'xlm_large'
model = XLMRobertaForMaskedLMUpdated.from_pretrained('xlm-roberta-large')
config = XLMRobertaConfig.from_pretrained('xlm-roberta-large')
if random_initialization:
embedding_path += '_rand'
else:
if only_eng_vocab:
embedding_path += '_eng'
else:
embedding_path += '_all'
embedding_path += f"_{str(dim)}"
primitive_embeddings = None
if factorize:
primitive_embeddings = np.load(f"{path}/{embedding_path}/primitive_embeddings.npy")
config.num_primitive = dim
target_matrix = np.load(f"{path}/{embedding_path}/target_matrix.npy")
config.vocab_size = len(target_matrix)
if factorize:
if base_model_type == 'roberta-base':
assembled_model = RobertaAssembledForMaskedLM(config=config)
else:
assembled_model = XLMRobertaAssembledForMaskedLM(config=config)
# copy the encoder
assembled_model.roberta.encoder = model.roberta.encoder
# initializing / copying some embeddings
assert np.shape(target_matrix)[1] == np.shape(primitive_embeddings)[0]
assembled_model.roberta.embeddings.primitive_embeddings.weight.data = torch.from_numpy(primitive_embeddings.T)
assembled_model.roberta.embeddings.target_coordinates.weight.data = torch.from_numpy(target_matrix)
# regarding embeddings
assembled_model.roberta.embeddings.token_type_embeddings = model.roberta.embeddings.token_type_embeddings
assembled_model.roberta.embeddings.position_embeddings = model.roberta.embeddings.position_embeddings
assembled_model.roberta.embeddings.LayerNorm = model.roberta.embeddings.LayerNorm
assembled_model.roberta.embeddings.dropout = model.roberta.embeddings.dropout
# regarding lm head
assembled_model.lm_head.dense = model.lm_head.dense
assembled_model.lm_head.layer_norm = model.lm_head.layer_norm
else:
assembled_model = model
assembled_model.config.vocab_size = len(target_matrix)
assembled_model.resize_token_embeddings(len(target_matrix))
assembled_model.get_input_embeddings().weight.data = torch.from_numpy(target_matrix)
return assembled_model
def print_model_stats(model):
Total_params = 0
Trainable_params = 0
NonTrainable_params = 0
for param in model.parameters():
mulValue = np.prod(param.size())
Total_params += mulValue
if param.requires_grad:
Trainable_params += mulValue
else:
NonTrainable_params += mulValue
print(f'Total params: {Total_params}')
print(f'Trainable params: {Trainable_params}')
print(f'Non-trainable params: {NonTrainable_params}')