CoolFace
Apppublic

declare-lab/tango2

sourceHugging Faceupdated 2y agoView on Hugging Face
92likes
testing_utils.cpython-39.pyc169 linesDownload Raw Back to __pycache__
1a

2��'d�C�@s�ddlZddlZddlZddlZddlZddlZddlZddlZddl	m3Z4ddlmZm
Z
ddlmZddlmZmZmZddlZddlZddlZddlZddlmZddlmZmZmZm Z m!Z!m"Z"m#Z#m$Z$dd	lm%Z%e�&�Z'e%e(�Z)e#��r�ddl*Z*d5ej+v�rJej+d6Z,gd�Z-e,e-v�r8e.de,�d
e-����e)�/de,���n`e*j0�1��rZdndZ,e�2e�2e*j3�j4�e�2d�kZ5e5�r�e6e*j7d�Z8e8�r�e*j7j9�1��r�dne,Z,dd�Z:dMdd�Z;dNdd�Z<dOdd�Z=e=ddd�Z>e=d dd�Z?dPd"d#�Z@d$d%�ZAd&d'�ZBd(d)�ZCd*d+�ZDd,d-�ZEd.d/�ZFd0d1�ZGd2d3�ZHd4d5�ZId6d7�ZJdQeeKejLfeeKejLd8�d9d:�ZMeKd;�d<d=�ZNeeKejOjOfejOjOd>�d?d@�ZPdReejLeKeKdA�dBdC�ZQejLdD�dEdF�ZRiZSdGdH�ZTdIdJ�ZUGdKdL�dL�ZVdS)S�N)�	strtobool)�BytesIO�StringIO)�Path)�List�Optional�Union)�version�)�BACKENDS_MAPPING�is_compel_available�is_flax_available�is_note_seq_available�is_onnx_available�is_opencv_available�is_torch_available�is_torch_version)�7get_loggerZDIFFUSERS_TEST_DEVICE)�cuda�cpu�mpsz+unknown torch backend for diffusers tests: z. Available backends are: ztorch_device overrode to rrz1.12rcOsZt�std��tj||g|�Ri|��sVdsVJd||�����d||���d���dS)Nz3PyTorch needs to be installed to use this function.FzMax diff is absolute z. Diff tensor is �.T)r�8ValueError�torch�allclose�abs�max)�a�b�args�kwargs�r!�R/home/deep/Projects/audio_diffusion/diffusers/src/diffusers/utils/testing_utils.py�torch_all_close>s9.r#�test_corrections.txt�expected_slicec10Cs�tj�d�}t�|�s t�|�}t|�����	��11tj���dd�}|�d|�d��}|�
d�\}}}|�
�d}t|d��*}	td	�||||g�|	d12�Wd�n1s�0YdS)N�PYTEST_CURRENT_TEST�13��tensorz = np.arrayz::rr�;)�file)�os�environ�getr�	is_tensor�14from_numpy�str�detachr�flatten�to�float32�replace�split�open�print�join)15r)�filenameZexpected_tensor_nameZ	test_name�16tensor_strZ17output_strZ	test_fileZ18test_classZtest_fn�fr!r!r"�print_tensor_testFs1920$r>cCsVt��dd}tj�tj�|��}|�d�s<tj�|�}q$|rNtj�||�S|SdS)a21    Args:22        append_path: optional path to append to the tests dir path23    Return:24        The full path to the `tests` dir, so that the tests can be invoked from anywhere. Optionally `append_path` is25        joined after the `tests` dir the former is provided.26    r27�testsN)�inspect�stackr,�path�abspath�dirname�endswithr:)Zappend_pathZcaller__file__Z	tests_dirr!r!r"�
get_tests_dirUs	28rFFcCsZztj|}Wnty$|}Yn20zt|�}Wn"tyTtd|�d���Yn0|S)NzIf set, z must be yes or no.)r,r-�KeyErrorrr)�key�default�value�_valuer!r!r"�parse_flag_from_envjs29rLZRUN_SLOW)rIZRUN_NIGHTLY��?cCs`|durt}d}|D]}||9}qg}t|�D]}|�|��|�q.tj|tjd��|���S)zCreates a random float32 tensorNr30)�data�dtype)	�31global_rng�range�append�randomrr)�float�view�32contiguous)�shape�scale�rng�nameZ33total_dims�dim�values�_r!r!r"�
floats_tensor~s34r^cCst�td�|�S)z�35    Decorator marking a test as slow.36 37    Slow tests are skipped by default. Set the RUN_SLOW environment variable to a truthy value to run them.38 39    ztest is slow)�unittest�40skipUnless�_run_slow_tests�Z	test_caser!r!r"�slow�srccCst�td�|�S)z�41    Decorator marking a test that runs nightly in the diffusers CI.42 43    Slow tests are skipped by default. Set the RUN_NIGHTLY environment variable to a truthy value to run them.44 45    ztest is nightly)r_r`�_run_nightly_testsrbr!r!r"�nightly�srecCst�t�d�|�S)zo46    Decorator marking a test that requires PyTorch. These tests are skipped when PyTorch isn't installed.47    ztest requires PyTorch)r_r`rrbr!r!r"�
require_torch�srfcCst�t�otdd�d�|�S)zl48    Decorator marking a test that requires PyTorch 2. These tests are skipped when it isn't installed.49    z>=z2.0.0ztest requires PyTorch 2)r_r`rrrbr!r!r"�require_torch_2�s�rgcCst�t�otdkd�|�S)z8Decorator marking a test that requires CUDA and PyTorch.rztest requires PyTorch+CUDA)r_r`r�torch_devicerbr!r!r"�require_torch_gpu�s�ricCst�tdkd�|�S)z9Decorator marking a test to skip if torch_device is 'mps'rztest requires non 'mps' device)r_r`rhrbr!r!r"�skip_mps�srjcCst�t�d�|�S)zv50    Decorator marking a test that requires JAX & Flax. These tests are skipped when one / both are not installed51    ztest requires JAX & Flax)r_r`r
rbr!r!r"�require_flax�srkcCst�t�d�|�S)z�52    Decorator marking a test that requires compel: https://github.com/damian0815/compel. These tests are skipped when53    the library is not installed.54    ztest requires compel)r_r`rrbr!r!r"�require_compel�srlcCst�t�d�|�S)zw55    Decorator marking a test that requires onnxruntime. These tests are skipped when onnxruntime isn't installed.56    ztest requires onnxruntime)r_r`rrbr!r!r"�require_onnxruntime�srmcCst�t�d�|�S)zq57    Decorator marking a test that requires note_seq. These tests are skipped when note_seq isn't installed.58    ztest requires note_seq)r_r`rrbr!r!r"�require_note_seq�srn)�arry�59local_path�returnc60Cs�t|t�r�|durJtj�|d�|�d�d|�d�d|�d�dg��S|�d�s^|�d�r�t�|�}|�	�t61�t|j
��}q�tj�|�r�t62�|�}q�td|�d���nt|t63j�r�ntd	��|S)64N�/����������������http://�https://�IIncorrect path or url, URLs must start with `http://` or `https://`, and � is not a valid pathzjIncorrect format used for numpy ndarray. Should be an url linking to an image, a local path, or a ndarray.)�65isinstancer1r,rBr:r7�66startswith�requestsr.�raise_for_status�np�loadr�content�isfiler�ndarray)rorp�responser!r!r"�67load_numpy�s$6886970��r�)�urlcCs&t�|�}|��t�t|j��}|S�N)r|r.r}rrrr�)r�r�ror!r!r"�load_pt�s71r�)�imagerqcCs�t|t�rd|�d�s|�d�r8tj�tj|dd�j�}q�t	j72�|�rRtj�|�}q�td|�d���nt|tjj�rx|}ntd��tj
�|�}|�d�}|S)	z�73    Args:74    Loads `image` to a PIL Image.75        image (`str` or `PIL.Image.Image`):76            The image to convert to the PIL Image format.77    Returns:78        `PIL.Image.Image`: A PIL Image.79    rvrwT)�streamrxryzdIncorrect format used for image. Should be an url linking to an image, a local path, or a PIL image.�RGB)rzr1r{�PIL�Imager8r|r.�rawr,rBr�r�ImageOps�exif_transpose�convert)r�r!r!r"�80load_image�s 	8182��83r�)�video_frames�output_video_pathrqc84Cs�t�rddl}nttdd�d���|dur<tjdd�j}|jd�}|dj	\}}}|j85||d||fd	�}tt|��D] }|�
|||j�}	|�|	�qx|S)86Nr�opencvr87�export_to_videoz.mp4)�suffixZmp4v�)�fpsZ	frameSize)r�cv2�ImportErrorr�format�tempfile�NamedTemporaryFilerZZVideoWriter_fourccrWZVideoWriterrQ�lenZcvtColorZ
COLOR_RGB2BGR�write)88r�r�r�Zfourcc�h�w�cZvideo_writer�i�imgr!r!r"r�s8990r�)rqcCs2|�d�r|�d�r*tj�dtj�|��}t|�S)NrvrwzEhttps://huggingface.co/datasets/fusing/diffusers-testing/resolve/main)r{r,rBr:�urllib�parse�quoter�)rBr!r!r"�
load_hf_numpy+s91�r�cCs*d}|tvr&|j|dddd�dt|<dS)z�92    This function is to be called from `conftest.py` via `pytest_addoption` wrapper that has to be defined there.93 94    It allows loading both `conftest.py` files at once without causing a failure due to adding the same `pytest`95    option.96 97    z--make-reports�storeFzSgenerate report files. The value of this option is used as a prefix to report names)�actionrI�helpr98N)�pytest_opt_registeredZ	addoption)�parser�optionr!r!r"�pytest_addoption_shared:s�r�c99s�ddlm}t��sd�|j}|��}|jj}|j}d�t��j	ddd���fdd�d	D�}g}|j100��D]"}	|	D]}101t|102d103�rt|�
|104�qtql|�rN|jdd�dd
�t|dd���}d}|�d�t|�D]^\}
}105|106j|k�r|�t|�|
�d|�d���q.|�|107jd�d|108jd�d|109j�d��q�Wd�n1�sD0Ydd�}d|j_t|dd��$}|||�|_|��Wd�n1�s�0Yt|dd��$}|||�|_||�Wd�n1�s�0Yd|j_t|d d��$}|||�|_|��Wd�n1�s(0Yt|d!d��$}|||�|_|��Wd�n1�sl0Yt|d"d��,}|||�|_|��|��Wd�n1�s�0Yd#|_t|d$d��$}|||�|_|��Wd�n1�s0Yt|d%d��$}|||�|_|��Wd�n1�sF0Yt|d&d��$}|||�|_|��Wd�n1�s�0Y||_||_||j_dS)'a�110    Generate multiple reports at the end of test suite run - each report goes into a dedicated file in the current111    directory. The report files are prefixed with the test suite name.112 113    This function emulates --duration and -rA pytest arguments.114 115    This function is to be called from `conftest.py` via `pytest_terminal_summary` wrapper that has to be defined116    there.117 118    Args:119    - tr: `terminalreporter` passed from `conftest.py`120    - id: unique id like `tests` or `examples` that will be incorporated into the final reports filenames - this is121      needed as some jobs have multiple runs of pytest, so we can't have them overwrite each other.122 123    NB: this functions taps into a private _pytest API and while unlikely, it could break should124    pytest do internal changes - also it calls default internal methods of terminalreporter which125    can be hijacked by various `pytest-` plugins and interfere.126 127    r)�create_terminal_writerr?�reportsT)�parents�exist_okc	s$i|]}|��d��d|�d��qS)rrr]z.txtr!)�.0�k��dir�idr!r"�128<dictcomp>ms�z0pytest_terminal_summary_main.<locals>.<dictcomp>)	�	durations�errors�
failures_long�failures_short�
failures_line�passes�stats�
summary_short�warnings�durationcSs|jSr�)r�)�xr!r!r"�<lambda>��z.pytest_terminal_summary_main.<locals>.<lambda>)rH�reverser�r�g�������?zslowest durations129z
 durations < z secs were omittedz02.2fzs z<8� r'Nc	Ssp|�d�}|sdS|�dd�|D]H}|�|�}|jd|ddd�t�dd|jd	tjtjB�}|j�	|�q"dS)130N�failed�=zFAILURES SHORT STACKr]T)�red�boldz.*_ _ _ (_ ){10,}_ _ r(r)131Z132getreportsZ	write_sepZ_getfailureheadline�re�subZlongreprtext�M�S�_tw�line)�trr��rep�msgZlongreprr!r!r"�summary_failures_short�s133134z<pytest_terminal_summary_main.<locals>.summary_failures_short�autor�r�r�r�r�r�ZwPpsxXEfr�r�r�)Z_pytest.configr�r��configZget_terminal_writerr�ZtbstyleZreportcharsr�mkdirr�r\�hasattrrR�sortr8r��	enumerater��whenZnodeidr�Zsummary_failures�summary_errors�summary_warningsZsummary_passesZshort_test_summaryZ
summary_stats)r�r�r�r�Zorig_writerZorig_tbstyleZorig_reportcharsZreport_filesZdlistZreplistr�r=Z
durations_minr�r�r!r�r"�pytest_terminal_summary_mainMsv�135136H((((((((r�c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd137S)�
CaptureLoggeraG138    Args:139    Context manager to capture `logging` streams140        logger: 'logging` logger object141    Returns:142        The captured output is available via `self.out`143    Example:144    ```python145    >>> from diffusers import logging146    >>> from diffusers.testing_utils import CaptureLogger147 148    >>> msg = "Testing 1, 2, 3"149    >>> logging.set_verbosity_info()150    >>> logger = logging.get_logger("diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.py")151    >>> with CaptureLogger(logger) as cl:152    ...     logger.info(msg)153    >>> assert cl.out, msg + "154"155    ```156    cCs&||_t�|_t�|j�|_d|_dS)Nr()�loggerr�io�logging�
StreamHandler�sh�out)�selfr�r!r!r"�__init__�szCaptureLogger.__init__cCs|j�|j�|Sr�)r��157addHandlerr��r�r!r!r"�	__enter__�szCaptureLogger.__enter__cGs|j�|j�|j��|_dSr�)r��
removeHandlerr�r��getvaluer�)r��excr!r!r"�__exit__�szCaptureLogger.__exit__cCsd|j�d�S)Nz158captured: r')r�r�r!r!r"�__repr__�szCaptureLogger.__repr__N)�__name__�159__module__�__qualname__�__doc__r�r�r�r�r!r!r!r"r��s160r�)r$r%)N)F)rMNN)N)N)Wr@r�r,rSr�r�r_�urllib.parser��distutils.utilrr�rr�pathlibr�typingrrr�numpyr~�	PIL.Imager��PIL.ImageOpsr|�	packagingr	�import_utilsrrr
rrrrrr�RandomrPr�r�rr-rhZavailable_backendsr�infor�is_availabler��__version__�base_versionZis_torch_higher_equal_than_1_12r��backendsZmps_backend_registeredrr#r>rFrLrardr^rcrerfrgrirjrkrlrmrnr1r�r�r�r�r�r�r�r�r�r�r�r!r!r!r"�<module>s�(161162163����164165166167168169	$