|
| 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-unsafe |
| 8 | + |
| 9 | +import unittest |
| 10 | + |
| 11 | +from typing import List |
| 12 | + |
| 13 | +import torch |
| 14 | +from executorch.exir import EdgeCompileConfig, to_edge |
| 15 | + |
| 16 | +from .replace_custom_ops_with_aten_ops_pass import ReplaceCustomOpsWithAtenOpsPass |
| 17 | + |
| 18 | + |
| 19 | +class TestPasses(unittest.TestCase): |
| 20 | + def test_replace_custom_ops_with_aten_ops_pass(self) -> None: |
| 21 | + from executorch.extension.llm.custom_ops import preprocess_custom_ops # noqa |
| 22 | + |
| 23 | + class Pad(torch.nn.Module): |
| 24 | + def forward(self, x: torch.Tensor, padding: List[int]) -> torch.Tensor: |
| 25 | + return torch.ops.preprocess.pad.default(x, padding) |
| 26 | + |
| 27 | + pad = Pad() |
| 28 | + |
| 29 | + image_tensor = torch.ones([3, 4, 5]) |
| 30 | + padding = [0, 2, 0, 1] |
| 31 | + |
| 32 | + edge_prog = to_edge( |
| 33 | + torch.export.export(pad, (image_tensor, padding), strict=False), |
| 34 | + compile_config=EdgeCompileConfig(_check_ir_validity=False), |
| 35 | + ) |
| 36 | + |
| 37 | + # Check that the custom op exists in the graph, and aten op does not. |
| 38 | + edge_nodes = [node.name for node in edge_prog.exported_program().graph.nodes] |
| 39 | + assert "constant_pad_nd" not in edge_nodes |
| 40 | + assert "preprocess_pad_default" in edge_nodes |
| 41 | + |
| 42 | + edge_prog = edge_prog.transform([ReplaceCustomOpsWithAtenOpsPass()]) |
| 43 | + |
| 44 | + # After running replace_custom_ops_with_aten_ops pass, the custom op |
| 45 | + # should be replaced with aten op. |
| 46 | + post_transform_nodes = [ |
| 47 | + node.name for node in edge_prog.exported_program().graph.nodes |
| 48 | + ] |
| 49 | + assert "constant_pad_nd" in post_transform_nodes |
| 50 | + assert "preprocess_pad_default" not in post_transform_nodes |
0 commit comments