a
    zþdÖ  ã                   @   sB  U d dl Z ddlmZ dZejed< dZejed< dZejed	< d
Z	ejed< dZ
e je
fejdœdd„ƒZe je
fejdœdd„ƒZe je
fejdœdd„ƒZe je
fejdœdd„ƒZe jdd„ ƒZe je
fejdœdd„ƒZe je
fejdœdd„ƒZe jdd„ ƒZe je
fejdœdd„ƒZe je
fejdœd d!„ƒZdS )"é    Né   )Úcorel   ¹yn< ÚPHILOX_KEY_Al   ….Ïv ÚPHILOX_KEY_Bl   S¢$ ÚPHILOX_ROUND_Al   W= ÚPHILOX_ROUND_Bé
   )Ún_roundsc                 C   sv   t  |¡D ]^}t}t}	| | }
}t  |	|¡|A |A } t  ||
¡|A |A }|	| }||
 }|t }|t }q
| |||fS )zV
    Run `n_rounds` rounds of Philox for state (c0, c1, c2, c3) and key (k0, k1).
    )ÚtlZstatic_ranger   r   Zumulhir   r   )Úc0Úc1Úc2Úc3Zk0Zk1r	   Ú_ÚAÚBZ_c0Z_c2© r   ú_/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/triton/language/random.pyÚphilox_impl   s    

r   c                 C   s„   |   tj¡} | d? d@   tj¡}| d@   tj¡}|j tjdd}|j tjdd}|j tjdd}|j tjdd}t|||||||ƒS )Né    l   ÿÿ T©Zbitcast)Útor
   Zuint64Úuint32r   )Úseedr   r   r   r   r	   Zseed_hiZseed_lor   r   r   Úphilox#   s    r   c                 C   s   t | ||ƒ\}}}}|S )ar  
    Given a :code:`seed` scalar and an :code:`offset` block, returns a single
    block of random :code:`int32`.

    If you need multiple streams of random numbers,
    using `randint4x` is likely to be faster than calling `randint` 4 times.

    :param seed: The seed for generating random numbers.
    :param offsets: The offsets to generate random numbers for.
    )Ú	randint4x)r   Úoffsetr	   Úretr   r   r   r   Úrandint/   s    r   c                 C   s   |d }t | |||||ƒS )aU  
    Given a :code:`seed` scalar and an :code:`offset` block, returns four
    blocks of random :code:`int32`.

    This is the maximally efficient entry point
    to Triton's Philox pseudo-random number generator.

    :param seed: The seed for generating random numbers.
    :param offsets: The offsets to generate random numbers for.
    r   )r   )r   r   r	   Z_0r   r   r   r   ?   s    r   c                 C   s4   | j tjdd} d}t | dk |  d | ¡} | | S )zq
    Numerically stable function to convert a random uint32 into a random float uniformly sampled in [0, 1).
    Tr   gýïÿÿÿ=r   r   )r   r
   Úint32Úwhere)ÚxZscaler   r   r   Úuint32_to_uniform_float\   s    r"   c                 C   s$   |j tjdd}t| ||ƒ}t|ƒS )zý
    Given a :code:`seed` scalar and an :code:`offset` block,
    returns a block of random :code:`float32` in :math:`U(0, 1)`

    :param seed: The seed for generating random numbers.
    :param offsets: The offsets to generate random numbers for.
    Tr   )r   r
   r   r   r"   )r   r   r	   Úsourcer   r   r   Úrandh   s    	r$   c                 C   sP   |j tjdd}t| ||ƒ\}}}}t|ƒ}t|ƒ}t|ƒ}	t|ƒ}
|||	|
fS )a  
    Given a :code:`seed` scalar and an :code:`offsets` block,
    returns a 4 blocks of random :code:`float32` in :math:`U(0, 1)`

    :param seed: The seed for generating random numbers.
    :param offsets: The offsets to generate random numbers for.
    Tr   )r   r
   r   r   r"   )r   Úoffsetsr	   Úi1Úi2Zi3Úi4Úu1Úu2Úu3Úu4r   r   r   Úrand4xv   s    	r-   c                 C   sD   t  d| ¡} d| }t  dt  | ¡ ¡}|t  |¡ |t  |¡ fS )zBox-Muller transformgH¯¼šò×z>g-DTû!@g       À)r
   ÚmaximumÚsqrtÚlogÚcosÚsin)r)   r*   ÚthÚrr   r   r   Úpair_uniform_to_normalŒ   s    r5   c           	      C   s6   t | ||ƒ\}}}}t|ƒ}t|ƒ}t||ƒ\}}|S )a  
    Given a :code:`seed` scalar and an :code:`offset` block,
    returns a block of random :code:`float32` in :math:`\mathcal{N}(0, 1)`

    :param seed: The seed for generating random numbers.
    :param offsets: The offsets to generate random numbers for.
    )r   r"   r5   )	r   r   r	   r&   r'   r   r)   r*   Ún1r   r   r   Úrandn•   s
    	r7   c                 C   s<   t | ||ƒ\}}}}t||ƒ\}}t||ƒ\}	}
|||	|
fS )a
  
    Given a :code:`seed` scalar and an :code:`offset` block,
    returns a 4 blocks of random :code:`float32` in :math:`\mathcal{N}(0, 1)`

    :param seed: The seed for generating random numbers.
    :param offsets: The offsets to generate random numbers for.
    )r-   r5   )r   r   r	   r)   r*   r+   r,   r6   Zn2Zn3Zn4r   r   r   Úrandn4x¥   s    	r8   )ZtritonÚ r   r
   r   Z	constexprÚ__annotations__r   r   r   ZN_ROUNDS_DEFAULTZjitr   r   r   r   r"   r$   r-   r5   r7   r8   r   r   r   r   Ú<module>   s4   


