code: def _safe_coalesce(t):
      tc = t.coalesce()
      value_map = {}
      for idx, val in zip(t._indices().t(), t._values()):
          idx_tup = tuple(idx)
          if idx_tup in value_map:
              value_map[idx_tup] += val
          else:
              value_map[idx_tup] = (
                  val.clone() if torch.is_tensor(val) else val
              )
  
      new_indices = sorted(list(value_map.keys()))
      new_values = [value_map[idx] for idx in new_indices]
      if t._values().dim() < 2:
          new_values = t._values().new_tensor(new_values)
      else:
          new_values = torch.stack(new_values)
  
      new_indices = t._indices().new_tensor(new_indices).t()
      tg = t.new(new_indices, new_values, t.size())
  
      assert (tc._indices() == tg._indices()).all()
      assert (tc._values() == tg._values()).all()
      return tg
  
imports: import contextlib
  import numbers
  import os
  import re
  import warnings
  from itertools import product
  import numpy
  import pytest
  import torch
  import torch.cuda
  from numpy.testing import assert_allclose
  from pytest import approx
label_desc: Use image node_id 13 for calling a global function with example usage: _safe_coalesce(t) and returns: tg