MCPcopy Create free account
hub / github.com/ChenhongyiYang/QueryDet-PyTorch / checkpoint_sequential

Function checkpoint_sequential

utils/gradient_checkpoint.py:167–229  ·  view source on GitHub ↗

r"""A helper function for checkpointing sequential models. Sequential models execute a list of modules/functions in order (sequentially). Therefore, we can divide such a model in various segments and checkpoint each segment. All segments except the last will run in :func:`torch.no_g

(functions, segments, input, **kwargs)

Source from the content-addressed store, hash-verified

165
166
167def checkpoint_sequential(functions, segments, input, **kwargs):
168 r"""A helper function for checkpointing sequential models.
169
170 Sequential models execute a list of modules/functions in order
171 (sequentially). Therefore, we can divide such a model in various segments
172 and checkpoint each segment. All segments except the last will run in
173 :func:`torch.no_grad` manner, i.e., not storing the intermediate
174 activations. The inputs of each checkpointed segment will be saved for
175 re-running the segment in the backward pass.
176
177 See :func:`~torch.utils.checkpoint.checkpoint` on how checkpointing works.
178
179 .. warning::
180 Checkpointing doesn't work with :func:`torch.autograd.grad`, but only
181 with :func:`torch.autograd.backward`.
182
183 .. warning:
184 At least one of the inputs needs to have :code:`requires_grad=True` if
185 grads are needed for model inputs, otherwise the checkpointed part of the
186 model won't have gradients.
187
188 .. warning:
189 Since PyTorch 1.4, it allows only one Tensor as the input and
190 intermediate outputs, just like :class:`torch.nn.Sequential`.
191
192 Args:
193 functions: A :class:`torch.nn.Sequential` or the list of modules or
194 functions (comprising the model) to run sequentially.
195 segments: Number of chunks to create in the model
196 input: A Tensor that is input to :attr:`functions`
197 preserve_rng_state(bool, optional, default=True): Omit stashing and restoring
198 the RNG state during each checkpoint.
199
200 Returns:
201 Output of running :attr:`functions` sequentially on :attr:`*inputs`
202
203 Example:
204 >>> model = nn.Sequential(...)
205 >>> input_var = checkpoint_sequential(model, chunks, input_var)
206 """
207 # Hack for keyword-only parameter in a python 2.7-compliant way
208 preserve = kwargs.pop('preserve_rng_state', True)
209 if kwargs:
210 raise ValueError("Unexpected keyword arguments: " + ",".join(arg for arg in kwargs))
211
212 def run_function(start, end, functions):
213 def forward(input):
214 for j in range(start, end + 1):
215 input = functions[j](input)
216 return input
217 return forward
218
219 if isinstance(functions, torch.nn.Sequential):
220 functions = list(functions.children())
221
222 segment_size = len(functions) // segments
223 # the last chunk has to be non-volatile
224 end = -1

Callers

nothing calls this directly

Calls 2

checkpointFunction · 0.85
run_functionFunction · 0.85

Tested by

no test coverage detected