code: def _multivalent_span_alignment(overlaps):
      """Returns the multivalent span alignment for a given overlaps tensor.
  
      Args:
        overlaps: `<int64>[D1...DB, source_size, target_size]`: `overlaps[b1...bB,
          i, j]` is true if source span `i` overlaps target span `j` (in batch
          `b1...bB`).
  
      Returns:
        `<int64>[D1...DB, source_size, (num_aligned_spans)]`:
        `result[b1...bB, i, n]=j` if target span `j` is the `n`'th target span
        that aligns with source span `i` (in batch `b1...bB`).
      """
      overlaps_ndims = overlaps.shape.ndims
      assert (
          overlaps_ndims is not None
      )  # guaranteed/checked by span_overlaps()
      assert overlaps_ndims >= 2
  
      # If there are multiple batch dimensions, then flatten them and recurse.
      if overlaps_ndims > 3:
          if not isinstance(overlaps, ragged_tensor.RaggedTensor):
              overlaps = ragged_tensor.RaggedTensor.from_tensor(
                  overlaps, ragged_rank=overlaps.shape.ndims - 3
              )
          return overlaps.with_values(
              _multivalent_span_alignment(overlaps.values)
          )
  
      elif overlaps_ndims == 2:  # no batch dimension
          assert not isinstance(overlaps, ragged_tensor.RaggedTensor)
          overlap_positions = array_ops.where(overlaps)
          return ragged_tensor.RaggedTensor.from_value_rowids(
              values=overlap_positions[:, 1],
              value_rowids=overlap_positions[:, 0],
              nrows=array_ops.shape(overlaps, out_type=dtypes.int64)[0],
          )
  
      else:  # batch dimension
          if not isinstance(overlaps, ragged_tensor.RaggedTensor):
              overlaps = ragged_tensor.RaggedTensor.from_tensor(
                  overlaps, ragged_rank=1
              )
          overlap_positions = ragged_where_op.where(overlaps.values)
          if isinstance(overlaps.values, ragged_tensor.RaggedTensor):
              overlaps_values_nrows = overlaps.values.nrows()
          else:
              overlaps_values_nrows = array_ops.shape(
                  overlaps.values, out_type=dtypes.int64
              )[0]
          return overlaps.with_values(
              ragged_tensor.RaggedTensor.from_value_rowids(
                  values=overlap_positions[:, 1],
                  value_rowids=overlap_positions[:, 0],
                  nrows=overlaps_values_nrows,
              )
          )
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import functools
  from tensorflow.python.framework import dtypes
  from tensorflow.python.framework import ops
  from tensorflow.python.framework import tensor
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import array_ops_stack
  from tensorflow.python.ops import check_ops
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops.ragged import ragged_functional_ops
  from tensorflow.python.ops.ragged import ragged_gather_ops
  from tensorflow.python.ops.ragged import ragged_math_ops
  from tensorflow.python.ops.ragged import ragged_tensor
  from tensorflow.python.ops.ragged import ragged_where_op
  from tensorflow.python.ops.ragged import segment_id_ops
label_desc: Use image node_id 7 for calling a global function with example usage: _multivalent_span_alignment(overlaps) and returns: overlaps, ragged_tensor, overlaps