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 parameters
(func, inputs, params, flag)
| 15 | |
| 16 | |
| 17 | def checkpoint(func, inputs, params, flag): |
| 18 | """ |
| 19 | Evaluate a function without caching intermediate activations, allowing for |
| 20 | reduced memory at the expense of extra compute in the backward pass. |
| 21 | :param func: the function to evaluate. |
| 22 | :param inputs: the argument sequence to pass to `func`. |
| 23 | :param params: a sequence of parameters `func` depends on but does not |
| 24 | explicitly take as arguments. |
| 25 | :param flag: if False, disable gradient checkpointing. |
| 26 | """ |
| 27 | if flag: |
| 28 | args = tuple(inputs) + tuple(params) |
| 29 | return CheckpointFunction.apply(func, len(inputs), *args) |
| 30 | else: |
| 31 | return func(*inputs) |
| 32 | |
| 33 | |
| 34 | class CheckpointFunction(torch.autograd.Function): |
no outgoing calls
no test coverage detected