from __future__ import annotations import copy import csv import json import math import time from pathlib import Path from statistics import mean, pstdev import numpy as np import torch try: from .datasets import AUDIO_DATASETS, DATASETS, load_dataset from .losses import DIVERGENCES, align_loss, mse_loss from .model import SQMM from . import baselines except ImportError: from datasets import AUDIO_DATASETS, DATASETS, load_dataset from losses import DIVERGENCES, align_loss, mse_loss from model import SQMM import baselines # Main-table protocol per dataset: epochs and the selected growth interval G*. DATASET_DEFAULTS = { "cello": {"epochs": 300, "growth": 25}, "camera": {"epochs": 300, "growth": 25}, "coffee": {"epochs": 300, "growth": 25}, "astronaut": {"epochs": 600, "growth": 50}, } DEFAULT_MODEL_CONFIG = { # PE frequencies; None picks the modality default (audio: dyadic 16, images: consecutive 8). "frequencies": None, "band_depths": [2, 3, 4, 4, 5], "band_widths": [2, 2, 2, 2, 2], "memory": { "mode": "read_write", # read_write | read | write | none "qubits": 2, "init_axes": None, # initialisation axis per memory qubit; None = RY, RX, ... "read_gates": None, # per band; None = [cry] ... [cry, crx] "write_gates": None, # per band; None = [xy] ... [xy], [] (the last band does not write) }, "upload_axes": None, # rotation axis per coordinate; None = RY (1-D), RY, RX (2-D) "readout": ["Z", "ZZ"], "readout_mlp": {"widths": [5], "activation": "gelu"}, } MODALITY_FREQUENCIES = { "audio": {"mode": "dyadic", "amount": 16}, "image": {"mode": "consecutive", "amount": 8}, } DEFAULT_TRAIN_CONFIG = { "dataset": "cello", "model": "sqmm", # sqmm | qiren | siren | iga | msa_inr | cafe_enc | finer "shots": 0, # SQMM readout shots; 0 = exact expectation "iga_patch_size": 32, "iga_xuhao": 30, "iga_replace_start": 0, "iga_replace_end": 30, "epochs": None, # None = DATASET_DEFAULTS "growth": None, # updates between band introductions; None = G*; 0 = no growth "fade_updates": 5, "learning_rate": 0.03, "img_size": 32, "align_weight": 3.0, "align_divergence": "squared", # squared | kl "align_end_update": None, # None = (num_bands - 1) * growth, the last band's introduction } def deep_update(base: dict, updates: dict | None) -> dict: result = copy.deepcopy(base) for key, value in (updates or {}).items(): if isinstance(value, dict) and isinstance(result.get(key), dict): result[key] = deep_update(result[key], value) else: result[key] = copy.deepcopy(value) return result def resolve_train_config(train_config: dict | None = None) -> dict: config = deep_update(DEFAULT_TRAIN_CONFIG, train_config) unknown = set(config) - set(DEFAULT_TRAIN_CONFIG) if unknown: raise ValueError(f"unknown train_config keys: {sorted(unknown)}") if config["dataset"] not in DATASETS: raise ValueError(f"dataset must be one of {DATASETS}") for key, value in DATASET_DEFAULTS[config["dataset"]].items(): if config[key] is None: config[key] = value if config["align_divergence"] not in DIVERGENCES: raise ValueError(f"align_divergence must be one of {DIVERGENCES}") return config def build_model(model_config: dict | None, *, coord_dim: int, out_features: int) -> SQMM: config = deep_update(DEFAULT_MODEL_CONFIG, model_config) modality = "audio" if coord_dim == 1 else "image" return SQMM( coord_dim=coord_dim, out_features=out_features, frequencies=config["frequencies"] or MODALITY_FREQUENCIES[modality], band_depths=config["band_depths"], band_widths=config["band_widths"], memory=config["memory"], upload_axes=config["upload_axes"], readout=config["readout"], readout_mlp=config["readout_mlp"], ) def growth_state(update: int, growth: int, num_bands: int, fade_updates: int = 5): """(active bands, fade of the newest band) at an optimizer update.""" if growth <= 0: return num_bands, 1.0 active = min(1 + update // growth, num_bands) if active == 1 or fade_updates == 0: return active, 1.0 return active, min(1.0, float(update - (active - 1) * growth + 1) / float(fade_updates)) def batch_loss(model, coords, target, grid_shape, config, *, update: int, active: int, fade: float): """Training loss of one full-batch update. Returns (loss, task MSE).""" end = config["align_end_update"] if end is None and config["growth"] > 0: end = (model.num_bands - 1) * config["growth"] use_align = active >= 2 and (end is None or update < end) if use_align: prediction, band_features, memory_states = model(coords, active, fade, return_memory=True, shots=config["shots"]) else: prediction = model(coords, active, fade, shots=config["shots"]) mse = mse_loss(prediction, target) loss = mse if use_align: aligned = align_loss(band_features, memory_states, grid_shape, config["align_divergence"]) loss = loss + config["align_weight"] * aligned return loss, mse @torch.no_grad() def evaluate(model, coords, target, *, image: bool, shots: int = 0) -> dict: """Full-grid scores. MSE is in the training range [-1, 1]; image PSNR/SSIM use [0, 1].""" prediction = model(coords, shots=shots) if isinstance(model, SQMM) else baselines.baseline_forward(model, coords) mse = float(((prediction - target) ** 2).mean()) if not image: return {"mse": mse, "psnr": 10.0 * math.log10(1.0 / max(mse, 1e-12))} unit_prediction = (prediction.clamp(-1.0, 1.0) + 1.0) * 0.5 unit_target = (target.clamp(-1.0, 1.0) + 1.0) * 0.5 unit_mse = float(((unit_prediction - unit_target) ** 2).mean()) metrics = {"mse": mse, "psnr": 10.0 * math.log10(1.0 / max(unit_mse, 1e-12))} try: from skimage.metrics import structural_similarity side = int(round(math.sqrt(target.shape[1]))) images = [t.reshape(side, side).numpy().astype(np.float32) for t in (unit_target[0, :, 0], unit_prediction[0, :, 0])] metrics["ssim"] = float(structural_similarity(*images, data_range=1.0)) except ImportError: metrics["ssim"] = None return metrics ### IGA (implicit geometry alignment): patch-NTK preconditioned loss (train-loop mechanism) ### def iga_compute_ntk(model, coords, prediction, target, patch_size: int): """Per-patch NTK on the largest-error point of each patch, over one full batch. Port of IGA-INR fitting.compute_NTK for the 1-D full-batch SQMM protocol: the data is split into contiguous patches of ``patch_size`` points, the largest-error point of each patch is selected, and the NTK over those points is formed with a functional per-point Jacobian of a scalar-headed copy of the model. """ from torch.func import functional_call, grad, vmap n_points = coords.shape[1] patch_size = min(patch_size, n_points) if n_points % patch_size: # trim so patches divide evenly coords = coords[:, :n_points - (n_points % patch_size)] prediction = prediction[:, :coords.shape[1]] target = target[:, :coords.shape[1]] n_points = coords.shape[1] num_patches = n_points // patch_size error = (prediction - target).abs().sum(-1).squeeze(0) # [N] idx = error.reshape(num_patches, patch_size).argmax(dim=1) + torch.arange(num_patches) * patch_size sel = coords.squeeze(0)[idx] # [P, coord_dim] # out_features == 1, so the model itself is the scalar-headed network of the # reference implementation (their extra fixed Linear is the identity here). params = {k: v.detach() for k, v in model.named_parameters()} def compute_loss(p, x): out = functional_call(model, p, (x.unsqueeze(0),)) if isinstance(out, tuple): out = out[0] return out.sum() jac = vmap(grad(compute_loss, argnums=0), (None, 0))(params, sel) jac = torch.cat([t.flatten(1) for t in jac.values()], dim=1) return jac @ jac.T, num_patches, patch_size def iga_precondition_matrix(ntk, xuhao: int, replace_start: int, replace_end: int): """Port of IGA-INR fitting.calculate_precondition_matrix (eigenvalue replacement).""" eigenvalues, eigenvectors = torch.linalg.eigh(ntk) sorted_indices = torch.argsort(eigenvalues, descending=True) eigenvalues = eigenvalues[sorted_indices] eigenvectors = eigenvectors[:, sorted_indices] S = torch.eye(ntk.size(0)) for i in range(replace_start, replace_end): if i >= len(eigenvalues) or eigenvalues[i] <= 0: break S -= (1 - eigenvalues[xuhao] / eigenvalues[i]) * (eigenvectors[:, i].view(-1, 1) @ eigenvectors[:, i].view(-1, 1).T) return S.detach() def iga_weighted_loss(prediction, target, S, num_patches: int, patch_size: int): """Port of IGA-INR weighted_loss_IGA: S acts per patch on the full-batch error.""" n_used = num_patches * patch_size error = (prediction - target).squeeze(0).detach()[:n_used] # [N, 1], trimmed as in compute_ntk error = error.reshape(num_patches, patch_size, 1) error = torch.einsum("ij,jkl->ikl", S, error) outputs = prediction.squeeze(0).contiguous().view(-1, 1)[:n_used].t() error = error.reshape(n_used, 1) loss = 2 * torch.matmul(outputs, error) / error.shape[0] return loss def train_one_run(*, run_id: int = 0, model_config: dict | None = None, train_config: dict | None = None, verbose: bool = True) -> dict: config = resolve_train_config(train_config) dataset = config["dataset"] coords, target, grid_shape = load_dataset(dataset, config["img_size"]) is_sqmm = config["model"] == "sqmm" if is_sqmm: model = build_model(model_config, coord_dim=coords.shape[-1], out_features=target.shape[-1]) lr = config["learning_rate"] else: model, lr = baselines.build_baseline(config["model"], coord_dim=coords.shape[-1], out_features=target.shape[-1]) is_iga = config["model"] == "iga" if is_iga: ntk_patch = int(config.get("iga_patch_size", 32)) ntk_xuhao = int(config.get("iga_xuhao", 30)) ntk_replace = (int(config.get("iga_replace_start", 0)), int(config.get("iga_replace_end", 30))) optimizer = torch.optim.Adam(model.parameters(), lr=lr, betas=(0.9, 0.999), eps=1e-8) best_mse, best_epoch, best_state = float("inf"), None, None started = time.time() for epoch in range(config["epochs"]): update = epoch # one full-batch update per epoch if is_sqmm: # The epoch's MSE is measured before its update, so it scores these weights. state = {key: value.detach().clone() for key, value in model.state_dict().items()} else: state = {key: value.detach().clone() for key, value in model.state_dict().items()} if is_sqmm: active, fade = growth_state(update, config["growth"], model.num_bands, config["fade_updates"]) loss, mse = batch_loss(model, coords, target, grid_shape, config, update=update, active=active, fade=fade) elif is_iga: prediction = baselines.baseline_forward(model, coords) mse = mse_loss(prediction, target) pntk, n_patches, n_patch = iga_compute_ntk(model, coords, prediction, target, ntk_patch) S = iga_precondition_matrix(pntk, ntk_xuhao, *ntk_replace) loss = iga_weighted_loss(prediction, target, S, n_patches, n_patch) else: prediction = baselines.baseline_forward(model, coords) mse = mse_loss(prediction, target) loss = mse optimizer.zero_grad() loss.backward() optimizer.step() epoch_mse = mse.item() model_done = (not is_sqmm) or (active == model.num_bands and fade >= 1.0) if model_done and epoch_mse < best_mse: best_mse, best_epoch, best_state = epoch_mse, epoch, state if verbose and epoch % 50 == 0: if is_sqmm: print(f"{dataset} run {run_id} | epoch {epoch:4d}/{config['epochs']} | " f"bands {active} | mse {epoch_mse:.6f} | best {best_mse:.6f}", flush=True) else: print(f"{dataset} run {run_id} | epoch {epoch:4d}/{config['epochs']} | " f"{config['model']} | mse {epoch_mse:.6f} | best {best_mse:.6f}", flush=True) image = dataset not in AUDIO_DATASETS final = evaluate(model, coords, target, image=image, shots=config["shots"]) final_state = {key: value.detach().clone() for key, value in model.state_dict().items()} if best_state is not None: model.load_state_dict(best_state) best = evaluate(model, coords, target, image=image, shots=config["shots"]) result = { "dataset": dataset, "run_id": int(run_id), "model": config["model"], "shots": config["shots"], "epochs": config["epochs"], "growth": config["growth"], "best_epoch": best_epoch, **{f"{key}_best": value for key, value in best.items()}, **{f"{key}_final": value for key, value in final.items()}, **(model.parameter_counts() if hasattr(model, "parameter_counts") else { "trainable_params": sum(p.numel() for p in model.parameters() if p.requires_grad), "classical_params": sum(p.numel() for p in model.parameters() if p.requires_grad), "quantum_params": 0}), "elapsed_s": time.time() - started, } if verbose: print(f"{dataset} run {run_id} | DONE | mse_best={result['mse_best']:.6e} " f"psnr_best={result['psnr_best']:.2f} | params={result['trainable_params']}", flush=True) return {"result": result, "best_state": model.state_dict(), "final_state": final_state} ### Multi-run driver and outputs ### def summarize(rows: list[dict]) -> dict: summary = {} for dataset in sorted({r["dataset"] for r in rows}): group = [r for r in rows if r["dataset"] == dataset] mses = [r["mse_best"] for r in group] psnrs = [r["psnr_best"] for r in group] summary[dataset] = { "runs": len(group), "mean_mse_best": mean(mses), "std_mse_best": pstdev(mses), "min_mse_best": min(mses), "mean_psnr_best": mean(psnrs), "max_psnr_best": max(psnrs), } return summary def write_outputs(output_dir: Path, rows: list[dict], configs: dict) -> None: output_dir.mkdir(parents=True, exist_ok=True) fields = list(dict.fromkeys(key for row in rows for key in row)) with (output_dir / "runs.csv").open("w", newline="") as handle: writer = csv.DictWriter(handle, fieldnames=fields) writer.writeheader() writer.writerows(rows) payload = {**configs, "summary": summarize(rows), "runs": rows} (output_dir / "results.json").write_text(json.dumps(payload, indent=2)) def run(*, runs: int, output_dir: str | Path, model_config: dict | None = None, train_config: dict | None = None, save_weights: bool = False, verbose: bool = True) -> dict: config = resolve_train_config(train_config) output_dir = Path(output_dir) rows = [] for run_id in range(runs): out = train_one_run(run_id=run_id, model_config=model_config, train_config=config, verbose=verbose) rows.append(out["result"]) if save_weights: weights = output_dir / "weights" weights.mkdir(parents=True, exist_ok=True) tag = f"{config['dataset']}_run{run_id}" torch.save(out["best_state"], weights / f"{tag}_best.pt") torch.save(out["final_state"], weights / f"{tag}_final.pt") write_outputs(output_dir, rows, {"model_config": deep_update(DEFAULT_MODEL_CONFIG, model_config), "train_config": config}) return {"summary": summarize(rows), "runs": rows}