Evaluate a function without caching intermediate activations, allowing for reduced memory at the expense of extra compute in the backward pass. :param func: the function to evaluate. :param inputs: the argument sequence to pass to `func`. :param params: a sequence of parame
(func, inputs, params, flag)
| 23 | ################################################################################# |
| 24 | |
| 25 | def checkpoint(func, inputs, params, flag): |
| 26 | """ |
| 27 | Evaluate a function without caching intermediate activations, allowing for |
| 28 | reduced memory at the expense of extra compute in the backward pass. |
| 29 | :param func: the function to evaluate. |
| 30 | :param inputs: the argument sequence to pass to `func`. |
| 31 | :param params: a sequence of parameters `func` depends on but does not |
| 32 | explicitly take as arguments. |
| 33 | :param flag: if False, disable gradient checkpointing. |
| 34 | """ |
| 35 | if flag: |
| 36 | args = tuple(inputs) + tuple(params) |
| 37 | return CheckpointFunction.apply(func, len(inputs), *args) |
| 38 | else: |
| 39 | return func(*inputs) |
| 40 | |
| 41 | |
| 42 | class CheckpointFunction(torch.autograd.Function): |
nothing calls this directly
no outgoing calls
no test coverage detected