onnx.helper

find_min_ir_version_for(opsetidlist[, ...])

給定運算子集合 ID 列表,判斷所需的最低 IR 版本。

get_all_tensor_dtypes()

從 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, ...])

將 float32 值轉換為 float8,e4m3(為 int)。

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

將 float32 值轉換為 float8,e5m2(為 int)。

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

根據值類型製作 AttributeProto。

make_attribute_ref(name, attr_type[, doc_string])

製作一個 AttributeProto,其中包含對父函式具有給定名稱和類型的屬性的參考。

make_empty_tensor_value_info(name)

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

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

建構 GraphProto

make_map(name, key_type, keys, values)

製作具有指定鍵值對引數的 Map。

make_map_type_proto(key_type, value_type)

製作 Map TypeProto。

make_model(graph, **kwargs)

建構 ModelProto

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

建構 NodeProto。

make_operatorsetid(domain, version)

建構 OperatorSetIdProto。

make_opsetid(domain, version)

建構 OperatorSetIdProto。

make_model_gen_version(graph, **kwargs)

make_optional(name, elem_type, value)

製作具有指定值引數的 Optional。

make_optional_type_proto(inner_type_proto)

製作 optional TypeProto。

make_sequence(name, elem_type, values)

製作具有指定值引數的 Sequence。

make_sequence_type_proto(inner_type_proto)

製作 sequence TypeProto。

make_sparse_tensor(values, indices, dims)

建構 SparseTensorProto

make_sparse_tensor_type_proto(elem_type, shape)

根據資料類型和形狀製作 SparseTensor TypeProto。

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

根據資料類型和形狀製作 SparseTensor ValueInfoProto。

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

製作具有指定引數的 TensorProto。

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

根據資料類型和形狀製作 Sequence[Tensors] ValueInfoProto。

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

根據資料類型和形狀製作 Tensor TypeProto。

make_training_info(algorithm, ...)

make_tensor_value_info(name, elem_type, shape)

根據資料類型和形狀製作 ValueInfoProto。

make_value_info(name, type_proto[, doc_string])

製作具有給定 type_proto 的 ValueInfoProto。

np_dtype_to_tensor_dtype(np_dtype)

將 numpy 的 dtype 轉換為對應的張量類型。

printable_attribute(attr[, subgraphs])

printable_dim(dim)

printable_graph(graph[, prefix])

將 GraphProto 顯示為字串。

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)

從上表建立從 (opset-domain, opset-version) 到 ir-version 的映射。

strip_doc_string(proto)

清空任何巢狀 protobuf 訊息上的 doc_string 欄位

pack_float32_to_4bit(array, signed)

將 float32 值的陣列轉換為 4 位元資料類型,並將每兩個連續元素打包成一個位元組。

tensor_dtype_to_np_dtype(tensor_dtype)

將 TensorProto 的 data_type 轉換為對應的 numpy dtype。

tensor_dtype_to_storage_tensor_dtype(...)

將 TensorProto 的 data_type 轉換為用於儲存的對應 data_type。

tensor_dtype_to_string(tensor_dtype)

取得給定 TensorProto 的 data_type 名稱。

tensor_dtype_to_field(tensor_dtype)

將 TensorProto 的 data_type 轉換為用於儲存的對應欄位名稱。

getter

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

setter

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

print

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

將 GraphProto 顯示為字串。

參數:
  • graph (GraphProto) – 要顯示的圖形

  • prefix (string) – 每行的前綴

返回:

字串

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

工具

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

給定運算子集合 ID 列表,判斷所需的最低 IR 版本。

參數:
  • opsetidlist – 一個 OperatorSetIdProto 的序列。

  • ignore_unknown – 如果為 True,則忽略未知網域,並為該網域返回預設的最小版本。

返回:

所需的最小 IR 版本 (整數)

onnx.helper.split_complex_to_pairs(ca: Sequence[complex64]) Sequence[int][原始碼]
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][原始碼]

從上表建立從 (opset-domain, opset-version) 到 ir-version 的映射。

onnx.helper.strip_doc_string(proto: Message) None[原始碼]

清空任何巢狀 protobuf 訊息上的 doc_string 欄位

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

將 float32 數值陣列轉換為 4 位元資料類型,並將每兩個連續元素封裝到一個位元組中。 有關技術細節,請參閱4 位元整數類型

參數:
  • array – 要轉換和封裝的浮點數陣列

  • signed – 4 位元變體是否為有號或無號

返回:

封裝後的陣列大小為 ceil(farray.size/2)(單維度)。

建立函式

所有用於建立 ONNX 圖形的函式。

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

根據值類型製作 AttributeProto。

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

建立一個 AttributeProto,其中包含對父函數指定名稱和類型的屬性的參照。

onnx.helper.make_empty_tensor_value_info(name: str) ValueInfoProto[原始碼]
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[原始碼]
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[原始碼]

建構 GraphProto

參數:
  • nodes – NodeProto 的列表

  • name (string) – 圖的名稱

  • inputs – ValueInfoProto 的列表

  • outputs – ValueInfoProto 的列表

  • initializer – TensorProto 的列表

  • doc_string (string) – 圖的文件字串

  • value_info – ValueInfoProto 的列表

  • sparse_initializer – SparseTensorProto 的列表

返回:

GraphProto

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

製作具有指定鍵值對引數的 Map。

轉換的條件: - Keys 和 Values 必須有相同數量的元素 - keys 中的每個 key 必須是相同類型 - values 中的每個 value 必須是相同類型

onnx.helper.make_map_type_proto(key_type: int, value_type: TypeProto) TypeProto[原始碼]

製作 Map TypeProto。

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

建構 ModelProto

參數:
  • graph (GraphProto) – make_graph 的返回值

  • **kwargs – 任何要添加到返回實例的屬性

返回:

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[原始碼]

建構 NodeProto。

參數:
  • op_type (字串) – 要建構的運算子名稱

  • inputs (字串列表) – 輸入名稱列表

  • outputs (字串列表) – 輸出名稱列表

  • name (字串,預設為 None) – NodeProto 的可選唯一識別符

  • doc_string (字串,預設為 None) – NodeProto 的可選文件字串

  • domain (字串,預設為 None) – NodeProto 的可選網域。 如果為 None,我們將只使用預設網域(為空)

  • overload (字串,預設為 None) – 可選欄位,用於解析對模型局部函數的呼叫

  • **kwargs (字典) – 節點的屬性。可接受的值在 make_attribute() 中有說明。

返回:

NodeProto

onnx.helper.make_operatorsetid(domain: str, version: int) OperatorSetIdProto[原始碼]

建構 OperatorSetIdProto。

參數:
  • domain (字串) – 運算子集 ID 的網域

  • version (整數) – 運算子集 ID 的版本

返回:

OperatorSetIdProto

onnx.helper.make_opsetid(domain: str, version: int) OperatorSetIdProto[原始碼]

建構 OperatorSetIdProto。

參數:
  • domain (字串) – 運算子集 ID 的網域

  • version (整數) – 運算子集 ID 的版本

返回:

OperatorSetIdProto

onnx.helper.make_model_gen_version(graph: GraphProto, **kwargs: Any) ModelProto[原始碼]
onnx.helper.make_optional(name: str, elem_type: OptionalProto.DataType, value: google.protobuf.message.Message | None) OptionalProto[原始碼]

製作具有指定值引數的 Optional。

onnx.helper.make_optional_type_proto(inner_type_proto: TypeProto) TypeProto[原始碼]

製作 optional TypeProto。

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

製作具有指定值引數的 Sequence。

onnx.helper.make_sequence_type_proto(inner_type_proto: TypeProto) TypeProto[原始碼]

製作 sequence TypeProto。

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

建構 SparseTensorProto

參數:
  • values (TensorProto) – 值

  • indices (TensorProto) – 索引

  • dims – 形狀

返回:

SparseTensorProto

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

根據資料類型和形狀製作 SparseTensor TypeProto。

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[原始碼]

根據資料類型和形狀製作 SparseTensor ValueInfoProto。

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

使用指定的引數建立 TensorProto。如果 raw 為 False,此函式會根據 data_type 選擇對應的 proto 欄位來儲存值。如果 raw 為 True,則使用「raw_data」proto 欄位來儲存值,並且在此情況下,值應為位元組類型。

參數:
  • name (字串) – 張量名稱

  • data_type (int) – 一個值,例如 onnx.TensorProto.FLOAT

  • dims (List[int]) – 形狀

  • vals – 值

  • raw (bool) – 如果為 True,則 vals 包含張量的序列化內容,否則,vals 應為由 data_type 定義的類型的值的列表

返回:

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[原始碼]

根據資料類型和形狀製作 Sequence[Tensors] ValueInfoProto。

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

根據資料類型和形狀製作 Tensor TypeProto。

onnx.helper.make_training_info(algorithm: GraphProto, algorithm_bindings: List[Tuple[str, str]], initialization: GraphProto | None, initialization_bindings: List[Tuple[str, str]] | None) TrainingInfoProto[原始碼]
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[原始碼]

根據資料類型和形狀製作 ValueInfoProto。

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

製作具有給定 type_proto 的 ValueInfoProto。

類型映射

onnx.helper.get_all_tensor_dtypes() KeysView[int][原始碼]

從 TensorProto 取得所有張量類型。

返回:

來自 TensorProto 的所有張量類型

onnx.helper.np_dtype_to_tensor_dtype(np_dtype: dtype) int[原始碼]

將 numpy 的 dtype 轉換為對應的張量類型。它可以在將 numpy 陣列轉換為張量時使用。

參數:

np_dtype – numpy 的 data_type

返回:

TensorsProto 的 data_type

onnx.helper.tensor_dtype_to_field(tensor_dtype: int) str[原始碼]

將 TensorProto 的 data_type 轉換為對應的儲存欄位名稱。它可以在製作張量時使用。

參數:

tensor_dtype – TensorProto 的 data_type

返回:

欄位名稱

onnx.helper.tensor_dtype_to_np_dtype(tensor_dtype: int) dtype[原始碼]

將 TensorProto 的 data_type 轉換為對應的 numpy dtype。它可以在製作張量時使用。

參數:

tensor_dtype – TensorProto 的 data_type

返回:

numpy 的 data_type

onnx.helper.tensor_dtype_to_storage_tensor_dtype(tensor_dtype: int) int[原始碼]

將 TensorProto 的 data_type 轉換為對應的儲存 data_type。

參數:

tensor_dtype – TensorProto 的 data_type

返回:

儲存的 data_type

onnx.helper.tensor_dtype_to_string(tensor_dtype: int) str[原始碼]

取得給定 TensorProto 的 data_type 名稱。

參數:

tensor_dtype – TensorProto 的 data_type

返回:

data_type 的名稱

轉換

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

將 float32 值轉換為 float8,e4m3(為 int)。

請參考 以 8 位元儲存的浮點數 以取得技術細節。

參數:
  • fval – 要轉換的浮點數

  • scale – 縮放比例,在轉換前先將 fval 除以 scale

  • fn – 無限值無效

  • uz – 無負零

  • saturate – 如果為 True,任何超出範圍(包含無限值)的值都會變成最大值,否則會變成 NaN。Cast 運算子的說明完整描述了這些差異。

返回:

已轉換的浮點數

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

將 float32 值轉換為 float8,e5m2(為 int)。

參數:
  • fval – 要轉換的浮點數

  • scale – 縮放比例,在轉換前先將 fval 除以 scale

  • fn – 無限值無效

  • uz – 無負零

  • saturate – 如果為 True,任何超出範圍(包含無限值)的值都會變成最大值,否則會變成 NaN。Cast 運算子的說明完整描述了這些差異。

返回:

已轉換的浮點數