Upload code/nci_mlx_patch.py with huggingface_hub
Browse files- code/nci_mlx_patch.py +90 -0
code/nci_mlx_patch.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""NCI Experiment on Rushd-Geo via MLX — monkey-patch approach"""
|
| 2 |
+
import os, json, sys, time
|
| 3 |
+
os.environ["WANDB_DISABLED"] = "true"
|
| 4 |
+
|
| 5 |
+
import mlx.core as mx
|
| 6 |
+
import numpy as np
|
| 7 |
+
from mlx_lm import load
|
| 8 |
+
from mlx_lm.models.qwen3_5 import DecoderLayer
|
| 9 |
+
|
| 10 |
+
MODEL = "/Users/ai/rushd-geo-mlx-4bit"
|
| 11 |
+
N_LAYERS = 64
|
| 12 |
+
|
| 13 |
+
# Monkey-patch: wrap DecoderLayer.__call__ to capture hidden states
|
| 14 |
+
original_call = DecoderLayer.__call__
|
| 15 |
+
hidden_states_collector = []
|
| 16 |
+
|
| 17 |
+
def patched_call(self, x, mask=None, cache=None):
|
| 18 |
+
global hidden_states_collector
|
| 19 |
+
result = original_call(self, x, mask=mask, cache=cache)
|
| 20 |
+
hidden_states_collector.append(result)
|
| 21 |
+
return result
|
| 22 |
+
|
| 23 |
+
DecoderLayer.__call__ = patched_call
|
| 24 |
+
|
| 25 |
+
print(f"Loading model from {MODEL}...", flush=True)
|
| 26 |
+
t0 = time.time()
|
| 27 |
+
model, tokenizer = load(MODEL)
|
| 28 |
+
print(f"Loaded in {time.time()-t0:.1f}s", flush=True)
|
| 29 |
+
print(f"Layers: {len(model.layers)}", flush=True)
|
| 30 |
+
|
| 31 |
+
# Test prompts
|
| 32 |
+
prompts = [
|
| 33 |
+
("meaningful", "تحليل الوضع الجيوسياسي في الشرق الأوسط بعد اتفاقيات التطبيع."),
|
| 34 |
+
("nonsense", "jdska flpz xqwy bnmzx cvbnm lkjh"),
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
def compute_nci(hs_list, layer_a, layer_b):
|
| 38 |
+
n_a = hs_list[layer_a].mean(axis=1).squeeze(0)
|
| 39 |
+
n_b = hs_list[layer_b].mean(axis=1).squeeze(0)
|
| 40 |
+
cos = mx.sum(n_a * n_b) / (mx.linalg.norm(n_a) * mx.linalg.norm(n_b))
|
| 41 |
+
return float(cos)
|
| 42 |
+
|
| 43 |
+
def compute_norm(hs_list, layer):
|
| 44 |
+
n = hs_list[layer].mean(axis=1).squeeze(0)
|
| 45 |
+
return float(mx.linalg.norm(n))
|
| 46 |
+
|
| 47 |
+
print(f"\n{'Prompt':<12} {'Norm L6':<10} {'Norm L32':<10} {'NCI(6,32)':<12} {'NCI(6,54)':<12}", flush=True)
|
| 48 |
+
print("-" * 60, flush=True)
|
| 49 |
+
|
| 50 |
+
for label, text in prompts:
|
| 51 |
+
hidden_states_collector.clear()
|
| 52 |
+
|
| 53 |
+
tokens = list(tokenizer.encode(text))
|
| 54 |
+
input_ids = mx.array([tokens])
|
| 55 |
+
|
| 56 |
+
t0 = time.time()
|
| 57 |
+
logits = model(input_ids)
|
| 58 |
+
elapsed = time.time() - t0
|
| 59 |
+
|
| 60 |
+
hs = hidden_states_collector # 64 layers
|
| 61 |
+
# hs[0] = layer 1 output, hs[63] = layer 64 output
|
| 62 |
+
|
| 63 |
+
nci_6_32 = compute_nci(hs, 6, 32)
|
| 64 |
+
nci_6_54 = compute_nci(hs, 6, 54)
|
| 65 |
+
norm_6 = compute_norm(hs, 6)
|
| 66 |
+
norm_32 = compute_norm(hs, 32)
|
| 67 |
+
|
| 68 |
+
print(f"{label:<12} {norm_6:<10.1f} {norm_32:<10.1f} {nci_6_32:<12.4f} {nci_6_54:<12.4f}", flush=True)
|
| 69 |
+
print(f"{'':12} tokens={len(tokens)} time={elapsed:.2f}s", flush=True)
|
| 70 |
+
|
| 71 |
+
# Full NCI profile
|
| 72 |
+
print(f"\n=== Full NCI Profile (meaningful) ===", flush=True)
|
| 73 |
+
text = "تحليل الوضع الجيوسياسي في الشرق الأوسط."
|
| 74 |
+
hidden_states_collector = []
|
| 75 |
+
tokens = list(tokenizer.encode(text))
|
| 76 |
+
input_ids = mx.array([tokens])
|
| 77 |
+
logits = model(input_ids)
|
| 78 |
+
hs = hidden_states_collector
|
| 79 |
+
|
| 80 |
+
print(f"{'Layer':<6} {'NCI(L6,Ln)':<14} {'Norm':<12}", flush=True)
|
| 81 |
+
print("-" * 35, flush=True)
|
| 82 |
+
for i in range(N_LAYERS):
|
| 83 |
+
nci = compute_nci(hs, 6, i) if i < len(hs) else 0
|
| 84 |
+
norm_i = compute_norm(hs, i) if i < len(hs) else 0
|
| 85 |
+
mark = ""
|
| 86 |
+
if i in [0, 3, 6, 12, 18, 24, 30, 32, 40, 48, 54, 60, 63]:
|
| 87 |
+
mark = " ★"
|
| 88 |
+
print(f"L{i:<4} {nci:<14.4f} {norm_i:<12.1f}{mark}", flush=True)
|
| 89 |
+
|
| 90 |
+
print("\nDone!", flush=True)
|