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

38 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) 2026 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 = "liboffline_compile_wrapper.so" 

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

21offline_compile_lib = load_lib_from_path(LIB_NAME, _dir) 

22 

23# 常用C类型别名 

24c_void_p = ctypes.c_void_p 

25c_char_p = ctypes.c_char_p 

26c_int = ctypes.c_int 

27c_uint32 = ctypes.c_uint32 

28c_uint64 = ctypes.c_uint64 

29c_p_to_char_p = ctypes.POINTER(c_char_p) 

30c_pp_to_char_p = ctypes.POINTER(c_p_to_char_p) 

31c_p_to_void_p = ctypes.POINTER(c_void_p) 

32c_p_to_int = ctypes.POINTER(c_int) 

33 

34 

35class ModelBufferData(ctypes.Structure): 

36 """C层 struct ModelBufferData""" 

37 

38 pass 

39 

40 

41# 指针类型定义 

42ModelBufferDataPtr = ctypes.POINTER(ModelBufferData) 

43 

44# ============ Offline compile C API ============ 

45 

46offline_compile_lib.GeApiWrapper_OfflineCompile_BuildInitialize.argtypes = [ 

47 c_p_to_char_p, 

48 c_p_to_char_p, 

49 c_int, 

50] 

51offline_compile_lib.GeApiWrapper_OfflineCompile_BuildInitialize.restype = c_uint32 

52 

53offline_compile_lib.GeApiWrapper_OfflineCompile_BuildFinalize.argtypes = [] 

54offline_compile_lib.GeApiWrapper_OfflineCompile_BuildFinalize.restype = None 

55 

56offline_compile_lib.GeApiWrapper_OfflineCompile_BuildModel.argtypes = [ 

57 c_void_p, 

58 c_p_to_char_p, 

59 c_p_to_char_p, 

60 c_int, 

61 ctypes.POINTER(ModelBufferDataPtr), 

62] 

63offline_compile_lib.GeApiWrapper_OfflineCompile_BuildModel.restype = c_uint32 

64 

65offline_compile_lib.GeApiWrapper_OfflineCompile_SaveModel.argtypes = [ 

66 c_char_p, 

67 ModelBufferDataPtr, 

68] 

69offline_compile_lib.GeApiWrapper_OfflineCompile_SaveModel.restype = c_uint32 

70 

71offline_compile_lib.GeApiWrapper_OfflineCompile_BundleBuildModel.argtypes = [ 

72 c_p_to_void_p, 

73 c_pp_to_char_p, 

74 c_pp_to_char_p, 

75 c_p_to_int, 

76 c_int, 

77 ctypes.POINTER(ModelBufferDataPtr), 

78] 

79offline_compile_lib.GeApiWrapper_OfflineCompile_BundleBuildModel.restype = c_uint32 

80 

81offline_compile_lib.GeApiWrapper_OfflineCompile_BundleSaveModel.argtypes = [ 

82 c_char_p, 

83 ModelBufferDataPtr, 

84] 

85offline_compile_lib.GeApiWrapper_OfflineCompile_BundleSaveModel.restype = c_uint32 

86 

87offline_compile_lib.GeApiWrapper_ModelBuffer_Destroy.argtypes = [ModelBufferDataPtr] 

88offline_compile_lib.GeApiWrapper_ModelBuffer_Destroy.restype = None 

89 

90offline_compile_lib.GeApiWrapper_ModelBuffer_GetLength.argtypes = [ModelBufferDataPtr] 

91offline_compile_lib.GeApiWrapper_ModelBuffer_GetLength.restype = c_uint64 

92 

93 

94def get_offline_compile_lib(): 

95 """Get the offline compile wrapper library handle. 

96 

97 Returns: 

98 ctypes.CDLL: The loaded liboffline_compile_wrapper.so library handle. 

99 """ 

100 return offline_compile_lib 

101 

102 

103def is_offline_compile_lib_loaded(): 

104 """Check if the offline compile library is loaded. 

105 

106 Returns: 

107 bool: True if library is loaded successfully. 

108 """ 

109 return offline_compile_lib is not None