-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastfood.py
238 lines (187 loc) · 7.89 KB
/
fastfood.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
from torch.utils.cpp_extension import load
fwh_cuda = load(name='fwh_cuda',
sources=['IntrinsicDimensions/id_fb_test/fwh_extension/fwh_cpp.cpp', 'IntrinsicDimensions/id_fb_test/fwh_extension/fwh_cu.cu'],
verbose=True)
import torch
from torch.nn import functional as F
from fwh_cuda import fast_walsh_hadamard_transform as fast_walsh_hadamard_transform_cuda
## Fastfood Wrapper
class FastfoodWrap(nn.Module):
def __init__(self, module, intrinsic_dimension, said=False, device=0):
"""
Wrapper to estimate the intrinsic dimensionality of the
objective landscape for a specific task given a specific model using FastFood transform
:param module: pytorch nn.Module
:param intrinsic_dimension: dimensionality within which we search for solution
:param device: cuda device id
"""
super(FastfoodWrap, self).__init__()
# Hide this from inspection by get_parameters()
self.m = [module]
self.name_base_localname = []
# Stores the initial value: \theta_{0}^{D}
self.initial_value = dict()
# Fastfood parameters
self.fastfood_params = {}
# SAID
self.said = said
self.said_size = len(list(module.named_parameters()))
if self.said:
assert intrinsic_dimension > self.said_size
intrinsic_dimension -= self.said_size
# Parameter vector that is updated
# Initialised with zeros as per text: \theta^{d}
intrinsic_parameter = nn.Parameter(torch.zeros((intrinsic_dimension)).to(device))
self.register_parameter("intrinsic_parameter", intrinsic_parameter)
v_size = (intrinsic_dimension,)
length = 0
# Iterate over layers in the module
for name, param in module.named_parameters():
# If param requires grad update
if param.requires_grad:
length += 1
# Saves the initial values of the initialised parameters from param.data and sets them to no grad.
# (initial values are the 'origin' of the search)
self.initial_value[name] = v0 = (
param.clone().detach().requires_grad_(False).to(device)
)
# Generate fastfood parameters
DD = np.prod(v0.size())
self.fastfood_params[name] = fastfood_vars(DD, device)
base, localname = module, name
while "." in localname:
prefix, localname = localname.split(".", 1)
base = base.__getattr__(prefix)
self.name_base_localname.append((name, base, localname))
if "intrinsic_parameter" not in name:
param.requires_grad_(False)
if said:
intrinsic_parameter_said = nn.Parameter(torch.ones((length)).to(device))
self.register_parameter("intrinsic_parameter_said", intrinsic_parameter_said)
# for name, base, localname in self.name_base_localname:
# delattr(base, localname)
def forward(self, x):
index = 0
# Iterate over layers
for name, base, localname in self.name_base_localname:
init_shape = self.initial_value[name].size()
DD = np.prod(init_shape)
# Fastfood transform te replace dence P
ray = fastfood_torched(self.intrinsic_parameter, DD, self.fastfood_params[name]).view(
init_shape
)
if self.said:
ray = ray * self.intrinsic_parameter_said[index]
param = self.initial_value[name] + ray
delattr(base, localname)
setattr(base, localname, param)
index += 1
# Pass through the model, by getting hte module from a list self.m
module = self.m[0]
x = module(x)
return x
class FastWalshHadamard(torch.autograd.Function):
@staticmethod
def forward(ctx, input):
ctx.save_for_backward(torch.tensor(
[1 / np.sqrt(float(input.size(0)))]).to(input))
if input.is_cuda:
return fast_walsh_hadamard_transform_cuda(input.float(), False)
else:
return fast_walsh_hadamard_torched(input.float(), normalize=False)
@staticmethod
def backward(ctx, grad_output):
input, = ctx.saved_tensors
if grad_output.is_cuda:
return input*fast_walsh_hadamard_transform_cuda(grad_output.clone().float(), False).to(grad_output)
else:
return input*fast_walsh_hadamard_torched(grad_output.clone().float(), normalize=False).to(grad_output)
def fast_walsh_hadamard_torched(x, axis=0, normalize=False):
"""
Performs fast Walsh Hadamard transform
:param x:
:param axis:
:param normalize:
:return:
"""
orig_shape = x.size()
assert axis >= 0 and axis < len(orig_shape), (
"For a vector of shape %s, axis must be in [0, %d] but it is %d"
% (orig_shape, len(orig_shape) - 1, axis)
)
h_dim = orig_shape[axis]
h_dim_exp = int(round(np.log(h_dim) / np.log(2)))
assert h_dim == 2 ** h_dim_exp, (
"hadamard can only be computed over axis with size that is a power of two, but"
" chosen axis %d has size %d" % (axis, h_dim)
)
working_shape_pre = [int(np.prod(orig_shape[:axis]))] # prod of empty array is 1 :)
working_shape_post = [
int(np.prod(orig_shape[axis + 1 :]))
] # prod of empty array is 1 :)
working_shape_mid = [2] * h_dim_exp
working_shape = working_shape_pre + working_shape_mid + working_shape_post
ret = x.view(working_shape)
for ii in range(h_dim_exp):
dim = ii + 1
arrs = torch.chunk(ret, 2, dim=dim)
assert len(arrs) == 2
ret = torch.cat((arrs[0] + arrs[1], arrs[0] - arrs[1]), axis=dim)
if normalize:
ret = ret / torch.sqrt(float(h_dim))
ret = ret.view(orig_shape)
return ret
def fastfood_vars(DD, device=0):
"""
Returns parameters for fast food transform
:param DD: desired dimension
:return:
"""
ll = int(np.ceil(np.log(DD) / np.log(2)))
LL = 2 ** ll
# Binary scaling matrix where $B_{i,i} \in \{\pm 1 \}$ drawn iid
BB = torch.FloatTensor(LL).uniform_(0, 2).type(torch.LongTensor)
BB = (BB * 2 - 1).type(torch.FloatTensor).to(device)
BB.requires_grad = False
# Random permutation matrix
Pi = torch.LongTensor(np.random.permutation(LL)).to(device)
Pi.requires_grad = False
# Gaussian scaling matrix, whose elements $G_{i,i} \sim \mathcal{N}(0, 1)$
GG = torch.FloatTensor(LL,).normal_().to(device)
GG.requires_grad = False
# Hadamard Matrix
# HH = torch.tensor(hadamard(LL)).to(device)
# HH.requirez_grad = False
divisor = torch.sqrt(LL * torch.sum(torch.pow(GG, 2)))
# return [BB, Pi, GG, HH, divisor, LL]
return [BB, Pi, GG, divisor, LL]
def fastfood_torched(x, DD, param_list=None, device=0):
"""
Fastfood transform
:param x: array of dd dimension
:param DD: desired dimension
:return:
"""
dd = x.size(0)
if not param_list:
BB, Pi, GG, divisor, LL = fastfood_vars(DD, device=device)
else:
BB, Pi, GG, divisor, LL = param_list
# Padd x if needed
dd_pad = F.pad(x, pad=(0, LL - dd), value=0, mode="constant")
# From left to right HGPiH(BX), where H is Walsh-Hadamard matrix
mul_1 = torch.mul(BB, dd_pad)
# HGPi(HBX)
mul_2 = fast_walsh_hadamard_torched(mul_1, 0, normalize=False)
# mul2 = hadamard_torched_matmul(mul_1, 0, normalize=False)
# mul_2 = torch.mul(HH, mul_1)
# mul_2 = FastWalshHadamard.apply(mul_1)
# HG(PiHBX)
mul_3 = mul_2[Pi]
# H(GPiHBX)
mul_4 = torch.mul(mul_3, GG)
# (HGPiHBX)
# mul_5 = fast_walsh_hadamard_torched(mul_4, 0, normalize=False)
mul_5 = FastWalshHadamard.apply(mul_4)
ret = torch.div(mul_5[:DD], divisor * np.sqrt(float(DD) / LL))
return ret