|
| 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 | +""" |
| 8 | +reinplace pass for the native backend. |
| 9 | +
|
| 10 | +reinplace rewrites functional ops into their in-place forms (e.g. relu -> relu_), |
| 11 | +which are not in the core-ATen opset, so callers must disable IR validity |
| 12 | +checking via ``EdgeCompileConfig(_check_ir_validity=False)`` (the same config the |
| 13 | +MLX backend requires). |
| 14 | +""" |
| 15 | + |
| 16 | +from typing import Tuple |
| 17 | + |
| 18 | +import torch |
| 19 | + |
| 20 | +from executorch.exir.dialects._ops import ops as _edge_ops |
| 21 | +from executorch.exir.pass_base import ExportedProgramPassBase, ExportedProgramPassResult |
| 22 | + |
| 23 | +_edge = _edge_ops.edge.aten |
| 24 | + |
| 25 | +# Single source of truth: base names of the ops the backend can execute in-place. |
| 26 | +# reinplace rewrites the functional edge op to its mutating variant (e.g. |
| 27 | +# relu -> relu_), and the partitioner must claim that variant to keep it in the |
| 28 | +# delegate (see backend_inplace_aten_variants). |
| 29 | +_INPLACE_OP_NAMES: Tuple[str, ...] = ( |
| 30 | + "relu", |
| 31 | + "gelu", |
| 32 | + "sigmoid", |
| 33 | + "index_put", |
| 34 | + "index_copy", |
| 35 | +) |
| 36 | + |
| 37 | +# Edge ops the backend can execute in-place, consumed as reinplace's op set. |
| 38 | +BACKEND_INPLACE_OPS: frozenset = frozenset( |
| 39 | + getattr(_edge, name).default for name in _INPLACE_OP_NAMES |
| 40 | +) |
| 41 | + |
| 42 | + |
| 43 | +def backend_inplace_aten_variants() -> set: |
| 44 | + """Mutating aten variants that reinplace introduces for BACKEND_INPLACE_OPS. |
| 45 | +
|
| 46 | + reinplace rewrites e.g. relu -> aten.relu_.default; those in-place ops are |
| 47 | + not core-tagged, so the partitioner must claim them explicitly to keep them |
| 48 | + inside the delegate. |
| 49 | + """ |
| 50 | + ops = set() |
| 51 | + for name in _INPLACE_OP_NAMES: |
| 52 | + packet = getattr(torch.ops.aten, f"{name}_", None) |
| 53 | + overload = getattr(packet, "default", None) if packet is not None else None |
| 54 | + if overload is not None: |
| 55 | + ops.add(overload) |
| 56 | + return ops |
| 57 | + |
| 58 | + |
| 59 | +class NativeReinplacePass(ExportedProgramPassBase): |
| 60 | + """Rewrite backend-supported elementwise ops into their in-place edge forms. |
| 61 | +
|
| 62 | + Runs as an EP-aware pass because ``reinplace_pass`` needs ``graph_signature`` |
| 63 | + to protect mutable inputs/buffers. A fresh op set is passed each call so |
| 64 | + ``reinplace_pass`` cannot mutate shared state, and passing an explicit |
| 65 | + ``ops_to_inplace`` fully replaces reinplace's default set. |
| 66 | + """ |
| 67 | + |
| 68 | + def call(self, exported_program) -> ExportedProgramPassResult: |
| 69 | + from executorch.exir.passes.reinplace import reinplace_pass |
| 70 | + |
| 71 | + exported_program = reinplace_pass( |
| 72 | + exported_program, ops_to_inplace=set(BACKEND_INPLACE_OPS) |
| 73 | + ) |
| 74 | + return ExportedProgramPassResult(exported_program, True) |
0 commit comments