|
28 | 28 |
|
29 | 29 | import numpy as np |
30 | 30 | import onnx.parser as oprs |
| 31 | +import pytest |
31 | 32 |
|
32 | 33 | from qonnx.core.modelwrapper import ModelWrapper |
33 | 34 | from qonnx.custom_op.base import CustomOp |
34 | 35 | from qonnx.custom_op.registry import add_op_to_domain, getCustomOp |
| 36 | +from onnx import helper |
35 | 37 |
|
36 | 38 |
|
37 | 39 | class AttrTestOp(CustomOp): |
38 | 40 | def get_nodeattr_types(self): |
39 | | - my_attrs = {"tensor_attr": ("t", True, np.asarray([])), "strings_attr": ("strings", True, [""])} |
| 41 | + my_attrs = { |
| 42 | + "tensor_attr": ("t", True, np.asarray([])), |
| 43 | + "strings_attr": ("strings", True, [""]), |
| 44 | + } |
40 | 45 | return my_attrs |
41 | 46 |
|
42 | 47 | def make_shape_compatible_op(self, model): |
@@ -101,3 +106,178 @@ def test_attr(): |
101 | 106 | strings_attr_prod[0] = "test" |
102 | 107 | inst.set_nodeattr("strings_attr", strings_attr_prod) |
103 | 108 | assert inst.get_nodeattr("strings_attr") == ["test"] + strings_attr[1:] |
| 109 | + |
| 110 | + |
| 111 | +class MyCustomOp(CustomOp): |
| 112 | + def __init__(self, onnx_node, onnx_opset_version=10): |
| 113 | + super().__init__(onnx_node, onnx_opset_version) |
| 114 | + |
| 115 | + def get_nodeattr_types(self): |
| 116 | + return { |
| 117 | + "my_int_attr": ("i", False, -1), |
| 118 | + "my_float_attr": ("f", False, 0.0), |
| 119 | + "my_string_attr": ("s", False, "default"), |
| 120 | + "my_ints_attr": ("ints", False, []), |
| 121 | + "my_floats_attr": ("floats", False, []), |
| 122 | + "my_strings_attr": ("strings", False, []), |
| 123 | + "my_allowed_attr": ("i", False, 1, {1, 2, 3}), |
| 124 | + "my_tensor_attr": ("t", False, np.array([])), |
| 125 | + } |
| 126 | + |
| 127 | + def execute_node(self, context, graph): |
| 128 | + pass |
| 129 | + |
| 130 | + def infer_node_datatype(self, model): |
| 131 | + pass |
| 132 | + |
| 133 | + def make_shape_compatible_op(self, model): |
| 134 | + pass |
| 135 | + |
| 136 | + def verify_node(self): |
| 137 | + pass |
| 138 | + |
| 139 | + |
| 140 | +def test_set_get_nodeattr(): |
| 141 | + node = helper.make_node("myOpType", [], []) |
| 142 | + myCustomOp = MyCustomOp(node, 13) |
| 143 | + |
| 144 | + # Test integer attribute |
| 145 | + assert myCustomOp.get_nodeattr("my_int_attr") == -1 |
| 146 | + myCustomOp.set_nodeattr("my_int_attr", 2) |
| 147 | + assert myCustomOp.get_nodeattr("my_int_attr") == 2 |
| 148 | + |
| 149 | + # Test that setting wrong type raises TypeError |
| 150 | + with pytest.raises(TypeError, match="expects int"): |
| 151 | + myCustomOp.set_nodeattr("my_int_attr", 2.5) |
| 152 | + with pytest.raises(TypeError, match="expects int"): |
| 153 | + myCustomOp.set_nodeattr("my_int_attr", "string") |
| 154 | + |
| 155 | + # Test float attribute |
| 156 | + assert myCustomOp.get_nodeattr("my_float_attr") == 0.0 |
| 157 | + myCustomOp.set_nodeattr("my_float_attr", 3.14) |
| 158 | + assert abs(myCustomOp.get_nodeattr("my_float_attr") - 3.14) < 1e-6 |
| 159 | + |
| 160 | + with pytest.raises(TypeError, match="expects float"): |
| 161 | + myCustomOp.set_nodeattr("my_float_attr", 42) |
| 162 | + with pytest.raises(TypeError, match="expects float"): |
| 163 | + myCustomOp.set_nodeattr("my_float_attr", "string") |
| 164 | + |
| 165 | + # Test string attribute |
| 166 | + assert myCustomOp.get_nodeattr("my_string_attr") == "default" |
| 167 | + myCustomOp.set_nodeattr("my_string_attr", "test_value") |
| 168 | + assert myCustomOp.get_nodeattr("my_string_attr") == "test_value" |
| 169 | + |
| 170 | + with pytest.raises(TypeError, match="expects str"): |
| 171 | + myCustomOp.set_nodeattr("my_string_attr", 123) |
| 172 | + with pytest.raises(TypeError, match="expects str"): |
| 173 | + myCustomOp.set_nodeattr("my_string_attr", 3.14) |
| 174 | + |
| 175 | + # Test ints attribute |
| 176 | + assert myCustomOp.get_nodeattr("my_ints_attr") == [] |
| 177 | + myCustomOp.set_nodeattr("my_ints_attr", [1, 2, 3]) |
| 178 | + assert myCustomOp.get_nodeattr("my_ints_attr") == [1, 2, 3] |
| 179 | + |
| 180 | + with pytest.raises(TypeError, match="expects list of ints"): |
| 181 | + myCustomOp.set_nodeattr("my_ints_attr", [1, 2.5, 3]) |
| 182 | + with pytest.raises(TypeError, match="expects list of ints"): |
| 183 | + myCustomOp.set_nodeattr("my_ints_attr", [1, "two", 3]) |
| 184 | + with pytest.raises(TypeError, match="expects list of ints"): |
| 185 | + myCustomOp.set_nodeattr("my_ints_attr", 123) |
| 186 | + |
| 187 | + # Test floats attribute |
| 188 | + assert myCustomOp.get_nodeattr("my_floats_attr") == [] |
| 189 | + myCustomOp.set_nodeattr("my_floats_attr", [1.0, 2.5, 3.14]) |
| 190 | + result = myCustomOp.get_nodeattr("my_floats_attr") |
| 191 | + assert len(result) == 3 |
| 192 | + assert abs(result[0] - 1.0) < 1e-6 |
| 193 | + assert abs(result[1] - 2.5) < 1e-6 |
| 194 | + assert abs(result[2] - 3.14) < 1e-6 |
| 195 | + # floats can accept ints |
| 196 | + myCustomOp.set_nodeattr("my_floats_attr", [1, 2, 3]) |
| 197 | + assert myCustomOp.get_nodeattr("my_floats_attr") == [1, 2, 3] |
| 198 | + |
| 199 | + with pytest.raises(TypeError, match="expects list of floats"): |
| 200 | + myCustomOp.set_nodeattr("my_floats_attr", [1.0, "two", 3.0]) |
| 201 | + with pytest.raises(TypeError, match="expects list of floats"): |
| 202 | + myCustomOp.set_nodeattr("my_floats_attr", 3.14) |
| 203 | + |
| 204 | + # Test strings attribute |
| 205 | + assert myCustomOp.get_nodeattr("my_strings_attr") == [] |
| 206 | + myCustomOp.set_nodeattr("my_strings_attr", ["a", "b", "c"]) |
| 207 | + assert myCustomOp.get_nodeattr("my_strings_attr") == ["a", "b", "c"] |
| 208 | + |
| 209 | + with pytest.raises(TypeError, match="expects list of strings"): |
| 210 | + myCustomOp.set_nodeattr("my_strings_attr", ["a", 2, "c"]) |
| 211 | + with pytest.raises(TypeError, match="expects list of strings"): |
| 212 | + myCustomOp.set_nodeattr("my_strings_attr", "not a list") |
| 213 | + |
| 214 | + # Test allowed_values validation |
| 215 | + assert myCustomOp.get_nodeattr("my_allowed_attr") == 1 |
| 216 | + myCustomOp.set_nodeattr("my_allowed_attr", 2) |
| 217 | + assert myCustomOp.get_nodeattr("my_allowed_attr") == 2 |
| 218 | + myCustomOp.set_nodeattr("my_allowed_attr", 3) |
| 219 | + assert myCustomOp.get_nodeattr("my_allowed_attr") == 3 |
| 220 | + |
| 221 | + with pytest.raises(ValueError, match="not in"): |
| 222 | + myCustomOp.set_nodeattr("my_allowed_attr", 5) |
| 223 | + |
| 224 | + # Test tensor attribute (numpy arrays) |
| 225 | + default_tensor = myCustomOp.get_nodeattr("my_tensor_attr") |
| 226 | + assert default_tensor.shape == (0,) |
| 227 | + |
| 228 | + # Set a 1D numpy array |
| 229 | + tensor_1d = np.array([1, 2, 3, 4, 5], dtype=np.int32) |
| 230 | + myCustomOp.set_nodeattr("my_tensor_attr", tensor_1d) |
| 231 | + result_1d = myCustomOp.get_nodeattr("my_tensor_attr") |
| 232 | + assert np.array_equal(result_1d, tensor_1d) |
| 233 | + assert result_1d.dtype == tensor_1d.dtype |
| 234 | + |
| 235 | + # Set a 2D numpy array |
| 236 | + tensor_2d = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float32) |
| 237 | + myCustomOp.set_nodeattr("my_tensor_attr", tensor_2d) |
| 238 | + result_2d = myCustomOp.get_nodeattr("my_tensor_attr") |
| 239 | + assert np.array_equal(result_2d, tensor_2d) |
| 240 | + assert result_2d.shape == tensor_2d.shape |
| 241 | + |
| 242 | + # Set a 3D numpy array with different dtype |
| 243 | + tensor_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=np.int8) |
| 244 | + myCustomOp.set_nodeattr("my_tensor_attr", tensor_3d) |
| 245 | + result_3d = myCustomOp.get_nodeattr("my_tensor_attr") |
| 246 | + assert np.array_equal(result_3d, tensor_3d) |
| 247 | + assert result_3d.shape == (2, 2, 2) |
| 248 | + assert result_3d.dtype == np.int8 |
| 249 | + |
| 250 | + # Test assigning numpy arrays to non-tensor attributes (should fail) |
| 251 | + numpy_arr = np.array([1, 2, 3]) |
| 252 | + with pytest.raises(TypeError, match="expects int"): |
| 253 | + myCustomOp.set_nodeattr("my_int_attr", numpy_arr) |
| 254 | + with pytest.raises(TypeError, match="expects float"): |
| 255 | + myCustomOp.set_nodeattr("my_float_attr", numpy_arr) |
| 256 | + with pytest.raises(TypeError, match="expects str"): |
| 257 | + myCustomOp.set_nodeattr("my_string_attr", numpy_arr) |
| 258 | + with pytest.raises(TypeError, match="expects list of ints"): |
| 259 | + myCustomOp.set_nodeattr("my_ints_attr", numpy_arr) |
| 260 | + with pytest.raises(TypeError, match="expects list of floats"): |
| 261 | + myCustomOp.set_nodeattr("my_floats_attr", numpy_arr) |
| 262 | + with pytest.raises(TypeError, match="expects list of strings"): |
| 263 | + myCustomOp.set_nodeattr("my_strings_attr", numpy_arr) |
| 264 | + |
| 265 | + # Test assigning non-numpy values to tensor attribute (should fail or convert) |
| 266 | + # Scalars should fail |
| 267 | + with pytest.raises((TypeError, AttributeError)): |
| 268 | + myCustomOp.set_nodeattr("my_tensor_attr", 42) |
| 269 | + with pytest.raises((TypeError, AttributeError)): |
| 270 | + myCustomOp.set_nodeattr("my_tensor_attr", 3.14) |
| 271 | + with pytest.raises((TypeError, AttributeError)): |
| 272 | + myCustomOp.set_nodeattr("my_tensor_attr", "string") |
| 273 | + |
| 274 | + # Test assigning lists to tensor attribute (should fail with TypeError) |
| 275 | + # Plain lists are not accepted - must be numpy arrays |
| 276 | + with pytest.raises(TypeError, match="expects numpy array"): |
| 277 | + myCustomOp.set_nodeattr("my_tensor_attr", [10, 20, 30, 40]) |
| 278 | + |
| 279 | + with pytest.raises(TypeError, match="expects numpy array"): |
| 280 | + myCustomOp.set_nodeattr("my_tensor_attr", [[1, 2, 3], [4, 5, 6]]) |
| 281 | + |
| 282 | + with pytest.raises(TypeError, match="expects numpy array"): |
| 283 | + myCustomOp.set_nodeattr("my_tensor_attr", [1.5, 2.5, 3.5]) |
0 commit comments