onnx.numpy_helper

bfloat16_to_float32(data[, dims])

将bf16(作为uint32)的ndarray转换为f32(作为uint32)。

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

将float8、e4m3(作为uint32)的ndarray转换为f32(作为uint32)。

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

将float8、e5m2(作为uint32)的ndarray转换为f32(作为uint32)。

from_array(tensor[, name])

将数组转换为TensorProto,包括在onnx._custom_element_types中定义的支持类型。

from_dict(dict_[, name])

将Python字典转换为映射定义。

from_list(lst[, name, dtype])

将列表转换为序列 def。

from_optional(opt[, 名称, 数据类型])

将可选值转换为Optional def。

to_array(tensor[, base_dir])

将张量定义对象转换为numpy数组。

to_dict(map_proto)

将地图定义转换为Python字典。

to_list(sequence)

将序列 def 转换为 Python 列表。

to_optional(optional)

将可选的def转换为Python的可选类型。

数组

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

将数组转换为TensorProto,包括在onnx._custom_element_types中定义的支持类型。

Parameters:
  • tensor – 一个numpy数组。

  • name – (可选)张量的名称。

Returns:

转换后的张量定义。

Return type:

TensorProto

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

将张量定义对象转换为numpy数组。 支持在onnx._custom_element_types中定义的类型。

Parameters:
  • tensor – 一个 TensorProto 对象。

  • base_dir – 如果存在外部张量,base_dir可以帮助找到它的路径

Returns:

转换后的数组。

Return type:

数组

由于numpy不支持ONNX中定义的所有类型(float 8类型、blofat16、int4、uint4、float4e2m1),这两个函数使用了在:mod:onnx._custom_element_types中定义的自定义dtype。

序列

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

将序列 def 转换为 Python 列表。

Parameters:

sequence – 一个 SequenceProto 对象。

Returns:

转换后的列表。

Return type:

list

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

将列表转换为序列 def。

Parameters:
  • lst – 一个Python列表

  • name – (可选)序列的名称。

  • dtype – (可选)输入列表中元素的类型,用于在转换空列表时指定序列值。

Returns:

转换后的序列定义。

Return type:

SequenceProto

字典

onnx.numpy_helper.to_dict(map_proto: MapProto) dict[Any, Any][源代码]

将地图定义转换为Python字典。

Parameters:

map_proto – 一个 MapProto 对象。

Returns:

转换后的字典。

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

将Python字典转换为映射定义。

Parameters:
  • dict – Python 字典

  • name – (可选)地图的名称。

Returns:

转换后的地图定义。

Return type:

MapProto

可选的

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

将可选的def转换为Python的可选类型。

Parameters:

optional – 一个 OptionalProto 对象。

Returns:

转换后的可选。

Return type:

opt

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

将可选值转换为Optional def。

Parameters:
  • opt – 一个Python可选参数

  • name – (可选)可选参数的名称。

  • dtype – (可选)输入元素的类型,用于在转换空值时指定可选值。dtype 必须是有效的 OptionalProto.DataType 值

Returns:

转换后的可选定义。

Return type:

可选的

工具

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

调用以转换张量中原始数据的字节序。

Parameters:

tensor – 要转换的TensorProto。

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

为后端/测试/案例/节点创建随机整数数组。

Parameters:
  • input_shape – 返回的整数数组的形状。

  • dtype – 返回整数数组的NumPy数据类型。

  • seed – np.random 的种子。

Returns:

随机整数数组。

Return type:

np.ndarray

onnx.numpy_helper.unpack_int4(data: int32 | ndarray, dims: int | Sequence[int], signed: bool) ndarray[source]

将int4的ndarray(作为打包的uint8)转换为f32 有关技术细节,请参见4位整数类型

Parameters:
  • data – 一个numpy数组,如果dims为None,则允许空维度。

  • dims – 维度用于重塑解包的缓冲区

  • signed – 4位整数是有符号还是无符号

Returns:

一个被重塑为dims的float32类型的numpy数组。

cast

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

将bf16(作为uint32)的ndarray转换为f32(作为uint32)。

Parameters:
  • data – 一个numpy数组,如果dims为None,则允许空维度。

  • dims – 如果指定,函数将重新调整结果。

Returns:

如果dims为None,则返回一个具有相同维度的float32类型的numpy数组,如果指定了dims,则将其重塑为dims

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

将float8、e4m3(作为uint32)的ndarray转换为f32(作为uint32)。

有关技术细节,请参见8位存储的浮点数

Parameters:
  • data – 一个numpy数组,如果dims为None,则允许空维度。

  • dims – 如果指定,函数将重新调整结果。

  • fn – 没有无限值。

  • uz – 没有负零。

Returns:

如果dims为None,则返回一个具有相同维度的float32类型的numpy数组,如果指定了dims,则将其重塑为dims。

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

将float8、e5m2(作为uint32)的ndarray转换为f32(作为uint32)。

请参阅8位存储的浮点数以获取技术细节。

Parameters:
  • data – 一个numpy数组,如果dims为None,则允许空维度。

  • dims – 如果指定,函数将重新调整结果。

  • fn – 没有无限值。

  • uz – 没有负零。

Returns:

如果dims为None,则返回一个具有相同维度的float32类型的numpy数组,如果指定了dims,则将其重塑为dims