tvm.relay.frontend

Frontends for constructing Relay programs.

Contains the model importers currently defined for Relay.

Functions

from_caffe2(init_net, predict_net[, shape, …])

Load caffe2 graph which contains init_net and predict_net into Relay Function.

from_coreml(model[, shape])

Convert from coreml model into Relay Function.

from_darknet(net[, shape, dtype])

Convert from Darknet’s model into compatible relay Function.

from_keras(model[, shape, layout])

Convert keras model to relay Function.

from_mxnet(symbol[, shape, dtype, …])

Convert from MXNet”s model into compatible relay Function.

from_onnx(model[, shape, dtype, opset])

Convert a ONNX model into an equivalent Relay Function.

from_pytorch(script_module, input_shapes[, …])

Load PyTorch model in the form of a scripted PyTorch model and convert into relay.

from_tensorflow(graph[, layout, shape, outputs])

Load tensorflow graph which is a python tensorflow graph object into relay.

from_tflite(model, shape_dict, dtype_dict)

Convert from tflite model into compatible relay Function.

tvm.relay.frontend.from_mxnet(symbol, shape=None, dtype='float32', arg_params=None, aux_params=None)

Convert from MXNet”s model into compatible relay Function.

Parameters
  • symbol (mxnet.Symbol or mxnet.gluon.HybridBlock) – MXNet symbol.

  • shape (dict of str to tuple, optional) – The input shape to the graph

  • dtype (str or dict of str to str) – The input types to the graph

  • arg_params (dict of str to mx.NDArray) – The argument parameters in mxnet

  • aux_params (dict of str to mx.NDArray) – The auxiliary parameters in mxnet

Returns

  • mod (tvm.IRModule) – The relay module for compilation

  • params (dict of str to tvm.nd.NDArray) – The parameter dict to be used by nnvm

tvm.relay.frontend.from_keras(model, shape=None, layout='NCHW')

Convert keras model to relay Function.

Parameters
  • model (keras.engine.training.Model or tensorflow.keras.models.Model) – The keras model to be converted.

  • shape (dict of str to int list/tuple) – Input shapes of the model, optional

  • layout (str) – One of ‘NCHW’ or ‘NHWC’, indicates how data should be arranged in the output model. Default layout is ‘NCHW’ as it in general performs better across TVM.

Returns

  • mod (tvm.IRModule) – The relay module for compilation.

  • params (dict of str to tvm.nd.NDArray) – The parameter dict to be used by Relay.

tvm.relay.frontend.from_onnx(model, shape=None, dtype='float32', opset=None)

Convert a ONNX model into an equivalent Relay Function.

ONNX graphs are represented as Python Protobuf objects. The companion parameters will be handled automatically. However, the input names from onnx graph is vague, mixing inputs and network weights/bias such as “1”, “2”… For convenience, we rename the real input names to “input_0”, “input_1”… And renaming parameters to “param_0”, “param_1”…

Parameters
  • model (protobuf object) – ONNX ModelProto after ONNX v1.1.0

  • shape (dict of str to tuple, optional) – The input shape to the graph

  • dtype (str or dict of str to str) – The input types to the graph

  • opset (int, optional) – Override to autodetected opset. This can be helpful for some testing.

Returns

  • mod (tvm.IRModule) – The relay module for compilation

  • params (dict of str to tvm.nd.NDArray) – The parameter dict to be used by relay

tvm.relay.frontend.from_tflite(model, shape_dict, dtype_dict)

Convert from tflite model into compatible relay Function.

Parameters
  • model – tflite.Model or tflite.Model.Model (depending on tflite version)

  • shape_dict (dict of str to int list/tuple) – Input shapes of the model.

  • dtype_dict (dict of str to str) – Input types of the model.

Returns

  • mod (tvm.IRModule) – The relay module for compilation.

  • params (dict of str to tvm.nd.NDArray) – The parameter dict to be used by relay

tvm.relay.frontend.from_coreml(model, shape=None)

Convert from coreml model into Relay Function.

Parameters
  • model – coremltools.models.MLModel of a NeuralNetworkClassifier

  • shape (dict of str to int list/tuple, optional) – The input shapes

Returns

  • mod (tvm.IRModule) – The relay module for compilation.

  • params (dict of str to tvm.nd.NDArray) – The parameter dict to be used by Relay.

tvm.relay.frontend.from_caffe2(init_net, predict_net, shape=None, dtype='float32')

Load caffe2 graph which contains init_net and predict_net into Relay Function.

Parameters
  • init_net (protobuf object) – Caffe2 NetDef containing the weights

  • predict_net (protobuf object) – Caffe2 NetDef containing the graph

  • shape (dict of str to tuple) – The input shape to the graph

  • dtype (str or dict of str to str) – The input types to the graph

Returns

  • mod (tvm.IRModule) – The module that optimizations will be performed on.

  • params (dict of str to tvm.nd.NDArray) – Dict of converted parameters stored in tvm.nd.NDArray format

tvm.relay.frontend.from_tensorflow(graph, layout='NHWC', shape=None, outputs=None)

Load tensorflow graph which is a python tensorflow graph object into relay. The companion parameters will be handled automatically.

Parameters
  • graph (GraphDef object) – Tensorflow GraphDef

  • layout (target layout to be used (Optional)) – NCHW only supported now to enable NHWC models on GPU.

  • shape (Dictionary of input dimensions (Optional)) – Graph level input shape dictionary.

  • outputs (List of output tensor names (Optional)) – if not specified then the last node is assumed as graph output.

Returns

  • mod (tvm.IRModule) – The module that optimizations will be performed on.

  • params (dict of str to tvm.nd.NDArray) – Dict of converted parameters stored in tvm.nd.NDArray format

tvm.relay.frontend.from_darknet(net, shape=None, dtype='float32')

Convert from Darknet’s model into compatible relay Function.

Parameters
  • net (Darknet net parameter) – Darknet net structure.

  • shape (dict of str to tuple, optional) – The input shape to the graph

  • dtype (str or dict of str to str) – The input types to the graph

Returns

  • mod (tvm.IRModule) – The relay module for compilation.

  • params (dict of str to tvm.nd.NDArray) – The parameter dict to be used by relay

tvm.relay.frontend.from_pytorch(script_module, input_shapes, custom_convert_map=None)

Load PyTorch model in the form of a scripted PyTorch model and convert into relay. The companion parameters will be handled automatically.

Parameters
  • script_module (TopLevelTracedModule object) – TorchScripted PyTorch graph Note: We currently only support traces (ie: torch.jit.trace(model, input))

  • input_shapes (List of tuples of input name and input dimensions) – Graph level input shape list The same input names need to be used for deployment, so choose easy to remember names (such as: input0, input1)

  • custom_convert_map (Dictionary of str to Relay op) – A custom op conversion map in the same format as _convert_map above

Returns

  • mod (tvm.relay.Module) – The module that optimizations will be performed on.

  • params (dict of str to tvm.runtime.NDArray) – Dict of converted parameters stored in tvm.runtime.ndarray format