-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearutils.py
More file actions
183 lines (158 loc) · 6.46 KB
/
linearutils.py
File metadata and controls
183 lines (158 loc) · 6.46 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
import torch
from quantutils import DeltaInteger, GroupFinite
from quiputils import RHT, quant_linear
import contextlib
import copy
class quantize_linearlayer_multimode(torch.nn.Module):
'''
Wrapper class to quantize linear layer. Contains original layer weights,
as well as "x" parameters which interpolate between the closest up and down
quantization grid for each weight.
self.grid abstracts the quantization grid, and exposes generic round_down and round_up
functions.
'''
@torch.no_grad()
def __init__(self, linear, args):
super().__init__()
self.mode = 'x' # 'x', 'rdx', 'original', 'greedy'
self.linear = linear
if args.quip:
self.S_in = torch.randint(0, 2, (linear.in_features,)) * 2 - 1
self.S_out = torch.randint(0, 2, (linear.out_features,)) * 2 - 1
self.S_in = self.S_in.to(linear.weight.device).to(linear.weight.dtype)
self.S_out = self.S_out.to(linear.weight.device).to(linear.weight.dtype)
self.linear.weight.data = RHT(self.linear.weight.data, self.S_in, self.S_out)
self.linear = torch.compile(linear)
self.grid = GroupFinite(
linear.weight, args.wbits, args.groupsize, args.cache_down, args.cache_up,
with_grad=False)
if args.init_x == 'rand':
init_val = torch.rand_like(self.linear.weight)
elif args.init_x == 'orig':
# init_val = self._gen_y().to(self.linear.weight.dtype).to(self.linear.weight.device).detach().clone()
init_val = self._gen_y().to(self.linear.weight.dtype).detach().clone()
else:
raise NotImplementedError
self.x = torch.nn.Parameter(init_val)
self.quant_args = args
y = self._gen_y()
assert torch.all(0 - 1e-6 <= y) and torch.all(y <= 1 + 1e-6)
greedy = self._unquant(y, rd=True) - self.linear.weight
self.delta2 = 12 * greedy.square().sum() / y.numel()
self.grid._test_compression( self._unquant(self.x, rd=True) )
def _unquant(self, a, rd=False):
'''
entry-wise interpolation between self.grid.round_down and round_up.
'''
W = self.linear.weight
if rd:
a = torch.round(a)
return (1-a) * self.grid.round_down(W) + a * self.grid.round_up(W)
@torch.no_grad()
@torch.compile(dynamic=True)
def _gen_y(self):
'''
y is the interpolation between the closest down and up quantizatoin
gridpoints which gives the original weights
'''
W = self.linear.weight
up = self.grid.round_up(W)
down = self.grid.round_down(W)
y = (W - down) / (up - down)
y = torch.where(torch.isclose(up, down), 0.5, y)
return y
@torch.compile(dynamic=True)
def _d_sigmoid(self, a):
return torch.exp( -a.abs() ) / (1 + torch.exp( -a.abs() )).square()
@torch.no_grad()
@torch.compile(dynamic=True)
def _c_grad(self):
'''
gradient of linear term which encourages rounding
'''
W = self.linear.weight
delta2_i = 1
c = (2 * self._gen_y() - 1)
return delta2_i * c
@torch.no_grad()
def _projection_step(self):
self.x.data.clamp_(0, 1)
@torch.no_grad()
@torch.compile(dynamic=True)
def _num_rounded(self):
x_val = self.x
return torch.sum(
torch.isclose(
x_val,
torch.tensor(0.0, dtype=x_val.dtype, device=x_val.device),
atol=1e-2
) | torch.isclose(
x_val,
torch.tensor(1.0, dtype=x_val.dtype, device=x_val.device),
atol=1e-2)
).item()
def forward(self, input):
'''
Each quantized linear layer contains multiple modes, or multiple models.
"x" is the non-rounded version of our quantization parameter.
"rdx" rounds our quantization parameter.
"original" is the original weights.
"greedy" is the original weights greedily quantized.
'''
bias = 0
if self.linear.bias is not None: bias = self.linear.bias
if self.mode == 'x':
Wq = self._unquant(self.x, rd=False)
elif self.mode == 'rdx':
Wq = self._unquant(self.x, rd=True)
elif self.mode == 'original':
Wq = self.linear.weight
elif self.mode == 'greedy':
Wq = self._unquant(self._gen_y(), rd=True)
else:
raise ValueError('Invalid Mode')
# no_grad() if not x
ctx_mgr = contextlib.nullcontext() if self.mode=='x' else torch.no_grad()
with ctx_mgr:
if self.quant_args.quip:
return quant_linear(input, Wq, bias, self.S_in, self.S_out)
else:
return input @ Wq.T + bias
def set_mode(model,mode):
if mode not in ['x','rdx','original','greedy']:
raise ValueError('Invalid mode')
for module in model.modules():
#if isinstance(module,quantize_linearlayer_multimode):
if str(type(module))==str(quantize_linearlayer_multimode):
module.mode=mode
def mygetattr(obj, attr_path):
"""
Recursively get an attribute from an object using a dotted path.
:param obj: The base object to get attributes from.
:param attr_path: A string containing the dotted path of attribute names.
:param default: A default value to return if the attribute is not found.
:return: The value of the nested attribute, or the default if not found.
"""
attributes = attr_path.split('.')
for attr in attributes:
obj = getattr(obj, attr)
return obj
def mysetattr(obj, attr_path, value):
"""
Recursively set an attribute on an object using a dotted path.
:param obj: The base object to set attributes on.
:param attr_path: A string containing the dotted path of attribute names.
:param value: The value to set on the final attribute.
"""
attributes = attr_path.split('.')
for attr in attributes[:-1]:
# Retrieve the next level object
obj = getattr(obj, attr)
# Set the final attribute
setattr(obj, attributes[-1], value)
def quantize_model(model, quantlist, args):
for param in model.parameters():
param.requires_grad = False
for layer in model.model.layers:
for name in quantlist:
mysetattr(layer, name, quantize_linearlayer_multimode(mygetattr(layer, name), args))