#!/usr/bin/env python3
"""Compare individual and joint head ablations on short real-text sequences."""
import argparse
import hashlib
import json
from pathlib import Path

import torch
import torch.nn.functional as F
import transformers
from transformers import AutoModelForCausalLM, AutoTokenizer
import induction_heads as induction
import induction_joint as joint

TEXTS = [
    'On a rainy afternoon, Maya opened the old book and read the first page.',
    'The scientist checked the measurements twice before writing the result in a notebook.',
    'A small dog waited by the door while the family prepared dinner.',
    'During the experiment, the model predicted the next word from its context.',
    'The train arrived late because heavy snow covered the tracks.',
]


def validate_texts(texts):
    if not isinstance(texts, list) or not 2 <= len(texts) <= 10 or any(not isinstance(t, str) or not 4 <= len(t) <= 500 for t in texts) or len(set(texts)) != len(texts):
        raise ValueError('Brug 2–10 forskellige tekster med 4–500 tegn hver.')


def measure(model, ids, heads):
    width = model.config.hidden_size // model.config.num_attention_heads
    by_layer = {}
    for layer, head in heads:
        by_layer.setdefault(layer, []).append(head)
    handles = []
    try:
        for layer, selected in by_layer.items():
            def zero_heads(_module, inputs, selected=tuple(selected)):
                combined = inputs[0].clone()
                for head in selected:
                    combined[..., head * width:(head + 1) * width] = 0
                return (combined, *inputs[1:])
            handles.append(model.gpt_neox.layers[layer].attention.dense.register_forward_pre_hook(zero_heads))
        with torch.inference_mode():
            logits = model(ids, use_cache=False).logits
        return F.cross_entropy(logits[0, :-1], ids[0, 1:], reduction='none').tolist()
    finally:
        for handle in handles:
            handle.remove()


def experiment(model, tokenizer, head_a, head_b, texts):
    joint.validate(head_a, head_b, [0, 1])
    validate_texts(texts)
    rows = []
    for text in texts:
        ids = tokenizer(text, return_tensors='pt', add_special_tokens=False)['input_ids']
        if not 3 <= ids.shape[-1] <= 128:
            raise ValueError('Hver tekst skal give 3–128 tokens med Pythias tokenizer.')
        rows.append({'text': text, 'token_ids': ids[0].tolist(),
                     'baseline': measure(model, ids, []),
                     'head_a': measure(model, ids, [head_a]),
                     'head_b': measure(model, ids, [head_b]),
                     'both': measure(model, ids, [head_a, head_b])})
    return {'format': 'learning-lab-induction-joint-text-v1', 'model': induction.MODEL,
            'revision': induction.REVISION, 'head_a': head_a, 'head_b': head_b,
            'torch_version': str(torch.__version__), 'transformers_version': transformers.__version__,
            'script_sha256': hashlib.sha256(Path(__file__).read_bytes()).hexdigest(),
            'sequence_sha256': hashlib.sha256(Path(induction.__file__).read_bytes()).hexdigest(),
            'rows': rows}


def main():
    p = argparse.ArgumentParser(description=__doc__)
    p.add_argument('--head-a', type=int, nargs=2, default=[3, 6], metavar=('LAG', 'HEAD'))
    p.add_argument('--head-b', type=int, nargs=2, default=[0, 0], metavar=('LAG', 'HEAD'))
    p.add_argument('--texts', type=Path, help='UTF-8 JSON-fil med en liste af 2–10 korte tekster')
    p.add_argument('--output', type=Path, default=Path('induction-joint-text.json'))
    p.add_argument('--offline', action='store_true')
    args = p.parse_args()
    try:
        texts = json.loads(args.texts.read_text(encoding='utf-8')) if args.texts else TEXTS
        validate_texts(texts)
        joint.validate(args.head_a, args.head_b, [0, 1])
    except (ValueError, OSError) as exc:
        p.error(str(exc))
    if args.output.exists():
        p.error('Outputfilen findes allerede. Vælg et andet navn.')
    torch.set_num_threads(1)
    tokenizer = AutoTokenizer.from_pretrained(induction.MODEL, revision=induction.REVISION, local_files_only=args.offline, trust_remote_code=False)
    model = AutoModelForCausalLM.from_pretrained(induction.MODEL, revision=induction.REVISION, local_files_only=args.offline, attn_implementation='eager').eval()
    try:
        result = experiment(model, tokenizer, args.head_a, args.head_b, texts)
    except ValueError as exc:
        p.error(str(exc))
    with args.output.open('x', encoding='utf-8') as out:
        json.dump(result, out, ensure_ascii=False, indent=2, allow_nan=False)
        out.write('\n')
    print(f"Sammenlignede {args.head_a} og {args.head_b} på {len(texts)} tekster; gemt: {args.output}")


if __name__ == '__main__':
    main()
