-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodeling_grasp.py
More file actions
467 lines (396 loc) · 20.5 KB
/
modeling_grasp.py
File metadata and controls
467 lines (396 loc) · 20.5 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import torch
import logging
import numpy as np
import torch.nn as nn
from torch.utils.data import DataLoader
from tqdm import tqdm
from typing import Literal, Optional, List, Union
from tools.utils_func import block_influence, adaptive_rank_selection
from transformers import AutoModelForCausalLM, AutoConfig, PreTrainedModel
logger = logging.getLogger(__name__)
def setup_logger(log_file=None):
# Clear existing handlers
if logger.hasHandlers():
logger.handlers.clear()
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
if log_file:
handler = logging.FileHandler(log_file)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
class SVDLinear(nn.Module):
def __init__(self, U: torch.Tensor, S: torch.Tensor, Vh: torch.Tensor, bias: Optional[torch.Tensor], sigma_fuse: Literal["UV", "U", "V"] = "UV"):
'''
**__Args__:**
U: Left Singular Vectors after rank truncation, which is shape of [rank, out_features]
S: Diagonal Matrix of singular values, which is shape of [rank, rank]
Vh: Right Singular Vectors after rank truncation, which is shape of [in_features, rank]
bias: bias
'''
super(SVDLinear, self).__init__()
in_features = Vh.shape[1]
out_features = U.shape[0]
hidden_size = S.shape[0]
self.InLinear = nn.Linear(in_features=in_features, out_features=hidden_size, bias=False)
self.OutLinear = nn.Linear(in_features=hidden_size, out_features=out_features, bias=True if bias is not None else False)
if bias is not None:
self.OutLinear.bias.data = bias
if sigma_fuse == "UV":
self.InLinear.weight.data = Vh.mul(S.sqrt().view(-1, 1)).contiguous()
self.OutLinear.weight.data = U.mul(S.sqrt()).contiguous()
elif sigma_fuse == "U":
self.InLinear.weight.data = Vh.contiguous()
self.OutLinear.weight.data = U.mul(S).contiguous()
elif sigma_fuse == "V":
self.InLinear.weight.data = Vh.mul(S.view(-1, 1)).contiguous()
else:
raise ValueError(f"value of sigma_fuse {sigma_fuse} not support")
def forward(self, x: torch.Tensor):
output = self.OutLinear(self.InLinear(x))
return output
class GRASPLayer(nn.Module):
def __init__(self, U: torch.Tensor, S: torch.Tensor, Vh: torch.Tensor, bias: Optional[torch.Tensor], compression_ratio: Optional[float]):
super(GRASPLayer, self).__init__()
self.U = nn.Parameter(U.clone().detach().requires_grad_(False))
self.S = nn.Parameter(S.clone().detach().requires_grad_(True))
self.Vh = nn.Parameter(Vh.clone().detach().requires_grad_(False))
self.in_features = self.Vh.shape[1]
self.out_features = self.U.shape[0]
self.bias = bias
self.compression_ratio = compression_ratio
def forward(self, x: torch.Tensor):
b, s, d = x.shape
sigma = torch.diag(self.S)
W_reconstructed = torch.mm(self.U, torch.mm(sigma, self.Vh))
return torch.mm(x.view(b*s, -1), W_reconstructed.t()).view(b, s, -1)
class GRASPBaseModel(nn.Module):
def __init__(self, model: nn.Module, *args, **kwargs) -> None:
super(GRASPBaseModel, self).__init__(*args, **kwargs)
self.model = model
for params in self.model.parameters():
params.requires_grad = False
self.grasp_values_dict = {}
def calculate_layer_compression_ratio(self, redundant_layers: Optional[List] = None):
'''
calculate module-wise compression ratio
for module_name, module in self.model.named_modules():
if isinstance(module, nn.Linear) and "lm_head" not in module_name:
module.compression_ratio = self_define_ratio
'''
pass
@staticmethod
def _extract_layer_index(module_name):
"""
Extracts the layer index from the module name.
Supposes the module name contains the layer index in a consistent format, e.g., 'model.layers.23.mlp'.
"""
try:
parts = module_name.split('.')
if "layers" in parts:
index = int(parts[parts.index("layers") + 1]) # Index after "layers"
return index
except (ValueError, IndexError):
return None
def print_trainable_params(self, log_file: Optional[str] = None):
setup_logger(log_file=log_file)
total_params = sum(p.numel() for p in self.parameters())
trainable_params = sum(p.numel() for p in self.parameters() if p.requires_grad)
trainable_percentage = (trainable_params / total_params) * 100
logger.info(f"trainable params: {trainable_params} || all params: {total_params} || trainable: {trainable_percentage:.2f}%")
def compute_bi(
self,
num_prune_layers: Optional[int] = 1,
calibration_dataloader: Optional[DataLoader] = None,
hiddens: Optional[List[torch.Tensor]] = None,
angular: bool = False,
device: Literal["cpu", "cuda"] = "cuda",
log_file: Optional[str] = None,
*args, **kwargs
):
setup_logger(log_file=log_file)
self.layer_importances = [0 for _ in self.model.model.layers]
"""
Computes layer-wise importances over input tokens.
"""
def compute_bi_hiddens(hiddens: Optional[List[torch.Tensor]] = None):
if not angular:
num_prune_layers = 1
for i in range(len(hiddens) - num_prune_layers):
in_hidden = hiddens[i]
out_hidden = hiddens[i+num_prune_layers]
if angular:
# use only last token for angular distance as described in section 3.2
# https://arxiv.org/pdf/2403.17887.pdf
in_hidden = in_hidden[:,-1:]
out_hidden = out_hidden[:,-1:]
self.layer_importances[i] += block_influence(
in_hidden,
out_hidden,
angular=angular
).mean().cpu().item()
logger.info("=======>Compute Block Influence")
assert hiddens is not None or calibration_dataloader is not None, "please provide hidden_states or calibration dataloader to compute block influence"
if hiddens is not None:
compute_bi_hiddens(hiddens=hiddens)
else:
for batch in tqdm(calibration_dataloader, desc="Compute BI", total=len(calibration_dataloader), leave=True):
if len(batch) == 2:
attention_mask = None
else:
attention_mask = batch["attention_mask"].to(device=device)
input_ids = batch["input_ids"].to(device=device)
outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, use_cache=False, output_hidden_states=True, return_dict=True)
hiddens = outputs.hidden_states
compute_bi_hiddens(hiddens=hiddens)
if angular:
start_layer = np.argsort(np.array(self.layer_importances[:-num_prune_layers+1]))[0]
layers_to_remove = list(range(start_layer, start_layer + num_prune_layers))
else:
layers_to_remove = np.argsort(np.array(self.layer_importances))[:num_prune_layers].tolist()
self.redundant_layers = layers_to_remove
return self.layer_importances, layers_to_remove
def remove_layers(self, layers_to_remove: Optional[List[int]] = [], angular: Optional[bool] = False, num_prune_layers: Optional[int] = None):
if not layers_to_remove:
if angular:
assert self.layer_importances, "Need to compute importances with self.compute_bi()"
assert num_prune_layers, "Need number of layers to prune"
start_layer = np.argsort(np.array(self.layer_importances[:-num_prune_layers+1]))[0]
layers_to_remove = list(range(start_layer, start_layer + num_prune_layers))
else:
layers_to_remove = np.argsort(np.array(self.layer_importances))[:num_prune_layers].tolist()
if layers_to_remove is not None:
# remove layers in reverse to avoid indexing errors
for layer_idx in sorted(layers_to_remove, reverse=True):
try:
del self.model.model.layers[layer_idx]
except IndexError:
logger.info(f"layer {layer_idx} does not exist, function may have already been called")
return []
return layers_to_remove
else:
raise NotImplementedError("lack layers_to_remove")
def _set_module(self, model, submodule_key, module):
tokens = submodule_key.split('.')
sub_model = model
for token in tokens[:-1]:
sub_model = getattr(sub_model, token)
setattr(sub_model, tokens[-1], module)
def replace_with_GRASPLayer(self, target_layer: str, device: Literal["cuda", "cpu"] = "cuda", log_file: Optional[str] = None):
setup_logger(log_file=log_file)
replace_flag = False
module = self.model.get_submodule(target=target_layer)
if isinstance(module, nn.Linear):
w = module.weight.data
U, S, Vh = torch.linalg.svd(w.to(device=device), full_matrices=False)
bias = module.bias
compression_ratio = getattr(module, "compression_ratio", None)
grasp_layer = GRASPLayer(U=U, S=S, Vh=Vh, bias=bias, compression_ratio=compression_ratio)
self._set_module(self.model, target_layer, grasp_layer)
replace_flag = True
else:
raise TypeError(f"target layer should be of Linear module, but got {type(module)}")
if not replace_flag:
logger.info(f"failed to replace with GRASPLayer, target layer: {target_layer} not found in model")
return
def compress_block(
self,
layer_id: int,
block_type: Literal["attention", "mlp"],
target_layer_types: Union[List[str], str] = ["q_proj", "k_proj", "v_proj", "o_proj", "down_proj", "up_proj", "gate_proj"],
device: Literal["cuda", "cpu"] = "cuda",
allocation_aware: Optional[bool] = None,
verbose: bool = False,
log_file: Optional[str] = None
):
'''
Compress transformer-based LLM within a transformer block using GRASP
'''
setup_logger(log_file=log_file)
if layer_id is None:
raise ValueError("Layer id should be given, but got None")
if target_layer_types is None:
return True
if block_type == "attention":
default_layer_types = ["q_proj", "k_proj", "v_proj", "o_proj"] # by default
if not target_layer_types:
target_layer_types = default_layer_types
else:
is_valid = all(layer in default_layer_types for layer in target_layer_types)
if not is_valid:
raise ValueError(f"values in target layer types is not valid, should be one of {default_layer_types}")
target_layer_types = ["self_attn." + target_layer_type for target_layer_type in target_layer_types]
elif block_type == "mlp":
default_layer_types = ["down_proj", "up_proj", "gate_proj"] # by default
if not target_layer_types:
target_layer_types = default_layer_types
else:
is_valid = all(layer in default_layer_types for layer in target_layer_types)
if not is_valid:
raise ValueError(f"values in target layer types is not valid, should be one of {default_layer_types}")
target_layer_types = ["mlp." + target_layer_type for target_layer_type in target_layer_types]
else:
raise NotImplementedError(f"block type {block_type} not support")
base_layer_name = f"model.layers.{layer_id}."
target_layer_names = [base_layer_name + target_layer_type for target_layer_type in target_layer_types]
if allocation_aware:
compression_ratio_list = []
for target_layer in target_layer_names:
module = self.model.get_submodule(target_layer)
if isinstance(module, nn.Linear):
compression_ratio = getattr(module, "compression_ratio", None)
if compression_ratio is not None:
if isinstance(compression_ratio, torch.Tensor):
compression_ratio = compression_ratio.cpu().item()
else:
compression_ratio = None
compression_ratio_list.append(compression_ratio)
if compression_ratio == 0:
continue
else:
self.replace_with_GRASPLayer(target_layer=target_layer, device=device)
if np.all(np.array(compression_ratio_list) == 0):
return True
else:
for target_layer in target_layer_names:
self.replace_with_GRASPLayer(target_layer=target_layer, device=device)
return
def compute_preserve_rank(self, grasp_layer: GRASPLayer, compression_ratio: float):
if compression_ratio is None:
raise ValueError("Compression ratio should not be None")
in_features = grasp_layer.in_features
out_features = grasp_layer.out_features
k = int(in_features * out_features * (1 - compression_ratio) / (in_features + out_features))
return k
def check_exists_grasp_layer(self, log_file: Optional[str] = None):
setup_logger(log_file=log_file)
grasp_layer_names = []
for name, module in self.model.named_modules():
if isinstance(module, GRASPLayer):
grasp_layer_names.append(name)
continue
if not grasp_layer_names:
logger.info("GRASPLayer not found in current model, please use GRASPBaseModel.replace_with_GRASPLayer first")
return grasp_layer_names
def get_svdlayer_gradients(self, calibration_dataloader: DataLoader, device: Literal["cuda:0", "cpu"] = "cuda:0", log_file: Optional[str] = None, *args, **kwargs):
setup_logger(log_file=log_file)
grasp_layer_names = self.check_exists_grasp_layer()
if grasp_layer_names is None:
raise NotImplementedError("GRASPLayer not found, can not compute gradients, please use GRASPBaseModel.replace_with_GRASPLayer first")
iterator = tqdm(calibration_dataloader, desc="Gradients Collection", total=len(calibration_dataloader), leave=True)
grasp_layer_grads = {}
self.model.to(device=device)
for batch_idx, batch in enumerate(iterator):
if len(batch) == 2:
attention_mask = None
else:
attention_mask = batch["attention_mask"].to(device=device)
input_ids = batch["input_ids"].to(device=device)
labels = batch["labels"].to(device=device)
outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, labels=labels, use_cache=False)
loss = outputs[0]
# clear gradients cache
self.model.zero_grad()
# backpropogation
loss.backward()
for grasp_layer_name in grasp_layer_names:
module: GRASPLayer = self.model.get_submodule(grasp_layer_name)
if not module:
raise ValueError("module can not found")
if grasp_layer_name not in grasp_layer_grads:
grasp_layer_grads[grasp_layer_name] = module.S.grad
else:
grasp_layer_grads[grasp_layer_name] += module.S.grad
if "cuda" in device:
torch.cuda.empty_cache()
self.grasp_layer_grads = grasp_layer_grads
return grasp_layer_grads
def dynamic_svd_selection(
self,
grasp_layer_grads: dict,
metric: Literal["gradient", "taylor"] = "taylor",
compression_ratio: Optional[float] = None,
threshold_ratio: Optional[float] = None,
verbose: Optional[bool] = False,
log_file: Optional[str] = None
):
setup_logger(log_file=log_file)
if not grasp_layer_grads:
grasp_layer_grads = self.grasp_layer_grads
raise ValueError("gradients of grasp_layer should be given, but got None")
indices_dict = {}
for grasp_layer_name, grasp_layer_grad in grasp_layer_grads.items():
grasp_layer: GRASPLayer = self.model.get_submodule(grasp_layer_name)
S = grasp_layer.S
if metric == "gradient":
svd_importance: torch.Tensor = torch.abs(grasp_layer_grad)
elif metric == "taylor":
svd_importance: torch.Tensor = torch.abs(grasp_layer_grad * S)
else:
raise RuntimeError(f"{metric} not support")
if grasp_layer.compression_ratio is not None:
compression_ratio = grasp_layer.compression_ratio
if compression_ratio is not None:
k = self.compute_preserve_rank(grasp_layer, compression_ratio=compression_ratio)
_, indices = torch.topk(svd_importance, k=k)
else:
assert threshold_ratio, "Please provide Taylor threshold to select rank adaptively"
indices = adaptive_rank_selection(svd_importance_list=svd_importance, target_ratio=threshold_ratio)
indices_dict[grasp_layer_name] = indices
self.grasp_values_dict[grasp_layer_name] = {}
self.grasp_values_dict[grasp_layer_name]["svd_importance"] = torch.round(svd_importance.cpu(), decimals=3).tolist()
self.grasp_values_dict[grasp_layer_name]["svd_value"] = torch.round(S.data.cpu(), decimals=3).tolist()
if verbose:
logger.info("+" * 100)
for grasp_layer_name, indices in indices_dict.items():
logger.info(f"{grasp_layer_name}")
logger.info(indices.detach().cpu().numpy().tolist()[:128])
logger.info("+" * 100)
self.indices_dict = indices_dict
return indices_dict
def compile_grasp_model(
self,
indices_dict: Optional[dict] = None,
merge: Optional[bool] = False,
sigma_fuse: Literal["UV", "U", "V"] = "UV",
device: Literal["cpu", "cuda"] = "cuda",
log_file: Optional[str] = None
):
setup_logger(log_file=log_file)
if indices_dict is None:
indices_dict = self.indices_dict
rank_dict = {}
for grasp_layer_name, indices in indices_dict.items():
grasp_layer: GRASPLayer = self.model.get_submodule(grasp_layer_name)
S = grasp_layer.S[indices]
U = grasp_layer.U[:, indices]
Vh = grasp_layer.Vh[indices, :]
bias = grasp_layer.bias
rank_dict[grasp_layer_name] = S.shape[0]
if merge:
in_features = Vh.shape[1]
out_features = U.shape[0]
self._set_module(self.model, grasp_layer_name, nn.Linear(in_features=in_features, out_features=out_features, bias=True if bias is not None else False))
linear_layer: nn.Linear = self.model.get_submodule(grasp_layer_name)
# re-initialize linear weight and bias
W_compressed = torch.mm(U, torch.mm(torch.diag(S), Vh))
linear_layer.weight.data = W_compressed
if bias is not None:
linear_layer.bias = bias
linear_layer.requires_grad_(False)
else:
self._set_module(self.model, grasp_layer_name, SVDLinear(U=U, S=S, Vh=Vh, bias=bias, sigma_fuse=sigma_fuse))
svd_linear_layer: SVDLinear = self.model.get_submodule(grasp_layer_name)
svd_linear_layer.requires_grad_(False)
del grasp_layer
if "cuda" in device:
torch.cuda.empty_cache()
return
class GRASPModel(PreTrainedModel):
config_class = AutoConfig
def __init__(self, config, grasp_base_model: GRASPBaseModel, *args, **kwargs):
super().__init__(config, *args, **kwargs)
self.model = grasp_base_model.model
self.config = config
def forward(self, *args, **kwargs):
return self.model(*args, **kwargs)