code: def _aggregate_loss(
      self, loss: Tensor, loss_mask: Optional[BoolTensor]
  ) -> Tensor:
      batch_size = loss.size(0)
      if loss_mask is None:
          length = loss.size(1)
          loss = loss.view(batch_size, -1).sum(dim=1)
          if length > 0:
              loss = loss / length
      else:
          loss = loss.masked_fill(
              loss_mask.view(*loss_mask.shape, 1, 1), 0.0
          )
          loss = loss.view(batch_size, -1).sum(dim=1)
          loss_count = loss_mask.logical_not().to(pt.float).sum(dim=1)
          loss = pt.where(loss_count.eq(0), loss, loss.div(loss_count))
      return loss
  
imports: from typing import TYPE_CHECKING, Optional
  import torch
  from torch import Tensor, BoolTensor
  from torch.nn import functional
label_desc: Use image node_id 2 for calling the LossFunction obj's underlying member method code with example usage: obj._aggregate_loss(loss, loss_mask) and returns: loss