|
| 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 | +from typing import cast, final, List |
| 10 | + |
| 11 | +import torch |
| 12 | +from executorch.backends.aoti.aoti_backend import AotiBackend # usort: skip |
| 13 | +from executorch.exir.backend.compile_spec_schema import CompileSpec |
| 14 | +from executorch.exir.backend.partitioner import ( |
| 15 | + DelegationSpec, |
| 16 | + Partitioner, |
| 17 | + PartitionResult, |
| 18 | +) |
| 19 | +from executorch.exir.backend.utils import tag_constant_data |
| 20 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 21 | +from torch.export.exported_program import ExportedProgram |
| 22 | +from torch.fx.passes.infra.partitioner import CapabilityBasedPartitioner |
| 23 | + |
| 24 | +from torch.fx.passes.operator_support import OperatorSupportBase |
| 25 | + |
| 26 | + |
| 27 | +class AOTISupportedOperators(OperatorSupportBase): |
| 28 | + def is_node_supported(self, submodules, node: torch.fx.Node) -> bool: |
| 29 | + supported = node.op == "call_function" and node.target in [ |
| 30 | + exir_ops.edge.aten.add.Tensor, |
| 31 | + exir_ops.edge.aten._to_copy.default, |
| 32 | + ] |
| 33 | + |
| 34 | + return supported |
| 35 | + |
| 36 | + def is_node_supported_custom(self, node: torch.fx.Node) -> bool: |
| 37 | + if node.target == exir_ops.edge.aten.mean.dim: |
| 38 | + keep_dim = node.args[2] if len(node.args) > 2 else False |
| 39 | + return cast(bool, keep_dim) |
| 40 | + if node.target == exir_ops.edge.aten.var.correction: |
| 41 | + keep_dim = node.kwargs.get("keepdim", False) |
| 42 | + return cast(bool, keep_dim) |
| 43 | + return True |
| 44 | + |
| 45 | + |
| 46 | +@final |
| 47 | +class AotiPartitioner(Partitioner): |
| 48 | + def __init__(self, compile_spec: List[CompileSpec]) -> None: |
| 49 | + self.delegation_spec = DelegationSpec(AotiBackend.__name__, compile_spec) |
| 50 | + print(self.delegation_spec) |
| 51 | + |
| 52 | + def partition(self, exported_program: ExportedProgram) -> PartitionResult: |
| 53 | + # Run the CapabilityBasedPartitioner to return the largest possible |
| 54 | + # subgraphs containing the nodes with the tags |
| 55 | + # logger.info("AotiPartitioner::partition") |
| 56 | + partition_tags = {} |
| 57 | + |
| 58 | + capability_partitioner = CapabilityBasedPartitioner( |
| 59 | + exported_program.graph_module, |
| 60 | + AOTISupportedOperators(), |
| 61 | + allows_single_node_partition=True, |
| 62 | + ) |
| 63 | + partition_list = capability_partitioner.propose_partitions() |
| 64 | + for partition in partition_list: |
| 65 | + for node in partition.nodes: |
| 66 | + tag = f"tag{partition.id}" |
| 67 | + node.meta["delegation_tag"] = tag |
| 68 | + partition_tags[tag] = self.delegation_spec |
| 69 | + |
| 70 | + tag_constant_data(exported_program) |
| 71 | + |
| 72 | + return PartitionResult( |
| 73 | + tagged_exported_program=exported_program, partition_tags=partition_tags |
| 74 | + ) |
0 commit comments