|
| 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 itertools |
| 10 | +import os |
| 11 | +import unittest |
| 12 | +from typing import Any, Callable |
| 13 | + |
| 14 | +import torch |
| 15 | +from executorch.backends.test.harness import Tester |
| 16 | +from executorch.backends.test.suite import get_test_flows |
| 17 | +from executorch.backends.test.suite.context import get_active_test_context, TestContext |
| 18 | +from executorch.backends.test.suite.flow import TestFlow |
| 19 | +from executorch.backends.test.suite.reporting import log_test_summary |
| 20 | +from executorch.backends.test.suite.runner import run_test |
| 21 | + |
| 22 | + |
| 23 | +DTYPES: list[torch.dtype] = [ |
| 24 | + torch.float16, |
| 25 | + torch.float32, |
| 26 | + torch.float64, |
| 27 | +] |
| 28 | + |
| 29 | + |
| 30 | +def load_tests(loader, suite, pattern): |
| 31 | + package_dir = os.path.dirname(__file__) |
| 32 | + discovered_suite = loader.discover( |
| 33 | + start_dir=package_dir, pattern=pattern or "test_*.py" |
| 34 | + ) |
| 35 | + suite.addTests(discovered_suite) |
| 36 | + return suite |
| 37 | + |
| 38 | + |
| 39 | +def _create_test( |
| 40 | + cls, |
| 41 | + test_func: Callable, |
| 42 | + flow: TestFlow, |
| 43 | + dtype: torch.dtype, |
| 44 | + use_dynamic_shapes: bool, |
| 45 | +): |
| 46 | + def wrapped_test(self): |
| 47 | + params = { |
| 48 | + "dtype": dtype, |
| 49 | + "use_dynamic_shapes": use_dynamic_shapes, |
| 50 | + } |
| 51 | + with TestContext(test_name, flow.name, params): |
| 52 | + test_func(self, dtype, use_dynamic_shapes, flow.tester_factory) |
| 53 | + |
| 54 | + dtype_name = str(dtype)[6:] # strip "torch." |
| 55 | + test_name = f"{test_func.__name__}_{flow.name}_{dtype_name}" |
| 56 | + if use_dynamic_shapes: |
| 57 | + test_name += "_dynamic_shape" |
| 58 | + |
| 59 | + wrapped_test._name = test_func.__name__ # type: ignore |
| 60 | + wrapped_test._flow = flow # type: ignore |
| 61 | + |
| 62 | + setattr(cls, test_name, wrapped_test) |
| 63 | + |
| 64 | + |
| 65 | +# Expand a test into variants for each registered flow. |
| 66 | +def _expand_test(cls, test_name: str) -> None: |
| 67 | + test_func = getattr(cls, test_name) |
| 68 | + supports_dynamic_shapes = getattr(test_func, "supports_dynamic_shapes", True) |
| 69 | + dynamic_shape_values = [True, False] if supports_dynamic_shapes else [False] |
| 70 | + dtypes = getattr(test_func, "dtypes", DTYPES) |
| 71 | + |
| 72 | + for flow, dtype, use_dynamic_shapes in itertools.product( |
| 73 | + get_test_flows().values(), dtypes, dynamic_shape_values |
| 74 | + ): |
| 75 | + _create_test(cls, test_func, flow, dtype, use_dynamic_shapes) |
| 76 | + delattr(cls, test_name) |
| 77 | + |
| 78 | + |
| 79 | +def model_test_cls(cls) -> Callable | None: |
| 80 | + """Decorator for model tests. Handles generating test variants for each test flow and configuration.""" |
| 81 | + for key in dir(cls): |
| 82 | + if key.startswith("test_"): |
| 83 | + _expand_test(cls, key) |
| 84 | + return cls |
| 85 | + |
| 86 | + |
| 87 | +def model_test_params( |
| 88 | + supports_dynamic_shapes: bool = True, |
| 89 | + dtypes: list[torch.dtype] | None = None, |
| 90 | +) -> Callable: |
| 91 | + """Optional parameter decorator for model tests. Specifies test pararameters. Only valid with a class decorated by model_test_cls.""" |
| 92 | + |
| 93 | + def inner_decorator(func: Callable) -> Callable: |
| 94 | + func.supports_dynamic_shapes = supports_dynamic_shapes # type: ignore |
| 95 | + |
| 96 | + if dtypes is not None: |
| 97 | + func.dtypes = dtypes # type: ignore |
| 98 | + |
| 99 | + return func |
| 100 | + |
| 101 | + return inner_decorator |
| 102 | + |
| 103 | + |
| 104 | +def run_model_test( |
| 105 | + model: torch.nn.Module, |
| 106 | + inputs: tuple[Any], |
| 107 | + dtype: torch.dtype, |
| 108 | + dynamic_shapes: Any | None, |
| 109 | + tester_factory: Callable[[], Tester], |
| 110 | +): |
| 111 | + model = model.to(dtype) |
| 112 | + context = get_active_test_context() |
| 113 | + |
| 114 | + # This should be set in the wrapped test. See _create_test above. |
| 115 | + assert context is not None, "Missing test context." |
| 116 | + |
| 117 | + run_summary = run_test( |
| 118 | + model, |
| 119 | + inputs, |
| 120 | + tester_factory, |
| 121 | + context.test_name, |
| 122 | + context.flow_name, |
| 123 | + context.params, |
| 124 | + dynamic_shapes=dynamic_shapes, |
| 125 | + ) |
| 126 | + |
| 127 | + log_test_summary(run_summary) |
| 128 | + |
| 129 | + if not run_summary.result.is_success(): |
| 130 | + if run_summary.result.is_backend_failure(): |
| 131 | + raise RuntimeError("Test failure.") from run_summary.error |
| 132 | + else: |
| 133 | + # Non-backend failure indicates a bad test. Mark as skipped. |
| 134 | + raise unittest.SkipTest( |
| 135 | + f"Test failed for reasons other than backend failure. Error: {run_summary.error}" |
| 136 | + ) |
0 commit comments