# examples/beit_tokenizer.py
"""Restricted CPU adapter for the original OpenAI DALL-E discrete VAE encoder.

Forward methods adapt OpenAI DALL-E revision
5be4b236bc3ade6943662354117a0e83752cc322 (Modified MIT License).
Copyright (c) 2021 OpenAI. See beit-NOTICE.txt beside this download.

Only the pinned encoder is accepted. Seven legacy globals are mapped to native
PyTorch modules inside a scoped weights-only load; no DALL-E or attrs dependency
is installed. The serialized constructors are never run. CPU arithmetic remains
float32, including convolutions that the original implementation halves on GPU.
"""

import hashlib
from pathlib import Path
import shutil
from urllib.request import urlopen

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

ENCODER = {
    "name": "encoder.pkl",
    "url": "https://cdn.openai.com/dall-e/encoder.pkl",
    "sha256": "96b0f478c2e8d912f745f1bdde186efaac4265d38568a52f33bd40d3a15935d4",
    "bytes": 215185363,
    "profile": "frozen DALL-E discrete VAE encoder, 8192 categories",
    "recipe": "OpenAI released pretrained discrete VAE; no tokenizer retraining",
    "license": "Modified MIT (OpenAI)",
    "parameterCount": 53786240,
    "config": {
        "hiddenChannels": 256,
        "groups": 4,
        "blocksPerGroup": 2,
        "inputChannels": 3,
        "vocabSize": 8192,
        "downsampleFactor": 8,
    },
}


def sha256(path):
    """Hash a file without retaining checkpoint bytes in memory."""
    with Path(path).open("rb") as stream:
        return hashlib.file_digest(stream, "sha256").hexdigest()


def cached_file(cache_dir, record):
    """Download once, then require exact pinned bytes on every use."""
    cache_dir = Path(cache_dir)
    cache_dir.mkdir(parents=True, exist_ok=True)
    path = cache_dir / record["name"]
    if not path.exists():
        temporary = path.with_suffix(path.suffix + ".partial")
        try:
            with urlopen(record["url"], timeout=120) as response:
                with temporary.open("wb") as destination:
                    shutil.copyfileobj(response, destination)
            if sha256(temporary) != record["sha256"]:
                raise ValueError(f"SHA256 mismatch for {record['name']}")
            temporary.replace(path)
        finally:
            temporary.unlink(missing_ok=True)
    if sha256(path) != record["sha256"]:
        raise ValueError(f"SHA256 mismatch for {record['name']}")
    if "bytes" in record and path.stat().st_size != record["bytes"]:
        raise ValueError(f"File size mismatch for {record['name']}")
    return path


class Conv2d(nn.Module):
    """Restore saved w/b parameters and use the author's CPU convolution."""

    def forward(self, x):
        return F.conv2d(x.float(), self.w, self.b, padding=(self.kw - 1) // 2)


class EncoderBlock(nn.Module):
    """Restore the released residual paths and their saved post_gain."""

    def forward(self, x):
        return self.id_path(x) + self.post_gain * self.res_path(x)


class Encoder(nn.Module):
    """Restore the released four-group convolutional encoder."""

    group_count = 4
    use_mixed_precision = True

    def forward(self, x):
        if (
            x.ndim != 4
            or x.shape[1] != 3
            or x.dtype != torch.float32
            or x.device.type != "cpu"
            or min(x.shape[2:]) < 8
            or x.shape[2] % 8
            or x.shape[3] % 8
        ):
            raise ValueError(
                "encoder needs CPU float32 [B,3,H,W], H/W divisible by 8"
            )
        return self.blocks(x)


def tokenizer_globals(encoder=Encoder, block=EncoderBlock, conv=Conv2d):
    """Exactly the seven globals present in the released legacy pickle."""
    return [
        (encoder, "dall_e.encoder.Encoder"),
        (block, "dall_e.encoder.EncoderBlock"),
        (conv, "dall_e.utils.Conv2d"),
        nn.Sequential,
        nn.ReLU,
        nn.Identity,
        nn.MaxPool2d,
    ]


def restricted_load(path, globals_list=None):
    """Reject unknown pickle globals before scoped weights-only deserialization."""
    allowed = tokenizer_globals() if globals_list is None else globals_list
    names = {
        item[1]
        if isinstance(item, tuple)
        else f"{item.__module__}.{item.__qualname__}"
        for item in allowed
    }
    expected_names = {
        item[1]
        if isinstance(item, tuple)
        else f"{item.__module__}.{item.__qualname__}"
        for item in tokenizer_globals()
    }
    if names != expected_names:
        raise ValueError(
            "tokenizer compatibility globals must be exactly the seven release types"
        )
    # Do not let unrelated caller-added safe globals widen this loader's scope.
    previous = torch.serialization.get_safe_globals().copy()
    torch.serialization.clear_safe_globals()
    try:
        unknown = (
            set(torch.serialization.get_unsafe_globals_in_checkpoint(path))
            - names
        )
        if unknown:
            raise ValueError(
                f"Unknown tokenizer pickle globals: {sorted(unknown)}"
            )
        with torch.serialization.safe_globals(allowed):
            return torch.load(path, map_location="cpu", weights_only=True)
    finally:
        torch.serialization.clear_safe_globals()
        torch.serialization.add_safe_globals(previous)


def validate_encoder(model):
    """Check architecture, every tensor shape, finite weights, and residual gains."""
    if type(model) is not Encoder:
        raise ValueError("checkpoint is not the released encoder")
    config = (
        model.n_hid,
        model.n_blk_per_group,
        model.input_channels,
        model.vocab_size,
    )
    if config != (256, 2, 3, 8192):
        raise ValueError("unexpected DALL-E encoder configuration")
    expected = {}

    def convolution(name, incoming, outgoing, kernel):
        expected[name + ".w"] = (outgoing, incoming, kernel, kernel)
        expected[name + ".b"] = (outgoing,)
        module = model.get_submodule(name)
        if type(module) is not Conv2d or (
            module.n_in,
            module.n_out,
            module.kw,
        ) != (incoming, outgoing, kernel):
            raise ValueError(f"unexpected convolution {name}")

    convolution("blocks.input", 3, 256, 7)
    for group in range(1, 5):
        outgoing = 256 * 2 ** (group - 1)
        for block in (1, 2):
            prefix = f"blocks.group_{group}.block_{block}"
            incoming = outgoing // 2 if group > 1 and block == 1 else outgoing
            module = model.get_submodule(prefix)
            if type(module) is not EncoderBlock or module.post_gain != 1 / 64:
                raise ValueError("unexpected residual block or gain")
            if incoming != outgoing:
                convolution(prefix + ".id_path", incoming, outgoing, 1)
            elif type(module.id_path) is not nn.Identity:
                raise ValueError("unexpected identity path")
            for index, (n_in, n_out, kernel) in enumerate(
                [
                    (incoming, outgoing // 4, 3),
                    (outgoing // 4, outgoing // 4, 3),
                    (outgoing // 4, outgoing // 4, 3),
                    (outgoing // 4, outgoing, 1),
                ],
                start=1,
            ):
                convolution(
                    f"{prefix}.res_path.conv_{index}", n_in, n_out, kernel
                )
        if group < 4:
            pool = model.get_submodule(f"blocks.group_{group}.pool")
            if (
                type(pool) is not nn.MaxPool2d
                or pool.kernel_size != 2
                or pool.stride != 2
                or pool.padding != 0
            ):
                raise ValueError("unexpected tokenizer downsampling")
    convolution("blocks.output.conv", 2048, 8192, 1)
    state = model.state_dict()
    if set(state) != set(expected):
        raise ValueError("unexpected encoder tensor keys")
    for name, shape in expected.items():
        tensor = state[name]
        if (
            tuple(tensor.shape) != shape
            or tensor.dtype != torch.float32
            or not torch.isfinite(tensor).all()
        ):
            raise ValueError(f"invalid encoder tensor {name}")
    if sum(p.numel() for p in model.parameters()) != ENCODER["parameterCount"]:
        raise ValueError("unexpected encoder parameter count")


def load_tokenizer(cache_dir):
    """Return the pinned, validated, frozen native CPU DALL-E encoder."""
    model = restricted_load(cached_file(cache_dir, ENCODER))
    validate_encoder(model)
    return model.cpu().float().eval().requires_grad_(False)
