Functions for converting from PyTorch TorchScript to MDF models.

This code was originally inspired by the following blog post:

Mike He, “From Models to Computation Graphs (Part I)”,



Helper function that converts some common unserializable types to JSON seriralizable types


Find all constant nodes in the graph and extract their values as a proper JSON serializable value.


Helper function to get a unique name (used in MDF as id) for a TorchScript node's op/function.


Helper function that generates a clean graph and model name from a TorchScript model


Helper function to get a unique name (used in MDF as id) from a TorchScript Node object

process_onnx_schema(node, consts, port_mapper)

Retrieve the argument names and attributes (parameters in MDF) for this Operation.

process_torch_schema(node, consts, port_mapper)

Parse a TorchScript node schema into argument names and constant attributes (parameters in MDF)

pytorch_to_mdf(model[, args, trace, ...])

Convert a PyTorch model to an MDF model.

torchnode_to_mdfnode(node, graph, consts, ...)

Convert a TorchScript node to an MDF node.

translate_graph(graph, mdf_graph, consts, ...)

Go through a Graph or Block and translate the nodes and edges to MDF nodes and edges.


PortMapper(graph, args)

A simple class that handles mapping TorchScript inputouput ids to MDF InputPortOutputPort ids.