-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdivisor_reducer.py
32 lines (24 loc) · 1.15 KB
/
divisor_reducer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import torch
from .base_reducer import BaseReducer
class DivisorReducer(BaseReducer):
def unpack_loss_info(self, loss_info):
losses, loss_indices, reduction_type, kwargs = super().unpack_loss_info(
loss_info
)
if reduction_type != "already_reduced":
kwargs = {"divisor": loss_info["divisor"]}
self.divisor = kwargs["divisor"]
self.add_to_recordable_attributes(name="divisor", is_stat=True)
return losses, loss_indices, reduction_type, kwargs
def sum_and_divide(self, losses, embeddings, divisor):
if divisor != 0:
return torch.sum(losses) / divisor
return self.zero_loss(embeddings)
def element_reduction(self, losses, loss_indices, embeddings, labels, divisor=1):
return self.sum_and_divide(losses, embeddings, divisor)
def pos_pair_reduction(self, *args, **kwargs):
return self.element_reduction(*args, **kwargs)
def neg_pair_reduction(self, *args, **kwargs):
return self.element_reduction(*args, **kwargs)
def triplet_reduction(self, *args, **kwargs):
return self.element_reduction(*args, **kwargs)