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   ^/home/www/facesmatcher.com/frenv_anti/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   sumZfdiv)r   dimr   Zieee_rounding_dimznumZ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   jZsize_iZsize_jZsize_gZijZsize_gjZgroup_idZoff_iZnew_iZnew_jr   r   r   	swizzle2dL   s   r(   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   
r,   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
    )r,   r*   r+   )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gtZv_retZi_retr   r   r   _argmax_combine      r9   c                 C     t | |||dS NTr9   r2   r3   r4   r5   r   r   r   _argmax_combine_tie_break_left      r?   c                 C  r;   r/   r=   r>   r   r   r   _argmax_combine_tie_break_fast   r@   rA   c                 C     t | |S N)r   maximumabr   r   r   _elementwise_max      rH   rD   return_indicesreturn_indices_tie_break_left)Z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_indicesr?   rA   r
   r+   primitive_bitwidthis_floatingtofloat32is_intint32reducerH   r-   axisrJ   rK   r   r   r   r   r      s   
r   zmaximum indexr6   )rL   c                 C     t | |d||d\}}|S NT)rJ   rK   r   )r   r-   rY   r6   r   _retr   r   r   argmax      r_   c           	      C  sJ   |r| |ko	||k }nd}| |k p|}t || |}t |||}||fS r/   r0   )	r2   r3   r4   r5   r6   r7   ltZ	value_retZ	index_retr   r   r   _argmin_combine   r:   rb   c                 C  r;   r<   rb   r>   r   r   r   _argmin_combine_tie_break_left   r@   rd   c                 C  r;   r/   rc   r>   r   r   r   _argmin_combine_tie_break_fast   r@   re   c                 C  rB   rC   r%   rE   r   r   r   _elementwise_min   rI   rf   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 rM   )r   rO   rP   rd   re   r
   r+   rQ   rR   rS   rT   rU   rV   rW   rf   rX   r   r   r   min   s   
rg   zminimum indexc                 C  rZ   r[   )rg   r\   r   r   r   argmin   r`   rh   c                 C  s   | | S rC   r   rE   r   r   r   _sum_combine     ri   in_dtyper+   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 )NrN   )r   _unwrap_if_constexprZis_int_signedZint_bitwidthrV   Zis_int_unsignedZuint32)rk   r+   	out_dtyper   r   r   _pick_sum_dtype
  s   
rn   r   )Z	dtype_argc                 C  s0   t | j|}|d ur| |} tj| |t|dS )Nr   )rn   r+   rS   r   rW   ri   )r-   rY   r   r+   rm   r   r   r   r     s   
c                 C  s   | |A S rC   r   rE   r   r   r   _xor_combine%  rj   ro   zxor sumc                 C  &   t | jj d t j| |t|dS )Nz#xor_sum only supported for integersr   )r   static_asserttypescalarrU   rW   ro   r-   rY   r   r   r   r   xor_sum-     ru   c                 C  s   | |B S rC   r   )r   yr   r   r   _or_combine8  rj   rx   Z	reduce_ofc                 C  rp   )Nz%reduce_of only supported for integersr   )r   rq   rr   rs   rU   rW   rx   rt   r   r   r   	reduce_or=  rv   ry   cumsumc                 C  s8   t | } t| j|}|d ur| |} t | |t|S rC   )r   rO   rn   r+   rS   associative_scanri   )r-   rY   reverser+   rm   r   r   r   rz   H  s
   

c                 C  s   | | S rC   r   rE   r   r   r   _prod_combineZ  rj   r}   cumprodc                 C  s   t | } t | |t|S rC   )r   rO   r{   r}   )r-   rY   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   Z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ZbitwidthsignedZbitcastr   )
r   r#   r   get_int_dtyper+   rQ   rS   ru   r   r1   )
r   flipr   r   idtypeZixZiyrw   Zis_rightr^   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   r*   )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   )rY   )lenr*   r   rq   r   r#   r"   r   r   r   rg   )
r   r   r   r   r   Zlog_nZ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  r@   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   r*   r   rq   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   rl   r   r
   )r   r*   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   rq   r   r*   r   r   r   r   r+   rQ   r"   rS   r   ru   )r   r   r   Zstepsr   rw   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   r*   r"   )rF   rG   cr   r   r   
interleave  s   &r   )r   r	   )NFF)F)NFTF)TF)rk   r	   r+   r	   )NFN)r+   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	   rC   )r   r	   r   r	   )6
__future__r   Zruntime.jitr    r   r   r   r   Z_tensor_member_fnr   Z_add_math_1arg_docstrr   r   r$   r(   r,   r.   r9   r?   rA   rH   Z_add_reduction_docstrr   r_   rb   rd   re   rf   rg   rh   ri   rn   r   ro   ru   rx   ry   Z_add_scan_docstrrz   r}   r~   r   r   r   r   ZCONSTEXPR_0r   r   r   r   r   r   r   r   r   r   r   <module>   s    

	


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