# examples/ijepa_export.py
"""Export measured ViT-H/14 masked-feature diagnostics for the I-JEPA page.

Requires the pinned official checkout and its published 10.36 GB checkpoint.
No model executes in the browser. Every saved crop is the exact inference input.
"""

import argparse
import copy
from datetime import datetime, timezone
import hashlib
import io
import json
from pathlib import Path
import platform
import urllib.request

import numpy as np
from PIL import Image, __version__ as pillow_version
import torch
import torch.nn.functional as F

from ijepa_training import (
    OFFICIAL_REVISION,
    encode_context,
    load_official,
    predict_blocks,
    sample_masks,
    teacher_targets,
)

CHECKPOINT_URL = (
    "https://dl.fbaipublicfiles.com/ijepa/IN1K-vit.h.14-300e.pth.tar"
)
SOURCE_BASE = "https://raw.githubusercontent.com/scikit-image/scikit-image/v0.25.2/skimage/data/"
LICENSE_URL = "https://github.com/scikit-image/scikit-image/blob/v0.25.2/skimage/data/_fetchers.py"
IMAGES = [
    ("cat", "Chelsea the cat", "chelsea.png", "CC0", "Stefan van der Walt"),
    (
        "coffee",
        "Coffee cup",
        "coffee.png",
        "CC0",
        "Rachel Michetti / Pikolo Espresso Bar",
    ),
    (
        "astronaut",
        "Astronaut Eileen Collins",
        "astronaut.png",
        "Public domain",
        "NASA",
    ),
]


def file_digest(path):
    """Hash without loading the full checkpoint into Python memory."""
    with Path(path).open("rb") as stream:
        return hashlib.file_digest(stream, "sha256").hexdigest()


def load_models(models, checkpoint_path, device):
    """Strictly load all three official state dictionaries; safe tensor loader."""
    encoder = models.vit_huge(img_size=[224], patch_size=14)
    teacher = copy.deepcopy(encoder)
    predictor = models.vit_predictor(
        num_patches=256,
        embed_dim=1280,
        predictor_embed_dim=384,
        depth=12,
        num_heads=16,
    )
    checkpoint = torch.load(
        checkpoint_path, map_location="cpu", weights_only=True, mmap=True
    )
    for model, key in (
        (encoder, "encoder"),
        (predictor, "predictor"),
        (teacher, "target_encoder"),
    ):
        state = {
            name.removeprefix("module."): value
            for name, value in checkpoint[key].items()
        }
        model.load_state_dict(state, strict=True, assign=True)
        model.to(device).eval().requires_grad_(False)
    return encoder, predictor, teacher


def prepare_image(filename, cache, image_dir):
    """Save a lossless center crop; normalize that exact RGB array."""
    source = cache / filename
    if not source.exists():
        with urllib.request.urlopen(
            SOURCE_BASE + filename, timeout=60
        ) as response:
            source.write_bytes(response.read())
    with Image.open(io.BytesIO(source.read_bytes())) as opened:
        image = opened.convert("RGB")
    width, height = image.size
    side = min(width, height)
    left, top = (width - side) // 2, (height - side) // 2
    image = image.crop((left, top, left + side, top + side))
    image = image.resize((224, 224), Image.Resampling.BICUBIC)
    values = (
        torch.from_numpy(np.asarray(image).copy()).permute(2, 0, 1).float()
        / 255
    )
    mean = torch.tensor([0.485, 0.456, 0.406])[:, None, None]
    std = torch.tensor([0.229, 0.224, 0.225])[:, None, None]
    return image, ((values - mean) / std).unsqueeze(0), file_digest(source)


def per_patch_errors(prediction, target):
    """Feature-coordinate mean, retaining every target patch's raw value."""
    return F.smooth_l1_loss(prediction, target, beta=1, reduction="none").mean(
        -1
    )[0]


@torch.inference_mode()
def export_case(encoder, predictor, teacher, images, context, targets):
    """Measure predictions and controls; prove the context sees no hidden pixels."""
    features = encode_context(encoder, images, context)
    expected = teacher_targets(teacher, images, targets)
    predictions = predict_blocks(predictor, features, context, targets)
    mean_context = F.layer_norm(features, (features.shape[-1],)).mean(
        1, keepdim=True
    )
    blocks = []
    for mask, prediction, target in zip(
        targets, predictions, expected, strict=True
    ):
        errors = {
            "prediction": per_patch_errors(prediction, target),
            "baseline": per_patch_errors(
                mean_context.expand_as(target), target
            ),
            "shuffled": per_patch_errors(prediction, target.roll(1, dims=1)),
        }
        block = {"indices": mask[0].tolist()}
        for name, values in errors.items():
            if not torch.isfinite(values).all():
                raise ValueError("Nonfinite checkpoint output")
            block[name + "Errors"] = values.cpu().tolist()
            block[name + "Mean"] = values.double().mean().item()
        blocks.append(block)

    # Modify tensor pixels AFTER crop/resize, avoiding interpolation leakage.
    changed = images.clone()
    visible = set(context[0].tolist())
    for index in range(256):
        if index not in visible:
            row, col = divmod(index, 16)
            changed[
                :, :, row * 14 : (row + 1) * 14, col * 14 : (col + 1) * 14
            ] += 1
    modified_context = encode_context(encoder, changed, context)
    modified_predictions = predict_blocks(
        predictor, modified_context, context, targets
    )
    context_delta = (features - modified_context).abs().max().item()
    prediction_delta = max(
        (a - b).abs().max().item()
        for a, b in zip(predictions, modified_predictions, strict=True)
    )
    modified_targets = teacher_targets(teacher, changed, targets)
    teacher_delta = max(
        (a - b).abs().max().item()
        for a, b in zip(expected, modified_targets, strict=True)
    )
    if context_delta != 0 or prediction_delta != 0 or teacher_delta == 0:
        raise ValueError("Hidden-pixel isolation check failed")
    return blocks, {
        "contextMaxDelta": context_delta,
        "predictionMaxDelta": prediction_delta,
        "teacherMaxDelta": teacher_delta,
    }


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--repo", required=True, type=Path)
    parser.add_argument("--checkpoint", required=True, type=Path)
    parser.add_argument(
        "--root", type=Path, default=Path(__file__).resolve().parents[1]
    )
    parser.add_argument("--device", default="cuda")
    args = parser.parse_args()
    torch.set_num_threads(4)
    torch.manual_seed(7)
    torch.use_deterministic_algorithms(True)
    torch.backends.cuda.matmul.allow_tf32 = False
    torch.backends.cudnn.allow_tf32 = False
    if args.checkpoint.stat().st_size != 10358004345:
        parser.error(
            "Unexpected checkpoint size; finish the official download first"
        )
    models, collator_class = load_official(args.repo)
    encoder, predictor, teacher = load_models(
        models, args.checkpoint, args.device
    )
    print("All three checkpoint sections loaded strictly.", flush=True)
    image_dir = args.root / "public/images/papers/ijepa"
    image_dir.mkdir(parents=True, exist_ok=True)
    cache = args.checkpoint.parent
    cases = []
    for name, label, filename, license_name, credit in IMAGES:
        image, inputs, source_hash = prepare_image(filename, cache, image_dir)
        saved = image_dir / (name + ".png")
        image.save(saved)
        inputs = inputs.to(args.device)
        for seed in (11, 29):
            torch.manual_seed(seed)
            collator = collator_class(
                input_size=224,
                patch_size=14,
                enc_mask_scale=(0.85, 1.0),
                pred_mask_scale=(0.15, 0.2),
                aspect_ratio=(0.75, 1.5),
                nenc=1,
                npred=4,
                min_keep=10,
                allow_overlap=False,
            )
            context, targets = sample_masks(collator, inputs)
            blocks, isolation = export_case(
                encoder, predictor, teacher, inputs, context, targets
            )
            cases.append(
                {
                    "id": f"{name}-{seed}",
                    "imageKey": name,
                    "label": label,
                    "mask": f"Seed {seed}",
                    "image": f"/images/papers/ijepa/{name}.png",
                    "imageSha256": file_digest(saved),
                    "sourceSha256": source_hash,
                    "sourceUrl": SOURCE_BASE + filename,
                    "license": license_name,
                    "licenseUrl": LICENSE_URL,
                    "credit": credit,
                    "contextIndices": context[0].tolist(),
                    "blocks": blocks,
                    "isolation": isolation,
                }
            )
            print(
                f"Measured {name}, mask seed {seed}; isolation passed.",
                flush=True,
            )
    max_error = max(
        error
        for case in cases
        for block in case["blocks"]
        for name in ("prediction", "baseline", "shuffled")
        for error in block[name + "Errors"]
    )
    data = {
        "schemaVersion": 1,
        "gridSize": 16,
        "errorMax": max_error,
        "metric": "Smooth L1 beta=1; feature-coordinate mean per patch, patch mean per block",
        "baseline": "Layer-normalize each visible context feature, then average tokens; no second normalization",
        "control": "Cyclic shift of teacher target features within each block; diagnostic, not semantic proof",
        "preprocessing": "RGB; center square crop; bicubic 224 resize; ImageNet mean/std normalization",
        "provenance": {
            "revision": OFFICIAL_REVISION,
            "checkpointUrl": CHECKPOINT_URL,
            "checkpointBytes": args.checkpoint.stat().st_size,
            "checkpointSha256": file_digest(args.checkpoint),
            "exportSha256": file_digest(__file__),
            "trainingSha256": file_digest(
                Path(__file__).with_name("ijepa_training.py")
            ),
            "strictLoad": True,
            "model": "ViT-H/14",
            "dtype": "float32",
            "device": args.device,
            "gpu": torch.cuda.get_device_name()
            if args.device.startswith("cuda")
            else None,
            "torch": torch.__version__,
            "numpy": np.__version__,
            "pillow": pillow_version,
            "python": platform.python_version(),
            "createdAt": datetime.now(timezone.utc).isoformat(),
        },
        "cases": cases,
    }
    destination = args.root / "public/data/ijepa/predictions.json"
    destination.parent.mkdir(parents=True, exist_ok=True)
    destination.write_text(json.dumps(data, indent=2, allow_nan=False) + "\n")
    print(f"Saved {len(cases)} cases to {destination}", flush=True)


if __name__ == "__main__":
    main()
