models/convert/export.py

Python source for Making faces, 82 lines. Raw file

"""Shared by the converters: export a PyTorch module to ONNX, then check it.

`export` writes one self-contained .onnx file (weights included, even past
2 GB of protobuf, by folding the external data back in where it fits).
`verify` runs the file with onnxruntime when it's installed, or with the
ONNX reference checker otherwise, and compares it with PyTorch's answer.
"""

import os
import pickle
import types

import numpy as np
import onnx
import torch


class _Stub:
    """Stands in for a class the checkpoint names but this environment doesn't have."""

    def __init__(self, *args, **kwargs):
        pass

    def __setstate__(self, state):
        self.__dict__["state"] = state


class _StubUnpickler(pickle.Unpickler):
    def find_class(self, module, name):
        try:
            return super().find_class(module, name)
        except (ModuleNotFoundError, AttributeError):
            return _Stub


_stub_pickle = types.SimpleNamespace(
    Unpickler=_StubUnpickler, load=pickle.load, loads=pickle.loads, __name__="pickle")


def load_checkpoint(path):
    """torch.load for checkpoints that pickle training objects as well as weights.

    PyTorch Lightning checkpoints, for example, name Lightning's own classes.
    Those aren't needed for the weights, so they're loaded as empty stubs.
    """
    return torch.load(path, map_location="cpu", weights_only=False, pickle_module=_stub_pickle)


def export(module, inputs, input_names, output_names, path, opset=17):
    module = module.eval()
    with torch.no_grad():
        torch.onnx.export(
            module,
            inputs,
            path,
            input_names=input_names,
            output_names=output_names,
            opset_version=opset,
            do_constant_folding=True,
            dynamo=False,
        )
    model = onnx.load(path)
    onnx.checker.check_model(path if os.path.getsize(path) > 2_000_000_000 else model)
    size = os.path.getsize(path) / 1_000_000
    print(f"  wrote {os.path.basename(path)} ({size:.0f} MB)")


def verify(path, feeds, expected):
    """Run the ONNX file and print how far it is from the PyTorch result."""
    try:
        import onnxruntime as ort
    except ImportError:
        print(f"  (onnxruntime isn't installed, so {os.path.basename(path)} wasn't run)")
        return
    session = ort.InferenceSession(path, providers=["CPUExecutionProvider"])
    outputs = session.run(None, {name: value.detach().numpy() for name, value in feeds.items()})
    if isinstance(expected, (tuple, list)):
        expected = expected[0]
    difference = float(np.abs(outputs[0] - expected.detach().numpy()).max())
    print(f"  {os.path.basename(path)}: largest difference from PyTorch {difference:.2e}")
    if difference > 1e-2:
        raise SystemExit(f"{path} doesn't match PyTorch")