Spaces:
Running
Running
Upload cosine_magnitude_audit.py
Browse files- cosine_magnitude_audit.py +72 -0
cosine_magnitude_audit.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
from safetensors.torch import safe_open
|
| 4 |
+
import yaml
|
| 5 |
+
|
| 6 |
+
# --- CONFIGURATION ---
|
| 7 |
+
YAML_PATH = "B:/24B/qliphoth2e/mergekit_config.yml"
|
| 8 |
+
FINAL_MERGE_DIR = "B:/24B/qliphoth2e"
|
| 9 |
+
LAYERS_TO_SCAN =[
|
| 10 |
+
"model.layers.10.mlp.up_proj.weight",
|
| 11 |
+
"model.layers.20.mlp.gate_proj.weight",
|
| 12 |
+
"model.layers.30.mlp.down_proj.weight"
|
| 13 |
+
]
|
| 14 |
+
# ---------------------
|
| 15 |
+
|
| 16 |
+
def load_tensor(model_dir, tensor_name):
|
| 17 |
+
"""Finds and loads a tensor from a directory of safetensors."""
|
| 18 |
+
for file in os.listdir(model_dir):
|
| 19 |
+
if file.endswith(".safetensors"):
|
| 20 |
+
filepath = os.path.join(model_dir, file)
|
| 21 |
+
with safe_open(filepath, framework="pt", device="cpu") as f:
|
| 22 |
+
if tensor_name in f.keys():
|
| 23 |
+
return f.get_tensor(tensor_name).float()
|
| 24 |
+
raise ValueError(f"Tensor {tensor_name} not found in {model_dir}")
|
| 25 |
+
|
| 26 |
+
def main():
|
| 27 |
+
print("Loading YAML config...")
|
| 28 |
+
with open(YAML_PATH, 'r') as f:
|
| 29 |
+
config = yaml.safe_load(f)
|
| 30 |
+
|
| 31 |
+
base_path = config['base_model']
|
| 32 |
+
donor_paths = [m['model'] for m in config['models']]
|
| 33 |
+
|
| 34 |
+
print(f"\nScanning {len(LAYERS_TO_SCAN)} MLP layers for structural influence...\n")
|
| 35 |
+
|
| 36 |
+
for layer in LAYERS_TO_SCAN:
|
| 37 |
+
print(f"--- Layer: {layer} ---")
|
| 38 |
+
try:
|
| 39 |
+
base_w = load_tensor(base_path, layer)
|
| 40 |
+
final_w = load_tensor(FINAL_MERGE_DIR, layer)
|
| 41 |
+
final_tv = final_w - base_w
|
| 42 |
+
|
| 43 |
+
results =[]
|
| 44 |
+
for donor in donor_paths:
|
| 45 |
+
donor_w = load_tensor(donor, layer)
|
| 46 |
+
donor_tv = donor_w - base_w
|
| 47 |
+
|
| 48 |
+
# Calculate Cosine Similarity (How much does the final model align with this donor?)
|
| 49 |
+
cos_sim = torch.nn.functional.cosine_similarity(
|
| 50 |
+
final_tv.flatten(), donor_tv.flatten(), dim=0
|
| 51 |
+
).item()
|
| 52 |
+
|
| 53 |
+
# Calculate Relative Magnitude
|
| 54 |
+
rel_mag = (donor_tv.norm() / final_tv.norm()).item()
|
| 55 |
+
|
| 56 |
+
name = donor.split("/")[-1][:50]
|
| 57 |
+
results.append((name, cos_sim, rel_mag))
|
| 58 |
+
|
| 59 |
+
# Sort by highest similarity
|
| 60 |
+
results.sort(key=lambda x: x[1], reverse=True)
|
| 61 |
+
|
| 62 |
+
print(f"{'Donor Model':<55} | {'Alignment (Cos Sim)':<20} | {'Relative Mag'}")
|
| 63 |
+
print("-" * 95)
|
| 64 |
+
for name, sim, mag in results:
|
| 65 |
+
print(f"{name:<55} | {sim:>18.4f} | {mag:>10.2f}x")
|
| 66 |
+
print("\n")
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"Skipping layer due to error: {e}")
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
main()
|