-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_transformer.py
More file actions
355 lines (221 loc) · 8.6 KB
/
binary_transformer.py
File metadata and controls
355 lines (221 loc) · 8.6 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
#!/usr/bin/env python3
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
# --------------------------------------------------
# Straight Through Estimator
# --------------------------------------------------
class BinarySign(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
return x.sign()
@staticmethod
def backward(ctx, grad_output):
return grad_output
def binarize_weight(W):
# FIX 2: per-output-channel (row-wise) alpha instead of a single scalar.
# Shape: (out_features, 1) for linear; keepdim handles conv too.
alpha = W.abs().mean(dim=tuple(range(1, W.dim())), keepdim=True)
B = BinarySign.apply(W)
return alpha * B
# --------------------------------------------------
# Binary Linear Layer
# --------------------------------------------------
class BinaryLinear(nn.Module):
def __init__(self, linear):
super().__init__()
self.weight_real = nn.Parameter(linear.weight.data.clone())
if linear.bias is not None:
self.bias = nn.Parameter(linear.bias.data.clone())
else:
self.bias = None
def forward(self, x):
W = binarize_weight(self.weight_real)
return F.linear(x, W, self.bias)
# --------------------------------------------------
# Binary Conv Layer
# --------------------------------------------------
class BinaryConv2d(nn.Module):
def __init__(self, conv):
super().__init__()
self.weight_real = nn.Parameter(conv.weight.data.clone())
if conv.bias is not None:
self.bias = nn.Parameter(conv.bias.data.clone())
else:
self.bias = None
self.stride = conv.stride
self.padding = conv.padding
self.dilation = conv.dilation
self.groups = conv.groups
def forward(self, x):
W = binarize_weight(self.weight_real)
return F.conv2d(
x,
W,
self.bias,
self.stride,
self.padding,
self.dilation,
self.groups
)
# --------------------------------------------------
# Convert model recursively
# --------------------------------------------------
def convert_model(module):
for name, child in module.named_children():
if isinstance(child, nn.Linear):
setattr(module, name, BinaryLinear(child))
elif isinstance(child, nn.Conv2d):
setattr(module, name, BinaryConv2d(child))
else:
convert_model(child)
return module
# --------------------------------------------------
# Bit packing utilities
# --------------------------------------------------
def pack_binary_tensor(tensor):
arr = tensor.detach().cpu().numpy()
# FIX 5: use != -1 (or equivalently >= 0) so that the zero-weight edge
# case (sign() returns 0, which is neither +1 nor -1) maps consistently
# to the negative class rather than silently splitting across the threshold.
arr = (arr != -1).astype(np.uint8)
flat = arr.flatten()
packed = np.packbits(flat)
return packed
def export_binary_model(model, path):
data = {}
for name, param in model.named_parameters():
if "weight_real" in name:
# Per-channel alphas: one scalar per output channel.
alpha = param.abs().mean(dim=tuple(range(1, param.dim())))
B = param.sign()
packed = pack_binary_tensor(B)
data[name] = {
# Store as numpy array so every channel's scale is preserved.
"alpha": alpha.detach().cpu().numpy(),
"bits": packed,
"shape": list(param.shape),
}
torch.save(data, path)
# --------------------------------------------------
# Binary Attention (FIX 1: proper multi-head split)
# --------------------------------------------------
class BinaryAttention(nn.Module):
def __init__(self, dim, heads):
super().__init__()
assert dim % heads == 0, "dim must be divisible by heads"
self.heads = heads
self.head_dim = dim // heads
self.scale = self.head_dim ** -0.5
self.q = BinaryLinear(nn.Linear(dim, dim))
self.k = BinaryLinear(nn.Linear(dim, dim))
self.v = BinaryLinear(nn.Linear(dim, dim))
self.proj = BinaryLinear(nn.Linear(dim, dim))
def forward(self, x):
B, T, C = x.shape
def split_heads(t):
# (B, T, C) -> (B, heads, T, head_dim)
return t.view(B, T, self.heads, self.head_dim).transpose(1, 2)
q = split_heads(self.q(x))
k = split_heads(self.k(x))
v = split_heads(self.v(x))
att = (q @ k.transpose(-2, -1)) * self.scale # (B, heads, T, T)
att = torch.softmax(att, dim=-1)
y = att @ v # (B, heads, T, head_dim)
# Merge heads: (B, heads, T, head_dim) -> (B, T, C)
y = y.transpose(1, 2).contiguous().view(B, T, C)
return self.proj(y)
# --------------------------------------------------
# Binary MLP
# --------------------------------------------------
class BinaryMLP(nn.Module):
def __init__(self, dim):
super().__init__()
self.fc1 = BinaryLinear(nn.Linear(dim, dim * 4))
self.fc2 = BinaryLinear(nn.Linear(dim * 4, dim))
def forward(self, x):
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
return x
# --------------------------------------------------
# Binary Transformer Block
# --------------------------------------------------
class BinaryTransformerBlock(nn.Module):
def __init__(self, dim, heads):
super().__init__()
self.norm1 = nn.LayerNorm(dim)
self.norm2 = nn.LayerNorm(dim)
self.attn = BinaryAttention(dim, heads)
self.mlp = BinaryMLP(dim)
def forward(self, x):
x = x + self.attn(self.norm1(x))
x = x + self.mlp(self.norm2(x))
return x
# --------------------------------------------------
# Binary Transformer (FIX 4: positional encoding added)
# --------------------------------------------------
class BinaryTransformer(nn.Module):
def __init__(self, vocab, dim=256, depth=6, heads=8, max_seq_len=512):
super().__init__()
self.embed = nn.Embedding(vocab, dim)
# Learned positional embeddings — keeps sequence order information.
self.pos_embed = nn.Embedding(max_seq_len, dim)
self.blocks = nn.ModuleList(
[BinaryTransformerBlock(dim, heads) for _ in range(depth)]
)
self.norm = nn.LayerNorm(dim)
self.head = nn.Linear(dim, vocab)
def forward(self, x):
B, T = x.shape
positions = torch.arange(T, device=x.device).unsqueeze(0) # (1, T)
x = self.embed(x) + self.pos_embed(positions)
for block in self.blocks:
x = block(x)
x = self.norm(x)
return self.head(x)
# --------------------------------------------------
# Example pipeline
# --------------------------------------------------
def main():
vocab = 10000
model = BinaryTransformer(
vocab=vocab,
dim=256,
depth=4,
heads=4,
max_seq_len=128,
)
print("Model architecture:")
print(model)
tokens = torch.randint(0, vocab, (2, 64))
logits = model(tokens)
print("Output shape:", logits.shape) # expect (2, 64, 10000)
export_binary_model(model, "binary_model.pt")
print("Binary weights exported")
# --------------------------------------------------
# Minimal training loop demo (FIX 6)
# --------------------------------------------------
# The real benefit of weight_real + binarize-in-forward is that latent
# weights accumulate small gradient updates while the forward pass stays
# fully binary. Without a training loop the binarization has no effect
# beyond compression.
print("\nRunning a quick training-loop sanity check …")
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
for step in range(3):
tokens = torch.randint(0, vocab, (2, 32))
# Shift targets by one for a simple next-token prediction loss.
logits = model(tokens[:, :-1]) # (B, T-1, vocab)
targets = tokens[:, 1:].contiguous() # (B, T-1)
loss = F.cross_entropy(
logits.view(-1, vocab),
targets.view(-1),
)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(f" step {step+1} loss={loss.item():.4f}")
print("Training loop OK — gradients flow through weight_real correctly.")
if __name__ == "__main__":
main()