code: def reference_loss_func(
      loss_sum_or_avg: torch.Tensor,
      num_measurements: torch.Tensor,
      take_avg_loss: bool,
  ):
      """
      Returns average loss for data from``loss_sum_or_avg``. This function sums all losses from ``loss_sum_or_avg`` and
      divides the sum by the sum of ``num_measurements`` elements.
  
      If ``take_avg_loss`` is ``True`` then ``loss_sum_or_avg[i]`` elements are mean values of ``num_measurements[i]``
      losses. In that case before computing sum of losses each element of ``loss_sum_or_avg`` is multiplied by
      corresponding element of ``num_measurements``.
  
      If ``num_measurements`` sum is zero then the function returns NaN tensor.
  
      The function is used for testing ``nemo.collections.common.metrics.GlobalAverageLossMetric`` class.
  
      Args:
          loss_sum_or_avg: a one dimensional float ``torch.Tensor``. Sums or mean values of loss.
          num_measurements: a one dimensional integer ``torch.Tensor``. Number of values on which sums of means in
              ``loss_sum_or_avg`` are calculated.
          take_avg_loss: if ``True`` then ``loss_sum_or_avg`` contains mean losses else ``loss_sum_or_avg`` contains
              sums of losses.
      """
      loss_sum_or_avg = loss_sum_or_avg.clone().detach()
      if take_avg_loss:
          loss_sum_or_avg *= num_measurements
      nm_sum = num_measurements.sum()
      if nm_sum.eq(0):
          return torch.tensor(float("nan"))
      return loss_sum_or_avg.sum() / nm_sum
  
imports: import os
  import pickle
  import sys
  from functools import partial
  from typing import Callable, Optional
  import numpy
  import pytest
  import torch
  from scipy.stats import entropy
  from torch.distributions.utils import logits_to_probs
  from torch.multiprocessing import Pool, set_start_method
  from torchmetrics import Metric
  from nemo.collections.common.metrics import GlobalAverageLossMetric, Perplexity
label_desc: Use image node_id 6 for calling a global function with example usage: reference_loss_func(loss_sum_or_avg, num_measurements, take_avg_loss) and returns: unknown, torch