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)
| 74 | |
| 75 | |
| 76 | def checkpoint(func, inputs, params, flag): |
| 77 | """ |
| 78 | Evaluate a function without caching intermediate activations, allowing for |
| 79 | reduced memory at the expense of extra compute in the backward pass. |
| 80 | :param func: the function to evaluate. |
| 81 | :param inputs: the argument sequence to pass to `func`. |
| 82 | :param params: a sequence of parameters `func` depends on but does not |
| 83 | explicitly take as arguments. |
| 84 | :param flag: if False, disable gradient checkpointing. |
| 85 | """ |
| 86 | if flag: |
| 87 | args = tuple(inputs) + tuple(params) |
| 88 | return CheckpointFunction.apply(func, len(inputs), *args) |
| 89 | else: |
| 90 | return func(*inputs) |
| 91 | |
| 92 | |
| 93 | class AttentionPool2d(nn.Module): |
no test coverage detected
searching dependent graphs…