afnio.autodiff.function
afnio.autodiff.function.Function
Base class to create custom autodiff Function.
To create a custom Function, subclass this class and implement the
forward and backward static methods. Then, to use your
custom op in the forward pass, call the class method apply.
Do not call forward directly.
Examples:
>>> class Func(Function):
>>> @staticmethod
>>> def forward(ctx, x: afnio.Variable):
>>> reverse = x.data[::-1]
>>> out = afnio.Variable(data=reverse, role=x.role, requires_grad=True)
>>> ctx.save_for_backward(x, reverse, out)
>>> return out
>>>
>>> @staticmethod
>>> def backward(ctx, grad_out):
>>> x, reverse, out = ctx.saved_variables
>>> grad = f"Here is the feedback for {x.role} (reversed): {grad_out.grad}"
>>> role = f"Feedback to {x.role}"
>>> x.grad = afnio.Variable(data=grad, role=role)
>>> return x.grad
>>>
>>> a = afnio.Variable(data="This is a string", role="Input string", requires_grad=True)
>>> c = Func.apply(a)
Source code in afnio/autodiff/function.py
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
forward(*args, **kwargs)
staticmethod
Define the forward of the custom autodiff Function.
This function is to be overridden by all subclasses. There are two ways to define forward:
Examples:
Usage 1 (Combined forward and ctx):
>>> @staticmethod
>>> def forward(ctx: Any, *args: Any, **kwargs: Any) -> Any:
... pass
- It must accept a context
ctxas the first argument, followed by any number of arguments (Variables or other types).
Usage 2 (Separate forward and ctx):
>>> @staticmethod
>>> def forward(*args: Any, **kwargs: Any) -> Any:
... pass
>>> @staticmethod
>>> def setup_context(ctx: Any, inputs: Tuple[Any, ...], output: Any) -> None:
... pass
- The forward no longer accepts a
ctxargument. - Instead, you must also override the
afnio.autodiff.Function.setup_contextstaticmethod to handle setting up thectxobject.outputis the output of the forward,inputsare a Tuple of inputs to the forward.
The context can be used to store arbitrary data that can be then retrieved
during the backward pass. Variables should not be stored
directly on ctx. Instead, Variables should be saved either with
ctx.save_for_backward if they are intended to be used in backward.
Source code in afnio/autodiff/function.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
setup_context(ctx, inputs, output)
staticmethod
There are two ways to define the forward pass of an autodiff Function.
Either:
- Override forward with the signature
forward(ctx, *args, **kwargs).setup_contextis not overridden. Setting up thectxfor backward happens inside theforward. - Override forward with the signature
forward(*args, **kwargs)and overridesetup_context. Setting up thectxfor backward happens insidesetup_context(as opposed to inside theforward)
Source code in afnio/autodiff/function.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
backward(ctx, *grad_outputs)
staticmethod
Define a formula for differentiating the operation with backward mode automatic differentiation.
This function is to be overridden by all subclasses.
It must accept a context ctx as the first argument, followed by
as many outputs as the forward returned (None will be passed in
for non variable outputs of the forward function), and it should return as many
variables, as there were inputs to forward. Each argument is the
gradient w.r.t the given output, and each returned value should be the gradient
w.r.t. the corresponding input. If an input is not a
Variable or is a Variable
not requiring grads, you can just pass None as a gradient for that input.
The context can be used to retrieve variables saved during the forward
pass. It also has an attribute ctx.needs_input_grad as a tuple of booleans
representing whether each input needs gradient. E.g., backward
will have ctx.needs_input_grad[0] = True if the first input to
forward needs gradient computed w.r.t. the output.
Source code in afnio/autodiff/function.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
apply(*args, **kwargs)
classmethod
Applies the forward function of the custom Function class.
This method handles cases where setup_context is defined
to set up the ctx (context) object separately or within the
forward method itself.
Source code in afnio/autodiff/function.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |