-
Notifications
You must be signed in to change notification settings - Fork 1
/
key_code_sample.py
177 lines (154 loc) · 7.65 KB
/
key_code_sample.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
# Code adapted from https://github.com/microsoft/LoRA/blob/main/loralib/layers.py.
# ------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
# ------------------------------------------------------------------------------------------
import numpy as np
import torch
from src.transformers.adapters.configuration import LoRAConfig
from src.transformers.adapters.tensor_buffer import TensorBuffer
# ------------------------------------------------------------------------------------------
# The original CapaBoost idea should use one weight and several different masks. For convenience,
# here we create an equivalent network with several tied-layers with same weights and different
# masks.
#
# After initialization, all tied-layers have same weights and execute forward and backward pass
# normally. After that, aggregate_grads_for_tied_weights will gather and sum up gradient of all
# tied-layers and assign sum-up values back. Then, all tied-layers will execute gradient descent
# and update values. All tied-layers will maintain same values all the time.
# ------------------------------------------------------------------------------------------
# only used in initialization
def init_masks_for_tied_weights(
self, adapter_name, mask_density, rng_state=np.random.RandomState(0), sparsity_type='equal_per_layer',
): # net is a weight-tied block
def _get_masks(layer):
if sparsity_type == "equal_per_layer":
mask = torch.zeros_like(layer.weight.view(-1))
mask_length = len(mask)
num_params_to_keep_per_layer = int(mask_length * mask_density)
selected_keep_pos = rng_state.choice(
np.arange(mask_length), num_params_to_keep_per_layer, replace=False
)
mask[selected_keep_pos] = 1
return mask.view(layer.weight.size())
# support NVIDIA sparse tensor core: N:M sparsity
elif sparsity_type == "NM_structured":
N, M = 2, 4
grads_abs = torch.randn(layer.weight.shape)
group = int(grads_abs.numel()/M)
weight_temp = grads_abs.detach().reshape(group, M)
index = torch.argsort(weight_temp, dim=1)[:, :int(M-N)]
w_b = torch.ones(weight_temp.shape, device=weight_temp.device)
w_b = w_b.scatter_(dim=1, index=index, value=0).reshape(grads_abs.shape)
mask = (
w_b >= 1e-10
).float()
return mask.view(layer.weight.size())
else:
raise NotImplementedError
lora_config = self.config.adapters.match(
adapter_name,
config_type=LoRAConfig,
layer_idx=self.layer_idx,
location_key=self.location_key,
)
if adapter_name in self.loras:
self.lora_A_masks[adapter_name] = []
self.lora_B_masks[adapter_name] = []
for i in range(lora_config.num_layer):
self.lora_A_masks[adapter_name].append(_get_masks(self.loras[adapter_name].lora_A[i]))
self.lora_B_masks[adapter_name].append(_get_masks(self.loras[adapter_name].lora_B[i]))
def get_latest_dense_tied_weights(self, adapter_name):
if adapter_name in self.loras:
self.lora_A_dense_tied_weights[adapter_name] = TensorBuffer(
self.loras[adapter_name].lora_A[0].weight.data.clone()
).buffer
self.lora_B_dense_tied_weights[adapter_name] = TensorBuffer(
self.loras[adapter_name].lora_B[0].weight.data.clone()
).buffer
def restore_dense_tied_weights(self, adapter_name):
if adapter_name in self.loras:
lora_config = self.config.adapters.match(
adapter_name,
config_type=LoRAConfig,
layer_idx=self.layer_idx,
location_key=self.location_key,
)
for i in range(lora_config.num_layer):
device = self.loras[adapter_name].lora_A[0].weight.device
d_type = self.loras[adapter_name].lora_A[0].weight.dtype
self.loras[adapter_name].lora_A[i].weight.data = (self.lora_A_dense_tied_weights[adapter_name].clone()).reshape(self.loras[adapter_name].lora_A[i].weight.data.shape).to(d_type).to(device)
self.loras[adapter_name].lora_B[i].weight.data = (self.lora_B_dense_tied_weights[adapter_name].clone()).reshape(self.loras[adapter_name].lora_B[i].weight.data.shape).to(d_type).to(device)
def aggregate_grads_for_tied_weights(self, adapter_name, avg_tied_grads=False):
# extract grads.
if adapter_name in self.loras:
lora_config = self.config.adapters.match(
adapter_name,
config_type=LoRAConfig,
layer_idx=self.layer_idx,
location_key=self.location_key,
)
buffers = []
for i in range(lora_config.num_layer):
hp_grad = self.loras[adapter_name].lora_A[i].weight.grad.clone()
tb = TensorBuffer(
[hp_grad]
)
buffers.append(tb.buffer)
# aggregate grads.
aggregated_grads = (
sum(buffers) / len(buffers) if avg_tied_grads else sum(buffers)
)
# assign grads back (inplace).
for i in range(lora_config.num_layer):
grads = self.loras[adapter_name].lora_A[i].weight.grad
tb = TensorBuffer(grads)
tb.buffer = aggregated_grads.clone()
tb.unpack(grads)
#for B
buffers = []
for i in range(lora_config.num_layer):
hp_grad = self.loras[adapter_name].lora_B[i].weight.grad.clone()
tb = TensorBuffer(
[hp_grad]
)
buffers.append(tb.buffer)
# aggregate grads.
aggregated_grads = (
sum(buffers) / len(buffers) if avg_tied_grads else sum(buffers)
)
# assign grads back (inplace).
for i in range(lora_config.num_layer):
grads = self.loras[adapter_name].lora_B[i].weight.grad
tb = TensorBuffer(grads)
tb.buffer = aggregated_grads.clone()
tb.unpack(grads)
def apply_masks_to_grads_of_tied_weights(self, adapter_name):
if adapter_name in self.loras:
lora_config = self.config.adapters.match(
adapter_name,
config_type=LoRAConfig,
layer_idx=self.layer_idx,
location_key=self.location_key,
)
for i in range(lora_config.num_layer):
device = self.loras[adapter_name].lora_A[i].weight.device
mask = self.lora_A_masks[adapter_name][i].to(device)
if self.loras[adapter_name].lora_A[i].weight.grad is not None:
self.loras[adapter_name].lora_A[i].weight.grad.data.mul_(mask)
mask = self.lora_B_masks[adapter_name][i].to(device)
if self.loras[adapter_name].lora_B[i].weight.grad is not None:
self.loras[adapter_name].lora_B[i].weight.grad.data.mul_(mask)
def apply_masks_to_dense_tied_weights(self, adapter_name):
if adapter_name in self.loras:
lora_config = self.config.adapters.match(
adapter_name,
config_type=LoRAConfig,
layer_idx=self.layer_idx,
location_key=self.location_key,
)
for i in range(lora_config.num_layer):
mask = self.lora_A_masks[adapter_name][i].to(self.loras[adapter_name].lora_A[i].weight.device)
self.loras[adapter_name].lora_A[i].weight.data.mul_(mask)
mask = self.lora_B_masks[adapter_name][i].to(self.loras[adapter_name].lora_B[i].weight.device)
self.loras[adapter_name].lora_B[i].weight.data.mul_(mask)