o
    h?                     @  sH  d dl mZ ddlmZ ddlmZ ddlmZ dud	d
ZduddZej	edd Z
ej	eeddd Zej	eeddvddZej	edwddZedd Zedd Zedd Zedd Zedd  Zed!d" Zed#d$ Zej	eejd%d&d'd(dxd*d+Zej	eejd,d-d.dyd/d0Zed1d2 Zed3d4 Zed5d6 Zed7d8 Zej	eejd9d&d'd(dxd:d;Zej	eejd<d-d.dyd=d>Zed?d@ ZdzdCdDZ ej	eejdEdBdFd{d|dGdEZ!edHdI Z"ej	eedJd}dKdLZ#edMdN Z$ej	eedOdwdPdQZ%ej	eej&dRdBdFd~d|dSdRZ'edTdU Z(ej	ee&dVddWdVZ)eddZd[Z*edud\d]Z+edd`daZ,eddbdcZ-eddej.fddgdhZ/edej.fddidjZ0edddkdlZ1edej.fddmdnZ2dodp Z3ej	eddqdrZ4edsdt Z5dS )    )annotations   )jit   )core)mathicore.constexprc                 C  s:   d}t | j}|dkr|dL }|d7 }|dkst |S )Nr   r   )r   	constexprvalue)r   log2n r   S/var/www/html/scripts/venv/lib/python3.10/site-packages/triton/language/standard.py_log2
   s   
r   c                 C  s$   | j }t||d @ dko|dkS Nr   r   )r   r   r
   )r   r   r   r   r   _is_power_of_two   s   r   c                 C  s   | | d | S )z
    Computes the ceiling division of :code:`x` by :code:`div`

    :param x: the input number
    :type x: Block
    :param div: the divisor
    :type div: Block
    r   r   )xdivr   r   r   cdiv   s   r   sigmoidc                 C  s   ddt |    S )Nr   )r   exp)r   r   r   r   r   +   s   softmaxNFc                 C  sJ   |d u rd}n|}| t | ||d }t|}t|||d}t|||S )Nr   	keep_dims)maxr   r   sumfdiv)r   dimr   ieee_rounding_dimznumdenr   r   r   r   2   s   
c                 C  s   t j| | jg|dS )zn
    Returns a contiguous flattened view of :code:`x`.

    :param x: the input tensor
    :type x: Block
    )can_reorder)r   reshapenumel)r   r$   r   r   r   ravel@   s   	r'   c                 C  sX   | | | }|| }|| }|| }t || |}|| }|||  }	|| }
|	|
fS )a  
    Transforms the indices of a row-major `size_i * size_j` matrix into
    the indices of a column-major matrix for each group of `size_g` rows.

    For example, for :code:`size_i = size_j = 4` and :code:`size_g = 2`, it will
    transform ::

        [[0 , 1 , 2 , 3 ],
         [4 , 5 , 6 , 7 ],
         [8 , 9 , 10, 11],
         [12, 13, 14, 15]]

    into ::

        [[0, 2,  4 , 6 ],
         [1, 3,  5 , 7 ],
         [8, 10, 12, 14],
         [9, 11, 13, 15]]
    r   minimum)r   jsize_isize_jsize_gijsize_gjgroup_idoff_inew_inew_jr   r   r   	swizzle2dL   s   r4   c                 C  s   t | d|S )a'  
    Returns a tensor filled with the scalar value 0 for the given :code:`shape` and :code:`dtype`.

    :param shape: Shape of the new array, e.g., (8, 16) or (8, )
    :type shape: tuple of ints
    :param dtype: Data-type of the new array, e.g., :code:`tl.float16`
    :type dtype: DType
    r   )r   full)shapedtyper   r   r   zerost   s   
r8   c                 C  s   t | j| jS )z
    Returns a tensor of zeros with the same shape and type as a given tensor.

    :param input: input tensor
    :type input: Tensor
    )r8   r6   r7   )inputr   r   r   
zeros_like   s   r:   c           	      C  sJ   |r| |ko	||k }nd}| |kp|}t || |}t |||}||fS NFr   where)	value1index1value2index2tie_break_lefttiegtv_reti_retr   r   r   _argmax_combine      rG   c                 C     t | |||dS NTrG   r>   r?   r@   rA   r   r   r   _argmax_combine_tie_break_left      rM   c                 C  rI   r;   rK   rL   r   r   r   _argmax_combine_tie_break_fast   rN   rO   c                 C     t | |S N)r   maximumabr   r   r   _elementwise_max      rV   rR   return_indicesreturn_indices_tie_break_left)return_indices_argtie_break_argTc                 C  s   t | } |r|rt j| |t|dS t j| |t|dS t | jjt dk rEt | j r6| 	t j
} n| j s?J d| 	t j} t j| |t|dS Nr       z"Expecting input to be integer type)r   _promote_bfloat16_to_float32_reduce_with_indicesrM   rO   r
   r7   primitive_bitwidthis_floatingtofloat32is_intint32reducerV   r9   axisrX   rY   r   r   r   r   r      s   
r   zmaximum indexrB   )r[   c                 C     t | |d||d\}}|S NT)rX   rY   r   )r   r9   rh   rB   r   _retr   r   r   argmax      rn   c           	      C  sJ   |r| |ko	||k }nd}| |k p|}t || |}t |||}||fS r;   r<   )	r>   r?   r@   rA   rB   rC   lt	value_ret	index_retr   r   r   _argmin_combine   rH   rs   c                 C  rI   rJ   rs   rL   r   r   r   _argmin_combine_tie_break_left   rN   ru   c                 C  rI   r;   rt   rL   r   r   r   _argmin_combine_tie_break_fast   rN   rv   c                 C  rP   rQ   r(   rS   r   r   r   _elementwise_min   rW   rw   r)   c                 C  s   t | } |r|rt j| |t|dS t j| |t|dS t | jjdk rBt | j r3| 	t j
} n| j s<J d| 	t j} t j| |t|dS r\   )r   r^   r_   ru   rv   r
   r7   r`   ra   rb   rc   rd   re   rf   rw   rg   r   r   r   min   s   
rx   zminimum indexc                 C  ri   rj   )rx   rk   r   r   r   argmin   ro   ry   c                 C  s   | | S rQ   r   rS   r   r   r   _sum_combine     rz   in_dtyper7   c                 C  s^   t |}|d ur|S d }|  r| jdk rt j}|S d }|S |  r-| jdk r+t jnd }|S )Nr]   )r   _unwrap_if_constexpris_int_signedint_bitwidthre   is_int_unsigneduint32)r|   r7   	out_dtyper   r   r   _pick_sum_dtype
  s   
r   r   )	dtype_argc                 C  s0   t | j|}|d ur| |} tj| |t|dS )Nr   )r   r7   rb   r   rf   rz   )r9   rh   r   r7   r   r   r   r   r     s   
c                 C  s   | |A S rQ   r   rS   r   r   r   _xor_combine%  r{   r   zxor sumc                 C  &   t | jj d t j| |t|dS )Nz#xor_sum only supported for integersr   )r   static_asserttypescalarrd   rf   r   r9   rh   r   r   r   r   xor_sum-     r   c                 C  s   | |B S rQ   r   )r   yr   r   r   _or_combine8  r{   r   	reduce_ofc                 C  r   )Nz%reduce_of only supported for integersr   )r   r   r   r   rd   rf   r   r   r   r   r   	reduce_or=  r   r   cumsumc                 C  s8   t | } t| j|}|d ur| |} t | |t|S rQ   )r   r^   r   r7   rb   associative_scanrz   )r9   rh   reverser7   r   r   r   r   r   H  s
   

c                 C  s   | | S rQ   r   rS   r   r   r   _prod_combineZ  r{   r   cumprodc                 C  s   t | } t | |t|S rQ   )r   r^   r   r   )r9   rh   r   r   r   r   r   _  s   
n_dimsr*   c                 C  s:   t dd}t |dg| | d  dg dg|  }|S )Nr   r   r   )r   aranger%   )r   r*   arr   r   r   
_indicatork  s   *r   c           
      C  sz   t | j}tj| jjdd}| j|dd}|t||d | dA }|j| jdd}t||}t	| |k||A k|| }	|	S )NTbitwidthsignedbitcastr   )
r   r&   r   get_int_dtyper7   r`   rb   r   r   r=   )
r   flipr   r   idtypeixiyr   is_rightrm   r   r   r   _compare_and_swapr  s   

r   stageorderc                 C  sF   |dkrt t| j|}n|}t|D ]}t| ||d | } q| S )zb
    order_type 0 == ascending
    order_type 1 == descending
    order_type 2 == alternating
    r   r   )r   r   r&   r   static_ranger   )r   r   r   r   r   r   r   r   _bitonic_merge_hypercube  s   r   c                 C  s6   t | dgt| j }t|||}t || j} | S )Nr   )r   r%   r   r&   r   r6   )r   r   r   r   hr   r   r   _bitonic_merge  s   r   kr   
descendingc           
      C  s.  |du rt | jd n|}t|t | jd kd t| j| }|du r'|nt|}t| j}t| dg| }td|d D ]}	t||	|	|k rLdn|}qAt|d |d D ]*}	|rkt	|t|jd | dnt
|t|jd | d}t|||	|k rdn|}qZt|| jdd d| g } | S )ai  
    Sorts a tensor along a specified dimension.

    :param x: The input tensor to be sorted.
    :type x: Tensor
    :param dim: The dimension along which to sort the tensor. If None, the tensor is sorted along the last dimension. Currently, only sorting along the last dimension is supported.
    :type dim: int, optional
    :param k: the number of top elements to select. If none, assume k = x.shape[dim]
    :type k: int, optional
    :param descending: If set to True, the tensor is sorted in descending order. If set to False, the tensor is sorted in ascending order.
    :type descending: bool, optional
    Nr   +only minor dimension is currently supportedr   )rh   )lenr6   r   r   r   r&   r%   r   r   r   rx   )
r   r   r   r   r    log_nlog_kr   r   r   r   r   r   	sort_impl  s   
8 r   c                 C  s   t | ||dS )N)r   r   r   )r   r   r   r   r   r   sort  s   r   c                 C  s   t | ||ddS )NT)r   r   r   r   )r   r   r   r   r   r   topk  rN   r   c                 C  sP   |d u rt | jd n|}t|t | jd kd t| jd }t| |||S )Nr   r   r   )r   r6   r   r   r   r   )r   r   r   r    r   r   r   r   bitonic_merge  s   r   c                 C  sF   t | } t |}| d u rt|d } | dk r| t|7 } t | S r   )r   r}   r   r
   )r   r6   r   r   r   _get_flip_dim  s   


r   c                 C  s   t t| j |ko|t| jk  t|| j}t t| j|  t| j| }t j| jj	dd}t 
| j|dd| jd| dg|  | j|d d  }t |D ]}|t||| dA }qUt 
|| jj| jdd} | S )z
    Flips a tensor `x` along the dimension `dim`.

    :param x: the first input tensor
    :type x: Block
    :param dim: the dimension to flip along
    :type dim: int
    Tr   r   Nr   r   )r   r   r   r6   r   r   r   r   r7   r`   r%   rb   r   r   )r   r   r    stepsr   r   r   r   r   r   r     s   $<r   c                 C  sD   t | |}t|jdkr|S t ||jdd d|jd  g S )a7  
    Interleaves the values of two tensors along their last dimension. The two tensors must have the same shape.
    Equivalent to `tl.join(a, b).reshape(a.shape[:-1] + [2 * a.shape[-1]])`

    :param a: The first input tensor.
    :type a: Tensor
    :param b: The second input tensor.
    :type b: Tensor
    r   Nr   )r   joinr   r6   r%   )rT   rU   cr   r   r   
interleave  s   &r   )r   r	   )NFF)F)NFTF)TF)r|   r	   r7   r	   )NFN)r7   r	   r;   )r   FN)r   F)r   r	   r*   r	   )r   r	   r   r	   )r   r	   r   r	   r   r	   )r   r	   r   r	   r   r	   )r   r	   r   r	   rQ   )r   r	   r   r	   )6
__future__r   runtime.jitr    r   r   r   r   _tensor_member_fnr   _add_math_1arg_docstrr   r   r'   r4   r8   r:   rG   rM   rO   rV   _add_reduction_docstrr   rn   rs   ru   rv   rw   rx   ry   rz   r   r   r   r   r   r   _add_scan_docstrr   r   r   r   r   r   r   CONSTEXPR_0r   r   r   r   r   r   r   r   r   r   r   <module>   s    

	


'











	


	(
