onnx.numpy_helper

bfloat16_to_float32(data[, dims])

Converts ndarray of bf16 (as uint32) to f32 (as uint32).

float8e4m3_to_float32(data[, dims, fn, uz])

Converts ndarray of float8, e4m3 (as uint32) to f32 (as uint32).

float8e5m2_to_float32(data[, dims, fn, uz])

Converts ndarray of float8, e5m2 (as uint32) to f32 (as uint32).

from_array(arr[, name])

Converts a numpy array to a tensor def.

from_dict(dict_[, name])

Converts a Python dictionary into a map def.

from_list(lst[, name, dtype])

Converts a list into a sequence def.

from_optional(opt[, name, dtype])

Converts an optional value into a Optional def.

to_array(tensor[, base_dir])

Converts a tensor def object to a numpy array.

to_dict(map_proto)

Converts a map def to a Python dictionary.

to_list(sequence)

Converts a sequence def to a Python list.

to_optional(optional)

Converts an optional def to a Python optional.

array

onnx.numpy_helper.from_array(arr: ndarray, name: str | None = None) TensorProto[source]

Converts a numpy array to a tensor def.

Parameters:
  • arr – a numpy array.

  • name – (optional) the name of the tensor.

Returns:

the converted tensor def.

Return type:

TensorProto

onnx.numpy_helper.to_array(tensor: TensorProto, base_dir: str = '') ndarray[source]

Converts a tensor def object to a numpy array.

Parameters:
  • tensor – a TensorProto object.

  • base_dir – if external tensor exists, base_dir can help to find the path to it

Returns:

the converted array.

Return type:

arr

sequence

onnx.numpy_helper.to_list(sequence: SequenceProto) list[Any][source]

Converts a sequence def to a Python list.

Parameters:

sequence – a SequenceProto object.

Returns:

the converted list.

Return type:

list

onnx.numpy_helper.from_list(lst: list[Any], name: str | None = None, dtype: int | None = None) SequenceProto[source]

Converts a list into a sequence def.

Parameters:
  • lst – a Python list

  • name – (optional) the name of the sequence.

  • dtype – (optional) type of element in the input list, used for specifying sequence values when converting an empty list.

Returns:

the converted sequence def.

Return type:

SequenceProto

dictionary

onnx.numpy_helper.to_dict(map_proto: MapProto) dict[Any, Any][source]

Converts a map def to a Python dictionary.

Parameters:

map_proto – a MapProto object.

Returns:

The converted dictionary.

onnx.numpy_helper.from_dict(dict_: dict[Any, Any], name: str | None = None) MapProto[source]

Converts a Python dictionary into a map def.

Parameters:
  • dict – Python dictionary

  • name – (optional) the name of the map.

Returns:

the converted map def.

Return type:

MapProto

optional

onnx.numpy_helper.to_optional(optional: OptionalProto) Any | None[source]

Converts an optional def to a Python optional.

Parameters:

optional – an OptionalProto object.

Returns:

the converted optional.

Return type:

opt

onnx.numpy_helper.from_optional(opt: Any | None, name: str | None = None, dtype: int | None = None) OptionalProto[source]

Converts an optional value into a Optional def.

Parameters:
  • opt – a Python optional

  • name – (optional) the name of the optional.

  • dtype – (optional) type of element in the input, used for specifying optional values when converting empty none. dtype must be a valid OptionalProto.DataType value

Returns:

the converted optional def.

Return type:

optional

tools

onnx.numpy_helper.convert_endian(tensor: TensorProto) None[source]

Call to convert endianness of raw data in tensor.

Parameters:

tensor – TensorProto to be converted.

onnx.numpy_helper.combine_pairs_to_complex(fa: Sequence[int]) list[complex][source]
onnx.numpy_helper.create_random_int(input_shape: tuple[int], dtype: dtype, seed: int = 1) ndarray[source]

Create random integer array for backend/test/case/node.

Parameters:
  • input_shape – The shape for the returned integer array.

  • dtype – The NumPy data type for the returned integer array.

  • seed – The seed for np.random.

Returns:

Random integer array.

Return type:

np.ndarray

cast

onnx.numpy_helper.bfloat16_to_float32(data: int16 | int32 | ndarray, dims: int | Sequence[int] | None = None) ndarray[source]

Converts ndarray of bf16 (as uint32) to f32 (as uint32).

Parameters:
  • data – A numpy array, empty dimensions are allowed if dims is None.

  • dims – If specified, the function reshapes the results.

Returns:

A numpy array of float32 with the same dimension if dims is None, or reshaped to dims if specified

onnx.numpy_helper.float8e4m3_to_float32(data: int16 | int32 | ndarray, dims: int | Sequence[int] | None = None, fn: bool = True, uz: bool = False) ndarray[source]

Converts ndarray of float8, e4m3 (as uint32) to f32 (as uint32).

See Float stored in 8 bits for technical details.

Parameters:
  • data – A numpy array, empty dimensions are allowed if dims is None.

  • dims – If specified, the function reshapes the results.

  • fn – No infinite values.

  • uz – No negative zero.

Returns:

A numpy array of float32 with the same dimension if dims is None, or reshaped to dims if specified.

onnx.numpy_helper.float8e5m2_to_float32(data: int16 | int32 | ndarray, dims: int | Sequence[int] | None = None, fn: bool = False, uz: bool = False) ndarray[source]

Converts ndarray of float8, e5m2 (as uint32) to f32 (as uint32).

See Float stored in 8 bits for technical details.

Parameters:
  • data – A numpy array, empty dimensions are allowed if dims is None.

  • dims – If specified, the function reshapes the results.

  • fn – No infinite values.

  • uz – No negative zero.

Returns:

A numpy array of float32 with the same dimension if dims is None, or reshaped to dims if specified