
    +h                        S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	r	S SK
r
S SKrS SKrS SKrS SKrS SKrS SKrS SKJr  S SKJr  S SKJrJr  S SKJr  S SKJrJrJrJrJrJ r J!r!J"r"J#r#  S SK$r%S SK&r'S SK(r'S SK)r)S SK*J+r+  S SK,J-r-  S	S
K.J/r/  S	SK0J1r1J2r2J3r3J4r4J5r5J6r6J7r7J8r8J9r9J:r:J;r;J<r<J=r=J>r>J?r?J@r@JArAJBrB  S	SKJCrC  \>" 5       (       aK  S SKDrD\DRZ                  R                  SLrF\DRZ                  R                  SLrH\I" \DRZ                  SS5      SLrJOSrFSrHSrJ\	R                  " 5       rL\C" \M5      rN\<" 5       =(       aX    \-R                  " \-R                  " \R                  R[                  S5      5      R                  5      \-R                  " S5      :  rR\B" 5       =(       aX    \-R                  " \-R                  " \R                  R[                  S5      5      R                  5      \-R                  " S5      :  rS\R=(       a    \SrT\U" \R                  " SS5      5      rW\>" 5       (       Ga`  S SKDrDS\R                  ;   a"  \R                  S   rY \R                  " \Y5      r[S\R                  ;   a7  \R                  S   r^ \DR                  " \^5      r[\NR                  S\^ 35        O\DR                  R                  5       (       a  Sr^O$\DR                  R                  5       (       a  Sr^OSr^\-R                  " \-R                  " \DR                  5      R                  5      \-R                  " S5      :  re\e(       aG  \f" \DR                  S5      rh\h(       a+  \DR                  R                  R                  5       (       a  SO\^r^S	SKjJkrk  S  rlS! rmS" rn    SS# jroSS$ jrpS%\U4S& jrqSS' jrr\r" S(SS)9rs\r" S*SS)9rt\r" S+SS)9ruSS, jrvS- rwS. rxS/ ryS0 rzS1 r{S2 r|S3 r}S4 r~S5 rS6 rS7 rS8 rS9 rS: rS; rS< rS= rS> rS? rS@ rSA rSB rSC rSD rSE rSF rSG rSH rSI rSJ rSK rSL rSM rSN rSO rSP rSQ rSR rSS rSST\#\\%GR>                  4   SU\ \   S%\%GR>                  4SV jjrSSX\SY\ \   SZ\ \   4S[ jjrS\\#\\'GRF                  GRF                  4   S%\'GRF                  GRF                  4S] jrS\\'GRF                  S^\U4S_ jrSS\\\'GRF                  GRF                     S`\S%\4Sa jjr\Sb 5       rSSc\4Sd jjrSSe\4Sf jjrSSg\\%GR>                     Sh\S%\4Si jjrS%\%GR>                  4Sj jr0 rSk rSl rSSm\USn\ \   So\ \   4Sp jjrSSq jr " Sr Ss5      rSt rSu rSv rSw r\>" 5       (       Ga  SWSWSWSSWSx.r\DR                  GRp                  \DR                  GRp                  S\DR                  GRp                  SSx.r\DR                  GRt                  \DR                  GRt                  Sy Sz S Sx.r\DR                  GRx                  \DR                  GRx                  \DGRx                  \DR                  GRx                  \DGRx                  Sx.r\DR                  GR|                  \I" \DR                  S{S5      SSSSx.r\DR                  GR                  \I" \DR                  S{S5      SSSSx.r\DR                  GR                  \I" \DR                  S|S5      S S S Sx.r\DR                  GR                  \I" \DR                  S}S5      SSSSx.rS~\S\\\4   4S jrS~\S\U4S jrS~\4S jrS~\4S jrS~\4S jrS~\4S jrS~\4S jrS~\4S jrS~\4S jr\>" 5       (       a  S\\\4   S\4S jrS\R                  ;   a  \R                  S   r\" \5      GR                  5       (       d  \" S\ 35      e \S\GR                  S5       r\R                  " \5      r \GR                  rS\R                  ;   a  \^\:w  a  S\^ S\ S3r\S-  r\" \5      e\r^\" \S5        \" \S5        \" \S5        \" \S5        \" \S5        \" \S5        \" \S5        \"\#\S4   \#\US4   4   r\ GR                  S%\4S j5       r\(       a  \\\4   rO\r\>" 5       (       am  S SKJr  S SKJrJrJrJrJr   SS\DGR                  GR                  S\S\S\ \U   S%\!\   4
S jjr SS\DGR                  GR                  S\S\S\ \U   S%\4
S jjr " S S\5      rg! \\ a  r]\\" S\Y S\] 35      \]eSr]C]ff = f! \` a  r]\`" S\^ 35      \]eSr]C]ff = f! \ a  r]\" S\ 35      \]eSr]C]ff = f! \ a	    \" S5      ef = f)    N)UserDict)contextmanager)BytesIOStringIO)Path)	TYPE_CHECKINGAnyCallableDictListOptionalSetTupleUnion)norm)version   )DIFFUSERS_REQUEST_TIMEOUT)BACKENDS_MAPPINGis_accelerate_availableis_bitsandbytes_availableis_compel_availableis_flax_availableis_gguf_availableis_kernels_availableis_note_seq_availableis_onnx_availableis_opencv_availableis_optimum_quanto_availableis_peft_availableis_timm_availableis_torch_availableis_torch_versionis_torchao_availableis_torchsde_availableis_transformers_available)
get_loggerxpuFpeftz0.5transformersz4.33BIG_GPU_MEMORY(   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mps) get_torch_cuda_device_capabilityc                     [        5       (       d  [        S5      e[        R                  " X/UQ70 UD6(       d<   SX-
  R	                  5       R                  5        SX-
  R	                  5        S35       eg)Nz3PyTorch needs to be installed to use this function.zMax diff is absolute z. Diff tensor is .T)r"   
ValueErrortorchallcloseabsmax)abargskwargss       W/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/utils/testing_utils.pytorch_all_closer?   {   sl    NOO>>!000d-qukkm.?.?.A-BBSUVUZT_T_TaSbbcddu    c                     [         R                  " X5      [        U 5      [        U5      -  -  nSUR                  5       -
  nU$ )N      ?)npdotr   mean)r:   r;   
similaritydistances       r>    numpy_cosine_similarity_distancerH      s7    a47!23JZ__&&HOr@   c                    U R                  5       UR                  5       pU R                  5        H)  u  p#[        U[        5      (       d  M  [	        U5      X'   M+     UR                  5        H)  u  p#[        U[        5      (       d  M  [	        U5      X'   M+     U  H  nX!;  a    gX   X   :w  d  M    g   U H
  nX ;  d  M
    g   g)NFT)copyitems
isinstancesetsorted)dict1dict2keyvalues       r>   check_if_dicts_are_equalrS      s    ::<5kkm
eS!!EJ $ kkm
eS!!EJ $ :#	    r@   c           	         U(       a  [         R                  " SS9  [        R                  R	                  S5      n[         R
                  " U 5      (       d  [         R                  " U 5      n U(       a  U SSS 2SS 2S4   n [        U R                  5       R                  5       R                  5       R                  [         R                  5      5      R                  SS5      nUR                  S	U S
35      nUR                  S5      u  pn
U
R                  5       S   n
[        US5       n[!        SR#                  XX/5      US9  S S S 5        g ! , (       d  f       g = f)Ni'  )	thresholdPYTEST_CURRENT_TESTr   
 tensorz = np.arrayz::r:   file)r6   set_printoptionsosenvironget	is_tensor
from_numpystrdetachr0   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fs               r>   print_tensor_testry      s    0

45I??6""!!&)23R(V]]_((*22477FGOOPTVXYJ ##H1E0Fk.RSJ%.__T%:"I7mmoa G	h	diiDEAN 
		s   *E
Ec                    [         R                  " 5       S   S   n[        R                  R	                  [        R                  R                  U5      5      nUR                  S5      (       d7  [        R                  R                  U5      nUR                  S5      (       d  M7  U (       a  [        X 5      R                  5       $ U$ )z
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stackr_   pathabspathdirnameendswithr   as_posix)append_pathcaller__file__	tests_dirs      r>   get_tests_dirr      s     ]]_Q'*N ?@I  ))GGOOI.	   )) I+4466r@   returnc                 Z    U R                  5       n U S;   a  gU S;   a  g[        SU  35      e)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norx   falseoff0r   zinvalid truth value )lowerr5   )rR   s    r>   str_to_boolr      s:    
 KKME44	7	7/w788r@   c                      [         R                  U    n [        U5      nU$ ! [         a    [        SU  S35      ef = f! [         a    Un U$ f = f)NzIf set, z must be yes or no.)r_   r`   r   r5   KeyError)rQ   defaultrR   _values       r>   parse_flag_from_envr      sp    B

3	B 'F M  	Bxu,?@AA	B   Ms   A  # = AARUN_SLOW)r   RUN_NIGHTLYRUN_COMPILEc                    Uc  [         nSnU  H  nXE-  nM	     / n[        U5       H%  nUR                  UR                  5       U-  5        M'     [        R
                  " U[        R                  S9R                  U 5      R                  5       $ )zCreates a random float32 tensorr   )datadtype)	
global_rngrangeappendrandomr6   r[   floatview
contiguous)shapescalerngname
total_dimsdimvalues_s           r>   floats_tensorr      s{    
{J
  F:cjjlU*+  <<V5;;7<<UCNNPPr@   c                 D    [         R                  " [        S5      " U 5      $ )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_cases    r>   slowr     s     ?	JJr@   c                 D    [         R                  " [        S5      " U 5      $ )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   s    r>   nightlyr     s     13DEiPPr@   c                 D    [         R                  " [        S5      " U 5      $ )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   s    r>   is_torch_compiler     s     13JKIVVr@   c                 L    [         R                  " [        5       S5      " U 5      $ )zg
Decorator marking a test that requires PyTorch. These tests are skipped when PyTorch isn't installed.
test requires PyTorch)r   r   r"   r   s    r>   require_torchr   #  s!     135LMiXXr@   c                 r    [         R                  " [        5       =(       a    [        SS5      S5      " U 5      $ )zd
Decorator marking a test that requires PyTorch 2. These tests are skipped when it isn't installed.
>=z2.0.0ztest requires PyTorch 2)r   r   r"   r#   r   s    r>   require_torch_2r   *  s3     13W8Hw8WYrs r@   c                    ^  U 4S jnU$ )zPDecorator marking a test that requires torch with a specific version or greater.c                 ~   > [        5       =(       a    [        ST5      n[        R                  " UST 35      " U 5      $ )Nr   z>test requires torch with the version greater than or equal to r"   r#   r   r   r   correct_torch_versiontorch_versions     r>   	decorator6require_torch_version_greater_equal.<locals>.decorator6  sD     2 4 ^9I$P]9^""!%cdqcr#s

 	r@    r   r   s   ` r>   #require_torch_version_greater_equalr   3       r@   c                    ^  U 4S jnU$ )zMDecorator marking a test that requires torch with a specific version greater.c                 ~   > [        5       =(       a    [        ST5      n[        R                  " UST 35      " U 5      $ )N>z2test requires torch with the version greater than r   r   s     r>   r   0require_torch_version_greater.<locals>.decoratorB  sC     2 4 ]9I#}9]""!%WXeWf#g

 	r@   r   r   s   ` r>   require_torch_version_greaterr   ?  r   r@   c                 l    [         R                  " [        5       =(       a	    [        S:H  S5      " U 5      $ )z8Decorator marking a test that requires CUDA and PyTorch.r/   ztest requires PyTorch+CUDAr   r   r"   torch_devicer   s    r>   require_torch_gpur   K  s.    13N8NPlm r@   c                    ^  U 4S jnU$ )Nc                    > [         R                  R                  5       (       a6  [        5       n[        R
                  " [        U5      [        T5      :H  S5      $ g )Nz/Test not supported for this compute capability.)r6   r/   is_availabler2   r   r   r   )r   current_compute_capabilityexpected_compute_capabilitys     r>   r   3require_torch_cuda_compatibility.<locals>.decoratorS  sN    ::""$$)I)K&&&01U;V5WWA  %r@   r   )r   r   s   ` r>    require_torch_cuda_compatibilityr   R  s     r@   c                 l    [         R                  " [        5       =(       a	    [        S:g  S5      " U 5      $ )zJDecorator marking a test that requires an accelerator backend and PyTorch.r0   z!test requires accelerator+PyTorchr   r   s    r>   require_torch_acceleratorr   _  s.    13M8MOrs r@   c                     [        5       (       d  [        R                  " S5      " U 5      $ SSKn[        R                  " UR
                  R                  5       S:  S5      " U 5      $ )z
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   skipr6   r   r/   device_countr   r6   s     r>   require_torch_multi_gpur   f  sQ     }}45i@@uzz6681<>[\]fggr@   c                    [        5       (       d  [        R                  " S5      " U 5      $ SSKn[        R                  " UR
                  R                  5       S:  =(       d    UR                  R                  5       S:  S5      " U 5      $ )z
Decorator marking a test that requires a multi-accelerator setup (in PyTorch). These tests are skipped on a machine
without multiple hardware accelerators.
r   r   Nr   z,test requires multiple hardware accelerators)r"   r   r   r6   r   r/   r   r(   r   s     r>   require_torch_multi_acceleratorr   t  so    
 }}45i@@

!A%E)?)?)AA)EGu r@   c                 V    [         R                  " [        [        5      S5      " U 5      $ )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   s    r>   #require_torch_accelerator_with_fp16r     &    7EGtu r@   c                 V    [         R                  " [        [        5      S5      " U 5      $ )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   s    r>   #require_torch_accelerator_with_fp64r     r   r@   c                    [        5       (       d  [        R                  " S5      " U 5      $ SSKnUR                  R                  5       (       d  [        R                  " S5      " U 5      $ UR                  R                  S5      nUR                  S-  n[        R                  " U[        :  S[         S35      " U 5      $ )zx
Decorator marking a test that requires a bigger GPU (24GB) for execution. Some example pipelines: Flux, SD3, Cog,
etc.
r   r   Ntest requires PyTorch CUDA   @z"test requires a GPU with at least 
 GB memory)
r"   r   r   r6   r/   r   get_device_propertiestotal_memoryr   r+   )r   r6   device_propertiesr   s       r>   require_big_gpu_with_torch_cudar     s    
 }}45i@@::""$$}}9:9EE

88;$11W=L&*L^L\\f(g r@   c                 t   SSK nUR                  R                  U 5      n [        5       (       d  [        R
                  " S5      " U 5      $ SSKnUR                  R                  5       (       d;  UR                  R                  5       (       d  [        R
                  " S5      " U 5      $ UR                  R                  5       (       a  UR                  R                  S5      nOUR                  R                  S5      nUR                  S-  n[        R                  " U[        :  S[         S35      " U 5      $ )z
Decorator marking a test that requires a bigger hardware accelerator (24GB) for execution. Some example pipelines:
Flux, SD3, Cog, etc.
r   Nr   r   r   z3test requires a hardware accelerator with at least r   )pytestmarkbig_acceleratorr"   r   r   r6   r/   r   r(   r   r   r   r+   )r   r   r6   r   r   s        r>   require_big_acceleratorr     s    
 ++I6I}}45i@@JJ##%%)?)?)A)A}}9:9EEyy!II;;A>!JJ<<Q?$11W=L&
=n=MZX  r@   c                 x    [         R                  " [        5       =(       a    [        [        5      S5      " U 5      $ )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   s    r>   'require_torch_accelerator_with_trainingr     s3    H!:<!H9  r@   c                 J    [         R                  " [        S:g  S5      " U 5      $ )z9Decorator marking a test to skip if torch_device is 'mps'r1   ztest requires non 'mps' devicer   r   r   r   s    r>   skip_mpsr    s!    |u46VWXabbr@   c                 L    [         R                  " [        5       S5      " U 5      $ )zn
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   s    r>   require_flaxr    s"     024NOPYZZr@   c                 L    [         R                  " [        5       S5      " U 5      $ )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   s    r>   require_compelr    s!    
 246LMiXXr@   c                 L    [         R                  " [        5       S5      " U 5      $ )zo
Decorator marking a test that requires onnxruntime. These tests are skipped when onnxruntime isn't installed.
ztest requires onnxruntime)r   r   r   r   s    r>   require_onnxruntimer    s"     024OPQZ[[r@   c                 L    [         R                  " [        5       S5      " U 5      $ )zi
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   s    r>   require_note_seqr
    "     468PQR[\\r@   c                 J    [         R                  " [        S:g  S5      " U 5      $ )z
Decorator marking a test that requires a hardware accelerator backend. These tests are skipped when there are no
hardware accelerator available.
r0   z$test requires a hardware acceleratorr  r   s    r>   require_acceleratorr    s#    
 |u46\]^ghhr@   c                 L    [         R                  " [        5       S5      " U 5      $ )zi
Decorator marking a test that requires torchsde. These tests are skipped when torchsde isn't installed.
ztest requires torchsde)r   r   r%   r   s    r>   require_torchsder    r  r@   c                 D    [         R                  " [        S5      " U 5      $ )zz
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   s    r>   require_peft_backendr    s    
 /1MNyYYr@   c                 L    [         R                  " [        5       S5      " U 5      $ )za
Decorator marking a test that requires timm. These tests are skipped when timm isn't installed.
ztest requires timm)r   r   r!   r   s    r>   require_timmr    s!     024HI)TTr@   c                 L    [         R                  " [        5       S5      " U 5      $ )zq
Decorator marking a test that requires bitsandbytes. These tests are skipped when bitsandbytes isn't installed.
ztest requires bitsandbytes)r   r   r   r   s    r>   require_bitsandbytesr  
  s"     8:<XYZcddr@   c                 L    [         R                  " [        5       S5      " U 5      $ )ze
Decorator marking a test that requires quanto. These tests are skipped when quanto isn't installed.
ztest requires quanto)r   r   r   r   s    r>   require_quantor    s"     :<>TUV_``r@   c                 L    [         R                  " [        5       S5      " U 5      $ )zm
Decorator marking a test that requires accelerate. These tests are skipped when accelerate isn't installed.
ztest requires accelerate)r   r   r   r   s    r>   require_accelerater    s"     68:TUV_``r@   c                    ^  U 4S jnU$ )z
Decorator marking a test that requires PEFT backend with a specific version, this would require some specific
versions of PEFT and transformers.
c                 6  > [        5       =(       ah    [        R                  " [        R                  " [        R                  R                  S5      5      R
                  5      [        R                  " T5      :  n[        R                  " UST 35      " U 5      $ )Nr)   z9test requires PEFT backend with the version greater than )r    r   parse	importlibmetadatabase_versionr   r   )r   correct_peft_versionpeft_versions     r>   r   /require_peft_version_greater.<locals>.decorator%  s|    02  (w}}MM),,44V<=JJ8
MM,'8( "" $]^j]k"l

 	r@   r   )r#  r   s   ` r>   require_peft_version_greaterr%    s     r@   c                    ^  U 4S jnU$ )z
Decorator marking a test that requires transformers with a specific version, this would require some specific
versions of PEFT and transformers.
c                 6  > [        5       =(       ah    [        R                  " [        R                  " [        R                  R                  S5      5      R
                  5      [        R                  " T5      :  n[        R                  " UST 35      " U 5      $ )Nr*   z9test requires transformers with the version greater than )r&   r   r  r  r   r!  r   r   )r   correct_transformers_versiontransformers_versions     r>   r   7require_transformers_version_greater.<locals>.decorator6  s    '@'B (0w}}MM),,44^DERRH
MM./H0$ ""(GH\G]^
  	r@   r   )r)  r   s   ` r>   $require_transformers_version_greaterr+  0  s     r@   c                    ^  U 4S jnU$ )Nc                 8  > [        5       =(       ah    [        R                  " [        R                  " [        R                  R                  S5      5      R
                  5      [        R                  " T5      :  n[        R                  " UST S35      " U 5      $ )N
acceleratez7Test requires accelerate with the version greater than r4   )r   r   r  r  r   r!  r   r   )r   correct_accelerate_versionaccelerate_versions     r>   r   5require_accelerate_version_greater.<locals>.decoratorC  s    %<%> &.7==MM),,44\BCPPD
MM,-D." ""&*abtauuv(w

 	r@   r   )r0  r   s   ` r>   "require_accelerate_version_greaterr2  B       r@   c                    ^  U 4S jnU$ )Nc                 8  > [        5       =(       ah    [        R                  " [        R                  " [        R                  R                  S5      5      R
                  5      [        R                  " T5      :  n[        R                  " UST S35      " U 5      $ )Nbitsandbytesz9Test requires bitsandbytes with the version greater than r4   )r   r   r  r  r   r!  r   r   )r   correct_bnb_versionbnb_versions     r>   r   7require_bitsandbytes_version_greater.<locals>.decoratorO  s    79 'gmmMM),,44^DERR?
MM+&?' ""#\]h\iij!k

 	r@   r   )r8  r   s   ` r>   $require_bitsandbytes_version_greaterr:  N  r3  r@   c                    ^  U 4S jnU$ )Nc                   > [         R                  " [         R                  " [        R                  R                  S5      5      R                  5      [         R                  " T5      :  n[
        R                  " UST S35      " U 5      $ )Nhuggingface_hubz<Test requires huggingface_hub with the version greater than r4   )r   r  r  r   r!  r   r   )r   correct_hf_hub_versionhf_hub_versions     r>   r   1require_hf_hub_version_greater.<locals>.decorator[  sv    !(MM),,445FGHUU"
MM.)"* """&bcqbrrs$t

 	r@   r   )r?  r   s   ` r>   require_hf_hub_version_greaterrA  Z  r3  r@   c                    ^  U 4S jnU$ )Nc                 8  > [        5       =(       ah    [        R                  " [        R                  " [        R                  R                  S5      5      R
                  5      [        R                  " T5      :  n[        R                  " UST S35      " U 5      $ )Nggufz1Test requires gguf with the version greater than r4   )r   r   r  r  r   r!  r   r   )r   correct_gguf_versiongguf_versions     r>   r   8require_gguf_version_greater_or_equal.<locals>.decoratorg  s    02  )w}}MM),,44V<=JJ8
]]<(8) "" $UVbUccd"e

 	r@   r   )rF  r   s   ` r>   %require_gguf_version_greater_or_equalrH  f  r3  r@   c                    ^  U 4S jnU$ )Nc                 8  > [        5       =(       ah    [        R                  " [        R                  " [        R                  R                  S5      5      R
                  5      [        R                  " T5      :  n[        R                  " UST S35      " U 5      $ )Ntorchaoz0Test requires torchao with version greater than r4   )r$   r   r  r  r   r!  r   r   )r   correct_torchao_versiontorchao_versions     r>   r   ;require_torchao_version_greater_or_equal.<locals>.decorators      "6"8 #,W]]MM),,44Y?@MM>
]]?+>, ""#'WXgWhhi%j

 	r@   r   )rM  r   s   ` r>   (require_torchao_version_greater_or_equalrP  r  r3  r@   c                    ^  U 4S jnU$ )Nc                 8  > [        5       =(       ah    [        R                  " [        R                  " [        R                  R                  S5      5      R
                  5      [        R                  " T5      :  n[        R                  " UST S35      " U 5      $ )Nkernelsz0Test requires kernels with version greater than r4   )r   r   r  r  r   r!  r   r   )r   correct_kernels_versionkernels_versions     r>   r   ;require_kernels_version_greater_or_equal.<locals>.decorator  rO  r@   r   )rU  r   s   ` r>   (require_kernels_version_greater_or_equalrW  ~  r3  r@   c                 N    [         R                  " [        (       + S5      " U 5      $ )zB
Decorator marking a test that will be skipped after PEFT backend
z%test skipped in favor of PEFT backendr  r   s    r>   deprecate_after_peft_backendrY    s"     #335\]^ghhr@   c                  V    [         R                  n U R                  U R                  p!X4$ N)sysversion_infomajorminor)sys_infor^  r_  s      r>   get_python_versionra    s$    H>>8>>5<r@   arry
local_pathc                    [        U [        5      (       Ga  UbQ  [        XR                  S5      S   U R                  S5      S   U R                  S5      S   5      R	                  5       $ U R                  S5      (       d  U R                  S5      (       aT  [        R                  " U [        S9nUR                  5         [        R                  " [        UR                  5      5      n U $ [        R                  R!                  U 5      (       a  [        R                  " U 5      n U $ [#        SU  S	35      e[        U [        R$                  5      (       a   U $ [#        S
5      e)N/rX   http://https://timeoutIIncorrect 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.)rL   rd   r   rj   r   
startswithrequestsra   r   raise_for_statusrC   loadr   contentr_   r~   isfiler5   ndarray)rb  rc  responses      r>   
load_numpyrv    s(   $!
JJsOB$7C9LdjjY\o^`Nabkkmm__Y''4??:+F+F||D2KLH%%'7778#3#345D K WW^^D!!774=D K [\`[aauv  
D"**	%	% K 
 	
r@   Turlmap_locationweights_onlyc                     [         R                  " U [        S9nUR                  5         [        R
                  " [        UR                  5      XS9nU$ )Nrj  )rx  ry  )ro  ra   r   rp  r6   rq  r   rr  )rw  rx  ry  ru  rb  s        r>   load_ptr{    s?    ||C)BCH::gh../lfDKr@   imagec                    [        U [        5      (       a  U R                  S5      (       d  U R                  S5      (       aB  [        R                  R                  [        R                  " U S[        S9R                  5      n O[        R                  R                  U 5      (       a   [        R                  R                  U 5      n OF[        SU  S35      e[        U [        R                  R                  5      (       a  U n O[        S5      e[        R                  R                  U 5      n U R!                  S5      n U $ )	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.
rh  ri  T)streamrk  rl  rm  zdIncorrect format used for image. Should be an url linking to an image, a local path, or a PIL image.RGB)rL   rd   rn  PILImagerk   ro  ra   r   rawr_   r~   rs  r5   ImageOpsexif_transposeconvert)r|  s    r>   
load_imager    s     %I&&%*:*::*F*FIINN8<<dLe#f#j#jkEWW^^E""IINN5)E[\a[bbvw  
E399??	+	+r
 	
 LL''.EMM% ELr@   
batch_sizec                    U R                   u  p#S X#4 5       u  p#U R                  X#4[        R                  R                  S9n [
        R                  " U 5      R                  [
        R                  5      S-  n [
        R                  " U S    R                  SSSS5      /U-  5      n [        R                  " U 5      n SU -  S	-
  $ )
Nc              3   0   #    U  H  oUS -  -
  v   M     g7f)   Nr   ).0xs     r>   	<genexpr>#preprocess_image.<locals>.<genexpr>  s     &v!AIvs   )resampleg     o@r      r      g       @rB   )sizeresizer  r  LANCZOSrC   arrayastyperh   vstack	transposer6   rc   )r|  r  whs       r>   preprocess_imager    s    ::DA&v&DALL!#))*;*;L<EHHUO""2::.6EIIuT{,,Q1a89JFGEU#E;r@   output_gif_pathc           	      |    Uc  [         R                  " SS9R                  nU S   R                  USU SS  SSSS9  U$ )	Nz.gifsuffixr   Tr   Fd   )save_allappend_imagesoptimizedurationloop)tempfileNamedTemporaryFiler   save)r|  r  s     r>   export_to_gifr    sS    "55VDII	!HMMABi   r@   c              #   `   #    [         R                  " U 5      nUv   UR                  5         g 7fr[  )ioBufferedWriterflush)raw_frx   s     r>   buffered_writerr    s#     
% A
GGGIs   ,.output_ply_pathc           	         Uc  [         R                  " SS9R                  nU R                  R	                  5       R                  5       R                  5       nU R                  R                  5       R                  5       n[        R                  " S Vs/ s H;  o@R                  U   R	                  5       R                  5       R                  5       PM=     snSS9n[        [        US5      5       nUR                  S5        UR                  S	5        UR                  [        S
[        U5       S3S5      5        UR                  S5        UR                  S5        UR                  S5        Ub3  UR                  S5        UR                  S5        UR                  S5        Ub9  UR                  [        S[        U5       S3S5      5        UR                  S5        UR                  S5        Ub  US-  R!                  5       R#                  [$        5      n['        UR)                  5       UR)                  5       5       VVs/ s H  u  pu/ UQUQ7PM     nnn[*        R,                  " S5      n	U H!  n
UR                  U	R.                  " U
6 5        M#     OK[*        R,                  " S5      n	UR)                  5        H!  nUR                  U	R.                  " U6 5        M#     UbX  [*        R,                  " S5      n	UR)                  5        H.  nUR                  U	R.                  " [        U5      /UQ76 5        M0     SSS5        U$ s  snf s  snnf ! , (       d  f       U$ = f)z
Write a PLY file for a mesh.
Nz.plyr  r  r   axiswbs   ply
s    format binary_little_endian 1.0
zelement vertex rY   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@z<3f3Bz<3fz<B3I)r  r  r   vertsre   r0   numpyfacesrC   r}   vertex_channelsr  rk   writebyteslenroundr  intziptoliststructStructpack)meshr  coordsr  r  rgbrx   coordverticesformatitemvertextris                r>   export_to_plyr    s    "55VDIIZZ $$&,,.FJJNN""$E
((EREq((+22488:@@BERYZ
[C	ot4	5		45	F}B7AB	%&	%&	%&?GG+,GG-.GG,-GGEM#e*R8'BCGG=>	 ?='')005C #&MMOJJL##JE %##   ]]7+F T*+ ! ]]5)F --/V,- * ]]6*F||~CH3s34 &G 
6L Q S(% 
6	5L s'   AM
1EMMC'MM
M$output_obj_pathc                    Uc  [         R                  " SS9R                  nU R                  R	                  5       R                  5       R                  5       nU R                  R                  5       R                  5       n[        R                  " S Vs/ s H;  o@R                  U   R	                  5       R                  5       R                  5       PM=     snSS9n[        UR                  5       UR                  5       5       VVs/ s H  u  pgSR                  " / UQUQ76 PM     nnnUR                  5        V	s/ s HC  n	SR                  [        U	S   S-   5      [        U	S   S-   5      [        U	S	   S-   5      5      PME     nn	U V
s/ s H  n
S
U
-   PM
     sn
U-   n[        US5       nUR!                  SR#                  U5      5        S S S 5        g s  snf s  snnf s  sn	f s  sn
f ! , (       d  f       g = f)Nz.objr  r  r   r  z{} {} {} {} {} {}z
f {} {} {}r   r  zv r  rY   )r  r  r   r  re   r0   r  r  rC   r}   r  r  r  r  rd   rk   
writelinesrm   )r  r  r  r  r  vertex_colorsr  colorr  r  r  combined_datarx   s                r>   export_to_objr  +  s   "55VDIIJJ##%++-EJJNN""$EHHV[\V[QR2215<<>BBDJJLV[\cdeMGJ5<<>[h[o[o[qGrGr|u""2E2E2Gr   `e_k_k_mn_mX[\  SVaZ#c!fqj/3s1vPQz?S_mEn19:vTF]:UBM	os	#q	TYY}-. 
$	# ] o:	#	#s&   AG G<A
G"G'-!G,,
G:video_framesoutput_video_pathc                    [        5       (       a  SS KnO$[        [        S   S   R	                  S5      5      eUc  [
        R                  " SS9R                  nUR                  " S6 nU S   R                  u  pEnUR                  XSXT4S	9n[        [        U 5      5       H2  nUR                  X   UR                  5      n	UR                  U	5        M4     U$ )
Nr   opencvr   export_to_videoz.mp4r  mp4vr  )fps	frameSize)r   cv2ImportErrorr   r  r  r  r   VideoWriter_fourccr   VideoWriterr   r  cvtColorCOLOR_RGB2BGRr  )
r  r  r  fourccr  r  cvideo_writeriimgs
             r>   r  r  ?  s    *84Q7>>?PQRR $77vFKK##V,F1o##GA!??#4!PQv?VL3|$%ll<?C,=,=>3 & r@   c                     SnU R                  S5      (       dS  U R                  S5      (       d=  [        R                  R                  U[        R
                  R                  U 5      5      n [        U 5      $ )NzEhttps://huggingface.co/datasets/fusing/diffusers-testing/resolve/mainrh  ri  )rn  r_   r~   rm   urllibr  quoterv  )r~   base_urls     r>   load_hf_numpyr  P  sR    VH??9%%dooj.I.Iww||Hfll&8&8&>?dr@   c                 T    SnU[         ;  a  U R                  USSSS9  S[         U'   gg)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)actionr   helpr   N)pytest_opt_registered	addoption)parseroptions     r>   pytest_addoption_sharedr  _  sC     F**f	 	 	
 )*f% +r@   c           
         SSK Jn  [        U5      (       d  SnU R                  nUR	                  5       nUR
                  R                  nU R                  nSn[        U5      R                  SSS9  S Vs0 s H  nX SU S	U S
3_M     n	n/ n
U R                  R                  5        H0  nU H'  n[        US5      (       d  M  U
R                  U5        M)     M2     U
(       a  U
R                  S SS9  [        U	S   S5       nSnUR!                  S5        [#        U
5       Ht  u  pUR$                  U:  a%  UR!                  [        U
5      U-
   SU S35          O>UR!                  UR$                  S SUR&                  S SUR(                   S35        Mv     SSS5        S nSUR
                  l        [        U	S   S5       nU" X=5      U l        U R-                  5         SSS5        [        U	S   S5       nU" X=5      U l        U" U 5        SSS5        SUR
                  l        [        U	S   S5       nU" X=5      U l        U R-                  5         SSS5        [        U	S    S5       nU" X=5      U l        U R/                  5         SSS5        [        U	S!   S5       nU" X=5      U l        U R1                  5         U R1                  5         SSS5        S"U l        [        U	S#   S5       nU" X=5      U l        U R3                  5         SSS5        [        U	S$   S5       nU" X=5      U l        U R5                  5         SSS5        [        U	S%   S5       nU" X=5      U l        U R7                  5         SSS5        X@l        X`l        XSR
                  l        gs  snf ! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GNd= f! , (       d  f       GN1= f! , (       d  f       GN= f! , (       d  f       N= f! , (       d  f       N= f)&ai  
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_ok)		durationserrorsfailures_longfailures_shortfailures_linepassesstatssummary_shortwarningsre  r   z.txtr  c                     U R                   $ r[  )r  )r  s    r>   <lambda>.pytest_terminal_summary_main.<locals>.<lambda>  s    r@   )rQ   reverser  r  g?zslowest durations
z durations < z secs were omittedz02.2fzs z<8 rY   Nc           	      n   U R                  S5      nU(       d  g U R                  SS5        U H  nU R                  U5      nU R                  SUSSS9  [        R                  " SSUR
                  S	[        R                  [        R                  -  5      nU R                  R                  U5        M     g )
Nfailed=zFAILURES SHORT STACKr   T)redboldz.*_ _ _ (_ ){10,}_ _ rZ   r   )

getreports	write_sep_getfailureheadlineresublongreprtextMS_twline)trr  repmsglongreprs        r>   summary_failures_short<pytest_terminal_summary_main.<locals>.summary_failures_short  s    --)
S01C((-CLLct$L7vv6C<L<LaQSQUQUXZX\X\Q\]HFFKK! r@   autor  r  r  r  r  r  wPpsxXEfr   r  r  )_pytest.configr  r  configget_terminal_writerr  tbstylereportcharsr   mkdirr  r   hasattrr   sortrk   r  	enumerater  whennodeidr  summary_failuressummary_errorssummary_warningssummary_passesshort_test_summarysummary_stats)r  idr  r!  orig_writerorig_tbstyleorig_reportcharsdirkreport_filesdlistreplistr  rx   durations_minr  r  s                    r>   pytest_terminal_summary_mainr;  r  s   ( 6r77YYF,,.K==((L~~
CIOOD4O0



A 	
U!B4q4  

  $ E88??$CsJ''S!  % 

+T
:,{+S1Q MGG)*#E*<<-/GGs5zA~.mM?J\]^3<<.b"Qszzl"MN	 + 2"( #FMM	l?+S	1Q'2
 
2
 
l+,c	2a'2r" 
3 #FMM	l?+S	1Q'2
 
2 
l8$c	*a'2
 
+ 
l:&	,'2

 
-
  BN	l8$c	*a'2
 
+ 
l?+S	1Q'2
 
2 
l7#S	)Q'2
 
*
 F%N(MM}2 21< 
2	1
 
3	2
 
2	1 
+	* 
-	, 
+	* 
2	1 
*	)ss   6NBN!N3;O9O.O)#.O;/P$PP0!
N03
O
O
O&)
O8;
P

P
P-0
P>max_attemptswait_before_retrydescriptionc                     ^ ^^^ UUU U4S jmT$ )a  
To decorate flaky tests (methods or entire classes). 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                 Z  >^  [         R                  " T 5      (       ak  [        T R                  R	                  5       5       HB  u  p[        U5      (       d  M  UR                  S5      (       d  M/  [        T UT" U5      5        MD     T $ [        R                  " T 5      UUU U4S j5       nU$ )Ntestc            	      &  > SnUT:  a	   T" U 0 UD6$ T" U 0 UD6$ ! [          ab  nST=(       d    TR                  < SU ST SU 3n[        U[        R                  S9  Tb  [
        R                  " T5        US-  n S nAOS nAff = fUT:  a  M  N)Nr   z[FLAKY] z failed on attempt re  z: r\   )	Exception__name__rl   r\  stderrtimesleep)	r<   r=   retry_counterrr  r>  r<  objr=  s	        r>   wrapper,is_flaky.<locals>.decorator.<locals>.wrapper  s    K,
%/// ''' ! %";#>#,,"A B--8M<.3%Q  #CJJ/(4

#451$K% ,s    
BABB)
r|   isclasslist__dict__rK   callablern  setattr	functoolswraps)rJ  	attr_name
attr_valuerK  r   r>  r<  r=  s   `   r>   r   is_flaky.<locals>.decorator  s    ??3)-cll.@.@.B)C%	J''I,@,@,H,HCIj,AB *D J 
		( 
	(" r@   r   )r<  r=  r>  r   s   ```@r>   is_flakyrW    s     < r@   c                 6   Uc)  [        [        R                  R                  SS5      5      nSn[        R
                  " U5      nUR                  S5      nUR                  S5      nUR                  X#S9  UR                  XXs4S9nUR                  5          UR                  US9n	UR                  5         UR                  US9  W	S   b  U R                  U	S    5        gg! [         a+  n
UR                  5         U R                  U
5         Sn
A
N\Sn
A
ff = f)	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   rj  )targetr<   error)r  r_   r`   ra   multiprocessingget_contextQueueJoinableQueueputProcessstart	task_donerC  	terminatefailrm   )r   target_funcinputsrk  start_methohdctxinput_queueoutput_queueprocessresultses              r>   run_test_in_subprocessrp  $  s    bjjnn%5s;<M

%
%m
4C))A,K$$Q'L OOFO,kk3WkXGMMO""7"3  LLL!w#''*+- $  qs   C# #
D-!DDc                   0    \ rS rSrSrS rS rS rS rSr	g)	CaptureLoggeriO  aG  
    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                     Xl         [        5       U l        [        R                  " U R                  5      U l        SU l        g )NrZ   )loggerr   r  loggingStreamHandlershout)selfrt  s     r>   __init__CaptureLogger.__init__d  s-    *''0r@   c                 P    U R                   R                  U R                  5        U $ r[  )rt  
addHandlerrw  ry  s    r>   	__enter__CaptureLogger.__enter__j  s    tww'r@   c                     U R                   R                  U R                  5        U R                  R	                  5       U l        g r[  )rt  removeHandlerrw  r  getvaluerx  )ry  excs     r>   __exit__CaptureLogger.__exit__n  s,    !!$''*77##%r@   c                 "    SU R                    S3$ )Nz
captured: rY   )rx  r~  s    r>   __repr__CaptureLogger.__repr__r  s    DHH:R((r@   )r  rt  rx  rw  N)
rD  
__module____qualname____firstlineno____doc__rz  r  r  r  __static_attributes__r   r@   r>   rr  rr  O  s    (&)r@   rr  c                  J   S[         R                  S'   S[         R                  S'   [        R                  " S5        S[        R                  R
                  l        S[        R                  R
                  l        S[        R                  R                  R                  l
        g)z
Helper function for reproducible behavior during distributed training. See
- https://pytorch.org/docs/stable/notes/randomness.html for pytorch
r   CUDA_LAUNCH_BLOCKINGz:16:8CUBLAS_WORKSPACE_CONFIGTFN)r_   r`   r6   use_deterministic_algorithmsbackendscudnndeterministic	benchmarkr/   matmul
allow_tf32r   r@   r>   enable_full_determinismr  v  sn     *-BJJ%&,3BJJ()	&&t, *.ENN&%*ENN",1ENN)r@   c                  |    S[         R                  S'   S[         R                  S'   [        R                  " S5        g )Nr   r  rZ   r  F)r_   r`   r6   r  r   r@   r>   disable_full_determinismr    s.    ),BJJ%&,.BJJ()	&&u-r@   c                 4   [        5       (       d  gSS KnUR                  " U 5      n  UR                  " SUR                  S9R                  U 5      nUR                  " X"5      ng! [         a(  nU R                  S:X  a  [        SU 35      e S nAgS nAff = f)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"   r6   devicezerosfloat16rg   mulrC  typer5   r  r6   r  r   ro  s        r>   r   r         \\&!FKKemm477?IIaO ;;&  ^  _`  ^a  b     <A% %
B/BBc                 4   [        5       (       d  gSS KnUR                  " U 5      n  UR                  " SUR                  S9R                  U 5      nUR                  " X"5      ng! [         a(  nU R                  S:X  a  [        SU 35      e S nAgS nAff = f)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"   r6   r  r  float64rg   r  rC  r  r5   r  s        r>   r   r     r  r  )r/   r(   r0   r1   r   c                      gNr   r   r   r@   r>   r  r        qr@   c                      gr  r   r   r@   r>   r  r    r  r@   reset_peak_memory_statsmax_memory_allocatedsynchronizer  dispatch_tablec                 ^    X;  a  US   " U0 UD6$ X   n[        U5      (       d  U$ U" U0 UD6$ )Nr   )rP  )r  r  r<   r=   fns        r>   _device_agnostic_dispatchr    sE    #i($9&99		B B<<	tvr@   seedc                 $    [        U [        U5      $ r[  )r  BACKEND_MANUAL_SEED)r  r  s     r>   backend_manual_seedr    s    $V-@$GGr@   c                 "    [        U [        5      $ r[  )r  BACKEND_SYNCHRONIZEr  s    r>   backend_synchronizer        $V-@AAr@   c                 "    [        U [        5      $ r[  )r  BACKEND_EMPTY_CACHEr  s    r>   backend_empty_cacher    r  r@   c                 "    [        U [        5      $ r[  )r  BACKEND_DEVICE_COUNTr  s    r>   backend_device_countr    s    $V-ABBr@   c                 "    [        U [        5      $ r[  )r  BACKEND_RESET_PEAK_MEMORY_STATSr  s    r>   backend_reset_peak_memory_statsr    s    $V-LMMr@   c                 "    [        U [        5      $ r[  )r  "BACKEND_RESET_MAX_MEMORY_ALLOCATEDr  s    r>   "backend_reset_max_memory_allocatedr    s    $V-OPPr@   c                 "    [        U [        5      $ r[  )r  BACKEND_MAX_MEMORY_ALLOCATEDr  s    r>   backend_max_memory_allocatedr    s    $V-IJJr@   c                 L    [        5       (       d  gU [        ;  a  Sn [        U    $ )NFr   )r"   BACKEND_SUPPORTS_TRAININGr  s    r>   r   r   "  s'    ..$V,,r@   device_fn_dictattribute_namec                      [        [        U5      nX [        '   g ! [         a'  nSU ;  a  [        SU S[         S35      Ue S nAg S nAff = f)Nr   `z` not found in 'z)' and no default fallback function found.)getattrdevice_spec_moduler   AttributeErrordevice_spec_path)r  r  spec_fnro  s       r>   update_mapping_from_specr  /  s`    		0.AG+2<( 	.$''78H7IIrs /	s    
AAA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RESET_PEAK_MEMORY_STATS_FNRESET_MAX_MEMORY_ALLOCATED_FNMAX_MEMORY_ALLOCATED_FNc                     [         (       d  [        (       a3  SSKn U R                  R	                  5       u  p[        (       a  SU4$ SU4$ [
        (       a.  SSKn U R                  R	                  5       S   nSnX4-  S-	  nSU4$ [        S4$ )	z$
Get environment device properties.
r   Nrocmr/   architecturel           r(   )IS_CUDA_SYSTEMIS_ROCM_SYSTEMr6   r/   get_device_capabilityIS_XPU_SYSTEMr(   r   )r6   r^  r   archgen_maskgens         r>   r   r   c  s    
 ~::335>E?"E?"	 yy..0@%2%s|d##r@   )_GO_LC_SUPPORTED_PYTORCH_LAYERS)_GROUP_ID_LAZY_LEAF_compute_group_hash"_find_parent_module_in_module_dict/_gather_buffers_with_no_group_offloading_parent2_gather_parameters_with_no_group_offloading_parentmoduleoffload_to_disk_pathoffload_typenum_blocks_per_groupc           
        ^ [        5       nS[        S[        4U4S jjnUS:X  Ga  Uc  [        S5      e/ nU R                  5        H  u  px[	        U[
        R                  R                  [
        R                  R                  45      (       d  UR                  U 5        M[  [        S[        U5      U5       HB  n	XX-    n
U
(       d  M  U SU	 SU	[        U
5      -   S-
   3nUR                  U" U5      5        MD     M     U H1  n UR                  U" U R                  R                   S	35      5        M3     U$ US
:X  Ga2  U R                  5        H3  u  px[	        U[         5      (       d  M  UR                  U" U5      5        M5     U R                  5        VVs1 s H  u  p|[	        U[         5      (       d  M  UiM      nnn[#        X5      n[%        X5      nX-   nU(       ar  0 n['        U R                  5       5      nU H1  u  nn[)        UU5      nUU;  a  / UU'   UU   R                  U5        M3     U H  nUR                  U" U5      5        M     UR                  U" [*        5      5        U$ [        SU 35      es  snnf )Ngroup_idr   c                 b   > [        U 5      n[        R                  R                  TSU S35      $ )Ngroup_z.safetensors)r  r_   r~   rm   )r  
short_hashr  s     r>   get_hashed_filename<_get_expected_safetensors_files.<locals>.get_hashed_filename  s,    ,X6J77<< 4zl,6WXXr@   block_levelzCnum_blocks_per_group must be provided for 'block_level' offloading.r   r   r   _unmatched_group
leaf_levelzUnsupported offload_type: )rM   rd   r5   named_childrenrL   r6   nn
ModuleList
Sequentialr   r   r  add	__class__rD  named_modulesr  r  r  dictr  r  )r  r  r  r  expected_filesr  unmatched_modulesr   	submoduler  current_modulesr  smmodules_with_group_offloading
parametersbuffersall_orphansparent_to_tensorsmodule_dicttensor_namer   parent_names    `                    r>   _get_expected_safetensors_filesr    s    	Y# 	Y# 	Y =(#+ !fgg !##)#8#8#:!)ehh.A.A588CVCV-WXX%,,V4q#i.2FGA&/A4L&MO* "&q1Q_1E-E-I,JKH"&&':8'DE H $; ,""#6&:J:J:S:S9TTd7e#fg ,F A \)#)#7#7#9i)HII"&&':4'@A $: &,%9%9%;-%;z"Nm?n%; * - LFrJEflG$.K$&!"6#7#7#9:&1NK"D[R]"^K"*;;9;)+6%k299+F	 '2 $5K"&&':;'GH $5 23FGH
  9,HII--s   J;Jc                     [         R                  R                  U5      (       d  g[        XX#5      n[	        [
        R
                  " [         R                  R                  US5      5      5      nXE-
  nXT-
  nU(       + =(       a    U(       + nXU4$ )N)FNNz*.safetensors)r_   r~   isdirr  rM   globrm   )	r  r  r  r  r  actual_filesmissing_filesextra_files
is_corrects	            r>    _check_safetensors_serializationr    sy     ww}}122$8,
 499RWW\\2F%XYZ&5"3&&:{?
55r@   c                   z    \ rS rSrS\4S jr\S\S\4S j5       r	\S\S\S\
4S j5       rSS\S\4S jjrS	 rS
rg)Expectationsi  r   c                 4    U R                  [        5       5      $ )zH
Find best matching expectation based on environment device properties.
)find_expectationr   r~  s    r>   get_expectationExpectations.get_expectation  s     $$%:%<==r@   rQ   c                 &    [        S U  5       5      $ )Nc              3   (   #    U  H  oS L v   M
     g 7fr[  r   )r  ps     r>   r  *Expectations.is_default.<locals>.<genexpr>  s     *c9cs   )allrQ   s    r>   
is_defaultExpectations.is_default  s    *c***r@   otherc                     U u  p#Uu  pESnX$:X  a  US-  nOUS;   a  US;   a  US-  nX5:X  a  Ub  US-  n[         R                  U5      (       a  US-  n[        U5      $ )a  
Returns score indicating how similar two instances of the `Properties` tuple are. Points are calculated using
bits, but documented as int. Rules are as follows:
    * Matching `type` gives 8 points.
    * Semi-matching `type`, for example cuda and rocm, gives 4 points.
    * Matching `major` (compute capability major version) gives 2 points.
    * Default expectation (if present) gives 1 points.
r   r  )r/   r     r  r   )r  r)  r  )rQ   r+  device_typer^  other_device_typeother_majorscores          r>   r1  Expectations.score  s|      #+0(	+VOE,,1BFV1VUNEK$;TME""5))SLE5zr@   c                    ^ [        U R                  R                  5       U4S jS9u  p#[        R	                  TU5      S:X  a  [        ST 35      eU$ )zE
Find best matching expectation based on provided device properties.
c                 6   > [         R                  TU S   5      $ r  )r  r1  )r  rQ   s    r>   r  /Expectations.find_expectation.<locals>.<lambda>  s    LDVDVWZ\]^_\`Dar@   r(  r   z"No matching expectation found for )r9   r   rK   r  r1  r5   )ry  rQ   
result_keyresults    `  r>   r   Expectations.find_expectation	  sM      #499??#4:abc:.!3A#GHHr@   c                     U R                    $ r[  )r   r~  s    r>   r  Expectations.__repr__  s    ))r@   r   N)NN)rD  r  r  r  r	   r!  staticmethodDevicePropertiesboolr)  r  r1  r   r  r  r   r@   r>   r  r    su    > > +( +T + + # ,<   4	$4 	 	r@   r  )NNztest_corrections.txtexpected_slicer[  )F)rB   NN)NT)   NNr;  )rR  r  r  importlib.metadatar|   r  ru  r]  r_   r   r  r  r\  r  rF  r   urllib.parser  collectionsr   
contextlibr   r   r   pathlibr   typingr   r	   r
   r   r   r   r   r   r   r  rC   	PIL.Imager  PIL.ImageOpsro  numpy.linalgr   	packagingr   	constantsr   import_utilsr   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r6   hipr  r/   r  r  r  Randomr   rD  rt  r  r   r!  _required_peft_version_required_transformers_versionr  r  getenvr+   r`   backendimport_moduler   ModuleNotFoundErrorro  r   r  RuntimeErrorinfor   r(   __version__is_torch_higher_equal_than_1_12r&  r  mps_backend_registeredr1   torch_utilsr2   r?   rH   rS   ry   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  r
  r  r  r  r  r  r  r  r%  r+  r2  r:  rA  rH  rP  rW  rY  ra  rd   rt  rv  r>  r{  r  r  r  r  r  r  r  r  r  r  r  r;  r   rW  rp  rr  r  r  r   r   r  empty_cacher  r   r  manual_seedr  r  r  reset_max_memory_allocatedr  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  is_filer5   indeximport_namer  DEVICE_NAMEdevice_namer  r  r=  	lru_cacher   DevicePropertiesUserDictdiffusers.hooks._commonr   diffusers.hooks.group_offloadingr  r  r  r  r  r   Moduler  r  r  r   r@   r>   <module>rh     s
        	   	  	  
       %    X X X       0    (   ]]&&d2N]]''t3NEMM5$7tCMNNM]]_
	H	*, MM)$$,,V45BB2MM%2  ";!< "MM)$$,,^<=JJBMM&B  *L.L RYY/45  2::-**56	''0A "**,zz"9:	\*A
 	/~>?::""$$!LYY##%% L L*1--MM%++,99+
]]6"+#' +%,U^^U%C"%;@R@R@_@_@a@a5htL=2 #)O4.9# 9  &j%@(F (F Q KQWY			h(:c
[Y\]i]ZUeaa"$						iU3

?+ # RTR\R\ 4 HSM QU eC01 ciioo >CII 3 ciioo.  PS   1 1h/ /($rzz"2 s VY "2::   *&~)D,3 ,x ,\deh\i ,`(.V$) $)N2$.,. )-d4PUbf g 

&&yy$$yy$$ 

''yy%% 

&&yy$$  yy$$$$ 

22uyy";TB'# 

55uyy";TB*& 

//uyy"8$?$  

&&uyy-6c 4X;N H H3 HB BB BC CNC NQs QK K-c - 
c8m1D 
VY 
 $rzz1::&BC$%--//bcsbtuvv	t*+J-=-C-CE-JKK '44[A	S,88K #bjj0\[5PST`Saa}  J  ~K  KN  OC``CS/!" 	!!46FG !46FG !57HI !:<OP !@B^_ !CEde !=?XY sDy)5d+;;<  $/ $ $0 '(8#(=>'G  /3	BB!B B 'sm	B
 
SBP /3	66!6 6 'sm	6
 
6(2+ 2W, # 	%=gY G778c; 	  	deqdrs	~&  	tXYiXjklrss	t  	S !QRR	SsT   d+ e e" 3e< +e1e  eeee"e9(e44e9<f