|
| 1 | +from typing import Any, Callable, cast |
| 2 | + |
| 3 | +import torch |
| 4 | +import torch.library |
| 5 | + |
| 6 | +# Libraries used to store the ops definitions |
| 7 | +library = torch.library.Library("ppe", "DEF") |
| 8 | +library_impl = torch.library.Library("ppe", "IMPL", "CompositeExplicitAutograd") |
| 9 | +library_autograd_impl = torch.library.Library("ppe", "IMPL", "Autograd") |
| 10 | +library_meta_impl = torch.library.Library("ppe", "IMPL", "Meta") |
| 11 | + |
| 12 | + |
| 13 | +class OpDesc: |
| 14 | + """Metadata to register an op to torch.library. |
| 15 | +
|
| 16 | + Attributes: |
| 17 | + op (callable): code to be executed in the forward/backward of the op. |
| 18 | + meta (callable): function to perform shape inference for forward/backward |
| 19 | + passes. |
| 20 | + signature (str): Arguments and return type of the function |
| 21 | + ``"(Tensor a, Tensor b) -> Tensor[]"``. |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__( |
| 25 | + self, |
| 26 | + op: Callable[..., Any], |
| 27 | + meta: Callable[..., Any], |
| 28 | + signature: str, |
| 29 | + ) -> None: |
| 30 | + self.op = op |
| 31 | + self.meta = meta |
| 32 | + self.signature = signature |
| 33 | + |
| 34 | + |
| 35 | +def _get_autograd(name: str) -> Callable[..., Any]: |
| 36 | + class RunBackward(torch.autograd.Function): |
| 37 | + @staticmethod |
| 38 | + def forward(ctx, *args, **kwargs): # type: ignore[no-untyped-def] |
| 39 | + ctx.save_for_backward(*args) |
| 40 | + op_h = torch._C._dispatch_find_schema_or_throw( |
| 41 | + f"ppe::{name}_fwd", "" |
| 42 | + ) |
| 43 | + return torch._C._dispatch_call_boxed(op_h, *args, **kwargs) |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def backward(ctx, *args): # type: ignore[no-untyped-def] |
| 47 | + i_args = tuple(ctx.saved_tensors) |
| 48 | + op_h = torch._C._dispatch_find_schema_or_throw( |
| 49 | + f"ppe::{name}_bwd", "" |
| 50 | + ) |
| 51 | + return torch._C._dispatch_call_boxed(op_h, *(args + i_args), **{}) |
| 52 | + |
| 53 | + return cast(Callable[..., Any], RunBackward.apply) |
| 54 | + |
| 55 | + |
| 56 | +def register( |
| 57 | + name: str, |
| 58 | + fwd_op: OpDesc, |
| 59 | + bwd_op: OpDesc, |
| 60 | +) -> None: |
| 61 | + """ |
| 62 | + Register a custom op under ``torch.ops.ppe.name`` |
| 63 | +
|
| 64 | + The function appears as a primitive op in the forward and backward |
| 65 | + ``torch.fx.Graph``s after compiling torch code with `aot_autograd` backend. |
| 66 | + Note that for backward functions, all the arguments of the backward pass |
| 67 | + together with the forward arguments are passed to it. This means if forward had |
| 68 | + ``fwd_op(x, y)`` ``x,y`` arguments, the custom bwd_op needs to have a |
| 69 | + signature like``bwd_op(grad_output, x, y)`` |
| 70 | +
|
| 71 | + Arguments: |
| 72 | + name (str): name of the op, shows how it is registered in ``torch.ops.ppe``. |
| 73 | + fwd_op (ppe.ops.OpDesc): code that is executed in the forward pass |
| 74 | + bwd_op (ppe.ops.OpDesc): code that is executed in the backward pass |
| 75 | + """ |
| 76 | + function_sig = f"{name}{fwd_op.signature}" |
| 77 | + function_fwd_sig = f"{name}_fwd{fwd_op.signature}" |
| 78 | + function_bwd_sig = f"{name}_bwd{bwd_op.signature}" |
| 79 | + for s in (function_sig, function_fwd_sig, function_bwd_sig): |
| 80 | + library.define(s) |
| 81 | + |
| 82 | + def function(*args): # type: ignore[no-untyped-def] |
| 83 | + op_h = torch._C._dispatch_find_schema_or_throw(f"ppe::{name}_fwd", "") |
| 84 | + return torch._C._dispatch_call_boxed(op_h, *args, **{}) |
| 85 | + |
| 86 | + library_impl.impl(name, function) |
| 87 | + library_impl.impl(f"{name}_fwd", fwd_op.op) |
| 88 | + library_impl.impl(f"{name}_bwd", bwd_op.op) |
| 89 | + library_meta_impl.impl(f"{name}_fwd", fwd_op.meta) |
| 90 | + library_meta_impl.impl(f"{name}_bwd", bwd_op.meta) |
| 91 | + library_autograd_impl.impl(name, _get_autograd(name)) |
0 commit comments