o
    W۷i8                     @  s   d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ ddd	Zdd
dZdddZdddZdddZ				dddZdd Z				dddddZ				dddddZdd ZdS )     )annotationsN)_core)_routines_statistics)_fusion_thread_local)contentFc                 C  8   t  r|r
tdt jtj| ||dS | j|||dS )a  Returns the minimum of an array or the minimum along an axis.

    .. note::

       When at least one element is NaN, the corresponding min value will be
       NaN.

    Args:
        a (cupy.ndarray): Array to take the minimum.
        axis (int): Along which axis to take the minimum. The flattened array
            is used by default.
        out (cupy.ndarray): Output array.
        keepdims (bool): If ``True``, the axis is remained as an axis of
            size one.

    Returns:
        cupy.ndarray: The minimum of ``a``, along the axis if specified.

    .. note::
       When cuTENSOR accelerator is used, the output value might be collapsed
       for reduction axes that have one or more NaN elements.

    .. seealso:: :func:`numpy.amin`

    z4cupy.amin does not support `keepdims` in fusion yet.axisoutr	   r
   keepdims)r   	is_fusingNotImplementedErrorcall_reduction_statisticsaminminar	   r
   r    r   L/home/ubuntu/vllm_env/lib/python3.10/site-packages/cupy/_statistics/order.pyr         
r   c                 C  r   )a  Returns the maximum of an array or the maximum along an axis.

    .. note::

       When at least one element is NaN, the corresponding min value will be
       NaN.

    Args:
        a (cupy.ndarray): Array to take the maximum.
        axis (int): Along which axis to take the maximum. The flattened array
            is used by default.
        out (cupy.ndarray): Output array.
        keepdims (bool): If ``True``, the axis is remained as an axis of
            size one.

    Returns:
        cupy.ndarray: The maximum of ``a``, along the axis if specified.

    .. note::
       When cuTENSOR accelerator is used, the output value might be collapsed
       for reduction axes that have one or more NaN elements.

    .. seealso:: :func:`numpy.amax`

    z4cupy.amax does not support `keepdims` in fusion yet.r   r   )r   r   r   r   r   amaxmaxr   r   r   r   r   3   r   r   c                 C  0   t j| |||d}t| rtdt |S )a  Returns the minimum of an array along an axis ignoring NaN.

    When there is a slice whose elements are all NaN, a :class:`RuntimeWarning`
    is raised and NaN is returned.

    Args:
        a (cupy.ndarray): Array to take the minimum.
        axis (int): Along which axis to take the minimum. The flattened array
            is used by default.
        out (cupy.ndarray): Output array.
        keepdims (bool): If ``True``, the axis is remained as an axis of
            size one.

    Returns:
        cupy.ndarray: The minimum of ``a``, along the axis if specified.

    .. warning::

        This function may synchronize the device.

    .. seealso:: :func:`numpy.nanmin`

    r   All-NaN slice encountered)r   nanminr   isnananywarningswarnRuntimeWarningr   r	   r
   r   resr   r   r   r   X      r   c                 C  r   )a  Returns the maximum of an array along an axis ignoring NaN.

    When there is a slice whose elements are all NaN, a :class:`RuntimeWarning`
    is raised and NaN is returned.

    Args:
        a (cupy.ndarray): Array to take the maximum.
        axis (int): Along which axis to take the maximum. The flattened array
            is used by default.
        out (cupy.ndarray): Output array.
        keepdims (bool): If ``True``, the axis is remained as an axis of
            size one.

    Returns:
        cupy.ndarray: The maximum of ``a``, along the axis if specified.

    .. warning::

        This function may synchronize the device.

    .. seealso:: :func:`numpy.nanmax`

    r   r   )r   nanmaxr   r   r   r   r    r!   r"   r   r   r   r%   w   r$   r%   c                 C  s   | j |||dS )aP  Returns the range of values (maximum - minimum) along an axis.

    .. note::

       The name of the function comes from the acronym for 'peak to peak'.

       When at least one element is NaN, the corresponding ptp value will be
       NaN.

    Args:
        a (cupy.ndarray): Array over which to take the range.
        axis (int): Axis along which to take the minimum. The flattened
            array is used by default.
        out (cupy.ndarray): Output array.
        keepdims (bool): If ``True``, the axis is retained as an axis of
            size one.

    Returns:
        cupy.ndarray: The minimum of ``a``, along the axis if specified.

    .. note::
       When cuTENSOR accelerator is used, the output value might be collapsed
       for reduction axes that have one or more NaN elements.

    .. seealso:: :func:`numpy.amin`

    r   )ptpr   r   r   r   r&      s   r&   linearc                   s  t  |}t |}|jdkr|d  }d}nd}|jdkr&td|jt|tr.|f}|rO|d u r:d j }	nt j	}	|D ]	}
d|	|
 j < qAt
|	}	|d u ra|rZ  }n  }d}nHt
 fdd|D }tt jt| }t|}tt|D ]
\}} || q|r  j	d | d	 }n  j	d | d	  }d
}|j|d |j	| }||d  }|dv rtd| d|dkrt |t j}n;|dkrt |t j}n-|dkrdt |t |  }n|dkrt |t j}n|dkr
ntd||jt jkr't ||}|j|d|d}n=|d u r<t j|j	d d
 |j	 |d}nt |d|j}t dddd|||jdkrX|j	d
 nd|j | t |d
}|rl|!d}|r~|j dkryd	|	 }	||	}t"#|S )Nr   TF   z4Expected q to have a dimension of 1.
Actual: {} != 1)r(   c                 3  s    | ]}| j  V  qd S )N)ndim).0axr   r   r   	<genexpr>   s    z&_quantile_unchecked.<locals>.<genexpr>)r.   )r	         ?)inverted_cdfaveraged_inverted_cdfclosest_observationinterpolated_inverted_cdfhazenweibullmedian_unbiasednormal_unbiased'z;' method is not yet supported. Please use any other method.lowerhighermidpointg      ?nearestr'   z^Unexpected interpolation method.
Actual: '{}' not in ('linear', 'lower', 'higher', 'midpoint')r   )dtypez0S idx, raw T a, raw int32 offset, raw int32 sizezU reta#  
            ptrdiff_t idx_below = floor(idx);
            U weight_above = idx - idx_below;

            ptrdiff_t max_idx = size - 1;
            ptrdiff_t offset_bottom = _ind.get()[0] * offset + idx_below;
            ptrdiff_t offset_top = min(offset_bottom + 1, max_idx);

            U diff = a[offset_top] - a[offset_bottom];

            if (weight_above < 0.5) {
                ret = a[offset_bottom] + diff * weight_above;
            } else {
                ret = a[offset_top] - diff * (1 - weight_above);
            }
            cupy_percentile_weightnening)$cupyresult_typeasarrayr)   
ValueErrorformat
isinstanceintlistshapetupleravelflattensetrangelen	enumeratesortedswapaxesreshapecopysortfloorastypeint32ceilaroundr=   rollaxistakeemptyElementwiseKernelsizesqueezer   _internal_ascontiguousarray)r   qr	   r
   overwrite_inputmethodr   r=   zerodkeepdimr+   apnkeepkeepisNxindicesretr   r,   r   _quantile_unchecked   s   









 "


rm   c                 C  s0   t | tjrtnt}|d| ko|| dkS )Ng        r/   )rD   r?   ndarraynumpycount_nonzero)r`   xpr   r   r   _quantile_is_valid#  s   rr   )interpolationc             	   C  sZ   |dur
t ||d}t|ttfrt|}|d }t|s"tdt| ||||||dS )a  Computes the q-th percentile of the data along the specified axis.

    Args:
        a (cupy.ndarray): Array for which to compute percentiles.
        q (float, tuple of floats or cupy.ndarray): Percentiles to compute
            in the range between 0 and 100 inclusive.
        axis (int or tuple of ints): Along which axis or axes to compute the
            percentiles. The flattened array is used by default.
        out (cupy.ndarray): Output array.
        overwrite_input (bool): If True, then allow the input array `a`
            to be modified by the intermediate calculations, to save
            memory. In this case, the contents of the input `a` after this
            function completes is undefined.
        method (str): Interpolation method when a quantile lies between
            two data points. ``linear`` interpolation is used by default.
            Supported interpolations are ``lower``, ``higher``, ``midpoint``,
            ``nearest`` and ``linear``.
        keepdims (bool): If ``True``, the axis is remained as an axis of
            size one.
        interpolation (str): Deprecated name for the method keyword argument.

    Returns:
        cupy.ndarray: The percentiles of ``a``, along the axis if specified.

    .. seealso:: :func:`numpy.percentile`
    N
percentiled   z)Percentiles must be in the range [0, 100]r	   r
   ra   rb   r   	_check_interpolation_as_methodrD   rH   rF   ro   rA   rr   rB   rm   r   r`   r	   r
   ra   rb   r   rs   r   r   r   rt   (  s    
rt   c             	   C  sR   |dur
t ||d}t|ttfrt|}t|stdt| ||||||dS )a  Computes the q-th quantile of the data along the specified axis.

    Args:
        a (cupy.ndarray): Array for which to compute quantiles.
        q (float, tuple of floats or cupy.ndarray): Quantiles to compute
            in the range between 0 and 1 inclusive.
        axis (int or tuple of ints): Along which axis or axes to compute the
            quantiles. The flattened array is used by default.
        out (cupy.ndarray): Output array.
        overwrite_input (bool): If True, then allow the input array `a`
            to be modified by the intermediate calculations, to save
            memory. In this case, the contents of the input `a` after this
            function completes is undefined.
        method (str): Interpolation method when a quantile lies between
            two data points. ``linear`` interpolation is used by default.
            Supported interpolations are ``lower``, ``higher``, ``midpoint``,
            ``nearest`` and ``linear``.
        keepdims (bool): If ``True``, the axis is remained as an axis of
            size one.
        interpolation (str): Deprecated name for the method keyword argument.

    Returns:
        cupy.ndarray: The quantiles of ``a``, along the axis if specified.

    .. seealso:: :func:`numpy.quantile`
    Nquantilez%Quantiles must be in the range [0, 1]rv   rw   ry   r   r   r   rz   Y  s    
rz   c                 C  s,   t jd| dtdd | dkrtd|S )Nz!the `interpolation=` argument to z was renamed to `method=`, which has additional options.
Users of the modes 'nearest', 'lower', 'higher', or 'midpoint' are encouraged to review the method they. (Deprecated NumPy 1.22)   )
stacklevelr'   zjYou shall not pass both `method` and `interpolation`!
(`interpolation` is Deprecated in favor of `method`))r   r    DeprecationWarning	TypeError)rb   rs   fnamer   r   r   rx     s   
rx   )NNF)NNFr'   F)
__future__r   r   ro   r?   r   
cupy._corer   r   r   cupy._logicr   r   r   r   r%   r&   rm   rr   rt   rz   rx   r   r   r   r   <module>   sD    

%
%


n11