forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_ops_1.py
More file actions
58 lines (42 loc) · 1.56 KB
/
custom_ops_1.py
File metadata and controls
58 lines (42 loc) · 1.56 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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.
"""Example of showcasing registering custom operator through torch library API."""
import torch
from examples.portable.scripts.export import export_to_exec_prog, save_pte_program
from executorch.exir import EdgeCompileConfig
from torch.library import impl, Library
my_op_lib = Library("my_ops", "DEF")
# registering an operator that multiplies input tensor by 3 and returns it.
my_op_lib.define("mul3(Tensor input) -> Tensor") # should print 'mul3'
@impl(my_op_lib, "mul3", dispatch_key="CompositeExplicitAutograd")
def mul3_impl(a: torch.Tensor) -> torch.Tensor:
return a * 3
# registering the out variant.
my_op_lib.define(
"mul3.out(Tensor input, *, Tensor(a!) output) -> Tensor(a!)"
) # should print 'mul3.out'
@impl(my_op_lib, "mul3.out", dispatch_key="CompositeExplicitAutograd")
def mul3_out_impl(a: torch.Tensor, *, out: torch.Tensor) -> torch.Tensor:
out.copy_(a)
out.mul_(3)
return out
# example model
class Model(torch.nn.Module):
def forward(self, a):
return torch.ops.my_ops.mul3.default(a)
def main():
m = Model()
input = torch.randn(2, 3)
# capture and lower
model_name = "custom_ops_1"
prog = export_to_exec_prog(
m,
(input,),
edge_compile_config=EdgeCompileConfig(_check_ir_validity=False),
)
save_pte_program(prog, model_name)
if __name__ == "__main__":
main()