code: def _safe_mean_terms(terms):
      mask = (
          torch.isnan(terms)
          | (terms == float("-inf"))
          | (terms == float("inf"))
      )
      if terms.dtype is torch.float32:
          nonnan = (~mask).sum(0).float()
      elif terms.dtype is torch.float64:
          nonnan = (~mask).sum(0).double()
      terms[mask] = 0.0
      loss = terms.sum(0) / nonnan
      agg_loss = loss.sum()
      return agg_loss, loss
  
imports: import math
  import warnings
  import torch
  import pyro
  from pyro import poutine
  from pyro.contrib.oed.search import Search
  from pyro.contrib.util import lexpand
  from pyro.infer import SVI, EmpiricalMarginal, Importance
  from pyro.infer.autoguide.utils import mean_field_entropy
  from pyro.util import torch_isinf, torch_isnan
label_desc: Use image node_id 22 for calling a global function with example usage: _safe_mean_terms(terms) and returns: agg_loss,  loss