suchirsalhan commited on
Commit
0389a3d
·
verified ·
1 Parent(s): fa5e404

Upload code/gpt2_align.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. code/gpt2_align.py +15 -0
code/gpt2_align.py CHANGED
@@ -123,3 +123,18 @@ def align_full(sd_a, sd_b, d, nh, acts_a=None, acts_b=None, method="permutation"
123
  hp = head_match(sd_a, sd, d, nh)
124
  sd, ok = keep(apply_head(sd, hp, d, nh), "heads"); info["heads"] = len(hp) if ok else 0
125
  return sd, info
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  hp = head_match(sd_a, sd, d, nh)
124
  sd, ok = keep(apply_head(sd, hp, d, nh), "heads"); info["heads"] = len(hp) if ok else 0
125
  return sd, info
126
+
127
+
128
+ # --------------------------------------------------------------- embedding-row Procrustes
129
+ def emb_procrustes(sd_a, sd_b, tok_a, tok_b, key="transformer.wte.weight", max_anchors=None):
130
+ """Fit the residual-basis rotation from the EMBEDDING ROWS of shared surface forms, data-free.
131
+
132
+ Rows of the two embedding tables that spell the same string are the one correspondence two
133
+ monolingual tokenizers genuinely share, so `min_R ||E_a[anchors] - E_b[anchors] R||` is a
134
+ legitimate estimate of the basis map with no corpus and no forward pass. Returns (R, n_anchors).
135
+ """
136
+ anchors = AL.vocab_anchors(tok_a, tok_b, max_anchors)
137
+ ia = [i for i, _ in anchors]; ib = [j for _, j in anchors]
138
+ Ea = np.asarray(sd_a[key], float)[ia]
139
+ Eb = np.asarray(sd_b[key], float)[ib]
140
+ return AL.procrustes_align(Ea, Eb)["R"], len(anchors)