|
37 | 37 | ) |
38 | 38 | from executorch.exir.dialects._ops import ops as exir_ops |
39 | 39 | from executorch.exir.dialects.edge._ops import EdgeOpOverload |
40 | | -from executorch.exir.pass_base import ExportPass, PassResult |
| 40 | +from executorch.exir.pass_base import PassResult |
| 41 | +from torch.fx.passes.infra.pass_base import PassBase |
41 | 42 |
|
42 | 43 | # A map to represent ops that: |
43 | 44 | # (a) are functionally equivalent; and |
@@ -1981,47 +1982,37 @@ def maybe_remove_or_replace(self, node: torch.fx.Node) -> bool: |
1981 | 1982 | return True |
1982 | 1983 |
|
1983 | 1984 |
|
1984 | | -class ReplaceEmptyTensorsWithFullPass(ExportPass): |
| 1985 | +class ReplaceEmptyTensorsWithFullPass(PassBase): |
1985 | 1986 | """Replaces nodes that produce empty tensors with full nodes.""" |
1986 | 1987 |
|
1987 | | - def call_operator(self, op, args, kwargs, meta): |
1988 | | - val = meta.data.get("val", None) |
1989 | | - if isinstance(val, torch.Tensor) and val.numel() == 0: |
1990 | | - return super().call_operator( |
1991 | | - exir_ops.edge.aten.full.default, |
1992 | | - args=(val.shape, 0), |
1993 | | - kwargs={"dtype": val.dtype}, |
1994 | | - meta=meta, |
1995 | | - ) |
1996 | | - return super().call_operator(op, args, kwargs, meta) |
1997 | | - |
1998 | 1988 | def call(self, graph_module: torch.fx.GraphModule) -> PassResult: |
1999 | 1989 | changed = False |
2000 | 1990 | for module in filter( |
2001 | 1991 | lambda m: isinstance(m, torch.fx.GraphModule), graph_module.modules() |
2002 | 1992 | ): |
2003 | | - module = cast(torch.fx.GraphModule, module) |
2004 | | - for node in module.graph.nodes: |
| 1993 | + graph_module_to_update = cast(torch.fx.GraphModule, module) |
| 1994 | + module_changed = False |
| 1995 | + for node in graph_module_to_update.graph.nodes: |
2005 | 1996 | if node.op != "call_function": |
2006 | 1997 | continue |
2007 | 1998 | val = node.meta.get("val", None) |
2008 | 1999 | if isinstance(val, torch.Tensor) and val.numel() == 0: |
2009 | | - with module.graph.inserting_before(node): |
2010 | | - new_node = module.graph.call_function( |
| 2000 | + with graph_module_to_update.graph.inserting_before(node): |
| 2001 | + new_node = graph_module_to_update.graph.call_function( |
2011 | 2002 | exir_ops.edge.aten.full.default, |
2012 | 2003 | args=(val.shape, 0), |
2013 | 2004 | kwargs={"dtype": val.dtype}, |
2014 | 2005 | ) |
2015 | | - new_node.meta = node.meta |
| 2006 | + new_node.meta = node.meta.copy() |
2016 | 2007 | node.replace_all_uses_with(new_node) |
2017 | | - changed = True |
| 2008 | + module_changed = True |
2018 | 2009 |
|
2019 | | - if changed: |
2020 | | - graph_module.graph.eliminate_dead_code() |
2021 | | - graph_module.recompile() |
2022 | | - return super().call(graph_module) |
| 2010 | + if module_changed: |
| 2011 | + graph_module_to_update.graph.eliminate_dead_code() |
| 2012 | + graph_module_to_update.recompile() |
| 2013 | + changed = True |
2023 | 2014 |
|
2024 | | - return PassResult(graph_module, False) |
| 2015 | + return PassResult(graph_module, changed) |
2025 | 2016 |
|
2026 | 2017 |
|
2027 | 2018 | class ReplaceWhereWithFullArgsWithWhereScalar(RemoveOrReplacePassInterface): |
|
0 commit comments