#!/usr/bin/env python3
"""Compare zero and training-mean replacement of tiny transformer components."""
import argparse
import hashlib
import json
import math
from datetime import datetime, timezone
from pathlib import Path

import torch
from transformer import TinyTransformer, VOCAB, QUERY, data


def forward_with_hooks(model, tokens, attention=False, mlp=False, identity=False, attention_reference=None, mlp_reference=None):
    handles = []
    def replace(reference):
        def change(_module, _args, output):
            result = output.clone()
            if not identity:
                result[:, -1, :] = 0 if reference is None else reference
            return result
        return change
    try:
        if attention or identity:
            handles.append(model.proj.register_forward_hook(replace(attention_reference)))
        if mlp or identity:
            handles.append(model.mlp.register_forward_hook(replace(mlp_reference)))
        with torch.no_grad():
            logits, _ = model(tokens)
        return logits.detach()
    finally:
        for handle in handles:
            handle.remove()


def training_means(model):
    train,_=data();captured={}
    def save(name):
        def capture(_module,_args,output):captured[name]=output[:,-1,:].detach().clone()
        return capture
    handles=[model.proj.register_forward_hook(save('attention')),model.mlp.register_forward_hook(save('mlp'))]
    try:
        with torch.no_grad():model(train[0])
    finally:
        for handle in handles:handle.remove()
    return {name:values.mean(dim=0) for name,values in captured.items()}


def investigate(model):
    tokens = torch.tensor([[a,b,QUERY] for a in range(VOCAB) for b in range(VOCAB)], dtype=torch.long)
    with torch.no_grad(): baseline, _ = model(tokens)
    means=training_means(model)
    modes = {'identity':forward_with_hooks(model,tokens,identity=True),
             'attention':forward_with_hooks(model,tokens,attention=True),
             'mlp':forward_with_hooks(model,tokens,mlp=True),
             'both':forward_with_hooks(model,tokens,attention=True,mlp=True),
             'mean_attention':forward_with_hooks(model,tokens,attention=True,attention_reference=means['attention']),
             'mean_mlp':forward_with_hooks(model,tokens,mlp=True,mlp_reference=means['mlp']),
             'mean_both':forward_with_hooks(model,tokens,attention=True,mlp=True,attention_reference=means['attention'],mlp_reference=means['mlp'])}
    if not all(torch.isfinite(x).all() for x in [baseline,*modes.values()]):
        raise ValueError('Modellen producerede ikke-endelige logits.')
    rows=[]
    for i,(first,second,_) in enumerate(tokens.tolist()):
        rows.append({'first':first,'second':second,'split':'validation' if (first+second)%4==0 else 'train',
                     'baseline':baseline[i].tolist(),**{name:value[i].tolist() for name,value in modes.items()}})
    return rows,means


def main():
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--model',required=True,type=Path);parser.add_argument('--output-dir',type=Path);args=parser.parse_args()
    if not args.model.is_file():parser.error(f'Modelfilen findes ikke: {args.model}')
    if args.output_dir and args.output_dir.exists():parser.error(f'Outputmappen findes allerede: {args.output_dir}')
    torch.set_num_threads(1);model=TinyTransformer()
    try:model.load_state_dict(torch.load(args.model,map_location='cpu',weights_only=True))
    except Exception as exc:parser.error(f'Kunne ikke indlæse modelvægte: {exc}')
    model.eval();rows,means=investigate(model);digest=hashlib.sha256(args.model.read_bytes()).hexdigest()
    result={'format':'learning-lab-component-ablation-v2','model_sha256':digest,'torch_version':str(torch.__version__),'mean_reference':{'split':'train','count':27,'attention':means['attention'].tolist(),'mlp':means['mlp'].tolist()},'rows':rows}
    output=args.output_dir or Path('component-ablation-runs')/datetime.now(timezone.utc).strftime('%Y%m%dT%H%M%S_%fZ')
    try:output.mkdir(parents=True,exist_ok=False)
    except FileExistsError:parser.error(f'Outputmappen findes allerede: {output}')
    (output/'components.json').write_text(json.dumps(result,ensure_ascii=False,indent=2),encoding='utf-8')
    identity=max(abs(a-b) for row in rows for a,b in zip(row['baseline'],row['identity']))
    print(f'{len(rows)} inputpar. Identitetsafvigelse: {identity:.3g}. Gemt i {output / "components.json"}.')


if __name__=='__main__':main()
