CLIP4Clip icon indicating copy to clipboard operation
CLIP4Clip copied to clipboard

Problem about Multi-GPU-Train architecture

Open zsnoob opened this issue 1 year ago • 0 comments

In main_task_retrieval.py, fuction "train_epoch", we can see:

        if n_gpu > 1:
            loss = loss.mean()  # mean() to average on multi-gpu.

But in modeling.py, there is:

        if self.training:
            visual_output = allgather(visual_output, self.task_config)
            video_mask = allgather(video_mask, self.task_config)
            sequence_output = allgather(sequence_output, self.task_config)
            torch.distributed.barrier()

which means ALL GPUs' results get synchronized here. For instance, when my batch size is 128, i have 2 GPU to train the model and before the code above, every output's 0th dimension is 64, half of 128. And after this code block, they will be aggregated and output's size will be 128. So, when we get loss value in main_task, there is only a scalar tensor. Looks like there is no need to use "loss.mean()". I have some print results:

1700469492366

        if n_gpu > 1:
            print(loss)
            print(loss.size())
            loss = loss.mean()  # mean() to average on multi-gpu.
            print(loss)
            print(loss.size())

zsnoob avatar Nov 20 '23 08:11 zsnoob