CANN/GE Python Graph API指南
Graph【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/geProduct Support StatusProductSupport StatusAtlas A3 Training Series Products/Atlas A3 Inference Series Products√Atlas A2 Training Series Products/Atlas A2 Inference Series Products√Module Importfrom ge.graph import GraphFunctionality DescriptionGraph 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) - NoneCreates a Graph object.name(property)property def name(self) - strGets graph name.get_all_nodesdef get_all_nodes(self) - List[Node]Gets all nodes in graph, including nodes in subgraphs.get_direct_nodesdef get_direct_nodes(self) - List[Node]Gets direct nodes in current graph, not including nodes in subgraphs.get_attrdef get_attr(self, key: str) - AnyGets specified attribute value of graph.set_attrdef set_attr(self, key: str, value: Any) - NoneSets specified attribute of graph.dump_to_filedef dump_to_file(self, format: DumpFormat DumpFormat.kReadable, suffix: str ) - NoneExports graph to file.dump_to_streamdef dump_to_stream(self, format: DumpFormat DumpFormat.kReadable) - strExports graph to string.save_to_airdef save_to_air(self, file_path: str) - NoneSaves graph as AIR format file.###load_from_airdef load_from_air(self, file_path: str) - NoneLoads graph from AIR format file.remove_nodedef remove_node(self, node: Node) - NoneRemoves specified node from graph.remove_edgedef remove_edge(self, src_node: Node, src_port_index: int, dst_node: Node, dst_port_index: int) - NoneRemoves specified edge.add_data_edgedef add_data_edge(self, src_node: Node, src_port_index: int, dst_node: Node, dst_port_index: int) - NoneAdds data edge.add_control_edgedef add_control_edge(self, src_node: Node, dst_node: Node) - NoneAdds control edge.find_node_by_namedef find_node_by_name(self, name: str) - NodeFinds node by node name.get_all_subgraphsdef get_all_subgraphs(self) - List[Graph]Gets all subgraphs in graph.get_subgraphdef get_subgraph(self, name: str) - Optional[Graph]Gets specified subgraph by name.add_subgraphdef add_subgraph(self, subgraph: Graph) - NoneAdds subgraph to graph.remove_subgraphdef remove_subgraph(self, name: str) - NoneRemoves subgraph by name.Parameter Description__init__Parameter NameTypeRequiredDefault ValueDescriptionnameOptional[str]NographGraph name, must be string type.nameNo parameters (read-only property).get_all_nodesNo parameters.get_direct_nodesNo parameters.get_attrParameter NameTypeRequiredDefault ValueDescriptionkeystrYes-Attribute name, must be string type.set_attrParameter NameTypeRequiredDefault ValueDescriptionkeystrYes-Attribute name, must be string type.valueAnyYes-Attribute value, supports multiple data types.dump_to_fileParameter NameTypeRequiredDefault ValueDescriptionformatDumpFormatNoDumpFormat.kReadableExport file format, valid values: DumpFormat.kOnnx、DumpFormat.kTxt、DumpFormat.kReadable.suffixstrNoFilename 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 ValueDescriptionDumpFormat.kOnnx0ONNX text format (pbtxt), contains only graph structure, no weight data or other attributes.DumpFormat.kTxt1Text format.DumpFormat.kReadable2Readable format (default).dump_to_streamParameter NameTypeRequiredDefault ValueDescriptionformatDumpFormatNoDumpFormat.kReadableExport string format, valid values: DumpFormat.kOnnx、DumpFormat.kTxt、DumpFormat.kReadable.save_to_airParameter NameTypeRequiredDefault ValueDescriptionfile_pathstrYes-AIR file save path, must be string type.load_from_airParameter NameTypeRequiredDefault ValueDescriptionfile_pathstrYes-AIR file load path, must be string type.remove_nodeParameter NameTypeRequiredDefault ValueDescriptionnodeNodeYes-Node object to be removed, must be Node type.remove_edgeParameter NameTypeRequiredDefault ValueDescriptionsrc_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_edgeParameter NameTypeRequiredDefault ValueDescriptionsrc_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_edgeParameter NameTypeRequiredDefault ValueDescriptionsrc_nodeNodeYes-Control edge source node, must be Node type.dst_nodeNodeYes-Control edge target node, must be Node type.find_node_by_nameParameter NameTypeRequiredDefault ValueDescriptionnamestrYes-Node name, must be string type.get_all_subgraphsNo parameters.get_subgraphParameter NameTypeRequiredDefault ValueDescriptionnamestrYes-Subgraph name, must be string type.add_subgraphParameter NameTypeRequiredDefault ValueDescriptionsubgraphGraphYes-Subgraph object to be added, must be Graph type.###remove_subgraphParameter NameTypeRequiredDefault ValueDescriptionnamestrYes-Subgraph name to be removed, must be string type.Return Value DescriptionMethodReturn 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 DescriptionOwnership 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.【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考