# examples/vjepa_training.py
"""Run original V-JEPA mechanics with small random-weight native Torch models.

This offline teaching core uses two batch-shared full-time block masks, a
48-wide two-block encoder and 24-wide one-block predictor. It follows original
absolute sin/cos positions, target LayerNorm, equal per-mask mean L1 and EMA.
It is not a checkpoint-compatible model or a benchmark reproduction. The
separate vjepa_export.py executes the pinned original pretrained encoder.
"""

import argparse
import copy
import json
import math

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


def validate_grid(grid):
    """Rejects empty or nonintegral token-grid dimensions."""
    if len(grid) != 3 or any(not isinstance(n, int) or n <= 0 for n in grid):
        raise ValueError("Grid must contain three positive integers.")


# region: tubelets
class Tubelets(nn.Module):
    """Projects nonoverlapping tubelets in time, row, column order."""

    def __init__(self, width=48, tubelet=2, patch=8):
        super().__init__()
        if min(width, tubelet, patch) <= 0:
            raise ValueError(
                "Tubelet, patch and embedding dimensions must be positive."
            )
        self.stride = (tubelet, patch, patch)
        self.projection = nn.Conv3d(3, width, self.stride, self.stride)

    def forward(self, video):
        if video.ndim != 5 or video.shape[1] != 3:
            raise ValueError("Expected video [B, 3, T, H, W].")
        if any(n <= 0 or n % s for n, s in zip(video.shape[2:], self.stride)):
            raise ValueError(
                "Video dimensions must be positive multiples of tubelet strides."
            )
        return self.projection(video).flatten(2).transpose(1, 2)


# endregion: tubelets


# region: positions
def positions(grid, width):
    """Builds original absolute 3D sin/cos with uniform_power=True."""
    validate_grid(grid)
    if not isinstance(width, int) or width <= 0:
        raise ValueError("Position width must be a positive integer.")
    coordinates = torch.meshgrid(
        *(torch.arange(n) for n in grid), indexing="ij"
    )
    axis_width = 2 * math.ceil(width / 6)
    omega = 10000 ** (
        -torch.arange(axis_width // 2, dtype=torch.float64) / (axis_width / 2)
    )
    axes = []
    for coordinate in coordinates:  # t, h, w; concatenate then truncate.
        phase = coordinate.flatten()[:, None] * omega[None]
        axes.append(torch.cat((phase.sin(), phase.cos()), dim=-1))
    return torch.cat(axes, dim=-1)[:, :width].float().unsqueeze(0)


# endregion: positions


# region: masks
def sample_mask(grid, blocks, scale, generator):
    """Unions possibly overlapping spatial rectangles spanning all time."""
    validate_grid(grid)
    if blocks <= 0 or not 0 < scale < 1 or min(grid[1:]) < 2:
        raise ValueError(
            "Need positive blocks, scale in (0, 1) and spatial grid >= 2."
        )
    _time, height, width = grid
    area = max(1, int(height * width * scale))
    aspect = 0.75 + 0.75 * torch.rand((), generator=generator).item()
    bh = min(height, max(1, round(math.sqrt(area * aspect))))
    bw = min(width, max(1, round(math.sqrt(area / aspect))))
    for _ in range(1000):
        target = torch.zeros(grid, dtype=torch.bool)
        for _ in range(blocks):
            top = torch.randint(height - bh + 1, (), generator=generator).item()
            left = torch.randint(width - bw + 1, (), generator=generator).item()
            target[:, top : top + bh, left : left + bw] = True
        if target.any() and not target.all():
            return (
                ~target
            ).flatten().nonzero().flatten(), target.flatten().nonzero().flatten()
    raise ValueError(
        "Cannot sample nonempty context with these mask parameters."
    )


def short_long_masks(grid, seed=7):
    """Samples separate short-range 8x15% and long-range 2x70% unions."""
    generator = torch.Generator().manual_seed(seed)
    return [
        sample_mask(grid, blocks, scale, generator)
        for blocks, scale in ((8, 0.15), (2, 0.70))
    ]


# endregion: masks


class Block(nn.Module):
    """Pre-norm transformer block with zero dropout and four attention heads."""

    def __init__(self, width):
        super().__init__()
        if width <= 0 or width % 4:
            raise ValueError(
                "Width must be positive and divisible by four heads."
            )
        self.norm1 = nn.LayerNorm(width, eps=1e-6)
        self.attention = nn.MultiheadAttention(width, 4, batch_first=True)
        self.norm2 = nn.LayerNorm(width, eps=1e-6)
        self.mlp = nn.Sequential(
            nn.Linear(width, 4 * width), nn.GELU(), nn.Linear(4 * width, width)
        )

    def forward(self, x):
        normalized = self.norm1(x)
        x = (
            x
            + self.attention(
                normalized, normalized, normalized, need_weights=False
            )[0]
        )
        return x + self.mlp(self.norm2(x))


# region: encoder
class Encoder(nn.Module):
    """Adds original absolute positions, then gathers only visible tokens."""

    def __init__(self, grid=(4, 8, 8), width=48):
        super().__init__()
        validate_grid(grid)
        self.grid = grid
        self.tubelets = Tubelets(width)
        self.register_buffer("position", positions(grid, width))
        self.blocks = nn.Sequential(Block(width), Block(width))
        self.norm = nn.LayerNorm(width, eps=1e-6)

    def forward(self, video, ids=None):
        actual = tuple(n // s for n, s in zip(video.shape[2:], (2, 8, 8)))
        if actual != self.grid:
            raise ValueError(
                "Video shape does not match configured token grid."
            )
        tokens = self.tubelets(video) + self.position
        if ids is not None:
            tokens = tokens[:, ids]  # Masked pixels never enter self-attention.
        return self.norm(self.blocks(tokens))


# endregion: encoder


# region: teacher
@torch.no_grad()
def teacher_targets(teacher, video, masks):
    """Runs the full frozen EMA encoder once and normalizes each token."""
    full = teacher(video)
    full = F.layer_norm(full, (full.shape[-1],))
    return [full[:, target_ids].detach() for _, target_ids in masks]


# endregion: teacher


# region: predictor
class Predictor(nn.Module):
    """Predicts masked features from context plus position-marked queries."""

    def __init__(self, grid=(4, 8, 8), encoder_width=48, width=24):
        super().__init__()
        self.input = nn.Linear(encoder_width, width)
        self.register_buffer("position", positions(grid, width))
        self.mask_tokens = nn.Parameter(torch.zeros(2, 1, 1, width))
        self.block = Block(width)
        self.norm = nn.LayerNorm(width, eps=1e-6)
        self.output = nn.Linear(width, encoder_width)

    def forward(self, context, context_ids, target_ids, mask_index):
        if mask_index not in (0, 1):
            raise ValueError("Mask index must select short or long range.")
        visible = self.input(context) + self.position[:, context_ids]
        queries = self.mask_tokens[mask_index] + self.position[:, target_ids]
        queries = queries.expand(context.shape[0], -1, -1)
        combined = torch.cat((visible, queries), dim=1)
        hidden = self.norm(self.block(combined))
        return self.output(hidden[:, len(context_ids) :])


# endregion: predictor


# region: loss
def masked_l1(predictions, targets):
    """Averages over B, masked tokens, D; then equally over mask views."""
    if not predictions or len(predictions) != len(targets):
        raise ValueError(
            "Predictions and targets must have matching nonempty views."
        )
    losses = []
    for prediction, target in zip(predictions, targets):
        if prediction.shape != target.shape or prediction.numel() == 0:
            raise ValueError(
                "Each prediction and target must have identical nonempty shape."
            )
        losses.append((prediction - target.detach()).abs().mean())
    return torch.stack(losses).mean()


# endregion: loss


# region: ema
@torch.no_grad()
def update_teacher(encoder, teacher, momentum):
    """Updates the EMA teacher after the encoder/predictor optimizer step."""
    if not 0 <= momentum <= 1:
        raise ValueError("EMA momentum must lie in [0, 1].")
    for student, target in zip(encoder.parameters(), teacher.parameters()):
        target.mul_(momentum).add_(student.detach(), alpha=1 - momentum)


def train_step(
    encoder, predictor, teacher, video, masks, optimizer, momentum=0.998
):
    targets = teacher_targets(teacher, video, masks)
    predictions = [
        predictor(encoder(video, c), c, m, index)
        for index, (c, m) in enumerate(masks)
    ]
    loss = masked_l1(predictions, targets)
    optimizer.zero_grad(set_to_none=True)
    loss.backward()
    optimizer.step()
    update_teacher(encoder, teacher, momentum)
    return float(loss.detach())


# endregion: ema


# region: probe
class AttentiveProbe(nn.Module):
    """Learns a query, cross-attention, residual MLP and classifier."""

    def __init__(self, width=48, classes=3):
        super().__init__()
        if width <= 0 or width % 4:
            raise ValueError(
                "Probe width must be positive and divisible by four heads."
            )
        if classes < 2:
            raise ValueError("A classifier needs at least two classes.")
        self.query = nn.Parameter(torch.zeros(1, 1, width))
        self.norm1, self.norm2 = nn.LayerNorm(width), nn.LayerNorm(width)
        self.query_projection = nn.Linear(width, width)
        self.key_value = nn.Linear(width, 2 * width)
        self.mlp = nn.Sequential(
            nn.Linear(width, 4 * width), nn.GELU(), nn.Linear(4 * width, width)
        )
        self.classifier = nn.Linear(width, classes)

    def forward(self, features):
        batch, count, width = features.shape
        query = self.query.expand(batch, -1, -1)
        q = (
            self.query_projection(query)
            .reshape(batch, 1, 4, width // 4)
            .transpose(1, 2)
        )
        kv = self.key_value(self.norm1(features)).reshape(
            batch, count, 2, 4, width // 4
        )
        key, value = kv.permute(2, 0, 3, 1, 4).unbind(0)
        pooled = F.scaled_dot_product_attention(q, key, value)
        query = query + pooled.transpose(1, 2).reshape(batch, 1, width)
        query = query + self.mlp(self.norm2(query))
        return self.classifier(query[:, 0])


def probe_step(encoder, probe, video, labels, optimizer):
    encoder.eval().requires_grad_(False)
    encoder.zero_grad(set_to_none=True)
    with torch.no_grad():
        features = encoder(video)
    logits = probe(features.detach())
    if labels.shape != (video.shape[0],) or labels.dtype != torch.long:
        raise ValueError("Labels must be int64 [B].")
    if (labels < 0).any() or (labels >= logits.shape[-1]).any():
        raise ValueError("Label outside class range.")
    loss = F.cross_entropy(logits, labels)
    optimizer.zero_grad(set_to_none=True)
    loss.backward()
    optimizer.step()  # Only the attentive probe is in this optimizer.
    return float(loss.detach())


# endregion: probe


def smoke():
    """Executes one pretraining update followed by one frozen probing update."""
    torch.set_num_threads(2)
    torch.manual_seed(7)
    video = torch.randn(2, 3, 8, 64, 64)
    encoder, predictor = Encoder(), Predictor()
    teacher = copy.deepcopy(encoder).eval().requires_grad_(False)
    masks = short_long_masks(encoder.grid)
    optimizer = torch.optim.AdamW(
        list(encoder.parameters()) + list(predictor.parameters()), lr=1e-3
    )
    loss = train_step(encoder, predictor, teacher, video, masks, optimizer)
    probe = AttentiveProbe()
    probe_loss = probe_step(
        teacher,
        probe,
        video,
        torch.tensor([0, 2]),
        torch.optim.AdamW(probe.parameters(), lr=1e-3),
    )
    return {
        "scope": "Reduced random-weight training and frozen probe mechanics, no pretrained semantics",
        "videoShape": list(video.shape),
        "grid": list(encoder.grid),
        "maskCounts": [{"context": len(c), "target": len(m)} for c, m in masks],
        "l1": loss,
        "probeCrossEntropy": probe_loss,
        "teacherHasGradient": any(
            p.grad is not None for p in teacher.parameters()
        ),
        "probeHasGradient": any(p.grad is not None for p in probe.parameters()),
    }


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--smoke", action="store_true", required=True)
    parser.parse_args()
    print(json.dumps(smoke(), indent=2))
