"""Measure 640px training throughput on resolved source frames; never score test data."""
from __future__ import annotations

import argparse
from dataclasses import replace
import itertools
import json
from pathlib import Path
import time

import numpy as np
import torch
from torch.utils.data import DataLoader

from pallet.annotation import load_jsonl,is_resolved
from pallet.data.protocol import load_protocol,write_json,file_digest
from pallet.data.loader import fit_normalization
from pallet.devices import resolve_device,device_metadata
from pallet.experiments.dataset import PalletDataset,collate_pallet
from pallet.models import PalletDetector


def measure(annotations,protocol,output,device,*,p2=True,steps=64,batch_size=2):
    records,_,_=load_protocol(protocol)
    labels=load_jsonl(annotations)
    cache=json.loads(Path('outputs/review_v3/cache_index.json').read_text())
    source=[replace(r,cache_path=cache.get(r.frame_id)) for r in records
            if r.frame_id in labels.frames and labels.frames[r.frame_id].split=='train'
            and labels.frames[r.frame_id].status in {'pos','negative'} and is_resolved(labels.frames[r.frame_id])]
    if len(source)<4:raise ValueError('need four resolved source training frames')
    source=source[:4]
    if 16%batch_size:raise ValueError('physical batch must divide effective batch16')
    stats=fit_normalization(source)
    repeated=[source[i%len(source)] for i in range(batch_size*2)]
    dataset=PalletDataset(repeated,labels,stats,size=640)
    loader=DataLoader(dataset,batch_size=batch_size,collate_fn=collate_pallet)
    dev=resolve_device(device)
    if dev.type!='cuda':raise ValueError('GPU throughput requires CUDA')
    model=PalletDetector(1,p2=p2,pretrained='yolo26s.pt').train().to(dev)
    optimizer=torch.optim.AdamW(model.parameters(),lr=.001)
    scaler=torch.amp.GradScaler('cuda')
    optimizer.zero_grad(set_to_none=True)
    batches=iter(loader);durations=[]
    torch.cuda.reset_peak_memory_stats(dev)
    for index in range(8+steps):
        start=time.perf_counter()
        try:
            batch=next(batches)
        except StopIteration:
            batches=iter(loader)
            batch=next(batches)
        values={k:v.to(dev) for k,v in batch.items() if k!='meta'}
        with torch.autocast('cuda'):
            loss,_=model.loss(values)
            value=loss.sum()/16
        if not torch.isfinite(value):raise ValueError('non-finite pilot training loss')
        scaler.scale(value).backward()
        if (index+1)%(16//batch_size)==0:
            scaler.step(optimizer);scaler.update();optimizer.zero_grad(set_to_none=True)
        torch.cuda.synchronize(dev)
        if index>=8:durations.append(time.perf_counter()-start)
    result={'status':'throughput only; small repeated source batch, no accuracy claim',
            'preprocessing':'Re-read compressed float cache and apply native-grid normalization/letterbox each step; filesystem cache warm.',
            'device':device_metadata(dev),'p2':p2,'image_size':640,'physical_batch':batch_size,'effective_batch':16,
            'steps':steps,'median_microstep_ms':float(np.median(durations)*1000),
            'mean_microstep_ms':float(np.mean(durations)*1000),
            'p95_microstep_ms':float(np.percentile(durations,95)*1000),
            'peak_gpu_mib':torch.cuda.max_memory_allocated(dev)/2**20,
            'estimated_600_frame_train_epoch_seconds_excluding_validation':int(np.ceil(600/batch_size))*float(np.mean(durations)),
            'source_frame_ids':[r.frame_id for r in source],
            'annotation_sha256':file_digest(annotations), 'protocol_sha256':file_digest(Path(protocol)/'protocol.json')}
    write_json(output,result);return result


if __name__=='__main__':
    parser=argparse.ArgumentParser();parser.add_argument('--annotations',required=True)
    parser.add_argument('--protocol',default='outputs/protocol_v3');parser.add_argument('--output',required=True)
    parser.add_argument('--device',default='GPU-09ed84c8-4dee-efc0-dc1d-deda5177e4e8')
    parser.add_argument('--batch-size',type=int,default=2)
    parser.add_argument('--no-p2',action='store_true');args=parser.parse_args()
    print(json.dumps(measure(args.annotations,args.protocol,args.output,args.device,p2=not args.no_p2,batch_size=args.batch_size)))
