#!/usr/bin/env python3
"""Intervene on a trained TinyTransformer attention output using forward hooks."""
import argparse
import json
from datetime import datetime, timezone
from pathlib import Path

import torch
from transformer import TinyTransformer, VOCAB, QUERY


def capture_projection(model, tokens):
    captured = []
    def hook(_module, _args, output):
        captured.append(output.detach().clone())
    handle = model.proj.register_forward_hook(hook)
    try:
        with torch.no_grad(): logits, _ = model(tokens)
    finally:
        handle.remove()
    if len(captured) != 1:
        raise RuntimeError('Forventede ét kald til attention-projektionen.')
    return logits.detach(), captured[0]


def intervened_logits(model, target_tokens, replacement, position):
    def patch(_module, _args, output):
        new_output = output.clone()
        new_output[:, position, :] = replacement[:, position, :]
        return new_output
    handle = model.proj.register_forward_hook(patch)
    try:
        with torch.no_grad(): logits, _ = model(target_tokens)
    finally:
        handle.remove()
    return logits.detach()


def run_experiment(model, source, target, second):
    if source == target:
        raise ValueError('Kilde og modtager skal have forskellige første symboler.')
    source_tokens = torch.tensor([[source, second, QUERY]], dtype=torch.long)
    target_tokens = torch.tensor([[target, second, QUERY]], dtype=torch.long)
    a, a_projection = capture_projection(model, source_tokens)
    b, b_projection = capture_projection(model, target_tokens)
    identity = intervened_logits(model, target_tokens, b_projection, -1)
    patch_final = intervened_logits(model, target_tokens, a_projection, -1)
    patch_early = intervened_logits(model, target_tokens, a_projection, 0)
    def describe(logits):
        return {'logit_difference':float(logits[0,source]-logits[0,target]),
                'prediction':int(logits.argmax(-1)[0]),
                'probabilities':logits.softmax(-1)[0].tolist()}
    return {'source':describe(a),'target':describe(b),'identity':describe(identity),
            'patch_final':describe(patch_final),'patch_early':describe(patch_early)}


def natural_reference(model, second):
    """Six ordinary runs at the same position, second symbol and query."""
    return torch.cat([capture_projection(model, torch.tensor([[first, second, QUERY]], dtype=torch.long))[1][:, -1, :]
                      for first in range(VOCAB)], dim=0)


def interpolate_experiment(model, source, target, second, reference=None):
    """Interpolate the final-position projected attention output, then rerun the model."""
    a_tokens = torch.tensor([[source, second, QUERY]], dtype=torch.long)
    b_tokens = torch.tensor([[target, second, QUERY]], dtype=torch.long)
    _, a_projection = capture_projection(model, a_tokens)
    _, b_projection = capture_projection(model, b_tokens)
    # Reference set: all six first symbols, with the second symbol and query fixed.
    if reference is None:
        reference = natural_reference(model, second)
    rows = []
    for alpha in (0.0, 0.25, 0.5, 0.75, 1.0):
        mixture = b_projection if alpha == 0 else a_projection if alpha == 1 else b_projection + alpha * (a_projection - b_projection)
        logits = intervened_logits(model, b_tokens, mixture, -1)
        distances = torch.linalg.vector_norm(reference - mixture[0, -1, :], dim=-1)
        nearest = int(distances.argmin())
        rows.append({'alpha': alpha, 'nearest_natural_distance': float(distances[nearest]), 'nearest_natural_first': nearest,
                     'logit_difference': float(logits[0, source] - logits[0, target]),
                     'prediction': int(logits.argmax(-1)[0]),
                     'source_probability': float(logits.softmax(-1)[0, source])})
    return rows


def main():
    parser=argparse.ArgumentParser(description='Undersøg et kausalt indgreb i den lille transformer.')
    parser.add_argument('--model',type=Path,required=True,help='Sti til model.pt fra transformer.py')
    parser.add_argument('--source',type=int,default=0);parser.add_argument('--target',type=int,default=1);parser.add_argument('--second',type=int,default=3)
    parser.add_argument('--output-dir',type=Path)
    args=parser.parse_args()
    if any(not 0 <= x < VOCAB for x in (args.source,args.target,args.second)) or args.source==args.target:
        parser.error('Symboler skal være 0–5, og source og target skal være forskellige.')
    if not args.model.is_file():parser.error(f'Modelfilen findes ikke: {args.model}')
    torch.set_num_threads(1);model=TinyTransformer()
    # Load only model weights, never a serialized arbitrary Python object.
    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ægtene: {exc}')
    model.eval()
    result=run_experiment(model,args.source,args.target,args.second)
    for name in ['source','target','identity','patch_early','patch_final']:
        print(f'{name:12s} | logit-forskel {result[name]["logit_difference"]:+.4f} | valgt symbol {result[name]["prediction"]}')
    output=args.output_dir or Path('patch-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}')
    reference=natural_reference(model,args.second)
    pairwise=[float(torch.linalg.vector_norm(reference[i]-reference[j])) for i in range(VOCAB) for j in range(i+1,VOCAB)]
    report={'format':'learning-lab-transformer-patch-v1','source_token':args.source,'target_token':args.target,'second_token':args.second,'intervention':'post_projection_final_position','results':result,'interpolation':interpolate_experiment(model,args.source,args.target,args.second,reference),'natural_pairwise_distances':pairwise}
    (output/'patch.json').write_text(json.dumps(report,ensure_ascii=False,indent=2),encoding='utf-8')
    print(f'Resultatet er gemt i {output / "patch.json"}.')


if __name__=='__main__':main()
