-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
351 lines (319 loc) · 12.1 KB
/
model.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from lib import Embedding, Routeur, NextTokenPrediction, Block
from lib import config, device, set_grad_params
from lib import printd, tokenizer, lineno
from typing import Union, List
import torch
import torch.nn as nn
import os
import gc
PASSAGE_STOP = -1
PASSAGE_CONTINUE_WITH_ROUTEUR = -2
class MixtofExp(nn.Module):
def __init__(
self,
max_length: int = 50,
max_routeur_passages: int = 8,
force_passage: list[int] = [],
model_name: str = "moe"
) -> None:
"""
The `__init__` function initializes the attributes of the `MixtofExp`
class, including the embedding layer, routing layer,
next token prediction layer, and various parameters.
:param max_length: The `max_length` parameter represents
the maximum length of the input sequence.
It is used to determine the size of the embedding layer in the model,
defaults to 50
(optional)
"""
super().__init__()
#
self.embedding: Embedding = Embedding()
self.routeur: Routeur = Routeur()
self.next_token_prediction: NextTokenPrediction = NextTokenPrediction()
#
self.max_routeur_passages: int = max_routeur_passages
self.force_passage: list[int] = force_passage
self.max_blocks: int = config["max_loaded_blocks"]
self.nb_blocks_to_remove: int = config["nb_blocks_to_remove"]
self.blocks: dict = {}
#
self.max_length = max_length
#
self.model_name = model_name
#
self.training_config: Union[None, dict] = None
self.is_in_training_mode = False
def set_training_mode(self):
self.is_in_training_mode = True
self.train()
def forward_block(self, X: torch.Tensor, block_id: int) -> torch.Tensor:
if block_id not in self.blocks:
self.load_block(block_id)
#
self.blocks[block_id]["usage"] += 1
printd(f"Model, l{lineno()}, Passing by block ", block_id)
#
printd(f"Model, l{lineno()}, X : ", X.shape, type(X), X.type())
X.type(torch.float)
#
# if "gradient_checkpoint_enable" in config and \
# config["gradient_checkpoint_enable"] == 1:
# #
# X = torch.utils.checkpoint.checkpoint(
# self.blocks[block_id]["model"].forward, X)
# else:
# X = self.blocks[block_id]["model"].forward(X)
X = self.blocks[block_id]["model"].forward(X)
#
X.type(torch.float)
return X
def forward_routeur_passage(
self,
X: torch.Tensor,
routeur_passages: int = 1,
blocks_filter: List[int] = []
) -> torch.Tensor:
#
block_id_t: torch.Tensor = self.routeur(X, blocks_filter)
if block_id_t.shape[0] > 1: # Multiple batchs
block_id: int = int(torch.argmax(torch.bincount(block_id_t)).item())
else: # Unique Batch
block_id: int = int(block_id_t.item())
printd(f"Model, l{lineno()}, block_id : ", block_id, type(block_id))
while block_id != 0 or routeur_passages < self.max_routeur_passages:
routeur_passages += 1
#
X = self.forward_block(X, block_id)
#
block_id_t = self.routeur(X, blocks_filter)
if block_id_t.shape[0] > 1: # Multiple batchs
block_id = int(torch.argmax(torch.bincount(block_id_t)).item())
else: # Unique Batch
block_id = int(block_id_t.item())
#
return X
def forward(self, X: torch.Tensor):
"""
The forward function takes an input tensor,
applies embedding and routing operations, passes the
input through multiple blocks, and returns the predicted next token.
@param X The parameter X is a torch.Tensor, which represents
the input data for the forward pass of the model.
@return the predicted token (tk) after passing through
the model and blocks.
"""
#
#
# if "gradient_checkpoint_enable" in config and \
# config["gradient_checkpoint_enable"] == 1:
# #
# X = torch.utils.checkpoint.checkpoint(self.embedding, X)
# else:
# X = self.embedding(X)
X = self.embedding(X)
#
printd(f"Model, l{lineno()}, X: ", X.shape, type(X), X.type())
X.type(torch.float)
#
routeur_passages: int = 1
#
if self.force_passage != []:
for block_id in self.force_passage:
if block_id == PASSAGE_STOP:
break
elif block_id == PASSAGE_CONTINUE_WITH_ROUTEUR:
#
# if "gradient_checkpoint_enable" in config and \
# config["gradient_checkpoint_enable"] == 1:
# #
# torch.utils.checkpoint.checkpoint(
# self.forward_routeur_passage, X, routeur_passages)
# else:
# self.forward_routeur_passage(X, routeur_passages)
self.forward_routeur_passage(X, routeur_passages)
#
break
elif isinstance(block_id, list):
#
# if "gradient_checkpoint_enable" in config and \
# config["gradient_checkpoint_enable"] == 1:
# #
# torch.utils.checkpoint.checkpoint(
# self.forward_routeur_passage,
# X, routeur_passages, block_id)
# else:
# self.forward_routeur_passage(
# X, routeur_passages, block_id)
self.forward_routeur_passage(
X, routeur_passages, block_id)
#
continue
#
routeur_passages += 1
#
X = self.forward_block(X, block_id)
else:
self.forward_routeur_passage(X)
#
tk = self.next_token_prediction(X)
printd(f"Model, l{lineno()}, tk: ", tk, tk.shape, type(tk), tk.type())
return tk
def forward_txt(self, txt: str):
#
X: torch.Tensor = tokenizer.encode(
txt,
max_length=config["context_length"],
padding="max_length",
truncation=True,
return_tensors="pt"
).to(device)
printd(f"Model->forward_txt, l{lineno()}, X: ", X.shape, type(X))
printd(X)
#
return self.forward(X)
def use(self, txt: str):
"""
The function takes an input X, passes it through a forward function,
converts the output index to tokens using a tokenizer,
and returns the tokens.
:param X: The parameter X is the input data that you want
to pass through the model. It could be a single example
or a batch of examples, depending on the implementation of the model
:return: the token corresponding to the index obtained from
the forward pass of the model.
"""
printd(f"Model->forward_txt, l{lineno()}, txt: \"{txt}\"")
idx = self.next_token_prediction.get_next_token_idx(
self.forward_txt(txt))
printd(f"Model->forward_txt, l{lineno()}, idx: \"{idx}\"")
idx = idx.item()
printd(f"Model->forward_txt, l{lineno()}, idx: \"{idx}\"")
tk = tokenizer.convert_ids_to_tokens(idx)
printd(f"Model->forward_txt, l{lineno()}"
f", tk: \"{tk}\"")
return tk
def load_weights(self) -> None:
"""
The function loads weights from a file and sets the model
to evaluation mode.
:param filename: The `filename` parameter is a string that represents
the path and name of the
file from which the weights will be loaded.
By default, it is set to "weights/moe.pt", defaults
to weights/moe.pt
:type filename: str (optional)
"""
#
filename = f"weights/{self.model_name}/moe.pt"
#
if os.path.exists(filename):
self.load_state_dict(torch.load(filename), strict=False)
self.eval()
def save_weights(self) -> None:
"""
This function saves the weights of a model.
"""
#
if not os.path.exists("weights/"):
os.mkdir("weights/")
if not os.path.exists(f"weights/{self.model_name}/"):
os.mkdir(f"weights/{self.model_name}/")
#
filename = f"weights/{self.model_name}/moe.pt"
#
torch.save(self.state_dict(), filename)
#
for id_block in self.blocks:
self.save_block(id_block)
def load_block(self, block_id: int) -> None:
"""
The `load_block` function loads a block of weights from a file
and adds it to a dictionary of blocks, while also managing
the number of blocks to ensure it doesn't exceed a maximum limit.
:param block_id: The `block_id` parameter is an integer
that represents the ID of the block to be loaded
:type block_id: int
"""
#
if len(self.blocks) >= self.max_blocks:
while len(self.blocks) >= self.max_blocks-self.nb_blocks_to_remove:
min_blk: Union[int, None] = None
for i in self.blocks:
if min_blk is None or \
self.blocks[min_blk]["usage"] > self.blocks[i]["usage"]:
min_blk = i
#
if min_blk is not None:
printd("Limit of blocks reached."
f"Unloading block : {min_blk}")
self.unload_block(min_blk)
else:
break
#
if not os.path.exists("weights/"):
os.mkdir("weights/")
#
block = Block(block_id)
#
if os.path.exists(f"weights/{self.model_name}/block_{block_id}.pt"):
#
block.load_state_dict(
torch.load(f"weights/{self.model_name}/block_{block_id}.pt")
)
#
printd(f"Loading block : {block_id}")
#
self.register_module(f"block_{block_id}", block)
#
set_grad_params(
block,
False if block_id in self.training_config["freeze_blocks"]
else True
)
#
# if "gradient_checkpoint_enable" in config and \
# config["gradient_checkpoint_enable"] == 1:
# #
# block.gradient_checkpointing_enable()
if self.is_in_training_mode:
block.train()
block.zero_grad()
else:
block.eval()
#
self.blocks[block_id] = {
"model": block,
"usage": 0
}
def save_block(self, block_id: int) -> None:
"""
The function saves the state dictionary of a model associated
with a given block ID to a file.
:param block_id: The `block_id` parameter is an integer
that represents the unique identifier of
a block
:type block_id: int
"""
#
if not os.path.exists(f"weights/{self.model_name}/"):
os.mkdir(f"weights/{self.model_name}/")
#
if block_id in self.blocks:
torch.save(self.blocks[block_id]["model"].state_dict(),
f"weights/{self.model_name}/block_{block_id}.pt")
def unload_block(self, block_id: int) -> None:
"""
The function unloads a block by saving it and removing it from
the blocks dictionary.
:param block_id: The `block_id` parameter is an integer
that represents the ID of the block that
needs to be unloaded
:type block_id: int
"""
assert block_id in self.blocks
#
self.save_block(block_id)
del (self.blocks[block_id])
# gc.collect()
torch.cuda.empty_cache()