|
| 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 | +import unittest |
| 8 | +from typing import Any, Optional, Tuple |
| 9 | + |
| 10 | +import executorch.exir as exir |
| 11 | + |
| 12 | +import torch |
| 13 | + |
| 14 | +from executorch.codegen.tools.selective_build import ( |
| 15 | + _get_io_metadata_for_program_operators, |
| 16 | + _get_program_from_buffer, |
| 17 | + _get_program_operators, |
| 18 | + _IOMetaData, |
| 19 | +) |
| 20 | +from executorch.exir import CaptureConfig |
| 21 | +from executorch.exir.print_program import pretty_print |
| 22 | +from executorch.exir.scalar_type import ScalarType |
| 23 | +from executorch.exir.schema import Program |
| 24 | + |
| 25 | + |
| 26 | +class ModuleAdd(torch.nn.Module): |
| 27 | + """The module to serialize and execute.""" |
| 28 | + |
| 29 | + def __init__(self): |
| 30 | + super(ModuleAdd, self).__init__() |
| 31 | + |
| 32 | + def forward(self, x, y): |
| 33 | + return x + y |
| 34 | + |
| 35 | + def get_methods_to_export(self): |
| 36 | + return ("forward",) |
| 37 | + |
| 38 | + |
| 39 | +class ModuleMulti(torch.nn.Module): |
| 40 | + """The module to serialize and execute.""" |
| 41 | + |
| 42 | + def __init__(self): |
| 43 | + super(ModuleMulti, self).__init__() |
| 44 | + |
| 45 | + def forward(self, x, y): |
| 46 | + return x + y |
| 47 | + |
| 48 | + def forward2(self, x, y): |
| 49 | + return x + y + 1 |
| 50 | + |
| 51 | + def get_methods_to_export(self): |
| 52 | + return ("forward", "forward2") |
| 53 | + |
| 54 | + |
| 55 | +def create_program( |
| 56 | + eager_module: Optional[torch.nn.Module] = None, |
| 57 | +) -> Tuple[Program, Tuple[Any, ...]]: |
| 58 | + """Returns an executorch program based on ModuleAdd, along with inputs.""" |
| 59 | + |
| 60 | + if eager_module is None: |
| 61 | + eager_module = ModuleAdd() |
| 62 | + |
| 63 | + # Trace the test module and create a serialized Executorch program. |
| 64 | + inputs = (torch.ones(2, 2), torch.ones(2, 2)) |
| 65 | + input_map = {} |
| 66 | + for method in eager_module.get_methods_to_export(): |
| 67 | + input_map[method] = inputs |
| 68 | + |
| 69 | + # These cleanup passes are required to convert the `add` op to its out |
| 70 | + # variant, along with some other transformations. |
| 71 | + exec_prog = ( |
| 72 | + exir.capture_multiple(eager_module, input_map, config=CaptureConfig()) |
| 73 | + .to_edge() |
| 74 | + .to_executorch() |
| 75 | + ) |
| 76 | + |
| 77 | + # Create the Executorch program from the graph. |
| 78 | + pretty_print(exec_prog.program) |
| 79 | + return (exec_prog, inputs) |
| 80 | + |
| 81 | + |
| 82 | +class PybindingsTest(unittest.TestCase): |
| 83 | + def test_dump_operators(self): |
| 84 | + # Create and serialize a program. |
| 85 | + orig_program, _ = create_program() |
| 86 | + |
| 87 | + # Deserialize the program and demonstrate that we could get its operator |
| 88 | + # list. |
| 89 | + program = _get_program_from_buffer(orig_program.buffer) |
| 90 | + operators = _get_program_operators(program) |
| 91 | + self.assertEqual(operators, ["aten::add.out"]) |
| 92 | + |
| 93 | + def test_get_op_io_meta(self): |
| 94 | + # Checking whether get_op_io_meta returns the correct metadata for all its ios. |
| 95 | + orig_program, inputs = create_program() |
| 96 | + |
| 97 | + # Deserialize the program and demonstrate that we could get its operator |
| 98 | + # list. |
| 99 | + program = _get_program_from_buffer(orig_program.buffer) |
| 100 | + program_op_io_metadata = _get_io_metadata_for_program_operators(program) |
| 101 | + |
| 102 | + self.assertTrue(len(program_op_io_metadata) == 1) |
| 103 | + self.assertTrue(isinstance(program_op_io_metadata, dict)) |
| 104 | + |
| 105 | + self.assertTrue("aten::add.out" in program_op_io_metadata) |
| 106 | + self.assertTrue(isinstance(program_op_io_metadata["aten::add.out"], set)) |
| 107 | + self.assertTrue(len(program_op_io_metadata["aten::add.out"]) == 1) |
| 108 | + |
| 109 | + for op_io_metadata in program_op_io_metadata["aten::add.out"]: |
| 110 | + self.assertTrue(len(op_io_metadata) == 5) |
| 111 | + self.assertTrue(isinstance(op_io_metadata, tuple)) |
| 112 | + |
| 113 | + for io_idx, io_metadata in enumerate(op_io_metadata): |
| 114 | + self.assertTrue(isinstance(io_metadata, _IOMetaData)) |
| 115 | + if io_idx == 2: |
| 116 | + # TODO(gasoonjia): Create a enum class to map KernelTypes to int, remove the hardcoded 2 and 5 below. |
| 117 | + self.assertEqual(io_metadata.kernel_type, 2) |
| 118 | + else: |
| 119 | + self.assertEqual(io_metadata.kernel_type, 5) |
| 120 | + self.assertEqual(io_metadata.dtype, ScalarType.FLOAT) |
| 121 | + self.assertEqual(io_metadata.dim_order, [0, 1]) |
0 commit comments