o
    W۷i                     @  sf   d dl mZ d dlZd dlmZ dddZdd Zdd	d
ZdddZdddZ	dd Z
dddZdS )    )annotationsN)internalc                 C     |  |||S )a  Takes elements of an array at specified indices along an axis.

    This is an implementation of "fancy indexing" at single axis.

    This function does not support ``mode`` option.

    Args:
        a (cupy.ndarray): Array to extract elements.
        indices (int or array-like): Indices of elements that this function
            takes.
        axis (int): The axis along which to select indices. The flattened input
            is used by default.
        out (cupy.ndarray): Output array. If provided, it should be of
            appropriate shape and dtype.

    Returns:
        cupy.ndarray: The result of fancy indexing.

    .. seealso:: :func:`numpy.take`

    )take)aindicesaxisout r
   M/home/ubuntu/vllm_env/lib/python3.10/site-packages/cupy/_indexing/indexing.pyr      s   r   c                 C  s   |j jdvr
td|du r|  } d}| j}t||}||jkr&tdg }t| j	D ]'\}}||kr;|
| q-d| d d|| d   }|
t|| q-| t| S )	aw  Take values from the input array by matching 1d index and data slices.

    Args:
        a (cupy.ndarray): Array to extract elements.
        indices (cupy.ndarray): Indices to take along each 1d slice of ``a``.
        axis (int): The axis to take 1d slices along.

    Returns:
        cupy.ndarray: The indexed result.

    .. seealso:: :func:`numpy.take_along_axis`
    )iuz"`indices` must be an integer arrayNr   z9`indices` and `a` must have the same number of dimensions)   )r   )dtypekind
IndexErrorravelndimr   _normalize_axis_index
ValueError	enumerateshapeappendcupyarangereshapetuple)r   r   r   r   fancy_indexr   n	ind_shaper
   r
   r   take_along_axis!   s$   
r!   raisec                 C  r   )N)choose)r   choicesr	   moder
   r
   r   r#   I   s   r#   c                 C  s   | | ||S )aF  Returns selected slices of an array along given axis.

    Args:
        condition (1-D array of bools): Array that selects which entries to
            return. If len(condition) is less than the size of a along the
            given axis, then output is truncated to the length of the condition
            array.
        a (cupy.ndarray): Array from which to extract a part.
        axis (int): Axis along which to take slices. If None (default), work
            on the flattened array.
        out (cupy.ndarray): Output array. If provided, it should be of
            appropriate shape and dtype.

    Returns:
        cupy.ndarray: A copy of a without the slices along axis for which
        condition is false.

    .. warning::

            This function may synchronize the device.


    .. seealso:: :func:`numpy.compress`

    )compress)	conditionr   r   r	   r
   r
   r   r&   M   s   r&   r   c                 C  r   )a  Returns specified diagonals.

    This function extracts the diagonals along two specified axes. The other
    axes are not changed. This function returns a writable view of this array
    as NumPy 1.10 will do.

    Args:
        a (cupy.ndarray): Array from which the diagonals are taken.
        offset (int): Index of the diagonals. Zero indicates the main
            diagonals, a positive value upper diagonals, and a negative value
            lower diagonals.
        axis1 (int): The first axis to take diagonals from.
        axis2 (int): The second axis to take diagonals from.

    Returns:
        cupy.ndarray: A view of the diagonals of ``a``.

    .. seealso:: :func:`numpy.diagonal`

    )diagonal)r   offsetaxis1axis2r
   r
   r   r(   j   s   r(   c                 C  sL   t |tjs
tdt | tjst| } | }|  } ||  d S )a  Return the elements of an array that satisfy some condition.

    This is equivalent to ``np.compress(ravel(condition), ravel(arr))``.
    If ``condition`` is boolean, ``np.extract`` is equivalent to
    ``arr[condition]``.

    Args:
        condition (int or array_like): An array whose nonzero or True entries
            indicate the elements of array to extract.
        a (cupy.ndarray): Input array of the same size as condition.

    Returns:
        cupy.ndarray: Rank 1 array of values from arr where condition is True.

    .. warning::

            This function may synchronize the device.

    .. seealso:: :func:`numpy.extract`
    z/extract requires input array to be cupy.ndarrayr   )
isinstancer   ndarray	TypeErrorarrayr   r   nonzero)r'   r   r
   r
   r   extract   s   
r1   c           	      C  sD  t | t |krtdt | dkrtdt|stdtt |D ] }t|| tjs3td| | }|jj	tj
urEtd|q%tj| }tj|  } tjg ||R  }|d jdkrg| d j}nt| d |d d j}t||}t|| |ddd	 }| ddd	 } t|| D ]\}}tj|||d
 q|S )a  Return an array drawn from elements in choicelist, depending on conditions.

    Args:
        condlist (list of bool arrays): The list of conditions which determine
            from which array in `choicelist` the output elements are taken.
            When multiple conditions are satisfied, the first one encountered
            in `condlist` is used.
        choicelist (list of cupy.ndarray): The list of arrays from which the
            output elements are taken. It has to be of the same length
            as `condlist`.
        default (scalar) : If provided, will fill element inserted in `output`
            when all conditions evaluate to False. default value is 0.

    Returns:
        cupy.ndarray: The output at position m is the m-th element of the
        array in `choicelist` where the m-th element of the corresponding
        array in `condlist` is True.

    .. seealso:: :func:`numpy.select`
    z7list of cases must be same length as list of conditionsr   z3select with an empty condition list is not possiblez"default only accepts scalar valuesz.choicelist only accepts lists of cupy ndarraysz7invalid entry {} in condlist: should be boolean ndarrayNr   )where)lenr   r   isscalarr.   ranger,   r-   r   typebool_formatresult_typebroadcast_arraysr   r   emptycopytozip)	condlist
choicelistdefaultr   condr   result_shaperesultchoicer
   r
   r   select   sJ   



rF   )NN)Nr"   )r   r   r   )r   )
__future__r   r   
cupy._corer   r   r!   r#   r&   r(   r1   rF   r
   r
   r
   r   <module>   s    

(

"