o
    wi(                     @   s  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mZ d dlm	Z	m
Z
mZmZ d dlmZ d dlmZ d dlmZ d dlZd dlmZ dZd	Zd
Zdd Zejdd Zejdddd Zejdddd Zejdddd Zejdddd Zejdddd Zejdddd Z ejdddd  Z!d+d"d#Z"ejddd$ee#e$f fd%d&Z%ejddd$ee#e$f fd'd(Z&d)d* Z'dS ),    N)mkdir)dirnameexistsgetsizejoin)Path)rmtree)Tuple)	Singletonztest_data.tar.gzz;https://github.com/NVIDIA/NeMo/releases/download/v1.0.0rc1/z.datac                 C   sT   | j dddd | j dddd | j dddd | j d	d
dd | j dddd dS )z
    Additional command-line arguments passed to pytest.
    For now:
        --cpu: use CPU during testing (DEFAULT: GPU)
        --use_local_test_data: use local test data/skip downloading from URL/GitHub (DEFAULT: False)
    --cpu
store_truezCpass that argument to use CPU during testing (DEFAULT: False = GPU))actionhelpz--use_local_test_dataz[pass that argument to use local test data/skip downloading from URL/GitHub (DEFAULT: False)--with_downloadszHpass this argument to active tests which download models from the cloud.z--relax_numba_compatstore_falsezpnumba compatibility checks will be relaxed to just availability of cuda, without cuda compatibility matrix check	--nightlyzepass this argument to activate tests which have been marked as nightly for nightly quality assurance.N)	addoption)parser r   K/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/tests/conftest.pypytest_addoption%   s.   
r   c                 C   s   | j drdS dS )z?Simple fixture returning string denoting the device [CPU | GPU]r   CPUGPU)config	getoption)requestr   r   r   deviceF   s   r   T)autousec                 C   s>   | j dr| j djd |krtd| d S d S d S )Nrun_only_onr   zskipped on this device: {})nodeget_closest_markerargspytestskipformatr   r   r   r   r   run_only_on_device_fixtureO   s
   r&   c                 C   .   | j dr| jdstd d S d S d S )Nwith_downloadsr   z_To run this test, pass --with_downloads option. It will download (and cache) models from cloud.r   r    r   r   r"   r#   r%   r   r   r   downloads_weightsV      r*   c                 C   r'   )Nnightlyr   zTo run this test, pass --nightly option. It will run any tests marked with "nightly". Currently, These tests are mostly used for QA.r)   r%   r   r   r   run_nightly_test_for_qa_   r+   r-   c                   c   s    t d r	J t d rJ t d rJ d V  t d r(tddd t d r4tddd t d rBtddd d S d S )Nz./lightning_logsz./NeMo_experimentsz./nemo_experimentsT)ignore_errors)r   r   r   r   r   r   r   cleanup_local_folderh   s   r/   c                   C   s
   i t _d S N)r
   _Singleton__instancesr   r   r   r   reset_singletonsy   s   
r2   c                  c   s,    t tj} d V  tj  tj|  d S r0   )dictosenvironclearupdate)original_envr   r   r   reset_env_vars~   s
   

r9   session)scopec                  C   s   t ttt} | S )z
    Fixture returns test_data_dir.
    Use the highest fixture scope `session` to allow other fixtures with any other scope to use it.
    )r   r   __file____TEST_DATA_SUBDIR)test_data_dir_r   r   r   test_data_dir   s   r?   Fc              	   C   s   t | rH|st|  n=t 1}td t|| td t|  t|  td ttj	
|tj	|| W d    n1 sCw   Y  t | sPt|  |d ur]|s]tj|| td| t|}|j| d |  d S )Nz,Copying local tarfile to temporary storage..z%Deleting test dir to cleanup old dataz#Restoring local tarfile to test dirz0Extracting the `{}` test archive, please wait...)path)r   r   tempfileTemporaryDirectoryprintshutilcopy2r   r4   r@   r   basenameurllibr   urlretriever$   tarfileopen
extractallclose)test_dirtest_data_archiveurl
local_datatemp_dirtarr   r   r   extract_data_from_tar   s(   

 

rS   returnc               
   C   sH   z	ddl m}  W dS  ty# } ztj|dd W Y d }~dS d }~ww )Nr   k2)Tzk2 is appropriate.T)exc_info)Fz6k2 is not available or does not meet the requirements.)nemo.core.utils.k2_guardrV   	Exceptionlogging	exception)rV   er   r   r   k2_is_appropriate   s   r]   c                 C   sF   | d s| S dd l }ddlm} |j r|jrdS |j r!dS dS )Nr   rU   )Tzk2 supports CUDA.)FzFk2 does not support CUDA. Consider using a k2 build with CUDA support.)Fz'k2 needs CUDA to be available in torch.)torchrX   rV   cudais_available	with_cuda)r]   r^   rV   r   r   r   k2_cuda_is_enabled   s   
rb   c           	   	   C   s  |  dd |  dd |  dd tttt}ttttt}zt|}W n   d}Y | jjrH|dkr?t	
d| n	tdt|| d}| jjsztt }tj|}W n   |dkrkt	
d	| ntd
||| Y dS Y | }t|d }||krtdtt t|||| jjd ntdt|| n	t||| jjd | jjdurddlm} td| jj |j| jjd dS dS )a  
    Initial configuration of conftest.
    The function checks if test_data.tar.gz is present in tests/.data.
    If so, compares its size with github's test_data.tar.gz.
    If file absent or sizes not equal, function downloads the archive from github and unpacks it.
    markerszErun_only_on(device): runs the test only on a given device [CPU | GPU]z?with_downloads: runs the test using data present in tests/.dataz&nightly: runs the nightly test for QA.z+Test data `{}` is not present in the systemzAUsing the local `{}` test archive ({}B) found in the `{}` folder.NzBTest data not present in the system and cannot access the '{}' URLzOCannot access the '{}' URL, using the test data ({}B) found in the `{}` folder.zContent-Lengthz;Downloading the `{}` test archive from `{}`, please wait...)rO   rP   z9A valid `{}` test archive ({}B) found in the `{}` folder.)rP   r   )numba_utilszSetting numba compat :)strict)addinivalue_liner   r   r<   r=   __TEST_DATA_FILENAMEr   optionuse_local_test_datar"   exitr$   rC   __TEST_DATA_URLrG   r   urlopeninfointrS   relax_numba_compatnemo.core.utilsre   set_numba_compat_strictness)	r   rM   rN   test_data_local_sizerO   umetatest_data_remote_sizere   r   r   r   pytest_configure   sz   
rw   )NF)(rZ   r4   os.pathrD   rI   rA   urllib.requestrG   r   r   r   r   r   pathlibr   r   typingr	   r"   nemo.utils.metaclassesr
   rh   rl   r=   r   fixturer   r&   r*   r-   r/   r2   r9   r?   rS   boolstrr]   rb   rw   r   r   r   r   <module>   sP   !



















