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
6 changes: 6 additions & 0 deletions swift/loss/embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def __call__(self, outputs, labels, **kwargs) -> torch.Tensor:
rank, _, world_size, _ = get_dist_setting()
# repeat of anchor(1)+positive(1)+negatives(n)
sentences = outputs['last_hidden_state']
gradient_scale = 1

if world_size > 1 and use_batch:
if getattr(sequence_parallel, 'dp_group', None) is not None:
Expand Down Expand Up @@ -163,6 +164,8 @@ def __call__(self, outputs, labels, **kwargs) -> torch.Tensor:
labels = gather_object(labels)
# override the gathered one
all_sentences[rank] = sentences
if not self.is_megatron:
gradient_scale = len(all_sentences)
for idx in range(len(all_sentences)):
if idx == rank:
continue
Expand Down Expand Up @@ -321,4 +324,7 @@ def __call__(self, outputs, labels, **kwargs) -> torch.Tensor:
# next positive is neg+1
length += tensor.size(0) - 1
loss /= len(split_tensors)
if gradient_scale > 1:
# DDP averages the local contributions to the global loss graph; preserve the reported loss.
loss = loss.detach() + gradient_scale * (loss - loss.detach())
return loss
Loading