Upload code/gmap.py with huggingface_hub
Browse files- code/gmap.py +34 -1
code/gmap.py
CHANGED
|
@@ -8,9 +8,16 @@ each factor accepted only if it does not increase the scale-free block-normalise
|
|
| 8 |
reference -- the identity is in every one of these groups, so min_g must range over it.
|
| 9 |
"""
|
| 10 |
from __future__ import annotations
|
| 11 |
-
import sys, time
|
| 12 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
sys.path.insert(0, "/root/mergeability/src")
|
|
|
|
|
|
|
| 14 |
from mergeschool.core import alignment as AL
|
| 15 |
|
| 16 |
|
|
@@ -23,6 +30,32 @@ def _fast_assignment(gain):
|
|
| 23 |
am = np.argmax(gain, axis=1)
|
| 24 |
if len(np.unique(am)) == gain.shape[0]:
|
| 25 |
return am, "argmax_exact"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
from scipy.optimize import linear_sum_assignment
|
| 27 |
r, c = linear_sum_assignment(-gain)
|
| 28 |
return c[np.argsort(r)], "hungarian"
|
|
|
|
| 8 |
reference -- the identity is in every one of these groups, so min_g must range over it.
|
| 9 |
"""
|
| 10 |
from __future__ import annotations
|
| 11 |
+
import os, sys, time
|
| 12 |
import numpy as np
|
| 13 |
+
# VENDORED SNAPSHOT of mergeschool.core. /root/mergeability is another agent's live working tree
|
| 14 |
+
# and it is being edited concurrently -- two runs of this study died mid-flight with
|
| 15 |
+
# "ImportError: cannot import name 'merge' from 'mergeschool.core' (unknown location)" while its
|
| 16 |
+
# package __init__ was mid-rewrite. We take a frozen copy at /root/merge-accuracy/vendor and fall
|
| 17 |
+
# back to the original only if the copy is missing. /root/mergeability is never written to.
|
| 18 |
sys.path.insert(0, "/root/mergeability/src")
|
| 19 |
+
if os.path.isdir("/root/merge-accuracy/vendor/mergeschool"):
|
| 20 |
+
sys.path.insert(0, "/root/merge-accuracy/vendor")
|
| 21 |
from mergeschool.core import alignment as AL
|
| 22 |
|
| 23 |
|
|
|
|
| 30 |
am = np.argmax(gain, axis=1)
|
| 31 |
if len(np.unique(am)) == gain.shape[0]:
|
| 32 |
return am, "argmax_exact"
|
| 33 |
+
if os.environ.get("MA_FAST_ASSIGN") == "1":
|
| 34 |
+
# CONFLICT REPAIR. The row-wise argmax attains the row-wise upper bound, so every row whose
|
| 35 |
+
# choice is unique is already at its optimum and can be frozen. Only the rows that collided
|
| 36 |
+
# need a real assignment, and they are solved exactly on the (tiny) submatrix of contested
|
| 37 |
+
# rows x still-free columns. n = 14336 makes a full Hungarian minutes-to-hours; the contested
|
| 38 |
+
# set here is a handful of rows. Not provably globally optimal, but it dominates the greedy
|
| 39 |
+
# fallback and matches the full solve on every case we checked.
|
| 40 |
+
n = gain.shape[0]
|
| 41 |
+
first, dup_rows = {}, []
|
| 42 |
+
for i, j in enumerate(am):
|
| 43 |
+
if j in first:
|
| 44 |
+
dup_rows.append(i)
|
| 45 |
+
else:
|
| 46 |
+
first[j] = i
|
| 47 |
+
free_cols = np.array(sorted(set(range(n)) - set(first.keys())), dtype=int)
|
| 48 |
+
rows = np.array(dup_rows, dtype=int)
|
| 49 |
+
perm = np.empty(n, dtype=int)
|
| 50 |
+
for j, i in first.items():
|
| 51 |
+
perm[i] = j
|
| 52 |
+
if len(rows):
|
| 53 |
+
from scipy.optimize import linear_sum_assignment
|
| 54 |
+
sub = gain[np.ix_(rows, free_cols)]
|
| 55 |
+
r, c = linear_sum_assignment(-sub)
|
| 56 |
+
for ri, ci in zip(r, c):
|
| 57 |
+
perm[rows[ri]] = free_cols[ci]
|
| 58 |
+
return perm, f"argmax_repair({len(rows)})"
|
| 59 |
from scipy.optimize import linear_sum_assignment
|
| 60 |
r, c = linear_sum_assignment(-gain)
|
| 61 |
return c[np.argsort(r)], "hungarian"
|