From 54144168541f07eff6e6d5b8e95454bf6ea846dd Mon Sep 17 00:00:00 2001 From: mdaniowi Date: Tue, 30 Jul 2024 14:55:46 +0100 Subject: [PATCH 1/3] strings attribute support added to CustomOp --- src/qonnx/custom_op/base.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/qonnx/custom_op/base.py b/src/qonnx/custom_op/base.py index bd2545fa..775d9f95 100644 --- a/src/qonnx/custom_op/base.py +++ b/src/qonnx/custom_op/base.py @@ -74,6 +74,8 @@ def get_nodeattr(self, name): if dtype == "s": # decode string attributes ret = ret.decode("utf-8") + elif dtype == "strings": + ret = [x.decode("utf-8") for x in ret] elif dtype == "t": # use numpy helper to convert TensorProto -> np array ret = np_helper.to_array(ret) @@ -123,13 +125,15 @@ def set_nodeattr(self, name, value): # encode string attributes value = value.encode("utf-8") attr.__setattr__(dtype, value) + elif dtype == "strings": + attr.strings[:] = [x.encode("utf-8") for x in value] elif dtype == "floats": # list of floats attr.floats[:] = value elif dtype == "ints": # list of integers attr.ints[:] = value elif dtype == "t": # single tensor attr.t.CopyFrom(value) - elif dtype in ["strings", "tensors", "graphs", "sparse_tensors"]: + elif dtype in ["tensors", "graphs", "sparse_tensors"]: # untested / unsupported attribute types # add testcases & appropriate getters before enabling raise Exception("Attribute type %s not yet supported" % dtype) From ba5c41f527bb7fb045bed8842786af2d17ec75e9 Mon Sep 17 00:00:00 2001 From: mdaniowi Date: Thu, 1 Aug 2024 09:30:14 +0100 Subject: [PATCH 2/3] strings attr test added to test_attr.py --- tests/custom_op/test_attr.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/custom_op/test_attr.py b/tests/custom_op/test_attr.py index 9db644d7..6e2527ac 100644 --- a/tests/custom_op/test_attr.py +++ b/tests/custom_op/test_attr.py @@ -37,7 +37,11 @@ class AttrTestOp(CustomOp): def get_nodeattr_types(self): - return {"tensor_attr": ("t", True, np.asarray([]))} + my_attrs = { + "tensor_attr": ("t", True, np.asarray([])), + "strings_attr": ("strings", True, [""]) + } + return my_attrs def make_shape_compatible_op(self, model): param_tensor = self.get_nodeattr("tensor_attr") @@ -70,6 +74,7 @@ def test_attr(): strarr = np.array2string(w, separator=", ") w_str = strarr.replace("[", "{").replace("]", "}").replace(" ", "") tensor_attr_str = f"int8{wshp_str} {w_str}" + strings_attr = ["a", "bc", "def"] input = f""" < @@ -86,9 +91,18 @@ def test_attr(): model = oprs.parse_model(input) model = ModelWrapper(model) inst = getCustomOp(model.graph.node[0]) + w_prod = inst.get_nodeattr("tensor_attr") assert (w_prod == w).all() w = w - 1 inst.set_nodeattr("tensor_attr", w) w_prod = inst.get_nodeattr("tensor_attr") assert (w_prod == w).all() + + inst.set_nodeattr("strings_attr", strings_attr) + strings_attr_prod = inst.get_nodeattr("strings_attr") + assert strings_attr_prod == strings_attr + strings_attr_prod[0] = "test" + inst.set_nodeattr("strings_attr", strings_attr_prod) + assert inst.get_nodeattr("strings_attr") == ["test"] + strings_attr[1:] + From 654bf1526075f4335b733cefc64e660e42db4b15 Mon Sep 17 00:00:00 2001 From: mdaniowi Date: Mon, 12 Aug 2024 08:31:07 +0000 Subject: [PATCH 3/3] pre-commit applied --- tests/custom_op/test_attr.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/custom_op/test_attr.py b/tests/custom_op/test_attr.py index 6e2527ac..cde5a321 100644 --- a/tests/custom_op/test_attr.py +++ b/tests/custom_op/test_attr.py @@ -37,10 +37,7 @@ class AttrTestOp(CustomOp): def get_nodeattr_types(self): - my_attrs = { - "tensor_attr": ("t", True, np.asarray([])), - "strings_attr": ("strings", True, [""]) - } + my_attrs = {"tensor_attr": ("t", True, np.asarray([])), "strings_attr": ("strings", True, [""])} return my_attrs def make_shape_compatible_op(self, model): @@ -105,4 +102,3 @@ def test_attr(): strings_attr_prod[0] = "test" inst.set_nodeattr("strings_attr", strings_attr_prod) assert inst.get_nodeattr("strings_attr") == ["test"] + strings_attr[1:] -