onnx_helpersο
Classesο
Configuration about how to split a model. |
Functionsο
|
Update an ONNX model with IR and OPSET supported by Model SDK. |
|
Remove shape inference results in a model. |
|
Load a model and update its version information. |
|
Save a model to disk. |
|
Run inference on a model. |
|
Run inference on two models and expect identical match numerically. |
|
Run inference on two models and expect close match numerically. |
|
Extract from a model. |
|
Split a model. |
|
Verify split models by comparing final inference outputs of the whole model and the cascaded splits. |
|
Merge two models. |
|
Merge two models which may have shared constants. |
|
Find an operator node by name. |
|
Find an operator node whose output is the specified output_name. |
|
Find an operator node whose input is the specified input_name. |
|
Make an operator node following ONNX specification. |
|
Remove a node in a model. |
|
Insert a node in a model. |
|
Replace a node in a model. |
|
Set attribute of a node in a model. |
|
Remove multiple nodes by a list of names. |
|
Insert a pair of transpose operators before and after a node. |
|
Rewrite a Matmul operator as a Conv operator. |
|
Rewrite a Matmul operator as an Einsum operator. |
|
Insert Slice operators after a node. All Slice operators output the same length on the slicing axis. |
|
Insert Concat operator after a group of nodes. |
|
Insert Slices + Concat between two nodes. |
|
Rewrite a Gemm operator as a Conv operator between two nodes. |
|
Connect two nodes by feeding the output of one node to the input of next node. |
|
Run outputs of a model. |
|
Remove multiple outputs by a list of names. |
|
Remove multiple inputs by a list of names. |
|
Add input or output to a model. |
|
Add input to a model. |
|
Add output to a model. |
|
Get the shape of a tensor. |
|
Update I/O shape in a model. |
|
Change output of a node in a model. |
|
Check if a name is an initializer. |
|
Find value of an initializer in a model. |
|
Remove initializers from a list. |
|
Add an initializer to a model. |
Remove duplicated initializers in a model. |
|
|
Perform transpose and/or reshape on an existing constant initializer. |
|
Convert an initializer in a model. |
|
Uniquify initializer names in a model by a prefix name. |
|
Convert a model to a simpler json readable representation |
|
Update a model, with node names |
Module Contentsο
- class onnx_helpers.ModelSplit[source]ο
Configuration about how to split a model.
- Parameters:
split_name β Name of the extracted model.
input_names β List of input names demarcating the beginning of the split.
output_names β List of output names demarcating the end of the split.
filename β File name of the extracted model to be saved.
parent_split β ModelSplit for the parent if the parent is a split. Cannot be None when parent_path is None.
parent_path β Path to the parent model to extract from.
- parent_split: ModelSplit | None = None[source]ο
- onnx_helpers.update_model_version(model: onnx.ModelProto) onnx.ModelProto [source]ο
Update an ONNX model with IR and OPSET supported by Model SDK.
- Parameters:
model β Loaded ONNX model in onnx.ModelProto representation.
- Returns:
Updated ONNX model in onnx.ModelProto representation.
- onnx_helpers.remove_infer_shape(model: onnx.ModelProto)[source]ο
Remove shape inference results in a model.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
- onnx_helpers.load_model(model_fname: str, load_only: bool = False) onnx.ModelProto [source]ο
Load a model and update its version information.
- Parameters:
model_fname β File name of the model to load from disk.
load_only β Boolean flag, if set to False, to simplify the model after loading.
- Returns:
Loaded model in onnx.ModelProto representation.
- onnx_helpers.save_model(model: onnx.ModelProto, model_fname: str, save_only: bool = False)[source]ο
Save a model to disk.
- Parameters:
model β Model to be saved.
model_fname β File name to be used to save the model.
save_only β Boolean flag, if set to False, to simplify and re-generate shape inference result before saving to disk.
- onnx_helpers.run_model(model_name: str, input_names: List[str], input_data: List[numpy.ndarray]) List[numpy.ndarray] [source]ο
Run inference on a model.
- Parameters:
model_name β File name of the model saved on disk.
input_names β List of input names expected by the model.
input_data β List of input data for the model to run inference on.
- Returns:
List of outputs from inference result.
- onnx_helpers.verify_models_equal(model_0: str, model_1: str, input_names: List[str], input_data: List[numpy.ndarray])[source]ο
Run inference on two models and expect identical match numerically.
- Parameters:
model_0 β File name of the first model.
model_1 β File name of the second model.
input_names β List of input names expected by both models.
input_data β List of input data to run inference on.
- onnx_helpers.verify_models_close(model_0: str, model_1: str, input_names: List[str], input_data: List[numpy.ndarray], atol: float = 1e-06)[source]ο
Run inference on two models and expect close match numerically.
- Parameters:
model_0 β File name of the first model.
model_1 β File name of the second model.
input_names β List of input names expected by both models.
input_data β List of input data to run inference on.
atol β Float value as absolute tolerance in comparison.
- onnx_helpers.extract_model(model: onnx.ModelProto, input_names: List[str], output_names: List[str]) onnx.ModelProto [source]ο
Extract from a model.
- Parameters:
model β The model to extract from.
input_names β List of input names demarcating the beginning of the split.
output_names β List of output names demarcating the end of the split.
- Returns:
Extracted model in onnx.ModelProto representation.
- onnx_helpers.split_model(model_splits: List[ModelSplit])[source]ο
Split a model.
- Parameters:
model_splits β List of ModelSplit to perform model extraction.
- onnx_helpers.verify_split_models(whole_model: str, split_models: List[ModelSplit], input_names: List[str], input_data: List[numpy.ndarray])[source]ο
- Verify split models by comparing final inference outputs of the whole model and the cascaded splits.
The order of the splits must be in the same execution order of the original whole model.
- Parameters:
whole_model β File name of the original whole model.
split_models β List of ModelSplit extracted from the whole model.
input_names β List of input names expected by the whole model.
input_data β List of input data to run inference on.
- onnx_helpers.merge_model(model_0: str | pathlib.Path | onnx.ModelProto, model_1: str | pathlib.Path | onnx.ModelProto, io_map: List[Tuple[str, str]]) onnx.ModelProto [source]ο
Merge two models.
- Parameters:
model_0 β First model provided by file name or loaded as onnx.ModelProto.
model_1 β Second model provided by file name or loaded as onnx.ModelProto.
io_map β List of tuples mapping output of the first model to input of the second model.
- Returns:
Merged model in onnx.ModelProto representation.
- Merge two models which may have shared constants.
When a model is split into multiple parts, some constants are duplicated in multiple parts. Later, when they are merged (possibly after modifications), there are conflicts because of the same constant names. A solution is to add prefix to constant names in the second model.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
model_split β File name of the second model to merge.
constant_prefix β Prefix name to prepend to all constants in the second model.
- Returns:
Merged model in onnx.ModelProto representation.
- onnx_helpers.find_node(model: onnx.ModelProto, node_name: str) onnx.NodeProto [source]ο
Find an operator node by name.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
node_name β Name of a node.
- Returns:
Operator node in onnx.NodeProto representation.
- onnx_helpers.find_node_output(model: onnx.ModelProto, output_name: str) onnx.NodeProto [source]ο
Find an operator node whose output is the specified output_name.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
output_name β Name of the output.
- Returns:
Operator node in onnx.NodeProto representation.
- onnx_helpers.find_node_input(model: onnx.ModelProto, input_name: str) Tuple[onnx.NodeProto, int] [source]ο
Find an operator node whose input is the specified input_name.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
input_name β Name of the input.
- Returns:
Tuple of Operator node and input index.
- onnx_helpers.make_node(**kwargs: Any) onnx.NodeProto [source]ο
Make an operator node following ONNX specification.
- Parameters:
**kwargs β
Operator dependent properties.
- Returns:
Newly constructed node in onnx.NodeProto representation.
- onnx_helpers.remove_node(model: onnx.ModelProto, node: str | onnx.NodeProto, remove_only: bool = False)[source]ο
Remove a node in a model.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
node β Node to be removed, provided as node name or onnx.NodeProto representation.
remove_only β Boolean flag, if set to False, to reconnect surrounding nodes.
- onnx_helpers.insert_node(model: onnx.ModelProto, node: onnx.NodeProto, new_node: onnx.NodeProto, insert_before: bool = False, insert_only: bool = False)[source]ο
Insert a node in a model.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
node β Reference node where to insert a new node.
new_node β New node to be inserted.
insert_before β Boolean flag, True to insert before or False to insert after the reference node.
insert_only β Boolean flag, if False, to connect the new node to the reference node.
- onnx_helpers.replace_node(model: onnx.ModelProto, node: onnx.NodeProto, new_nodes: List[onnx.NodeProto])[source]ο
Replace a node in a model.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
node β Reference node to replace.
new_nodes β List of nodes to replace with.
- onnx_helpers.set_attribute_to_node(model: onnx.ModelProto, node_name: str, attr_name: str, val: Any)[source]ο
Set attribute of a node in a model.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
node_name β Name of the node.
attr_name β Name of the attribute.
val β Value of the attribute.
- onnx_helpers.remove_nodes_by_name_list(model: onnx.ModelProto, name_list: List[str])[source]ο
Remove multiple nodes by a list of names.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
name_list β List of names of the nodes to be removed.
- onnx_helpers.insert_transpose_pair(model: onnx.ModelProto, node_name: str, perm_before: Tuple[int], perm_after: Tuple[int]) List[str] [source]ο
Insert a pair of transpose operators before and after a node.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
node_name β Name of the reference node.
perm_before β Permutation before the node.
perm_after β Permutation after the node.
- Returns:
List of names of the newly inserted transpose nodes.
- onnx_helpers.rewrite_matmul_as_conv(model: onnx.ModelProto, name_list: List[str]) List[str] [source]ο
Rewrite a Matmul operator as a Conv operator.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
name_list β List of Matmul node names to be rewritten.
- Returns:
List of names of the new convolution nodes.
- onnx_helpers.rewrite_matmul_as_einsum(model: onnx.ModelProto, eqn_list: Dict[str, str]) List[str] [source]ο
Rewrite a Matmul operator as an Einsum operator.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
eqn_list β Dictionary for rewrite with key being node name and value being equation string for Einsum.
- Returns:
List of names of the new Einsum nodes.
- onnx_helpers.insert_slices_after_node(model: onnx.ModelProto, node_name: str, *, axis: int, nslices: int, slice_size: int) List[str] [source]ο
Insert Slice operators after a node. All Slice operators output the same length on the slicing axis.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
node_name β Name of the node after which to insert slices.
axis β Axis for the Slice operators.
nslices β Number of Slice operators.
slice_size β Length for the Slice operators on the slicing axis.
- Returns:
List of names of the new Slice nodes.
- onnx_helpers.insert_concat(model: onnx.ModelProto, input_nodes: List[str], at_node: str, *, axis: int) str [source]ο
Insert Concat operator after a group of nodes.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
input_nodes β List of names representing the group of nodes which are inputs to Concat.
at_node β Name of node where insertion happens.
axis β Axis on which to concatenate.
- Returns:
Name of newly inserted Concat node.
- onnx_helpers.insert_slices_concat_between_nodes(model: onnx.ModelProto, after_node: str, before_node: str, *, slice_axis: int, nslices: int, slice_size: int, concat_axis: int) str [source]ο
Insert Slices + Concat between two nodes.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
after_node β Name of the node after which a group of Slice operators are inserted.
before_node β Name of the node before which a Concat operator is inserted.
slice_axis β Axis for the Slice operators.
nslices β Number of Slice operators.
slice_size β Length for the Slice operators on the slicing axis.
concat_axis β Axis on which to concatenate.
- Returns:
Name of newly inserted Concat node.
- onnx_helpers.rewrite_gemm_as_conv(model: onnx.ModelProto, after_node: str, at_node: str, *, w_r: int, w_s: int, w_c: int, w_k: int) str [source]ο
Rewrite a Gemm operator as a Conv operator between two nodes.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
after_node β Name of the node after which the Conv operator is inserted.
at_node β Name of the node before which the Conv operator is inserted.
w_k (w_r, w_s, w_c,) β Shape of the convolution weight as RSCK
- Returns:
Name of newly inserted Conv node.
- onnx_helpers.connect_nodes(model: onnx.ModelProto, node_pair: List[str], out_idx: int, in_idx: int)[source]ο
Connect two nodes by feeding the output of one node to the input of next node.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
node_pair β List of two nodes in execution order.
out_idx β The index of output of the first node.
in_idx β The index of input of the second node.
- onnx_helpers.remove_output(model: onnx.ModelProto, keep_list: List[str] = None)[source]ο
Run outputs of a model.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
keep_list β List of output names not to be removed.
- onnx_helpers.remove_outputs_by_name_list(model: onnx.ModelProto, name_list: List[str])[source]ο
Remove multiple outputs by a list of names.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
name_list β List of names of the outputs to be removed.
- onnx_helpers.remove_inputs_by_name_list(model: onnx.ModelProto, name_list: List[str])[source]ο
Remove multiple inputs by a list of names.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
name_list β List of names of the inputs to be removed.
- onnx_helpers.add_io(model: onnx.ModelProto, io_name: str, io_shape: Tuple[int], io_dir: str)[source]ο
Add input or output to a model.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
io_name β Name to be added for an input/output.
io_shape β Shape of the added input/output.
io_dir β Designation of input or output.
- onnx_helpers.add_input(model: onnx.ModelProto, input_name: str, input_shape: Tuple[int])[source]ο
Add input to a model.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
input_name β Name of the input to be added.
input_shape β Shape of the input to be added.
- onnx_helpers.add_output(model: onnx.ModelProto, output_name, output_shape)[source]ο
Add output to a model.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
output_name β Name of the output to be added.
output_shape β Shape of the output to be added.
- onnx_helpers.get_io_shape(node: onnx.NodeProto) Tuple[int] [source]ο
Get the shape of a tensor.
- Parameters:
node β Node representing the tensor.
- Returns:
Shape of the tensor.
- onnx_helpers.update_io_shape(model: onnx.ModelProto, io_name: str, new_shape: Tuple[int])[source]ο
Update I/O shape in a model.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
io_name β Name of the I/O
new_shape β Shape of the I/O
- onnx_helpers.change_node_output(model: onnx.ModelProto, node_name: str, new_output_name: str, old_output_name: str = None)[source]ο
Change output of a node in a model.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
node_name β Name of a node.
new_output_name β Name of the new output for the node.
old_output_name β Name of previous output to replace.
- onnx_helpers.is_initializer(model: onnx.ModelProto, name: str) bool [source]ο
Check if a name is an initializer.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
name β Name to check on.
- Returns:
True if provided name is an initializer.
- onnx_helpers.find_initializer_value(model: onnx.ModelProto, initializer_name: str) numpy.ndarray [source]ο
Find value of an initializer in a model.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
initializer_name β Name of the initializer.
- Returns:
Value of the initializer.
- onnx_helpers.remove_initializers(model: onnx.ModelProto, name_list: List[str])[source]ο
Remove initializers from a list.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
name_list β List of initializer names.
- onnx_helpers.add_initializer(model: onnx.ModelProto, initializer_name: str, initializer_value: numpy.ndarray)[source]ο
Add an initializer to a model.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
initializer_name β Name of the initializer.
initializer_value β Value of the initializer.
- onnx_helpers.remove_duplicated_initializer(model: onnx.ModelProto)[source]ο
Remove duplicated initializers in a model.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
- onnx_helpers.transpose_reshape_constant(model: onnx.ModelProto, param_name: str, perm: Tuple[int], new_shape: Tuple[int])[source]ο
Perform transpose and/or reshape on an existing constant initializer.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
param_name β Name of the constant as an initializer.
perm β Permutation to be performed.
new_shape β New shape to be reshaped.
- onnx_helpers.convert_initializer(model: onnx.ModelProto, init_name: str, convert_type: str, convert_func: Callable[[numpy.ndarray], numpy.ndarray] | None = None)[source]ο
Convert an initializer in a model.
- Parameters:
model β Loaded model in onnx.ModelProto representation.
init_name β Name of the initializer.
convert_type β String describing format conversion.
convert_func β Custom conversion function for βcustomβ convert_type.
- onnx_helpers.uniquify_initializer_name(prefix_name: str, model: onnx.ModelProto) onnx.ModelProto [source]ο
Uniquify initializer names in a model by a prefix name.
- Parameters:
prefix_name β Name of the prefix to prepend to all initializer names.
model β Loaded model in onnx.ModelProto representation.
- Returns:
Updated model in onnx.ModelProto representation.
- onnx_helpers.export_to_json(model_path: str, json_file_path: str = 'model.json')[source]ο
Convert a model to a simpler json readable representation :param model: Loaded model in onnx.ModelProto representation. :param the path where to write the json data to
- onnx_helpers.update_model_node_names(model_path: str, updated_model_path: str = 'updated_model.onnx')[source]ο
Update a model, with node names At times, when converting a model from pytorch to onnx the nodes names are not generated. This API helps in updating the nodes with generated names identified by the operator :param model_path: Input modelpath :param updated_model_path: Modified model save path