#!/usr/bin/env python3
"""Measure the student's fixed model on every symbol pair with a controlled ablation."""
import argparse
import json
from datetime import datetime, timezone
from pathlib import Path
import torch
import probe_egen_attention as probe
import tjek_attention


def distributions(model, first, second, trace):
    """Measure the full output distributions with and without the same ablation."""
    tokens=torch.tensor([[first,second,probe.transformer.QUERY]])
    def zero_attention(_module,inputs):
        value=inputs[0].clone();value[:,-1,:]=0
        return (value,*inputs[1:])
    model.eval()
    with torch.no_grad():
        baseline=model(tokens)[0][0]
        handle=model.proj.register_forward_pre_hook(zero_attention)
        try:zero=model(tokens)[0][0]
        finally:handle.remove()
        a=torch.log_softmax(baseline.double(),dim=-1);b=torch.log_softmax(zero.double(),dim=-1)
    if not torch.isfinite(a).all() or not torch.isfinite(b).all():raise ValueError('Fordelingerne er ikke endelige.')
    if (int(b.argmax())!=trace['intervention']['zero_prediction'] or
        abs(float(b.exp()[first])-trace['intervention']['zero_probability'])>1e-6 or
        max(abs(float(x)-y) for x,y in zip(a.exp(),trace['probabilities']))>1e-6):
        raise ValueError('Gentaget måling stemmer ikke med attention-indgrebet.')
    return {'base_log_probs':a.tolist(),'zero_log_probs':b.tolist()}


def main():
    p=argparse.ArgumentParser(description=__doc__);p.add_argument('--run-dir',required=True,type=Path);p.add_argument('--output',type=Path);a=p.parse_args()
    if a.output and a.output.exists():p.error('Outputfilen findes allerede.')
    try:
        model,report,last,report_sha,model_sha=probe.load_run(a.run_dir)
        traces=[tjek_attention.make_trace(model,i,j,last['step'],last['validation_accuracy'],report_sha,model_sha) for i in range(6) for j in range(6)]
        if any('intervention' not in trace for trace in traces):raise ValueError('Kørslen mangler attention-indgrebet. Træn først med den nyere opgavepakke.')
        for trace in traces:
            trace['fidelity']=distributions(model,int(trace['tokens'][0]),int(trace['tokens'][1]),trace)
        result={'format':'learning-lab-own-attention-sweep-v1','model_sha256':model_sha,'exercise_report_sha256':report_sha,'torch_version':str(torch.__version__),'training_torch_version':report.get('torch_version','ukendt'),'traces':traces}
    except (OSError,UnicodeError,ValueError,RuntimeError,TypeError,AttributeError,NotImplementedError) as exc:p.error(str(exc))
    out=a.output or a.run_dir/('attention-alle-par-'+datetime.now(timezone.utc).strftime('%Y%m%dT%H%M%S_%fZ')+'.json')
    try:
        with out.open('x',encoding='utf-8') as f:json.dump(result,f,ensure_ascii=False,indent=2,allow_nan=False)
    except FileExistsError:p.error('Outputfilen findes allerede.')
    print(f'Målte alle 36 symbolpar med samme model og kontroller. Gemt: {out}')

if __name__=='__main__':main()
