def _merge_strides(
    lhs,
    rhs
):
  """Merge two sets of strides.

  Args:
    lhs: A tuple (x, y) where each element is either an integer or None.
    rhs: A tuple (x, y) where each element is either an integer or None.

  Returns:
    A merged tuple (x, y). For example:
    * merge((1, 1), (None, None)) = (1, 1)
    * merge((None, None), (None, None)) = None
    * merge((1, 1), (2, 2)) triggers an error, since the two strides
      are incompatible.
  """

  if lhs[0] is not None and rhs[0] is not None and lhs[0] != rhs[0]:
    raise ValueError('Stride mismatch: {} vs {}'.format(lhs, rhs))
  if lhs[1] is not None and rhs[1] is not None and lhs[1] != rhs[1]:
    raise ValueError('Stride mismatch: {} vs {}'.format(lhs, rhs))
  x = lhs[0] if lhs[0] is not None else rhs[0]
  y = lhs[1] if lhs[1] is not None else rhs[1]
  return (x, y)