# examples/ijepa_training.py
"""Run a small I-JEPA training step using the pinned official model classes.

This CPU reference teaches the mechanics; its random inputs and reduced model
are not pretrained representations or a reproduction of ImageNet results.
"""

import argparse
import copy
from functools import partial
import importlib
import json
from pathlib import Path
import subprocess
import sys

import torch
from torch import nn
import torch.nn.functional as F

OFFICIAL_REVISION = "52c1ae95d05f743e000e8f10a1f3a79b10cff048"


def load_official(repo):
    """Import unchanged model/masking sources from the specified revision."""
    repo = Path(repo).resolve()
    revision = subprocess.check_output(
        ["git", "-C", str(repo), "rev-parse", "HEAD"], text=True
    ).strip()
    dirty = subprocess.check_output(
        ["git", "-C", str(repo), "status", "--porcelain", "--", "src"],
        text=True,
    ).strip()
    if revision != OFFICIAL_REVISION or dirty:
        raise ValueError(
            "Use an unchanged official I-JEPA checkout at " + OFFICIAL_REVISION
        )
    sys.path.insert(0, str(repo))
    models = importlib.import_module("src.models.vision_transformer")
    masks = importlib.import_module("src.masks.multiblock")
    if not Path(models.__file__).resolve().is_relative_to(repo):
        raise ValueError("Another src package is shadowing the official models")
    return models, masks.MaskCollator


def make_tiny_models(models):
    """Reduce dimensions, retaining official positional and attention code."""
    encoder = models.VisionTransformer(
        img_size=[64],
        patch_size=8,
        embed_dim=64,
        depth=2,
        num_heads=4,
        norm_layer=partial(nn.LayerNorm, eps=1e-6),
    ).double()
    predictor = models.vit_predictor(
        num_patches=64,
        embed_dim=64,
        predictor_embed_dim=32,
        depth=2,
        num_heads=4,
    ).double()
    teacher = copy.deepcopy(encoder).requires_grad_(False)
    return encoder, predictor, teacher


# region: masking
def sample_masks(collator, images):
    """Retain actual indices; reject relaxed context-target overlap."""
    for _ in range(128):
        _, contexts, targets = collator(list(images))
        context = contexts[0].to(images.device)
        targets = [mask.to(images.device) for mask in targets]
        disjoint = all(
            not torch.isin(context[b], mask[b]).any()
            for mask in targets
            for b in range(len(images))
        )
        if disjoint:
            return context, targets  # Target blocks may overlap each other.
    raise ValueError("No disjoint context/target masks found in 128 attempts")


# endregion: masking


# region: context
def encode_context(encoder, images, context):
    """Official encoder: patchify + positions, gather, then self-attention."""
    return encoder(images, [context])  # [B, N_context, D], no CLS token.


# endregion: context


def gather_features(features, indices):
    """Gather each sample's row-major patch indices without changing order."""
    return features.gather(
        1, indices.unsqueeze(-1).expand(-1, -1, features.shape[-1])
    )


# region: teacher
@torch.no_grad()
def teacher_targets(teacher, images, targets):
    """Encode the full image before selecting target patch features."""
    full = teacher(images)
    full = F.layer_norm(full, (full.shape[-1],))
    return [gather_features(full, mask) for mask in targets]


# endregion: teacher


# region: queries
def target_queries(predictor, indices):
    """One shared learned mask token plus each target's fixed 2D position."""
    positions = predictor.predictor_pos_embed.expand(len(indices), -1, -1)
    positions = gather_features(positions, indices)
    return predictor.mask_token + positions  # [B, N_target, D_predictor]


# endregion: queries


# region: predictor
def predict_block(predictor, features, context, target):
    """The official predictor operations for one independent target block."""
    x = predictor.predictor_embed(features)
    positions = predictor.predictor_pos_embed.expand(len(x), -1, -1)
    x = x + gather_features(positions, context)
    n_context = x.shape[1]
    x = torch.cat((x, target_queries(predictor, target)), dim=1)
    for block in predictor.predictor_blocks:
        x = block(x)
    x = predictor.predictor_norm(x)[:, n_context:]
    return predictor.predictor_proj(x)  # [B, N_target, D]; do not normalize.


def predict_blocks(predictor, features, context, targets):
    """Preserve block IDs, including unequal sizes and overlapping patches."""
    return [
        predict_block(predictor, features, context, mask) for mask in targets
    ]


# endregion: predictor


# region: loss
def prediction_loss(predictions, targets):
    """Mean Smooth L1 per block, then mean over blocks (released beta=1)."""
    if not predictions or len(predictions) != len(targets):
        raise ValueError("Expected matching nonempty prediction/target lists")
    if any(
        p.shape != h.shape for p, h in zip(predictions, targets, strict=True)
    ):
        raise ValueError("Prediction and target shapes must match")
    return torch.stack(
        [
            F.smooth_l1_loss(p, h, beta=1.0)
            for p, h in zip(predictions, targets, strict=True)
        ]
    ).mean()


def optimize(optimizer, predictions, targets):
    """Only the context encoder and predictor belong to this optimizer."""
    optimizer.zero_grad(set_to_none=True)
    loss = prediction_loss(predictions, targets)
    loss.backward()
    optimizer.step()
    return loss.detach()


# endregion: loss


# region: ema
@torch.no_grad()
def update_teacher(encoder, teacher, momentum):
    """Call after AdamW: the teacher follows the encoder, not the predictor."""
    if not 0 <= momentum <= 1:
        raise ValueError("Momentum must be between zero and one")
    for student, target in zip(
        encoder.parameters(), teacher.parameters(), strict=True
    ):
        target.mul_(momentum).add_(student, alpha=1 - momentum)


# endregion: ema


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--repo", required=True, type=Path)
    parser.add_argument("--steps", type=int, default=2)
    args = parser.parse_args()
    if args.steps < 1:
        parser.error("--steps must be positive")
    torch.set_num_threads(2)
    torch.manual_seed(7)
    models, collator_class = load_official(args.repo)
    encoder, predictor, teacher = make_tiny_models(models)
    collator = collator_class(
        input_size=64,
        patch_size=8,
        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=4,
        allow_overlap=False,
    )
    optimizer = torch.optim.AdamW(
        list(encoder.parameters()) + list(predictor.parameters()), lr=1e-3
    )
    images = torch.randn(2, 3, 64, 64, dtype=torch.float64)
    for step in range(args.steps):
        context, targets = sample_masks(collator, images)
        features = encode_context(encoder, images, context)
        expected = teacher_targets(teacher, images, targets)
        predictions = predict_blocks(predictor, features, context, targets)
        loss = optimize(optimizer, predictions, expected)
        # The released schedule is linear, not cosine. This is a shortened demo.
        momentum = 0.996 + step * 0.004 / args.steps
        update_teacher(encoder, teacher, momentum)
        print(
            json.dumps(
                {
                    "step": step,
                    "loss": loss.item(),
                    "momentum": momentum,
                    "context_shape": list(features.shape),
                    "target_shapes": [list(x.shape) for x in predictions],
                    "teacher_gradients": any(
                        p.grad is not None for p in teacher.parameters()
                    ),
                }
            )
        )


if __name__ == "__main__":
    main()
