def _as_pairs(x, ndim, as_index=False):
    """Broadcasts `x` to an array with shape (`ndim`, 2).

    A helper function for `pad` that prepares and validates arguments like
    `pad_width` for iteration in pairs.

    Args:
      x(scalar or array-like, optional): The object to broadcast to the shape
          (`ndim`, 2).
      ndim(int): Number of pairs the broadcasted `x` will have.
      as_index(bool, optional): If `x` is not None, try to round each
          element of `x` to an integer (dtype `cupy.intp`) and ensure every
          element is positive. (Default value = False)

    Returns:
      nested iterables, shape (`ndim`, 2): The broadcasted version of `x`.
    """
    if x is None:
        # Pass through None as a special case, otherwise cupy.round(x) fails
        # with an AttributeError
        return ((None, None),) * ndim
    elif isinstance(x, numbers.Number):
        if as_index:
            x = round(x)
        return ((x, x),) * ndim

    x = numpy.array(x)
    if as_index:
        x = numpy.asarray(numpy.round(x), dtype=numpy.intp)

    if x.ndim < 3:
        # Optimization: Possibly use faster paths for cases where `x` has
        # only 1 or 2 elements. `numpy.broadcast_to` could handle these as well
        # but is currently slower

        if x.size == 1:
            # x was supplied as a single value
            x = x.ravel()  # Ensure x[0] works for x.ndim == 0, 1, 2
            if as_index and x < 0:
                raise ValueError("index can't contain negative values")
            return ((x[0], x[0]),) * ndim

        if x.size == 2 and x.shape != (2, 1):
            # x was supplied with a single value for each side
            # but except case when each dimension has a single value
            # which should be broadcasted to a pair,
            # e.g. [[1], [2]] -> [[1, 1], [2, 2]] not [[1, 2], [1, 2]]
            x = x.ravel()  # Ensure x[0], x[1] works
            if as_index and (x[0] < 0 or x[1] < 0):
                raise ValueError("index can't contain negative values")
            return ((x[0], x[1]),) * ndim

    if as_index and x.min() < 0:
        raise ValueError("index can't contain negative values")

    # Converting the array with `tolist` seems to improve performance
    # when iterating and indexing the result (see usage in `pad`)
    x_view = x.view()
    x_view.shape = (ndim, 2)
    return x_view.tolist()