-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_hope.py
More file actions
434 lines (318 loc) · 11.3 KB
/
test_hope.py
File metadata and controls
434 lines (318 loc) · 11.3 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
"""
Test script for HOPE architecture.
Verifies:
1. Model creation and forward pass
2. Delta rule implementation (Eq. 28-29)
3. Memory state management
4. Continuum Memory System
5. Optimizers
Run: python test_hope.py
"""
import torch
import torch.nn as nn
import math
from typing import Optional
# Import HOPE components
from src.config import HopeConfig, HopeSmallConfig
from src.model import Hope, HopeForCausalLM, createHopeModel
from src.modules.titans import SelfModifyingTitans, SelfModifyingTitansChunk
from src.modules.continuum_memory import ContinuumMemorySystem, FrequencyFFN
from src.modules.hope_block import HopeBlock
from src.layers.associative_memory import DeltaRuleMemory, LinearAttentionMemory
from src.layers.neural_memory import FastNeuralMemory, NeuralMemory
from src.optimizers import (
DeepMomentumGD,
DeltaRuleOptimizer,
AdamWithDeltaRule,
MuonOptimizer,
)
def testDeltaRuleMemory():
"""Test Delta Rule Memory implementation (Eq. 28-29)."""
print("Testing DeltaRuleMemory...")
batch_size = 2
dim_key = 64
dim_value = 64
memory_module = DeltaRuleMemory(
dim_key=dim_key,
dim_value=dim_value,
learning_rate=0.1,
)
# Initialize memory
memory = memory_module.initMemory(batch_size, torch.device("cpu"))
assert memory.shape == (batch_size, dim_value, dim_key)
# Create test key-value pairs
key = torch.randn(batch_size, dim_key)
value = torch.randn(batch_size, dim_value)
# Update memory
memory, _ = memory_module.update(memory, key, value)
# Retrieve using the same key
retrieved = memory_module.retrieve(memory, key)
assert retrieved.shape == (batch_size, dim_value)
# The retrieved value should be close to the stored value
# (not exact due to delta rule dynamics)
print(f" Retrieval error: {(retrieved - value).abs().mean().item():.4f}")
print(" DeltaRuleMemory: PASSED")
def testSelfModifyingTitans():
"""Test Self-Modifying Titans with delta rule."""
print("Testing SelfModifyingTitans...")
batch_size = 2
seq_len = 32
dim = 256
num_heads = 4
head_dim = 64
titans = SelfModifyingTitans(
dim=dim,
head_dim=head_dim,
num_heads=num_heads,
learning_rate=0.1,
use_delta_rule=True,
)
# Forward pass
x = torch.randn(batch_size, seq_len, dim)
output, memory_state = titans(x, memory_state=None, return_memory=True)
assert output.shape == (batch_size, seq_len, dim)
assert memory_state.shape == (batch_size, num_heads, head_dim, head_dim)
# Test with previous memory state
x2 = torch.randn(batch_size, seq_len, dim)
output2, memory_state2 = titans(x2, memory_state=memory_state, return_memory=True)
assert output2.shape == (batch_size, seq_len, dim)
# Memory should have changed
memory_diff = (memory_state2 - memory_state).abs().mean().item()
print(f" Memory state change: {memory_diff:.4f}")
print(" SelfModifyingTitans: PASSED")
def testContinuumMemorySystem():
"""Test Continuum Memory System (Eq. 30-31)."""
print("Testing ContinuumMemorySystem...")
batch_size = 2
seq_len = 32
dim = 256
num_levels = 3
chunk_sizes = [4, 16, 64]
cms = ContinuumMemorySystem(
dim=dim,
num_levels=num_levels,
chunk_sizes=chunk_sizes,
expansion=4,
)
# Forward pass
x = torch.randn(batch_size, seq_len, dim)
output = cms(x, enable_online_learning=False)
assert output.shape == (batch_size, seq_len, dim)
# Test with online learning
cms.resetAccumulators()
output_online = cms(x, enable_online_learning=True)
assert output_online.shape == (batch_size, seq_len, dim)
# Check update schedule
schedule = cms.getUpdateSchedule(100)
print(f" Update schedule (first 100 steps): {len(schedule)} updates")
print(" ContinuumMemorySystem: PASSED")
def testHopeBlock():
"""Test complete HOPE block."""
print("Testing HopeBlock...")
batch_size = 2
seq_len = 32
dim = 256
block = HopeBlock(
dim=dim,
head_dim=64,
num_heads=4,
num_memory_levels=3,
chunk_sizes=[4, 16, 64],
use_delta_rule=True,
)
# Forward pass
x = torch.randn(batch_size, seq_len, dim)
output, memory_state = block(x, memory_state=None, return_memory=True)
assert output.shape == (batch_size, seq_len, dim)
assert memory_state is not None
# Test with memory
x2 = torch.randn(batch_size, seq_len, dim)
output2, memory_state2 = block(x2, memory_state=memory_state, return_memory=True)
assert output2.shape == (batch_size, seq_len, dim)
print(" HopeBlock: PASSED")
def testHopeModel():
"""Test full HOPE model."""
print("Testing Hope model...")
config = HopeSmallConfig(vocab_size=1000)
model = Hope(config)
batch_size = 2
seq_len = 64
# Forward pass
input_ids = torch.randint(0, config.vocab_size, (batch_size, seq_len))
logits = model(input_ids)
assert logits.shape == (batch_size, seq_len, config.vocab_size)
# Test with memory
logits, memory_states = model(input_ids, return_memory=True)
assert len(memory_states) == config.num_layers
# Test generation
prompt = torch.randint(0, config.vocab_size, (1, 10))
generated = model.generate(prompt, max_new_tokens=20)
assert generated.shape[1] == 30 # 10 prompt + 20 generated
num_params = model.getNumParams()
print(f" Model parameters: {num_params:,}")
print(" Hope model: PASSED")
def testHopeForCausalLM():
"""Test HOPE for causal language modeling."""
print("Testing HopeForCausalLM...")
config = HopeSmallConfig(vocab_size=1000)
model = HopeForCausalLM(config)
batch_size = 2
seq_len = 64
# Forward pass with labels
input_ids = torch.randint(0, config.vocab_size, (batch_size, seq_len))
labels = input_ids.clone()
outputs = model(input_ids=input_ids, labels=labels)
assert "logits" in outputs
assert "loss" in outputs
assert outputs["logits"].shape == (batch_size, seq_len, config.vocab_size)
loss = outputs["loss"]
print(f" Loss: {loss.item():.4f}")
# Backward pass
loss.backward()
print(" HopeForCausalLM: PASSED")
def testOptimizers():
"""Test custom optimizers."""
print("Testing optimizers...")
# Create a simple model
model = nn.Linear(64, 64)
x = torch.randn(8, 64)
target = torch.randn(8, 64)
optimizers = {
"DeepMomentumGD": DeepMomentumGD(model.parameters(), lr=0.01, memory_depth=2),
"DeltaRuleOptimizer": DeltaRuleOptimizer(model.parameters(), lr=0.01),
"AdamWithDeltaRule": AdamWithDeltaRule(model.parameters(), lr=0.01),
"MuonOptimizer": MuonOptimizer(model.parameters(), lr=0.01),
}
for name, optimizer in optimizers.items():
# Reset model
model.reset_parameters()
optimizer.zero_grad()
# Forward-backward
output = model(x)
loss = nn.functional.mse_loss(output, target)
loss.backward()
# Step
optimizer.step()
print(f" {name}: OK (loss={loss.item():.4f})")
print(" Optimizers: PASSED")
def testFastNeuralMemory():
"""Test FastNeuralMemory with delta rule."""
print("Testing FastNeuralMemory...")
batch_size = 2
seq_len = 16
dim = 128
num_heads = 2
head_dim = 64
memory = FastNeuralMemory(
dim=dim,
head_dim=head_dim,
num_heads=num_heads,
learning_rate=0.1,
use_delta_rule=True,
)
# Forward pass
x = torch.randn(batch_size, seq_len, dim)
output, mem_state, mom_buffer = memory(x)
assert output.shape == (batch_size, seq_len, dim)
assert mem_state.shape == (batch_size, num_heads, head_dim, head_dim)
# Test with previous state
x2 = torch.randn(batch_size, seq_len, dim)
output2, mem_state2, mom_buffer2 = memory(x2, mem_state, mom_buffer)
# Memory should evolve
mem_diff = (mem_state2 - mem_state).abs().mean().item()
print(f" Memory evolution: {mem_diff:.4f}")
print(" FastNeuralMemory: PASSED")
def testDeltaRuleFormula():
"""
Verify delta rule formula matches Eq. 28-29.
M_{t+1} = M_t - M_t * k * k^T - eta * (M_t * k - v) * k^T
"""
print("Testing Delta Rule formula (Eq. 28-29)...")
# Manual implementation
def deltaRuleManual(M, k, v, eta=0.1):
"""Manual delta rule for verification."""
# Normalize key
k_norm = k / (k.norm() + 1e-8)
# Prediction: M @ k
predicted = M @ k_norm
# Surprise: M*k - v
surprise = predicted - v
# Forgetting term: (M @ k) @ k^T
forget_term = torch.outer(predicted, k_norm)
# Learning term: surprise @ k^T
learn_term = torch.outer(surprise, k_norm)
# Update: M = M - forget_term - eta * learn_term
M_new = M - forget_term - eta * learn_term
return M_new
# Compare with DeltaRuleMemory
dim = 32
memory_module = DeltaRuleMemory(dim_key=dim, dim_value=dim, learning_rate=0.1)
# Initialize
M = torch.randn(dim, dim)
k = torch.randn(dim)
v = torch.randn(dim)
# Manual update
M_manual = deltaRuleManual(M.clone(), k, v, eta=0.1)
# Module update
M_batch = M.unsqueeze(0) # Add batch dim
k_batch = k.unsqueeze(0)
v_batch = v.unsqueeze(0)
M_module, _ = memory_module.update(M_batch, k_batch, v_batch)
M_module = M_module.squeeze(0)
# Compare
diff = (M_manual - M_module).abs().max().item()
print(f" Max difference between manual and module: {diff:.6f}")
assert diff < 1e-5, f"Delta rule mismatch: {diff}"
print(" Delta Rule formula: PASSED")
def testMemoryPersistence():
"""Test that memory persists across sequences."""
print("Testing memory persistence...")
config = HopeSmallConfig(vocab_size=1000)
model = Hope(config)
batch_size = 1
seq_len = 32
# Process first sequence
input1 = torch.randint(0, config.vocab_size, (batch_size, seq_len))
_, memory_states = model(input1, return_memory=True)
# Process second sequence with previous memory
input2 = torch.randint(0, config.vocab_size, (batch_size, seq_len))
_, memory_states2 = model(input2, memory_states=memory_states, return_memory=True)
# Memory should have evolved
for i, (m1, m2) in enumerate(zip(memory_states, memory_states2)):
if m1 is not None and m2 is not None:
diff = (m2 - m1).abs().mean().item()
print(f" Layer {i} memory change: {diff:.4f}")
print(" Memory persistence: PASSED")
def runAllTests():
"""Run all tests."""
print("=" * 60)
print("HOPE Architecture Tests")
print("=" * 60)
tests = [
testDeltaRuleMemory,
testDeltaRuleFormula,
testSelfModifyingTitans,
testContinuumMemorySystem,
testHopeBlock,
testHopeModel,
testHopeForCausalLM,
testFastNeuralMemory,
testOptimizers,
testMemoryPersistence,
]
passed = 0
failed = 0
for test in tests:
try:
test()
passed += 1
except Exception as e:
print(f" FAILED: {e}")
failed += 1
print("=" * 60)
print(f"Results: {passed} passed, {failed} failed")
print("=" * 60)
return failed == 0
if __name__ == "__main__":
success = runAllTests()
exit(0 if success else 1)