|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +# pyre-strict |
| 8 | + |
| 9 | +from typing import Set, Union |
| 10 | + |
| 11 | +import torch |
| 12 | + |
| 13 | +from executorch.exir.dialects.edge._ops import EdgeOpOverload |
| 14 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 15 | +from executorch.exir.program._program import _get_updated_graph_signature |
| 16 | + |
| 17 | +from torch.export.exported_program import ExportedProgram |
| 18 | + |
| 19 | +OpType = Union[str, torch._ops.OpOverload, EdgeOpOverload] |
| 20 | + |
| 21 | + |
| 22 | +class RemoveAssertsTransform(ExportPass): |
| 23 | + """ |
| 24 | + Remove operators which perform assertions. These are not possible to execute in |
| 25 | + Vulkan since GLSL shaders cannot abort execution at runtime. Therefore, remove these |
| 26 | + operators. |
| 27 | + """ |
| 28 | + |
| 29 | + assert_ops: Set[OpType] = { |
| 30 | + torch.ops.aten._assert_scalar.default, |
| 31 | + torch.ops.aten.sym_constrain_range_for_size.default, |
| 32 | + } |
| 33 | + |
| 34 | + def call(self, graph_module: torch.fx.GraphModule) -> PassResult: |
| 35 | + for node in graph_module.graph.nodes: |
| 36 | + if node.target in self.assert_ops: |
| 37 | + graph_module.graph.erase_node(node) |
| 38 | + |
| 39 | + graph_module.graph.eliminate_dead_code() |
| 40 | + graph_module.recompile() |
| 41 | + return PassResult(graph_module, True) |
| 42 | + |
| 43 | + |
| 44 | +def remove_asserts(edge_program: ExportedProgram) -> ExportedProgram: |
| 45 | + graph_module = edge_program.graph_module |
| 46 | + RemoveAssertsTransform()(graph_module) |
| 47 | + |
| 48 | + edge_program._graph_signature = _get_updated_graph_signature( |
| 49 | + edge_program.graph_signature, graph_module |
| 50 | + ) |
| 51 | + edge_program._validate() |
| 52 | + return edge_program |
0 commit comments