From c5d02e34fd52974735e7a35a5939529434abd2e2 Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Thu, 25 Jul 2024 14:42:24 -0700 Subject: [PATCH] remove unused tensors from VK model's graph Summary: We implemented [operators fusion](https://github.com/pytorch/executorch/pull/3769?fbclid=IwZXh0bgNhZW0CMTEAAR3kYya0wRrkupmV86OpPZZ9_QhqLYEmNrKcJk5Jj_4VSO_WqvFsbWNigTs_aem_gQeSu2zvazf_hpy3RsIXhg) (`conv+bn`) which fused `conv` and `bn`'s weights and biases, but the old parameters are not deleted. Hence we saw that VK model's size is nearly twice large as CPU's. As regards mobilenet_v2, before this diff CPU vs VK: 14M vs 22M. After this diff, both of them have 14M. Differential Revision: D60257047 --- .../vulkan/serialization/vulkan_graph_builder.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/backends/vulkan/serialization/vulkan_graph_builder.py b/backends/vulkan/serialization/vulkan_graph_builder.py index 477e54a2d76..f2e29e28568 100644 --- a/backends/vulkan/serialization/vulkan_graph_builder.py +++ b/backends/vulkan/serialization/vulkan_graph_builder.py @@ -262,12 +262,14 @@ def get_or_create_value_for(self, arg: _Argument): raise RuntimeError(f"Cannot create value for arg of type {type(arg)}") def process_placeholder_node(self, node: Node) -> None: - ids = self.create_node_value(node) - if not self.is_param_node(node): - if isinstance(ids, int): - self.input_ids.append(ids) - else: - self.input_ids += ids + # ignores any tensors that don't get used in any ops + if len(node.users) != 0: + ids = self.create_node_value(node) + if not self.is_param_node(node): + if isinstance(ids, int): + self.input_ids.append(ids) + else: + self.input_ids += ids def process_getitem_node(self, node: Node) -> None: # Find ValueList id from the collection node.