# examples/ddpm.py
"""Seeded offline DDPM vector mechanics, not an image-generation reproduction.

Grounding: Ho et al. (2020), Equations 4, 7, 11, 14 and Algorithms 1–2.
Original convention: hojonathanho/diffusion at
1e0dceb3b3495bbe19116a5e1b3596cd0706c543, diffusion_tf/diffusion_utils_2.py.
Uses the linear endpoints, epsilon prediction, fixed posterior variance,
no x0 clipping and feature-mean then batch-mean MSE (original code convention).
"""

import argparse
import hashlib
import json
from pathlib import Path

import torch
from torch import nn

DTYPE = torch.float64


# region: schedule
def schedule(steps):
    """Build original linear endpoints; index i means paper time t=i+1."""
    if not isinstance(steps, int) or isinstance(steps, bool) or steps < 2:
        raise ValueError("steps must be an integer >= 2")
    beta = torch.linspace(1e-4, 0.02, steps, dtype=DTYPE)
    alpha = 1.0 - beta
    alpha_bar = torch.cumprod(alpha, dim=0)
    previous = torch.cat((torch.ones(1, dtype=DTYPE), alpha_bar[:-1]))
    posterior_variance = beta * (1.0 - previous) / (1.0 - alpha_bar)
    return {"beta": beta, "alpha": alpha, "alpha_bar": alpha_bar,
            "previous": previous, "posterior_variance": posterior_variance}
# endregion: schedule


def check_pair(x, other):
    """Reject empty, nonfinite, mismatched or non-CPU float64 vectors."""
    for value in (x, other):
        if (value.ndim != 2 or min(value.shape) < 1 or
                value.dtype != DTYPE or value.device.type != "cpu" or
                not torch.isfinite(value).all()):
            raise ValueError("expected nonempty finite CPU float64 [B,D]")
    if x.shape != other.shape:
        raise ValueError("tensor shapes must match; implicit broadcasting forbidden")


def gather(values, indices, batch):
    """Gather one coefficient per example with an explicit feature axis."""
    if (indices.dtype != torch.long or indices.device.type != "cpu" or
            indices.shape != (batch,) or (indices < 0).any() or
            (indices >= len(values)).any()):
        raise ValueError("indices must be valid CPU int64 [B]")
    return values[indices, None]


# region: sample_training_batch
def sample_training_batch(batch, dimensions, steps, generator):
    """Sample synthetic clean vectors, independent timesteps and noise."""
    if any(not isinstance(n, int) or isinstance(n, bool) or n < 1
           for n in (batch, dimensions, steps)):
        raise ValueError("batch, dimensions and steps must be positive integers")
    x0 = torch.randn(batch, dimensions, generator=generator, dtype=DTYPE)
    indices = torch.randint(steps, (batch,), generator=generator)
    epsilon = torch.randn(batch, dimensions, generator=generator, dtype=DTYPE)
    return x0, indices, epsilon
# endregion: sample_training_batch


# region: q_sample
def q_sample(x0, indices, epsilon, coefficients):
    """Sample x_t directly, without traversing earlier forward steps."""
    check_pair(x0, epsilon)
    alpha_bar = gather(coefficients["alpha_bar"], indices, x0.shape[0])
    signal = alpha_bar.sqrt() * x0
    noise = (1.0 - alpha_bar).sqrt() * epsilon
    return signal + noise
# endregion: q_sample


# region: epsilon_model
class EpsilonModel(nn.Module):
    """Tiny time-conditioned MLP; this is not the paper's image U-Net."""

    def __init__(self, dimensions, hidden, steps, seed):
        super().__init__()
        if any(not isinstance(n, int) or isinstance(n, bool) or n < 1
               for n in (dimensions, hidden, steps)):
            raise ValueError("model dimensions and steps must be positive integers")
        self.steps = steps
        with torch.random.fork_rng(devices=[]):
            torch.manual_seed(seed)
            self.network = nn.Sequential(
                nn.Linear(dimensions + 1, hidden, dtype=DTYPE), nn.Tanh(),
                nn.Linear(hidden, dimensions, dtype=DTYPE))

    def forward(self, xt, indices):
        """Concatenate normalized paper time and return predicted [B,D] noise."""
        check_pair(xt, xt)
        gather(torch.ones(self.steps, dtype=DTYPE), indices, xt.shape[0])
        time = (indices.to(DTYPE) + 1.0)[:, None] / self.steps
        return self.network(torch.cat((xt, time), dim=1))
# endregion: epsilon_model


# region: training_loss
def training_loss(model, xt, indices, epsilon):
    """Use feature-mean then batch-mean MSE, not the full weighted ELBO."""
    check_pair(xt, epsilon)
    prediction = model(xt, indices)
    per_example = (prediction - epsilon).square().mean(dim=1)
    return per_example.mean(), per_example


def training_step(model, xt, indices, epsilon, learning_rate=0.01):
    """Take one actual SGD update and expose gradients and changed state."""
    if not 0 < learning_rate < float("inf"):
        raise ValueError("learning rate must be finite and positive")
    optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
    before = [parameter.detach().clone() for parameter in model.parameters()]
    optimizer.zero_grad()
    loss, per_example = training_loss(model, xt, indices, epsilon)
    loss.backward()
    gradients = [parameter.grad.detach().clone() for parameter in model.parameters()]
    if not all(torch.isfinite(g).all() for g in gradients):
        raise ValueError("nonfinite gradient")
    optimizer.step()
    with torch.no_grad():
        after_loss, _ = training_loss(model, xt, indices, epsilon)
    return {"loss_before": loss.detach(), "loss_after": after_loss,
            "per_example": per_example.detach(), "before": before,
            "gradients": gradients, "learning_rate": learning_rate}
# endregion: training_loss


# region: reverse_mean
def reverse_mean(xt, indices, epsilon_prediction, coefficients):
    """Evaluate Equation 11 directly, without clipping reconstructed x0."""
    check_pair(xt, epsilon_prediction)
    beta = gather(coefficients["beta"], indices, xt.shape[0])
    alpha = gather(coefficients["alpha"], indices, xt.shape[0])
    alpha_bar = gather(coefficients["alpha_bar"], indices, xt.shape[0])
    return (xt - beta / (1.0 - alpha_bar).sqrt() * epsilon_prediction) / alpha.sqrt()
# endregion: reverse_mean


# region: sample_reverse_step
def sample_reverse_step(xt, indices, epsilon_prediction, noise, coefficients):
    """Use supplied noise and fixed beta-tilde; paper t=1 suppresses noise."""
    check_pair(xt, noise)
    mean = reverse_mean(xt, indices, epsilon_prediction, coefficients)
    variance = gather(coefficients["posterior_variance"], indices, xt.shape[0])
    active = (indices > 0)[:, None]
    return mean + active * variance.sqrt() * noise


@torch.no_grad()
def sample_reverse_loop(model, initial, supplied_noises, coefficients):
    """Visit every declared timestep and return the complete state trace."""
    steps = len(coefficients["beta"])
    check_pair(initial, initial)
    if supplied_noises.shape != (steps, *initial.shape):
        raise ValueError("supplied noises must be [T,B,D]")
    xt = initial.clone()
    trace = []
    for index in range(steps - 1, -1, -1):
        indices = torch.full((xt.shape[0],), index, dtype=torch.long)
        epsilon_prediction = model(xt, indices)
        xt = sample_reverse_step(xt, indices, epsilon_prediction,
                                 supplied_noises[index], coefficients)
        trace.append({"index": index, "paper_time": index + 1,
                      "noise_enabled": index > 0, "state": xt.tolist(),
                      "finite": bool(torch.isfinite(xt).all())})
    return xt, trace
# endregion: sample_reverse_step


def smoke(seed=7):
    """Report actual small-run evidence; no image quality or terminal prior match."""
    batch, dimensions, hidden, steps = 4, 3, 8, 8
    generator = torch.Generator().manual_seed(seed)
    coefficients = schedule(steps)
    x0, indices, epsilon = sample_training_batch(batch, dimensions, steps, generator)
    xt = q_sample(x0, indices, epsilon, coefficients)
    model = EpsilonModel(dimensions, hidden, steps, seed)
    update = training_step(model, xt, indices, epsilon)
    initial = torch.randn(batch, dimensions, generator=generator, dtype=DTYPE)
    noises = torch.randn(steps, batch, dimensions, generator=generator, dtype=DTYPE)
    final, trace = sample_reverse_loop(model, initial, noises, coefficients)
    gradients = update["gradients"]
    gradient_norm = torch.cat([g.flatten() for g in gradients]).norm()
    state_delta = torch.cat([(p.detach() - old).flatten() for p, old
                             in zip(model.parameters(), update["before"])]).norm()
    alpha_terminal = coefficients["alpha_bar"][-1]
    assert gradient_norm > 0 and state_delta > 0
    assert update["loss_after"] < update["loss_before"]
    assert all(row["finite"] for row in trace)
    assert [row["index"] for row in trace] == list(range(steps - 1, -1, -1))
    return {"source_sha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(),
            "seed": seed, "dtype": "float64", "device": "cpu",
            "reductions": {"batch": batch, "dimensions": dimensions,
                           "hidden": hidden, "timesteps": steps,
                           "optimizer_updates": 1,
                           "feature_reduction": "mean", "batch_reduction": "mean"},
            "variance": "posterior beta_tilde; no x0 clipping",
            "shapes": {"x0": list(x0.shape), "indices": list(indices.shape),
                       "epsilon": list(epsilon.shape), "xt": list(xt.shape),
                       "gathered_coefficients": [batch, 1],
                       "supplied_noises": list(noises.shape), "final": list(final.shape)},
            "indices": indices.tolist(), "x0": x0.tolist(), "epsilon": epsilon.tolist(),
            "xt": xt.tolist(), "betas": coefficients["beta"].tolist(),
            "alpha_bar_terminal": float(alpha_terminal),
            "terminal_snr": float(alpha_terminal / (1.0 - alpha_terminal)),
            "terminal_boundary": "substantial signal remains; not a pure-noise terminal",
            "loss_before": float(update["loss_before"]),
            "loss_after": float(update["loss_after"]),
            "per_example_loss": update["per_example"].tolist(),
            "gradient_norm": float(gradient_norm), "state_delta_norm": float(state_delta),
            "finite_gradients": all(bool(torch.isfinite(g).all()) for g in gradients),
            "finite_parameters": all(bool(torch.isfinite(p).all()) for p in model.parameters()),
            "reverse_initial": initial.tolist(), "reverse_noises": noises.tolist(),
            "reverse_trace": trace, "finite_final": bool(torch.isfinite(final).all())}


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--smoke", action="store_true")
    parser.add_argument("--seed", type=int, default=7)
    arguments = parser.parse_args()
    if not arguments.smoke:
        parser.error("pass --smoke to run the reduced offline reference")
    print(json.dumps(smoke(arguments.seed), allow_nan=False))
