Coverage for /opt/cloud/slavespace/usr1/096471637100f3de0fcfc01072822a80/dttest/api/python/ge/ge/_capi/pygraph_wrapper.py: 99%
215 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-27 10:03 +0800
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-27 10:03 +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# -----------------------------------------------------------------------------------------------------------
14import ctypes
15import os
17from ._lib_loader import load_lib_from_path
18from .pyes_graph_builder_wrapper import EsCTensorPtr
20LIB_NAME = "libgraph_wrapper.so"
21_dir = os.path.dirname(os.path.abspath(__file__))
22graph_lib = load_lib_from_path(LIB_NAME, _dir)
23if graph_lib is None:
24 raise RuntimeError(f"Failed to load library {LIB_NAME}")
26# 常用C类型别名
27c_void_p = ctypes.c_void_p
28# c_char_p 会自动转换为 Python 字符串
29c_char_p = ctypes.c_char_p
30c_char_ptr = ctypes.POINTER(ctypes.c_char)
31c_int = ctypes.c_int
32c_int32 = ctypes.c_int32
33c_int64 = ctypes.c_int64
34c_float = ctypes.c_float
35c_bool = ctypes.c_bool
36c_uint32 = ctypes.c_uint32
37c_size_t = ctypes.c_size_t
39# ============ AttrValue C API ============
40# 创建/销毁
41graph_lib.GeApiWrapper_AttrValue_Create.restype = c_void_p
42graph_lib.GeApiWrapper_AttrValue_Create.argtypes = []
44graph_lib.GeApiWrapper_AttrValue_Destroy.restype = None
45graph_lib.GeApiWrapper_AttrValue_Destroy.argtypes = [c_void_p]
47# 类型获取
48graph_lib.GeApiWrapper_AttrValue_GetValueType.restype = c_int32
49graph_lib.GeApiWrapper_AttrValue_GetValueType.argtypes = [c_void_p]
51# 标量值获取
52graph_lib.GeApiWrapper_AttrValue_GetString.restype = c_char_ptr
53graph_lib.GeApiWrapper_AttrValue_GetString.argtypes = [c_void_p]
55graph_lib.GeApiWrapper_AttrValue_GetFloat.restype = c_int
56graph_lib.GeApiWrapper_AttrValue_GetFloat.argtypes = [c_void_p, ctypes.POINTER(c_float)]
58graph_lib.GeApiWrapper_AttrValue_GetBool.restype = c_int
59graph_lib.GeApiWrapper_AttrValue_GetBool.argtypes = [c_void_p, ctypes.POINTER(c_bool)]
61graph_lib.GeApiWrapper_AttrValue_GetInt.restype = c_int
62graph_lib.GeApiWrapper_AttrValue_GetInt.argtypes = [c_void_p, ctypes.POINTER(c_int64)]
64graph_lib.GeApiWrapper_AttrValue_GetDataType.restype = c_int
65graph_lib.GeApiWrapper_AttrValue_GetDataType.argtypes = [
66 c_void_p,
67 ctypes.POINTER(c_int32),
68]
70graph_lib.GeApiWrapper_AttrValue_GetTensor.restype = EsCTensorPtr
71graph_lib.GeApiWrapper_AttrValue_GetTensor.argtypes = [c_void_p]
73# 列表值获取
74graph_lib.GeApiWrapper_AttrValue_GetListString.restype = ctypes.POINTER(c_char_p)
75graph_lib.GeApiWrapper_AttrValue_GetListString.argtypes = [
76 c_void_p,
77 ctypes.POINTER(c_int64),
78]
80graph_lib.GeApiWrapper_AttrValue_GetListFloat.restype = ctypes.POINTER(c_float)
81graph_lib.GeApiWrapper_AttrValue_GetListFloat.argtypes = [
82 c_void_p,
83 ctypes.POINTER(c_int64),
84]
86graph_lib.GeApiWrapper_AttrValue_GetListBool.restype = ctypes.POINTER(c_bool)
87graph_lib.GeApiWrapper_AttrValue_GetListBool.argtypes = [
88 c_void_p,
89 ctypes.POINTER(c_int64),
90]
92graph_lib.GeApiWrapper_AttrValue_GetListInt.restype = ctypes.POINTER(c_int64)
93graph_lib.GeApiWrapper_AttrValue_GetListInt.argtypes = [
94 c_void_p,
95 ctypes.POINTER(c_int64),
96]
98graph_lib.GeApiWrapper_AttrValue_GetListDataType.restype = ctypes.POINTER(c_int32)
99graph_lib.GeApiWrapper_AttrValue_GetListDataType.argtypes = [
100 c_void_p,
101 ctypes.POINTER(c_int64),
102]
104# 标量值设置
105graph_lib.GeApiWrapper_AttrValue_SetString.restype = c_int
106graph_lib.GeApiWrapper_AttrValue_SetString.argtypes = [c_void_p, c_char_p]
108graph_lib.GeApiWrapper_AttrValue_SetFloat.restype = c_int
109graph_lib.GeApiWrapper_AttrValue_SetFloat.argtypes = [c_void_p, c_float]
111graph_lib.GeApiWrapper_AttrValue_SetBool.restype = c_int
112graph_lib.GeApiWrapper_AttrValue_SetBool.argtypes = [c_void_p, c_bool]
114graph_lib.GeApiWrapper_AttrValue_SetInt.restype = c_int
115graph_lib.GeApiWrapper_AttrValue_SetInt.argtypes = [c_void_p, c_int64]
117graph_lib.GeApiWrapper_AttrValue_SetDataType.restype = c_int
118graph_lib.GeApiWrapper_AttrValue_SetDataType.argtypes = [c_void_p, c_int32]
120graph_lib.GeApiWrapper_AttrValue_SetTensor.restype = c_int
121graph_lib.GeApiWrapper_AttrValue_SetTensor.argtypes = [c_void_p, EsCTensorPtr]
123# 列表值设置
124graph_lib.GeApiWrapper_AttrValue_SetListString.restype = c_int
125graph_lib.GeApiWrapper_AttrValue_SetListString.argtypes = [
126 c_void_p,
127 ctypes.POINTER(c_char_p),
128 c_int64,
129]
131graph_lib.GeApiWrapper_AttrValue_SetListFloat.restype = c_int
132graph_lib.GeApiWrapper_AttrValue_SetListFloat.argtypes = [
133 c_void_p,
134 ctypes.POINTER(c_float),
135 c_int64,
136]
138graph_lib.GeApiWrapper_AttrValue_SetListBool.restype = c_int
139graph_lib.GeApiWrapper_AttrValue_SetListBool.argtypes = [
140 c_void_p,
141 ctypes.POINTER(c_bool),
142 c_int64,
143]
145graph_lib.GeApiWrapper_AttrValue_SetListInt.restype = c_int
146graph_lib.GeApiWrapper_AttrValue_SetListInt.argtypes = [
147 c_void_p,
148 ctypes.POINTER(c_int64),
149 c_int64,
150]
152graph_lib.GeApiWrapper_AttrValue_SetListDataType.restype = c_int
153graph_lib.GeApiWrapper_AttrValue_SetListDataType.argtypes = [
154 c_void_p,
155 ctypes.POINTER(c_int32),
156 c_int64,
157]
159# 内存释放函数
160graph_lib.GeApiWrapper_FreeString.restype = None
161graph_lib.GeApiWrapper_FreeString.argtypes = [c_char_ptr]
163graph_lib.GeApiWrapper_FreeListString.restype = None
164graph_lib.GeApiWrapper_FreeListString.argtypes = [ctypes.POINTER(c_char_p)]
166graph_lib.GeApiWrapper_FreeListFloat.restype = None
167graph_lib.GeApiWrapper_FreeListFloat.argtypes = [ctypes.POINTER(c_float)]
169graph_lib.GeApiWrapper_FreeListBool.restype = None
170graph_lib.GeApiWrapper_FreeListBool.argtypes = [ctypes.POINTER(c_bool)]
172graph_lib.GeApiWrapper_FreeListInt.restype = None
173graph_lib.GeApiWrapper_FreeListInt.argtypes = [ctypes.POINTER(c_int64)]
175graph_lib.GeApiWrapper_FreeListDataType.restype = None
176graph_lib.GeApiWrapper_FreeListDataType.argtypes = [ctypes.POINTER(c_int32)]
178# ============ Graph C API ============
179# 创建/销毁
180graph_lib.GeApiWrapper_Graph_CreateGraph.restype = c_void_p
181graph_lib.GeApiWrapper_Graph_CreateGraph.argtypes = [c_char_p]
183graph_lib.GeApiWrapper_Graph_DestroyGraph.restype = None
184graph_lib.GeApiWrapper_Graph_DestroyGraph.argtypes = [c_void_p]
186graph_lib.GeApiWrapper_Graph_FreeGraphArray.restype = None
187graph_lib.GeApiWrapper_Graph_FreeGraphArray.argtypes = [ctypes.POINTER(c_void_p)]
189# 属性操作
190graph_lib.GeApiWrapper_Graph_GetName.restype = c_char_ptr
191graph_lib.GeApiWrapper_Graph_GetName.argtypes = [c_void_p]
193graph_lib.GeApiWrapper_Graph_GetAllNodes.restype = ctypes.POINTER(c_void_p)
194graph_lib.GeApiWrapper_Graph_GetAllNodes.argtypes = [c_void_p, ctypes.POINTER(c_size_t)]
196graph_lib.GeApiWrapper_Graph_GetAttr.restype = c_int
197graph_lib.GeApiWrapper_Graph_GetAttr.argtypes = [c_void_p, c_char_p, c_void_p]
199graph_lib.GeApiWrapper_Graph_SetAttr.restype = c_int
200graph_lib.GeApiWrapper_Graph_SetAttr.argtypes = [c_void_p, c_char_p, c_void_p]
202graph_lib.GeApiWrapper_Graph_Dump_To_Onnx.restype = c_int
203graph_lib.GeApiWrapper_Graph_Dump_To_Onnx.argtypes = [c_void_p, c_char_p, c_char_p]
205graph_lib.GeApiWrapper_Graph_Dump_To_File.restype = c_int
206graph_lib.GeApiWrapper_Graph_Dump_To_File.argtypes = [c_void_p, c_int32, c_char_p]
208graph_lib.GeApiWrapper_Graph_Dump_To_Stream.restype = c_char_ptr
209graph_lib.GeApiWrapper_Graph_Dump_To_Stream.argtypes = [c_void_p, c_int32]
211graph_lib.GeApiWrapper_Graph_SaveToAir.restype = c_int
212graph_lib.GeApiWrapper_Graph_SaveToAir.argtypes = [c_void_p, c_char_p]
215graph_lib.GeApiWrapper_Graph_LoadFromAir.restype = c_int
216graph_lib.GeApiWrapper_Graph_LoadFromAir.argtypes = [c_void_p, c_char_ptr]
218graph_lib.GeApiWrapper_Graph_GetDirectNode.restype = ctypes.POINTER(c_void_p)
219graph_lib.GeApiWrapper_Graph_GetDirectNode.argtypes = [
220 c_void_p,
221 ctypes.POINTER(c_size_t),
222]
224graph_lib.GeApiWrapper_Graph_RemoveEdge.restype = c_int
225graph_lib.GeApiWrapper_Graph_RemoveEdge.argtypes = [
226 c_void_p,
227 c_void_p,
228 c_int32,
229 c_void_p,
230 c_int32,
231]
233graph_lib.GeApiWrapper_Graph_AddDataEdge.restype = c_int
234graph_lib.GeApiWrapper_Graph_AddDataEdge.argtypes = [
235 c_void_p,
236 c_void_p,
237 c_int32,
238 c_void_p,
239 c_int32,
240]
242graph_lib.GeApiWrapper_Graph_AddControlEdge.restype = c_int
243graph_lib.GeApiWrapper_Graph_AddControlEdge.argtypes = [c_void_p, c_void_p, c_void_p]
245graph_lib.GeApiWrapper_Graph_RemoveNode.restype = c_int
246graph_lib.GeApiWrapper_Graph_RemoveNode.argtypes = [c_void_p, c_void_p]
249graph_lib.GeApiWrapper_Graph_FindNodeByName.restype = int
250graph_lib.GeApiWrapper_Graph_FindNodeByName.argtypes = [
251 c_void_p,
252 c_char_p,
253 ctypes.POINTER(c_void_p),
254]
256# 子图相关 API
257graph_lib.GeApiWrapper_Graph_GetAllSubgraphs.restype = ctypes.POINTER(c_void_p)
258graph_lib.GeApiWrapper_Graph_GetAllSubgraphs.argtypes = [
259 c_void_p,
260 ctypes.POINTER(c_size_t),
261]
263graph_lib.GeApiWrapper_Graph_GetSubGraph.restype = c_void_p
264graph_lib.GeApiWrapper_Graph_GetSubGraph.argtypes = [c_void_p, c_char_p]
266graph_lib.GeApiWrapper_Graph_AddSubGraph.restype = c_int
267graph_lib.GeApiWrapper_Graph_AddSubGraph.argtypes = [c_void_p, c_void_p]
269graph_lib.GeApiWrapper_Graph_RemoveSubgraph.restype = c_int
270graph_lib.GeApiWrapper_Graph_RemoveSubgraph.argtypes = [c_void_p, c_char_p]
272# ============ GNode C API ============
273# 销毁
274graph_lib.GeApiWrapper_GNode_DestroyGNode.restype = None
275graph_lib.GeApiWrapper_GNode_DestroyGNode.argtypes = [c_void_p]
277graph_lib.GeApiWrapper_GNode_FreeGNodeArray.restype = None
278graph_lib.GeApiWrapper_GNode_FreeGNodeArray.argtypes = [ctypes.POINTER(c_void_p)]
280graph_lib.GeApiWrapper_GNode_FreeIntArray.restype = None
281graph_lib.GeApiWrapper_GNode_FreeIntArray.argtypes = [ctypes.POINTER(c_int32)]
283# 基本信息
284graph_lib.GeApiWrapper_GNode_GetName.restype = c_char_ptr
285graph_lib.GeApiWrapper_GNode_GetName.argtypes = [c_void_p]
287graph_lib.GeApiWrapper_GNode_GetType.restype = c_char_ptr
288graph_lib.GeApiWrapper_GNode_GetType.argtypes = [c_void_p]
290# 节点关系
291graph_lib.GeApiWrapper_GNode_GetInControlNodes.restype = ctypes.POINTER(c_void_p)
292graph_lib.GeApiWrapper_GNode_GetInControlNodes.argtypes = [
293 c_void_p,
294 ctypes.POINTER(c_size_t),
295]
297graph_lib.GeApiWrapper_GNode_GetInDataNodesAndPortIndexes.restype = c_int
298graph_lib.GeApiWrapper_GNode_GetInDataNodesAndPortIndexes.argtypes = [
299 c_void_p,
300 c_int32,
301 ctypes.POINTER(c_void_p),
302 ctypes.POINTER(c_int32),
303]
305graph_lib.GeApiWrapper_GNode_GetOutDataNodesAndPortIndexes.restype = c_int
306graph_lib.GeApiWrapper_GNode_GetOutDataNodesAndPortIndexes.argtypes = [
307 c_void_p,
308 c_int32,
309 ctypes.POINTER(ctypes.POINTER(c_void_p)),
310 ctypes.POINTER(ctypes.POINTER(c_int32)),
311 ctypes.POINTER(c_int),
312]
314graph_lib.GeApiWrapper_GNode_GetOutControlNodes.restype = ctypes.POINTER(c_void_p)
315graph_lib.GeApiWrapper_GNode_GetOutControlNodes.argtypes = [
316 c_void_p,
317 ctypes.POINTER(c_size_t),
318]
320graph_lib.GeApiWrapper_GNode_GetInputsSize.restype = c_size_t
321graph_lib.GeApiWrapper_GNode_GetInputsSize.argtypes = [c_void_p]
323graph_lib.GeApiWrapper_GNode_GetOutputsSize.restype = c_size_t
324graph_lib.GeApiWrapper_GNode_GetOutputsSize.argtypes = [c_void_p]
326graph_lib.GeApiWrapper_GNode_HasAttr.restype = c_bool
327graph_lib.GeApiWrapper_GNode_HasAttr.argtypes = [c_void_p, c_char_p]
329# 属性操作
330graph_lib.GeApiWrapper_GNode_GetAttr.restype = c_int
331graph_lib.GeApiWrapper_GNode_GetAttr.argtypes = [c_void_p, c_char_p, c_void_p]
333graph_lib.GeApiWrapper_GNode_SetAttr.restype = c_int
334graph_lib.GeApiWrapper_GNode_SetAttr.argtypes = [c_void_p, c_char_p, c_void_p]
336graph_lib.GeApiWrapper_GNode_GetInputAttr.restype = c_int
337graph_lib.GeApiWrapper_GNode_GetInputAttr.argtypes = [
338 c_void_p,
339 c_char_p,
340 c_uint32,
341 c_void_p,
342]
344graph_lib.GeApiWrapper_GNode_SetInputAttr.restype = c_int
345graph_lib.GeApiWrapper_GNode_SetInputAttr.argtypes = [
346 c_void_p,
347 c_char_p,
348 c_uint32,
349 c_void_p,
350]
352graph_lib.GeApiWrapper_GNode_GetOutputAttr.restype = c_int
353graph_lib.GeApiWrapper_GNode_GetOutputAttr.argtypes = [
354 c_void_p,
355 c_char_p,
356 c_uint32,
357 c_void_p,
358]
360graph_lib.GeApiWrapper_GNode_SetOutputAttr.restype = c_int
361graph_lib.GeApiWrapper_GNode_SetOutputAttr.argtypes = [
362 c_void_p,
363 c_char_p,
364 c_uint32,
365 c_void_p,
366]
368graph_lib.GeApiWrapper_GNode_GetInputDesc.restype = c_void_p
369graph_lib.GeApiWrapper_GNode_GetInputDesc.argtypes = [c_void_p, c_int32]
371graph_lib.GeApiWrapper_GNode_UpdateInputDesc.restype = c_int
372graph_lib.GeApiWrapper_GNode_UpdateInputDesc.argtypes = [c_void_p, c_int32, c_void_p]
374graph_lib.GeApiWrapper_GNode_GetOutputDesc.restype = c_void_p
375graph_lib.GeApiWrapper_GNode_GetOutputDesc.argtypes = [c_void_p, c_int32]
377graph_lib.GeApiWrapper_GNode_UpdateOutputDesc.restype = c_int
378graph_lib.GeApiWrapper_GNode_UpdateOutputDesc.argtypes = [c_void_p, c_int32, c_void_p]
381# ============ TensorDesc C API ============
382graph_lib.GeApiWrapper_TensorDesc_Create.restype = c_void_p
383graph_lib.GeApiWrapper_TensorDesc_Create.argtypes = [
384 ctypes.POINTER(c_int64),
385 c_size_t,
386 c_int,
387 c_int,
388]
390graph_lib.GeApiWrapper_TensorDesc_Destroy.restype = None
391graph_lib.GeApiWrapper_TensorDesc_Destroy.argtypes = [c_void_p]
393graph_lib.GeApiWrapper_TensorDesc_GetShape.restype = c_int
394graph_lib.GeApiWrapper_TensorDesc_GetShape.argtypes = [
395 c_void_p,
396 ctypes.POINTER(ctypes.POINTER(c_int64)),
397 ctypes.POINTER(c_size_t),
398]
400graph_lib.GeApiWrapper_TensorDesc_SetShape.restype = c_int
401graph_lib.GeApiWrapper_TensorDesc_SetShape.argtypes = [
402 c_void_p,
403 ctypes.POINTER(c_int64),
404 c_size_t,
405]
407graph_lib.GeApiWrapper_TensorDesc_GetOriginShape.restype = c_int
408graph_lib.GeApiWrapper_TensorDesc_GetOriginShape.argtypes = [
409 c_void_p,
410 ctypes.POINTER(ctypes.POINTER(c_int64)),
411 ctypes.POINTER(c_size_t),
412]
414graph_lib.GeApiWrapper_TensorDesc_SetOriginShape.restype = c_int
415graph_lib.GeApiWrapper_TensorDesc_SetOriginShape.argtypes = [
416 c_void_p,
417 ctypes.POINTER(c_int64),
418 c_size_t,
419]
421graph_lib.GeApiWrapper_TensorDesc_GetFormat.restype = c_int
422graph_lib.GeApiWrapper_TensorDesc_GetFormat.argtypes = [c_void_p]
424graph_lib.GeApiWrapper_TensorDesc_SetFormat.restype = c_int
425graph_lib.GeApiWrapper_TensorDesc_SetFormat.argtypes = [c_void_p, c_int]
427graph_lib.GeApiWrapper_TensorDesc_GetOriginFormat.restype = c_int
428graph_lib.GeApiWrapper_TensorDesc_GetOriginFormat.argtypes = [c_void_p]
430graph_lib.GeApiWrapper_TensorDesc_SetOriginFormat.restype = c_int
431graph_lib.GeApiWrapper_TensorDesc_SetOriginFormat.argtypes = [c_void_p, c_int]
433graph_lib.GeApiWrapper_TensorDesc_GetDataType.restype = c_int
434graph_lib.GeApiWrapper_TensorDesc_GetDataType.argtypes = [c_void_p]
436graph_lib.GeApiWrapper_TensorDesc_SetDataType.restype = c_int
437graph_lib.GeApiWrapper_TensorDesc_SetDataType.argtypes = [c_void_p, c_int]
439# ============ Tensor C API ============
440graph_lib.GeApiWrapper_Tensor_SetFormat.restype = c_int
441graph_lib.GeApiWrapper_Tensor_SetFormat.argtypes = [c_void_p, ctypes.POINTER(c_int)]
443graph_lib.GeApiWrapper_Tensor_GetFormat.restype = c_int
444graph_lib.GeApiWrapper_Tensor_GetFormat.argtypes = [c_void_p]
446graph_lib.GeApiWrapper_Tensor_SetDataType.restype = c_int
447graph_lib.GeApiWrapper_Tensor_SetDataType.argtypes = [c_void_p, ctypes.POINTER(c_int)]
449graph_lib.GeApiWrapper_Tensor_GetDataType.restype = c_int
450graph_lib.GeApiWrapper_Tensor_GetDataType.argtypes = [c_void_p]
452graph_lib.GeApiWrapper_Tensor_GetTensorDesc.restype = c_void_p
453graph_lib.GeApiWrapper_Tensor_GetTensorDesc.argtypes = [c_void_p]
455graph_lib.GeApiWrapper_Tensor_CreateTensor.restype = EsCTensorPtr
456graph_lib.GeApiWrapper_Tensor_CreateTensor.argtypes = []
458graph_lib.GeApiWrapper_Tensor_DestroyEsCTensor.restype = None
459graph_lib.GeApiWrapper_Tensor_DestroyEsCTensor.argtypes = [c_void_p]
461graph_lib.GeApiWrapper_Tensor_GetShape.restype = c_int
462graph_lib.GeApiWrapper_Tensor_GetShape.argtypes = [
463 c_void_p,
464 ctypes.POINTER(ctypes.POINTER(c_int64)),
465 ctypes.POINTER(c_size_t),
466]
468graph_lib.GeApiWrapper_Tensor_FreeDimsArray.restype = None
469graph_lib.GeApiWrapper_Tensor_FreeDimsArray.argtypes = [ctypes.POINTER(c_int64)]
471graph_lib.GeApiWrapper_Tensor_GetData.restype = c_char_ptr
472graph_lib.GeApiWrapper_Tensor_GetData.argtypes = [c_void_p]
474graph_lib.GeApiWrapper_Tensor_GetPlacement.restype = c_int32
475graph_lib.GeApiWrapper_Tensor_GetPlacement.argtypes = [c_void_p]