def _to_global_document_frequency(
    x: tf.SparseTensor, vocab_size: Union[int, tf.Tensor]) -> tf.Tensor:
  """Summerizes term/doc one-hot tensor to get document frequency of each term.

  Args:
    x: a SparseTensor of size (batch_size, vocab_size) and values 0/1 to
      indicate to existence of each term in each document. x is expected to be
      the output of _to_term_document_one_hot.
    vocab_size: A scalar int64 Tensor - the count of vocab used to turn the
      string into int64s including any OOV buckets.

  Returns:
    a tensor with indices as (1, <vocab_index>) and values as the count of
      documents in the entire dataset that contain each vocab term.
  """
  # term_doc_freq is the one-hot encoding of term existence in each document.
  # It is a (batch_size, vocab_size)-shaped, 0/1 valued sparse tensor.
  term_doc_one_hot = _to_term_document_one_hot(x, vocab_size)

  # Reduce sum the one-hot tensor within each mini batch to get one
  # (1, vocab_size)-shaped sparse tensor for each mini batch, with the value
  # being the count of documents containing each term in that batch.
  count_docs_with_term = tf.sparse.reduce_sum(
      term_doc_one_hot, axis=0, keepdims=True)

  # Sum up all batches to get a (1, vocab_size)-shaped sparse tensor storing
  # count of documents containing each term across the entire dataset.
  return analyzers.sum(count_docs_with_term, reduce_instance_dims=False)
Summerizes term/doc one-hot tensor to get document frequency of each term.

  Args:
    x: a SparseTensor of size (batch_size, vocab_size) and values 0/1 to
      indicate to existence of each term in each document. x is expected to be
      the output of _to_term_document_one_hot.
    vocab_size: A scalar int64 Tensor - the count of vocab used to turn the
      string into int64s including any OOV buckets.

  Returns:
    a tensor with indices as (1, <vocab_index>) and values as the count of
      documents in the entire dataset that contain each vocab term.
  
tensorflow_transform/experimental/mappers.py