Coverage for /opt/cloud/slavespace/usr1/096471637100f3de0fcfc01072822a80/dttest/api/python/ge/ge/onnx_plugin/onnx_node.py: 90%
58 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-18 20:50 +0800
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-18 20:50 +0800
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# -----------------------------------------------------------------------------------------------------------
4# Copyright (c) 2026 Huawei Technologies Co., Ltd.
5# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6# CANN Open Software License Agreement Version 2.0 (the "License").
7# Please refer to the License for details. You may not use this file except in compliance with the License.
8# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10# See LICENSE in the root of the software repository for the full text of the License.
11# -----------------------------------------------------------------------------------------------------------
13"""Read-only ONNX node values exposed to Python parser callbacks."""
15from types import MappingProxyType
16from typing import Mapping, Sequence
18_ONNX_NODE_FACTORY_TOKEN = object()
21class OnnxNode:
22 """Flattened ONNX source node created by the parser bridge."""
24 __slots__ = ("_attrs", "_inputs", "_name", "_origin_type", "_outputs")
26 def __init__(
27 self,
28 *,
29 name=None,
30 origin_type=None,
31 inputs=None,
32 outputs=None,
33 attrs=None,
34 token=None,
35 ) -> None:
36 if token is not _ONNX_NODE_FACTORY_TOKEN:
37 raise RuntimeError("OnnxNode objects should not be created directly.")
38 if not isinstance(name, str):
39 raise TypeError("OnnxNode name must be a string")
40 if not isinstance(origin_type, str) or not origin_type:
41 raise TypeError("OnnxNode origin_type must be a non-empty string")
42 normalized_inputs = self._normalize_names(inputs, "inputs")
43 normalized_outputs = self._normalize_names(outputs, "outputs")
44 normalized_attrs = self._normalize_attrs(attrs)
46 object.__setattr__(self, "_name", name)
47 object.__setattr__(self, "_origin_type", origin_type)
48 object.__setattr__(self, "_inputs", normalized_inputs)
49 object.__setattr__(self, "_outputs", normalized_outputs)
50 object.__setattr__(self, "_attrs", MappingProxyType(normalized_attrs))
52 def __setattr__(self, name, value) -> None:
53 raise AttributeError("OnnxNode is read-only")
55 @staticmethod
56 def _normalize_names(values: Sequence[str], field_name: str) -> tuple:
57 if isinstance(values, (str, bytes)) or not isinstance(values, Sequence):
58 raise TypeError(f"OnnxNode {field_name} must be a sequence of strings")
59 if any(not isinstance(value, str) for value in values):
60 raise TypeError(f"OnnxNode {field_name} must contain only strings")
61 return tuple(values)
63 @staticmethod
64 def _normalize_attrs(attrs: Mapping[str, object]) -> dict:
65 if not isinstance(attrs, Mapping):
66 raise TypeError("OnnxNode attrs must be a mapping")
67 normalized = {}
68 for name, value in attrs.items():
69 if not isinstance(name, str) or not name:
70 raise TypeError("OnnxNode attribute name must be a non-empty string")
71 if type(value) not in (int, float):
72 raise TypeError("OnnxNode attrs only supports int and float values")
73 normalized[name] = value
74 return normalized
76 @property
77 def name(self) -> str:
78 return self._name
80 @property
81 def origin_type(self) -> str:
82 return self._origin_type
84 @property
85 def inputs(self) -> tuple:
86 return self._inputs
88 @property
89 def outputs(self) -> tuple:
90 return self._outputs
92 @property
93 def attrs(self) -> Mapping[str, object]:
94 return self._attrs
97def create_onnx_node(
98 *,
99 name: str,
100 origin_type: str,
101 inputs: Sequence[str],
102 outputs: Sequence[str],
103 attrs: Mapping[str, object],
104) -> OnnxNode:
105 """Create an OnnxNode for internal bridge use."""
107 return OnnxNode(
108 name=name,
109 origin_type=origin_type,
110 inputs=inputs,
111 outputs=outputs,
112 attrs=attrs,
113 token=_ONNX_NODE_FACTORY_TOKEN,
114 )