Compare isolated layer sharing against the native lookup model

This commit is contained in:
2026-12-02 15:56:05 +08:00
parent 41895ae357
commit 80f79f7d15
4 changed files with 108 additions and 4 deletions
+15 -2
View File
@@ -20,6 +20,12 @@ from functional.model import PREFIX, StreamingLM
from functional.run import write_json
# Vary only layers 15 and 19 with native attention at layer 23 in all four arms.
ISOLATION_SCHEDULES = {
'native': (), 'only-15': (15,), 'only-19': (19,), 'both-15-19': (15, 19)
}
class PathDecoder(GenerationDecoder):
def score_paths(self, h, layer, positions, scores):
if layer != self.site or self.mode == 'baseline':
@@ -66,8 +72,13 @@ def batch(model, tok, adapters, qs, site, mode):
ids = [tok.encode(prompt(q['question']),add_special_tokens=False).ids for q in qs]
width = max(map(len,ids))
padding = [width-len(x) for x in ids]
dec = PathDecoder(model,[SITES]*len(qs),padding,adapters)
dec.site, dec.mode = site, mode
if mode in ISOLATION_SCHEDULES:
schedule = ISOLATION_SCHEDULES[mode]
dec = GenerationDecoder(model, [schedule]*len(qs), padding,
{layer: adapters[layer] for layer in schedule})
else:
dec = PathDecoder(model,[SITES]*len(qs),padding,adapters)
dec.site, dec.mode = site, mode
h = dec.advance(torch.tensor([[0]*p+x for p,x in zip(padding,ids)]))[:,-1]
head = model.tensors[PREFIX+'embed_tokens.weight']
gold = torch.tensor([tok.encode(str(q['answer']),add_special_tokens=False).ids[0] for q in qs])
@@ -147,6 +158,8 @@ def main():
if not a.follow_through:
return
evaluate('diagnostic', 'isolation', ISOLATION_SCHEDULES)
def diagnostic_score(mode):
joint = total = 0
for kind, items in plan['diagnostic'].items():
+22 -2
View File
@@ -61,7 +61,8 @@ def main():
assert fingerprint(args.directory/'protocol.json') == 'c309f8721534b1f229a00ff556b48495c4e074be45c1905389aa57e9e3ddb048'
def read(name):
return json.loads((args.directory/name).read_text())
questions = read('protocol.json')['holdout']
plan = read('protocol.json')
questions = plan['holdout']
modes = read('selection.json')['holdout_modes']
n = len(questions['swap']) // 2
assert n >= 32 and len(questions['query']) == 2*n
@@ -108,9 +109,28 @@ def main():
sum(c*arms[m][i][k] for m, c in coefficients.items()) for i in range(n)
], resamples) for k in fields}
summary[kind] = dict(means=estimates, paired_contrasts=comparisons)
isolation = {}
for kind, prompts in plan['diagnostic'].items():
arms = {}
for mode in ('native', 'only-15', 'only-19', 'both-15-19'):
rows = read(f'diagnostic-isolation-{mode}-{kind}.json')
verify(tokenizer, protocol, prompts, rows, (mode,))
arms[mode], _ = table_metrics(prompts, rows, mode)
count = len(prompts)//2
diagnostic_rng = random.Random(120205)
samples = [diagnostic_rng.choices(range(count), k=count) for _ in range(10000)]
combinations = {
'only-15-minus-native': {'only-15': 1, 'native': -1},
'only-19-minus-native': {'only-19': 1, 'native': -1},
'joint-interaction': {'both-15-19': 1, 'only-19': -1, 'only-15': -1, 'native': 1}}
isolation[kind] = {
'means': {m: {k: mean(row[k] for row in arm) for k in fields} for m, arm in arms.items()},
'contrasts': {name: {k: interval([
sum(c*arms[m][i][k] for m,c in coefficients.items()) for i in range(count)
], samples) for k in fields} for name, coefficients in combinations.items()}}
write_json(args.directory/'audit.json', dict(
tables=n, bootstrap_seed=120204, bootstrap_replicates=len(resamples),
summary=summary, answers=detail, costs=costs))
summary=summary, answers=detail, costs=costs, diagnostic_isolation=isolation))
if __name__ == '__main__':