o
    W۷i9                     @  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dZ	
	d.ddZ			 d/ddZ
		 d0ddZdd ZG dd dZed
dZed	dZedddddddddd d!d"ged#d$d%d&d'R d(Zed)d*d(Zed)d+d,ZdS )1    )annotationsN)_core)	bf16_loop   c                 C  s   |du rt dd | ||fD rt}nt}|du r| }d} |du r#d}tt||  | }|dkr9tjd|dS t|jtj	krc|dkrJt
d	|dkrZtj| | | gtj	dS tj| gtj	dS tj|f|d}t|j}t|| ||||d |S )
ae  Returns an array with evenly spaced values within a given interval.

    Values are generated within the half-open interval [start, stop). The first
    three arguments are mapped like the ``range`` built-in function, i.e. start
    and step are optional.

    Args:
        start: Start of the interval.
        stop: End of the interval.
        step: Step width between each pair of consecutive values.
        dtype: Data type specifier. It is inferred from other arguments by
            default.

    Returns:
        cupy.ndarray: The 1-D array of range values.

    .. seealso:: :func:`numpy.arange`

    Nc                 s  s$    | ]}t t|jd kV  qdS )fN)numpydtypetypekind).0val r   K/home/ubuntu/vllm_env/lib/python3.10/site-packages/cupy/_creation/ranges.py	<genexpr>!   s    zarange.<locals>.<genexpr>r   r   )r   r      zMarange() is only supported for booleans when the result has at most length 2.)anyfloatintr   ceilcupyemptyr   r	   bool_	TypeErrorarray_arange_ufunc)startstopstepr   sizerettypr   r   r   arange   s4   r"   2   TFc           
      C  s   t | |t|}|du r|}t j|f|d}|r|d n|}|dkr1|dkr,||  td}	n&t||  | }	t|}|	dkrKt| ||  || nt| |	| |rW||d< t |t jret j	||d |j
|d	d
}|rr||	fS |S )a  Returns an array with evenly-spaced values within a given interval.

    Instead of specifying the step width like :func:`cupy.arange`, this
    function requires the total number of elements specified.

    Args:
        start: Start of the interval.
        stop: End of the interval.
        num: Number of elements.
        endpoint (bool): If ``True``, the stop value is included as the last
            element. Otherwise, the stop value is omitted.
        retstep (bool): If ``True``, this function returns (array, step).
            Otherwise, it returns only the array.
        dtype: Data type specifier. It is inferred from the start and stop
            arguments by default.

    Returns:
        cupy.ndarray: The 1-D array of ranged values.

    Nr   r   r   nan        outFcopy)r   result_typer   r   fill_linspace_ufunc_underflow_linspace_ufunc
issubdtypeintegerfloorastype)
r   r   numendpointretstepr   dtr    divr   r   r   r   _linspace_scalarC   s,   

r8   c                 C  s  |dk rt d|r|d n|}t| }t|}	|r'|	r't| |||||S |s<t| tjr5| jjdks<t| d } |	sQt|tjrJ|jjdksQt|d }t	| |t
|}
|du r`|
}||  }tj|f|
d}tdd||
d |d	d
|j  }|dkr|| }t|dkr|| }|| }n|| }nt
d}|| }|| 7 }|r|dkr||d< |dkrt|d|}t|tjrtj||d |j|dd}|r||fS |S )a  Returns an array with evenly-spaced values within a given interval.

    Instead of specifying the step width like :func:`cupy.arange`, this
    function requires the total number of elements specified.

    Args:
        start (scalar or array_like): Starting value(s) of the sequence.
        stop (scalar or array_like): Ending value(s) of the sequence, unless
            ``endpoint`` is set to ``False``. In that case, the sequence
            consists of all but the last of ``num + 1`` evenly spaced samples,
            so that ``stop`` is excluded.  Note that the step size changes when
            ``endpoint`` is ``False``.
        num: Number of elements.
        endpoint (bool): If ``True``, the stop value is included as the last
            element. Otherwise, the stop value is omitted.
        retstep (bool): If ``True``, this function returns (array, step).
            Otherwise, it returns only the array.
        dtype: Data type specifier. It is inferred from the start and stop
            arguments by default.
        axis (int):  The axis in the result to store the samples.  Relevant
            only if start or stop are array-like.  By default ``0``, the
            samples will be along a new axis inserted at the beginning.
            Use ``-1`` to get an axis at the end.

    Returns:
        cupy.ndarray: The 1-D array of ranged values.

    .. seealso:: :func:`numpy.linspace`

    r   z$linspace with num<0 is not supportedr   r         ?Nr   r%   )r&   )r   r$   r&   r'   Fr)   )
ValueErrorr   isscalarr8   
isinstancendarrayr   r
   asarrayr+   r   r   r   reshapendimr   moveaxisr/   r0   r1   r2   )r   r   r3   r4   r5   r   axisr7   scalar_startscalar_stopr6   deltar    r   r   r   r   linspace}   sN    



rF         $@c                 C  sT   t | ||||d}|du rt||S t|jdv r!|tj}t|||S )a2  Returns an array with evenly-spaced values on a log-scale.

    Instead of specifying the step width like :func:`cupy.arange`, this
    function requires the total number of elements specified.

    Args:
        start: Start of the interval.
        stop: End of the interval.
        num: Number of elements.
        endpoint (bool): If ``True``, the stop value is included as the last
            element. Otherwise, the stop value is omitted.
        base (float): Base of the log space. The step sizes between the
            elements on a log-scale are the same as ``base``.
        dtype: Data type specifier. It is inferred from the start and stop
            arguments by default.
        axis (int):  The axis in the result to store the samples.  Relevant
            only if start or stop are array-like.  By default ``0``, the
            samples will be along a new axis inserted at the beginning.
            Use ``-1`` to get an axis at the end.
    Returns:
        cupy.ndarray: The 1-D array of ranged values.

    .. seealso:: :func:`numpy.logspace`

    )r3   r4   rB   Niu)rF   r   powerr   r   r
   r2   float64)r   r   r3   r4   baser   rB   yr   r   r   logspace   s   rM   c                  O  sZ  | dd}t| dd}t| dd}|r#tdt|d |d	vr+td
| D ]}|jdkr8tdt|tj	sBtdq-t
| dkrMt| S g }t| D ]6\}}|dkrb|dkrbd}n|dkrm|dkrmd}n|}d| tdf dt
| |d    }	|||	  qS|r|}
nttj| }
|rtt
|
D ]
}|
|  |
|< qt|
S )a  Return coordinate matrices from coordinate vectors.

    Given one-dimensional coordinate arrays ``x1, x2, ... , xn`` this
    function makes N-D grids.

    For one-dimensional arrays ``x1, x2, ... , xn`` with lengths
    ``Ni = len(xi)``, this function returns ``(N1, N2, N3, ..., Nn)`` shaped
    arrays if indexing='ij' or ``(N2, N1, N3, ..., Nn)`` shaped arrays
    if indexing='xy'.

    Unlike NumPy, CuPy currently only supports 1-D arrays as inputs.

    Args:
        xi (tuple of ndarrays): 1-D arrays representing the coordinates
            of a grid.
        indexing ({'xy', 'ij'}, optional): Cartesian ('xy', default) or
            matrix ('ij') indexing of output.
        sparse (bool, optional): If ``True``, a sparse grid is returned in
            order to conserve memory. Default is ``False``.
        copy (bool, optional): If ``False``, a view
            into the original arrays are returned. Default is ``True``.

    Returns:
        tuple of cupy.ndarray

    .. seealso:: :func:`numpy.meshgrid`

    indexingxyr*   TsparseFz2meshgrid() got an unexpected keyword argument '{}'r   )rO   ijz.Valid values for `indexing` are 'xy' and 'ij'.r   zinput has to be 1dzinput has to be cupy.ndarrayNN)popboolr   formatlistr:   r@   r<   r   r=   lentuple	enumeratesliceappendbroadcast_arraysranger*   )xikwargsrN   r*   rP   xmeshesi	left_noneexpand_slices	meshes_brr   r   r   meshgrid  sN   

rf   c                   @  s*   e Zd ZdZd
ddZdd Zdd Zd	S )nd_grida  Construct a multi-dimensional "meshgrid".

    ``grid = nd_grid()`` creates an instance which will return a mesh-grid
    when indexed.  The dimension and number of the output arrays are equal
    to the number of indexing dimensions.  If the step length is not a
    complex number, then the stop is not inclusive.

    However, if the step length is a **complex number** (e.g. 5j), then the
    integer part of its magnitude is interpreted as specifying the
    number of points to create between the start and stop values, where
    the stop value **is inclusive**.

    If instantiated with an argument of ``sparse=True``, the mesh-grid is
    open (or not fleshed out) so that only one-dimension of each returned
    argument is greater than 1.

    Args:
        sparse (bool, optional): Whether the grid is sparse or not.
            Default is False.

    .. seealso:: :data:`numpy.mgrid` and :data:`numpy.ogrid`

    Fc                 C  s
   || _ d S rR   rP   )selfrP   r   r   r   __init__e  s   
znd_grid.__init__c              	   C  s^  t |trH|j}|j}|j}|d u rd}t |trAt|}t|}|dkr0|j| t|d  }|j| }t	
d|dt| | S t	
|||S g }t}tt|D ]P}|| j}|| j}|d u rdd}|d u rjd}t |tr{|tt| t}n|tt|| j| |d   t |tst |tst || jtrt}qR| jrdd t||ft| D }	nt	||}	tt|D ]>}|| j}|| j}|d u rd}|d u rd}t |trtt|}|dkr|| j| t|d  }|	| | | |	|< q| jr-t	jgt| }
tt|D ]}td d |
|< |	| t|
 |	|< t	j|
|< q|	S )Nr   r   r9   c                 S  s   g | ]\}}t j||d qS )r   )r   r"   )r   _x_tr   r   r   
<listcomp>  s    z'nd_grid.__getitem__.<locals>.<listcomp>)r<   rZ   r   r   r   complexabsr   r   r   r"   r]   rW   r[   mathr   rP   zipindicesnewaxisrX   )ri   keyr   r   r   lengthr   r!   knnslobjr   r   r   __getitem__h  sx   









znd_grid.__getitem__c                 C  s   dS )Nr   r   )ri   r   r   r   __len__  s   znd_grid.__len__N)F)__name__
__module____qualname____doc__rj   ry   rz   r   r   r   r   rg   L  s
    
=rg   rh   cupy_arangezbb->bzBB->Bzhh->hzHH->Hzii->izII->Izll->lzLL->Lzqq->qzQQ->Qzee->er   zff->fdd->d)zFF->Fzout0 = in0 + float(i) * in1)zDD->Dzout0 = in0 + double(i) * in1zout0 = in0 + i * in1cupy_linspace)r   )zddd->dzout0 = in0 + i * in1 / in2)Nr   N)r#   TFN)r#   TFNr   )r#   TrG   Nr   )
__future__r   rp   r   r   r   
cupy._utilr   r"   r8   rF   rM   rf   rg   mgridogridcreate_ufuncr   r.   r-   r   r   r   r   <module>   s^    
7
:
a
&H
]
