onnx.helper

find_min_ir_version_for(opsetidlist[, ...])

Given list of opset ids, determine minimum IR version required.

get_all_tensor_dtypes()

Get all tensor types from TensorProto.

get_attribute_value(attr)

get_node_attr_value(node, attr_name)

set_metadata_props(proto, dict_value)

set_model_props(model, dict_value)

float32_to_bfloat16(fval[, truncate])

float32_to_float8e4m3(fval[, scale, fn, uz, ...])

Convert a float32 value to a float8, e4m3 (as int).

float32_to_float8e5m2(fval[, scale, fn, uz, ...])

Convert a float32 value to a float8, e5m2 (as int).

make_attribute(key, value[, doc_string, ...])

Makes an AttributeProto based on the value type.

make_attribute_ref(name, attr_type[, doc_string])

Make an AttributeProto holding a reference to the parent function's attribute of given name and type.

make_empty_tensor_value_info(name)

make_function(domain, fname, inputs, ...[, ...])

make_graph(nodes, name, inputs, outputs[, ...])

Construct a GraphProto

make_map(name, key_type, keys, values)

Make a Map with specified key-value pair arguments.

make_map_type_proto(key_type, value_type)

Makes a map TypeProto.

make_model(graph, **kwargs)

Construct a ModelProto

make_node(op_type, inputs, outputs[, name, ...])

Construct a NodeProto.

make_operatorsetid(domain, version)

Construct an OperatorSetIdProto.

make_opsetid(domain, version)

Construct an OperatorSetIdProto.

make_model_gen_version(graph, **kwargs)

make_optional(name, elem_type, value)

Make an Optional with specified value arguments.

make_optional_type_proto(inner_type_proto)

Makes an optional TypeProto.

make_sequence(name, elem_type, values)

Make a Sequence with specified value arguments.

make_sequence_type_proto(inner_type_proto)

Makes a sequence TypeProto.

make_sparse_tensor(values, indices, dims)

Construct a SparseTensorProto

make_sparse_tensor_type_proto(elem_type, shape)

Makes a SparseTensor TypeProto based on the data type and shape.

make_sparse_tensor_value_info(name, ...[, ...])

Makes a SparseTensor ValueInfoProto based on the data type and shape.

make_tensor(name, data_type, dims, vals[, raw])

Make a TensorProto with specified arguments.

make_tensor_sequence_value_info(name, ...[, ...])

Makes a Sequence[Tensors] ValueInfoProto based on the data type and shape.

make_tensor_type_proto(elem_type, shape[, ...])

Makes a Tensor TypeProto based on the data type and shape.

make_training_info(algorithm, ...)

make_tensor_value_info(name, elem_type, shape)

Makes a ValueInfoProto based on the data type and shape.

make_value_info(name, type_proto[, doc_string])

Makes a ValueInfoProto with the given type_proto.

np_dtype_to_tensor_dtype(np_dtype)

Convert a numpy's dtype to corresponding tensor type.

printable_attribute(attr[, subgraphs])

printable_dim(dim)

printable_graph(graph[, prefix])

Display a GraphProto as a string.

printable_node(node[, prefix, subgraphs])

printable_tensor_proto(t)

printable_type(t)

printable_value_info(v)

split_complex_to_pairs(ca)

create_op_set_id_version_map(table)

Create a map from (opset-domain, opset-version) to ir-version from above table.

strip_doc_string(proto)

Empties doc_string field on any nested protobuf messages

pack_float32_to_4bit(array, signed)

Convert an array of float32 value to a 4bit data-type and pack every two concecutive elements in a byte.

tensor_dtype_to_np_dtype(tensor_dtype)

Convert a TensorProto's data_type to corresponding numpy dtype.

tensor_dtype_to_storage_tensor_dtype(...)

Convert a TensorProto's data_type to corresponding data_type for storage.

tensor_dtype_to_string(tensor_dtype)

Get the name of given TensorProto's data_type.

tensor_dtype_to_field(tensor_dtype)

Convert a TensorProto's data_type to corresponding field name for storage.

getter

onnx.helper.get_attribute_value(attr: AttributeProto) Any[source]
onnx.helper.get_node_attr_value(node: NodeProto, attr_name: str) Any[source]

setter

onnx.helper.set_metadata_props(proto: ModelProto | GraphProto | FunctionProto | NodeProto | TensorProto | ValueInfoProto, dict_value: dict[str, str]) None[source]
onnx.helper.set_model_props(model: ModelProto, dict_value: dict[str, str]) None[source]

print

onnx.helper.printable_attribute(attr: AttributeProto, subgraphs: bool = False) str | tuple[str, list[GraphProto]][source]
onnx.helper.printable_dim(dim: Dimension) str[source]
onnx.helper.printable_graph(graph: GraphProto, prefix: str = '') str[source]

Display a GraphProto as a string.

Parameters:
  • graph (GraphProto) – the graph to display

  • prefix (string) – prefix of every line

Returns:

string

onnx.helper.printable_node(node: NodeProto, prefix: str = '', subgraphs: bool = False) str | tuple[str, list[GraphProto]][source]
onnx.helper.printable_tensor_proto(t: TensorProto) str[source]
onnx.helper.printable_type(t: TypeProto) str[source]
onnx.helper.printable_value_info(v: ValueInfoProto) str[source]

tools

onnx.helper.find_min_ir_version_for(opsetidlist: Sequence[OperatorSetIdProto], ignore_unknown: bool = False) int[source]

Given list of opset ids, determine minimum IR version required.

Parameters:
  • opsetidlist – A sequence of OperatorSetIdProto.

  • ignore_unknown – If True, ignore unknown domain and return default minimum version for that domain.

Returns:

The minimum IR version required (integer)

onnx.helper.split_complex_to_pairs(ca: Sequence[complex64]) Sequence[int][source]
onnx.helper.create_op_set_id_version_map(table: List[Tuple[str, int, int, int] | Tuple[str, int, int, int, int]]) Dict[Tuple[str, int], int][source]

Create a map from (opset-domain, opset-version) to ir-version from above table.

onnx.helper.strip_doc_string(proto: Message) None[source]

Empties doc_string field on any nested protobuf messages

onnx.helper.pack_float32_to_4bit(array: ndarray | Sequence, signed: bool) ndarray[source]

Convert an array of float32 value to a 4bit data-type and pack every two concecutive elements in a byte. See 4 bit integer types for technical details.

Parameters:
  • array – array of float to convert and pack

  • signed – Whether the 4 bit variant is signed or unsigned

Returns:

Packed array with size ceil(farray.size/2) (single dimension).

make function

All functions uses to create an ONNX graph.

onnx.helper.make_attribute(key: str, value: Any, doc_string: str | None = None, attr_type: int | None = None) AttributeProto[source]

Makes an AttributeProto based on the value type.

onnx.helper.make_attribute_ref(name: str, attr_type: AttributeProto.AttributeType, doc_string: str | None = None) AttributeProto[source]

Make an AttributeProto holding a reference to the parent function’s attribute of given name and type.

onnx.helper.make_empty_tensor_value_info(name: str) ValueInfoProto[source]
onnx.helper.make_function(domain: str, fname: str, inputs: Sequence[str], outputs: Sequence[str], nodes: Sequence[NodeProto], opset_imports: Sequence[OperatorSetIdProto], attributes: Sequence[str] | None = None, attribute_protos: Sequence[AttributeProto] | None = None, doc_string: str | None = None, overload: str | None = None, value_info: Sequence[ValueInfoProto] | None = None) FunctionProto[source]
onnx.helper.make_graph(nodes: Sequence[NodeProto], name: str, inputs: Sequence[ValueInfoProto], outputs: Sequence[ValueInfoProto], initializer: Sequence[TensorProto] | None = None, doc_string: str | None = None, value_info: Sequence[ValueInfoProto] | None = None, sparse_initializer: Sequence[SparseTensorProto] | None = None) GraphProto[source]

Construct a GraphProto

Parameters:
  • nodes – list of NodeProto

  • name (string) – graph name

  • inputs – list of ValueInfoProto

  • outputs – list of ValueInfoProto

  • initializer – list of TensorProto

  • doc_string (string) – graph documentation

  • value_info – list of ValueInfoProto

  • sparse_initializer – list of SparseTensorProto

Returns:

GraphProto

onnx.helper.make_map(name: str, key_type: int, keys: list[Any], values: SequenceProto) MapProto[source]

Make a Map with specified key-value pair arguments.

Criteria for conversion: - Keys and Values must have the same number of elements - Every key in keys must be of the same type - Every value in values must be of the same type

onnx.helper.make_map_type_proto(key_type: int, value_type: TypeProto) TypeProto[source]

Makes a map TypeProto.

onnx.helper.make_model(graph: GraphProto, **kwargs: Any) ModelProto[source]

Construct a ModelProto

Parameters:
  • graph (GraphProto) – make_graph returns

  • **kwargs – any attribute to add to the returned instance

Returns:

ModelProto

onnx.helper.make_node(op_type: str, inputs: Sequence[str], outputs: Sequence[str], name: str | None = None, doc_string: str | None = None, domain: str | None = None, overload: str | None = None, **kwargs: Any) NodeProto[source]

Construct a NodeProto.

Parameters:
  • op_type (string) – The name of the operator to construct

  • inputs (list of string) – list of input names

  • outputs (list of string) – list of output names

  • name (string, default None) – optional unique identifier for NodeProto

  • doc_string (string, default None) – optional documentation string for NodeProto

  • domain (string, default None) – optional domain for NodeProto. If it’s None, we will just use default domain (which is empty)

  • overload (string, default None) – optional field, used to resolve calls to model-local functions

  • **kwargs (dict) – the attributes of the node. The acceptable values are documented in make_attribute().

Returns:

NodeProto

onnx.helper.make_operatorsetid(domain: str, version: int) OperatorSetIdProto[source]

Construct an OperatorSetIdProto.

Parameters:
  • domain (string) – The domain of the operator set id

  • version (integer) – Version of operator set id

Returns:

OperatorSetIdProto

onnx.helper.make_opsetid(domain: str, version: int) OperatorSetIdProto[source]

Construct an OperatorSetIdProto.

Parameters:
  • domain (string) – The domain of the operator set id

  • version (integer) – Version of operator set id

Returns:

OperatorSetIdProto

onnx.helper.make_model_gen_version(graph: GraphProto, **kwargs: Any) ModelProto[source]
onnx.helper.make_optional(name: str, elem_type: OptionalProto.DataType, value: Any | None) OptionalProto[source]

Make an Optional with specified value arguments.

onnx.helper.make_optional_type_proto(inner_type_proto: TypeProto) TypeProto[source]

Makes an optional TypeProto.

onnx.helper.make_sequence(name: str, elem_type: SequenceProto.DataType, values: Sequence[Any]) SequenceProto[source]

Make a Sequence with specified value arguments.

onnx.helper.make_sequence_type_proto(inner_type_proto: TypeProto) TypeProto[source]

Makes a sequence TypeProto.

onnx.helper.make_sparse_tensor(values: TensorProto, indices: TensorProto, dims: Sequence[int]) SparseTensorProto[source]

Construct a SparseTensorProto

Parameters:
Returns:

SparseTensorProto

onnx.helper.make_sparse_tensor_type_proto(elem_type: int, shape: Sequence[str | int | None] | None, shape_denotation: list[str] | None = None) TypeProto[source]

Makes a SparseTensor TypeProto based on the data type and shape.

onnx.helper.make_sparse_tensor_value_info(name: str, elem_type: int, shape: Sequence[str | int | None] | None, doc_string: str = '', shape_denotation: list[str] | None = None) ValueInfoProto[source]

Makes a SparseTensor ValueInfoProto based on the data type and shape.

onnx.helper.make_tensor(name: str, data_type: int, dims: Sequence[int], vals: Any, raw: bool = False) TensorProto[source]

Make a TensorProto with specified arguments. If raw is False, this function will choose the corresponding proto field to store the values based on data_type. If raw is True, use “raw_data” proto field to store the values, and values should be of type bytes in this case.

Parameters:
  • name (string) – tensor name

  • data_type (int) – a value such as onnx.TensorProto.FLOAT

  • dims (List[int]) – shape

  • vals – values

  • raw (bool) – if True, vals contains the serialized content of the tensor, otherwise, vals should be a list of values of the type defined by data_type

Returns:

TensorProto

onnx.helper.make_tensor_sequence_value_info(name: str, elem_type: int, shape: Sequence[str | int | None] | None, doc_string: str = '', elem_shape_denotation: list[str] | None = None) ValueInfoProto[source]

Makes a Sequence[Tensors] ValueInfoProto based on the data type and shape.

onnx.helper.make_tensor_type_proto(elem_type: int, shape: Sequence[str | int | None] | None, shape_denotation: list[str] | None = None) TypeProto[source]

Makes a Tensor TypeProto based on the data type and shape.

onnx.helper.make_training_info(algorithm: GraphProto, algorithm_bindings: List[Tuple[str, str]], initialization: GraphProto | None, initialization_bindings: List[Tuple[str, str]] | None) TrainingInfoProto[source]
onnx.helper.make_tensor_value_info(name: str, elem_type: int, shape: Sequence[str | int | None] | None, doc_string: str = '', shape_denotation: list[str] | None = None) ValueInfoProto[source]

Makes a ValueInfoProto based on the data type and shape.

onnx.helper.make_value_info(name: str, type_proto: TypeProto, doc_string: str = '') ValueInfoProto[source]

Makes a ValueInfoProto with the given type_proto.

type mappings

onnx.helper.get_all_tensor_dtypes() KeysView[int][source]

Get all tensor types from TensorProto.

Returns:

all tensor types from TensorProto

onnx.helper.np_dtype_to_tensor_dtype(np_dtype: dtype) int[source]

Convert a numpy’s dtype to corresponding tensor type. It can be used while converting numpy arrays to tensors.

Parameters:

np_dtype – numpy’s data_type

Returns:

TensorsProto’s data_type

onnx.helper.tensor_dtype_to_field(tensor_dtype: int) str[source]

Convert a TensorProto’s data_type to corresponding field name for storage. It can be used while making tensors.

Parameters:

tensor_dtype – TensorProto’s data_type

Returns:

field name

onnx.helper.tensor_dtype_to_np_dtype(tensor_dtype: int) dtype[source]

Convert a TensorProto’s data_type to corresponding numpy dtype. It can be used while making tensor.

Parameters:

tensor_dtype – TensorProto’s data_type

Returns:

numpy’s data_type

onnx.helper.tensor_dtype_to_storage_tensor_dtype(tensor_dtype: int) int[source]

Convert a TensorProto’s data_type to corresponding data_type for storage.

Parameters:

tensor_dtype – TensorProto’s data_type

Returns:

data_type for storage

onnx.helper.tensor_dtype_to_string(tensor_dtype: int) str[source]

Get the name of given TensorProto’s data_type.

Parameters:

tensor_dtype – TensorProto’s data_type

Returns:

the name of data_type

cast

onnx.helper.float32_to_bfloat16(fval: float, truncate: bool = False) int[source]
onnx.helper.float32_to_float8e4m3(fval: float, scale: float = 1.0, fn: bool = True, uz: bool = False, saturate: bool = True) int[source]

Convert a float32 value to a float8, e4m3 (as int).

See Float stored in 8 bits for technical details.

Parameters:
  • fval – float to convert

  • scale – scale, divide fval by scale before casting it

  • fn – no infinite values

  • uz – no negative zero

  • saturate – if True, any value out of range included inf becomes the maximum value, otherwise, it becomes NaN. The description of operator Cast fully describes the differences.

Returns:

converted float

onnx.helper.float32_to_float8e5m2(fval: float, scale: float = 1.0, fn: bool = False, uz: bool = False, saturate: bool = True) int[source]

Convert a float32 value to a float8, e5m2 (as int).

Parameters:
  • fval – float to convert

  • scale – scale, divide fval by scale before casting it

  • fn – no infinite values

  • uz – no negative zero

  • saturate – if True, any value out of range included inf becomes the maximum value, otherwise, it becomes NaN. The description of operator Cast fully describes the differences.

Returns:

converted float