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)
| 100 | |
| 101 | |
| 102 | def checkpoint(func, inputs, params, flag): |
| 103 | """ |
| 104 | Evaluate a function without caching intermediate activations, allowing for |
| 105 | reduced memory at the expense of extra compute in the backward pass. |
| 106 | :param func: the function to evaluate. |
| 107 | :param inputs: the argument sequence to pass to `func`. |
| 108 | :param params: a sequence of parameters `func` depends on but does not |
| 109 | explicitly take as arguments. |
| 110 | :param flag: if False, disable gradient checkpointing. |
| 111 | """ |
| 112 | if flag: |
| 113 | args = tuple(inputs) + tuple(params) |
| 114 | return CheckpointFunction.apply(func, len(inputs), *args) |
| 115 | else: |
| 116 | return func(*inputs) |
| 117 | |
| 118 | |
| 119 | class CheckpointFunction(torch.autograd.Function): |
no outgoing calls
no test coverage detected