qonnx.core.modelwrapper (module)
- class qonnx.core.modelwrapper.ModelWrapper(onnx_model_proto, make_deepcopy=False, fix_float64=False, fix_missing_initializer_valueinfo=True)
Bases:
objectA wrapper around ONNX ModelProto that exposes some useful utility functions for graph manipulation and exploration.
Creates a ModelWrapper instance. onnx_model_proto can be either a ModelProto instance, or a string with the path to a stored .onnx file on disk, or serialized bytes.
make_deepcopy: controls whether a deep copy of the ModelProto is made internally. fix_float64 : DoubleToSingleFloat correction before applying any transformations on this model. fix_missing_initializer_valueinfo: add ValueInfoProto fields for initializers that are missing theirs.
- analysis(analysis_fxn)
Runs given anaylsis_fxn on this model and return resulting dict.
- check_all_tensor_shapes_specified(fix_missing_init_shape=False)
Checks whether all tensors have a specified shape (ValueInfo). The ONNX standard allows for intermediate activations to have no associated ValueInfo, but QONNX expects this. If fix_missing_init_shape is specified, it will add a ValueInfoProto for initializers that are missing theirs.
- check_compatibility()
Checks this model for QONNX compatibility:
no embedded subgraphs
all tensor shapes are specified, including activations
all constants are initializers
- cleanup()
Run cleanup transformations on the model.
- find_consumer(tensor_name)
Finds and returns the node that consumes the tensor with given name. If there are multiple consumers, only the first one is returned. If there are no consumers, returns None.
- find_consumers(tensor_name)
Finds and returns a list of the nodes that consume tensor with given name.
- find_direct_predecessors(node)
Finds and returns a list of the nodes that are predecessors of given node.
- find_direct_successors(node)
Finds and returns a list of the nodes that are successors of given node.
- find_producer(tensor_name)
Finds and returns the node that produces the tensor with given name.
- find_upstream(tensor_name, finder_fxn)
Follow the producer chain upstream, calling finder_fxn on each upstream node until it returns True or there are no nodes left. Returns the list of nodes visited, or None if finder_fxn did not return True.
- get_all_tensor_names()
Returns a list of all (input, output and value_info) tensor names in the graph.
- get_finn_nodes()
Returns a list of nodes where domain == ‘qonnx.*’.
- get_initializer(tensor_name, return_dtype=False)
Gets the initializer value for tensor with given name, if any. ret_dtype can be set to True to retrieve the TensorProto.DataType of the initializer by returning it as a second element of a tuple.
- get_metadata_prop(key)
Returns the value associated with metadata_prop with given key, or None otherwise.
- get_node_index(node)
Returns current index of given node.
- get_nodes_by_op_type(op_type)
Returns a list of nodes with specified op_type.
- get_non_finn_nodes()
Returns a list of nodes where domain != ‘qonnx.*’.
- get_tensor_datatype(tensor_name)
Returns the QONNX DataType of tensor with given name.
- get_tensor_fanout(tensor_name)
Returns the number of nodes for which the tensor with given name is as input.
- get_tensor_layout(tensor_name)
Returns the data layout annotation of tensor with given name. The data layout is expressed as a list of strings with as many elements as the number of dimensions in the tensor shape. Each string annotates what is contained in that dimension. If there is no data layout annotation, None will be returned. Examples of data layout annotations: [“N”, “C”] is tensor[batch][channel] [“N”, “C”, “H”, “W”] is tensor[batch][channel][height][width] [“N”, “H”, “W”, “C”] is tensor[batch][height][width][channel]
- get_tensor_shape(tensor_name, fix_missing_init_shape=False)
Returns the shape of tensor with given name, if it has ValueInfoProto. If fix_missing_init_shape is specified, it will add a ValueInfoProto for initializers that are missing theirs.
- get_tensor_sparsity(tensor_name)
Returns the sparsity of a given tensor as dictionary.
- get_tensor_valueinfo(tensor_name)
Returns ValueInfoProto of tensor with given name, if it has one.
- property graph
Returns the graph of the model.
- is_fork_node(node)
Checks if the given node is a fork, that is, the node has multiple direct successors
- is_join_node(node)
Checks if the given node is a join, that is, the node has multiple direct predecessors
- make_empty_exec_context()
Creates an empty execution context for this model.
The execution context is a dictionary of all tensors used for the inference computation. Any initializer values will be taken into account, all other tensors will be zero.
- make_new_valueinfo_name()
Returns a name that can be used for a new value_info.
- property model
Returns the model.
- rename_tensor(old_name, new_name)
Renames a tensor from old_name to new_name.
- save(filename)
Saves the wrapper ONNX ModelProto into a file with given name.
- set_initializer(tensor_name, tensor_value)
Sets the initializer value for tensor with given name.
- set_metadata_prop(key, value)
Sets metadata property with given key to the given value.
- set_tensor_datatype(tensor_name, datatype)
Sets the QONNX DataType of tensor with given name.
- set_tensor_layout(tensor_name, data_layout)
Sets the data layout annotation of tensor with given name. See get_tensor_layout for examples.
- set_tensor_shape(tensor_name, tensor_shape, dtype=1)
Assigns shape in ValueInfoProto for tensor with given name.
- set_tensor_sparsity(tensor_name, sparsity_dict)
Sets the sparsity annotation of a tensor with given name.
- temporary_fix_oldstyle_domain()
- transform(transformation, make_deepcopy=True, cleanup=True)
Applies given Transformation repeatedly until no more changes can be made and returns a transformed ModelWrapper instance.
make_deepcopy : operates on a new (deep)copy of model.
cleanup : execute cleanup transformations before returning