Phase 7: Curriculum Learning (20K steps, BPC 1.78)
Browse files- test_recall.py +69 -0
test_recall.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from src.models.agiformer import AGIFORMER
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def run_needle_test(model_path, noise_len=1000):
|
| 6 |
+
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 7 |
+
|
| 8 |
+
print(f"Loading {model_path} for Recall Test...")
|
| 9 |
+
# Config (Eğitimdeki ile aynı olmalı)
|
| 10 |
+
model = AGIFORMER(d_model=512, n_layers=6, patch_size=4, thinking_steps=3).to(DEVICE)
|
| 11 |
+
model.load_state_dict(torch.load(model_path, map_location=DEVICE))
|
| 12 |
+
model.eval()
|
| 13 |
+
|
| 14 |
+
# 1. Senaryo Oluşturma
|
| 15 |
+
secret_key = "1453"
|
| 16 |
+
needle = f"Gizli şifre {secret_key}."
|
| 17 |
+
|
| 18 |
+
# Samanlık (Gürültü) - Wikipedia benzeri rastgele metin
|
| 19 |
+
haystack = " Tarih boyunca birçok medeniyet kurulmuş ve yıkılmıştır. " * (noise_len // 10)
|
| 20 |
+
|
| 21 |
+
query = " Soru: Gizli şifre nedir? Cevap:"
|
| 22 |
+
|
| 23 |
+
full_prompt = needle + haystack + query
|
| 24 |
+
|
| 25 |
+
print(f"\n--- TEST SETUP ---")
|
| 26 |
+
print(f"Context Length: {len(full_prompt)} bytes")
|
| 27 |
+
print(f"Needle: '{secret_key}' at the very beginning.")
|
| 28 |
+
print(f"Query: At the very end.")
|
| 29 |
+
print("-" * 30)
|
| 30 |
+
|
| 31 |
+
# 2. Generation
|
| 32 |
+
input_bytes = list(full_prompt.encode('utf-8'))
|
| 33 |
+
# Pad
|
| 34 |
+
pad = (4 - len(input_bytes) % 4) % 4
|
| 35 |
+
input_bytes.extend([32]*pad)
|
| 36 |
+
|
| 37 |
+
generated = input_bytes[:]
|
| 38 |
+
|
| 39 |
+
print("Generating answer...", end=" ", flush=True)
|
| 40 |
+
|
| 41 |
+
with torch.no_grad():
|
| 42 |
+
# Sadece cevabı üretmek için 10 byte (2-3 patch) yeterli
|
| 43 |
+
for _ in range(3):
|
| 44 |
+
# context = generated[-2048:] # ESKİ: Slicing hafızayı siliyordu
|
| 45 |
+
context = generated # YENİ: Tüm geçmişi ver, Hebbian Memory (Linear Attention) halleder.
|
| 46 |
+
curr_tensor = torch.tensor(context, dtype=torch.long).unsqueeze(0).to(DEVICE)
|
| 47 |
+
|
| 48 |
+
# Greedy decoding (Temperature 0) - Hafızayı test ediyoruz, yaratıcılığı değil
|
| 49 |
+
pred_patches = model(curr_tensor, temperature=0.0)
|
| 50 |
+
last_patch = pred_patches[0, -1, :].cpu().tolist()
|
| 51 |
+
generated.extend(last_patch)
|
| 52 |
+
|
| 53 |
+
# 3. Sonuç Analizi
|
| 54 |
+
full_text = bytes(generated).decode('utf-8', errors='replace')
|
| 55 |
+
answer = full_text[len(full_prompt):].strip()
|
| 56 |
+
|
| 57 |
+
print(f"\n\nModel Output: '{answer}'")
|
| 58 |
+
|
| 59 |
+
if secret_key in answer:
|
| 60 |
+
print("\n✅ SUCCESS: Memory retained the information!")
|
| 61 |
+
else:
|
| 62 |
+
print("\n❌ FAILURE: Information lost in noise.")
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
# Model eğitimi bitince çalıştırılacak
|
| 66 |
+
if os.path.exists("best_model_turkish.pth"):
|
| 67 |
+
run_needle_test("best_model_turkish.pth", noise_len=500)
|
| 68 |
+
else:
|
| 69 |
+
print("Model file not found yet. Wait for training to finish.")
|