# examples/beit_export.py
"""Export real BEiT masked-token and classification outputs for two CC0 photos.

Keep beit.py, beit_tokenizer.py, requirements-beit.txt and beit-NOTICE.txt beside
this file. The default cache is outside the download directory. Author reference
code is pinned and hashed before execution and needs no additional dependencies.
Run: python beit_export.py --output-dir beit-demo --cache-dir ~/.cache/beit-example
"""

import argparse
from contextlib import contextmanager
from functools import partial
import importlib.util
import json
from pathlib import Path
import sys
import types

from PIL import Image, ImageDraw, __version__ as pillow_version
import torch
from torch import nn
import torchvision
from torchvision.transforms import InterpolationMode
from torchvision.transforms import functional as TF

import beit
from beit_tokenizer import (
    ENCODER,
    cached_file,
    load_tokenizer,
    restricted_load,
    sha256,
    tokenizer_globals,
)

DEFAULT_CACHE = Path.home() / ".cache" / "beit-example"
UNILM_REVISION = "31c5b904ca1bf2afb4c234a6675c683a4e5fc7cd"
DALLE_REVISION = "5be4b236bc3ade6943662354117a0e83752cc322"
MIM = {
    "name": "beit_base_patch16_224_pt1k_800ep.pth",
    "url": "https://github.com/addf400/files/releases/download/v1.0/beit_base_patch16_224_pt1k_800ep.pth",
    "sha256": "1573c8f4af393243b420380fe653bc75b9719b66de879ae2f31087ca25098541",
    "bytes": 368419004,
    "profile": "mim",
    "recipe": (
        "ImageNet-1K masked image modeling, 800 epochs, absolute "
        "positions, no relative bias or LayerScale"
    ),
    "license": "MIT (Microsoft)",
    "parameterCount": 92089856,
    "config": {
        "imageSize": 224,
        "patchSize": 16,
        "width": 768,
        "depth": 12,
        "heads": 12,
        "vocabSize": 8192,
        "absolutePositions": True,
        "relativePositionBias": False,
        "layerScale": False,
        "normEpsilon": 1e-6,
    },
}
CLASSIFICATION = {
    "name": "beit_base_patch16_224_pt22k_ft1k.pth",
    "url": "https://github.com/addf400/files/releases/download/v1.0/beit_base_patch16_224_pt22k_ft1k.pth",
    "sha256": "871a845c428ee652da19b994c814d857289fde304dea9c7114d3a8b3cf3fbb1e",
    "bytes": 176830615,
    "profile": "classification",
    "recipe": (
        "Self-supervised ImageNet-22K pretraining then ImageNet-1K "
        "fine-tuning; relative position bias, LayerScale, patch mean "
        "then fc_norm"
    ),
    "license": "MIT (Microsoft)",
    "parameterCount": 86530984,
    "config": {
        "imageSize": 224,
        "patchSize": 16,
        "width": 768,
        "depth": 12,
        "heads": 12,
        "classes": 1000,
        "absolutePositions": False,
        "relativePositionBias": True,
        "layerScale": True,
        "normEpsilon": 1e-6,
    },
}
PHOTOS = [
    {
        "id": "cat",
        "title": "Chelsea the cat",
        "name": "chelsea.png",
        "author": "Stefan van der Walt",
        "sha256": "596aa1e7cb875eb79f437e310381d26b338a81c2da23439704a73c4651e8c4bb",
        "seed": 0,
    },
    {
        "id": "coffee",
        "title": "Cup of coffee",
        "name": "coffee.png",
        "author": "Rachel Michetti",
        "sha256": "cc02f8ca188b167c775a7101b5d767d1e71792cf762c33d6fa15a4599b5a8de7",
        "seed": 4,
    },
]
PHOTO_BASE = "https://raw.githubusercontent.com/scikit-image/scikit-image/v0.25.2/skimage/data/"
PHOTO_LICENSE_URL = "https://github.com/scikit-image/scikit-image/blob/v0.25.2/skimage/data/_fetchers.py"
REFERENCES = [
    {
        "name": "beit_modeling_finetune.py",
        "url": f"https://raw.githubusercontent.com/microsoft/unilm/{UNILM_REVISION}/beit/modeling_finetune.py",
        "sha256": "6828066f6da039ce28b17c2eae4bc5e04d694dae202015c403d0383410b9126a",
    },
    {
        "name": "beit_modeling_pretrain.py",
        "url": f"https://raw.githubusercontent.com/microsoft/unilm/{UNILM_REVISION}/beit/modeling_pretrain.py",
        "sha256": "3dd672b3f2d6dd63df4c110c8807bde6105f2f34b25573ee3ff0937b1c9c2eb6",
    },
    {
        "name": "dall_e_utils.py",
        "url": f"https://raw.githubusercontent.com/openai/DALL-E/{DALLE_REVISION}/dall_e/utils.py",
        "sha256": "9246f47d271389ad862786c0055b3850fb6eaa86b7c89628c4ef5d3875cca21d",
    },
    {
        "name": "dall_e_encoder.py",
        "url": f"https://raw.githubusercontent.com/openai/DALL-E/{DALLE_REVISION}/dall_e/encoder.py",
        "sha256": "b52c0b086ad2f920554b741d1537eedd820ec39023ee7da285f2aeccecfa5b84",
    },
]
PREPROCESSING = {
    "mim": {
        "crop": (
            "largest centered square of original pixels; same crop for "
            "both branches"
        ),
        "flip": False,
        "beitSize": [224, 224],
        "beitInterpolation": "bicubic",
        "mean": list(beit.IMAGENET_MEAN),
        "std": list(beit.IMAGENET_STD),
        "tokenizerSize": [112, 112],
        "tokenizerInterpolation": "lanczos",
        "pixelMapping": "0.8*x+0.1, x in [0,1]",
        "grid": [14, 14],
        "patchSize": 16,
        "flattenOrder": "row-major",
    },
    "classification": {
        "resizeShorterEdge": 256,
        "centerCrop": [224, 224],
        "cropPct": 0.875,
        "interpolation": "bicubic",
        "antialias": True,
        "mean": [0.5, 0.5, 0.5],
        "std": [0.5, 0.5, 0.5],
    },
}


def centered_square(image):
    """Deterministic example augmentation, shared by the two MIM views."""
    side = min(image.size)
    left, top = (image.width - side) // 2, (image.height - side) // 2
    return [left, top, left + side, top + side]


def classification_view(image):
    """Official sub-384 evaluation: shortest 256, center 224, mean/std 0.5."""
    resized = TF.resize(
        image.convert("RGB"), 256, InterpolationMode.BICUBIC, antialias=True
    )
    crop = TF.center_crop(resized, [224, 224])
    batch = TF.normalize(TF.to_tensor(crop), [0.5] * 3, [0.5] * 3).unsqueeze(0)
    return crop, batch


def load_model(cache_dir, profile):
    """Pin checkpoint bytes before using the strict native model loader."""
    record = MIM if profile == "mim" else CLASSIFICATION
    model = beit.BEiT.from_checkpoint(
        cached_file(cache_dir, record), profile=profile
    )
    if sum(p.numel() for p in model.parameters()) != record["parameterCount"]:
        raise ValueError("released checkpoint parameter count mismatch")
    return model


@contextmanager
def reference_imports(cache_dir):
    """Load untouched author files with validation-only dependency adapters.

    timm supplies registration, tuple conversion, initialization, and DropPath.
    PyTorch provides matching initialization; stochastic depth is disabled in
    evaluation and the adapter refuses training. attrs constructors are not run
    when restoring the saved DALL-E objects, so only its decorator/default fields
    need compatibility shims. None of these adapters implements model forwards.
    """
    files = {
        record["name"]: cached_file(cache_dir, record) for record in REFERENCES
    }
    names = [
        "timm",
        "timm.models",
        "timm.models.layers",
        "timm.models.registry",
        "attr",
        "dall_e",
        "dall_e.utils",
        "dall_e.encoder",
        "modeling_finetune",
        "modeling_pretrain",
    ]
    previous = {name: sys.modules.get(name) for name in names}
    try:
        for name in names:
            sys.modules[name] = types.ModuleType(name)
        layers = sys.modules["timm.models.layers"]
        layers.to_2tuple = lambda value: (
            (value, value) if isinstance(value, int) else value
        )
        layers.trunc_normal_ = nn.init.trunc_normal_

        def eval_drop_path(x, drop_prob=None, training=False):
            if training:
                raise ValueError(
                    "reference DropPath adapter is evaluation-only"
                )
            return x

        layers.drop_path = eval_drop_path
        sys.modules["timm.models.registry"].register_model = lambda function: (
            function
        )
        attr = sys.modules["attr"]
        attr.s = lambda **kwargs: lambda cls: cls
        attr.ib = lambda default=None, **kwargs: default

        def load(name, filename):
            spec = importlib.util.spec_from_file_location(name, files[filename])
            module = importlib.util.module_from_spec(spec)
            sys.modules[name] = module
            spec.loader.exec_module(module)
            return module

        fine = load("modeling_finetune", "beit_modeling_finetune.py")
        pre = load("modeling_pretrain", "beit_modeling_pretrain.py")
        utils = load("dall_e.utils", "dall_e_utils.py")
        encoder = load("dall_e.encoder", "dall_e_encoder.py")
        yield fine, pre, utils, encoder
    finally:
        for name, original in previous.items():
            if original is None:
                sys.modules.pop(name, None)
            else:
                sys.modules[name] = original


def reference_models(cache_dir):
    """Restore independent author implementations with strict checkpoint loads."""
    with reference_imports(cache_dir) as (fine, pre, utils, encoder):
        common = {
            "norm_layer": partial(nn.LayerNorm, eps=1e-6),
            "drop_path_rate": 0.1,
            "qkv_bias": True,
        }
        mim = pre.VisionTransformerForMaskedImageModeling(**common)
        classifier = fine.VisionTransformer(
            use_abs_pos_emb=False,
            use_rel_pos_bias=True,
            init_values=0.1,
            use_mean_pooling=True,
            **common,
        )
        for model, record in [(mim, MIM), (classifier, CLASSIFICATION)]:
            state = torch.load(
                cached_file(cache_dir, record),
                map_location="cpu",
                weights_only=True,
            )["model"]
            model.load_state_dict(state, strict=True)
            model.float().eval().requires_grad_(False)
        tokenizer = restricted_load(
            cached_file(cache_dir, ENCODER),
            tokenizer_globals(
                encoder.Encoder, encoder.EncoderBlock, utils.Conv2d
            ),
        )
        tokenizer.float().eval().requires_grad_(False)
    return mim, classifier, tokenizer


def parity(actual, reference, *, atol=2e-5, rtol=2e-4):
    """Compare every logit, not just the winning category."""
    torch.testing.assert_close(actual, reference, atol=atol, rtol=rtol)
    error = (actual - reference).abs()
    return {
        "shape": list(actual.shape),
        "elementsCompared": actual.numel(),
        "atol": atol,
        "rtol": rtol,
        "maxAbsError": error.max().item(),
        "maxAllowedErrorRatio": (error / (atol + rtol * reference.abs()))
        .max()
        .item(),
        "passed": True,
    }


def save_image(image, output_dir, filename):
    """Describe the exact saved RGB artifact, including dimensions and bytes."""
    path = output_dir / filename
    image.save(path)
    return {
        "filename": filename,
        "sha256": sha256(path),
        "width": image.width,
        "height": image.height,
    }


def mask_overlay(crop, mask):
    """Opaque overlay depicts hidden patches; it is not a reconstructed image."""
    overlay = crop.copy()
    draw = ImageDraw.Draw(overlay)
    for index, selected in enumerate(mask.tolist()):
        if selected:
            x, y = index % 14 * 16, index // 14 * 16
            draw.rectangle(
                (x, y, x + 15, y + 15),
                fill=(63, 63, 70),
                outline=(161, 161, 170),
            )
    return overlay


def mim_result(model, tokenizer, image, output_dir, prefix, seed):
    """Run the learned token objective and retain visible predictions as unscored."""
    crop_box = centered_square(image)
    views = beit.make_views(image, crop_box=crop_box)
    mask = beit.block_mask(seed=seed)
    with torch.inference_mode():
        targets = beit.visual_targets(tokenizer, views["dvae"])
        logits = model(views["beit"].unsqueeze(0), mask)
        loss = beit.masked_loss(logits, targets, mask).item()
        predictions = logits.argmax(-1)[0]
        log_probs = logits.log_softmax(-1)[0].gather(1, targets[0, :, None])[
            :, 0
        ]
    matches = predictions.eq(targets[0]) & mask
    images = {
        "beit": save_image(
            views["beit_crop"], output_dir, f"beit-{prefix}.png"
        ),
        "tokenizer": save_image(
            views["dvae_crop"], output_dir, f"tokenizer-{prefix}.png"
        ),
        "masked": save_image(
            mask_overlay(views["beit_crop"], mask),
            output_dir,
            f"masked-{prefix}.png",
        ),
    }
    result = {
        "inputImage": images["beit"]["filename"],
        "tokenizerImage": images["tokenizer"]["filename"],
        "maskImage": images["masked"]["filename"],
        "cropBox": crop_box,
        "inputShape": [1, 3, 224, 224],
        "tokenizerInputShape": [1, 3, 112, 112],
        "logitsShape": list(logits.shape),
        "seed": seed,
        "requestedMaskCount": 75,
        "minimumBlockArea": 4,
        "actualMaskCount": int(mask.sum()),
        "mask": mask.tolist(),
        "targetIds": targets[0].tolist(),
        "predictedIds": predictions.tolist(),
        "scoredPredictedIds": [
            int(predictions[i]) if selected else None
            for i, selected in enumerate(mask)
        ],
        "selectedTargetLogProbabilities": [
            float(log_probs[i]) if selected else None
            for i, selected in enumerate(mask)
        ],
        "matchCount": int(matches.sum()),
        "agreement": float(matches.sum()) / int(mask.sum()),
        "loss": loss,
        "visiblePositions": "unscored",
    }
    return result, images, views, logits


def classification_result(model, image, output_dir, prefix):
    """Return full logits and sorted top-five labels from the 1000-class release."""
    crop, batch = classification_view(image)
    with torch.inference_mode():
        logits = model(batch)
    categories = torchvision.models.ViT_B_16_Weights.IMAGENET1K_V1.meta[
        "categories"
    ]
    scores, indices = logits.softmax(-1)[0].topk(5)
    top5 = [
        {"index": int(index), "label": categories[index], "score": float(score)}
        for score, index in zip(scores, indices)
    ]
    saved = save_image(crop, output_dir, f"classification-{prefix}.png")
    return (
        {
            "inputImage": saved["filename"],
            "inputShape": list(batch.shape),
            "logits": logits[0].tolist(),
            "top5": top5,
        },
        saved,
        batch,
        logits,
    )


def provenance():
    """Hashes bind the exact executed programs to each measured export."""
    directory = Path(__file__).parent
    return {
        "sources": {
            name: sha256(directory / name)
            for name in [
                "beit.py",
                "beit_tokenizer.py",
                "beit_export.py",
                "requirements-beit.txt",
                "beit-NOTICE.txt",
            ]
        },
        "packages": {
            "torch": torch.__version__,
            "torchvision": torchvision.__version__,
            "Pillow": pillow_version,
        },
        "device": "cpu",
        "dtype": "float32",
        "threads": torch.get_num_threads(),
        "checkpoints": {
            "mim": MIM,
            "classification": CLASSIFICATION,
            "tokenizer": ENCODER,
        },
    }


def infer_image(image_path, output_dir, cache_dir, profile, seed=0):
    """CLI inference on a user photograph; reference validation is the exporter."""
    torch.set_num_threads(1)
    output_dir.mkdir(parents=True, exist_ok=True)
    with Image.open(image_path) as source:
        image = source.convert("RGB")
    model = load_model(cache_dir, profile)
    if profile == "mim":
        result, images, _, _ = mim_result(
            model, load_tokenizer(cache_dir), image, output_dir, "image", seed
        )
    else:
        result, saved, _, _ = classification_result(
            model, image, output_dir, "image"
        )
        images = {"classification": saved}
    data = {
        "profile": profile,
        "sourceSha256": sha256(image_path),
        "provenance": provenance(),
        "preprocessing": PREPROCESSING[profile],
        "images": images,
        "result": result,
    }
    (output_dir / "result.json").write_text(json.dumps(data, indent=2) + "\n")
    return {"output": str(output_dir / "result.json"), "profile": profile}


def export(output_dir, cache_dir=DEFAULT_CACHE):
    """Reproduce both learned examples and all-logit author-reference checks."""
    output_dir.mkdir(parents=True, exist_ok=True)
    torch.set_num_threads(1)
    mim = load_model(cache_dir, "mim")
    classifier = load_model(cache_dir, "classification")
    tokenizer = load_tokenizer(cache_dir)
    reference_mim, reference_classifier, reference_tokenizer = reference_models(
        cache_dir
    )
    photos = []
    for photo in PHOTOS:
        source_record = {**photo, "url": PHOTO_BASE + photo["name"]}
        source_path = cached_file(cache_dir, source_record)
        destination = output_dir / f"source-{photo['id']}.png"
        destination.write_bytes(source_path.read_bytes())
        with Image.open(source_path) as source:
            image = source.convert("RGB")
        mim_data, images, views, mim_logits = mim_result(
            mim, tokenizer, image, output_dir, photo["id"], photo["seed"]
        )
        class_data, class_image, batch, class_logits = classification_result(
            classifier, image, output_dir, photo["id"]
        )
        images["classification"] = class_image
        images["source"] = {
            "filename": destination.name,
            "sha256": sha256(destination),
            "width": image.width,
            "height": image.height,
        }
        with torch.inference_mode():
            mask = torch.tensor(mim_data["mask"], dtype=torch.bool).unsqueeze(0)
            ref_mim_logits = reference_mim(
                views["beit"].unsqueeze(0),
                bool_masked_pos=mask,
                return_all_tokens=True,
            )
            ref_class_logits = reference_classifier(batch)
            target_logits = tokenizer(views["dvae"].unsqueeze(0))
            ref_target_logits = reference_tokenizer(views["dvae"].unsqueeze(0))
        reference = {
            "mim": parity(mim_logits, ref_mim_logits),
            "classification": parity(class_logits, ref_class_logits),
            "tokenizer": parity(target_logits, ref_target_logits),
        }
        reference["tokenizer"]["argmaxMismatchCount"] = int(
            target_logits.argmax(1).ne(ref_target_logits.argmax(1)).sum()
        )
        if reference["tokenizer"]["argmaxMismatchCount"]:
            raise ValueError("author tokenizer targets differ")
        photos.append(
            {
                "id": photo["id"],
                "title": photo["title"],
                "source": {
                    "url": source_record["url"],
                    "author": photo["author"],
                    "license": "CC0",
                    "licenseUrl": PHOTO_LICENSE_URL,
                    "sha256": photo["sha256"],
                },
                "images": images,
                "mim": mim_data,
                "classification": class_data,
                "referenceParity": reference,
            }
        )
        print(
            json.dumps(
                {
                    "photo": photo["id"],
                    "maskedLoss": mim_data["loss"],
                    "agreement": mim_data["agreement"],
                    "referenceMaxAbsErrors": {
                        key: value["maxAbsError"]
                        for key, value in reference.items()
                    },
                }
            ),
            flush=True,
        )
    data = {
        "schemaVersion": 1,
        "provenance": provenance(),
        "preprocessing": PREPROCESSING,
        "reference": {
            "sources": REFERENCES,
            "method": (
                "Untouched pinned Microsoft model forwards and OpenAI encoder"
                " forwards; strict checkpoint loads; compare all logits on "
                "identical CPU float32 input tensors."
            ),
            "adapters": (
                "Reference-only timm registration/tuple/trunc_normal "
                "adapters; DropPath identity in eval, training refused. attrs"
                " decorator/default shim restores saved DALL-E modules "
                "without running constructors. No model-forward replacements."
            ),
            "stochasticDepth": (
                "disabled in eval for both native and author reference models"
            ),
            "toleranceRationale": (
                "Float32 CPU execution; atol 2e-5 plus rtol 2e-4 permits "
                "arithmetic ordering differences while requiring every logit "
                "to pass, including values near zero."
            ),
            "command": (
                "python beit_export.py --output-dir beit-demo --cache-dir "
                "~/.cache/beit-example"
            ),
        },
        "photos": photos,
    }
    (output_dir / "results.json").write_text(json.dumps(data, indent=2) + "\n")
    return data


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--output-dir", type=Path, default=Path("beit-demo"))
    parser.add_argument("--cache-dir", type=Path, default=DEFAULT_CACHE)
    args = parser.parse_args()
    export(args.output_dir, args.cache_dir)
