def prepare_tensor(g, data, name):
    """Convert the data to ID tensor and check its ID type and context.

    If the data is already in tensor type, raise error if its ID type
    and context does not match the graph's.
    Otherwise, convert it to tensor type of the graph's ID type and
    ctx and return.

    Parameters
    ----------
    g : DGLGraph
        Graph.
    data : int, iterable of int, tensor
        Data.
    name : str
        Name of the data.

    Returns
    -------
    Tensor
        Data in tensor object.
    """
    if F.is_tensor(data):
        if F.dtype(data) != g.idtype:
            raise DGLError(
                f'Expect argument "{name}" to have data type {g.idtype}. '
                f"But got {F.dtype(data)}."
            )
        if F.context(data) != g.device and not g.is_pinned():
            raise DGLError(
                f'Expect argument "{name}" to have device {g.device}. '
                f"But got {F.context(data)}."
            )
        ret = data
    else:
        data = F.tensor(data)
        if not (
            F.ndim(data) > 0 and F.shape(data)[0] == 0
        ) and F.dtype(  # empty tensor
            data
        ) not in (
            F.int32,
            F.int64,
        ):
            raise DGLError(
                'Expect argument "{}" to have data type int32 or int64,'
                " but got {}.".format(name, F.dtype(data))
            )
        ret = F.copy_to(F.astype(data, g.idtype), g.device)

    if F.ndim(ret) == 0:
        ret = F.unsqueeze(ret, 0)
    if F.ndim(ret) > 1:
        raise DGLError(
            'Expect a 1-D tensor for argument "{}". But got {}.'.format(
                name, ret
            )
        )
    return ret
Convert the data to ID tensor and check its ID type and context.

    If the data is already in tensor type, raise error if its ID type
    and context does not match the graph's.
    Otherwise, convert it to tensor type of the graph's ID type and
    ctx and return.

    Parameters
    ----------
    g : DGLGraph
        Graph.
    data : int, iterable of int, tensor
        Data.
    name : str
        Name of the data.

    Returns
    -------
    Tensor
        Data in tensor object.
    
python/dgl/utils/checks.py