예제 #1
0
def polymul(a1, a2):
    """Computes the product of two polynomials.

    Args:
        a1 (scalar, cupy.ndarray or cupy.poly1d): first input polynomial.
        a2 (scalar, cupy.ndarray or cupy.poly1d): second input polynomial.

    Returns:
        cupy.ndarray or cupy.poly1d: The product of the inputs.

    .. seealso:: :func:`numpy.polymul`

    """
    a1 = cupy.trim_zeros(a1, trim='f')
    a2 = cupy.trim_zeros(a2, trim='f')
    if a1.size == 0:
        a1 = cupy.array([0.], a1.dtype)
    if a2.size == 0:
        a2 = cupy.array([0.], a2.dtype)
    return cupy.convolve(a1, a2)
예제 #2
0
def fix_padding(tnsr):
    """Function to fix padding on a torch.Tensor object

    Args:
        tnsr ([torch.Tensor]): Tensor representing input_ids,
        attention_mask

    Returns:
        [torch.Tensor]: trimmed stack of Tensor objects
    """

    # Remove all the padding from the end
    trimmed_collections = list()
    max_arr_length = -1
    dx = to_dlpack(tnsr)
    embeddings_collecton = cp.fromDlpack(dx)
    for embeddings in embeddings_collecton:
        trimmed = cp.trim_zeros(embeddings, trim="b")
        max_arr_length = max(max_arr_length, len(trimmed))
        trimmed_collections.append(trimmed)

    first_arr_stack = cp.pad(
        trimmed_collections[0],
        (0, max_arr_length - len(trimmed_collections[0])),
        "constant",
    )

    # Add the required padding back
    for a in range(1, len(trimmed_collections)):
        padded = cp.pad(
            trimmed_collections[a],
            (0, max_arr_length - len(trimmed_collections[a])),
            "constant",
        )
        first_arr_stack = cp.vstack([first_arr_stack, padded])
        # Convert it back to a PyTorch tensor.
    tx2 = from_dlpack(first_arr_stack.toDlpack())

    # Taking care of case where we have only one sentence
    # Then, we need to reshape to get the right dimensions
    # since in the other cases cp.vstack handles that.

    if len(tx2.shape) == 1:
        dim = tx2.shape[0]
        tx2 = torch.reshape(tx2, (1, dim))

    return tx2
예제 #3
0
파일: polyutils.py 프로젝트: toslunar/cupy
def trimseq(seq):
    """Removes small polynomial series coefficients.

    Args:
        seq (cupy.ndarray): input array.

    Returns:
        cupy.ndarray: input array with trailing zeros removed. If the
        resulting output is empty, it returns the first element.

    .. seealso:: :func:`numpy.polynomial.polyutils.trimseq`

    """
    if seq.size == 0:
        return seq
    ret = cupy.trim_zeros(seq, trim='b')
    if ret.size > 0:
        return ret
    return seq[:1]