|
| 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 | + |
| 10 | +import logging |
| 11 | +import os |
| 12 | +import unittest |
| 13 | + |
| 14 | +from enum import Enum |
| 15 | +from typing import Any, Callable, Tuple |
| 16 | + |
| 17 | +import torch |
| 18 | +from executorch.backends.test.harness import Tester |
| 19 | + |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | +logger.setLevel(logging.INFO) |
| 22 | + |
| 23 | + |
| 24 | +# Read enabled backends from the environment variable. Enable all if |
| 25 | +# not specified (signalled by None). |
| 26 | +def get_enabled_backends(): |
| 27 | + et_test_backends = os.environ.get("ET_TEST_ENABLED_BACKENDS") |
| 28 | + if et_test_backends is not None: |
| 29 | + return et_test_backends.split(",") |
| 30 | + else: |
| 31 | + return None |
| 32 | + |
| 33 | + |
| 34 | +_ENABLED_BACKENDS = get_enabled_backends() |
| 35 | + |
| 36 | + |
| 37 | +def is_backend_enabled(backend): |
| 38 | + if _ENABLED_BACKENDS is None: |
| 39 | + return True |
| 40 | + else: |
| 41 | + return backend in _ENABLED_BACKENDS |
| 42 | + |
| 43 | + |
| 44 | +ALL_TEST_FLOWS = [] |
| 45 | + |
| 46 | +if is_backend_enabled("xnnpack"): |
| 47 | + from executorch.backends.xnnpack.test.tester import Tester as XnnpackTester |
| 48 | + |
| 49 | + XNNPACK_TEST_FLOW = ("xnnpack", XnnpackTester) |
| 50 | + ALL_TEST_FLOWS.append(XNNPACK_TEST_FLOW) |
| 51 | + |
| 52 | +if is_backend_enabled("coreml"): |
| 53 | + try: |
| 54 | + from executorch.backends.apple.coreml.test.tester import CoreMLTester |
| 55 | + |
| 56 | + COREML_TEST_FLOW = ("coreml", CoreMLTester) |
| 57 | + ALL_TEST_FLOWS.append(COREML_TEST_FLOW) |
| 58 | + except Exception: |
| 59 | + print("Core ML AOT is not available.") |
| 60 | + |
| 61 | + |
| 62 | +DTYPES = [ |
| 63 | + torch.int8, |
| 64 | + torch.uint8, |
| 65 | + torch.int16, |
| 66 | + torch.uint16, |
| 67 | + torch.int32, |
| 68 | + torch.uint32, |
| 69 | + torch.int64, |
| 70 | + torch.uint64, |
| 71 | + torch.float16, |
| 72 | + torch.float32, |
| 73 | + torch.float64, |
| 74 | +] |
| 75 | + |
| 76 | +FLOAT_DTYPES = [ |
| 77 | + torch.float16, |
| 78 | + torch.float32, |
| 79 | + torch.float64, |
| 80 | +] |
| 81 | + |
| 82 | + |
| 83 | +# The type of test function. This controls the test generation and expected signature. |
| 84 | +# Standard tests are run, as is. Dtype tests get a variant generated for each dtype and |
| 85 | +# take an additional dtype parameter. |
| 86 | +class TestType(Enum): |
| 87 | + STANDARD = 1 |
| 88 | + DTYPE = 2 |
| 89 | + |
| 90 | + |
| 91 | +# Function annotation for dtype tests. This instructs the test framework to run the test |
| 92 | +# for each supported dtype and to pass dtype as a test parameter. |
| 93 | +def dtype_test(func): |
| 94 | + func.test_type = TestType.DTYPE |
| 95 | + return func |
| 96 | + |
| 97 | + |
| 98 | +# Class annotation for operator tests. This triggers the test framework to register |
| 99 | +# the tests. |
| 100 | +def operator_test(cls): |
| 101 | + _create_tests(cls) |
| 102 | + return cls |
| 103 | + |
| 104 | + |
| 105 | +# Generate test cases for each backend flow. |
| 106 | +def _create_tests(cls): |
| 107 | + for key in dir(cls): |
| 108 | + if key.startswith("test_"): |
| 109 | + _expand_test(cls, key) |
| 110 | + |
| 111 | + |
| 112 | +# Expand a test into variants for each registered flow. |
| 113 | +def _expand_test(cls, test_name: str): |
| 114 | + test_func = getattr(cls, test_name) |
| 115 | + for flow_name, tester_factory in ALL_TEST_FLOWS: |
| 116 | + _create_test_for_backend(cls, test_func, flow_name, tester_factory) |
| 117 | + delattr(cls, test_name) |
| 118 | + |
| 119 | + |
| 120 | +def _make_wrapped_test(test_func, *args, **kwargs): |
| 121 | + def wrapped_test(self): |
| 122 | + test_func(self, *args, **kwargs) |
| 123 | + |
| 124 | + return wrapped_test |
| 125 | + |
| 126 | + |
| 127 | +def _make_wrapped_dtype_test(test_func, dtype, tester_factory): |
| 128 | + def wrapped_test(self): |
| 129 | + test_func(self, dtype, tester_factory) |
| 130 | + |
| 131 | + return wrapped_test |
| 132 | + |
| 133 | + |
| 134 | +def _create_test_for_backend( |
| 135 | + cls, |
| 136 | + test_func: Callable, |
| 137 | + flow_name: str, |
| 138 | + tester_factory: Callable[[torch.nn.Module, Tuple[Any]], Tester], |
| 139 | +): |
| 140 | + test_type = getattr(test_func, "test_type", TestType.STANDARD) |
| 141 | + |
| 142 | + if test_type == TestType.STANDARD: |
| 143 | + wrapped_test = _make_wrapped_test(test_func, tester_factory) |
| 144 | + test_name = f"{test_func.__name__}_{flow_name}" |
| 145 | + setattr(cls, test_name, wrapped_test) |
| 146 | + elif test_type == TestType.DTYPE: |
| 147 | + for dtype in DTYPES: |
| 148 | + # wrapped_test = _make_wrapped_dtype_test(test_func, dtype, tester_factory) |
| 149 | + wrapped_test = _make_wrapped_test(test_func, dtype, tester_factory) |
| 150 | + dtype_name = str(dtype)[6:] # strip "torch." |
| 151 | + test_name = f"{test_func.__name__}_{dtype_name}_{flow_name}" |
| 152 | + setattr(cls, test_name, wrapped_test) |
| 153 | + else: |
| 154 | + raise NotImplementedError(f"Unknown test type {test_type}.") |
| 155 | + |
| 156 | + |
| 157 | +class OperatorTest(unittest.TestCase): |
| 158 | + def _test_op(self, model, inputs, tester_factory): |
| 159 | + tester = ( |
| 160 | + tester_factory( |
| 161 | + model, |
| 162 | + inputs, |
| 163 | + ) |
| 164 | + .export() |
| 165 | + .to_edge_transform_and_lower() |
| 166 | + ) |
| 167 | + |
| 168 | + is_delegated = any( |
| 169 | + n.target == torch._higher_order_ops.executorch_call_delegate |
| 170 | + for n in tester.stages[tester.cur].graph_module.graph.nodes |
| 171 | + if n.op == "call_function" |
| 172 | + ) |
| 173 | + |
| 174 | + # Only run the runtime test if the op was delegated. |
| 175 | + if is_delegated: |
| 176 | + (tester.to_executorch().serialize().run_method_and_compare_outputs()) |
0 commit comments