onnx_light.onnx.helper#

class onnx_light.onnx.helper.TensorDtypeMap(np_dtype, storage_dtype, name)#
name: str#

Alias for field number 2

np_dtype: dtype#

Alias for field number 0

storage_dtype: int#

Alias for field number 1

onnx_light.onnx.helper.get_attribute_value(attr: AttributeProto) Any#

Returns the attribute value whatever the type is.

onnx_light.onnx.helper.make_attribute(key: str, value: Any, doc_string: str | None = None, attr_type: int | None = None) AttributeProto#

Makes an AttributeProto based on the value type.

onnx_light.onnx.helper.make_attribute_ref(name: str, attr_type: AttributeType, doc_string: str | None = None) AttributeProto#

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

onnx_light.onnx.helper.make_empty_tensor_value_info(name: str) ValueInfoProto#

Creates an empty tensor value info.

onnx_light.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#

Constructs a FunctionProto.

Parameters:
  • domain – domain name

  • fname – function name

  • inputs – input names

  • outputs – output names

  • nodes – sequence of NodeProto

  • opset_imports – required domains (opset_import)

  • attributes – attribute names

  • attribute_protos – typed attributes

  • doc_string – documentation

  • overload – overload

  • value_info – information about type and shape for results

Returns:

a FunctionProto

onnx_light.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#

Constructs 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_light.onnx.helper.make_map(name: str, key_type: int, keys: list[Any], values: SequenceProto) MapProto#

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_light.onnx.helper.make_map_type_proto(key_type: int, value_type: TypeProto) TypeProto#

Makes a map TypeProto.

onnx_light.onnx.helper.make_model(graph: GraphProto, ir_version: int | None = None, opset_imports: Sequence[OperatorSetIdProto] | None = None, functions: Sequence[FunctionProto] | None = None, metadata_props: Sequence[StringStringEntryProto] | None = None, doc_string: str | None = None, producer_name: str | None = None, producer_version: str | None = None) ModelProto#

Constructs a ModelProto

Parameters:
  • graph – GraphProto

  • ir_version – ir version; defaults to defs.onnx_ir_version() when None

  • opset_imports – required domains, use the default one if missing

  • functions – list of functions

  • metadata_props – additional information

  • producer_name – producer name

  • producer_version – producer version

  • doc_string – documentation

Returns:

model

onnx_light.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#

Constructs 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_light.onnx.helper.make_operatorsetid(domain: str, version: int) OperatorSetIdProto#

Construct an OperatorSetIdProto.

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

  • version (integer) – Version of operator set id

Returns:

OperatorSetIdProto

onnx_light.onnx.helper.make_opsetid(domain: str, version: int) OperatorSetIdProto#

Constructs an OperatorSetIdProto.

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

  • version (integer) – Version of operator set id

Returns:

OperatorSetIdProto

onnx_light.onnx.helper.make_optional(name: str, elem_type: DataType, value) OptionalProto#

Make an Optional with specified value arguments.

onnx_light.onnx.helper.make_optional_type_proto(inner_type_proto: TypeProto) TypeProto#

Makes an optional TypeProto.

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

Make a Sequence with specified value arguments.

onnx_light.onnx.helper.make_sequence_type_proto(inner_type_proto: TypeProto) TypeProto#

Makes a sequence TypeProto.

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

Construct a SparseTensorProto

Parameters:
Returns:

SparseTensorProto

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

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

onnx_light.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#

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

onnx_light.onnx.helper.make_tensor(name: str, data_type: int, dims: Sequence[int], vals: Sequence[int | float] | bytes | ndarray | None = None, raw: bool = False, external_data: dict[str, str] | Sequence[tuple[str, str]] | None = None) TensorProto#

Makes 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 – tensor name

  • data_type – a value such as TensorProto.FLOAT

  • dims – shape

  • vals – values. Must be None (or omitted) when external_data is provided, since the tensor content then lives in an external file.

  • raw – 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.

  • external_data – optional mapping (or sequence of (key, value) pairs) describing how to retrieve the tensor’s data from an external file. Common keys are "location", "offset", "length" and "checksum". When provided, the resulting tensor has its data_location set to TensorProto.EXTERNAL and its external_data field populated with the given entries; vals must be None and raw must be False.

Returns:

TensorProto

onnx_light.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#

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

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

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

onnx_light.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#

Makes a ValueInfoProto based on the data type and shape.

onnx_light.onnx.helper.make_value_info(name: str, type_proto: TypeProto, doc_string: str = '') ValueInfoProto#

Makes a ValueInfoProto with the given type_proto.

onnx_light.onnx.helper.np_dtype_to_tensor_dtype(np_dtype: dtype) int#

Converts the numpy dtype to corresponding TensorProto’s data_type.

Parameters:

np_dtype – numpy’s data_type

Returns:

TensorProto’s data_type

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

Sets metadata_props.

onnx_light.onnx.helper.set_model_props(model: ModelProto, dict_value: dict[str, str]) None#

Sets metadata_props.

onnx_light.onnx.helper.tensor_dtype_to_field(tensor_dtype: int) str#

Converts 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_light.onnx.helper.tensor_dtype_to_np_dtype(tensor_dtype: int) dtype#

Converts 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_light.onnx.helper.tensor_dtype_to_storage_tensor_dtype(tensor_dtype: int) int#

Converts a TensorProto’s data_type to the storage data_type used in proto fields.

For example, INT8 and UINT8 are stored as INT32, and FLOAT16 is stored as INT32.

Parameters:

tensor_dtype – TensorProto’s data_type

Returns:

The storage TensorProto data_type