"""Real CPU optimization of a 2-parameter student, NOT training Ornith.

An original, deterministic demonstration of soft-target distillation.
Teacher: fixed cubic logistic function (3 coefficients).
Student: linear logistic function (2 learned coefficients).
There is no network, GPU, model download, cloud service or secret.
"""
import argparse
import json
import math


def sigmoid(x):
    if x >= 0:
        return 1 / (1 + math.exp(-x))
    e = math.exp(x)
    return e / (1 + e)


def teacher(x, temperature=1.0):
    return sigmoid((1.6 * x + 0.5 * x ** 3 - 0.2) / temperature)


def student(x, weights, temperature=1.0):
    return sigmoid((weights[0] * x + weights[1]) / temperature)


def kl_bernoulli(p, q):
    p = min(max(p, 1e-12), 1 - 1e-12)
    q = min(max(q, 1e-12), 1 - 1e-12)
    return p * math.log(p / q) + (1 - p) * math.log((1 - p) / (1 - q))


def score(xs, weights):
    return sum(kl_bernoulli(teacher(x), student(x, weights)) for x in xs) / len(xs)


def train(epochs=1500, temperature=2.0, rate=0.15):
    if epochs < 1 or not math.isfinite(temperature) or temperature <= 0:
        raise ValueError("epochs >= 1 and finite temperature > 0 required")
    if not math.isfinite(rate) or rate <= 0:
        raise ValueError("rate must be finite and positive")
    train_x = [i / 8 for i in range(-16, 17, 2)]
    holdout_x = [i / 8 for i in range(-15, 16, 2)]
    weights = [0.0, 0.0]
    before = score(holdout_x, weights)
    for _ in range(epochs):
        gradient = [0.0, 0.0]
        for x in train_x:
            # Gradient of T^2 * KL(teacher_T || student_T).
            delta = temperature * (student(x, weights, temperature) - teacher(x, temperature))
            gradient[0] += delta * x / len(train_x)
            gradient[1] += delta / len(train_x)
        weights = [w - rate * g for w, g in zip(weights, gradient)]
    return {
        "scope": "CPU toy distillation only; not an Ornith quality result",
        "teacher_coefficients": 3, "student_parameters": 2,
        "train_points": len(train_x), "held_out_points": len(holdout_x),
        "epochs": epochs, "temperature": temperature, "weights": weights,
        "held_out_kl_before": before, "held_out_kl_after": score(holdout_x, weights),
        "overlap": len(set(train_x) & set(holdout_x)),
    }


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--epochs", type=int, default=1500)
    parser.add_argument("--temperature", type=float, default=2.0)
    args = parser.parse_args()
    print(json.dumps(train(args.epochs, args.temperature), indent=2))
