def embedding(input, weight, padding_idx=None):
    """Lookup the embedding matrix using index.

    Parameters
    ----------
    input : dragon.vm.torch.Tensor
        The index tensor.
    weight : dragon.vm.torch.Tensor
        The embedding matrix.
    padding_idx : int, optional
        The position where to return zeros.

    Returns
    -------
    dragon.vm.torch.Tensor
        The output tensor.

    """
    if padding_idx is not None:
        num_embeddings = weight.size(0)
        if padding_idx > 0:
            if padding_idx >= num_embeddings:
                raise ValueError('<padding_idx> must be within embedding matrix.')
        elif padding_idx < 0:
            if padding_idx < -num_embeddings:
                raise ValueError('<padding_idx> must be within embedding matrix.')
            padding_idx = num_embeddings + padding_idx
        weight[padding_idx] = 0
    return weight.index_select(0, input)