# examples/alexnet.py
"""Offline reduced AlexNet mechanics, authored from the 2012 paper.

Reference: §§3.1–3.5, Figure 2, §§4.1–4.2 and §5 at
https://papers.nips.cc/paper_files/paper/2012/file/c399862d3b9d6b76c8436e924a68c45b-Paper.pdf
This is not the original CUDA/C++ release or torchvision's modern AlexNet.
Synthetic inputs and random reduced weights have no pretrained semantics.
Import does not seed RNGs, allocate a model, download, or run a forward pass.
"""

import argparse
import hashlib
import json
from pathlib import Path

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


# region: geometry
def spatial_plan(input_size, conv1_padding):
    """Return exact square sizes; reject configurations with empty pooling."""
    if type(input_size) is not int or input_size <= 0:
        raise ValueError("input_size must be a positive integer")
    if type(conv1_padding) is not int or conv1_padding not in (0, 2):
        raise ValueError("conv1_padding must be 0 or 2")
    # Explicit choices: 224/pad2 -> 55; 227/pad0 -> 55; 224/pad0 -> 54.
    operations = [
        ("conv1", 11, 4, conv1_padding),
        ("pool1", 3, 2, 0),
        ("conv2", 5, 1, 2),
        ("pool2", 3, 2, 0),
        ("conv3", 3, 1, 1),
        ("conv4", 3, 1, 1),
        ("conv5", 3, 1, 1),
        ("pool5", 3, 2, 0),
    ]
    sizes = {}
    size = input_size
    for name, kernel, stride, padding in operations:
        size = (size + 2 * padding - kernel) // stride + 1
        if size <= 0:
            raise ValueError(f"input geometry yields an empty {name}")
        sizes[name] = size
    return sizes


def crop_input(images, size, top=0, left=0, *, flip=False):
    """Take a declared crop and optional reflection, not full augmentation."""
    if images.ndim != 4 or images.shape[1] != 3 or images.shape[0] < 1:
        raise ValueError("images must have nonempty shape [B, 3, H, W]")
    if not images.is_floating_point() or not torch.isfinite(images).all():
        raise ValueError("images must be finite floating-point tensors")
    if any(type(v) is not int for v in (size, top, left)):
        raise ValueError("crop size and offsets must be integers")
    if size < 1 or min(top, left) < 0:
        raise ValueError("crop must be positive with nonnegative offsets")
    if top + size > images.shape[2] or left + size > images.shape[3]:
        raise ValueError("crop exceeds image boundaries")
    crop = images[:, :, top : top + size, left : left + size]
    return crop.flip(-1) if flip else crop


# endregion: geometry


# region: grouped_convs
def grouped_convs(channels, conv1_padding):
    """Represent original two-GPU connectivity serially with channel groups."""
    invalid_width = any(type(c) is not int or c <= 0 or c % 2 for c in channels)
    if len(channels) != 5 or invalid_width:
        raise ValueError("five positive even channel counts are required")
    c1, c2, c3, c4, c5 = channels
    return nn.ModuleList(
        [
            nn.Conv2d(3, c1, 11, stride=4, padding=conv1_padding),
            nn.Conv2d(c1, c2, 5, padding=2, groups=2),
            nn.Conv2d(c2, c3, 3, padding=1, groups=1),
            nn.Conv2d(c3, c4, 3, padding=1, groups=2),
            nn.Conv2d(c4, c5, 3, padding=1, groups=2),
        ]
    )


# endregion: grouped_convs


# region: relu_lrn
def relu_lrn(x, *, normalize=True):
    """Apply ReLU, then the paper's channel-window SUM normalization."""
    activity = F.relu(x)
    if not normalize:
        return activity
    # PyTorch divides its window sum by size; alpha=5e-4 gives 1e-4 * SUM.
    # Padding zeros outside the channel range preserves clipped edge windows.
    return F.local_response_norm(activity, size=5, alpha=5e-4, beta=0.75, k=2.0)


# endregion: relu_lrn


# region: overlap_pool
def overlap_pool(x):
    """Pool overlapping 3 x 3 neighborhoods at stride 2, without padding."""
    if x.ndim != 4 or min(x.shape[-2:]) < 3:
        raise ValueError("pool input must be [B, C, H, W] with H, W >= 3")
    return F.max_pool2d(x, kernel_size=3, stride=2, padding=0, ceil_mode=False)


# endregion: overlap_pool


# region: classifier_dropout
def classifier_dropout(channels, spatial, hidden, classes, dropout):
    """Build two FC/ReLU/dropout stages followed by unactivated class logits."""
    if (
        type(hidden) is not int
        or hidden < 1
        or type(classes) is not int
        or classes < 2
    ):
        raise ValueError("hidden must be positive and classes must be >= 2")
    if not isinstance(dropout, (int, float)) or not 0 <= dropout < 1:
        raise ValueError("dropout must be in [0, 1)")
    # Modern inverted dropout: scale kept outputs by 1/(1-p) during TRAINING.
    # Evaluation is identity; do not also apply the paper's test-time scaling.
    return nn.Sequential(
        nn.Flatten(1),
        nn.Linear(channels * spatial * spatial, hidden),
        nn.ReLU(),
        nn.Dropout(dropout),
        nn.Linear(hidden, hidden),
        nn.ReLU(),
        nn.Dropout(dropout),
        nn.Linear(hidden, classes),
    )


# endregion: classifier_dropout


# region: objective
def objective(logits, labels):
    """Mean multinomial negative log likelihood from raw class logits."""
    if logits.ndim != 2 or logits.shape[0] < 1 or logits.shape[1] < 2:
        raise ValueError("logits must be nonempty [B, K], K >= 2")
    if not logits.is_floating_point() or not torch.isfinite(logits).all():
        raise ValueError("logits must be finite floating-point values")
    if labels.dtype != torch.int64 or labels.shape != (logits.shape[0],):
        raise ValueError("labels must be int64 [B]")
    if (
        labels.device != logits.device
        or (labels < 0).any()
        or (labels >= logits.shape[1]).any()
    ):
        raise ValueError("labels need valid indices on the logits device")
    # cross_entropy internally performs log-softmax; do not softmax first.
    return F.cross_entropy(logits, labels)


# endregion: objective


class AlexNet(nn.Module):
    """Reduced channels with explicit geometry and historical connectivity.

    Original widths: (96, 256, 384, 384, 256), hidden=4096, classes=1000.
    Defaults reduce these to (8, 12, 16, 16, 12), hidden=32, classes=5.
    The 67/pad0 default reaches pool5=1; no adaptive pooling hides geometry.
    Two-GPU execution, PCA color jitter, ten-crop inference and learned
    semantics are outside this serial reference.
    """

    def __init__(
        self,
        input_size=67,
        conv1_padding=0,
        channels=(8, 12, 16, 16, 12),
        hidden=32,
        classes=5,
        dropout=0.5,
    ):
        super().__init__()
        self.input_size = input_size
        self.sizes = spatial_plan(input_size, conv1_padding)
        self.convs = grouped_convs(channels, conv1_padding)
        self.classifier = classifier_dropout(
            channels[-1], self.sizes["pool5"], hidden, classes, dropout
        )
        # §5 Gaussian weights; positive biases in conv2/4/5 and FC6/7.
        for index, layer in enumerate(self.convs):
            nn.init.normal_(layer.weight, mean=0.0, std=0.01)
            nn.init.constant_(layer.bias, 1.0 if index in (1, 3, 4) else 0.0)
        for layer in self.classifier:
            if isinstance(layer, nn.Linear):
                nn.init.normal_(layer.weight, mean=0.0, std=0.01)
                bias = 0.0 if layer is self.classifier[-1] else 1.0
                nn.init.constant_(layer.bias, bias)

    def features(self, images, *, return_trace=False):
        """Run conv → ReLU → optional LRN → optional pool in paper order."""
        expected = (3, self.input_size, self.input_size)
        if (
            images.ndim != 4
            or images.shape[0] < 1
            or images.shape[1:] != expected
        ):
            raise ValueError("images need nonempty [B, 3, size, size] shape")
        if not images.is_floating_point() or not torch.isfinite(images).all():
            raise ValueError("images must be finite floating-point values")
        trace = {}
        x = images
        for index, conv in enumerate(self.convs, start=1):
            x = relu_lrn(conv(x), normalize=index in (1, 2))
            trace[f"conv{index}"] = list(x.shape)
            if index in (1, 2, 5):
                x = overlap_pool(x)
                trace[f"pool{index}"] = list(x.shape)
        return (x, trace) if return_trace else x

    def forward(self, images):
        """Return raw logits; dropout follows the module train/eval state."""
        return self.classifier(self.features(images))


def smoke(seed):
    """Measure one seeded CPU SGD step and emit finite mechanics receipts."""
    torch.set_num_threads(1)
    torch.manual_seed(seed)
    model = AlexNet()
    source = torch.randn(2, 3, 71, 71)
    images = crop_input(source, 67, 2, 1, flip=True)
    labels = torch.tensor([0, 1])
    _, shapes = model.features(images, return_trace=True)
    optimizer = torch.optim.SGD(
        model.parameters(), lr=0.01, momentum=0.9, weight_decay=0.0005
    )
    before = model.convs[0].weight.detach().clone()
    optimizer.zero_grad(set_to_none=True)
    logits = model(images)
    loss = objective(logits, labels)
    loss.backward()
    gradients = {
        name: float(p.grad.norm()) for name, p in model.named_parameters()
    }
    optimizer.step()
    change = float((model.convs[0].weight.detach() - before).norm())
    model.eval()
    with torch.no_grad():
        eval_logits = model(images)
        eval_repeat_error = float((eval_logits - model(images)).abs().max())
        activity = torch.rand(1, 8, 2, 2) * 10
        normalized = relu_lrn(activity)
        oracle = torch.stack(
            [
                activity[:, i]
                / (
                    2
                    + 1e-4 * activity[:, max(0, i - 2) : i + 3].square().sum(1)
                ).pow(0.75)
                for i in range(8)
            ],
            1,
        )
        lrn_error = float((normalized - oracle).abs().max())
        layer = model.convs[1]
        grouped_x = torch.randn(1, 8, 5, 5)
        altered = grouped_x.clone()
        altered[:, :4] += 1
        grouped_error = float(
            (layer(grouped_x)[:, 6:] - layer(altered)[:, 6:]).abs().max()
        )
        train_dropout = nn.Dropout(0.5).train()(torch.ones(100000))
    receipt = {
        "source_sha256": hashlib.sha256(
            Path(__file__).read_bytes()
        ).hexdigest(),
        "seed": seed,
        "device": "cpu",
        "torch_version": torch.__version__,
        "boundary": (
            "Reduced random network and synthetic images; no pretrained "
            "semantics, native GPU speed, or ImageNet reproduction."
        ),
        "reductions": {
            "channels": [8, 12, 16, 16, 12],
            "hidden": 32,
            "classes": 5,
            "input_size": 67,
            "conv1_padding": 0,
            "batch": 2,
        },
        "shapes": {
            "input": list(images.shape),
            **shapes,
            "logits": list(logits.shape),
        },
        "geometry_224_pad2": spatial_plan(224, 2),
        "geometry_227_pad0": spatial_plan(227, 0),
        "loss": float(loss.detach()),
        "gradient_norms": gradients,
        "conv1_update_norm": change,
        "lrn_sum_oracle_max_error": lrn_error,
        "grouped_untouched_max_error": grouped_error,
        "dropout_training_mean": float(train_dropout.mean()),
        "eval_repeat_max_error": eval_repeat_error,
        "finite_eval_logits": bool(torch.isfinite(eval_logits).all()),
    }
    # Reject nonfinite measurements instead of emitting invalid JSON.
    return receipt


def main():
    """Run only the explicitly requested synthetic smoke, offline."""
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--smoke", action="store_true", required=True)
    parser.add_argument("--seed", type=int, default=7)
    args = parser.parse_args()
    print(json.dumps(smoke(args.seed), indent=2, allow_nan=False))


if __name__ == "__main__":
    main()
