o
    W۷i3                     @  s2  d dl mZ d dlZd dlmZ d dlZd dlZd dlmZm	Z	 d dl
mZmZmZmZmZ edfd:ddZ	d;ddddZ				d<d=ddZdd edfd>d#d$Zefd?d&d'Zedfd:d(d)Z				d<d@d+d,Zedfd:d-d.Z				d<d@d/d0Z		dAdBd3d4Z				d<dCd5d6Zdd7d8d9ZdS )D    )annotationsN)Any)_get_strides_for_order_K_update_order_char)
_OrderKACF_OrderCF
_ShapeLike	DTypeLikeNDArrayCshaper   dtyper	   orderr   returnNDArray[Any]c                 C  s   t j| ||dS )a  Returns an array without initializing the elements.

    Args:
        shape (int or tuple of ints): Dimensionalities of the array.
        dtype (data-type, optional): Data type specifier.
        order ({'C', 'F'}): Row-major (C-style) or column-major
            (Fortran-style) order.

    Returns:
        cupy.ndarray: A new array with elements not initialized.

    .. seealso:: :func:`numpy.empty`

    r   )cupyndarray)r   r   r    r   J/home/ubuntu/vllm_env/lib/python3.10/site-packages/cupy/_creation/basic.pyempty   s   r   T)
get_memptrc                C  s   |  }|dvrtd|t|r|f}|dkr(|dur(t|| jkr(dS tt| j	j
| j	jt|}|dkrbt| t||}d}|durNt|n| j}|r[tj||djnd}|||fS |ddfS )zw
    Determine order and strides as in NumPy's PyArray_NewLikeArray.

    (see: numpy/core/src/multiarray/ctors.c)
    )r   FKAzorder not understood: {}r   N)r   NNr   r   )upper
ValueErrorformatnumpyisscalarlenndimchrr   flagsc_contiguousf_contiguousordr   r   mathprodsizer   r   data)ar   r   r   r   stridesr*   memptrr   r   r   _new_like_order_and_strides%   s"   


r/   r   	prototypeDTypeLike | Noner   subokNone_ShapeLike | Nonec                 C  sR   |durt d|du r| j}t| |||\}}}|r|n| j}t|||||S )a  Returns a new array with same shape and dtype of a given array.

    This function currently does not support ``subok`` option.

    Args:
        a (cupy.ndarray): Base array.
        dtype (data-type, optional): Data type specifier.
            The data type of ``a`` is used by default.
        order ({'C', 'F', 'A', or 'K'}): Overrides the memory layout of the
            result. ``'C'`` means C-order, ``'F'`` means F-order, ``'A'`` means
            ``'F'`` if ``a`` is Fortran contiguous, ``'C'`` otherwise.
            ``'K'`` means match the layout of ``a`` as closely as possible.
        subok: Not supported yet, must be None.
        shape (int or tuple of ints): Overrides the shape of the result. If
            ``order='K'`` and the number of dimensions is unchanged, will try
            to keep order, otherwise, ``order='C'`` is implied.

    Returns:
        cupy.ndarray: A new array with same shape and dtype of ``a`` with
        elements not initialized.

    .. seealso:: :func:`numpy.empty_like`

    Nsubok is not supported yet)	TypeErrorr   r/   r   r   r   )r0   r   r   r2   r   r-   r.   r   r   r   
empty_likeE   s   
r7   NintM
int | Nonekc                 C  sH   |du r| }t | |f||d}||  ks||kr|S ||d |S )a  Returns a 2-D array with ones on the diagonals and zeros elsewhere.

    Args:
        N (int): Number of rows.
        M (int): Number of columns. ``M == N`` by default.
        k (int): Index of the diagonal. Zero indicates the main diagonal,
            a positive index an upper diagonal, and a negative index a lower
            diagonal.
        dtype (data-type, optional): Data type specifier.
        order ({'C', 'F'}): Row-major (C-style) or column-major
            (Fortran-style) order.

    Returns:
        cupy.ndarray: A 2-D array with given diagonals filled with ones and
        zeros elsewhere.

    .. seealso:: :func:`numpy.eye`

    N)r   r      )zerosdiagonalfill)r8   r:   r<   r   r   retr   r   r   eyeo   s   rB   nc                 C  s   t | |dS )a'  Returns a 2-D identity array.

    It is equivalent to ``eye(n, n, dtype)``.

    Args:
        n (int): Number of rows and columns.
        dtype (data-type, optional): Data type specifier.

    Returns:
        cupy.ndarray: A 2-D identity array.

    .. seealso:: :func:`numpy.identity`

    r   )rB   )rC   r   r   r   r   identity   s   rD   c                 C  s   t j| ||d}|d |S )a  Returns a new array of given shape and dtype, filled with ones.

    This function currently does not support ``order`` option.

    Args:
        shape (int or tuple of ints): Dimensionalities of the array.
        dtype (data-type, optional): Data type specifier.
        order ({'C', 'F'}): Row-major (C-style) or column-major
            (Fortran-style) order.

    Returns:
        cupy.ndarray: An array filled with ones.

    .. seealso:: :func:`numpy.ones`

    r   r=   )r   r   r@   r   r   r   r,   r   r   r   ones   s   
rF   r,   c                 C  s`   |durt d|du r| j}t| |||\}}}|r|n| j}t|||||} | d | S )a  Returns an array of ones with same shape and dtype as a given array.

    This function currently does not support ``subok`` option.

    Args:
        a (cupy.ndarray): Base array.
        dtype (data-type, optional): Data type specifier.
            The dtype of ``a`` is used by default.
        order ({'C', 'F', 'A', or 'K'}): Overrides the memory layout of the
            result. ``'C'`` means C-order, ``'F'`` means F-order, ``'A'`` means
            ``'F'`` if ``a`` is Fortran contiguous, ``'C'`` otherwise.
            ``'K'`` means match the layout of ``a`` as closely as possible.
        subok: Not supported yet, must be None.
        shape (int or tuple of ints): Overrides the shape of the result. If
            ``order='K'`` and the number of dimensions is unchanged, will try
            to keep order, otherwise, ``order='C'`` is implied.

    Returns:
        cupy.ndarray: An array filled with ones.

    .. seealso:: :func:`numpy.ones_like`

    Nr5   r=   )r6   r   r/   r   r   r   r@   r,   r   r   r2   r   r-   r.   r   r   r   	ones_like   s   

rH   c                 C  s$   t j| ||d}|jd|j |S )a  Returns a new array of given shape and dtype, filled with zeros.

    Args:
        shape (int or tuple of ints): Dimensionalities of the array.
        dtype (data-type, optional): Data type specifier.
        order ({'C', 'F'}): Row-major (C-style) or column-major
            (Fortran-style) order.

    Returns:
        cupy.ndarray: An array filled with zeros.

    .. seealso:: :func:`numpy.zeros`

    r   r   )r   r   r+   memset_asyncnbytesrE   r   r   r   r>      s   r>   c                 C  sf   |durt d|du r| j}t| |||\}}}|r|n| j}t|||||} | jd| j | S )a  Returns an array of zeros with same shape and dtype as a given array.

    This function currently does not support ``subok`` option.

    Args:
        a (cupy.ndarray): Base array.
        dtype (data-type, optional): Data type specifier.
            The dtype of ``a`` is used by default.
        order ({'C', 'F', 'A', or 'K'}): Overrides the memory layout of the
            result. ``'C'`` means C-order, ``'F'`` means F-order, ``'A'`` means
            ``'F'`` if ``a`` is Fortran contiguous, ``'C'`` otherwise.
            ``'K'`` means match the layout of ``a`` as closely as possible.
        subok: Not supported yet, must be None.
        shape (int or tuple of ints): Overrides the shape of the result. If
            ``order='K'`` and the number of dimensions is unchanged, will try
            to keep order, otherwise, ``order='C'`` is implied.

    Returns:
        cupy.ndarray: An array filled with zeros.

    .. seealso:: :func:`numpy.zeros_like`

    Nr5   r   )	r6   r   r/   r   r   r   r+   rI   rJ   rG   r   r   r   
zeros_like  s   
rK   
fill_valuer   c                 C  sL   |du rt |tjr|j}nt|j}tj| ||d}tj||dd |S )a!  Returns a new array of given shape and dtype, filled with a given value.

    This function currently does not support ``order`` option.

    Args:
        shape (int or tuple of ints): Dimensionalities of the array.
        fill_value: A scalar value to fill a new array.
        dtype (data-type, optional): Data type specifier.
        order ({'C', 'F'}): Row-major (C-style) or column-major
            (Fortran-style) order.

    Returns:
        cupy.ndarray: An array filled with ``fill_value``.

    .. seealso:: :func:`numpy.full`

    Nr   unsafecasting)
isinstancer   r   r   r   arraycopyto)r   rL   r   r   r,   r   r   r   full,  s   rS   c                 C  sf   |durt d|du r| j}t| |||\}}}|r|n| j}t|||||} tj| |dd | S )a  Returns a full array with same shape and dtype as a given array.

    This function currently does not support ``subok`` option.

    Args:
        a (cupy.ndarray): Base array.
        fill_value: A scalar value to fill a new array.
        dtype (data-type, optional): Data type specifier.
            The dtype of ``a`` is used by default.
        order ({'C', 'F', 'A', or 'K'}): Overrides the memory layout of the
            result. ``'C'`` means C-order, ``'F'`` means F-order, ``'A'`` means
            ``'F'`` if ``a`` is Fortran contiguous, ``'C'`` otherwise.
            ``'K'`` means match the layout of ``a`` as closely as possible.
        subok: Not supported yet, must be None.
        shape (int or tuple of ints): Overrides the shape of the result. If
            ``order='K'`` and the number of dimensions is unchanged, will try
            to keep order, otherwise, ``order='C'`` is implied.

    Returns:
        cupy.ndarray: An array filled with ``fill_value``.

    .. seealso:: :func:`numpy.full_like`

    Nr5   rM   rN   )r6   r   r/   r   r   r   rR   )r,   rL   r   r   r2   r   r-   r.   r   r   r   	full_likeM  s    
rT   copyc               C  s.   t | tjstdt|  d| j||dS )a[  
    Copies an array to a specified data type.

    This function is an Array API compatible alternative to
    `cupy.ndarray.astype`.

    Parameters
    ----------
    x : ndarray
        Input CuPy array to cast.
    dtype : dtype
        Data type of the result.
    copy : bool, optional
        Specifies whether to copy an array when the specified dtype matches
        the data type of the input array ``x``. If ``True``, a newly allocated
        array must always be returned. If ``False`` and the specified dtype
        matches the data type of the input array, the input array must be
        returned; otherwise, a newly allocated array must be returned.
        Defaults to ``True``.

    Returns
    -------
    out : ndarray
        An array having the specified data type.

    See Also
    --------
    ndarray.astype
    numpy.astype

    z&Input should be a CuPy array. It is a z	 instead.rU   )rP   r   r   r6   typeastype)xr   rV   r   r   r   rX   |  s
    rX   )r   r   r   r	   r   r   r   r   )N)Nr   NN)r0   r   r   r1   r   r   r2   r3   r   r4   r   r   )r8   r9   r:   r;   r<   r9   r   r	   r   r   r   r   )rC   r9   r   r	   r   r   )r,   r   r   r1   r   r   r2   r3   r   r4   r   r   )Nr   )
r   r   rL   r   r   r1   r   r   r   r   )r,   r   rL   r   r   r1   r   r   r2   r3   r   r4   r   r   )
__future__r   r(   typingr   r   r   cupy._core.internalr   r   cupy.typing._typesr   r   r   r	   r
   floatr   r/   r7   rB   rD   rF   rH   r>   rK   rS   rT   rX   r   r   r   r   <module>   sd    ",#-.$/