o
    piZ                  
   @   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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mZ d dlmZmZ d dlmZ d dlmZmZmZmZm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% dd	l&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2 dd
lm3Z3 e4 Z5e3e6Z7e- oe%8e%8ej9%dj:e%8dkZ;e2 oe%8e%8ej9%dj:e%8dkZ<e;oe<Z=e/ rfd dl>Z>dej?v rej?d Z@zeAe@ZBW n eCy ZD zeCde@ deD eDdZD[Dww dej?v r6ej?d ZEze>FeEZBW n eGy, ZD zeGdeE eDdZD[Dww e7HdeE  n0e>jIJ r>dndZEe%8e%8e>jKj:e%8dkZLeLrfeMe>jNdZOeOrde>jNjPJ rddneEZEdd ZQdd ZR				ddd ZSdd!d"ZTd#eUfd$d%ZVdd'd(ZWeWd)d&d*ZXeWd+d&d*ZYeWd,d&d*ZZdd.d/Z[d0d1 Z\d2d3 Z]d4d5 Z^d6d7 Z_d8d9 Z`d:d; Zad<d= Zbd>d? Zcd@dA ZddBdC ZedDdE ZfdFdG ZgdHdI ZhdJdK ZidLdM ZjdNdO ZkdPdQ ZldRdS ZmdTdU ZndVdW ZodXdY ZpdZd[ Zqd\d] Zrdd^eesejtf d_ees d#ejtfd`daZudbesfdcddZvdeeesejwjwf d#ejwjwfdfdgZxdeejwdheUfdidjZyddeeejwjw dkesd#esfdldmZzedndo Z{ddpesfdqdrZ|ddsesfdtduZ}ddveejt dwesd#esfdxdyZ~d#ejtfdzd{Zi Zd|d} Zd~d ZddeUdee dees fddZdddZG dd dZdd Zdd Zdd Zdd Ze/ rddd&ddZe>jIjddddZe>jIjdd dd d dZe>jIje>je>jdZdesdeesef fddZdesdeUfddZdesfddZdesfddZdesfddZe/ rdeesef desfddZdej?v rej?d Zee s$ede zeded ZW n eyD ZD zede eDdZD[Dww eAeZzejZW n ey[   edw dej?v rxeEekrxdeE de dZed7 ZeeeZEeed eed eed eed dS dS dS )    N)contextmanager)BytesIOStringIO)Path)CallableDictListOptionalUnion)norm)version   )BACKENDS_MAPPINGis_compel_availableis_flax_availableis_note_seq_availableis_onnx_availableis_opencv_availableis_peft_availableis_timm_availableis_torch_availableis_torch_versionis_torchsde_availableis_transformers_available)
get_loggerpeftz0.5transformersz4.33DIFFUSERS_TEST_BACKENDz+Failed to import `DIFFUSERS_TEST_BACKEND` 'zg'! This should be the name of an installed module                     to enable a specified backend.):
DIFFUSERS_TEST_DEVICEzRUnknown testing device specified by environment variable `DIFFUSERS_TEST_DEVICE`: ztorch_device overrode to cudacpuz1.12mpsc                 O   sV   t  stdtj| |g|R i |s)J d| |    d| |   ddS )Nz3PyTorch needs to be installed to use this function.FzMax diff is absolute z. Diff tensor is .T)r   
ValueErrortorchallcloseabsmax)abargskwargs r,   [/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/diffusers/utils/testing_utils.pytorch_all_close^   s
   *r.   c                 C   s,   t | |t| t|  }d|  }|S )N      ?)npdotr   mean)r(   r)   
similaritydistancer,   r,   r-    numpy_cosine_similarity_distancef   s   r5   test_corrections.txtexpected_slicec                 C   s   |rt jdd tjd}t | st | } |r&| ddd dd df } t|  	 
 t jdd}|d	| d
}|d\}}	}
|
 d }
t|d}td||	|
|g|d W d    d S 1 smw   Y  d S )Ni'  )	thresholdPYTEST_CURRENT_TESTr   
 tensorz = np.arrayz::r(   file)r$   set_printoptionsosenvironget	is_tensor
from_numpystrdetachr    flattentofloat32replacesplitopenprintjoin)r>   limit_to_slicesmax_torch_printfilenameexpected_tensor_name	test_name
tensor_str
output_str	test_file
test_classtest_fnfr,   r,   r-   print_tensor_testm   s   

$"r\   c                 C   sZ   t  d d }tjtj|}|ds"tj|}|dr| r+t||  S |S )a  
    Args:
        append_path: optional path to append to the tests dir path
    Return:
        The full path to the `tests` dir, so that the tests can be invoked from anywhere. Optionally `append_path` is
        joined after the `tests` dir the former is provided.
    r   tests)	inspectstackrB   pathabspathdirnameendswithr   as_posix)append_pathcaller__file__	tests_dirr,   r,   r-   get_tests_dir   s   	

rh   returnc                 C   s.   |   } | dv r
dS | dv rdS td|  )z
    Converts a string representation of truth to `True` (1) or `False` (0). True values are `y`, `yes`, `t`, `true`,
    `on`, and `1`; False value are `n`, `no`, `f`, `false`, `off`, and `0`;
    )yyesttrueon1r   )nnor[   falseoff0r   zinvalid truth value )lowerr#   )valuer,   r,   r-   str_to_bool   s   rw   Fc                 C   sV   zt j|  }W n ty   |}Y |S w zt|}W |S  ty*   td|  dw )NzIf set, z must be yes or no.)rB   rC   KeyErrorrw   r#   )keydefaultrv   _valuer,   r,   r-   parse_flag_from_env   s   
r|   RUN_SLOW)rz   RUN_NIGHTLYRUN_COMPILEr/   c                 C   s`   |du rt }d}| D ]}||9 }q
g }t|D ]}|| |  qtj|tjd|  S )zCreates a random float32 tensorNr   )datadtype)	
global_rngrangeappendrandomr$   r>   floatview
contiguous)shapescalerngname
total_dimsdimvalues_r,   r,   r-   floats_tensor   s   
r   c                 C      t td| S )z
    Decorator marking a test as slow.

    Slow tests are skipped by default. Set the RUN_SLOW environment variable to a truthy value to run them.

    ztest is slow)unittest
skipUnless_run_slow_tests	test_caser,   r,   r-   slow      r   c                 C   r   )z
    Decorator marking a test that runs nightly in the diffusers CI.

    Slow tests are skipped by default. Set the RUN_NIGHTLY environment variable to a truthy value to run them.

    ztest is nightly)r   r   _run_nightly_testsr   r,   r,   r-   nightly   r   r   c                 C   r   )z
    Decorator marking a test that runs compile tests in the diffusers CI.

    Compile tests are skipped by default. Set the RUN_COMPILE environment variable to a truthy value to run them.

    ztest is torch compile)r   r   _run_compile_testsr   r,   r,   r-   is_torch_compile   r   r   c                 C      t t d| S )zo
    Decorator marking a test that requires PyTorch. These tests are skipped when PyTorch isn't installed.
    test requires PyTorch)r   r   r   r   r,   r,   r-   require_torch      r   c                 C   s   t t o	tddd| S )zl
    Decorator marking a test that requires PyTorch 2. These tests are skipped when it isn't installed.
    z>=z2.0.0ztest requires PyTorch 2)r   r   r   r   r   r,   r,   r-   require_torch_2   s   r   c                 C   s   t t otdkd| S )z8Decorator marking a test that requires CUDA and PyTorch.r   ztest requires PyTorch+CUDAr   r   r   torch_devicer   r,   r,   r-   require_torch_gpu      r   c                 C   s   t t otdkd| S )zJDecorator marking a test that requires an accelerator backend and PyTorch.r    z!test requires accelerator+PyTorchr   r   r,   r,   r-   require_torch_accelerator  r   r   c                 C   s6   t  s
td| S ddl}t|j dkd| S )a  
    Decorator marking a test that requires a multi-GPU setup (in PyTorch). These tests are skipped on a machine without
    multiple GPUs. To run *only* the multi_gpu tests, assuming all test names contain multi_gpu: $ pytest -sv ./tests
    -k "multi_gpu"
    r   r   Nr   ztest requires multiple GPUs)r   r   skipr$   r   r   device_count)r   r$   r,   r,   r-   require_torch_multi_gpu  s   r   c                 C      t ttd| S )zZDecorator marking a test that requires an accelerator with support for the FP16 data type.z+test requires accelerator with fp16 support)r   r   _is_torch_fp16_availabler   r   r,   r,   r-   #require_torch_accelerator_with_fp16     r   c                 C   r   )zZDecorator marking a test that requires an accelerator with support for the FP64 data type.z+test requires accelerator with fp64 support)r   r   _is_torch_fp64_availabler   r   r,   r,   r-   #require_torch_accelerator_with_fp64#  r   r   c                 C   s   t t ottd| S )zPDecorator marking a test that requires an accelerator with support for training.z/test requires accelerator with training support)r   r   r   backend_supports_trainingr   r   r,   r,   r-   'require_torch_accelerator_with_training*  s   r   c                 C   s   t tdkd| S )z9Decorator marking a test to skip if torch_device is 'mps'r!   ztest requires non 'mps' device)r   r   r   r   r,   r,   r-   skip_mps2  s   r   c                 C   r   )zv
    Decorator marking a test that requires JAX & Flax. These tests are skipped when one / both are not installed
    ztest requires JAX & Flax)r   r   r   r   r,   r,   r-   require_flax7  r   r   c                 C   r   )z
    Decorator marking a test that requires compel: https://github.com/damian0815/compel. These tests are skipped when
    the library is not installed.
    ztest requires compel)r   r   r   r   r,   r,   r-   require_compel>  s   r   c                 C   r   )zw
    Decorator marking a test that requires onnxruntime. These tests are skipped when onnxruntime isn't installed.
    ztest requires onnxruntime)r   r   r   r   r,   r,   r-   require_onnxruntimeF  r   r   c                 C   r   )zq
    Decorator marking a test that requires note_seq. These tests are skipped when note_seq isn't installed.
    ztest requires note_seq)r   r   r   r   r,   r,   r-   require_note_seqM  r   r   c                 C   r   )zq
    Decorator marking a test that requires torchsde. These tests are skipped when torchsde isn't installed.
    ztest requires torchsde)r   r   r   r   r,   r,   r-   require_torchsdeT  r   r   c                 C   r   )z
    Decorator marking a test that requires PEFT backend, this would require some specific versions of PEFT and
    transformers.
    ztest requires PEFT backendr   r   USE_PEFT_BACKENDr   r,   r,   r-   require_peft_backend[  s   r   c                 C   r   )zi
    Decorator marking a test that requires timm. These tests are skipped when timm isn't installed.
    ztest requires timm)r   r   r   r   r,   r,   r-   require_timmc  r   r   c                        fdd}|S )z
    Decorator marking a test that requires PEFT backend with a specific version, this would require some specific
    versions of PEFT and transformers.
    c                    s@   t  otttjdjt k}t|d  | S )Nr   z9test requires PEFT backend with the version greater than r   r   parse	importlibmetadatabase_versionr   r   )r   correct_peft_versionpeft_versionr,   r-   	decoratorp  s   

z/require_peft_version_greater.<locals>.decoratorr,   )r   r   r,   r   r-   require_peft_version_greaterj  s   r   c                    r   )Nc                    sB   t  otttjdjt k}t|d  d| S )N
acceleratez7Test requires accelerate with the version greater than r"   r   )r   correct_accelerate_versionaccelerate_versionr,   r-   r   |  s   
z5require_accelerate_version_greater.<locals>.decoratorr,   )r   r   r,   r   r-   "require_accelerate_version_greater{  s   r   c                 C   s   t t d| S )zJ
    Decorator marking a test that will be skipped after PEFT backend
    z%test skipped in favor of PEFT backendr   r   r,   r,   r-   deprecate_after_peft_backend  r   r   c                  C   s   t j} | j| j}}||fS N)sysversion_infomajorminor)sys_infor   r   r,   r,   r-   get_python_version  s   r   arry
local_pathc                 C   s   t | trS|d ur!t|| dd | dd | dd  S | ds+| dr>t| }|  t	
t|j} | S tj| rKt	
| } | S td|  dt | t	jr\	 | S td	)
N/r;   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.)
isinstancerG   r   rM   rd   
startswithrequestsrD   raise_for_statusr0   loadr   contentrB   r`   isfiler#   ndarray)r   r   responser,   r,   r-   
load_numpy  s(   
0


r   urlc                 C   s&   t | }|  tt|j}|S r   )r   rD   r   r$   r   r   r   )r   r   r   r,   r,   r-   load_pt  s   
r   imagec                 C   s   t | tr1| ds| drtjtj| ddj} n#t	j
| r)tj| } ntd|  dt | tjjr;| } ntdtj| } | d} | S )	z
    Loads `image` to a PIL Image.

    Args:
        image (`str` or `PIL.Image.Image`):
            The image to convert to the PIL Image format.
    Returns:
        `PIL.Image.Image`:
            A PIL Image.
    r   r   T)streamr   r   zdIncorrect format used for image. Should be an url linking to an image, a local path, or a PIL image.RGB)r   rG   r   PILImagerN   r   rD   rawrB   r`   r   r#   ImageOpsexif_transposeconvert)r   r,   r,   r-   
load_image  s    


r   
batch_sizec                 C   s   | j \}}dd ||fD \}}| j||ftjjd} t| tjd } t	| d  
ddddg| } t| } d	|  d
 S )Nc                 s   s    | ]	}||d   V  qdS )   Nr,   .0xr,   r,   r-   	<genexpr>  s    z#preprocess_image.<locals>.<genexpr>)resampleg     o@r      r      g       @r/   )sizeresizer   r   LANCZOSr0   arrayastyperK   vstack	transposer$   rF   )r   r   whr,   r,   r-   preprocess_image  s   
 
r  output_gif_pathc                 C   s<   |d u rt jddj}| d j|d| dd  dddd |S )	Nz.gifsuffixr   Tr   Fd   )save_allappend_imagesoptimizedurationloop)tempfileNamedTemporaryFiler   save)r   r  r,   r,   r-   export_to_gif  s   
r  c                 c   s    t | }|V  |  d S r   )ioBufferedWriterflush)raw_fr[   r,   r,   r-   buffered_writer  s   
r  output_ply_pathc                    s  |du rt jddj} j   } j  }tj	 fdddD dd}t
t|d	}|d
 |d |tdt| dd |d |d |d |durk|d |d |d |dur|tdt| dd |d |d |dur|d  t}dd t| | D }td}|D ]
}||j|  qntd}| D ]
}	||j|	  q|durtd}| D ]}
||jt|
g|
R   qW d   |S W d   |S 1 sw   Y  |S )z&
    Write a PLY file for a mesh.
    Nz.plyr  c                    "   g | ]} j |    qS r,   vertex_channelsrH   r    numpyr   meshr,   r-   
<listcomp>     " z!export_to_ply.<locals>.<listcomp>r   r   axiswbs   ply
s    format binary_little_endian 1.0
zelement vertex r<   asciis   property float x
s   property float y
s   property float z
s   property uchar red
s   property uchar green
s   property uchar blue
zelement face s%   property list uchar int vertex_index
s   end_header
go@c                 S   s   g | ]\}}g ||R qS r,   r,   )r   coordrgbr,   r,   r-   r     s    z<3f3Bz<3fz<B3I)r  r  r   vertsrH   r    r  facesr0   r_   r  rN   writebyteslenroundr  intziptoliststructStructpack)r  r  coordsr)  r'  r[   verticesformatitemvertextrir,   r  r-   export_to_ply  s^   













&
&&r:  output_obj_pathc                    s   |d u rt jddj} j   } j  }tj	 fdddD dd}dd t
| | D }d	d | D }d
d |D | }t|d}|d| W d    d S 1 sbw   Y  d S )Nz.objr  c                    r  r,   r  r   r  r,   r-   r   /  r!  z!export_to_obj.<locals>.<listcomp>r   r   r"  c                 S   s$   g | ]\}}d j g ||R  qS )z{} {} {} {} {} {})r6  )r   r&  colorr,   r,   r-   r   0  s    c              	   S   s>   g | ]}d  t|d d t|d d t|d d qS )z
f {} {} {}r   r   r   )r6  rG   )r   r9  r,   r,   r-   r   4  s   > c                 S   s   g | ]}d | qS )zv r,   )r   r8  r,   r,   r-   r   6  s    r  r<   )r  r  r   r(  rH   r    r  r)  r0   r_   r/  r0  rN   
writelinesrP   )r  r;  r(  r)  vertex_colorsr5  combined_datar[   r,   r  r-   export_to_obj(  s   "r@  video_framesoutput_video_pathc           
      C   s   t  rdd l}nttd d d|d u rtjddj}|jd }| d j	\}}}|j
||d||fd	}tt| D ]}|| | |j}	||	 q<|S )
Nr   opencvr   export_to_videoz.mp4r  mp4vr   )fps	frameSize)r   cv2ImportErrorr   r6  r  r  r   VideoWriter_fourccr   VideoWriterr   r,  cvtColorCOLOR_RGB2BGRr*  )
rA  rB  rH  fourccr  r  cvideo_writeriimgr,   r,   r-   rD  <  s   

rD  c                 C   s6   d}|  ds|  dstj|tj| } t| S )NzEhttps://huggingface.co/datasets/fusing/diffusers-testing/resolve/mainr   r   )r   rB   r`   rP   urllibr   quoter   )r`   base_urlr,   r,   r-   load_hf_numpyM  s   rV  c                 C   s.   d}|t vr| j|dddd dt |< dS dS )z
    This function is to be called from `conftest.py` via `pytest_addoption` wrapper that has to be defined there.

    It allows loading both `conftest.py` files at once without causing a failure due to adding the same `pytest`
    option.

    z--make-reportsstoreFzSgenerate report files. The value of this option is used as a prefix to report names)actionrz   helpr   N)pytest_opt_registered	addoption)parseroptionr,   r,   r-   pytest_addoption_shared\  s   r^  c              
      s  ddl m} tsd| j}| }|jj}| j}d t j	ddd  fddd	D }g }| j
 D ]}	|	D ]}
t|
d
rF||
 q:q6|r|jdd dd t|d dA}d}|d t|D ]-\}}
|
j|k r|t||  d| d  n||
jdd|
jdd|
j d qfW d   n1 sw   Y  dd }d|j_t|d d}|||| _|   W d   n1 sw   Y  t|d d}|||| _||  W d   n1 sw   Y  d|j_t|d  d}|||| _|   W d   n	1 sw   Y  t|d! d}|||| _|   W d   n	1 s0w   Y  t|d" d}|||| _|   |   W d   n	1 sVw   Y  d#| _t|d$ d}|||| _|   W d   n	1 s{w   Y  t|d% d}|||| _|   W d   n	1 sw   Y  t|d& d}|||| _|   W d   n	1 sw   Y  || _|| _||j_dS )'a  
    Generate multiple reports at the end of test suite run - each report goes into a dedicated file in the current
    directory. The report files are prefixed with the test suite name.

    This function emulates --duration and -rA pytest arguments.

    This function is to be called from `conftest.py` via `pytest_terminal_summary` wrapper that has to be defined
    there.

    Args:
    - tr: `terminalreporter` passed from `conftest.py`
    - id: unique id like `tests` or `examples` that will be incorporated into the final reports filenames - this is
      needed as some jobs have multiple runs of pytest, so we can't have them overwrite each other.

    NB: this functions taps into a private _pytest API and while unlikely, it could break should
    pytest do internal changes - also it calls default internal methods of terminalreporter which
    can be hijacked by various `pytest-` plugins and interfere.

    r   )create_terminal_writerr]   reportsT)parentsexist_okc              	      s$   i | ]}|  d  d| dqS )r   r   z.txtr,   )r   kdiridr,   r-   
<dictcomp>  s    z0pytest_terminal_summary_main.<locals>.<dictcomp>)		durationserrorsfailures_longfailures_shortfailures_linepassesstatssummary_shortwarningsr  c                 S   s   | j S r   )r  )r   r,   r,   r-   <lambda>  s    z.pytest_terminal_summary_main.<locals>.<lambda>)ry   reverserh  r  g?zslowest durations
z durations < z secs were omittedz02.2fzs z<8 r<   Nc              	   S   sp   |  d}|s	d S | dd |D ]$}| |}| jd|ddd tdd|jd	tjtjB }| j	| qd S )
Nfailed=zFAILURES SHORT STACKr   T)redboldz.*_ _ _ (_ ){10,}_ _ r=   r   )

getreports	write_sep_getfailureheadlineresublongreprtextMS_twline)trr`  repmsglongreprr,   r,   r-   summary_failures_short  s   

z<pytest_terminal_summary_main.<locals>.summary_failures_shortautorj  rk  r  rl  ri  rp  wPpsxXEfrm  ro  rn  )_pytest.configr_  r,  configget_terminal_writerr]  tbstylereportcharsr   mkdirrn  r   hasattrr   sortrN   r*  	enumerater  whennodeidr  summary_failuressummary_errorssummary_warningssummary_passesshort_test_summarysummary_stats)r  rf  r_  r  orig_writerorig_tbstyleorig_reportcharsreport_filesdlistreplistr  r[   durations_minrQ  r  r,   rd  r-   pytest_terminal_summary_maino  s   



(	







r     max_attemptswait_before_retrydescriptionc                    s    fdd}|S )a  
    To decorate flaky tests. They will be retried on failures.

    Args:
        max_attempts (`int`, *optional*, defaults to 5):
            The maximum number of attempts to retry the flaky test.
        wait_before_retry (`float`, *optional*):
            If provided, will wait that number of seconds before retrying the test.
        description (`str`, *optional*):
            A string to describe the situation (what / where / why is flaky, link to GH issue/PR comments, errors,
            etc.)
    c                    s   t   fdd}|S )Nc                     s   d}| k rCz| i |W S  t y> } z$td| d| d  dtjd d ur0t |d7 }W Y d }~nd }~ww | k s| i |S )Nr   zTest failed with z at try r   r"   r?   )	ExceptionrO   r   stderrtimesleep)r*   r+   retry_counterr)r  test_func_refr  r,   r-   wrapper   s   "

z,is_flaky.<locals>.decorator.<locals>.wrapper)	functoolswraps)r  r  r  r  )r  r-   r     s   zis_flaky.<locals>.decoratorr,   )r  r  r  r   r,   r  r-   is_flaky  s   r  c              
   C   s   |du rt tjdd}d}t|}|d}|d}|j||d |j	||||fd}|
  z|j|d}	|  W n tyZ }
 z|  | |
 W Y d}
~
nd}
~
ww |j|d |	d durq| |	d   dS dS )	a  
    To run a test in a subprocess. In particular, this can avoid (GPU) memory issue.

    Args:
        test_case (`unittest.TestCase`):
            The test that will run `target_func`.
        target_func (`Callable`):
            The function implementing the actual testing logic.
        inputs (`dict`, *optional*, defaults to `None`):
            The inputs that will be passed to `target_func` through an (input) queue.
        timeout (`int`, *optional*, defaults to `None`):
            The timeout (in seconds) that will be passed to the input and output queues. If not specified, the env.
            variable `PYTEST_TIMEOUT` will be checked. If still `None`, its value will be set to `600`.
    NPYTEST_TIMEOUTiX  spawnr   )timeout)targetr*   error)r.  rB   rC   rD   multiprocessingget_contextQueueJoinableQueueputProcessstart	task_doner  	terminatefailrP   )r   target_funcinputsr  start_methohdctxinput_queueoutput_queueprocessresultser,   r,   r-   run_test_in_subprocess  s*   


r  c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )CaptureLoggeraG  
    Args:
    Context manager to capture `logging` streams
        logger: 'logging` logger object
    Returns:
        The captured output is available via `self.out`
    Example:
    ```python
    >>> from diffusers import logging
    >>> from diffusers.testing_utils import CaptureLogger

    >>> msg = "Testing 1, 2, 3"
    >>> logging.set_verbosity_info()
    >>> logger = logging.get_logger("diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.py")
    >>> with CaptureLogger(logger) as cl:
    ...     logger.info(msg)
    >>> assert cl.out, msg + "
"
    ```
    c                 C   s&   || _ t | _t| j| _d| _d S )Nr=   )loggerr   r  loggingStreamHandlershout)selfr  r,   r,   r-   __init__V  s   
zCaptureLogger.__init__c                 C   s   | j | j | S r   )r  
addHandlerr  r  r,   r,   r-   	__enter__\  s   zCaptureLogger.__enter__c                 G   s   | j | j | j | _d S r   )r  removeHandlerr  r  getvaluer  )r  excr,   r,   r-   __exit__`  s   zCaptureLogger.__exit__c                 C   s   d| j  dS )Nz
captured: r<   )r  r  r,   r,   r-   __repr__d  s   zCaptureLogger.__repr__N)__name__
__module____qualname____doc__r  r  r  r  r,   r,   r,   r-   r  A  s    r  c                   C   sB   dt jd< dt jd< td dtjj_dtjj_dtjjj	_
dS )z
    Helper function for reproducible behavior during distributed training. See
    - https://pytorch.org/docs/stable/notes/randomness.html for pytorch
    ro   CUDA_LAUNCH_BLOCKINGz:16:8CUBLAS_WORKSPACE_CONFIGTFN)rB   rC   r$   use_deterministic_algorithmsbackendscudnndeterministic	benchmarkr   matmul
allow_tf32r,   r,   r,   r-   enable_full_determinismh  s   




r  c                   C   s"   dt jd< dt jd< td d S )Nrt   r  r=   r  F)rB   rC   r$   r  r,   r,   r,   r-   disable_full_determinismz  s   

r  c              
   C      t  sdS dd l}|| } z|jd|jd| }|||}W dS  tyA } z| jdkr6t	d| W Y d }~dS d }~ww )NFr   r   r   r   Tr   zYou have passed a device of type 'cuda' which should work with 'fp16', but 'cuda' does not seem to be correctly installed on your machine: )
r   r$   devicezerosfloat16rJ   mulr  typer#   r  r$   r   r   r  r,   r,   r-   r         

r   c              
   C   r  )NFr   r  r  Tr   zYou have passed a device of type 'cuda' which should work with 'fp64', but 'cuda' does not seem to be correctly installed on your machine: )
r   r$   r  r  float64rJ   r  r  r  r#   r  r,   r,   r-   r     r  r   T)r   r    r!   rz   c                   C   s   dS )Nr   r,   r,   r,   r,   r-   rq    s    rq  )r   r    rz   r  dispatch_tablec                 O   s<   | |vr|d |i |S ||  }|d u rd S ||i |S )Nrz   r,   )r  r  r*   r+   fnr,   r,   r-   _device_agnostic_dispatch  s   r  seedc                 C   s   t | t|S r   )r  BACKEND_MANUAL_SEED)r  r  r,   r,   r-   backend_manual_seed  s   r  c                 C   
   t | tS r   )r  BACKEND_EMPTY_CACHEr  r,   r,   r-   backend_empty_cache     
r  c                 C   r  r   )r  BACKEND_DEVICE_COUNTr  r,   r,   r-   backend_device_count  r  r  c                 C   s   t  sdS | tvrd} t|  S )NFrz   )r   BACKEND_SUPPORTS_TRAININGr  r,   r,   r-   r     s
   r   device_fn_dictattribute_namec              
   C   s`   zt t|}|| t< W d S  ty/ } zd| vr$td| dt d|W Y d }~d S d }~ww )Nrz   `z` not found in 'z)' and no default fallback function found.)getattrdevice_spec_moduler   AttributeErrordevice_spec_path)r  r  spec_fnr  r,   r,   r-   update_mapping_from_spec  s   
r  DIFFUSERS_TEST_DEVICE_SPECzCSpecified path to device specification file is not found. Received z.pyz9Provided device spec file is not a Python file! Received z.Device spec file did not contain `DEVICE_NAME`z?Mismatch between environment variable `DIFFUSERS_TEST_DEVICE` 'z' and device found in spec 'z'
zKEither unset `DIFFUSERS_TEST_DEVICE` or ensure it matches device spec name.MANUAL_SEED_FNEMPTY_CACHE_FNDEVICE_COUNT_FNSUPPORTS_TRAINING)NNr6   r7   r   )F)r/   NN)r  NN)NN)r  r   r^   r  r  r  rB   r   r{  r1  r   r  r  r   urllib.parserS  
contextlibr   r   r   pathlibr   typingr   r   r   r	   r
   r  r0   	PIL.Imager   PIL.ImageOpsr   numpy.linalgr   	packagingr   import_utilsr   r   r   r   r   r   r   r   r   r   r   r   r   Randomr   r  r  r   r   r   _required_peft_version_required_transformers_versionr   r$   rC   backendimport_moduler   ModuleNotFoundErrorr  r   r  RuntimeErrorinfor   is_available__version__is_torch_higher_equal_than_1_12r  r  mps_backend_registeredr!   r.   r5   r\   rh   r.  rw   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rG   r   r   r   r   r   r  r  r  r:  r@  rD  rV  rZ  r^  r  r   r  r  r  r  r  r   r   r  empty_cacher  r   r   manual_seedr  r  r  r  r  r   r  r	  is_filer#   indeximport_namer  DEVICE_NAMEdevice_namer  r  r,   r,   r,   r-   <module>   s`   8



	






	(" 

4  
%+'





