#!/usr/bin/env python3
"""Compare cached and full-prefix Pythia logits on fixed supplied text."""
import argparse
import hashlib
import json
from pathlib import Path

import torch
import transformers
from transformers import AutoModelForCausalLM, AutoTokenizer

MODEL = 'EleutherAI/pythia-70m'
REVISION = 'a39f36b100fe8a5377810d56c3f4789b9c53ac42'
PROMPT = 'The capital of France is'


def compare(model, tokenizer, prompt):
    ids = tokenizer.encode(prompt, add_special_tokens=False)
    if not 2 <= len(ids) <= 32:
        raise ValueError('Teksten skal give 2–32 tokens uden automatiske specialtokens.')
    input_ids = torch.tensor([ids], dtype=torch.long)
    past = None
    rows = []
    with torch.inference_mode():
        for i in range(len(ids)):
            cached = model(input_ids[:, i:i+1], past_key_values=past, use_cache=True, output_hidden_states=True)
            past = cached.past_key_values
            full = model(input_ids[:, :i+1], use_cache=False, output_hidden_states=True)
            repeated = model(input_ids[:, :i+1], use_cache=False, output_hidden_states=True)
            a, b, control = cached.logits[0, -1], full.logits[0, -1], repeated.logits[0, -1]
            if not torch.isfinite(a).all() or not torch.isfinite(b).all() or not torch.isfinite(control).all():
                raise ValueError('Modellen gav ugyldige logits.')
            a_top, b_top = int(a.argmax()), int(b.argmax())
            rows.append({'position':i+1,'input_id':ids[i],
                         'input_text':tokenizer.decode([ids[i]],clean_up_tokenization_spaces=False),
                         'cached_top_id':a_top,'full_top_id':b_top,
                         'cached_top_text':tokenizer.decode([a_top],clean_up_tokenization_spaces=False),
                         'full_top_text':tokenizer.decode([b_top],clean_up_tokenization_spaces=False),
                         'cached_top_probability':float(a.softmax(-1)[a_top]),
                         'full_top_probability':float(b.softmax(-1)[b_top]),
                         'max_logit_difference':float((a-b).abs().max()),
                         'repeat_logit_difference':float((b-control).abs().max()),
                         'hidden_max_differences':[float((x[0,-1]-y[0,-1]).abs().max()) for x,y in zip(cached.hidden_states,full.hidden_states)]})
    return {'format':'learning-lab-kv-check-v1','model':MODEL,'revision':REVISION,
            'torch_version':str(torch.__version__),'transformers_version':transformers.__version__,
            'device':str(next(model.parameters()).device), 'dtype':str(next(model.parameters()).dtype),
            'float32_matmul_precision':torch.get_float32_matmul_precision(),
            'script_sha256':hashlib.sha256(Path(__file__).read_bytes()).hexdigest(),
            'prompt':prompt,'rows':rows}


def main():
    p=argparse.ArgumentParser(description=__doc__)
    p.add_argument('--prompt',default=PROMPT)
    p.add_argument('--output',type=Path,default=Path('kv-cache-result.json'))
    p.add_argument('--offline',action='store_true')
    args=p.parse_args()
    if not isinstance(args.prompt,str) or not 1<=len(args.prompt)<=500:
        p.error('Teksten skal have 1–500 tegn.')
    if args.output.exists(): p.error('Outputfilen findes allerede; vælg et nyt navn.')
    torch.set_num_threads(1)
    try:
        tokenizer=AutoTokenizer.from_pretrained(MODEL,revision=REVISION,local_files_only=args.offline)
        model=AutoModelForCausalLM.from_pretrained(MODEL,revision=REVISION,local_files_only=args.offline,attn_implementation='eager').eval()
        result=compare(model,tokenizer,args.prompt)
        with args.output.open('x',encoding='utf-8') as f: json.dump(result,f,ensure_ascii=False,indent=2,allow_nan=False)
    except (OSError,ValueError,RuntimeError) as exc: p.error(str(exc))
    print(f"{len(result['rows'])} positioner; største logitforskel {max(r['max_logit_difference'] for r in result['rows']):.3g}. Gemt: {args.output}")

if __name__=='__main__': main()
