"""Same-device latency measurements for completed pilot checkpoints."""
from __future__ import annotations

import argparse
import json
from pathlib import Path
import statistics
import time

import torch

from pallet.data.loader import NormalizationStats, apply_normalization, load_frame
from pallet.data.protocol import file_digest, load_protocol, write_json
from pallet.devices import device_metadata, resolve_device
from pallet.experiments.dataset import SampleMeta, letterbox_float
from pallet.experiments.evaluate import predict_boxes
from pallet.export import load_detector_checkpoint


PILOT_VARIANTS = ("yolo26s", "yolo26_p2_only", "yolo26_p2_fusion_no_edge",
                  "yolo26_p2_edge_no_fusion", "pallet_yolo26", "yolov8s",
                  "yolov8_cspnext", "rfdetr_small")


def _sync(device):
    if device.type == "cuda":
        torch.cuda.synchronize(device)


def _summary(values):
    values = sorted(value * 1000 for value in values)
    return {"median_ms": statistics.median(values),
            "p95_ms": values[min(len(values) - 1, int(.95 * len(values)))],
            "repetitions": len(values)}


def benchmark(checkpoint, record, *, device, warmup=5, repeats=20, end_to_end_repeats=8):
    device = resolve_device(device)
    if warmup < 1 or repeats < 1 or end_to_end_repeats < 1:
        raise ValueError("warmup and repetition counts must be positive")
    state = torch.load(checkpoint, map_location="cpu", weights_only=False)
    model, config = load_detector_checkpoint(checkpoint)
    size = config["size"]
    if size != 640:
        raise ValueError("trained pilot benchmark requires a 640-pixel checkpoint")
    normalization = NormalizationStats(**state["normalization"])
    raw = load_frame(record)
    prepared, scale, padding = letterbox_float(apply_normalization(raw, normalization), size)
    tensor = prepared.unsqueeze(0).to(device)
    meta = SampleMeta(record.frame_id, raw.shape[:2], scale, padding)
    model.eval().to(device)
    _sync(device)
    if device.type == "cuda":
        torch.cuda.reset_peak_memory_stats(device)

    with torch.inference_mode():
        for _ in range(warmup):
            predict_boxes(model, tensor, meta, conf=.25, size=size)
        _sync(device)
        forward = []
        for _ in range(repeats):
            _sync(device)
            start = time.perf_counter()
            model(tensor)
            _sync(device)
            forward.append(time.perf_counter() - start)
        inference = []
        for _ in range(repeats):
            _sync(device)
            start = time.perf_counter()
            predict_boxes(model, tensor, meta, conf=.25, size=size)
            _sync(device)
            inference.append(time.perf_counter() - start)
        end_to_end = []
        for _ in range(end_to_end_repeats):
            _sync(device)
            start = time.perf_counter()
            raw = load_frame(record)
            prepared, scale, padding = letterbox_float(apply_normalization(raw, normalization), size)
            current = prepared.unsqueeze(0).to(device)
            current_meta = SampleMeta(record.frame_id, raw.shape[:2], scale, padding)
            predict_boxes(model, current, current_meta, conf=.25, size=size)
            _sync(device)
            end_to_end.append(time.perf_counter() - start)
    return {"checkpoint": str(Path(checkpoint).resolve()),
            "checkpoint_sha256": file_digest(checkpoint),
            "parameters": sum(parameter.numel() for parameter in model.parameters()),
            "peak_gpu_mib": torch.cuda.max_memory_allocated(device) / 2**20
            if device.type == "cuda" else None,
            "forward_only": _summary(forward),
            "model_and_postprocess": _summary(inference),
            "raw_sensor_to_boxes": _summary(end_to_end)}


def benchmark_pilot(protocol, root, output, *, frame_id, device,
                    warmup=5, repeats=20, end_to_end_repeats=8):
    records, _, identity = load_protocol(protocol)
    record = next(row for row in records if row.frame_id == frame_id)
    root = Path(root)
    metadata = device_metadata(device)
    report = {"scope": "completed pilot weights; no final test scoring",
              "frame_id": frame_id, "protocol_sha256": file_digest(Path(protocol) / "protocol.json"),
              "source_sha256": record.source_sha256,
              "device": metadata, "precision": "fp32", "batch": 1, "input_size": 640,
              "cuda_matmul_allow_tf32": torch.backends.cuda.matmul.allow_tf32,
              "cudnn_allow_tf32": torch.backends.cudnn.allow_tf32,
              "confidence_threshold": .25, "warmup": warmup,
              "measurement": "CUDA synchronized; raw PCD read/decode and normalization included only in raw_sensor_to_boxes",
              "models": {}}
    for slug in PILOT_VARIANTS:
        directory = root / "within" / "within" / slug / "seed_42"
        checkpoint = directory / "best.pt"
        completed = json.loads((directory / "completed.json").read_text())
        if file_digest(checkpoint) != completed["best_checkpoint_sha256"]:
            raise ValueError(f"checkpoint is not the completed best: {slug}")
        report["models"][slug] = benchmark(checkpoint, record, device=device,
                                              warmup=warmup, repeats=repeats,
                                              end_to_end_repeats=end_to_end_repeats)
        write_json(output, report)
        print(slug, report["models"][slug]["model_and_postprocess"], flush=True)
        if torch.cuda.is_available():
            torch.cuda.empty_cache()
    return report


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--protocol", default="outputs/protocol_v5")
    parser.add_argument("--root", default="outputs/pilot600_v5")
    parser.add_argument("--output", default="outputs/pilot600_v5/summary/trained_latency.json")
    parser.add_argument("--frame-id", default="2024_11_29/29-11-2024_07-00-28#all")
    parser.add_argument("--device", required=True)
    parser.add_argument("--warmup", type=int, default=5)
    parser.add_argument("--repeats", type=int, default=20)
    parser.add_argument("--end-to-end-repeats", type=int, default=8)
    args = parser.parse_args()
    benchmark_pilot(args.protocol, args.root, args.output, frame_id=args.frame_id,
                    device=args.device, warmup=args.warmup, repeats=args.repeats,
                    end_to_end_repeats=args.end_to_end_repeats)
