# examples/vjepa2_export.py
"""Export frozen original V-JEPA 2 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
remain unchanged (including the release's RoPE frequency expansion). Only timm
drop_path is replaced; the unused einops import is removed. This is appearance
inspection, not real motion video, an action classifier, or a robot planner.
"""

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

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

REVISION = '204698b45b3712590f06245fbfba32d3be539812'
WEIGHTS_URL = 'https://dl.fbaipublicfiles.com/vjepa2/vitl.pt'
WEIGHTS_HASH = '5346856ec9df69487fe72a25bf2632aaa8112df33fb67708e3f7374edc1f7012'
SOURCE_HASHES = {
    'src/models/vision_transformer.py': '9d4fd4e3603311803c105cc5114e2bc12ff28bc11870637dd3969511d9dead74',
    'src/models/utils/modules.py': 'b93f6c7e0747deb216419c000c2878f11a9189024a9adeacfd437e172396dff0',
    'src/models/utils/patch_embed.py': '1823c40b34410e70eb313360e21678e18a0ee5465b5d47de9f3d967c074f5a9d',
    'src/models/utils/pos_embs.py': 'e6d3b3863ade47b3734a3b443f1bcc93eab02e39bf596ea199517f68ccab443e',
    'src/masks/utils.py': '833f111a0fa5ffdbd3a6412e2dace2517c3c178f49c14f8bb631d9f6a070dfd0',
    'src/utils/tensors.py': '782b58bd2af456e184750e5318ab773105108383f61b280fe4c7a90f46add2c8',
}


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 drop_path(x, drop_prob=0., training=False, scale_by_keep=True):
    """Native timm stochastic-depth semantics; an identity during evaluation."""
    if drop_prob == 0. or not training:
        return x
    keep_prob = 1. - drop_prob
    random = x.new_empty((x.shape[0],) + (1,) * (x.ndim - 1)).bernoulli_(keep_prob)
    if keep_prob > 0. and scale_by_keep:
        random.div_(keep_prob)
    return x * random


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(compile(source, str(directory / filename), 'exec'), namespace)
        return namespace

    modules = load('src/models/utils/modules.py', {'drop_path': drop_path}, ('timm.models.layers',))
    patches = load('src/models/utils/patch_embed.py', stripped=('einops',))
    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=(256, 256), num_frames=64, tubelet_size=2,
                               use_rope=True, use_sdpa=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)
    # The original ViT-L release contains no pos_embed tensor with use_rope=True.
    # Do not silently drop unexpected positions or tolerate missing parameters.
    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 * 256 / min(width, height)), int(height * 256 / min(width, height)))
        image = image.resize(resized_size, Image.Resampling.BILINEAR)
        left, top = (image.width - 256) // 2, (image.height - 256) // 2
        image = image.crop((left, top, left + 256, top + 256))
        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([.485, .456, .406])[:, None, None]) / torch.tensor([.229, .224, .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 + 256, top + 256],
                        '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 2 ViT-L frozen features; each photograph repeated for 16 frames; appearance only, no motion or action recognition.',
        'provenance': {'architecture': 'Original V-JEPA 2 ViT-L/16, 24 layers, width 1024, 16 heads, 2-frame tubelets',
                       '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/vjepa2_export.py --checkpoint .context/paper-series/weights/vjepa2-vitl.pt --official-dir .context/paper-series/research/vjepa2-native --photos-dir public/examples/vit-demo --output public/examples/vjepa2-demo',
                       'adapter': 'Hash-pinned native author encoder; native timm drop_path replacement; unused einops import removed; released RoPE frequency-repeat behavior preserved.'},
        'preprocessing': {'resize': 'PIL bilinear, shortest edge 256; floor long edge',
                          'crop': '256x256 center crop; floor offsets', 'frames': 16,
                          'temporalInput': '16 identical copies of a still photograph, no measured motion',
                          'mean': [.485, .456, .406], 'std': [.229, .224, .225],
                          'pooling': 'Mean of all 2048 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)
