# examples/vjepa_export.py
"""Export frozen original V-JEPA ViT-L features from repeated photographs.

Supply a local verified checkpoint, pinned native author sources, and the
existing vit-demo photos/manifest. No downloads occur. Author encoder, attention and absolute 3D sin/cos positions remain unchanged. This is appearance
inspection, not real motion video, an action classifier, or a robot planner.
"""

import argparse
import ast
import hashlib
import json
import platform
from pathlib import Path

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

REVISION = "51c59d518fc63c08464af6de585f78ac0c7ed4d5"
WEIGHTS_URL = "https://dl.fbaipublicfiles.com/jepa/vitl16/vitl16.pth.tar"
WEIGHTS_HASH = (
    "0cd3bfce97d04891a31eac70d573ef94f423d858a6b0dc0b3ddf5f8d00b33fe4"
)
SOURCE_HASHES = {
    "src/models/vision_transformer.py": "3d2dd861d535434c4202c1b488436433fc545d558899a48c392c240b4afe8abf",
    "src/models/utils/modules.py": "b4e2f4259a2255173d1059865691e7e45974d121781601035c44c589a1bf62e7",
    "src/models/utils/patch_embed.py": "8167c7a9a9a15dfff81a0e3b8cdfcbfab6881cc0da396cc130c4729ccba75c11",
    "src/models/utils/pos_embs.py": "3fff7d09a8e99eaaca0c3e436ecc5794dc9e62245fe3e5489e8cd37a23f771a8",
    "src/masks/utils.py": "2297fb30aabdbc635a7b9e9793806cebec62367f032d4b9bd57fbb4e1681a381",
    "src/utils/tensors.py": "774a0f81eea52d2fcc4373818d8aab04e790c14f029e37e415ec0e5f6df2a8cb",
}


def digest(path):
    """Computes a streaming SHA-256, including for the multi-GB checkpoint."""
    with Path(path).open("rb") as stream:
        return hashlib.file_digest(stream, "sha256").hexdigest()


def author_model(directory):
    """Executes hash-pinned author files with a narrow local import adapter."""
    directory = Path(directory)
    for filename, expected in SOURCE_HASHES.items():
        if digest(directory / filename) != expected:
            raise ValueError(f"Official source checksum mismatch: {filename}")

    def load(filename, injected=None, stripped=()):
        source = ast.parse((directory / filename).read_text())
        source.body = [
            node
            for node in source.body
            if not (
                isinstance(node, ast.ImportFrom) and node.module in stripped
            )
        ]
        namespace = {} if injected is None else dict(injected)
        exec(  # noqa: S102 -- SHA-256-pinned author source only.
            compile(source, str(directory / filename), "exec"), namespace
        )
        return namespace

    modules = load("src/models/utils/modules.py")
    patches = load("src/models/utils/patch_embed.py")
    positions = load("src/models/utils/pos_embs.py")
    masks = load("src/masks/utils.py")
    tensors = load("src/utils/tensors.py")
    native = load(
        "src/models/vision_transformer.py",
        {
            "Block": modules["Block"],
            "PatchEmbed": patches["PatchEmbed"],
            "PatchEmbed3D": patches["PatchEmbed3D"],
            "apply_masks": masks["apply_masks"],
            "get_2d_sincos_pos_embed": positions["get_2d_sincos_pos_embed"],
            "get_3d_sincos_pos_embed": positions["get_3d_sincos_pos_embed"],
            "trunc_normal_": tensors["trunc_normal_"],
        },
        (
            "src.masks.utils",
            "src.models.utils.modules",
            "src.models.utils.patch_embed",
            "src.models.utils.pos_embs",
            "src.utils.tensors",
        ),
    )
    return native["vit_large"](
        img_size=224, num_frames=16, tubelet_size=2, uniform_power=True
    )


def load_encoder(checkpoint, official_dir):
    """Loads every tensor strictly after removing the known wrapper prefix."""
    if digest(checkpoint) != WEIGHTS_HASH:
        raise ValueError("Checkpoint SHA-256 mismatch.")
    checkpoint_state = torch.load(
        checkpoint, map_location="cpu", weights_only=True, mmap=True
    )
    raw = checkpoint_state["target_encoder"]
    prefix = "module.backbone."
    if not all(name.startswith(prefix) for name in raw):
        raise ValueError("Unexpected target encoder wrapper prefix.")
    state = {name[len(prefix) :]: value for name, value in raw.items()}
    model = author_model(official_dir)
    # Preserve the checkpoint's original absolute sin/cos position tensor.
    model.load_state_dict(state, strict=True)
    return model.eval().requires_grad_(False), len(state)


def export(checkpoint, official_dir, photos_dir, output):
    """Writes compact pooled features and exact provenance for two still clips."""
    torch.set_num_threads(2)
    torch.manual_seed(7)
    model, tensor_count = load_encoder(checkpoint, official_dir)
    print("Strict-loaded original ViT-L target encoder.", flush=True)
    output, photos_dir = Path(output), Path(photos_dir)
    output.mkdir(parents=True, exist_ok=True)
    source_manifest = json.loads((photos_dir / "predictions.json").read_text())
    results, pooled = [], []
    for photo in source_manifest["photos"]:
        source = photos_dir / photo["originalImage"]
        if digest(source) != photo["source"]["sha256"]:
            raise ValueError("Input photo hash mismatch.")
        image = Image.open(source).convert("RGB")
        width, height = image.size
        resized_size = (
            int(width * 224 / min(width, height)),
            int(height * 224 / min(width, height)),
        )
        image = image.resize(resized_size, Image.Resampling.BILINEAR)
        left, top = (image.width - 224) // 2, (image.height - 224) // 2
        image = image.crop((left, top, left + 224, top + 224))
        image_file = output / f'input-{photo["id"]}.png'
        image.save(image_file)
        frame = torch.from_numpy(
            np.array(image, dtype=np.float32) / 255
        ).permute(2, 0, 1)
        frame = (
            frame - torch.tensor([0.485, 0.456, 0.406])[:, None, None]
        ) / torch.tensor([0.229, 0.224, 0.225])[:, None, None]
        video = frame[None, :, None].repeat(1, 1, 16, 1, 1)
        with torch.inference_mode():
            tokens = model(video)
            feature = tokens.mean(dim=1)[0]
        if not torch.isfinite(tokens).all():
            raise ValueError("Encoder produced non-finite output.")
        pooled.append(feature)
        results.append(
            {
                "id": photo["id"],
                "title": photo["title"],
                "source": photo["source"],
                "inputImage": image_file.name,
                "inputSha256": digest(image_file),
                "inputShape": list(video.shape),
                "resizedSize": list(resized_size),
                "cropBox": [left, top, left + 224, top + 224],
                "tokenShape": list(tokens.shape),
                "meanTokenL2": float(tokens.norm(dim=-1).mean()),
                "pooledL2": float(feature.norm()),
                "pooledFirst8": feature[:8].tolist(),
            }
        )
        print(f'Exported {photo["id"]}: {list(tokens.shape)}', flush=True)
    features = torch.stack(pooled)
    np.save(
        output / "pooled-features.npy", features.numpy(), allow_pickle=False
    )
    normalized = F.normalize(features, dim=-1)
    manifest = {
        "scope": "Genuine original V-JEPA ViT-L frozen features; each photograph repeated for 16 frames; appearance only, no motion or action recognition.",
        "provenance": {
            "architecture": "Original V-JEPA ViT-L/16, 24 layers, width 1024, 16 heads, 2-frame tubelets",
            "uniformPower": True,
            "sourceRepository": "https://github.com/facebookresearch/jepa",
            "sourceLicense": "https://github.com/facebookresearch/jepa/blob/"
            + REVISION
            + "/LICENSE",
            "revision": REVISION,
            "checkpointUrl": WEIGHTS_URL,
            "checkpointSha256": WEIGHTS_HASH,
            "checkpointKey": "target_encoder",
            "removedPrefix": "module.backbone.",
            "strictLoadedTensors": tensor_count,
            "ignoredTensors": [],
            "sourceHashes": SOURCE_HASHES,
            "programSha256": digest(__file__),
            "parameterCount": sum(p.numel() for p in model.parameters()),
            "python": platform.python_version(),
            "torch": torch.__version__,
            "numpy": np.__version__,
            "pillow": pillow_version,
            "device": "cpu",
            "dtype": "float32",
            "command": "python examples/vjepa_export.py --checkpoint .context/paper-series/weights/vjepa-vitl16.pth.tar --official-dir .context/paper-series/research/jepa1 --photos-dir public/examples/vit-demo --output public/examples/vjepa-demo",
            "adapter": "Hash-pinned native author encoder without numerical edits; local import wiring only; original absolute 3D sin/cos positions retained.",
        },
        "preprocessing": {
            "resize": "PIL bilinear, shortest edge 224; floor long edge",
            "crop": "224x224 center crop; floor offsets",
            "frames": 16,
            "temporalInput": "16 identical copies of a still photograph, no measured motion",
            "mean": [0.485, 0.456, 0.406],
            "std": [0.229, 0.224, 0.225],
            "pooling": "Mean of all 1568 final-layer patch features, without extra feature normalization",
        },
        "photos": results,
        "cosineSimilarity": (normalized @ normalized.T).tolist(),
        "features": {
            "file": "pooled-features.npy",
            "shape": list(features.shape),
            "sha256": digest(output / "pooled-features.npy"),
        },
    }
    (output / "inspection.json").write_text(
        json.dumps(manifest, indent=2) + "\n"
    )


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    for option in ("checkpoint", "official-dir", "photos-dir", "output"):
        parser.add_argument(f"--{option}", required=True)
    args = parser.parse_args()
    export(args.checkpoint, args.official_dir, args.photos_dir, args.output)
