(self, forward_input)
| 300 | return idxs |
| 301 | |
| 302 | def forward(self, forward_input): |
| 303 | # We need to offset the seed by the microbatch ID. Save it in a local var to |
| 304 | # ensure it is preserved in the closure. Otherwise checkpointed forward funcs |
| 305 | # will see a different offset. |
| 306 | self.micro_offset += 1 |
| 307 | |
| 308 | def exec_range_func(start, end): |
| 309 | ''' Helper function to be used with checkpoint() |
| 310 | Adapted from torch.utils.checkpoint:checkpoint_sequential() |
| 311 | ''' |
| 312 | local_micro_offset = self.micro_offset + 1 |
| 313 | |
| 314 | def exec_func(*inputs): |
| 315 | # Single tensor inputs need to be unwrapped |
| 316 | if len(inputs) == 1: |
| 317 | inputs = inputs[0] |
| 318 | for idx, layer in enumerate(self.forward_funcs[start:end]): |
| 319 | self.curr_layer = idx + self._local_start |
| 320 | if self.seed_layers: |
| 321 | new_seed = (self.base_seed * local_micro_offset) + self.curr_layer |
| 322 | if self.seed_fn: |
| 323 | self.seed_fn(new_seed) |
| 324 | else: |
| 325 | ds_utils.set_random_seed(new_seed) |
| 326 | |
| 327 | inputs = layer(inputs) |
| 328 | return inputs |
| 329 | |
| 330 | return exec_func |
| 331 | |
| 332 | if self.activation_checkpoint_interval == 0: |
| 333 | func = exec_range_func(0, len(self.forward_funcs)) |
| 334 | x = func(forward_input) |
| 335 | else: |
| 336 | num_layers = len(self.forward_funcs) |
| 337 | x = forward_input |
| 338 | for start_idx in range(0, num_layers, self.activation_checkpoint_interval): |
| 339 | end_idx = min(start_idx + self.activation_checkpoint_interval, num_layers) |
| 340 | |
| 341 | funcs = self.forward_funcs[start_idx:end_idx] |
| 342 | # Since we either pass tensors or tuples of tensors without unpacking, we |
| 343 | # need to be careful not to double-wrap tensors with tuple. |
| 344 | if not isinstance(x, tuple): |
| 345 | x = (x, ) |
| 346 | |
| 347 | if self._is_checkpointable(funcs): |
| 348 | x = self.activation_checkpoint_func(exec_range_func(start_idx, end_idx), *x) |
| 349 | else: |
| 350 | x = exec_range_func(start_idx, end_idx)(*x) |
| 351 | return x |
| 352 | |
| 353 | def _partition_layers(self, method='uniform'): |
| 354 | num_stages = self._topo.get_dim('pipe') |
nothing calls this directly
no test coverage detected