"""MobileStyleGAN FFHQ: the authors' checkpoint to two ONNX networks.

    python mobilestylegan.py <checkpoint.ckpt> <repo> <output-prefix>

MobileStyleGAN is a small student network distilled from StyleGAN2 FFHQ, so
it's the fastest of the generators. The checkpoint holds the teacher's
mapping network and the student synthesis network. They're built here with
the authors' own classes (the `repo` argument, MobileStyleGAN.pytorch) and
exported the way the authors' own to_onnx does:

    <prefix>-map.onnx      var [1, 512]    ->  style [1, 512]
    <prefix>-synth.onnx    style [1, 512]  ->  img [1, 3, 1024, 1024] in -1..1

With trace_model set, the network adds no noise, so a seed always gives the
same face. Needs pytorch_wavelets for the inverse wavelet transform.
"""

import sys

import torch

from export import export, load_checkpoint, verify

checkpoint, repo, prefix = sys.argv[1:4]
sys.path.insert(0, repo)
from core.models.mapping_network import MappingNetwork  # noqa: E402
from core.models.mobile_synthesis_network import MobileSynthesisNetwork  # noqa: E402
from core.utils import apply_trace_model_mode, select_weights  # noqa: E402

state = load_checkpoint(checkpoint)
state = state.get("state_dict", state)

mapping = MappingNetwork(style_dim=512, n_layers=8, lr_mlp=0.01).eval()
mapping.load_state_dict(select_weights(state, "mapping_net."))
student = MobileSynthesisNetwork(style_dim=512).eval()
student.load_state_dict(select_weights(state, "student."), strict=False)
mapping.apply(apply_trace_model_mode(True))
student.apply(apply_trace_model_mode(True))


class Synthesis(torch.nn.Module):
    def __init__(self, network):
        super().__init__()
        self.network = network

    def forward(self, style):
        return self.network(style)["img"]


var = torch.randn(1, 512, generator=torch.Generator().manual_seed(1))
with torch.no_grad():
    style = mapping(var)
    #  The inverse wavelet transform makes its filters on the first run, which
    #  the exporter would see as the weights changing, so run it once first.
    Synthesis(student)(style)

export(mapping, (var,), ["var"], ["style"], f"{prefix}-map.onnx", opset=11)
export(Synthesis(student), (style,), ["style"], ["img"], f"{prefix}-synth.onnx", opset=11)
verify(f"{prefix}-map.onnx", {"var": var}, mapping(var))
verify(f"{prefix}-synth.onnx", {"style": style}, Synthesis(student)(style))
