(self, method='uniform')
| 393 | return x |
| 394 | |
| 395 | def _partition_layers(self, method='uniform'): |
| 396 | num_stages = self.num_stages |
| 397 | stage_id = self._topo.get_coord(self.global_rank).pipe |
| 398 | |
| 399 | if self.global_rank == 0: |
| 400 | logger.info(f'Partitioning pipeline stages with method {method}') |
| 401 | |
| 402 | method = method.lower() |
| 403 | |
| 404 | # Each stage gets a simple uniform number of layers. |
| 405 | if method == 'uniform': |
| 406 | num_layers = len(self._layer_specs) |
| 407 | self.parts = ds_utils.partition_uniform(num_items=num_layers, num_parts=num_stages) |
| 408 | elif method == 'parameters': |
| 409 | param_counts = self._count_layer_params() |
| 410 | self.parts = ds_utils.partition_balanced(weights=param_counts, num_parts=num_stages) |
| 411 | elif method.startswith('type:'): |
| 412 | layertype = method.split(':')[1] |
| 413 | binary_weights = [0] * len(self._layer_specs) |
| 414 | for idx in self._find_layer_type(layertype): |
| 415 | binary_weights[idx] = 1 |
| 416 | self.parts = ds_utils.partition_balanced(weights=binary_weights, num_parts=num_stages) |
| 417 | elif method == 'profile': |
| 418 | raise NotImplementedError(f'Partitioning method {method} not implemented.') |
| 419 | else: |
| 420 | raise NotImplementedError(f'Partitioning method {method} not implemented.') |
| 421 | |
| 422 | # Print some information on the partitioning. |
| 423 | if self.global_rank == 0: |
| 424 | for stage in range(num_stages): |
| 425 | start = self.parts[stage] |
| 426 | stop = self.parts[stage + 1] |
| 427 | print(f'stage={stage} layers={stop - start}') |
| 428 | for idx, layer in enumerate(self._layer_specs[start:stop]): |
| 429 | name = str(layer) |
| 430 | if isinstance(layer, LayerSpec): |
| 431 | name = layer.typename.__name__ |
| 432 | if isinstance(layer, nn.Module): |
| 433 | name = layer.__class__.__name__ |
| 434 | else: |
| 435 | try: |
| 436 | name = layer.__name__ |
| 437 | except AttributeError: |
| 438 | pass |
| 439 | print(f' {idx+start:2d}: {name}') |
| 440 | if self.loss_fn: |
| 441 | try: |
| 442 | print(f' loss: {self.loss_fn.__name__}') |
| 443 | except AttributeError: |
| 444 | print(f' loss: {self.loss_fn.__class__.__name__}') |
| 445 | |
| 446 | self._set_bounds(start=self.parts[stage_id], stop=self.parts[stage_id + 1]) |
| 447 | |
| 448 | @staticmethod |
| 449 | def _recursive_getattr(module: torch.nn.Module, attr_name: str) -> torch.Tensor: |
no test coverage detected