onnx_light.onnx.numpy_helper#

onnx_light.onnx.numpy_helper.create_random_int(input_shape: tuple[int, ...], dtype: dtype, seed: int = 1) ndarray#

Creates a random integer array for backend test cases.

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.

onnx_light.onnx.numpy_helper.from_array(array: ndarray, /, name: str | None = None) TensorProto#

Converts a numpy array into a TensorProto.

Parameters:
  • array – a numpy array.

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

Returns:

The converted TensorProto.

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

Converts a Python dictionary into a MapProto.

Parameters:
  • dict – Python dictionary.

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

Returns:

The converted MapProto.

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

Converts a Python list into a SequenceProto.

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 SequenceProto.

onnx_light.onnx.numpy_helper.from_optional(opt: Any | None, name: str | None = None, dtype: int | None = None) OptionalProto#

Converts an optional value into an OptionalProto.

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 OptionalProto.

onnx_light.onnx.numpy_helper.saturate_cast(x: ndarray, dtype: dtype) ndarray#

Performs saturate-cast of a numpy array to the target dtype.

Values outside the representable range of the target dtype are clamped to the maximum or minimum representable value of that dtype.

Parameters:
  • x – Input array.

  • dtype – Target numpy dtype.

Returns:

The clamped and cast array.

onnx_light.onnx.numpy_helper.to_array(tensor: TensorProto, base_dir: str = '') ndarray#

Converts a TensorProto object to a numpy array.

This function uses ml_dtypes if the dtype is not a native numpy dtype.

Parameters:
  • tensor – a TensorProto object.

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

Returns:

The converted numpy array.

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

Converts a MapProto to a Python dictionary.

Parameters:

map_proto – a MapProto object.

Returns:

The converted dictionary.

onnx_light.onnx.numpy_helper.to_float8e8m0(x: ndarray, saturate: bool = True, round_mode: str = 'up') ndarray#

Converts a float32 NumPy array to float8e8m0 representation.

If the input is not a float32 array, it will be cast to one first.

Parameters:
  • x – Input array to convert.

  • saturate – Whether to saturate at max/min float8e8m0 value.

  • round_mode – “nearest”, “up”, or “down”.

Returns:

Array of ml_dtypes.float8_e8m0fnu values.

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

Converts a SequenceProto to a Python list.

Parameters:

sequence – a SequenceProto object.

Returns:

The converted list.

onnx_light.onnx.numpy_helper.to_optional(optional: OptionalProto) Any | None#

Converts an OptionalProto to a Python optional.

Parameters:

optional – an OptionalProto object.

Returns:

The converted optional value, or None for UNDEFINED.

onnx_light.onnx.numpy_helper.tobytes_little_endian(array: ndarray) bytes#

Converts an array into bytes in little endian byte order.

Parameters:

array – a numpy array.

Returns:

Byte representation of passed array in little endian byte order.