-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset_memory_buffers.py
More file actions
118 lines (96 loc) · 3.19 KB
/
reset_memory_buffers.py
File metadata and controls
118 lines (96 loc) · 3.19 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
#!/usr/bin/env python3
# Reset exploded memory buffers WITHOUT touching learned weights
# These buffers accumulated extreme values and cause slowdown
import shutil
from datetime import datetime
import torch
from config import *
print("=" * 80)
print("MEMORY BUFFER RESET")
print("=" * 80)
checkpoint_path = modelFilePath
backup_path = checkpoint_path.replace(
".pth", f"_BEFORE_MEMORY_RESET_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pth"
)
print(f"\nCheckpoint: {checkpoint_path}")
print(f"Backup: {backup_path}")
# 1. Backup
print("\n[1/3] Creating backup...")
try:
shutil.copy2(checkpoint_path, backup_path)
print("✓ Backup saved")
except Exception as e:
print(f"❌ Backup failed: {e}")
exit(1)
# 2. Load
print("\n[2/3] Loading checkpoint...")
try:
state_dict = torch.load(checkpoint_path, map_location="cpu")
print(f"✓ Loaded {len(state_dict)} parameters")
except Exception as e:
print(f"❌ Load failed: {e}")
exit(1)
# 3. Reset memory buffers
print("\n[3/3] Resetting memory buffers...")
buffers_to_reset = [
"memory.shortTermMemory",
"memory.longTermMemory",
"memory2.shortTermMemory",
"memory2.longTermMemory",
"scratchpad.buffer", # Also reset scratchpad if it exists
]
reset_count = 0
for buffer_name in buffers_to_reset:
if buffer_name in state_dict:
old_norm = state_dict[buffer_name].norm().item()
# Reset to zeros (normal initialization)
state_dict[buffer_name].zero_()
print(f" ✓ {buffer_name:30s}: {old_norm:12.2e} → 0.0")
reset_count += 1
else:
print(f" ⊘ {buffer_name:30s}: not found (skipped)")
if reset_count == 0:
print("\n⚠️ No memory buffers found to reset")
print(" Checkpoint may not have memory buffers")
else:
print(f"\n✓ Reset {reset_count} memory buffers")
# 4. Save
print("\nSaving modified checkpoint...")
try:
torch.save(state_dict, checkpoint_path)
print("✓ Checkpoint saved")
except Exception as e:
print(f"❌ Save failed: {e}")
print(" Restoring backup...")
shutil.copy2(backup_path, checkpoint_path)
print(" ✓ Backup restored")
exit(1)
print("\n" + "=" * 80)
print("MEMORY BUFFERS RESET COMPLETE")
print("=" * 80)
print("""
What was reset:
- memory.shortTermMemory: 5.39M → 0
- memory.longTermMemory: 4.64M → 0
- memory2.shortTermMemory: 9.91M → 0
- memory2.longTermMemory: 9.92M → 0
What was preserved:
✓ ALL learned weights (attention, INN, gates, etc.)
✓ ALL parameters (nothing deleted)
✓ Full training history
Why this helps:
- Memory buffers were accumulating exploded activations
- They're processed EVERY forward pass (huge computational cost)
- NOT affected by weight decay (they're state, not parameters)
- Will naturally refill with normal values as training continues
Expected improvement:
- Training speed: 3 mins/step → 2-30 seconds/step
- Memory operations no longer bottlenecked by extreme values
- Model will rebuild memory state from normal activations
Next steps:
1. Restart training (python3 wakeup.py)
2. Memory buffers will refill normally
3. Training should be MUCH faster
4. Model behavior unchanged (memory is just working memory)
""")
print("=" * 80)