Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lightly/loss/barlow_twins_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import torch.distributed as dist
import torch.nn.functional as F
from torch import Tensor
from torch.distributed import nn as dist_nn


class BarlowTwinsLoss(torch.nn.Module):
Expand Down Expand Up @@ -76,8 +77,10 @@ def forward(self, z_a: torch.Tensor, z_b: torch.Tensor) -> torch.Tensor:
if self.gather_distributed and dist.is_initialized():
world_size = dist.get_world_size()
if world_size > 1:
c = c / world_size
dist.all_reduce(c)
# Use the autograd-aware torch.distributed.nn.all_reduce so the
# backward pass scales gradients by world_size, matching
# single-GPU behaviour. ref #1977
c = dist_nn.all_reduce(c / world_size)
Comment thread
saud5150 marked this conversation as resolved.

invariance_loss = torch.diagonal(c).add_(-1).pow_(2).sum()
redundancy_reduction_loss = _off_diagonal(c).pow_(2).sum()
Expand Down
Loading