Upload folder using huggingface_hub
Browse files- .gitattributes +1 -0
- __pycache__/circuit_llm.cpython-311.pyc +0 -0
- __pycache__/iron_eval.cpython-311.pyc +3 -0
- circuit_llm.py +606 -0
- stress_test.py +367 -0
- train_circuit_interface.py +306 -0
.gitattributes
CHANGED
|
@@ -34,3 +34,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
__pycache__/iron_eval.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
__pycache__/iron_eval.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
__pycache__/iron_eval.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
__pycache__/circuit_llm.cpython-311.pyc
ADDED
|
Binary file (30.9 kB). View file
|
|
|
__pycache__/iron_eval.cpython-311.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:889b91e75c83db93f3bb79eca5185dcc75309927c4b558944425b30365110603
|
| 3 |
+
size 274934
|
circuit_llm.py
ADDED
|
@@ -0,0 +1,606 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Circuit-Augmented LLM: Embedding threshold logic circuits into SmolLM2
|
| 3 |
+
======================================================================
|
| 4 |
+
|
| 5 |
+
Replaces/augments MLP layers with frozen threshold circuits for exact arithmetic.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
import torch.nn as nn
|
| 10 |
+
import torch.nn.functional as F
|
| 11 |
+
from typing import Dict, Optional, Tuple
|
| 12 |
+
from safetensors.torch import load_file
|
| 13 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 14 |
+
import warnings
|
| 15 |
+
warnings.filterwarnings('ignore')
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# =============================================================================
|
| 19 |
+
# HEAVISIDE WITH STRAIGHT-THROUGH ESTIMATOR
|
| 20 |
+
# =============================================================================
|
| 21 |
+
|
| 22 |
+
class HeavisideSTE(torch.autograd.Function):
|
| 23 |
+
"""Heaviside step function with straight-through estimator for backprop."""
|
| 24 |
+
|
| 25 |
+
@staticmethod
|
| 26 |
+
def forward(ctx, x):
|
| 27 |
+
return (x >= 0).float()
|
| 28 |
+
|
| 29 |
+
@staticmethod
|
| 30 |
+
def backward(ctx, grad_output):
|
| 31 |
+
# STE: pass gradient through unchanged
|
| 32 |
+
return grad_output
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def heaviside(x: torch.Tensor) -> torch.Tensor:
|
| 36 |
+
"""Heaviside step: 1 if x >= 0, else 0. Uses STE for training."""
|
| 37 |
+
return HeavisideSTE.apply(x)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# =============================================================================
|
| 41 |
+
# CIRCUIT EXECUTOR - Runs the frozen threshold circuits
|
| 42 |
+
# =============================================================================
|
| 43 |
+
|
| 44 |
+
class CircuitExecutor(nn.Module):
|
| 45 |
+
"""
|
| 46 |
+
Executes threshold logic circuits from the safetensors file.
|
| 47 |
+
All circuit weights are frozen - only interface layers train.
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
def __init__(self, circuit_path: str, device: str = 'cpu'):
|
| 51 |
+
super().__init__()
|
| 52 |
+
self.device = device
|
| 53 |
+
|
| 54 |
+
# Load all circuit tensors
|
| 55 |
+
raw_circuits = load_file(circuit_path)
|
| 56 |
+
|
| 57 |
+
# Store as frozen parameters (use underscores for valid param names)
|
| 58 |
+
self.circuits = {}
|
| 59 |
+
for k, v in raw_circuits.items():
|
| 60 |
+
safe_name = k.replace('.', '__')
|
| 61 |
+
self.register_buffer(safe_name, v.float().to(device))
|
| 62 |
+
self.circuits[k] = safe_name
|
| 63 |
+
|
| 64 |
+
def _get(self, name: str) -> torch.Tensor:
|
| 65 |
+
"""Get circuit tensor by original dotted name."""
|
| 66 |
+
return getattr(self, self.circuits[name])
|
| 67 |
+
|
| 68 |
+
# -------------------------------------------------------------------------
|
| 69 |
+
# Boolean Gates
|
| 70 |
+
# -------------------------------------------------------------------------
|
| 71 |
+
|
| 72 |
+
def eval_and(self, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
|
| 73 |
+
"""AND gate: output 1 iff both inputs are 1."""
|
| 74 |
+
inp = torch.stack([a, b], dim=-1)
|
| 75 |
+
w = self._get('boolean.and.weight')
|
| 76 |
+
bias = self._get('boolean.and.bias')
|
| 77 |
+
return heaviside(inp @ w + bias)
|
| 78 |
+
|
| 79 |
+
def eval_or(self, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
|
| 80 |
+
"""OR gate: output 1 if either input is 1."""
|
| 81 |
+
inp = torch.stack([a, b], dim=-1)
|
| 82 |
+
w = self._get('boolean.or.weight')
|
| 83 |
+
bias = self._get('boolean.or.bias')
|
| 84 |
+
return heaviside(inp @ w + bias)
|
| 85 |
+
|
| 86 |
+
def eval_xor(self, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
|
| 87 |
+
"""XOR gate: two-layer network (not linearly separable)."""
|
| 88 |
+
inp = torch.stack([a, b], dim=-1)
|
| 89 |
+
|
| 90 |
+
# Layer 1: OR and NAND neurons
|
| 91 |
+
w1_n1 = self._get('boolean.xor.layer1.neuron1.weight')
|
| 92 |
+
b1_n1 = self._get('boolean.xor.layer1.neuron1.bias')
|
| 93 |
+
w1_n2 = self._get('boolean.xor.layer1.neuron2.weight')
|
| 94 |
+
b1_n2 = self._get('boolean.xor.layer1.neuron2.bias')
|
| 95 |
+
|
| 96 |
+
h1 = heaviside(inp @ w1_n1 + b1_n1)
|
| 97 |
+
h2 = heaviside(inp @ w1_n2 + b1_n2)
|
| 98 |
+
hidden = torch.stack([h1, h2], dim=-1)
|
| 99 |
+
|
| 100 |
+
# Layer 2: AND of hidden
|
| 101 |
+
w2 = self._get('boolean.xor.layer2.weight')
|
| 102 |
+
b2 = self._get('boolean.xor.layer2.bias')
|
| 103 |
+
|
| 104 |
+
return heaviside(hidden @ w2 + b2)
|
| 105 |
+
|
| 106 |
+
# -------------------------------------------------------------------------
|
| 107 |
+
# Arithmetic: Full Adder
|
| 108 |
+
# -------------------------------------------------------------------------
|
| 109 |
+
|
| 110 |
+
def eval_full_adder(self, a: torch.Tensor, b: torch.Tensor,
|
| 111 |
+
cin: torch.Tensor, prefix: str) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 112 |
+
"""
|
| 113 |
+
Full adder: sum = a XOR b XOR cin, cout = (a AND b) OR (cin AND (a XOR b))
|
| 114 |
+
Returns (sum_bit, carry_out)
|
| 115 |
+
"""
|
| 116 |
+
inp_ab = torch.stack([a, b], dim=-1)
|
| 117 |
+
|
| 118 |
+
# HA1: a XOR b
|
| 119 |
+
w1_or = self._get(f'{prefix}.ha1.sum.layer1.or.weight')
|
| 120 |
+
b1_or = self._get(f'{prefix}.ha1.sum.layer1.or.bias')
|
| 121 |
+
w1_nand = self._get(f'{prefix}.ha1.sum.layer1.nand.weight')
|
| 122 |
+
b1_nand = self._get(f'{prefix}.ha1.sum.layer1.nand.bias')
|
| 123 |
+
w2 = self._get(f'{prefix}.ha1.sum.layer2.weight')
|
| 124 |
+
b2 = self._get(f'{prefix}.ha1.sum.layer2.bias')
|
| 125 |
+
|
| 126 |
+
h_or = heaviside(inp_ab @ w1_or + b1_or)
|
| 127 |
+
h_nand = heaviside(inp_ab @ w1_nand + b1_nand)
|
| 128 |
+
hidden = torch.stack([h_or, h_nand], dim=-1)
|
| 129 |
+
ha1_sum = heaviside(hidden @ w2 + b2)
|
| 130 |
+
|
| 131 |
+
# HA1 carry
|
| 132 |
+
w_c1 = self._get(f'{prefix}.ha1.carry.weight')
|
| 133 |
+
b_c1 = self._get(f'{prefix}.ha1.carry.bias')
|
| 134 |
+
ha1_carry = heaviside(inp_ab @ w_c1 + b_c1)
|
| 135 |
+
|
| 136 |
+
# HA2: ha1_sum XOR cin
|
| 137 |
+
inp_ha2 = torch.stack([ha1_sum, cin], dim=-1)
|
| 138 |
+
w1_or = self._get(f'{prefix}.ha2.sum.layer1.or.weight')
|
| 139 |
+
b1_or = self._get(f'{prefix}.ha2.sum.layer1.or.bias')
|
| 140 |
+
w1_nand = self._get(f'{prefix}.ha2.sum.layer1.nand.weight')
|
| 141 |
+
b1_nand = self._get(f'{prefix}.ha2.sum.layer1.nand.bias')
|
| 142 |
+
w2 = self._get(f'{prefix}.ha2.sum.layer2.weight')
|
| 143 |
+
b2 = self._get(f'{prefix}.ha2.sum.layer2.bias')
|
| 144 |
+
|
| 145 |
+
h_or = heaviside(inp_ha2 @ w1_or + b1_or)
|
| 146 |
+
h_nand = heaviside(inp_ha2 @ w1_nand + b1_nand)
|
| 147 |
+
hidden = torch.stack([h_or, h_nand], dim=-1)
|
| 148 |
+
ha2_sum = heaviside(hidden @ w2 + b2)
|
| 149 |
+
|
| 150 |
+
# HA2 carry
|
| 151 |
+
w_c2 = self._get(f'{prefix}.ha2.carry.weight')
|
| 152 |
+
b_c2 = self._get(f'{prefix}.ha2.carry.bias')
|
| 153 |
+
ha2_carry = heaviside(inp_ha2 @ w_c2 + b_c2)
|
| 154 |
+
|
| 155 |
+
# Carry out = ha1_carry OR ha2_carry
|
| 156 |
+
inp_cout = torch.stack([ha1_carry, ha2_carry], dim=-1)
|
| 157 |
+
w_or = self._get(f'{prefix}.carry_or.weight')
|
| 158 |
+
b_or = self._get(f'{prefix}.carry_or.bias')
|
| 159 |
+
cout = heaviside(inp_cout @ w_or + b_or)
|
| 160 |
+
|
| 161 |
+
return ha2_sum, cout
|
| 162 |
+
|
| 163 |
+
# -------------------------------------------------------------------------
|
| 164 |
+
# Arithmetic: 8-bit Ripple Carry Adder
|
| 165 |
+
# -------------------------------------------------------------------------
|
| 166 |
+
|
| 167 |
+
def add_8bit(self, a_bits: torch.Tensor, b_bits: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 168 |
+
"""
|
| 169 |
+
8-bit ripple carry addition.
|
| 170 |
+
a_bits, b_bits: [..., 8] tensors of bits (LSB first)
|
| 171 |
+
Returns: (result_bits [..., 8], carry_out [...])
|
| 172 |
+
"""
|
| 173 |
+
batch_shape = a_bits.shape[:-1]
|
| 174 |
+
carry = torch.zeros(batch_shape, device=a_bits.device)
|
| 175 |
+
result_bits = []
|
| 176 |
+
|
| 177 |
+
for i in range(8):
|
| 178 |
+
a_i = a_bits[..., i]
|
| 179 |
+
b_i = b_bits[..., i]
|
| 180 |
+
sum_bit, carry = self.eval_full_adder(
|
| 181 |
+
a_i, b_i, carry,
|
| 182 |
+
f'arithmetic.ripplecarry8bit.fa{i}'
|
| 183 |
+
)
|
| 184 |
+
result_bits.append(sum_bit)
|
| 185 |
+
|
| 186 |
+
return torch.stack(result_bits, dim=-1), carry
|
| 187 |
+
|
| 188 |
+
# -------------------------------------------------------------------------
|
| 189 |
+
# Arithmetic: 8-bit Comparators
|
| 190 |
+
# -------------------------------------------------------------------------
|
| 191 |
+
|
| 192 |
+
def greater_than_8bit(self, a_bits: torch.Tensor, b_bits: torch.Tensor) -> torch.Tensor:
|
| 193 |
+
"""Returns 1 if a > b, else 0. Bits are MSB first."""
|
| 194 |
+
diff = a_bits - b_bits # [..., 8]
|
| 195 |
+
w = self._get('arithmetic.greaterthan8bit.comparator')
|
| 196 |
+
score = (diff * w).sum(dim=-1)
|
| 197 |
+
return (score > 0).float()
|
| 198 |
+
|
| 199 |
+
def less_than_8bit(self, a_bits: torch.Tensor, b_bits: torch.Tensor) -> torch.Tensor:
|
| 200 |
+
"""Returns 1 if a < b, else 0. Bits are MSB first."""
|
| 201 |
+
diff = b_bits - a_bits # [..., 8]
|
| 202 |
+
w = self._get('arithmetic.lessthan8bit.comparator')
|
| 203 |
+
score = (diff * w).sum(dim=-1)
|
| 204 |
+
return (score > 0).float()
|
| 205 |
+
|
| 206 |
+
def equal_8bit(self, a_bits: torch.Tensor, b_bits: torch.Tensor) -> torch.Tensor:
|
| 207 |
+
"""Returns 1 if a == b, else 0."""
|
| 208 |
+
gt = self.greater_than_8bit(a_bits, b_bits)
|
| 209 |
+
lt = self.less_than_8bit(a_bits, b_bits)
|
| 210 |
+
return (1 - gt) * (1 - lt)
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
# =============================================================================
|
| 214 |
+
# BIT EXTRACTION / INJECTION INTERFACES
|
| 215 |
+
# =============================================================================
|
| 216 |
+
|
| 217 |
+
class BitExtractor(nn.Module):
|
| 218 |
+
"""
|
| 219 |
+
Learns to extract 8-bit operands from token embeddings.
|
| 220 |
+
Maps embedding -> 16 bits (two 8-bit operands).
|
| 221 |
+
"""
|
| 222 |
+
|
| 223 |
+
def __init__(self, d_model: int):
|
| 224 |
+
super().__init__()
|
| 225 |
+
self.d_model = d_model
|
| 226 |
+
|
| 227 |
+
# Project to logits, then binarize
|
| 228 |
+
self.proj = nn.Linear(d_model, 16)
|
| 229 |
+
|
| 230 |
+
# Learnable temperature for sigmoid approximation during training
|
| 231 |
+
self.temperature = nn.Parameter(torch.tensor(1.0))
|
| 232 |
+
|
| 233 |
+
def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 234 |
+
"""
|
| 235 |
+
x: [..., d_model]
|
| 236 |
+
Returns: a_bits [..., 8], b_bits [..., 8] (LSB first for arithmetic)
|
| 237 |
+
"""
|
| 238 |
+
logits = self.proj(x) # [..., 16]
|
| 239 |
+
|
| 240 |
+
# Binarize with STE
|
| 241 |
+
bits = heaviside(logits)
|
| 242 |
+
|
| 243 |
+
# Split into two operands
|
| 244 |
+
a_bits = bits[..., :8]
|
| 245 |
+
b_bits = bits[..., 8:]
|
| 246 |
+
|
| 247 |
+
return a_bits, b_bits
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
class BitInjector(nn.Module):
|
| 251 |
+
"""
|
| 252 |
+
Learns to inject circuit results back into embedding space.
|
| 253 |
+
Maps 16 bits (result + flags) -> embedding delta.
|
| 254 |
+
"""
|
| 255 |
+
|
| 256 |
+
def __init__(self, d_model: int):
|
| 257 |
+
super().__init__()
|
| 258 |
+
self.d_model = d_model
|
| 259 |
+
|
| 260 |
+
# Project bits to embedding
|
| 261 |
+
self.proj = nn.Linear(16, d_model)
|
| 262 |
+
|
| 263 |
+
# Learnable scale
|
| 264 |
+
self.scale = nn.Parameter(torch.tensor(0.1))
|
| 265 |
+
|
| 266 |
+
def forward(self, result_bits: torch.Tensor, flags: torch.Tensor) -> torch.Tensor:
|
| 267 |
+
"""
|
| 268 |
+
result_bits: [..., 8]
|
| 269 |
+
flags: [..., 8] (carry, overflow, zero, negative, etc.)
|
| 270 |
+
Returns: [..., d_model]
|
| 271 |
+
"""
|
| 272 |
+
combined = torch.cat([result_bits, flags], dim=-1) # [..., 16]
|
| 273 |
+
return self.proj(combined) * self.scale
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
# =============================================================================
|
| 277 |
+
# CIRCUIT-AUGMENTED MLP BLOCK
|
| 278 |
+
# =============================================================================
|
| 279 |
+
|
| 280 |
+
class CircuitAugmentedMLP(nn.Module):
|
| 281 |
+
"""
|
| 282 |
+
MLP block augmented with frozen threshold circuits.
|
| 283 |
+
|
| 284 |
+
The original MLP path runs in parallel with the circuit path.
|
| 285 |
+
A learned router decides how much to use each.
|
| 286 |
+
"""
|
| 287 |
+
|
| 288 |
+
def __init__(
|
| 289 |
+
self,
|
| 290 |
+
d_model: int,
|
| 291 |
+
intermediate_size: int,
|
| 292 |
+
circuit_path: str,
|
| 293 |
+
device: str = 'cpu'
|
| 294 |
+
):
|
| 295 |
+
super().__init__()
|
| 296 |
+
self.d_model = d_model
|
| 297 |
+
|
| 298 |
+
# Original MLP components (will be loaded from pretrained)
|
| 299 |
+
self.gate_proj = nn.Linear(d_model, intermediate_size, bias=False)
|
| 300 |
+
self.up_proj = nn.Linear(d_model, intermediate_size, bias=False)
|
| 301 |
+
self.down_proj = nn.Linear(intermediate_size, d_model, bias=False)
|
| 302 |
+
self.act_fn = nn.SiLU()
|
| 303 |
+
|
| 304 |
+
# Circuit components
|
| 305 |
+
self.circuits = CircuitExecutor(circuit_path, device)
|
| 306 |
+
self.bit_extractor = BitExtractor(d_model)
|
| 307 |
+
self.bit_injector = BitInjector(d_model)
|
| 308 |
+
|
| 309 |
+
# Router: decides circuit vs MLP contribution
|
| 310 |
+
self.router = nn.Sequential(
|
| 311 |
+
nn.Linear(d_model, 64),
|
| 312 |
+
nn.ReLU(),
|
| 313 |
+
nn.Linear(64, 2),
|
| 314 |
+
nn.Softmax(dim=-1)
|
| 315 |
+
)
|
| 316 |
+
|
| 317 |
+
# Operation selector (which arithmetic op to perform)
|
| 318 |
+
self.op_selector = nn.Sequential(
|
| 319 |
+
nn.Linear(d_model, 32),
|
| 320 |
+
nn.ReLU(),
|
| 321 |
+
nn.Linear(32, 4), # add, sub, compare, passthrough
|
| 322 |
+
nn.Softmax(dim=-1)
|
| 323 |
+
)
|
| 324 |
+
|
| 325 |
+
def _compute_flags(self, result_bits: torch.Tensor, carry: torch.Tensor) -> torch.Tensor:
|
| 326 |
+
"""Compute status flags from result."""
|
| 327 |
+
batch_shape = result_bits.shape[:-1]
|
| 328 |
+
|
| 329 |
+
# Zero flag: all bits are 0
|
| 330 |
+
zero = (result_bits.sum(dim=-1) == 0).float()
|
| 331 |
+
|
| 332 |
+
# Negative flag: MSB is 1 (two's complement)
|
| 333 |
+
negative = result_bits[..., 7]
|
| 334 |
+
|
| 335 |
+
# Carry flag
|
| 336 |
+
carry_flag = carry
|
| 337 |
+
|
| 338 |
+
# Pad to 8 flags
|
| 339 |
+
flags = torch.zeros(*batch_shape, 8, device=result_bits.device)
|
| 340 |
+
flags[..., 0] = zero
|
| 341 |
+
flags[..., 1] = negative
|
| 342 |
+
flags[..., 2] = carry_flag
|
| 343 |
+
|
| 344 |
+
return flags
|
| 345 |
+
|
| 346 |
+
def _circuit_forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 347 |
+
"""Run input through threshold circuits."""
|
| 348 |
+
# Extract operands
|
| 349 |
+
a_bits, b_bits = self.bit_extractor(x)
|
| 350 |
+
|
| 351 |
+
# Get operation weights
|
| 352 |
+
op_weights = self.op_selector(x) # [..., 4]
|
| 353 |
+
|
| 354 |
+
# Compute addition
|
| 355 |
+
add_result, add_carry = self.circuits.add_8bit(a_bits, b_bits)
|
| 356 |
+
add_flags = self._compute_flags(add_result, add_carry)
|
| 357 |
+
|
| 358 |
+
# Compute subtraction (a + (~b) + 1, simplified: just use add for now)
|
| 359 |
+
# For MVP, we'll focus on addition
|
| 360 |
+
|
| 361 |
+
# Inject result back
|
| 362 |
+
circuit_delta = self.bit_injector(add_result, add_flags)
|
| 363 |
+
|
| 364 |
+
return circuit_delta
|
| 365 |
+
|
| 366 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 367 |
+
"""
|
| 368 |
+
x: [batch, seq_len, d_model]
|
| 369 |
+
Returns: [batch, seq_len, d_model]
|
| 370 |
+
"""
|
| 371 |
+
# Original MLP path
|
| 372 |
+
mlp_out = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
|
| 373 |
+
|
| 374 |
+
# Circuit path
|
| 375 |
+
circuit_out = self._circuit_forward(x)
|
| 376 |
+
|
| 377 |
+
# Route between paths
|
| 378 |
+
route_weights = self.router(x) # [..., 2]
|
| 379 |
+
mlp_weight = route_weights[..., 0:1]
|
| 380 |
+
circuit_weight = route_weights[..., 1:2]
|
| 381 |
+
|
| 382 |
+
# Combine: MLP output + weighted circuit contribution
|
| 383 |
+
output = mlp_out + circuit_weight * circuit_out
|
| 384 |
+
|
| 385 |
+
return output
|
| 386 |
+
|
| 387 |
+
|
| 388 |
+
# =============================================================================
|
| 389 |
+
# MODEL SURGERY: Insert circuits into SmolLM2
|
| 390 |
+
# =============================================================================
|
| 391 |
+
|
| 392 |
+
def augment_smollm2_with_circuits(
|
| 393 |
+
model: AutoModelForCausalLM,
|
| 394 |
+
circuit_path: str,
|
| 395 |
+
layer_indices: list = None,
|
| 396 |
+
device: str = 'cpu'
|
| 397 |
+
) -> AutoModelForCausalLM:
|
| 398 |
+
"""
|
| 399 |
+
Surgically insert circuit blocks into SmolLM2's MLP layers.
|
| 400 |
+
|
| 401 |
+
Args:
|
| 402 |
+
model: Pretrained SmolLM2 model
|
| 403 |
+
circuit_path: Path to neural_computer.safetensors
|
| 404 |
+
layer_indices: Which layers to augment (default: middle layers)
|
| 405 |
+
device: Device for circuit tensors
|
| 406 |
+
|
| 407 |
+
Returns:
|
| 408 |
+
Modified model with circuit-augmented MLPs
|
| 409 |
+
"""
|
| 410 |
+
config = model.config
|
| 411 |
+
num_layers = config.num_hidden_layers
|
| 412 |
+
|
| 413 |
+
# Default: augment middle third of layers
|
| 414 |
+
if layer_indices is None:
|
| 415 |
+
start = num_layers // 3
|
| 416 |
+
end = 2 * num_layers // 3
|
| 417 |
+
layer_indices = list(range(start, end))
|
| 418 |
+
|
| 419 |
+
print(f"Augmenting layers {layer_indices} with threshold circuits...")
|
| 420 |
+
|
| 421 |
+
for idx in layer_indices:
|
| 422 |
+
layer = model.model.layers[idx]
|
| 423 |
+
old_mlp = layer.mlp
|
| 424 |
+
|
| 425 |
+
# Create augmented MLP
|
| 426 |
+
new_mlp = CircuitAugmentedMLP(
|
| 427 |
+
d_model=config.hidden_size,
|
| 428 |
+
intermediate_size=config.intermediate_size,
|
| 429 |
+
circuit_path=circuit_path,
|
| 430 |
+
device=device
|
| 431 |
+
)
|
| 432 |
+
|
| 433 |
+
# Copy pretrained weights
|
| 434 |
+
new_mlp.gate_proj.weight.data = old_mlp.gate_proj.weight.data.clone()
|
| 435 |
+
new_mlp.up_proj.weight.data = old_mlp.up_proj.weight.data.clone()
|
| 436 |
+
new_mlp.down_proj.weight.data = old_mlp.down_proj.weight.data.clone()
|
| 437 |
+
|
| 438 |
+
# Replace
|
| 439 |
+
layer.mlp = new_mlp
|
| 440 |
+
|
| 441 |
+
# Freeze circuit weights, keep interfaces trainable
|
| 442 |
+
for name, param in model.named_parameters():
|
| 443 |
+
if 'circuits' in name:
|
| 444 |
+
param.requires_grad = False
|
| 445 |
+
|
| 446 |
+
print(f"Done. Circuit weights frozen, interfaces trainable.")
|
| 447 |
+
|
| 448 |
+
return model
|
| 449 |
+
|
| 450 |
+
|
| 451 |
+
# =============================================================================
|
| 452 |
+
# TRAINING UTILITIES
|
| 453 |
+
# =============================================================================
|
| 454 |
+
|
| 455 |
+
def generate_arithmetic_batch(batch_size: int, max_val: int = 255) -> Tuple[list, list]:
|
| 456 |
+
"""Generate batch of arithmetic problems and solutions."""
|
| 457 |
+
prompts = []
|
| 458 |
+
targets = []
|
| 459 |
+
|
| 460 |
+
for _ in range(batch_size):
|
| 461 |
+
a = torch.randint(0, max_val + 1, (1,)).item()
|
| 462 |
+
b = torch.randint(0, max_val + 1, (1,)).item()
|
| 463 |
+
result = (a + b) % 256
|
| 464 |
+
|
| 465 |
+
prompts.append(f"{a} + {b} =")
|
| 466 |
+
targets.append(f" {result}")
|
| 467 |
+
|
| 468 |
+
return prompts, targets
|
| 469 |
+
|
| 470 |
+
|
| 471 |
+
def evaluate_arithmetic(
|
| 472 |
+
model: AutoModelForCausalLM,
|
| 473 |
+
tokenizer: AutoTokenizer,
|
| 474 |
+
n_problems: int = 100,
|
| 475 |
+
device: str = 'cpu'
|
| 476 |
+
) -> dict:
|
| 477 |
+
"""Evaluate model on random arithmetic problems."""
|
| 478 |
+
correct = 0
|
| 479 |
+
total = 0
|
| 480 |
+
errors = []
|
| 481 |
+
|
| 482 |
+
model.eval()
|
| 483 |
+
|
| 484 |
+
for _ in range(n_problems):
|
| 485 |
+
a = torch.randint(0, 256, (1,)).item()
|
| 486 |
+
b = torch.randint(0, 256, (1,)).item()
|
| 487 |
+
expected = (a + b) % 256
|
| 488 |
+
|
| 489 |
+
prompt = f"{a} + {b} ="
|
| 490 |
+
inputs = tokenizer(prompt, return_tensors='pt').to(device)
|
| 491 |
+
|
| 492 |
+
with torch.no_grad():
|
| 493 |
+
outputs = model.generate(
|
| 494 |
+
**inputs,
|
| 495 |
+
max_new_tokens=10,
|
| 496 |
+
do_sample=False,
|
| 497 |
+
pad_token_id=tokenizer.eos_token_id
|
| 498 |
+
)
|
| 499 |
+
|
| 500 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 501 |
+
|
| 502 |
+
# Extract number from response
|
| 503 |
+
try:
|
| 504 |
+
# Find the part after "="
|
| 505 |
+
answer_part = response.split('=')[-1].strip()
|
| 506 |
+
# Extract first number
|
| 507 |
+
predicted = int(''.join(c for c in answer_part.split()[0] if c.isdigit()))
|
| 508 |
+
|
| 509 |
+
if predicted == expected:
|
| 510 |
+
correct += 1
|
| 511 |
+
else:
|
| 512 |
+
errors.append((a, b, expected, predicted))
|
| 513 |
+
except:
|
| 514 |
+
errors.append((a, b, expected, "parse_error"))
|
| 515 |
+
|
| 516 |
+
total += 1
|
| 517 |
+
|
| 518 |
+
return {
|
| 519 |
+
'accuracy': correct / total,
|
| 520 |
+
'correct': correct,
|
| 521 |
+
'total': total,
|
| 522 |
+
'errors': errors[:10] # First 10 errors
|
| 523 |
+
}
|
| 524 |
+
|
| 525 |
+
|
| 526 |
+
# =============================================================================
|
| 527 |
+
# MAIN: Demo
|
| 528 |
+
# =============================================================================
|
| 529 |
+
|
| 530 |
+
if __name__ == "__main__":
|
| 531 |
+
import argparse
|
| 532 |
+
|
| 533 |
+
parser = argparse.ArgumentParser(description='Circuit-Augmented LLM Demo')
|
| 534 |
+
parser.add_argument('--circuit-path', type=str,
|
| 535 |
+
default='./neural_computer.safetensors',
|
| 536 |
+
help='Path to circuit weights')
|
| 537 |
+
parser.add_argument('--device', type=str, default='cpu',
|
| 538 |
+
help='Device (cpu or cuda)')
|
| 539 |
+
parser.add_argument('--eval-only', action='store_true',
|
| 540 |
+
help='Only evaluate, do not augment')
|
| 541 |
+
args = parser.parse_args()
|
| 542 |
+
|
| 543 |
+
print("=" * 70)
|
| 544 |
+
print(" CIRCUIT-AUGMENTED LLM")
|
| 545 |
+
print("=" * 70)
|
| 546 |
+
|
| 547 |
+
# Load tokenizer and model
|
| 548 |
+
print("\n[1] Loading SmolLM2-360M...")
|
| 549 |
+
model_id = "HuggingFaceTB/SmolLM2-360M"
|
| 550 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 551 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32)
|
| 552 |
+
|
| 553 |
+
print(f" Parameters: {sum(p.numel() for p in model.parameters()):,}")
|
| 554 |
+
|
| 555 |
+
# Baseline evaluation
|
| 556 |
+
print("\n[2] Baseline arithmetic evaluation...")
|
| 557 |
+
baseline = evaluate_arithmetic(model, tokenizer, n_problems=50, device=args.device)
|
| 558 |
+
print(f" Accuracy: {baseline['accuracy']*100:.1f}% ({baseline['correct']}/{baseline['total']})")
|
| 559 |
+
if baseline['errors']:
|
| 560 |
+
print(f" Sample errors:")
|
| 561 |
+
for a, b, exp, got in baseline['errors'][:5]:
|
| 562 |
+
print(f" {a} + {b} = {exp}, model said {got}")
|
| 563 |
+
|
| 564 |
+
if args.eval_only:
|
| 565 |
+
print("\nDone (eval only mode).")
|
| 566 |
+
exit(0)
|
| 567 |
+
|
| 568 |
+
# Augment with circuits
|
| 569 |
+
print(f"\n[3] Augmenting with threshold circuits...")
|
| 570 |
+
print(f" Circuit path: {args.circuit_path}")
|
| 571 |
+
model = augment_smollm2_with_circuits(
|
| 572 |
+
model,
|
| 573 |
+
args.circuit_path,
|
| 574 |
+
device=args.device
|
| 575 |
+
)
|
| 576 |
+
|
| 577 |
+
new_params = sum(p.numel() for p in model.parameters())
|
| 578 |
+
trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
| 579 |
+
print(f" Total parameters: {new_params:,}")
|
| 580 |
+
print(f" Trainable parameters: {trainable:,}")
|
| 581 |
+
|
| 582 |
+
# Test circuit execution directly
|
| 583 |
+
print("\n[4] Testing circuit execution...")
|
| 584 |
+
circuit_exec = CircuitExecutor(args.circuit_path, args.device)
|
| 585 |
+
|
| 586 |
+
test_cases = [(127, 128), (255, 1), (0, 0), (100, 55)]
|
| 587 |
+
for a, b in test_cases:
|
| 588 |
+
# Convert to bits (LSB first)
|
| 589 |
+
a_bits = torch.tensor([(a >> i) & 1 for i in range(8)], dtype=torch.float32)
|
| 590 |
+
b_bits = torch.tensor([(b >> i) & 1 for i in range(8)], dtype=torch.float32)
|
| 591 |
+
|
| 592 |
+
result_bits, carry = circuit_exec.add_8bit(
|
| 593 |
+
a_bits.unsqueeze(0),
|
| 594 |
+
b_bits.unsqueeze(0)
|
| 595 |
+
)
|
| 596 |
+
|
| 597 |
+
# Convert result bits back to int
|
| 598 |
+
result = sum(int(result_bits[0, i].item()) * (2**i) for i in range(8))
|
| 599 |
+
expected = (a + b) % 256
|
| 600 |
+
|
| 601 |
+
status = "OK" if result == expected else "FAIL"
|
| 602 |
+
print(f" {a} + {b} = {result} (expected {expected}) [{status}]")
|
| 603 |
+
|
| 604 |
+
print("\n[5] Model ready for fine-tuning.")
|
| 605 |
+
print(" Next: Train interface layers on arithmetic examples.")
|
| 606 |
+
print("=" * 70)
|
stress_test.py
ADDED
|
@@ -0,0 +1,367 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
WILD STRESS TESTS - Push the threshold CPU to its limits
|
| 3 |
+
"""
|
| 4 |
+
import torch
|
| 5 |
+
from safetensors.torch import load_file
|
| 6 |
+
|
| 7 |
+
model = load_file('./neural_computer.safetensors')
|
| 8 |
+
model = {k: v.float() for k, v in model.items()}
|
| 9 |
+
|
| 10 |
+
def heaviside(x):
|
| 11 |
+
return (x >= 0).float()
|
| 12 |
+
|
| 13 |
+
def int_to_bits(val, width=8):
|
| 14 |
+
return torch.tensor([(val >> (width-1-i)) & 1 for i in range(width)], dtype=torch.float32)
|
| 15 |
+
|
| 16 |
+
def bits_to_int(bits):
|
| 17 |
+
val = 0
|
| 18 |
+
for i, b in enumerate(bits):
|
| 19 |
+
val |= (int(b.item()) << (len(bits)-1-i))
|
| 20 |
+
return val
|
| 21 |
+
|
| 22 |
+
# === BASIC PRIMITIVES ===
|
| 23 |
+
|
| 24 |
+
def eval_xor(a, b):
|
| 25 |
+
inp = torch.tensor([float(a), float(b)], dtype=torch.float32)
|
| 26 |
+
w1_n1 = model['boolean.xor.layer1.neuron1.weight']
|
| 27 |
+
b1_n1 = model['boolean.xor.layer1.neuron1.bias']
|
| 28 |
+
w1_n2 = model['boolean.xor.layer1.neuron2.weight']
|
| 29 |
+
b1_n2 = model['boolean.xor.layer1.neuron2.bias']
|
| 30 |
+
w2 = model['boolean.xor.layer2.weight']
|
| 31 |
+
b2 = model['boolean.xor.layer2.bias']
|
| 32 |
+
h1 = heaviside(inp @ w1_n1 + b1_n1)
|
| 33 |
+
h2 = heaviside(inp @ w1_n2 + b1_n2)
|
| 34 |
+
hidden = torch.tensor([h1.item(), h2.item()])
|
| 35 |
+
return int(heaviside(hidden @ w2 + b2).item())
|
| 36 |
+
|
| 37 |
+
def eval_and(a, b):
|
| 38 |
+
inp = torch.tensor([float(a), float(b)], dtype=torch.float32)
|
| 39 |
+
return int(heaviside(inp @ model['boolean.and.weight'] + model['boolean.and.bias']).item())
|
| 40 |
+
|
| 41 |
+
def eval_or(a, b):
|
| 42 |
+
inp = torch.tensor([float(a), float(b)], dtype=torch.float32)
|
| 43 |
+
return int(heaviside(inp @ model['boolean.or.weight'] + model['boolean.or.bias']).item())
|
| 44 |
+
|
| 45 |
+
def eval_not(a):
|
| 46 |
+
inp = torch.tensor([float(a)], dtype=torch.float32)
|
| 47 |
+
return int(heaviside(inp @ model['boolean.not.weight'] + model['boolean.not.bias']).item())
|
| 48 |
+
|
| 49 |
+
def eval_xor_arith(inp, prefix):
|
| 50 |
+
w1_or = model[f'{prefix}.layer1.or.weight']
|
| 51 |
+
b1_or = model[f'{prefix}.layer1.or.bias']
|
| 52 |
+
w1_nand = model[f'{prefix}.layer1.nand.weight']
|
| 53 |
+
b1_nand = model[f'{prefix}.layer1.nand.bias']
|
| 54 |
+
w2 = model[f'{prefix}.layer2.weight']
|
| 55 |
+
b2 = model[f'{prefix}.layer2.bias']
|
| 56 |
+
h_or = heaviside(inp @ w1_or + b1_or)
|
| 57 |
+
h_nand = heaviside(inp @ w1_nand + b1_nand)
|
| 58 |
+
hidden = torch.tensor([h_or.item(), h_nand.item()])
|
| 59 |
+
return heaviside(hidden @ w2 + b2).item()
|
| 60 |
+
|
| 61 |
+
def eval_full_adder(a, b, cin, prefix):
|
| 62 |
+
inp_ab = torch.tensor([a, b], dtype=torch.float32)
|
| 63 |
+
ha1_sum = eval_xor_arith(inp_ab, f'{prefix}.ha1.sum')
|
| 64 |
+
ha1_carry = heaviside(inp_ab @ model[f'{prefix}.ha1.carry.weight'] + model[f'{prefix}.ha1.carry.bias']).item()
|
| 65 |
+
inp_ha2 = torch.tensor([ha1_sum, cin], dtype=torch.float32)
|
| 66 |
+
ha2_sum = eval_xor_arith(inp_ha2, f'{prefix}.ha2.sum')
|
| 67 |
+
ha2_carry = heaviside(inp_ha2 @ model[f'{prefix}.ha2.carry.weight'] + model[f'{prefix}.ha2.carry.bias']).item()
|
| 68 |
+
inp_cout = torch.tensor([ha1_carry, ha2_carry], dtype=torch.float32)
|
| 69 |
+
cout = heaviside(inp_cout @ model[f'{prefix}.carry_or.weight'] + model[f'{prefix}.carry_or.bias']).item()
|
| 70 |
+
return int(ha2_sum), int(cout)
|
| 71 |
+
|
| 72 |
+
def add_8bit(a, b):
|
| 73 |
+
carry = 0.0
|
| 74 |
+
result = 0
|
| 75 |
+
for i in range(8):
|
| 76 |
+
s, carry = eval_full_adder(float((a >> i) & 1), float((b >> i) & 1), carry, f'arithmetic.ripplecarry8bit.fa{i}')
|
| 77 |
+
result |= (s << i)
|
| 78 |
+
return result, int(carry)
|
| 79 |
+
|
| 80 |
+
def sub_8bit(a, b):
|
| 81 |
+
# a - b = a + (~b + 1)
|
| 82 |
+
not_b = 0
|
| 83 |
+
for i in range(8):
|
| 84 |
+
not_b |= (eval_not((b >> i) & 1) << i)
|
| 85 |
+
temp, _ = add_8bit(a, not_b)
|
| 86 |
+
result, _ = add_8bit(temp, 1)
|
| 87 |
+
return result
|
| 88 |
+
|
| 89 |
+
def gt(a, b):
|
| 90 |
+
a_bits, b_bits = int_to_bits(a), int_to_bits(b)
|
| 91 |
+
w = model['arithmetic.greaterthan8bit.comparator']
|
| 92 |
+
return 1 if ((a_bits - b_bits) @ w).item() > 0 else 0
|
| 93 |
+
|
| 94 |
+
def lt(a, b):
|
| 95 |
+
a_bits, b_bits = int_to_bits(a), int_to_bits(b)
|
| 96 |
+
w = model['arithmetic.lessthan8bit.comparator']
|
| 97 |
+
return 1 if ((b_bits - a_bits) @ w).item() > 0 else 0
|
| 98 |
+
|
| 99 |
+
def eq(a, b):
|
| 100 |
+
return 1 if (gt(a,b) == 0 and lt(a,b) == 0) else 0
|
| 101 |
+
|
| 102 |
+
def popcount(val):
|
| 103 |
+
bits = int_to_bits(val)
|
| 104 |
+
w = model['pattern_recognition.popcount.weight']
|
| 105 |
+
b = model['pattern_recognition.popcount.bias']
|
| 106 |
+
return int((bits @ w + b).item())
|
| 107 |
+
|
| 108 |
+
print('='*70)
|
| 109 |
+
print('WILD STRESS TESTS')
|
| 110 |
+
print('='*70)
|
| 111 |
+
|
| 112 |
+
# === TEST 1: FACTORIAL ===
|
| 113 |
+
print('\n[1] FACTORIAL via chained multiply-add')
|
| 114 |
+
def factorial(n):
|
| 115 |
+
result = 1
|
| 116 |
+
for i in range(2, n+1):
|
| 117 |
+
new_result = 0
|
| 118 |
+
for _ in range(i):
|
| 119 |
+
new_result, _ = add_8bit(new_result, result)
|
| 120 |
+
new_result &= 0xFF
|
| 121 |
+
result = new_result
|
| 122 |
+
return result
|
| 123 |
+
|
| 124 |
+
for n in [1, 2, 3, 4, 5]:
|
| 125 |
+
got = factorial(n)
|
| 126 |
+
expected = [1, 1, 2, 6, 24, 120][n]
|
| 127 |
+
status = 'OK' if got == expected else 'FAIL'
|
| 128 |
+
print(f' {n}! = {got} (expected {expected}) [{status}]')
|
| 129 |
+
|
| 130 |
+
# === TEST 2: GCD ===
|
| 131 |
+
print('\n[2] GCD via Euclidean algorithm')
|
| 132 |
+
def gcd(a, b):
|
| 133 |
+
iterations = 0
|
| 134 |
+
while not eq(b, 0) and iterations < 100:
|
| 135 |
+
temp = a
|
| 136 |
+
while not lt(temp, b) and not eq(temp, 0) and iterations < 100:
|
| 137 |
+
temp = sub_8bit(temp, b)
|
| 138 |
+
iterations += 1
|
| 139 |
+
a, b = b, temp
|
| 140 |
+
iterations += 1
|
| 141 |
+
return a
|
| 142 |
+
|
| 143 |
+
test_gcds = [(48, 18, 6), (100, 35, 5), (252, 105, 21), (17, 13, 1), (128, 64, 64)]
|
| 144 |
+
for a, b, expected in test_gcds:
|
| 145 |
+
got = gcd(a, b)
|
| 146 |
+
status = 'OK' if got == expected else 'FAIL'
|
| 147 |
+
print(f' gcd({a}, {b}) = {got} (expected {expected}) [{status}]')
|
| 148 |
+
|
| 149 |
+
# === TEST 3: FIBONACCI ===
|
| 150 |
+
print('\n[3] FIBONACCI until overflow')
|
| 151 |
+
def fib_sequence():
|
| 152 |
+
a, b = 0, 1
|
| 153 |
+
seq = [a, b]
|
| 154 |
+
for _ in range(20):
|
| 155 |
+
next_val, carry = add_8bit(a, b)
|
| 156 |
+
if carry:
|
| 157 |
+
break
|
| 158 |
+
seq.append(next_val)
|
| 159 |
+
a, b = b, next_val
|
| 160 |
+
return seq
|
| 161 |
+
|
| 162 |
+
fib = fib_sequence()
|
| 163 |
+
expected_fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]
|
| 164 |
+
print(f' Computed: {fib[:len(expected_fib)]}')
|
| 165 |
+
print(f' Expected: {expected_fib}')
|
| 166 |
+
print(f' Match: {fib[:len(expected_fib)] == expected_fib}')
|
| 167 |
+
|
| 168 |
+
# === TEST 4: PRIME CHECK ===
|
| 169 |
+
print('\n[4] PRIME CHECK via trial division')
|
| 170 |
+
def is_prime(n):
|
| 171 |
+
if n < 2: return False
|
| 172 |
+
if n == 2: return True
|
| 173 |
+
if (n & 1) == 0: return False
|
| 174 |
+
|
| 175 |
+
i = 3
|
| 176 |
+
while i * i <= n and i < n:
|
| 177 |
+
temp = n
|
| 178 |
+
while temp >= i:
|
| 179 |
+
temp = sub_8bit(temp, i)
|
| 180 |
+
if eq(temp, 0):
|
| 181 |
+
return False
|
| 182 |
+
i += 2
|
| 183 |
+
return True
|
| 184 |
+
|
| 185 |
+
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
|
| 186 |
+
non_primes = [4, 6, 8, 9, 10, 12, 14, 15, 16, 18]
|
| 187 |
+
|
| 188 |
+
prime_pass = sum(1 for p in primes if is_prime(p))
|
| 189 |
+
non_prime_pass = sum(1 for n in non_primes if not is_prime(n))
|
| 190 |
+
print(f' Primes correctly identified: {prime_pass}/10')
|
| 191 |
+
print(f' Non-primes correctly rejected: {non_prime_pass}/10')
|
| 192 |
+
|
| 193 |
+
# === TEST 5: INTEGER SQRT ===
|
| 194 |
+
print('\n[5] INTEGER SQUARE ROOT via binary search')
|
| 195 |
+
def isqrt(n):
|
| 196 |
+
if n == 0: return 0
|
| 197 |
+
lo, hi = 1, min(n, 15) # limit for 8-bit
|
| 198 |
+
result = 0
|
| 199 |
+
iterations = 0
|
| 200 |
+
while lo <= hi and iterations < 50:
|
| 201 |
+
mid = (lo + hi) >> 1
|
| 202 |
+
sq = 0
|
| 203 |
+
for _ in range(mid):
|
| 204 |
+
sq, _ = add_8bit(sq, mid)
|
| 205 |
+
sq &= 0xFF
|
| 206 |
+
|
| 207 |
+
if sq <= n:
|
| 208 |
+
result = mid
|
| 209 |
+
lo = mid + 1
|
| 210 |
+
else:
|
| 211 |
+
hi = mid - 1
|
| 212 |
+
iterations += 1
|
| 213 |
+
return result
|
| 214 |
+
|
| 215 |
+
sqrt_tests = [(0, 0), (1, 1), (4, 2), (9, 3), (16, 4), (25, 5), (36, 6), (49, 7), (64, 8), (81, 9), (100, 10), (144, 12)]
|
| 216 |
+
sqrt_pass = 0
|
| 217 |
+
for n, expected in sqrt_tests:
|
| 218 |
+
got = isqrt(n)
|
| 219 |
+
if got == expected:
|
| 220 |
+
sqrt_pass += 1
|
| 221 |
+
print(f' Passed: {sqrt_pass}/{len(sqrt_tests)}')
|
| 222 |
+
|
| 223 |
+
# === TEST 6: COLLATZ ===
|
| 224 |
+
print('\n[6] COLLATZ CONJECTURE iterations')
|
| 225 |
+
def collatz_steps(n):
|
| 226 |
+
steps = 0
|
| 227 |
+
while n != 1 and steps < 200:
|
| 228 |
+
if (n & 1) == 0:
|
| 229 |
+
n = n >> 1
|
| 230 |
+
else:
|
| 231 |
+
temp, _ = add_8bit(n, n)
|
| 232 |
+
temp, _ = add_8bit(temp, n)
|
| 233 |
+
n, _ = add_8bit(temp, 1)
|
| 234 |
+
n &= 0xFF
|
| 235 |
+
steps += 1
|
| 236 |
+
if n == 0: break
|
| 237 |
+
return steps
|
| 238 |
+
|
| 239 |
+
collatz_tests = [(1, 0), (2, 1), (3, 7), (6, 8)]
|
| 240 |
+
for start, expected in collatz_tests:
|
| 241 |
+
got = collatz_steps(start)
|
| 242 |
+
status = 'OK' if got == expected else f'got {got}'
|
| 243 |
+
print(f' collatz({start}) = {got} steps [{status}]')
|
| 244 |
+
|
| 245 |
+
# === TEST 7: SORT BY POPCOUNT ===
|
| 246 |
+
print('\n[7] SORT BY HAMMING WEIGHT (popcount)')
|
| 247 |
+
values = [0b11111111, 0b00000001, 0b10101010, 0b00001111, 0b11110000, 0b00000000]
|
| 248 |
+
weighted = [(v, popcount(v)) for v in values]
|
| 249 |
+
for i in range(len(weighted)):
|
| 250 |
+
for j in range(len(weighted) - 1):
|
| 251 |
+
if gt(weighted[j][1], weighted[j+1][1]):
|
| 252 |
+
weighted[j], weighted[j+1] = weighted[j+1], weighted[j]
|
| 253 |
+
|
| 254 |
+
print(f' Sorted by popcount:')
|
| 255 |
+
for v, p in weighted:
|
| 256 |
+
print(f' {bin(v):>12} -> popcount = {p}')
|
| 257 |
+
|
| 258 |
+
# === TEST 8: XOR CHECKSUM ===
|
| 259 |
+
print('\n[8] XOR CHECKSUM of message')
|
| 260 |
+
message = [0x48, 0x65, 0x6C, 0x6C, 0x6F] # "Hello"
|
| 261 |
+
checksum = 0
|
| 262 |
+
for byte in message:
|
| 263 |
+
for i in range(8):
|
| 264 |
+
bit_a = (checksum >> i) & 1
|
| 265 |
+
bit_b = (byte >> i) & 1
|
| 266 |
+
xor_bit = eval_xor(bit_a, bit_b)
|
| 267 |
+
checksum = (checksum & ~(1 << i)) | (xor_bit << i)
|
| 268 |
+
|
| 269 |
+
expected_checksum = 0x48 ^ 0x65 ^ 0x6C ^ 0x6C ^ 0x6F
|
| 270 |
+
status = 'OK' if checksum == expected_checksum else 'FAIL'
|
| 271 |
+
print(f' Message: {[hex(b) for b in message]}')
|
| 272 |
+
print(f' XOR checksum: {hex(checksum)} (expected {hex(expected_checksum)}) [{status}]')
|
| 273 |
+
|
| 274 |
+
# === TEST 9: PARITY TREE ===
|
| 275 |
+
print('\n[9] 8-BIT PARITY (full XOR tree)')
|
| 276 |
+
def parity_8bit(val):
|
| 277 |
+
bits = [(val >> i) & 1 for i in range(8)]
|
| 278 |
+
s1 = [eval_xor(bits[0], bits[1]), eval_xor(bits[2], bits[3]),
|
| 279 |
+
eval_xor(bits[4], bits[5]), eval_xor(bits[6], bits[7])]
|
| 280 |
+
s2 = [eval_xor(s1[0], s1[1]), eval_xor(s1[2], s1[3])]
|
| 281 |
+
return eval_xor(s2[0], s2[1])
|
| 282 |
+
|
| 283 |
+
parity_tests = [(0x00, 0), (0xFF, 0), (0x01, 1), (0x03, 0), (0x07, 1), (0xAA, 0), (0x55, 0), (0x81, 0), (0x80, 1)]
|
| 284 |
+
parity_pass = sum(1 for v, exp in parity_tests if parity_8bit(v) == exp)
|
| 285 |
+
print(f' Passed: {parity_pass}/{len(parity_tests)}')
|
| 286 |
+
|
| 287 |
+
# === TEST 10: OVERFLOW CASCADE ===
|
| 288 |
+
print('\n[10] OVERFLOW CASCADE (255 + 1 chain)')
|
| 289 |
+
val = 255
|
| 290 |
+
carries = []
|
| 291 |
+
for i in range(5):
|
| 292 |
+
val, carry = add_8bit(val, 1)
|
| 293 |
+
carries.append(carry)
|
| 294 |
+
print(f' 255 -> +1 -> +1 -> +1 -> +1 -> +1')
|
| 295 |
+
print(f' Carries: {carries}')
|
| 296 |
+
print(f' Final value: {val} (expected 4) [{"OK" if val == 4 else "FAIL"}]')
|
| 297 |
+
|
| 298 |
+
# === TEST 11: POWER OF 2 CHECK ===
|
| 299 |
+
print('\n[11] POWER OF 2 detection (popcount == 1)')
|
| 300 |
+
def is_power_of_2(n):
|
| 301 |
+
if n == 0: return False
|
| 302 |
+
return popcount(n) == 1
|
| 303 |
+
|
| 304 |
+
pow2_tests = [(1, True), (2, True), (4, True), (8, True), (16, True), (32, True), (64, True), (128, True),
|
| 305 |
+
(3, False), (5, False), (6, False), (7, False), (9, False), (15, False), (255, False)]
|
| 306 |
+
pow2_pass = sum(1 for n, exp in pow2_tests if is_power_of_2(n) == exp)
|
| 307 |
+
print(f' Passed: {pow2_pass}/{len(pow2_tests)}')
|
| 308 |
+
|
| 309 |
+
# === TEST 12: BYTE REVERSE ===
|
| 310 |
+
print('\n[12] BYTE REVERSE via bit manipulation')
|
| 311 |
+
def reverse_bits(val):
|
| 312 |
+
result = 0
|
| 313 |
+
for i in range(8):
|
| 314 |
+
bit = (val >> i) & 1
|
| 315 |
+
result |= (bit << (7 - i))
|
| 316 |
+
return result
|
| 317 |
+
|
| 318 |
+
reverse_tests = [(0b10000000, 0b00000001), (0b11110000, 0b00001111), (0b10101010, 0b01010101), (0b00000000, 0b00000000), (0b11111111, 0b11111111)]
|
| 319 |
+
reverse_pass = sum(1 for inp, exp in reverse_tests if reverse_bits(inp) == exp)
|
| 320 |
+
print(f' Passed: {reverse_pass}/{len(reverse_tests)}')
|
| 321 |
+
|
| 322 |
+
# === TEST 13: MAX/MIN via comparator ===
|
| 323 |
+
print('\n[13] MAX and MIN of array')
|
| 324 |
+
def find_max(arr):
|
| 325 |
+
m = arr[0]
|
| 326 |
+
for x in arr[1:]:
|
| 327 |
+
if gt(x, m):
|
| 328 |
+
m = x
|
| 329 |
+
return m
|
| 330 |
+
|
| 331 |
+
def find_min(arr):
|
| 332 |
+
m = arr[0]
|
| 333 |
+
for x in arr[1:]:
|
| 334 |
+
if lt(x, m):
|
| 335 |
+
m = x
|
| 336 |
+
return m
|
| 337 |
+
|
| 338 |
+
test_arr = [42, 17, 255, 0, 128, 64, 33]
|
| 339 |
+
got_max = find_max(test_arr)
|
| 340 |
+
got_min = find_min(test_arr)
|
| 341 |
+
print(f' Array: {test_arr}')
|
| 342 |
+
print(f' Max: {got_max} (expected 255) [{"OK" if got_max == 255 else "FAIL"}]')
|
| 343 |
+
print(f' Min: {got_min} (expected 0) [{"OK" if got_min == 0 else "FAIL"}]')
|
| 344 |
+
|
| 345 |
+
# === TEST 14: LFSR (pseudo-random) ===
|
| 346 |
+
print('\n[14] 8-BIT LFSR (taps at 8,6,5,4)')
|
| 347 |
+
def lfsr_step(state):
|
| 348 |
+
# Taps: 8, 6, 5, 4 (for maximal length)
|
| 349 |
+
bit = eval_xor((state >> 0) & 1, (state >> 2) & 1)
|
| 350 |
+
bit = eval_xor(bit, (state >> 3) & 1)
|
| 351 |
+
bit = eval_xor(bit, (state >> 4) & 1)
|
| 352 |
+
return ((state >> 1) | (bit << 7)) & 0xFF
|
| 353 |
+
|
| 354 |
+
state = 1
|
| 355 |
+
seen = set()
|
| 356 |
+
for i in range(300):
|
| 357 |
+
if state in seen:
|
| 358 |
+
break
|
| 359 |
+
seen.add(state)
|
| 360 |
+
state = lfsr_step(state)
|
| 361 |
+
|
| 362 |
+
print(f' Period: {len(seen)} (max possible: 255)')
|
| 363 |
+
print(f' Full period: {"OK" if len(seen) == 255 else "FAIL"}')
|
| 364 |
+
|
| 365 |
+
print('\n' + '='*70)
|
| 366 |
+
print('STRESS TESTS COMPLETE')
|
| 367 |
+
print('='*70)
|
train_circuit_interface.py
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Train the circuit interface layers on arithmetic examples.
|
| 3 |
+
============================================================
|
| 4 |
+
|
| 5 |
+
The threshold circuits are frozen - we only train:
|
| 6 |
+
- BitExtractor: embedding -> operand bits
|
| 7 |
+
- BitInjector: result bits -> embedding
|
| 8 |
+
- Router: when to use circuits vs MLP
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import torch
|
| 12 |
+
import torch.nn as nn
|
| 13 |
+
from torch.utils.data import Dataset, DataLoader
|
| 14 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 15 |
+
from tqdm import tqdm
|
| 16 |
+
import argparse
|
| 17 |
+
import warnings
|
| 18 |
+
warnings.filterwarnings('ignore')
|
| 19 |
+
|
| 20 |
+
from circuit_llm import (
|
| 21 |
+
augment_smollm2_with_circuits,
|
| 22 |
+
evaluate_arithmetic,
|
| 23 |
+
CircuitExecutor
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# =============================================================================
|
| 28 |
+
# ARITHMETIC DATASET
|
| 29 |
+
# =============================================================================
|
| 30 |
+
|
| 31 |
+
class ArithmeticDataset(Dataset):
|
| 32 |
+
"""Dataset of 8-bit addition problems."""
|
| 33 |
+
|
| 34 |
+
def __init__(self, tokenizer, n_samples: int = 10000, max_val: int = 255):
|
| 35 |
+
self.tokenizer = tokenizer
|
| 36 |
+
self.n_samples = n_samples
|
| 37 |
+
self.max_val = max_val
|
| 38 |
+
|
| 39 |
+
# Pre-generate all examples
|
| 40 |
+
self.examples = []
|
| 41 |
+
for _ in range(n_samples):
|
| 42 |
+
a = torch.randint(0, max_val + 1, (1,)).item()
|
| 43 |
+
b = torch.randint(0, max_val + 1, (1,)).item()
|
| 44 |
+
result = (a + b) % 256
|
| 45 |
+
|
| 46 |
+
prompt = f"{a} + {b} ="
|
| 47 |
+
target = f" {result}"
|
| 48 |
+
|
| 49 |
+
self.examples.append((prompt, target, a, b, result))
|
| 50 |
+
|
| 51 |
+
def __len__(self):
|
| 52 |
+
return len(self.examples)
|
| 53 |
+
|
| 54 |
+
def __getitem__(self, idx):
|
| 55 |
+
prompt, target, a, b, result = self.examples[idx]
|
| 56 |
+
|
| 57 |
+
# Tokenize
|
| 58 |
+
prompt_ids = self.tokenizer.encode(prompt, add_special_tokens=False)
|
| 59 |
+
target_ids = self.tokenizer.encode(target, add_special_tokens=False)
|
| 60 |
+
|
| 61 |
+
input_ids = prompt_ids + target_ids
|
| 62 |
+
labels = [-100] * len(prompt_ids) + target_ids # Only predict target
|
| 63 |
+
|
| 64 |
+
return {
|
| 65 |
+
'input_ids': torch.tensor(input_ids),
|
| 66 |
+
'labels': torch.tensor(labels),
|
| 67 |
+
'a': a,
|
| 68 |
+
'b': b,
|
| 69 |
+
'result': result
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def collate_fn(batch):
|
| 74 |
+
"""Collate with padding."""
|
| 75 |
+
max_len = max(len(item['input_ids']) for item in batch)
|
| 76 |
+
|
| 77 |
+
input_ids = []
|
| 78 |
+
labels = []
|
| 79 |
+
attention_mask = []
|
| 80 |
+
|
| 81 |
+
for item in batch:
|
| 82 |
+
pad_len = max_len - len(item['input_ids'])
|
| 83 |
+
|
| 84 |
+
input_ids.append(
|
| 85 |
+
torch.cat([item['input_ids'], torch.zeros(pad_len, dtype=torch.long)])
|
| 86 |
+
)
|
| 87 |
+
labels.append(
|
| 88 |
+
torch.cat([item['labels'], torch.full((pad_len,), -100, dtype=torch.long)])
|
| 89 |
+
)
|
| 90 |
+
attention_mask.append(
|
| 91 |
+
torch.cat([torch.ones(len(item['input_ids'])), torch.zeros(pad_len)])
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
return {
|
| 95 |
+
'input_ids': torch.stack(input_ids),
|
| 96 |
+
'labels': torch.stack(labels),
|
| 97 |
+
'attention_mask': torch.stack(attention_mask),
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
# =============================================================================
|
| 102 |
+
# TRAINING LOOP
|
| 103 |
+
# =============================================================================
|
| 104 |
+
|
| 105 |
+
def train_interface(
|
| 106 |
+
model: AutoModelForCausalLM,
|
| 107 |
+
tokenizer: AutoTokenizer,
|
| 108 |
+
n_epochs: int = 3,
|
| 109 |
+
batch_size: int = 16,
|
| 110 |
+
lr: float = 1e-4,
|
| 111 |
+
n_train_samples: int = 10000,
|
| 112 |
+
device: str = 'cpu',
|
| 113 |
+
eval_every: int = 500
|
| 114 |
+
):
|
| 115 |
+
"""
|
| 116 |
+
Train the circuit interface layers.
|
| 117 |
+
|
| 118 |
+
Only trains:
|
| 119 |
+
- bit_extractor (embedding -> bits)
|
| 120 |
+
- bit_injector (bits -> embedding)
|
| 121 |
+
- router (circuit vs MLP weighting)
|
| 122 |
+
- op_selector (which operation)
|
| 123 |
+
"""
|
| 124 |
+
print("\n" + "=" * 70)
|
| 125 |
+
print(" TRAINING CIRCUIT INTERFACE")
|
| 126 |
+
print("=" * 70)
|
| 127 |
+
|
| 128 |
+
# Freeze everything except interface layers
|
| 129 |
+
interface_params = []
|
| 130 |
+
frozen_count = 0
|
| 131 |
+
trainable_count = 0
|
| 132 |
+
|
| 133 |
+
for name, param in model.named_parameters():
|
| 134 |
+
if any(x in name for x in ['bit_extractor', 'bit_injector', 'router', 'op_selector']):
|
| 135 |
+
param.requires_grad = True
|
| 136 |
+
interface_params.append(param)
|
| 137 |
+
trainable_count += param.numel()
|
| 138 |
+
else:
|
| 139 |
+
param.requires_grad = False
|
| 140 |
+
frozen_count += param.numel()
|
| 141 |
+
|
| 142 |
+
print(f"\n Frozen parameters: {frozen_count:,}")
|
| 143 |
+
print(f" Trainable parameters: {trainable_count:,}")
|
| 144 |
+
print(f" Training {len(interface_params)} parameter groups")
|
| 145 |
+
|
| 146 |
+
# Create dataset
|
| 147 |
+
print(f"\n Creating dataset ({n_train_samples} examples)...")
|
| 148 |
+
dataset = ArithmeticDataset(tokenizer, n_samples=n_train_samples)
|
| 149 |
+
dataloader = DataLoader(
|
| 150 |
+
dataset,
|
| 151 |
+
batch_size=batch_size,
|
| 152 |
+
shuffle=True,
|
| 153 |
+
collate_fn=collate_fn
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
# Optimizer
|
| 157 |
+
optimizer = torch.optim.AdamW(interface_params, lr=lr)
|
| 158 |
+
|
| 159 |
+
# Training
|
| 160 |
+
model.to(device)
|
| 161 |
+
model.train()
|
| 162 |
+
|
| 163 |
+
global_step = 0
|
| 164 |
+
total_loss = 0
|
| 165 |
+
|
| 166 |
+
for epoch in range(n_epochs):
|
| 167 |
+
print(f"\n Epoch {epoch + 1}/{n_epochs}")
|
| 168 |
+
print(" " + "-" * 60)
|
| 169 |
+
|
| 170 |
+
epoch_loss = 0
|
| 171 |
+
epoch_steps = 0
|
| 172 |
+
|
| 173 |
+
pbar = tqdm(dataloader, desc=f" Training", leave=False)
|
| 174 |
+
|
| 175 |
+
for batch in pbar:
|
| 176 |
+
input_ids = batch['input_ids'].to(device)
|
| 177 |
+
labels = batch['labels'].to(device)
|
| 178 |
+
attention_mask = batch['attention_mask'].to(device)
|
| 179 |
+
|
| 180 |
+
# Forward
|
| 181 |
+
outputs = model(
|
| 182 |
+
input_ids=input_ids,
|
| 183 |
+
attention_mask=attention_mask,
|
| 184 |
+
labels=labels
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
loss = outputs.loss
|
| 188 |
+
|
| 189 |
+
# Backward
|
| 190 |
+
optimizer.zero_grad()
|
| 191 |
+
loss.backward()
|
| 192 |
+
optimizer.step()
|
| 193 |
+
|
| 194 |
+
# Logging
|
| 195 |
+
epoch_loss += loss.item()
|
| 196 |
+
epoch_steps += 1
|
| 197 |
+
global_step += 1
|
| 198 |
+
total_loss += loss.item()
|
| 199 |
+
|
| 200 |
+
pbar.set_postfix({'loss': f'{loss.item():.4f}'})
|
| 201 |
+
|
| 202 |
+
# Periodic evaluation
|
| 203 |
+
if global_step % eval_every == 0:
|
| 204 |
+
model.eval()
|
| 205 |
+
eval_results = evaluate_arithmetic(model, tokenizer, n_problems=50, device=device)
|
| 206 |
+
print(f"\n Step {global_step}: Loss={total_loss/eval_every:.4f}, "
|
| 207 |
+
f"Accuracy={eval_results['accuracy']*100:.1f}%")
|
| 208 |
+
total_loss = 0
|
| 209 |
+
model.train()
|
| 210 |
+
|
| 211 |
+
avg_loss = epoch_loss / epoch_steps
|
| 212 |
+
print(f"\n Epoch {epoch + 1} complete. Avg loss: {avg_loss:.4f}")
|
| 213 |
+
|
| 214 |
+
# End of epoch evaluation
|
| 215 |
+
model.eval()
|
| 216 |
+
eval_results = evaluate_arithmetic(model, tokenizer, n_problems=100, device=device)
|
| 217 |
+
print(f" Evaluation: {eval_results['accuracy']*100:.1f}% "
|
| 218 |
+
f"({eval_results['correct']}/{eval_results['total']})")
|
| 219 |
+
|
| 220 |
+
if eval_results['errors']:
|
| 221 |
+
print(f" Sample errors:")
|
| 222 |
+
for a, b, exp, got in eval_results['errors'][:3]:
|
| 223 |
+
print(f" {a} + {b} = {exp}, model said {got}")
|
| 224 |
+
|
| 225 |
+
model.train()
|
| 226 |
+
|
| 227 |
+
print("\n" + "=" * 70)
|
| 228 |
+
print(" TRAINING COMPLETE")
|
| 229 |
+
print("=" * 70)
|
| 230 |
+
|
| 231 |
+
return model
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
# =============================================================================
|
| 235 |
+
# MAIN
|
| 236 |
+
# =============================================================================
|
| 237 |
+
|
| 238 |
+
if __name__ == "__main__":
|
| 239 |
+
parser = argparse.ArgumentParser(description='Train Circuit Interface')
|
| 240 |
+
parser.add_argument('--circuit-path', type=str,
|
| 241 |
+
default='./neural_computer.safetensors',
|
| 242 |
+
help='Path to circuit weights')
|
| 243 |
+
parser.add_argument('--device', type=str, default='cpu',
|
| 244 |
+
help='Device (cpu or cuda)')
|
| 245 |
+
parser.add_argument('--epochs', type=int, default=3,
|
| 246 |
+
help='Number of epochs')
|
| 247 |
+
parser.add_argument('--batch-size', type=int, default=8,
|
| 248 |
+
help='Batch size')
|
| 249 |
+
parser.add_argument('--lr', type=float, default=1e-4,
|
| 250 |
+
help='Learning rate')
|
| 251 |
+
parser.add_argument('--n-samples', type=int, default=5000,
|
| 252 |
+
help='Number of training samples')
|
| 253 |
+
args = parser.parse_args()
|
| 254 |
+
|
| 255 |
+
print("=" * 70)
|
| 256 |
+
print(" CIRCUIT-AUGMENTED LLM TRAINING")
|
| 257 |
+
print("=" * 70)
|
| 258 |
+
|
| 259 |
+
# Load model
|
| 260 |
+
print("\n[1] Loading SmolLM2-360M...")
|
| 261 |
+
model_id = "HuggingFaceTB/SmolLM2-360M"
|
| 262 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 263 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 264 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32)
|
| 265 |
+
|
| 266 |
+
# Baseline
|
| 267 |
+
print("\n[2] Baseline evaluation...")
|
| 268 |
+
baseline = evaluate_arithmetic(model, tokenizer, n_problems=50, device=args.device)
|
| 269 |
+
print(f" Baseline accuracy: {baseline['accuracy']*100:.1f}%")
|
| 270 |
+
|
| 271 |
+
# Augment
|
| 272 |
+
print("\n[3] Augmenting with circuits...")
|
| 273 |
+
model = augment_smollm2_with_circuits(
|
| 274 |
+
model,
|
| 275 |
+
args.circuit_path,
|
| 276 |
+
device=args.device
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
# Train
|
| 280 |
+
print("\n[4] Training interface layers...")
|
| 281 |
+
model = train_interface(
|
| 282 |
+
model,
|
| 283 |
+
tokenizer,
|
| 284 |
+
n_epochs=args.epochs,
|
| 285 |
+
batch_size=args.batch_size,
|
| 286 |
+
lr=args.lr,
|
| 287 |
+
n_train_samples=args.n_samples,
|
| 288 |
+
device=args.device
|
| 289 |
+
)
|
| 290 |
+
|
| 291 |
+
# Final evaluation
|
| 292 |
+
print("\n[5] Final evaluation...")
|
| 293 |
+
final = evaluate_arithmetic(model, tokenizer, n_problems=100, device=args.device)
|
| 294 |
+
print(f" Final accuracy: {final['accuracy']*100:.1f}%")
|
| 295 |
+
print(f" Improvement: {baseline['accuracy']*100:.1f}% -> {final['accuracy']*100:.1f}%")
|
| 296 |
+
|
| 297 |
+
# Save
|
| 298 |
+
save_path = './circuit_augmented_smollm2.pt'
|
| 299 |
+
print(f"\n[6] Saving to {save_path}...")
|
| 300 |
+
torch.save({
|
| 301 |
+
'model_state_dict': model.state_dict(),
|
| 302 |
+
'baseline_accuracy': baseline['accuracy'],
|
| 303 |
+
'final_accuracy': final['accuracy']
|
| 304 |
+
}, save_path)
|
| 305 |
+
|
| 306 |
+
print("\nDone!")
|