# examples/blip.py
"""Seeded CPU MED/objective mechanics; no pretrained semantic predictions.

Conventions: salesforce/BLIP revision
073b821aa2a1ea9dfc56c66d7cfc17e25d755608, models/blip_pretrain.py and med.py.
One text block and a linear patch encoder replace BERT/ViT. The local momentum
queue retains detached ITC soft targets, but omits distributed all-gather.
CapFilt accepts supplied captions/scores from independently fine-tuned modules.
"""

import argparse
import copy
import hashlib
import json
from pathlib import Path

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

REVISION = "073b821aa2a1ea9dfc56c66d7cfc17e25d755608"
PAD, CLS, ENC, DEC = 0, 1, 2, 3


def validate_tokens(tokens, vocab=16, max_length=8):
    """Reject malformed batches before attention can produce nonfinite outputs."""
    if tokens.ndim != 2 or tokens.dtype != torch.long:
        raise ValueError("tokens must be long [B, L]")
    if tokens.shape[0] < 1 or not 2 <= tokens.shape[1] <= max_length:
        raise ValueError("require B >= 1 and 2 <= L <= max_length")
    if (tokens < 0).any() or (tokens >= vocab).any():
        raise ValueError("token outside vocabulary")
    if (tokens[:, 0] == PAD).any() or (tokens[:, 1:] != PAD).sum(1).min() == 0:
        raise ValueError("each sequence needs a start and a next-token target")
    if ((tokens[:, :-1] == PAD) & (tokens[:, 1:] != PAD)).any():
        raise ValueError("padding must be a suffix")


# region: med_sharing
class SharedText(nn.Module):
    """Embeddings, cross-attention and FFN shared by encoder and decoder."""

    def __init__(self, width=8, vocab=16):
        super().__init__()
        self.embedding = nn.Embedding(vocab, width, padding_idx=PAD)
        self.position = nn.Embedding(8, width)
        self.cross = nn.MultiheadAttention(width, 2, batch_first=True)
        self.cross_norm = nn.LayerNorm(width)
        self.ffn = nn.Sequential(
            nn.Linear(width, 16), nn.GELU(), nn.Linear(16, width)
        )
        self.ffn_norm = nn.LayerNorm(width)


class TextPath(nn.Module):
    """Each path owns separate self-attention and its associated LayerNorm."""

    def __init__(self, shared, width=8):
        super().__init__()
        self.shared = shared
        self.self_attention = nn.MultiheadAttention(width, 2, batch_first=True)
        self.self_norm = nn.LayerNorm(width)

    def forward(self, tokens, image=None, causal=False):
        validate_tokens(tokens)
        shared = self.shared
        x = shared.embedding(tokens) + shared.position(
            torch.arange(tokens.shape[1])
        )
        blocked = attention_mask(tokens.shape[1], causal)
        attended = self.self_attention(
            x,
            x,
            x,
            attn_mask=blocked,
            key_padding_mask=tokens == PAD,
            need_weights=False,
        )[0]
        x = self.self_norm(x + attended)
        if image is not None:
            x = shared.cross_norm(
                x + shared.cross(x, image, image, need_weights=False)[0]
            )
        return shared.ffn_norm(x + shared.ffn(x))


class MED(nn.Module):
    """Tiny trainable image/text model preserving the three parameter routes."""

    def __init__(self):
        super().__init__()
        shared = SharedText()
        self.encoder = TextPath(shared)
        self.decoder = TextPath(shared)
        self.image_encoder = nn.Linear(6, 8)
        self.image_projection = nn.Linear(8, 4)
        self.text_projection = nn.Linear(8, 4)
        self.itm_head = nn.Linear(8, 2)
        self.lm_head = nn.Linear(8, 16)
        self.temperature = nn.Parameter(torch.tensor(0.07))

    def image(self, patches):
        if patches.ndim != 3 or patches.shape[1] < 1 or patches.shape[2] != 6:
            raise ValueError("patches must have shape [B, P >= 1, 6]")
        if not torch.isfinite(patches).all():
            raise ValueError("patches must be finite")
        return self.image_encoder(patches)


# endregion: med_sharing


# region: masks
def attention_mask(length, causal):
    """True blocks a key; decoder position i cannot attend to j > i."""
    if not isinstance(length, int) or length < 1:
        raise ValueError("length must be a positive integer")
    return (
        torch.ones(length, length, dtype=torch.bool).triu(1) if causal else None
    )


# endregion: masks


# region: itc
def unimodal_features(model, image, tokens):
    """Text receives no image, and both projected representations are normalized."""
    text = model.encoder(tokens)
    # Mean patches replace the full ViT's learned CLS token in this reference.
    image_features = F.normalize(model.image_projection(image.mean(1)), dim=-1)
    text_features = F.normalize(model.text_projection(text[:, 0]), dim=-1)
    return image_features, text_features


def contrastive_loss(image, text, bank_image, bank_text, targets, temperature):
    """Symmetric soft-label ITC against detached momentum current/queue keys."""
    i2t = image @ bank_text.T / temperature
    t2i = text @ bank_image.T / temperature
    losses = [
        -(target * F.log_softmax(logits, dim=1)).sum(1).mean()
        for logits, target in zip((i2t, t2i), targets)
    ]
    return (losses[0] + losses[1]) / 2, i2t, t2i


# endregion: itc


# region: momentum_targets
class MomentumBank:
    """Frozen EMA teacher and a local FIFO of normalized paired ITC features."""

    def __init__(self, model, capacity=8):
        if not isinstance(capacity, int) or capacity < 1:
            raise ValueError("capacity must be positive")
        self.teacher = copy.deepcopy(model).requires_grad_(False)
        self.images = F.normalize(torch.randn(capacity, 4), dim=-1)
        self.texts = F.normalize(torch.randn(capacity, 4), dim=-1)
        self.pointer = 0

    @torch.no_grad()
    def targets(self, model, patches, tokens, alpha=0.4, momentum=0.995):
        if not 0 <= alpha <= 1 or not 0 <= momentum <= 1:
            raise ValueError("alpha and momentum must be in [0, 1]")
        for teacher, online in zip(
            self.teacher.parameters(), model.parameters()
        ):
            teacher.mul_(momentum).add_(online, alpha=1 - momentum)
        image, text = unimodal_features(
            self.teacher, self.teacher.image(patches), tokens
        )
        bank_image = torch.cat((image, self.images.clone()))
        bank_text = torch.cat((text, self.texts.clone()))
        hard = torch.zeros(image.shape[0], bank_text.shape[0])
        hard[:, : image.shape[0]].fill_diagonal_(1)
        soft_i = (image @ bank_text.T / model.temperature).softmax(1)
        soft_t = (text @ bank_image.T / model.temperature).softmax(1)
        targets = (
            alpha * soft_i + (1 - alpha) * hard,
            alpha * soft_t + (1 - alpha) * hard,
        )
        return bank_image, bank_text, targets, image, text

    @torch.no_grad()
    def enqueue(self, images, texts):
        # Serial local ring supports any batch size; upstream uses all-gather
        # and requires queue capacity divisible by the global batch size.
        for image, text in zip(images, texts):
            self.images[self.pointer] = image.detach()
            self.texts[self.pointer] = text.detach()
            self.pointer = (self.pointer + 1) % len(self.images)


# endregion: momentum_targets


# region: hard_negatives
def sample_negatives(similarity, generator):
    """Sample each row's non-positive with upstream's 1e-4 probability floor."""
    if similarity.ndim != 2 or similarity.shape[0] != similarity.shape[1]:
        raise ValueError("similarity must be square [B, B]")
    if similarity.shape[0] < 2 or not torch.isfinite(similarity).all():
        raise ValueError("hard-negative mining needs finite logits and B >= 2")
    with torch.no_grad():
        weights = similarity.softmax(1) + 1e-4
        weights.fill_diagonal_(0)
        return torch.multinomial(weights, 1, generator=generator).squeeze(1)


# endregion: hard_negatives


# region: itm
def matching_loss(model, image, tokens, negative_image, negative_text):
    """One matched and two unmatched batches, read at the Encode token."""
    grounded_tokens = tokens.clone()
    grounded_tokens[:, 0] = ENC
    all_tokens = torch.cat(
        (grounded_tokens, grounded_tokens, grounded_tokens[negative_text])
    )
    all_images = torch.cat((image, image[negative_image], image))
    hidden = model.encoder(all_tokens, all_images)
    logits = model.itm_head(hidden[:, 0])
    labels = torch.cat(
        (torch.ones(len(tokens)), torch.zeros(2 * len(tokens)))
    ).long()
    return F.cross_entropy(logits, labels), logits


# endregion: itm


# region: lm
def language_loss(logits, tokens):
    """Shift next-token targets once; ignore suffix pads and smooth labels 0.1."""
    validate_tokens(tokens)
    if logits.shape != (*tokens.shape, 16) or not torch.isfinite(logits).all():
        raise ValueError("finite logits must have shape [B, L, 16]")
    labels = tokens[:, 1:].clone().masked_fill(tokens[:, 1:] == PAD, -100)
    # The pinned pretrain call uses med.py's default mean reduction.
    return F.cross_entropy(
        logits[:, :-1].reshape(-1, 16),
        labels.reshape(-1),
        ignore_index=-100,
        label_smoothing=0.1,
    )


def decoder_loss(model, image, tokens):
    """Replace CLS by Decode without mutating input; hidden i predicts token i+1."""
    decoder_tokens = tokens.clone()
    decoder_tokens[:, 0] = DEC
    logits = model.lm_head(model.decoder(decoder_tokens, image, causal=True))
    return language_loss(logits, tokens)


# endregion: lm


# region: joint_update
def train_step(model, bank, patches, tokens, optimizer, generator):
    """EMA before teacher forward, enqueue detached keys, optimize three losses."""
    validate_tokens(tokens)
    if len(tokens) < 2 or len(patches) != len(tokens):
        raise ValueError("training needs aligned B >= 2 batches")
    optimizer.zero_grad(set_to_none=True)
    with torch.no_grad():
        model.temperature.clamp_(0.001, 0.5)
    image = model.image(
        patches
    )  # One online image forward reused by all losses.
    image_f, text_f = unimodal_features(model, image, tokens)
    bank_i, bank_t, targets, teacher_i, teacher_t = bank.targets(
        model, patches, tokens
    )
    itc, i2t, t2i = contrastive_loss(
        image_f, text_f, bank_i, bank_t, targets, model.temperature
    )
    bank.enqueue(teacher_i, teacher_t)
    negative_image = sample_negatives(t2i[:, : len(tokens)], generator)
    negative_text = sample_negatives(i2t[:, : len(tokens)], generator)
    itm, _ = matching_loss(model, image, tokens, negative_image, negative_text)
    lm = decoder_loss(model, image, tokens)
    total = itc + itm + lm
    total.backward()
    gradients = {
        name: float(module.weight.grad.norm())
        for name, module in (
            ("image_encoder", model.image_encoder),
            ("itm_head", model.itm_head),
            ("lm_head", model.lm_head),
        )
    }
    optimizer.step()
    return {
        "itc": float(itc.detach()),
        "itm": float(itm.detach()),
        "lm": float(lm.detach()),
        "total": float(total.detach()),
        "gradients": gradients,
        "negative_image": negative_image.tolist(),
        "negative_text": negative_text.tolist(),
    }


# endregion: joint_update


# region: capfilt
def filter_candidates(original, generated, original_scores, generated_scores):
    """Filter both candidate sets independently using supplied [unmatched, match] scores.

    These are fixture logits, not semantic predictions of the random MED.
    Captioner and filter are separately fine-tuned copies in actual CapFilt.
    Ties predict unmatched (class 0), matching the binary-head argmax rule.
    """
    if len(original) != len(generated) or not all(
        isinstance(s, str) and s for s in [*original, *generated]
    ):
        raise ValueError(
            "provide one nonempty original/generated caption per image"
        )
    kept = []
    for label, captions, scores in (
        ("original", original, original_scores),
        ("generated", generated, generated_scores),
    ):
        if (
            scores.shape != (len(captions), 2)
            or not torch.isfinite(scores).all()
        ):
            raise ValueError("candidate scores must be finite [B, 2]")
        kept.extend(
            (i, label, caption)
            for i, caption in enumerate(captions)
            if scores[i].argmax().item() == 1
        )
    return kept


# endregion: capfilt


def smoke(seed=7):
    """Return measured finite mechanics and source provenance, without downloads."""
    torch.manual_seed(seed)
    torch.set_num_threads(1)
    model = MED()
    bank = MomentumBank(model)
    patches = torch.randn(3, 4, 6)
    tokens = torch.tensor(
        [[CLS, 4, 5, 6, 0], [CLS, 7, 8, 0, 0], [CLS, 9, 10, 11, 12]]
    )
    before = model.image_encoder.weight.detach().clone()
    generator = torch.Generator().manual_seed(seed)
    observation = train_step(
        model,
        bank,
        patches,
        tokens,
        torch.optim.SGD(model.parameters(), lr=0.01),
        generator,
    )
    with torch.no_grad():
        image = model.image(patches)
        text_before = unimodal_features(model, image, tokens)[1]
        text_after = unimodal_features(model, image + 1, tokens)[1]
        unimodal_delta = (text_before - text_after).abs().max()
        grounded_delta = (
            (model.encoder(tokens, image) - model.encoder(tokens, image + 1))
            .abs()
            .max()
        )
        plain = torch.tensor([[DEC, 4, 5, 6, 7]])
        changed = plain.clone()
        changed[:, -1] = 13
        causal_delta = (
            (
                model.decoder(plain, image[:1], True)[:, :-1]
                - model.decoder(changed, image[:1], True)[:, :-1]
            )
            .abs()
            .max()
        )
    kept = filter_candidates(
        ["fixture original A", "fixture original B"],
        ["fixture generated A", "fixture generated B"],
        torch.tensor([[0.0, 1.0], [1.0, 0.0]]),
        torch.tensor([[1.0, 0.0], [0.0, 1.0]]),
    )
    return {
        "source_sha256": hashlib.sha256(
            Path(__file__).read_bytes()
        ).hexdigest(),
        "primary_revision": REVISION,
        "seed": seed,
        "reductions": "one block width 8, linear patch encoder, dim 4, local queue 8; no distributed all-gather or pretrained semantics",
        "shapes": {
            "patches": list(patches.shape),
            "tokens": list(tokens.shape),
            "itc_bank": [3, 11],
        },
        "objectives": observation,
        "queue_pointer": bank.pointer,
        "teacher_has_grad": any(
            p.grad is not None for p in bank.teacher.parameters()
        ),
        "image_weight_delta": float(
            (before - model.image_encoder.weight.detach()).abs().max()
        ),
        "unimodal_image_delta": float(unimodal_delta),
        "grounded_image_delta": float(grounded_delta),
        "causal_future_delta": float(causal_delta),
        "capfilt_fixture_kept": kept,
    }


def main():
    """Print actual deterministic smoke JSON only on explicit invocation."""
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--smoke", action="store_true")
    parser.add_argument("--seed", type=int, default=7)
    args = parser.parse_args()
    if not args.smoke:
        parser.error("use --smoke for the offline numerical reference")
    print(json.dumps(smoke(args.seed), indent=2, allow_nan=False))


if __name__ == "__main__":
    main()
