def validate_varlen_sparse_value(
    name: str, batched_value: common_types.SparseTensorValueType
):
  """Checks that the given SparseTensor is 2-D ragged and left-aligned."""
  indices = np.asarray(batched_value.indices)
  if indices.shape[1] != 2:
    raise ValueError(f'Encountered non 2-D varlen sparse feature {name}')
  if indices.shape[0] == 0:
    return
  indices_diff = np.diff(indices, axis=0)
  instance_index_diff, value_index_diff = indices_diff[:, 0], indices_diff[:, 1]
  if np.any(instance_index_diff < 0):
    raise ValueError(
        f'Encountered decreasing instance indices for feature {name}: {indices}'
    )
  if np.any(np.logical_and(instance_index_diff == 0, value_index_diff != 1)):
    raise ValueError(
        f'Encountered non-consecutive value indices for feature {name}:'
        f' {indices}'
    )
  (instance_boundaries,) = np.where(instance_index_diff != 0)
  if np.any(indices[np.append(instance_boundaries + 1, 0), 1] != 0):
    raise ValueError(
        f'Encountered non-zero starting value indices for feature {name}:'
        f' {indices}'
    )
Checks that the given SparseTensor is 2-D ragged and left-aligned.
tensorflow_transform/impl_helper.py