o
    XÛ·i’  ã                   @  s    d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	da
dadaddd„Zdd„ Zd aejdd	„ ƒZd
d„ Zdd„ Zdd„ Zdd„ ZdS )é    )ÚannotationsNTc                 C  s‚   t  ¡ atj  ¡ atj jja	tj  
¡  tj jjt	usJ ‚| s.t  ¡  tj  ¡  tj  ¡  d S t  d¡ tj  d¡ tj  d¡ d S )Néc   éd   ée   )ÚrandomÚgetstateÚ_old_python_random_stateÚnumpyÚ	get_stateÚ_old_numpy_random_stateÚcupyÚ
_generatorÚ_random_statesÚ_old_cupy_random_statesÚreset_statesÚseed)Údeterministic© r   úJ/home/ubuntu/vllm_env/lib/python3.10/site-packages/cupy/testing/_random.pyÚdo_setup   s   


ÿ

r   c                   C  s0   t  t¡ tj  t¡ ttj j_	d ad ad ad S ©N)
r   Úsetstater   r	   Ú	set_stater   r   r   r   r   r   r   r   r   Údo_teardown+   s   

r   c                   C  s   t dksJ dƒ‚d S )Nr   z?_setup_random() and _teardown_random() must be called in pairs.)Ú_nest_countr   r   r   r   Ú_check_teardown=   s   r   c                  C  s4   t dkrtttj dd¡ƒƒ} t|  ƒ t d7 a dS )zHSets up the deterministic random states of ``numpy`` and ``cupy``.

    r   Ú!CUPY_TEST_RANDOM_NONDETERMINISTICÚ0é   N)r   ÚboolÚintÚosÚenvironÚgetr   )Únondeterministicr   r   r   Ú_setup_randomC   s   
ÿ
r%   c                   C  s.   t dksJ dƒ‚t d8 a t dkrtƒ  dS dS )zMTears down the deterministic random states set up by ``_setup_random``.

    r   z!_setup_random has not been calledr   N)r   r   r   r   r   r   Ú_teardown_randomO   s
   
ÿr&   c                   C  s   t dksJ dƒ‚tj d¡S )Nr   zrandom is not set upiÿÿÿ)r   r	   r   Úrandintr   r   r   r   Úgenerate_seedZ   s   r(   c                  C  s   dd„ } | S )zÙDecorator that fixes random numbers in a test.

    This decorator can be applied to either a test case class or a test method.
    It should not be applied within ``condition.retry`` or
    ``condition.repeat``.
    c                   s†   t ˆ tjƒrˆ j d¡rt ˆ ¡‡ fdd„ƒ}|S t ˆ tƒr<tˆ t	j
ƒr<ˆ }dd„ }dd„ }||jƒ|_||jƒ|_|S td ˆ ¡ƒ‚)	NÚtest_c                   s2   t ƒ  zˆ | g|¢R i |¤Ž W tƒ  d S tƒ  w r   )r%   r&   )ÚselfÚargsÚkw©Úimplr   r   Ú	test_funcp   s   z0fix_random.<locals>.decorator.<locals>.test_funcc                   ó   ‡ fdd„}|S )Nc                   s   t ƒ  ˆ | ƒ d S r   )r%   ©r*   ©Úfr   r   Úfunc}   s   z?fix_random.<locals>.decorator.<locals>.wrap_setUp.<locals>.funcr   ©r3   r4   r   r2   r   Ú
wrap_setUp|   s   z1fix_random.<locals>.decorator.<locals>.wrap_setUpc                   r0   )Nc                   s   z
ˆ | ƒ W t ƒ  d S t ƒ  w r   )r&   r1   r2   r   r   r4   ƒ   s   
zBfix_random.<locals>.decorator.<locals>.wrap_tearDown.<locals>.funcr   r5   r   r2   r   Úwrap_tearDown‚   s   z4fix_random.<locals>.decorator.<locals>.wrap_tearDownzCan't apply fix_random to {})Ú
isinstanceÚtypesÚFunctionTypeÚ__name__Ú
startswithÚ	functoolsÚwrapsÚtypeÚ
issubclassÚunittestÚTestCaseÚsetUpÚtearDownÚ
ValueErrorÚformat)r.   r/   Úklassr6   r7   r   r-   r   Ú	decoratorl   s   
ÿzfix_random.<locals>.decoratorr   )rH   r   r   r   Ú
fix_random_   s   $rI   )T)Ú
__future__r   Úatexitr=   r	   r!   r   r9   rA   r   r   r   r   r   r   r   Úregisterr   r%   r&   r(   rI   r   r   r   r   Ú<module>   s*    

