
    bis                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dl mZ d dlm	Z	m
Z
mZmZmZmZmZ d dlZd dlZddlm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   e        r,d dl!Z!e!jD                  jF                  jI                         rd dl#Z# e       rd d	l%m&Z&  e       rd d
l'm(Z(  e       rd dl)Z)de*fdZ+d Z,de-fdZ.	 d6dededej^                  dej^                  dej^                  dej^                  dej^                  de0deeej^                     eej^                     f   fdZ1dede
e-ej^                  f   fdZ2ejf                  fdeejh                  jj                  eejh                  jj                     f   fdZ6de
e-ej^                  f   de-dejh                  jj                  fd Z7d!e
e-ejh                  jj                  f   de
e-e	f   fd"Z8	 	 	 	 	 d7d#e-d$e*d%e0d&e0d'e0d(eejr                  e-f   d)eejt                     fd*Z;d8d#e-fd+Z<d, Z=ed-d.d/eejh                  jj                  ef   d(ee-ejr                  f   d0e>fd1       Z?d2 Z@d3 ZA G d4 d5      ZBy)9    N)contextmanager)AnyDictIterableListOptionalTupleUnion   )UNet2DConditionModel)DiffusionPipeline)SchedulerMixin)convert_state_dict_to_diffusersconvert_state_dict_to_peft	deprecateis_peft_availableis_torch_npu_availableis_torchvision_availableis_transformers_available)set_peft_model_state_dict)
transformsseedc                 (   t        j                  |        t        j                   j                  |        t        j                  |        t               r t        j                  j                  |        yt        j                  j                  |        y)z
    Helper function for reproducible behavior to set the seed in `random`, `numpy`, `torch`.

    Args:
        seed (`int`): The seed to set.

    Returns:
        `None`
    N)	randomr   nptorchmanual_seedr   npumanual_seed_allcuda)r   s    S/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/training_utils.pyset_seedr"   ,   sX     KKIINN4	d		!!$'

""4(    c                    | j                   }|dz  }d|z
  dz  }|j                  |j                        |   j                         }t	        |j
                        t	        |j
                        k  r1|d   }t	        |j
                        t	        |j
                        k  r1|j                  |j
                        }|j                  |j                        |   j                         }t	        |j
                        t	        |j
                        k  r1|d   }t	        |j
                        t	        |j
                        k  r1|j                  |j
                        }||z  dz  }|S )a  
    Computes SNR as per
    https://github.com/TiankaiHang/Min-SNR-Diffusion-Training/blob/521b624bd70c67cee4bdf49225915f5945a872e3/guided_diffusion/gaussian_diffusion.py#L847-L849
    for the given timesteps using the provided noise scheduler.

    Args:
        noise_scheduler (`NoiseScheduler`):
            An object containing the noise schedule parameters, specifically `alphas_cumprod`, which is used to compute
            the SNR values.
        timesteps (`torch.Tensor`):
            A tensor of timesteps for which the SNR is computed.

    Returns:
        `torch.Tensor`: A tensor containing the computed SNR values for each timestep.
          ?      ?device).N   )alphas_cumprodtor(   floatlenshapeexpand)noise_scheduler	timestepsr*   sqrt_alphas_cumprodsqrt_one_minus_alphas_cumprodalphasigmasnrs           r!   compute_snrr7   @   sH     %33N(#-%(>%9c$A! .00	8H8H0I)TZZ\
!''
(3y+?
?1)< !''
(3y+?
?&&y7E$A$D$DIL\L\$D$]^g$h$n$n$p!
+11
2S5I
I(Ei(P% +11
2S5I
I)00AE 5=Q
CJr#   interpolation_typec                    t               st        d      | dk(  rt        j                  j                  }|S | dk(  rt        j                  j
                  }|S | dk(  rt        j                  j                  }|S | dk(  rt        j                  j                  }|S | dk(  rt        j                  j                  }|S | dk(  rt        j                  j                  }|S | dk(  rt        j                  j                  }|S t        d	|  d
      )a  
    Maps a string describing an interpolation function to the corresponding torchvision `InterpolationMode` enum. The
    full list of supported enums is documented at
    https://pytorch.org/vision/0.9/transforms.html#torchvision.transforms.functional.InterpolationMode.

    Args:
        interpolation_type (`str`):
            A string describing an interpolation method. Currently, `bilinear`, `bicubic`, `box`, `nearest`,
            `nearest_exact`, `hamming`, and `lanczos` are supported, corresponding to the supported interpolation modes
            in torchvision.

    Returns:
        `torchvision.transforms.InterpolationMode`: an `InterpolationMode` enum used by torchvision's `resize`
        transform.
    zhPlease make sure to install `torchvision` to be able to use the `resolve_interpolation_mode()` function.bilinearbicubicboxnearestnearest_exacthamminglanczoszThe given interpolation mode z is not supported. Currently supported interpolation modes are `bilinear`, `bicubic`, `box`, `nearest`, `nearest_exact`, `hamming`, and `lanczos`.)r   ImportErrorr   InterpolationModeBILINEARBICUBICBOXNEARESTNEAREST_EXACTHAMMINGLANCZOS
ValueError)r8   interpolation_modes     r!   resolve_interpolation_moderL   e   s;     $%v
 	
 Z''99BB& % 
y	('99AA" ! 
u	$'99==  
y	('99AA  
	.'99GG  
y	('99AA  
y	('99AA  +,>+? @m n
 	
r#   unetr0   r1   noisenoisy_latentstargetencoder_hidden_statesdream_detail_preservationreturnc                 X   |j                   j                  |j                        |dddf   }d|z
  dz  }	|	|z  }
d}t        j                         5   | |||      j
                  }ddd       d\  }}|j                  j                  dk(  rO|}||z
  j                         }|j                  |
       |j                  |	|z        }|j                  |      }||fS |j                  j                  dk(  rt        d      t        d|j                  j                         # 1 sw Y   xY w)	a  
    Implements "DREAM (Diffusion Rectification and Estimation-Adaptive Models)" from
    https://huggingface.co/papers/2312.00210. DREAM helps align training with sampling to help training be more
    efficient and accurate at the cost of an extra forward step without gradients.

    Args:
        `unet`: The state unet to use to make a prediction.
        `noise_scheduler`: The noise scheduler used to add noise for the given timestep.
        `timesteps`: The timesteps for the noise_scheduler to user.
        `noise`: A tensor of noise in the shape of noisy_latents.
        `noisy_latents`: Previously noise latents from the training loop.
        `target`: The ground-truth tensor to predict after eps is removed.
        `encoder_hidden_states`: Text embeddings from the text model.
        `dream_detail_preservation`: A float value that indicates detail preservation level.
          See reference.

    Returns:
        `tuple[torch.Tensor, torch.Tensor]`: Adjusted noisy_latents and target.
    Nr&   r%   )NNepsilonv_predictionz/DREAM has not been implemented for v-predictionzUnknown prediction type )r*   r+   r(   r   no_gradsampleconfigprediction_typedetachmul_addNotImplementedErrorrJ   )rM   r0   r1   rN   rO   rP   rQ   rR   r*   r3   dream_lambdapred_noisy_latents_targetpredicted_noisedelta_noises                   r!    compute_dream_and_update_latentsre      sC   : %3366y7G7GHTXZ^`dIdeN%(>%9c$A! 12KKLD	 LM9.CDKKL +NG--:.668&&**+H;+VW**[) 7"" 
			/	/>	A!"STT3O4J4J4Z4Z3[\]]L Ls   D  D)c                     i }| j                         D ]S  \  }}t        |d      st        |d      }|"|j                         }|j	                         D ]  \  }}||| d| <    U |S )zL
    Returns:
        A state dict containing just the LoRA parameters.
    set_lora_layer
lora_layerz.lora.)named_moduleshasattrgetattr
state_dictitems)rM   lora_state_dictnamemodulerh   current_lora_layer_sdlora_layer_matrix_name
lora_params           r!   unet_lora_state_dictrt      s    
 O**, Zf6+, 6J%(2(=(=(?%:O:U:U:W Z6*JOYOtfF3I2J$KLZZ r#   modelc                     t        | t              s| g} | D ]:  }|j                         D ]%  }|j                  s|j	                  |      |_        ' < y)z
    Casts the training parameters of the model to the specified data type.

    Args:
        model: The PyTorch model whose parameters will be cast.
        dtype: The data type to which the model parameters will be cast.
    N)
isinstancelist
parametersrequires_gradr+   data)ru   dtypemparams       r!   cast_training_paramsr      sQ     eT" -\\^ 	-E"""XXe_
	--r#   rn   prefixtext_encoderc                     | j                         D ci c]+  \  }}|j                  |      s|j                  |d       |- }}}t        t	        |            }t        ||d       yc c}}w )aD  
    Sets the `lora_state_dict` into `text_encoder` coming from `transformers`.

    Args:
        lora_state_dict: The state dictionary to be set.
        prefix: String identifier to retrieve the portion of the state dict that belongs to `text_encoder`.
        text_encoder: Where the `lora_state_dict` is to be set.
     default)adapter_nameN)rm   
startswithreplacer   r   r   )rn   r   r   kvtext_encoder_state_dicts         r!   !_set_state_dict_into_text_encoderr      ss     3B2G2G2I*.!QQ\\Z`Ma199VR 
!A%  99XYp9qrl,CR[\	s
   A*A*modules_to_savec                     i }| j                         D ]+  \  }}|	|j                  d   j                         || d<   - |S )Nr   _lora_adapter_metadata)rm   peft_configto_dict)r   	metadatasmodule_namerp   s       r!   _collate_lora_metadatar      s[    I.446 hV@F@R@RS\@]@e@e@gI%;<=h r#   weighting_scheme
batch_size
logit_mean	logit_std
mode_scaler(   	generatorc                    | dk(  rFt        j                  |||f||      }t         j                  j                  j	                  |      }|S | dk(  rVt        j
                  |f||      }d|z
  |t        j                  t        j                  |z  dz        dz  dz
  |z   z  z
  }|S t        j
                  |f||      }|S )a  
    Compute the density for sampling the timesteps when doing SD3 training.

    Courtesy: This was contributed by Rafie Walker in https://github.com/huggingface/diffusers/pull/8528.

    SD3 paper reference: https://huggingface.co/papers/2403.03206v1.
    logit_normal)meanstdsizer(   r   mode)r   r(   r   r   r)   )	r   normalnn
functionalsigmoidrandcosmathpi)r   r   r   r   r   r(   r   us           r!   %compute_density_for_timestep_samplingr     s      >)LLjizmTZfopHH''* H 
V	#JJZM&INEJ%))DGGaK!O"<"AA"E"IJJ H JJZM&INHr#   c                     | dk(  r|dz  j                         }|S | dk(  r)dd|z  z
  d|dz  z  z   }dt        j                  |z  z  }|S t        j                  |      }|S )z
    Computes loss weighting scheme for SD3 training.

    Courtesy: This was contributed by Rafie Walker in https://github.com/huggingface/diffusers/pull/8528.

    SD3 paper reference: https://huggingface.co/papers/2403.03206v1.
    
sigma_sqrtg       cosmapr   r)   )r,   r   r   r   	ones_like)r   sigmas	weightingbots       r!   compute_loss_weighting_for_sd3r   !  sz     <'T\((*	  
X	%!f*nq619},3'	  OOF+	r#   c                  "   t        j                          t        j                  j	                         rt        j                  j                          yt        j                  j                  j	                         rt        j                  j                          yt               rt        j                  j                          yt        t        d      r>t        j                  j	                         rt        j                  j                          yyy)zV
    Runs garbage collection. Then clears the cache of the available accelerator.
    xpuN)gccollectr   r    is_availableempty_cachebackendsmpsr   	torch_npur   rj   r    r#   r!   free_memoryr   3  s     JJLzz 

 				(	(	*				!!!#		599#9#9#;		 $<	r#   T)offloadmodulesr   c              '     K   |r~t        d |D               }|r1|D cg c]%  }t        |j                               j                  ' }}n t	        |      dk(  sJ |d   j                  g}|D ]  }|j                  |         	 d |r&t        |      D ]  \  }}|j                  |        yyc c}w # |r&t        |      D ]  \  }}|j                  |        w w xY ww)a  
    Context manager that, if offload=True, moves each module to `device` on enter, then moves it back to its original
    device on exit.

    Args:
        device (`str` or `torch.Device`): Device to move the `modules` to.
        offload (`bool`): Flag to enable offloading.
    c              3   <   K   | ]  }t        |t                y wN)rw   r   ).0r}   s     r!   	<genexpr>z!offload_models.<locals>.<genexpr>P  s     M:a):;Ms   r   r   N)anynextry   r(   r-   r+   zip)r(   r   r   is_modelr}   original_devicesorig_devs          r!   offload_modelsr   C  s      MWMMMELMQ\\^ 4 ; ;MMw<1$$$ '
 1 12 	ADDL	"7,<= 8X   N "7,<= 8X s(   C#*B1;C#B6 .C#6*C  C#c                 0   | st        d      | j                         j                  d      }g }|D ]  }t        j                  d|      }|st        d| d      	 t        |j                  d            }t        |j                  d            }|dk  s|dk  rt        d	      |d
z  dk7  s|d
z  dk7  rt        j                  d| d| d       |j                  ||f        |st        d      |S # t         $ r}t        d| d|       |d}~ww xY w)zGParses a string defining buckets into a list of (height, width) tuples.zBucket string cannot be empty.;z^\s*(\d+)\s*,\s*(\d+)\s*$zInvalid bucket format: 'z'. Expected 'height,width'.r   r)   r   z,Bucket dimensions must be positive integers.   zBucket dimension (,z.) not divisible by 8. This might cause issues.z Invalid integer in bucket pair 'z': Nz.No valid buckets found in the provided string.)
rJ   stripsplitrematchintgroupwarningswarnappend)buckets_strbucket_pairsparsed_bucketspair_strr   heightwidthes           r!   parse_buckets_stringr   e  s8   9::$$&,,S1LN  Y5x@7zA\]^^		YQ(FA'E{eqj !OPPzQ%!)q. 26(!E7Bpqr!!65/2Y IJJ  	Y?zQCPQWXX	Ys   BC55	D>DDc                     t        d      }d}t        |      D ]&  \  }\  }}t        | |z  ||z  z
        }||k  s#|}|}( |S )z6Finds the closes bucket to the given height and width.infN)r,   	enumerateabs)	hwbucket_options
min_metricbest_bucket_idx
bucket_idxbucket_hbucket_wmetrics	            r!   find_nearest_bucketr     s_    uJO,5n,E )(
(XxQ\AL01ZJ(O	)
 r#   c                      e Zd ZdZ	 	 	 	 	 	 	 	 	 ddeej                  j                     dedede	de
deee	f   d	eee	f   d
e
dee   deeef   fdZeddd       Zd Zde	defdZ ej*                         deej                  j                     fd       Zdeej                  j                     ddfdZddZd ddZdefdZdeej                  j                     ddfdZdeej                  j                     ddfdZdeddfdZy)!EMAModelz6
    Exponential Moving Average of models weights
    Nry   decay	min_decayupdate_after_stepuse_ema_warmup	inv_gammapowerforeach	model_clsmodel_configc                    t        |t        j                  j                        r#d}t	        dd|d       |j                         }d}|j                  dd      d	}t	        dd|d       |d   }|j                  d
d      d}t	        d
d|d       |d
   }t        |      }|D cg c]   }|j                         j                         " c}| _
        |j                  dd      &d}t	        dd|d       | j                  |d          d| _        || _        || _        || _        || _        || _        || _        d| _        d| _        || _        |	| _        |
| _        yc c}w )a{  
        Args:
            parameters (Iterable[torch.nn.Parameter]): The parameters to track.
            decay (float): The decay factor for the exponential moving average.
            min_decay (float): The minimum decay factor for the exponential moving average.
            update_after_step (int): The number of steps to wait before starting to update the EMA weights.
            use_ema_warmup (bool): Whether to use EMA warmup.
            inv_gamma (float):
                Inverse multiplicative factor of EMA warmup. Default: 1. Only used if `use_ema_warmup` is True.
            power (float): Exponential factor of EMA warmup. Default: 2/3. Only used if `use_ema_warmup` is True.
            foreach (bool): Use torch._foreach functions for updating shadow parameters. Should be faster.
            device (Optional[Union[str, torch.device]]): The device to store the EMA weights on. If None, the EMA
                        weights will be stored on CPU.

        @crowsonkb's notes on EMA Warmup:
            If gamma=1 and power=1, implements a simple average. gamma=1, power=2/3 are good values for models you plan
            to train for a million or more steps (reaches decay factor 0.999 at 31.6K steps, 0.9999 at 1M steps),
            gamma=1, power=3/4 for models you plan to train for less (reaches decay factor 0.999 at 10K steps, 0.9999
            at 215.4k steps).
        zzPassing a `torch.nn.Module` to `ExponentialMovingAverage` is deprecated. Please pass the parameters of the module instead.z9passing a `torch.nn.Module` to `ExponentialMovingAverage`1.0.0Fstandard_warnT	max_valueNzCThe `max_value` argument is deprecated. Please use `decay` instead.	min_valuezGThe `min_value` argument is deprecated. Please use `min_decay` instead.r(   z=The `device` argument is deprecated. Please use `to` instead.r'   r   )rw   r   r   Moduler   ry   getrx   cloner[   shadow_paramsr+   temp_stored_paramsr   r   r   r   r   r   optimization_stepcur_decay_valuer   r   r   )selfry   r   r   r   r   r   r   r   r   r   kwargsdeprecation_messageps                 r!   __init__zEMAModel.__init__  st   F j%((//2D   K##	 $..0J "N::k4(4"gk7,?uU;'E::k4(4"kk7,?uU{+I*%
:DEQaggi..0E::h%1"ah)<ERGG6(+G,"&
"!2,"
!"#"() Fs   '%E!rS   c                     |j                  |d      \  }}|j                  |      } | |j                         ||j                  |      }|j	                  |       |S )NT)return_unused_kwargs)r   r   r   )from_configfrom_pretrainedry   rY   load_state_dict)clspathr   r   _
ema_kwargsru   	ema_models           r!   r  zEMAModel.from_pretrained  s]    !--d-N:))$/((*ielldkl	!!*-r#   c                 r   | j                   t        d      | j                  t        d      | j                   j                  | j                        }| j	                         }|j                  dd         |j                  di | | j                  |j                                |j                  |       y )NzJ`save_pretrained` can only be used if `model_cls` was defined at __init__.zM`save_pretrained` can only be used if `model_config` was defined at __init__.r  r   )
r   rJ   r   r  rl   popregister_to_configcopy_tory   save_pretrained)r  r  ru   rl   s       r!   r  zEMAModel.save_pretrained  s    >>!ijj$lmm**4+<+<=__&
-   .:.U%%'(d#r#   r  c                    t        d|| j                  z
  dz
        }|dk  ry| j                  r$dd|| j                  z  z   | j                   z  z
  }nd|z   d|z   z  }t        || j                        }t        || j                        }|S )zN
        Compute the decay factor for the exponential moving average.
        r   r           
   )maxr   r   r   r   minr   r   )r  r  stepr  s       r!   	get_decayzEMAModel.get_decay   s     1'$*@*@@1DE191tdnn'<#<$**"LLO 4xBI6Ootzz:ot~~>r#   c           	         t        |t        j                  j                        r!d}t	        dd|d       |j                         }t        |      }| xj                  dz  c_        | j                  | j                        }|| _	        d|z
  }t        j                         }| j                  rZt               rIt        j                  j                   j#                         r!t         j$                  j'                  |d       }|5  |D cg c]  }|j(                  s| }}t+        | j,                  |      D cg c]  \  }}|j(                  s| }	}}t/        |      t/        |      k  rgt        j0                  t+        | j,                  |      D cg c]  \  }}|j(                  r| c}}|D cg c]  }|j(                  r| c}d	       t        j2                  |	t        j4                  |	|      |
       d d d        y t+        | j,                  |      D ]  \  }}t               rIt        j                  j                   j#                         r!t         j$                  j'                  |d       }|5  |j(                  r|j7                  |||z
  z         n|j9                  |       d d d         y c c}w c c}}w c c}}w c c}w # 1 sw Y   y xY w# 1 sw Y   xY w)NzPassing a `torch.nn.Module` to `ExponentialMovingAverage.step` is deprecated. Please pass the parameters of the module instead.z>passing a `torch.nn.Module` to `ExponentialMovingAverage.step`r   Fr   r   )modifier_rankT)non_blocking)r4   )rw   r   r   r   r   ry   rx   r  r   r  
contextlibnullcontextr   r   transformersintegrations	deepspeedis_deepspeed_zero3_enabledzeroGatheredParametersrz   r   r  r-   _foreach_copy__foreach_sub__foreach_subsub_copy_)
r  ry   r  r   one_minus_decaycontext_managerr~   params_grads_params_params_grads
             r!   r  zEMAModel.step  sz   j%((//2D   P##	 $..0J*%
!# t556$e)$002<<(*|/H/H/R/R/m/m/o"+.."C"CJ^b"C"c  2<T@S@SuTT25d6H6H*2U! .Y^YlYlG! ! {#c*o5((7:4;M;Mz7Zv^Webgbubuv,6R5e>Q>QR%) ##!5#5#5m[#QYh $ #&d&8&8*"E -,.<3L3L3V3V3q3q3s&/nn&G&G]a&G&bO$ -**_%%HIe,	- -	-# U! wR ,- -sg   KJ:J:K9J?J?AKK)K-K4KK
5K96K:KKK%	c           
         t        |      }| j                  ryt        j                  |D cg c]  }|j                   c}t        | j                  |      D cg c]*  \  }}|j                  |j                        j                  , c}}       yt        | j                  |      D ]C  \  }}|j                  j                  |j                  |j                        j                         E yc c}w c c}}w )aa  
        Copy current averaged parameters into given collection of parameters.

        Args:
            parameters: Iterable of `torch.nn.Parameter`; the parameters to be
                updated with the stored moving averages. If `None`, the parameters with which this
                `ExponentialMovingAverage` was initialized will be used.
        N)
rx   r   r   r,  r{   r   r  r+   r(   r0  )r  ry   r~   r4  s       r!   r  zEMAModel.copy_toM  s     *%
<<  )344EHI[I[]gEhi>7EELL)..i
 #&d&8&8*"E @

  ELL!9!>!>?@	 5is   C./C3c                 h    | j                   D cg c]  }|j                          c}| _         yc c}w )z
        Move internal buffers of the ExponentialMovingAverage to pinned memory. Useful for non-blocking transfers for
        offloading EMA params to the host.
        N)r  
pin_memory)r  r	  s     r!   r8  zEMAModel.pin_memory`  s&     7;6H6HIallnIIs   /c                     | j                   D cg c]9  }|j                         r|j                  |||      n|j                  ||      ; c}| _         yc c}w )z
        Move internal buffers of the ExponentialMovingAverage to `device`.

        Args:
            device: like `device` argument to `torch.Tensor.to`
        )r(   r|   r#  )r(   r#  N)r  is_floating_pointr+   )r  r(   r|   r#  r	  s        r!   r+   zEMAModel.toh  s_     ''	
  ""$ DDe,DGV,?@
 
s   >Ac           	          | j                   | j                  | j                  | j                  | j                  | j
                  | j                  | j                  dS )z
        Returns the state of the ExponentialMovingAverage as a dict. This method is used by accelerate during
        checkpointing to save the ema state dict.
        r   r   r  r   r   r   r   r  r<  )r  s    r!   rl   zEMAModel.state_dictw  sN     ZZ!%!7!7!%!7!7"11ZZ!//	
 		
r#   c                     |D cg c].  }|j                         j                         j                         0 c}| _        yc c}w )z
        Saves the current parameters for restoring later.

        Args:
            parameters: Iterable of `torch.nn.Parameter`. The parameters to be temporarily stored.
        N)r[   cpur  r  )r  ry   r~   s      r!   storezEMAModel.store  s4     NX"XE5<<>#5#5#7#=#=#?"X"Xs   3Ac                    | j                   t        d      | j                  rXt        j                  |D cg c]  }|j
                   c}| j                   D cg c]  }|j
                   c}       d| _         yt        | j                   |      D ]*  \  }}|j
                  j                  |j
                         , d| _         yc c}w c c}w )aG  
        Restore the parameters stored with the `store` method. Useful to validate the model with EMA parameters
        without: affecting the original optimization process. Store the parameters before the `copy_to()` method. After
        validation (or model saving), use this to restore the former parameters.

        Args:
            parameters: Iterable of `torch.nn.Parameter`; the parameters to be
                updated with the stored parameters. If `None`, the parameters with which this
                `ExponentialMovingAverage` was initialized will be used.
        NzGThis ExponentialMovingAverage has no `store()`ed weights to `restore()`)r  RuntimeErrorr   r   r,  r{   r   r0  )r  ry   r~   c_params       r!   restorezEMAModel.restore  s     ""*hii<<  )344SWSjSj6kw||6k #'	 #&d&=&=z"J /

  ./ #' 56ks   CC
rl   c                    t        j                  |      }|j                  d| j                        | _        | j                  dk  s| j                  dkD  rt	        d      |j                  d| j
                        | _        t        | j
                  t              st	        d      |j                  d| j                        | _        t        | j                  t              st	        d      |j                  d	| j                        | _
        t        | j                  t              st	        d
      |j                  d| j                        | _        t        | j                  t              st	        d      |j                  d| j                        | _        t        | j                  t        t        f      st	        d      |j                  d| j                        | _        t        | j                  t        t        f      st	        d      |j                  dd      }|T|| _        t        | j                  t               st	        d      t#        d | j                  D              st	        d      yy)a  
        Loads the ExponentialMovingAverage state. This method is used by accelerate during checkpointing to save the
        ema state dict.

        Args:
            state_dict (dict): EMA state. Should be an object returned
                from a call to :meth:`state_dict`.
        r   r  r&   zDecay must be between 0 and 1r   zInvalid min_decayr  zInvalid optimization_stepr   zInvalid update_after_stepr   zInvalid use_ema_warmupr   zInvalid inv_gammar   zInvalid powerr  Nzshadow_params must be a listc              3   P   K   | ]  }t        |t        j                           y wr   )rw   r   Tensor)r   r	  s     r!   r   z+EMAModel.load_state_dict.<locals>.<genexpr>  s     Oqz!U\\2Os   $&z!shadow_params must all be Tensors)copydeepcopyr   r   rJ   r   rw   r,   r  r   r   r   boolr   r   r  rx   all)r  rl   r  s      r!   r  zEMAModel.load_state_dict  s    ]]:.
^^GTZZ8
::tzzC/<==#T^^D$..%0011!+0CTE[E[!\$00#6899!+0CTE[E[!\$00#6899(nn-=t?R?RS$--t4566#T^^D$..5#,7011^^GTZZ8
$**ucl3_--"=$!.Dd00$7 !?@@OD<N<NOO !DEE P	 %r#   )	gH.?r  r   Fr&   gUUUUUU?FNN)F)rS   r   )rS   N)NNF)__name__
__module____qualname____doc__r   r   r   	Parameterr,   r   rI  r
   r   r   r   strr
  classmethodr  r  r   rW   r  r  r8  r+   dictrl   r?  rC  r  r   r#   r!   r   r     s    !"$'*#(#''+R)UXX//0R) R) 	R)
 R) R) $R) UCZ R) R) C=R) 38nR)h  $3 5 & U]]_7-x(:(:; 7- 7-r@(588+=+="> @4 @&J

D 
&Y);); < Y Y'(588+=+="> '4 '2.F$ .F4 .Fr#   r   )r&   )NNNr>  Nr   )Cr$  rG  r   r   r   r   r   r   typingr   r   r   r   r   r	   r
   numpyr   r   modelsr   	pipelinesr   
schedulersr   utilsr   r   r   r   r   r   r   r&  r'  r(  r)  peftr   torchvisionr   r   r   r"   r7   rP  rL   rF  r,   re   rt   float32r   r   r   r   r   r(   	Generatorr   r   r   rI  r   r   r   r   r   r#   r!   <module>r]     s     	   	  % D D D   ( ( &     **EEG.&)3 )("J)3 )h (+3#
3##3# ||3# <<	3#
 <<3# LL3# !<<3#  %3# 8ELL!8ELL#99:3#l3 S%,,=N8O & V[UbUb -ehhootEHHOO7L&L M -"]#u||+,]69]IN]&Dehhoo1E,F 4PSUXPX>  ',+/  	
  %,,#$ (6S $   koEHHOO%667AFsELLGXAYdh B8	LF LFr#   