o
    oh                     @   s  d dl mZ ddlmZ ddlmZ dZeefdejfddZeefdejfd	d
Z	eefdejfddZ
eefdejfddZedd ZeefdejfddZeefdejfddZedd ZeefdejfddZeefdejfddZdS )   )jit   )core)math
   n_roundsc                 C   s   | j tjkrd}d}d}	d}
nt| j tjkd d}d}d}	d	}
t|D ]?}|	}|
}| |}}t|||A |A } t|||A |A }tj||d
d}tj||d
d}tj	||d
d}tj	||d
d}q&| |||fS )zV
    Run `n_rounds` rounds of Philox for state (c0, c1, c2, c3) and key (k0, k1).
    l   yn< l   .v l   S$ l   W= z"dtype not supported in philox_impll   |~fq	 l   ;'	:=[ l   lB;: l   W$*	R F)sanitize_overflow)
dtypetluint32static_assertuint64static_ranger   umulhimuladd)c0c1c2c3k0k1r   PHILOX_KEY_APHILOX_KEY_BPHILOX_ROUND_APHILOX_ROUND_B_AB_c0_c2 r!   j/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/triton/language/random.pyphilox_impl   s*   
r#   c           	      C   s  t | } t | j  | t j} t |}t |}t |}t |}t |jjdkrFt j	}| d? d@ t j	}| d@ t j	}nt t |jjdkd t j}t j
dd|d}| }|j|dd	}|j|dd	}|j|dd	}|j|dd	}t|||||||S )
N    l    @   z bitwidth not supported in philox)r       )r	   Tbitcast)r
   	to_tensorr   r	   is_inttor   	constexprprimitive_bitwidthr   fullr#   )	seedr   r   r   r   r   	int_dtypeseed_hiseed_lor!   r!   r"   philox-   s(   




r3   c                 C   s   t | ||\}}}}|S )aq  
    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 offset: The offsets to generate random numbers for.
    )	randint4x)r/   offsetr   retr   r!   r!   r"   randintF   s   r7   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&   )r3   )r/   r5   r   _0r!   r!   r"   r4   V   s   r4   c                 C   s   t | jt jkst | jt jkr| jt jdd} d}nt t | jt jkp0t | jt jk | jt jdd} d}t 	| dk |  d | } | | S )zo
    Numerically stable function to convert a random uint into a random float uniformly sampled in [0, 1).
    Tr'   g=g   ;r&   r   )
r
   r,   r	   r   int32r+   r   r   int64where)xscaler!   r!   r"   uint_to_uniform_floatt   s   $*r>   c                 C   s   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.
    )r7   r>   )r/   r5   r   sourcer!   r!   r"   rand   s   	r@   c                 C   s@   t | ||\}}}}t|}t|}t|}	t|}
|||	|
fS )a   
    Given a :code:`seed` scalar and an :code:`offsets` block,
    returns 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.
    )r4   r>   )r/   offsetsr   i1i2i3i4u1u2u3u4r!   r!   r"   rand4x   s   	rJ   c                 C   sD   t d| } d| }tdt|  }|t| |t| fS )zBox-Muller transformgHz>g-DT!@g       )r
   maximumr   sqrtlogcossin)rF   rG   thrr!   r!   r"   pair_uniform_to_normal   s   rR   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.
    )r4   r>   rR   )	r/   r5   r   rB   rC   r   rF   rG   n1r!   r!   r"   randn   s
   	rT   c                 C   s<   t | ||\}}}}t||\}}t||\}	}
|||	|
fS )a	  
    Given a :code:`seed` scalar and an :code:`offset` block,
    returns 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.
    )rJ   rR   )r/   r5   r   rF   rG   rH   rI   rS   n2n3n4r!   r!   r"   randn4x   s   	rX   N)runtime.jitr    r   r
   r   N_ROUNDS_DEFAULTr,   r#   r3   r7   r4   r>   r@   rJ   rR   rT   rX   r!   r!   r!   r"   <module>   s0     

