
    ]i                         S r SSK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	r	SSK
Jr  SSKJr  SSKrS rSS jrSS	 jrSS
 jrSS jrS rSS jrS r\S 5       rS rg)z$
Utilities to save and load models.
    )contextmanagerN)Path)	OmegaConf)fatalc                  D     SS K n g ! [         a    [        S5         g f = f)Nr   zTrying to use DiffQ, but diffq is not installed.
On Windows run: python.exe -m pip install diffq 
On Linux/Mac, run: python3 -m pip install diffq)diffqImportErrorr   )r   s    J/mnt/rpi/tmp/demucs-venv-sys/lib/python3.13/site-packages/demucs/states.py_check_diffqr      s+    A A @ 	AAs    c                 &   SnUR                   (       aA  [        5         SSK Jn  U" XR                  UR                  S9nUb  UR                  U5        U$ UR                  (       a+  [        5         SSK Jn  U" XR                  UR                  S9nU$ )z4Return the quantizer given the XP quantization args.Nr   )DiffQuantizer)min_size
group_size)UniformQuantizer)bitsr   )r   r   r   r   r   setup_optimizerqatr   )modelargs	optimizer	quantizerr   r   s         r
   get_quantizerr       sx    Izz'!MMdooG	 %%i0  
*$HHt}}>	    c                 ^   [        U [        5      (       a  U nOx[        U [        [        45      (       aN  [        R
                  " 5          [        R                  " S5        U n[        R                  " US5      nSSS5        O[        SU  S35      eWS   nUS   nUS   nU(       a	  U" U0 UD6nO\[        R                  " U5      n[        U5       H/  n	XR                  ;  d  M  [        R                  " S	U	-   5        Xi	 M1     U" U0 UD6nUS
   n
[        Xz5        U$ ! , (       d  f       N= f)zrLoad a model from the given serialized model, either given as a dict (already loaded)
or a path to a file on disk.ignorecpuNzInvalid type for .klassr   kwargszDropping inexistant parameter state)
isinstancedictstrr   warningscatch_warningssimplefiltertorchload
ValueErrorinspect	signaturelist
parameterswarn	set_state)path_or_packagestrictpackagepathr   r   r   r   sigkeyr    s              r
   
load_modelr6   2   s    /4((!	Oc4[	1	1$$&!!(+"Djju-G '&
 ,_,=Q?@@GE6?DXFt&v&&<C..(>DEK   t&v&GEeL1 '&s   	0D
D,c           
         Uce  U(       a  [         R                  OSnU R                  5       R                  5        VVs0 s H  u  pEXER                  R                  SUS9_M!     nnnU$ UR                  5       nSUS'   U$ s  snnf )zGet the state from a model, potentially with quantization applied.
If `half` is True, model are stored as half precision, which shouldn't impact performance
but half the state size.Nr   )devicedtypeT__quantized)r'   half
state_dictitemsdatatoget_quantized_state)r   r   r;   r9   kpr    s          r
   	get_staterC   S   s     "

EJEUEUEWE]E]E_`E_TQFFIIU%I88E_` L --/#mL	 as   &B c                     UR                  S5      (       a4  Ub  UR                  XS   5        U$ [        5         SSKJn  U" X5         U$ U R	                  U5        U$ )zSet the state on a given model.r:   	quantizedr   )restore_quantized_state)getrF   r   r   load_state_dict)r   r    r   rF   s       r
   r/   r/   `   s_    yy --e;5GH L N5#E1 L 	e$Lr   c                 Z   [         R                  " 5       n[        R                  " X5        [        R
                  " UR                  5       5      R                  5       SS nUR                  UR                  S-   U-   UR                  -   -  nUR                  UR                  5       5        g)zSave the given value on disk, along with a sha256 hash.
Should be used with the output of either `serialize_model` or `get_state`.N   -)ioBytesIOr'   savehashlibsha256getvalue	hexdigestparentstemsuffixwrite_bytes)contentr3   bufr4   s       r
   save_with_checksumrY   n   sw     **,C	JJw
..
(
2
2
4Ra
8C;;$))c/C/$++=>DS\\^$r   c           	          U R                   u  pEU R                  n[        XU5      nUUUU[        R                  " USS9S.$ )NT)resolve)r   r   r   r    training_args)_init_args_kwargs	__class__rC   r   to_container)r   r\   r   r;   r   r   r   r    s           r
   serialize_modelr`   y   sJ    **LDOOEe-E"//tL r   c                     U R                  5        VVs0 s H#  u  pXR                  5       R                  5       _M%     snn$ s  snnf N)r=   r   clone)r    rA   vs      r
   
copy_statere      s1    +0;;=9=41Auuw}}=999s   *Ac              #      #    [        U R                  5       5      nU R                  USS9   Sv   U R                  U5        g! U R                  U5        f = f7f)z
Context manager that swaps the state of a model, e.g:

    # model is in old state
    with swap_state(model, new_state):
        # model in new state
    # model back to old state
F)r1   N)re   r<   rH   )r   r    	old_states      r
   
swap_staterh      sQ      5++-.I	%.)i(i(s   *AA AAAc                 F   ^  [         R                  " T 5      U 4S j5       nU$ )Nc                 .   > X4U l         T" U /UQ70 UD6  g rb   )r]   )selfr   r   inits      r
   __init__capture_init.<locals>.__init__   s    "&T#D#F#r   )	functoolswraps)rl   rm   s   ` r
   capture_initrq      s%    __T$ $ Or   rb   )F)NT)__doc__
contextlibr   ro   rO   r*   rL   pathlibr   r$   	omegaconfr   dora.logr   r'   r   r   r6   rC   r/   rY   r`   re   rh   rq    r   r
   <module>rx      sm    &    	     A$B
%: ) )"r   