From e5bb71cb82658f03b9290f60cc2c994ae3421a05 Mon Sep 17 00:00:00 2001 From: jvreca Date: Fri, 23 Aug 2024 15:20:36 +0200 Subject: [PATCH 1/3] Addded del_initializer to modelwrapper. --- src/qonnx/core/modelwrapper.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/qonnx/core/modelwrapper.py b/src/qonnx/core/modelwrapper.py index c27b7774..3a52769a 100644 --- a/src/qonnx/core/modelwrapper.py +++ b/src/qonnx/core/modelwrapper.py @@ -367,6 +367,14 @@ def get_initializer(self, tensor_name, return_dtype=False): else: return None + def del_initializer(self, initializer_name): + """Deletes an initializer from the model.""" + graph = self._model_proto.graph + for init in graph.initializer: + if init.name == initializer_name: + graph.initializer.remove(init) + break + def find_producer(self, tensor_name): """Finds and returns the node that produces the tensor with given name.""" for x in self._model_proto.graph.node: From 5269aa611eeba5cab9c3b0d4d2f987e7f2baf9a4 Mon Sep 17 00:00:00 2001 From: Yaman Umuroglu Date: Fri, 20 Dec 2024 09:25:47 +0100 Subject: [PATCH 2/3] [Test] factor out ModelWrapper init get/set tests, add init rm test --- tests/core/test_modelwrapper.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tests/core/test_modelwrapper.py b/tests/core/test_modelwrapper.py index f0cb203e..06d08798 100644 --- a/tests/core/test_modelwrapper.py +++ b/tests/core/test_modelwrapper.py @@ -54,11 +54,6 @@ def test_modelwrapper(): assert first_conv_iname != "" and (first_conv_iname is not None) assert first_conv_wname != "" and (first_conv_wname is not None) assert first_conv_oname != "" and (first_conv_oname is not None) - first_conv_weights = model.get_initializer(first_conv_wname) - assert first_conv_weights.shape == (8, 1, 5, 5) - first_conv_weights_rand = np.random.randn(8, 1, 5, 5) - model.set_initializer(first_conv_wname, first_conv_weights_rand) - assert (model.get_initializer(first_conv_wname) == first_conv_weights_rand).all() inp_cons = model.find_consumer(first_conv_iname) assert inp_cons == first_conv out_prod = model.find_producer(first_conv_oname) @@ -75,6 +70,21 @@ def test_modelwrapper(): assert model.get_tensor_sparsity(first_conv_iname) == inp_sparsity +def test_modelwrapper_set_get_rm_initializer(): + raw_m = get_data("qonnx.data", "onnx/mnist-conv/model.onnx") + model = ModelWrapper(raw_m) + conv_nodes = model.get_nodes_by_op_type("Conv") + first_conv = conv_nodes[0] + first_conv_wname = first_conv.input[1] + first_conv_weights = model.get_initializer(first_conv_wname) + assert first_conv_weights.shape == (8, 1, 5, 5) + first_conv_weights_rand = np.random.randn(8, 1, 5, 5) + model.set_initializer(first_conv_wname, first_conv_weights_rand) + assert (model.get_initializer(first_conv_wname) == first_conv_weights_rand).all() + model.del_initializer(first_conv_wname) + assert model.get_initializer(first_conv_wname) is None + + def test_modelwrapper_graph_order(): # create small network with properties to be tested Neg_node = onnx.helper.make_node("Neg", inputs=["in1"], outputs=["neg1"]) From 504c14e6dcddbacc8276f8ebdac1328f0374c8c8 Mon Sep 17 00:00:00 2001 From: Yaman Umuroglu Date: Fri, 20 Dec 2024 09:27:57 +0100 Subject: [PATCH 3/3] [Core] refactor ModelWrapper.del_initializer to use get_by_name --- src/qonnx/core/modelwrapper.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/qonnx/core/modelwrapper.py b/src/qonnx/core/modelwrapper.py index 3a52769a..f5895c56 100644 --- a/src/qonnx/core/modelwrapper.py +++ b/src/qonnx/core/modelwrapper.py @@ -370,10 +370,9 @@ def get_initializer(self, tensor_name, return_dtype=False): def del_initializer(self, initializer_name): """Deletes an initializer from the model.""" graph = self._model_proto.graph - for init in graph.initializer: - if init.name == initializer_name: - graph.initializer.remove(init) - break + init = util.get_by_name(graph.initializer, initializer_name) + if not (init is None): + graph.initializer.remove(init) def find_producer(self, tensor_name): """Finds and returns the node that produces the tensor with given name."""