Coverage for /opt/cloud/slavespace/usr1/096471637100f3de0fcfc01072822a80/dttest/api/python/ge/ge/_capi/pysession_wrapper.py: 100%

52 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-27 10:02 +0800

1#!/usr/bin/env python3 

2# -*- coding: utf-8 -*- 

3# ------------------------------------------------------------------- 

4# ----------------------------------------------------------------------------------------------------------- 

5# Copyright (c) 2025 Huawei Technologies Co., Ltd. 

6# This program is free software, you can redistribute it and/or modify it under the terms and conditions of 

7# CANN Open Software License Agreement Version 2.0 (the "License"). 

8# Please refer to the License for details. You may not use this file except in compliance with the License. 

9# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 

10# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. 

11# See LICENSE in the root of the software repository for the full text of the License. 

12# ----------------------------------------------------------------------------------------------------------- 

13 

14import ctypes 

15import os 

16 

17from ._lib_loader import load_lib_from_path 

18 

19LIB_NAME = "libge_runtime_wrapper.so" 

20_dir = os.path.dirname(os.path.abspath(__file__)) 

21session_lib = load_lib_from_path(LIB_NAME, _dir) 

22 

23# 常用C类型别名 

24c_void_p = ctypes.c_void_p 

25# c_char_p 会自动转换为 Python 字符串 

26c_char_p = ctypes.c_char_p 

27c_int = ctypes.c_int 

28c_int32 = ctypes.c_int32 

29c_uint32 = ctypes.c_uint32 

30c_size_t = ctypes.c_size_t 

31c_p_to_char_p = ctypes.POINTER(ctypes.c_char_p) 

32c_p_to_void_p = ctypes.POINTER(ctypes.c_void_p) 

33 

34c_func_t_malloc = ctypes.CFUNCTYPE(c_void_p, c_void_p, c_size_t) 

35c_func_t_free = ctypes.CFUNCTYPE(None, c_void_p, c_void_p) 

36c_func_t_get_addr = ctypes.CFUNCTYPE(c_void_p, c_void_p) 

37c_func_t_on_destroy = ctypes.CFUNCTYPE(None, c_void_p) 

38 

39# ============ Session C API ============ 

40 

41session_lib.GeApiWrapper_Session_CreateSession.argtypes = [] 

42session_lib.GeApiWrapper_Session_CreateSession.restype = c_void_p 

43 

44session_lib.GeApiWrapper_Session_CreateSessionWithOptions.argtypes = [ 

45 c_p_to_char_p, 

46 c_p_to_char_p, 

47 c_int, 

48] 

49session_lib.GeApiWrapper_Session_CreateSessionWithOptions.restype = c_void_p 

50 

51session_lib.GeApiWrapper_Session_AddGraph.argtypes = [c_void_p, c_uint32, c_void_p] 

52session_lib.GeApiWrapper_Session_AddGraph.restype = c_uint32 

53 

54session_lib.GeApiWrapper_Session_AddGraphWithOptions.argtypes = [ 

55 c_void_p, 

56 c_uint32, 

57 c_void_p, 

58 c_p_to_char_p, 

59 c_p_to_char_p, 

60 c_int, 

61] 

62session_lib.GeApiWrapper_Session_AddGraphWithOptions.restype = c_uint32 

63 

64session_lib.GeApiWrapper_Session_RemoveGraph.argtypes = [c_void_p, c_uint32] 

65session_lib.GeApiWrapper_Session_RemoveGraph.restype = c_uint32 

66 

67session_lib.GeApiWrapper_Session_RunGraph.argtypes = [ 

68 c_void_p, 

69 c_uint32, 

70 c_p_to_void_p, 

71 c_int, 

72 ctypes.POINTER(ctypes.POINTER(c_void_p)), 

73 ctypes.POINTER(c_size_t), 

74] 

75session_lib.GeApiWrapper_Session_RunGraph.restype = c_uint32 

76 

77session_lib.GeApiWrapper_Session_RunGraphWithStreamAsync.argtypes = [ 

78 c_void_p, 

79 c_uint32, 

80 c_void_p, 

81 c_p_to_void_p, 

82 c_int, 

83 ctypes.POINTER(ctypes.POINTER(c_void_p)), 

84 ctypes.POINTER(c_size_t), 

85] 

86session_lib.GeApiWrapper_Session_RunGraphWithStreamAsync.restype = c_uint32 

87 

88session_lib.GeApiWrapper_Session_FreeTensorArray.argtypes = [c_p_to_void_p] 

89session_lib.GeApiWrapper_Session_FreeTensorArray.restype = None 

90 

91session_lib.GeApiWrapper_Session_DestroySession.argtypes = [c_void_p] 

92session_lib.GeApiWrapper_Session_DestroySession.restype = None 

93 

94session_lib.GeApiWrapper_Session_RegisterDefaultAllocator.argtypes = [ 

95 c_void_p, 

96 c_void_p, 

97] 

98session_lib.GeApiWrapper_Session_RegisterDefaultAllocator.restype = c_uint32 

99 

100session_lib.GeApiWrapper_Session_RegisterExternalAllocator.argtypes = [ 

101 c_void_p, 

102 c_void_p, 

103 c_func_t_malloc, 

104 c_func_t_free, 

105 c_func_t_get_addr, 

106 c_func_t_on_destroy, 

107 c_void_p, 

108] 

109session_lib.GeApiWrapper_Session_RegisterExternalAllocator.restype = c_uint32 

110 

111session_lib.GeApiWrapper_Session_UnregisterExternalAllocator.argtypes = [ 

112 c_void_p, 

113 c_void_p, 

114] 

115session_lib.GeApiWrapper_Session_UnregisterExternalAllocator.restype = c_uint32 

116 

117session_lib.GeApiWrapper_HasExternalAllocator.argtypes = [c_void_p] 

118session_lib.GeApiWrapper_HasExternalAllocator.restype = ctypes.c_bool 

119 

120session_lib.GeApiWrapper_HasDefaultAllocator.argtypes = [c_void_p] 

121session_lib.GeApiWrapper_HasDefaultAllocator.restype = ctypes.c_bool 

122 

123session_lib.GeApiWrapper_IsGEInitialized.argtypes = [] 

124session_lib.GeApiWrapper_IsGEInitialized.restype = ctypes.c_bool 

125 

126 

127def get_session_lib(): 

128 """Get the session wrapper library handle. 

129 

130 Returns: 

131 ctypes.CDLL: The loaded libge_runtime_wrapper.so library handle. 

132 """ 

133 return session_lib 

134 

135 

136def is_session_lib_loaded(): 

137 """Check if session library is loaded. 

138 

139 Returns: 

140 bool: True if library is loaded successfully. 

141 """ 

142 return session_lib is not None