CANN/GE Python Graph API指南
2026/7/4 8:38:12 网站建设 项目流程

Graph

【免费下载链接】geGE(Graph Engine)是面向昇腾的图编译器和执行器,提供了计算图优化、多流并行、内存复用和模型下沉等技术手段,加速模型执行效率,减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力,并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge

Product Support Status

ProductSupport Status
Atlas A3 Training Series Products/Atlas A3 Inference Series Products
Atlas A2 Training Series Products/Atlas A2 Inference Series Products

Module Import

from ge.graph import Graph

Functionality Description

Graph class is the core graph operation class in GE Python interface, used to manage computation graph construction, query and modification. Mainly provides the following capabilities:

  • Graph Lifecycle Management: Create, destroy computation graphs, supports graph serialization and deserialization.
  • Node Query: Get all nodes or direct nodes in graph, find nodes by name.
  • Attribute Management: Get and set graph-level custom attributes.
  • Edge Operations: Add/remove data edges and control edges, build data dependencies and control dependencies between nodes.
  • Subgraph Management: Add, query, delete subgraphs, supports hierarchical graph structure.
  • Serialization and Persistence: Export graph to file (dump_to_file) or string (dump_to_stream), supports saving and loading AIR format models.

Function Prototypes

__init__

def __init__(self, name: Optional[str] = "graph") -> None

Creates a Graph object.

name(property)

@property def name(self) -> str

Gets graph name.

get_all_nodes

def get_all_nodes(self) -> List[Node]

Gets all nodes in graph, including nodes in subgraphs.

get_direct_nodes

def get_direct_nodes(self) -> List[Node]

Gets direct nodes in current graph, not including nodes in subgraphs.

get_attr

def get_attr(self, key: str) -> Any

Gets specified attribute value of graph.

set_attr

def set_attr(self, key: str, value: Any) -> None

Sets specified attribute of graph.

dump_to_file

def dump_to_file(self, format: DumpFormat = DumpFormat.kReadable, suffix: str = "") -> None

Exports graph to file.

dump_to_stream

def dump_to_stream(self, format: DumpFormat = DumpFormat.kReadable) -> str

Exports graph to string.

save_to_air

def save_to_air(self, file_path: str) -> None

Saves graph as AIR format file.###load_from_air

def load_from_air(self, file_path: str) -> None

Loads graph from AIR format file.

remove_node

def remove_node(self, node: Node) -> None

Removes specified node from graph.

remove_edge

def remove_edge(self, src_node: Node, src_port_index: int, dst_node: Node, dst_port_index: int) -> None

Removes specified edge.

add_data_edge

def add_data_edge(self, src_node: Node, src_port_index: int, dst_node: Node, dst_port_index: int) -> None

Adds data edge.

add_control_edge

def add_control_edge(self, src_node: Node, dst_node: Node) -> None

Adds control edge.

find_node_by_name

def find_node_by_name(self, name: str) -> Node

Finds node by node name.

get_all_subgraphs

def get_all_subgraphs(self) -> List[Graph]

Gets all subgraphs in graph.

get_subgraph

def get_subgraph(self, name: str) -> Optional[Graph]

Gets specified subgraph by name.

add_subgraph

def add_subgraph(self, subgraph: Graph) -> None

Adds subgraph to graph.

remove_subgraph

def remove_subgraph(self, name: str) -> None

Removes subgraph by name.

Parameter Description

__init__

Parameter NameTypeRequiredDefault ValueDescription
nameOptional[str]No"graph"Graph name, must be string type.

name

No parameters (read-only property).

get_all_nodes

No parameters.

get_direct_nodes

No parameters.

get_attr

Parameter NameTypeRequiredDefault ValueDescription
keystrYes-Attribute name, must be string type.

set_attr

Parameter NameTypeRequiredDefault ValueDescription
keystrYes-Attribute name, must be string type.
valueAnyYes-Attribute value, supports multiple data types.

dump_to_file

Parameter NameTypeRequiredDefault ValueDescription
formatDumpFormatNoDumpFormat.kReadableExport file format, valid values: DumpFormat.kOnnx、DumpFormat.kTxt、DumpFormat.kReadable.
suffixstrNo""Filename suffix, appended to end of generated filename. For example, when suffix is "xxxx", filename format isge_<format>_00000_<graph_name>_0_xxxx.<ext>.

DumpFormat enum value description:

Enum ValueNumeric ValueDescription
DumpFormat.kOnnx0ONNX text format (pbtxt), contains only graph structure, no weight data or other attributes.
DumpFormat.kTxt1Text format.
DumpFormat.kReadable2Readable format (default).

dump_to_stream

Parameter NameTypeRequiredDefault ValueDescription
formatDumpFormatNoDumpFormat.kReadableExport string format, valid values: DumpFormat.kOnnx、DumpFormat.kTxt、DumpFormat.kReadable.

save_to_air

Parameter NameTypeRequiredDefault ValueDescription
file_pathstrYes-AIR file save path, must be string type.

load_from_air

Parameter NameTypeRequiredDefault ValueDescription
file_pathstrYes-AIR file load path, must be string type.

remove_node

Parameter NameTypeRequiredDefault ValueDescription
nodeNodeYes-Node object to be removed, must be Node type.

remove_edge

Parameter NameTypeRequiredDefault ValueDescription
src_nodeNodeYes-Edge source node, must be Node type.
src_port_indexintYes-Source node output port index. When removing control edge, should be set to -1.
dst_nodeNodeYes-Edge target node, must be Node type.
dst_port_indexintYes-Target node input port index. When removing control edge, should be set to -1.

add_data_edge

Parameter NameTypeRequiredDefault ValueDescription
src_nodeNodeYes-Data edge source node, must be Node type.
src_port_indexintYes-Source node output port index, must be integer.
dst_nodeNodeYes-Data edge target node, must be Node type.
dst_port_indexintYes-Target node input port index, must be integer.

add_control_edge

Parameter NameTypeRequiredDefault ValueDescription
src_nodeNodeYes-Control edge source node, must be Node type.
dst_nodeNodeYes-Control edge target node, must be Node type.

find_node_by_name

Parameter NameTypeRequiredDefault ValueDescription
namestrYes-Node name, must be string type.

get_all_subgraphs

No parameters.

get_subgraph

Parameter NameTypeRequiredDefault ValueDescription
namestrYes-Subgraph name, must be string type.

add_subgraph

Parameter NameTypeRequiredDefault ValueDescription
subgraphGraphYes-Subgraph object to be added, must be Graph type.###remove_subgraph
Parameter NameTypeRequiredDefault ValueDescription
namestrYes-Subgraph name to be removed, must be string type.

Return Value Description

MethodReturn TypeDescription
__init__NoneNo return value. Returns Graph object on successful creation; throws exception on failure.
namestrReturns graph name string.
get_all_nodesList[Node]Returns list of all nodes in graph (including nodes in subgraphs). Returns empty list if graph is empty.
get_direct_nodesList[Node]Returns list of direct nodes in current graph (excluding nodes in subgraphs). Returns empty list if graph is empty.
get_attrAnyReturns attribute value corresponding to specified attribute name.
set_attrNoneNo return value. Throws exception on failure.
dump_to_fileNoneNo return value. Throws exception on export failure.
dump_to_streamstrReturns string representation of graph.
save_to_airNoneNo return value. Throws exception on save failure.
load_from_airNoneNo return value. Throws exception on load failure.
remove_nodeNoneNo return value. Throws exception on removal failure.
remove_edgeNoneNo return value. Throws exception on removal failure.
add_data_edgeNoneNo return value. Throws exception on addition failure.
add_control_edgeNoneNo return value. Throws exception on addition failure.
find_node_by_nameNodeReturns found node object. Throws exception if not found.
get_all_subgraphsList[Graph]Returns list of all subgraphs. Returns empty list if no subgraphs exist.
get_subgraphOptional[Graph]Returns subgraph object with specified name. Returns None if not found.
add_subgraphNoneNo return value. Throws exception on addition failure.
remove_subgraphNoneNo return value. Throws exception on removal failure.

Constraint Description

  • Ownership Model: Graph objects have two ownership states. By default, Python side manages C++ resource lifecycle. When Graph is passed as subgraph parameter to operator (like If、While、Case), ownership automatically transfers to C++ side to avoid double free issues.
  • No Copy: Graph class does not support copy operations (both shallow copy and deep copy are not supported), callingcopyordeepcopywill throw RuntimeError.
  • Subgraph Name Uniqueness: When callingadd_subgraphto add subgraph, subgraph name must be unique in parent graph. If name already exists, operation will fail and throw exception.
  • Edge Port Index: When callingremove_edgeto remove control edge, bothsrc_port_indexanddst_port_indexshould be set to -1. When removing data edge, port index must match the actual connected port.
  • dump_to_file Output Limitation: When exporting with DumpFormat.kOnnx format, pbtxt file only contains graph structure information, no weight data or other attributes.
  • Type Validation: All method parameters undergo type validation, throws TypeError on type mismatch; throws RuntimeError on operation failure.
  • Node Lookup:find_node_by_namethrows RuntimeError when specified name node is not found, instead of returning None. Need to confirm node exists before use.

【免费下载链接】geGE(Graph Engine)是面向昇腾的图编译器和执行器,提供了计算图优化、多流并行、内存复用和模型下沉等技术手段,加速模型执行效率,减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力,并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询