函数

class onnx_ir.Function(domain, name, overload='', *, graph, attributes)

IR 函数。

像图一样,函数可以有非拓扑排序的节点。用户有责任维护节点的拓扑顺序。

请注意,Function 中没有 node 属性。Function 可以被视为节点序列,并且应该这样使用。例如,要将所有节点作为列表获取,请调用 list(function)

参数:
name

函数名称。

domain

定义此函数的域。

overload

当函数被重载时,重载名称。

inputs

函数的输入值。

attributes

此函数定义的属性。

outputs

函数的输出值。

opset_imports

函数导入的操作集。

doc_string

文档字符串。

meta

用于图转换传递的元数据存储。

metadata_props

将序列化到 ONNX 文件中的元数据。

all_nodes()[source]

在 O(#节点 + #属性) 时间内获取图中及其子图中的所有节点。

这是 onnx_ir.traversal.RecursiveGraphIterator(graph) 的别名。考虑使用 onnx_ir.traversal.RecursiveGraphIterator 进行更高级的节点遍历。

0.1.2 版本新增。

返回类型:

Iterator[Node]

append(node, /)[source]

在 O(1) 时间内将节点附加到函数。

参数:

node (Node)

返回类型:

property attributes: Attributes
count(value) integer -- 返回 value 出现的次数
display(*, page=False)

漂亮地打印对象。

参数:

page (bool) – 是否分页输出。

返回类型:

property doc_string: str | None
property domain: str
extend(nodes, /)[source]

在 O(#新节点) 时间内用给定节点扩展函数。

参数:

nodes (Iterable[Node])

返回类型:

property graph: Graph

包含此函数节点的底层 Graph 对象。

仅将此图用于身份比较

if value.graph is function.graph:
    # Do something with the value that belongs to this function

否则,直接使用 Function 对象访问节点和其他属性。

添加于 0.1.7 版本。

identifier()[source]

返回函数的唯一标识符。

返回类型:

tuple[str, str, str]

index(value[, start[, stop]]) 整数 -- 返回 value 的第一个索引。

如果值不存在,则引发 ValueError。

支持 start 和 stop 参数是可选的,但建议使用。

property inputs: MutableSequence[Value]
insert_after(node, new_nodes, /)[source]

在 O(#新节点) 时间内在给定节点后插入新节点。

参数:
返回类型:

insert_before(node, new_nodes, /)[source]

在 O(#新节点) 时间内在给定节点前插入新节点。

参数:
返回类型:

property meta: MetadataStore

用于中间分析的元数据存储。

如果您希望将元数据序列化为 ONNX proto,请写入 metadata_props

property metadata_props: dict[str, str]

函数的元数据属性。

元数据属性用于存储有关函数的附加信息。与 meta 不同,此属性被序列化为 ONNX proto。

property name: str
property opset_imports: dict[str, int]
property outputs: MutableSequence[Value]
property overload: str
remove(nodes, /, safe=False)[source]

在 O(#节点数) 时间内从图中移除节点。

如果发生任何错误,为确保图不会处于不一致状态,图不会被修改。

参数:
  • nodes (Node | Iterable[Node]) – 要移除的节点。

  • safe (bool) –

    如果为 True,则在移除前执行以下操作

    1. 它检查以确保要移除的节点没有未被移除的用户。 2. 它检查节点不会导致任何图输出。 3. 它移除对所有输入的引用,因此它不再是其他节点的用户。

引发:
  • ValueError – 如果要移除的任何节点不属于此图。

  • ValueError – (当 safe=True 时) 如果节点不属于此图或存在节点的用户。

  • ValueError – (当 safe=True 时) 如果节点仍被其他未被移除的节点使用。

返回类型:

sort()[source]

在 O(#节点 + #值) 时间内对此图和所有子图执行拓扑排序。

返回类型:

subgraphs()[source]

在 O(#节点 + #属性) 时间内获取函数中的所有子图。

0.1.2 版本新增。

返回类型:

Iterator[Graph]