forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace_broken_ops_with_function_ops_pass.py
More file actions
42 lines (35 loc) · 1.5 KB
/
replace_broken_ops_with_function_ops_pass.py
File metadata and controls
42 lines (35 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
# pyre-strict
import torch
from executorch.exir.pass_base import ExportPass
class ReplaceBrokenOpsWithFunctionalOpsPass(ExportPass):
"""
TODO: Our backend expects pure functions. However, some operators
are not functionalized properly. This pass intends to replace
non-functionalized operators with their functionalized variant.
TODO: this can be refactors into a general OpReplacementPass
"""
# pyre-ignore
def call_operator(self, op, args, kwargs, meta):
if op.is_view:
namespace, op_full_name = op.name().split("::")
split = op_full_name.split(".")
if len(split) == 2:
op_name, overload_name = split[0], split[1]
elif len(split) == 1:
# Add default overload if no overload listed
op_name = op_full_name
overload_name = "default"
else:
raise RuntimeError(
f"Invalid op name expected only one '.' to be present: {op_full_name}"
)
view_copy_op = getattr(
getattr(getattr(torch.ops, namespace), f"{op_name}_copy"), overload_name
)
return super().call_operator(view_copy_op, args, kwargs, meta)
return super().call_operator(op, args, kwargs, meta)