Update AGIFORMER with Turkish benchmark
Browse files- inspect_reasoning.py +96 -0
inspect_reasoning.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## Developer: inkbytefo
|
| 2 |
+
## Modified: 2025-11-22
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
import torch.nn.functional as F
|
| 6 |
+
from src.models.agiformer import AGIFORMER
|
| 7 |
+
import os
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
def inspect_system_2(model_path):
|
| 11 |
+
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 12 |
+
|
| 13 |
+
# Config (Train ile aynı)
|
| 14 |
+
D_MODEL = 512
|
| 15 |
+
N_LAYERS = 6
|
| 16 |
+
PATCH_SIZE = 4
|
| 17 |
+
THINKING_STEPS = 3
|
| 18 |
+
|
| 19 |
+
print(f"Inspecting {model_path} on {DEVICE}...")
|
| 20 |
+
|
| 21 |
+
model = AGIFORMER(
|
| 22 |
+
d_model=D_MODEL,
|
| 23 |
+
n_layers=N_LAYERS,
|
| 24 |
+
patch_size=PATCH_SIZE,
|
| 25 |
+
thinking_steps=THINKING_STEPS
|
| 26 |
+
).to(DEVICE)
|
| 27 |
+
|
| 28 |
+
state_dict = torch.load(model_path, map_location=DEVICE)
|
| 29 |
+
model.load_state_dict(state_dict)
|
| 30 |
+
model.eval()
|
| 31 |
+
|
| 32 |
+
# Hook mekanizması: Reasoning bloğundaki gate ve update değerlerini yakalayalım
|
| 33 |
+
stats = {"gates": [], "updates": [], "z_diff": []}
|
| 34 |
+
|
| 35 |
+
def hook_fn(module, input, output):
|
| 36 |
+
# Input is tuple (x,), output is refined x
|
| 37 |
+
z_in = input[0]
|
| 38 |
+
z_out = output
|
| 39 |
+
|
| 40 |
+
# Measure how much the latent vector changed
|
| 41 |
+
# L2 Distance per token
|
| 42 |
+
diff = torch.norm(z_out - z_in, dim=-1).mean().item()
|
| 43 |
+
stats["z_diff"].append(diff)
|
| 44 |
+
|
| 45 |
+
# We can't easily hook internal variables of the forward loop without modifying the class.
|
| 46 |
+
# Instead, we will manually run the reasoning logic here to inspect.
|
| 47 |
+
|
| 48 |
+
# Register hook on the reasoning block
|
| 49 |
+
handle = model.reasoning.register_forward_hook(hook_fn)
|
| 50 |
+
|
| 51 |
+
# Dummy Input (from enwik8 context)
|
| 52 |
+
dummy_text = "The history of artificial intelligence"
|
| 53 |
+
input_bytes = [ord(c) for c in dummy_text]
|
| 54 |
+
# Pad
|
| 55 |
+
pad = (4 - len(input_bytes) % 4) % 4
|
| 56 |
+
input_bytes.extend([32]*pad)
|
| 57 |
+
|
| 58 |
+
x = torch.tensor(input_bytes, dtype=torch.long).unsqueeze(0).to(DEVICE)
|
| 59 |
+
|
| 60 |
+
with torch.no_grad():
|
| 61 |
+
# Run forward pass triggers the hook
|
| 62 |
+
_ = model(x)
|
| 63 |
+
|
| 64 |
+
# Manual Inspection of Internal Reasoning Weights
|
| 65 |
+
# Check if Gate biases are negative (which would mean closed gate by default)
|
| 66 |
+
gate_bias_mean = model.reasoning.gate.bias.mean().item()
|
| 67 |
+
|
| 68 |
+
print("\n--- SYSTEM 2 DIAGNOSTICS ---")
|
| 69 |
+
print(f"1. Latent Refinement (Thinking Magnitude):")
|
| 70 |
+
print(f" Average Euclidean Distance (z_out - z_in): {np.mean(stats['z_diff']):.4f}")
|
| 71 |
+
print(f" (If close to 0.0, the model is SKIPPING the thinking step.)")
|
| 72 |
+
|
| 73 |
+
print(f"\n2. Gate Bias Statistics:")
|
| 74 |
+
print(f" Mean Bias: {gate_bias_mean:.4f}")
|
| 75 |
+
print(f" (Negative values suggest the model prefers to keep the initial thought.)")
|
| 76 |
+
|
| 77 |
+
print(f"\n3. Parameter Health:")
|
| 78 |
+
mlp_weight_grad = model.reasoning.think_mlp[0].weight.std().item()
|
| 79 |
+
print(f" MLP Weight Std: {mlp_weight_grad:.4f}")
|
| 80 |
+
|
| 81 |
+
# Interpretation
|
| 82 |
+
avg_diff = np.mean(stats['z_diff'])
|
| 83 |
+
if avg_diff < 0.01:
|
| 84 |
+
print("\n[RESULT] SYSTEM 2 IS DORMANT (Collapsed).")
|
| 85 |
+
print("Reason: The model learned that 'not thinking' is safer for loss.")
|
| 86 |
+
elif avg_diff > 10.0:
|
| 87 |
+
print("\n[RESULT] SYSTEM 2 IS UNSTABLE (Exploding).")
|
| 88 |
+
else:
|
| 89 |
+
print("\n[RESULT] SYSTEM 2 IS ACTIVE.")
|
| 90 |
+
print("The model is actively modifying its latent state.")
|
| 91 |
+
|
| 92 |
+
# Cleanup
|
| 93 |
+
handle.remove()
|
| 94 |
+
|
| 95 |
+
if __name__ == "__main__":
|
| 96 |
+
inspect_system_2("best_model.pth")
|