"""Average HLSQConvNet checkpoints to reduce validation jitter.""" from __future__ import annotations import argparse import json from pathlib import Path import torch from hls_qcnn import create_hls_qcnn_from_checkpoint from train_hls_qcnn import configure_stdio configure_stdio() def main() -> int: parser = argparse.ArgumentParser(description="Average compatible HLS QCNN checkpoints") parser.add_argument("--checkpoints", nargs="+", required=True) parser.add_argument("--output", default="../data/models/hls_qcnn_averaged.pth") parser.add_argument("--metrics", default="../results/hls_qcnn_averaged_manifest.json") args = parser.parse_args() paths = [Path(item) for item in args.checkpoints] missing = [str(path) for path in paths if not path.exists()] if missing: print("Missing checkpoints:") for item in missing: print(f" {item}") return 2 checkpoints = [torch.load(path, map_location="cpu") for path in paths] base_config = checkpoints[0].get("model_config") for path, checkpoint in zip(paths, checkpoints): if checkpoint.get("model_config") != base_config: print(f"Model config mismatch: {path}") return 2 avg_state = {} state_keys = checkpoints[0]["model_state_dict"].keys() for key in state_keys: tensors = [checkpoint["model_state_dict"][key] for checkpoint in checkpoints] if torch.is_floating_point(tensors[0]): avg_state[key] = torch.stack([tensor.float() for tensor in tensors], dim=0).mean(dim=0) avg_state[key] = avg_state[key].to(dtype=tensors[0].dtype) else: avg_state[key] = tensors[-1].clone() model = create_hls_qcnn_from_checkpoint({"model_config": base_config}) model.load_state_dict(avg_state) output = Path(args.output) output.parent.mkdir(parents=True, exist_ok=True) best_acc = max(float(checkpoint.get("best_acc", 0.0)) for checkpoint in checkpoints) torch.save( { "epoch": max(int(checkpoint.get("epoch", 0)) for checkpoint in checkpoints), "best_acc": best_acc, "model_state_dict": avg_state, "model_config": base_config, "averaged_from": [str(path) for path in paths], }, output, ) metrics = { "output": str(output), "averaged_from": [str(path) for path in paths], "source_best_acc": best_acc, "model_config": base_config, } metrics_path = Path(args.metrics) metrics_path.parent.mkdir(parents=True, exist_ok=True) with metrics_path.open("w", encoding="utf-8") as f: json.dump(metrics, f, indent=2, ensure_ascii=False) print(f"Wrote averaged checkpoint: {output}") print(f"Wrote manifest: {metrics_path}") return 0 if __name__ == "__main__": raise SystemExit(main())