@@ -52,7 +52,7 @@ How to use
52
52
^^^^^^^^^^
53
53
Take the following steps:
54
54
1. Subclass :class: `~Function ` and implement the :meth: `~Function.forward `,
55
- (optional) `` setup_context ` ` and
55
+ (optional) :meth: ` ~Function. setup_context ` and
56
56
:meth: `~Function.backward ` methods.
57
57
2. Call the proper methods on the `ctx ` argument.
58
58
3. Declare whether your function supports
@@ -73,12 +73,12 @@ Take the following steps:
73
73
tensors if there are multiple outputs. Also, please refer to the
74
74
docs of :class: `Function ` to find descriptions of useful methods that can be
75
75
called only from :meth: `~Function.forward `.
76
- - `` setup_context ` ` (optional). One can either write a "combined" :meth: `~Function.forward ` that
76
+ - :meth: ` ~Function. setup_context ` (optional). One can either write a "combined" :meth: `~Function.forward ` that
77
77
accepts a ``ctx `` object or (as of PyTorch 2.0) a separate :meth: `~Function.forward ` that does
78
- not accept ``ctx `` and a `` setup_context ` ` method where the ``ctx `` modification happens.
79
- The :meth: `~Function.forward ` should have the compute and `` setup_context ` ` should
78
+ not accept ``ctx `` and a :meth: ` ~Function. setup_context ` method where the ``ctx `` modification happens.
79
+ The :meth: `~Function.forward ` should have the compute and :meth: ` ~Function. setup_context ` should
80
80
only be responsible for the ``ctx `` modification (and not have any compute).
81
- In general the separate :meth: `~Function.forward ` and `` setup_context ` ` is closer to how
81
+ In general the separate :meth: `~Function.forward ` and :meth: ` ~Function. setup_context ` is closer to how
82
82
PyTorch native operations work and therefore more composable with various PyTorch subsystems.
83
83
See :ref: `combining-forward-context ` for more details.
84
84
- :meth: `~Function.backward ` (or :meth: `~Function.vjp `) defines the gradient formula.
@@ -234,7 +234,7 @@ And here, we optimize the above example by calling set_materialize_grads(False):
234
234
return grad_output * ctx.constant, None
235
235
236
236
If you need any "intermediate" Tensors computed in :meth: `~Function.forward ` to be saved,
237
- either they must be returned as outputs, or combine ``forward `` and `` setup_context ` `
237
+ either they must be returned as outputs, or combine ``forward `` and :meth: ` ~Function. setup_context `
238
238
(see :ref: `combining-forward-context `).
239
239
Note that this means if you want gradients to flow through those intermediate values, you
240
240
need to define the gradient formula for them (see also
@@ -300,25 +300,25 @@ can use the ``gradgradcheck`` function from the same package to check higher ord
300
300
301
301
.. _combining-forward-context :
302
302
303
- Combined or separate :meth: `~Function.forward ` and `` setup_context ` `
304
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
303
+ Combined or separate :meth: `~Function.forward ` and :meth: ` ~Function. setup_context `
304
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
305
305
306
306
There are two main ways to define :class: `~Function `. Either:
307
307
308
- - define a :meth: `~Function.forward ` that combines the forward compute logic with `` setup_context ` `
309
- - (as of PyTorch 2.0) define a separate :meth: `~Function.forward ` and `` setup_context ``.
308
+ - define a :meth: `~Function.forward ` that combines the forward compute logic with :meth: ` ~Function. setup_context `
309
+ - (as of PyTorch 2.0) define a separate :meth: `~Function.forward ` and :meth: ` ~Function. setup_context `
310
310
311
- We recommend the second option (separate :meth: `~Function.forward ` and `` setup_context ` `)
311
+ We recommend the second option (separate :meth: `~Function.forward ` and :meth: ` ~Function. setup_context `)
312
312
because that is closer to how PyTorch native operations are implemented and it composes
313
313
with :mod: `torch.func ` transforms. However, we plan to support both approaches going forward;
314
- combining :meth: `~Function.forward ` with `` setup_context ` `: leads to more flexibility since
314
+ combining :meth: `~Function.forward ` with :meth: ` ~Function. setup_context `: leads to more flexibility since
315
315
you are able to save intermediates without returning them as output.
316
316
317
317
Please see the previous section for how to define :class: `~Function ` with separate
318
- :meth: `~Function.forward ` and `` setup_context ` `.
318
+ :meth: `~Function.forward ` and :meth: ` ~Function. setup_context `.
319
319
320
320
Here is an example of how to define a :class: `Function ` with combined :meth: `~Function.forward ` and
321
- `` setup_context ` `::
321
+ :meth: ` ~Function. setup_context `::
322
322
323
323
class LinearFunction(Function):
324
324
@staticmethod
0 commit comments