Download compare_clean_deltas.py from Ilikemechuri/diloco_floating_point: direct link, hf CLI and curl.
- Browser
- Download file 2.68 kB
-
https://huggingface.co/Ilikemechuri/diloco_floating_point/resolve/main/compare_clean_deltas.py
- Command line
-
hf download hf://Ilikemechuri/diloco_floating_point/compare_clean_deltas.py
-
curl -L -o compare_clean_deltas.py https://huggingface.co/Ilikemechuri/diloco_floating_point/resolve/main/compare_clean_deltas.py
2.68 kB
| #!/usr/bin/env python3 | |
| """Compare two clean safetensors deltas without loading both models into RAM.""" | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import torch | |
| from safetensors import safe_open | |
| def compare(single_path: Path, multi_path: Path): | |
| dot = norm_single_sq = norm_multi_sq = diff_sq = 0.0 | |
| count = 0 | |
| with safe_open(str(single_path), framework="pt", device="cpu") as single, \ | |
| safe_open(str(multi_path), framework="pt", device="cpu") as multi: | |
| single_keys, multi_keys = set(single.keys()), set(multi.keys()) | |
| if single_keys != multi_keys: | |
| missing = sorted(single_keys - multi_keys) | |
| extra = sorted(multi_keys - single_keys) | |
| raise ValueError(f"Parameter keys differ; missing in multi={missing[:5]}, extra={extra[:5]}") | |
| for key in sorted(single_keys): | |
| a = single.get_tensor(key) | |
| b = multi.get_tensor(key) | |
| if a.shape != b.shape: | |
| raise ValueError(f"Shape mismatch for {key}: {tuple(a.shape)} vs {tuple(b.shape)}") | |
| af, bf = a.reshape(-1), b.reshape(-1) | |
| chunk_size = 1_000_000 | |
| for start in range(0, af.numel(), chunk_size): | |
| x = af[start:start + chunk_size].to(torch.float64) | |
| y = bf[start:start + chunk_size].to(torch.float64) | |
| diff = x - y | |
| dot += torch.dot(x, y).item() | |
| norm_single_sq += torch.dot(x, x).item() | |
| norm_multi_sq += torch.dot(y, y).item() | |
| diff_sq += torch.dot(diff, diff).item() | |
| count += x.numel() | |
| single_norm = norm_single_sq ** 0.5 | |
| multi_norm = norm_multi_sq ** 0.5 | |
| l2 = diff_sq ** 0.5 | |
| return { | |
| "single_file": single_path.name, | |
| "multi_file": multi_path.name, | |
| "parameter_count": count, | |
| "single_delta_l2": single_norm, | |
| "multi_delta_l2": multi_norm, | |
| "cosine_similarity": dot / (single_norm * multi_norm) if single_norm and multi_norm else None, | |
| "l2_distance": l2, | |
| "relative_l2_to_single": l2 / single_norm if single_norm else None, | |
| } | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("single_delta", type=Path) | |
| parser.add_argument("multi_delta", type=Path) | |
| parser.add_argument("--output", type=Path, default=Path("clean_single_vs_multi_comparison.json")) | |
| args = parser.parse_args() | |
| result = compare(args.single_delta, args.multi_delta) | |
| args.output.write_text(json.dumps(result, indent=2) + "\n") | |
| print(json.dumps(result, indent=2)) | |
| print(f"Saved: {args.output}") | |
| if __name__ == "__main__": | |
| main() | |