6

我想知道使用 DDP 时记录指标的正确方法是什么。我注意到如果我想在里面打印一些东西validation_epoch_end,在使用 2 个 GPU 时会打印两次。我希望validation_epoch_end只在 0 级上被调用并接收来自所有 GPU 的输出,但我不确定这是否正确。因此我有几个问题:

  1. validation_epoch_end(self, outputs)- 当使用 DDP 时,每个子进程是否接收从当前 GPU 处理的数据或从所有 GPU 处理的数据,即输入参数是否outputs包含来自所有 GPU 的整个验证集的输出?
  2. 如果是 GPU/进程特定的,那么在使用 DDP 时outputs计算整个验证集上的任何指标的正确方法是什么?validation_epoch_end

我知道我self.global_rank == 0只能在这种情况下通过检查和打印/记录来解决打印问题,但是我试图更深入地了解在这种情况下我正在打印/记录的内容。

这是我的用例中的代码片段。我希望能够报告整个验证数据集的 f1、精度和召回率,我想知道使用 DDP 时正确的做法是什么。

    def _process_epoch_outputs(self,
                               outputs: List[Dict[str, Any]]
                               ) -> Tuple[torch.Tensor, torch.Tensor]:
        """Creates and returns tensors containing all labels and predictions

        Goes over the outputs accumulated from every batch, detaches the
        necessary tensors and stacks them together.

        Args:
            outputs (List[Dict])
        """
        all_labels = []
        all_predictions = []

        for output in outputs:
            for labels in output['labels'].detach():
                all_labels.append(labels)

            for predictions in output['predictions'].detach():
                all_predictions.append(predictions)

        all_labels = torch.stack(all_labels).long().cpu()
        all_predictions = torch.stack(all_predictions).cpu()

        return all_predictions, all_labels

    def validation_epoch_end(self, outputs: List[Dict[str, Any]]) -> None:
        """Logs f1, precision and recall on the validation set."""

        if self.global_rank == 0:
            print(f'Validation Epoch: {self.current_epoch}')

        predictions, labels = self._process_epoch_outputs(outputs)
        for i, name in enumerate(self.label_columns):

            f1, prec, recall, t = metrics.get_f1_prec_recall(predictions[:, i],
                                                             labels[:, i],
                                                             threshold=None)
            self.logger.experiment.add_scalar(f'{name}_f1/Val',
                                              f1,
                                              self.current_epoch)
            self.logger.experiment.add_scalar(f'{name}_Precision/Val',
                                              prec,
                                              self.current_epoch)
            self.logger.experiment.add_scalar(f'{name}_Recall/Val',
                                              recall,
                                              self.current_epoch)

            if self.global_rank == 0:
                print((f'F1: {f1}, Precision: {prec}, '
                       f'Recall: {recall}, Threshold {t}'))
4

1 回答 1

2

问题

validation_epoch_end(self, outputs) - 当使用 DDP 时,每个子进程是否接收从当前 GPU 处理的数据或从所有 GPU 处理的数据,即输入参数输出是否包含来自所有 GPU 的整个验证集的输出?

仅从当前 GPU 处理的数据,输出不同步,只有backward同步(梯度在训练期间同步并分发到驻留在每个 GPU 上的模型的副本)。

想象一下,所有的输出都从1000GPU 传递给这个可怜的主控,它可以很容易地给它一个 OOM

如果输出是 GPU/进程特定的,那么在使用 DDP 时,在 validation_epoch_end 中计算整个验证集的任何指标的正确方法是什么?

根据文件(强调我的):

当使用加速器在 GPU 之间拆分每个批次的数据进行验证时,有时您可能需要在主 GPU上聚合它们以进行处理(dp 或 ddp2)。

这是随附的代码(validation_epoch_end在这种情况下,将通过单步接收跨多个 GPU 的累积数据,另请参阅注释):

# Done per-process (GPU)
def validation_step(self, batch, batch_idx):
    x, y = batch
    y_hat = self.model(x)
    loss = F.cross_entropy(y_hat, y)
    pred = ...
    return {'loss': loss, 'pred': pred}

# Gathered data from all processes (per single step)
# Allows for accumulation so the whole data at the end of epoch
# takes less memory
def validation_step_end(self, batch_parts):
    gpu_0_prediction = batch_parts.pred[0]['pred']
    gpu_1_prediction = batch_parts.pred[1]['pred']

    # do something with both outputs
    return (batch_parts[0]['loss'] + batch_parts[1]['loss']) / 2

def validation_epoch_end(self, validation_step_outputs):
   for out in validation_step_outputs:
       # do something with preds

提示

专注于每个设备的计算和尽可能少的 GPU 间传输

  • 内部validation_step(或者training_step如果这是你想要的,这是一般的)计算f1,,precision以及recall其他任何基于批次的
  • 返回这些值(例如,作为字典)。现在您3将从每个设备返回数字,而不是(batch, outputs)(可能要大得多)
  • 在里面validation_step_end获取这些3值(实际上(2, 3),如果你有 2 个 GPU)并对它们求和/取平均值并返回3
  • 现在validation_epoch_end将获得(steps, 3)可用于累积的值

validation_epoch_end如果您可以在另一个值中累积它们而不是在值列表上进行操作,那就更好了3(假设您有很多验证步骤,列表可能会变得太大),但这应该足够了。

AFAIK PyTorch-Lightning 不这样做(例如,不是添加到list,而是直接应用一些累加器),但我可能弄错了,所以任何更正都会很棒。

于 2021-03-29T15:56:48.913 回答