"""Original scalar MoE teaching example. Not the REAP implementation or Ornith.

Demonstrates physical removal of an expert, router renormalization and symmetric
4-bit weights. Uses no GPU, network or external libraries. Scalar weights only;
there is no compressed storage or speedup claim.
"""
import json
import math

EXPERTS = [(1.5, 0.2), (-0.4, 0.6), (0.9, -0.3)]
ROUTER = [0.8, -0.6, 0.1]


def route(x, kept):
    if not kept or len(set(kept)) != len(kept):
        raise ValueError("non-empty, unique expert indices required")
    logits = [ROUTER[i] * x for i in kept]
    weights = [math.exp(v - max(logits)) for v in logits]
    return [v / sum(weights) for v in weights]


def predict(x, experts, kept):
    return sum(p * (experts[i][0] * x + experts[i][1])
               for i, p in zip(kept, route(x, kept)))


def quantize(experts):
    # Symmetric signed range [-7,7]: 15 levels, intentionally not a GGUF format.
    maximum = max(abs(v) for pair in experts for v in pair)
    scale = maximum / 7 if maximum else 1.0
    codes = [[max(-7, min(7, round(v / scale))) for v in pair] for pair in experts]
    return [[q * scale for q in pair] for pair in codes], codes, scale


def demo():
    calibration = [i / 4 for i in range(-8, 9, 2)]
    holdout = [i / 4 for i in range(-7, 8, 2)]
    all_ids = list(range(len(EXPERTS)))
    salience = [sum(predict_contribution(x, i) for x in calibration) / len(calibration) for i in all_ids]
    kept = sorted(sorted(all_ids, key=lambda i: salience[i], reverse=True)[:2])
    pruned_weights = [EXPERTS[i] for i in kept]
    dequantized, codes, scale = quantize(EXPERTS)
    mse = lambda weights, indices: sum((predict(x, weights, indices) - predict(x, EXPERTS, all_ids)) ** 2 for x in holdout) / len(holdout)
    return {"scope":"toy only; dense all-expert routing, not Ornith top-k or REAP", "experts_before":3,
            "experts_after":len(pruned_weights), "kept_indices":kept,
            "pruned_expert_weights":pruned_weights, "salience":salience,
            "quantized_integer_codes":codes, "scale":scale,
            "holdout_mse_pruning_only":mse(EXPERTS,kept),
            "holdout_mse_quantization_only":mse(dequantized,all_ids),
            "holdout_mse_combined":mse(dequantized,kept),
            "calibration_holdout_overlap":len(set(calibration)&set(holdout)),
            "packed_4bit_storage":False}


def predict_contribution(x, i):
    p = route(x, list(range(len(EXPERTS))))[i]
    return p * abs(EXPERTS[i][0] * x + EXPERTS[i][1])


if __name__ == "__main__":
    print(json.dumps(demo(), indent=2))
