File size: 8,763 Bytes
9d43dda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
PURE BINARY TRANSFORMER - BITS ALL THE WAY DOWN
- Vocab = 2 (0 and 1)
- Weights = binary (-1 or +1, stored as bits)
- Activations = binary where possible

Uses Straight-Through Estimator (STE) for gradients.
XNOR + popcount for matmul = insanely fast on hardware.
"""

import sys
import math
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
from collections import deque

DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Config for pure binary transformer
CONFIG = {
    "d": 256,       # must be divisible by heads
    "layers": 6,
    "heads": 8,
    "vocab": 2,     # 0 and 1
    "ctx": 2048,
}

LR = 1e-3
UPDATE_EVERY = 256
PRINT_EVERY = 50000

# ============== BINARY LAYERS ==============

class BinarySign(torch.autograd.Function):
    """Binarize to -1/+1 with straight-through estimator"""
    @staticmethod
    def forward(ctx, x):
        ctx.save_for_backward(x)
        return x.sign()
    
    @staticmethod
    def backward(ctx, grad_output):
        x, = ctx.saved_tensors
        # STE: pass gradient through if |x| <= 1
        grad_input = grad_output.clone()
        grad_input[x.abs() > 1] = 0
        return grad_input

def binarize(x):
    return BinarySign.apply(x)

class BinaryLinear(nn.Module):
    """Linear layer with binary weights (-1/+1)"""
    def __init__(self, in_features, out_features, bias=False):
        super().__init__()
        self.in_features = in_features
        self.out_features = out_features
        
        # Real-valued weights for training, binarized during forward
        self.weight = nn.Parameter(torch.randn(out_features, in_features) * 0.1)
        if bias:
            self.bias = nn.Parameter(torch.zeros(out_features))
        else:
            self.bias = None
    
    def forward(self, x):
        # Binarize weights to -1/+1
        binary_weight = binarize(self.weight)
        
        # Scale factor for better gradients (from XNOR-Net paper)
        # alpha = mean(|W|)
        alpha = self.weight.abs().mean()
        
        out = F.linear(x, binary_weight * alpha, self.bias)
        return out

class BinaryAttention(nn.Module):
    """Attention with binary QKV projections"""
    def __init__(self, d, h):
        super().__init__()
        self.h, self.dk = h, d // h
        self.q_proj = BinaryLinear(d, d)
        self.k_proj = BinaryLinear(d, d)
        self.v_proj = BinaryLinear(d, d)
        self.out_proj = BinaryLinear(d, d)
        
    def forward(self, x, mask=None):
        B, N, D = x.shape
        
        q = self.q_proj(x).view(B, N, self.h, self.dk).transpose(1, 2)
        k = self.k_proj(x).view(B, N, self.h, self.dk).transpose(1, 2)
        v = self.v_proj(x).view(B, N, self.h, self.dk).transpose(1, 2)
        
        # Standard attention (values stay real for now)
        att = (q @ k.transpose(-1, -2)) / math.sqrt(self.dk)
        if mask is not None:
            att = att + mask
        att = F.softmax(att, dim=-1)
        
        out = (att @ v).transpose(1, 2).reshape(B, N, D)
        return self.out_proj(out)

class BinaryMLP(nn.Module):
    """MLP with binary weights"""
    def __init__(self, d):
        super().__init__()
        self.fc1 = BinaryLinear(d, d * 4)
        self.fc2 = BinaryLinear(d * 4, d)
    
    def forward(self, x):
        # Binary weights, but ReLU activation (could binarize this too)
        x = F.gelu(self.fc1(x))
        return self.fc2(x)

class BinaryBlock(nn.Module):
    def __init__(self, d, h):
        super().__init__()
        self.ln1 = nn.LayerNorm(d)
        self.attn = BinaryAttention(d, h)
        self.ln2 = nn.LayerNorm(d)
        self.mlp = BinaryMLP(d)
        
    def forward(self, x, mask):
        x = x + self.attn(self.ln1(x), mask)
        return x + self.mlp(self.ln2(x))

class PureBinaryTransformer(nn.Module):
    """
    Transformer where:
    - Input vocab = 2 (bits)
    - All linear weights are binary (-1/+1)
    """
    def __init__(self, cfg):
        super().__init__()
        d, L, h = cfg["d"], cfg["layers"], cfg["heads"]
        
        # Embeddings stay real (only 2 of them anyway)
        self.emb = nn.Embedding(2, d)
        
        # Binary blocks
        self.blocks = nn.ModuleList([BinaryBlock(d, h) for _ in range(L)])
        
        self.ln = nn.LayerNorm(d)
        self.head = BinaryLinear(d, 2)  # Binary output projection too!
        
    def forward(self, x):
        B, N = x.shape
        mask = torch.triu(torch.ones(N, N, device=x.device), 1) * -1e9
        
        h = self.emb(x)
        for block in self.blocks:
            h = block(h, mask)
        
        return self.head(self.ln(h))
    
    def count_params(self):
        return sum(p.numel() for p in self.parameters())
    
    def count_binary_params(self):
        """Count params that are binarized"""
        count = 0
        for name, module in self.named_modules():
            if isinstance(module, BinaryLinear):
                count += module.weight.numel()
        return count

def byte_to_bits(byte_val):
    return [(byte_val >> (7 - i)) & 1 for i in range(8)]

class BinaryTrainer:
    def __init__(self, model, lr=LR):
        self.model = model.to(DEVICE)
        self.opt = torch.optim.AdamW(model.parameters(), lr=lr)
        self.ctx_size = CONFIG["ctx"]
        self.buffer = deque(maxlen=self.ctx_size + 1)
        
        self.bits_seen = 0
        self.bytes_seen = 0
        self.total_loss = 0.0
        self.updates = 0
        self.start_time = time.time()
        
    def ingest_byte(self, byte_val):
        bits = byte_to_bits(byte_val)
        for bit in bits:
            self.buffer.append(bit)
            self.bits_seen += 1
            
            if len(self.buffer) >= UPDATE_EVERY + 1 and self.bits_seen % UPDATE_EVERY == 0:
                self._update()
        
        self.bytes_seen += 1
        
        if self.bits_seen % PRINT_EVERY == 0:
            self._print_stats()
            
        if self.bytes_seen % 500000 == 0 and self.bytes_seen > 0:
            self._save()
    
    def _update(self):
        tokens = list(self.buffer)
        x = torch.tensor(tokens[:-1], device=DEVICE, dtype=torch.long).unsqueeze(0)
        y = torch.tensor(tokens[1:], device=DEVICE, dtype=torch.long).unsqueeze(0)
        
        self.model.train()
        logits = self.model(x)
        loss = F.cross_entropy(
            logits[:, -UPDATE_EVERY:].reshape(-1, 2),
            y[:, -UPDATE_EVERY:].reshape(-1)
        )
        
        self.opt.zero_grad()
        loss.backward()
        torch.nn.utils.clip_grad_norm_(self.model.parameters(), 1.0)
        self.opt.step()
        
        self.total_loss += loss.item()
        self.updates += 1
    
    def _print_stats(self):
        elapsed = time.time() - self.start_time
        bytes_per_sec = self.bytes_seen / elapsed if elapsed > 0 else 0
        avg_loss = self.total_loss / max(1, self.updates)
        
        entropy = avg_loss / math.log(2)
        compression = (1.0 - entropy) * 100
        
        print(f"[{elapsed:.0f}s] {self.bytes_seen/1000:.1f}KB | {bytes_per_sec/1000:.2f} KB/s | "
              f"loss={avg_loss:.4f} | entropy={entropy:.3f} | compression={compression:.1f}%", flush=True)
    
    def _save(self):
        avg_loss = self.total_loss / max(1, self.updates)
        kb = self.bytes_seen // 1000
        ckpt = {
            "model": self.model.state_dict(),
            "bits": self.bits_seen,
            "bytes": self.bytes_seen,
            "loss": avg_loss,
        }
        torch.save(ckpt, f"/workspace/purebit_ckpt_{kb}kb.pt")
        print(f"[SAVED] purebit_ckpt_{kb}kb.pt", flush=True)

def main():
    print(f"PURE BINARY TRANSFORMER - BITS ALL THE WAY DOWN", flush=True)
    print(f"Config: {CONFIG}", flush=True)
    print(f"Device: {DEVICE}", flush=True)
    
    model = PureBinaryTransformer(CONFIG)
    total_params = model.count_params()
    binary_params = model.count_binary_params()
    
    print(f"Total Parameters: {total_params:,} ({total_params/1e6:.2f}M)", flush=True)
    print(f"Binary Parameters: {binary_params:,} ({binary_params/total_params*100:.1f}%)", flush=True)
    print(f"Vocab: 2 (input bits)", flush=True)
    print(f"Weights: BINARY (-1/+1)", flush=True)
    print(f"", flush=True)
    print(f"🔥 BITS IN, BITS WEIGHTS, BITS OUT 🔥", flush=True)
    
    trainer = BinaryTrainer(model)
    
    print(f"Listening for bytes...", flush=True)
    
    while True:
        byte = sys.stdin.buffer.read(1)
        if not byte:
            break
        trainer.ingest_byte(byte[0])
    
    print(f"Done. {trainer.bytes_seen:,} bytes = {trainer.bits_seen:,} bits", flush=True)

if __name__ == "__main__":
    main()