昇腾CANN/GE会话管理API指南

📅 2026/7/4 7:09:25
昇腾CANN/GE会话管理API指南
Session【免费下载链接】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.session import Session from ge.error import GeErrorFunctionality DescriptionSession class manages graph compilation and execution sessions. Supports synchronous execution (run_graph) and asynchronous execution (run_graph_with_stream_async). In asynchronous execution scenarios, custom memory allocator can be registered through register_external_allocator. Does not support copy and deep copy.Class Definitionclass Session: def __init__(self, options: Optional[dict] None) - None def add_graph(self, graph_id: int, graph: Graph, options: Optional[dict] None) - None def remove_graph(self, graph_id: int) - None def run_graph(self, graph_id: int, inputs: List[Tensor]) - List[Tensor] def run_graph_with_stream_async(self, graph_id: int, stream: int, inputs: List[Tensor]) - List[Tensor] def register_external_allocator(self, stream: int, allocator: Allocator) - None def unregister_external_allocator(self, stream: int) - NoneFunction Description__init__def __init__(self, options: Optional[dict] None) - NoneFunctionality Description: Creates session instance, can pass configuration dictionary for initialization. If no configuration passed, uses default configuration to create session.Parameter Description:ParameterTypeRequired/OptionalDescriptionoptionsOptional[dict]OptionalSession configuration dictionary, key-value pairs are string type. When not passed, uses default configuration to create session.Return Value Description: No return value.Constraint Description:options must be dict type or None, passing other types will throw TypeError.When session creation fails, will throw GeError, exception information contains GE internal error information and interface context.Session does not support copy and deep copy, attempting to copy will throw RuntimeError.add_graphdef add_graph(self, graph_id: int, graph: Graph, options: Optional[dict] None) - NoneFunctionality Description: Adds graph to session, supports passing additional compilation options.Parameter Description:ParameterTypeRequired/OptionalDescriptiongraph_idintRequiredUnique identifier of graph, used to distinguish different graphs in session.graphGraphRequiredGraph object to be added.optionsOptional[dict]OptionalGraph compilation configuration dictionary, key-value pairs are string type. When not passed, uses default configuration.Return Value Description: No return value.Constraint Description:graph_id must be int type, otherwise throws TypeError.graph must be Graph type, otherwise throws TypeError.options must be dict type or None, otherwise throws TypeError.When adding graph fails, will throw GeError, exception information contains GE internal error information and interface context.remove_graphdef remove_graph(self, graph_id: int) - NoneFunctionality Description: Removes specified graph from session.Parameter Description:ParameterTypeRequired/OptionalDescriptiongraph_idintRequiredUnique identifier of graph to be removed.Return Value Description: No return value.Constraint Description:graph_id must be int type, otherwise throws TypeError.Constraint Description:graph_id must be int type, otherwise throws TypeError.When graph removal fails, throws GeError, exception message includes GE internal error message and interface context.run_graphdef run_graph(self, graph_id: int, inputs: List[Tensor]) - List[Tensor]Functionality Description: Synchronously execute specified graph, pass input tensor list, return output tensor list.Parameter Description:Parameter NameTypeRequired/OptionalDescriptiongraph_idintRequiredUnique identifier of graph to execute.inputsList[Tensor]RequiredInput tensor list, all elements in list must be Tensor type.Return Value Description:Return Value TypeDescriptionList[Tensor]Output tensor list after graph execution.Constraint Description:graph_id must be int type, otherwise throws TypeError.All elements in inputs must be Tensor type, otherwise throws TypeError.When graph execution fails, throws GeError, exception message includes GE internal error message and interface context.run_graph_with_stream_asyncdef run_graph_with_stream_async(self, graph_id: int, stream: int, inputs: List[Tensor]) - List[Tensor]Functionality Description: Asynchronously execute graph on specified stream, pass input tensor list, return output tensor list. Output tensor memory allocation prioritizes using external allocator registered through register_external_allocator; if no external allocator registered, GE will automatically use built-in allocator.Parameter Description:Parameter NameTypeRequired/OptionalDescriptiongraph_idintRequiredUnique identifier of graph to execute.streamintRequiredStream address, used to specify stream for asynchronous execution.inputsList[Tensor]RequiredInput tensor list, all elements in list must be Tensor type.Return Value Description:Return Value TypeDescriptionList[Tensor]Output tensor list after graph execution.Constraint Description:graph_id must be int type, otherwise throws TypeError.stream must be int type, otherwise throws TypeError.inputs must be list type and all elements must be Tensor type, otherwise throws TypeError.If this stream has no registered external allocator and default allocator registration fails, throws GeError, exception message includes GE internal error message and interface context.When graph execution fails, throws GeError, exception message includes GE internal error message and interface context.register_external_allocatordef register_external_allocator(self, stream: int, allocator: Allocator) - NoneFunctionality Description: Register external memory allocator for specified stream, used to manage device memory allocation in asynchronous execution scenarios.Parameter Description:Parameter NameTypeRequired/OptionalDescriptionstreamintRequiredStream address.allocatorAllocatorRequiredExternal memory allocator instance, must be subclass instance of Allocator abstract base class.Return Value Description: No return value.Constraint Description:stream must be int type, otherwise throws TypeError.allocator must be Allocator instance, otherwise throws TypeError.When registration fails, throws GeError, exception message includes GE internal error message and interface context.Re-registering same stream will overwrite previous external allocator.unregister_external_allocatordef unregister_external_allocator(self, stream: int) - NoneFunctionality Description: Unregister external memory allocator registered on specified stream.Parameter Description:Parameter NameTypeRequired/OptionalDescriptionstreamintRequiredStream address.Return Value Description: No return value.Constraint Description:stream must be int type, otherwise throws TypeError.When unregistration fails, throws GeError, exception message includes GE internal error message and interface context.【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考