|
| 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 | +from typing import Any, List, Optional, Tuple |
| 8 | + |
| 9 | +import executorch |
| 10 | +import executorch.backends.test.harness.stages as BaseStages |
| 11 | +import torch |
| 12 | +from executorch.backends.cuda.cuda_backend import CudaBackend |
| 13 | +from executorch.backends.cuda.cuda_partitioner import CudaPartitioner |
| 14 | +from executorch.backends.test.harness import Tester as TesterBase |
| 15 | +from executorch.backends.test.harness.stages import StageType |
| 16 | +from executorch.exir import EdgeCompileConfig |
| 17 | +from executorch.exir.backend.partitioner import Partitioner |
| 18 | + |
| 19 | + |
| 20 | +def _create_default_partitioner() -> CudaPartitioner: |
| 21 | + """Create a CudaPartitioner with default compile specs.""" |
| 22 | + compile_specs = [CudaBackend.generate_method_name_compile_spec("forward")] |
| 23 | + return CudaPartitioner(compile_specs) |
| 24 | + |
| 25 | + |
| 26 | +class ToEdgeTransformAndLower(BaseStages.ToEdgeTransformAndLower): |
| 27 | + """CUDA-specific ToEdgeTransformAndLower stage.""" |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + partitioners: Optional[List[Partitioner]] = None, |
| 32 | + edge_compile_config: Optional[EdgeCompileConfig] = None, |
| 33 | + ): |
| 34 | + if partitioners is None: |
| 35 | + partitioners = [_create_default_partitioner()] |
| 36 | + |
| 37 | + super().__init__( |
| 38 | + default_partitioner_cls=_create_default_partitioner, |
| 39 | + partitioners=partitioners, |
| 40 | + edge_compile_config=edge_compile_config |
| 41 | + or EdgeCompileConfig(_check_ir_validity=False), |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +class CudaTester(TesterBase): |
| 46 | + """ |
| 47 | + Tester subclass for CUDA backend. |
| 48 | +
|
| 49 | + This tester defines the recipe for lowering models to the CUDA backend |
| 50 | + using AOTInductor compilation. |
| 51 | + """ |
| 52 | + |
| 53 | + def __init__( |
| 54 | + self, |
| 55 | + module: torch.nn.Module, |
| 56 | + example_inputs: Tuple[torch.Tensor], |
| 57 | + dynamic_shapes: Optional[Tuple[Any]] = None, |
| 58 | + ): |
| 59 | + stage_classes = ( |
| 60 | + executorch.backends.test.harness.Tester.default_stage_classes() |
| 61 | + | { |
| 62 | + StageType.TO_EDGE_TRANSFORM_AND_LOWER: ToEdgeTransformAndLower, |
| 63 | + } |
| 64 | + ) |
| 65 | + |
| 66 | + super().__init__( |
| 67 | + module=module, |
| 68 | + stage_classes=stage_classes, |
| 69 | + example_inputs=example_inputs, |
| 70 | + dynamic_shapes=dynamic_shapes, |
| 71 | + ) |
0 commit comments