
    mi                         d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	Z	ddl
mZ ddlmZ ddlZd ZddZdd	Zdd
ZddZd ZddZd Zed        Zd Zy)z$
Utilities to save and load models.
    )contextmanagerN)Path)	OmegaConf)fatalc                  B    	 dd l } y # t        $ r t        d       Y y w xY w)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    G/var/www/stems/demucs_env/lib/python3.12/site-packages/demucs/states.py_check_diffqr      s+    A A @ 	AAs    c                     d}|j                   rDt                ddl m}  || |j                  |j                        }||j                  |       |S |j                  r/t                ddl m}  || |j                  |j                        }|S )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       s|    Izz'!DMMdooG	 %%i0  
*$DHHt}}>	    c                 (   t        | t              r| }npt        | t        t        f      rKt	        j
                         5  t	        j                  d       | }t        j                  |d      }ddd       nt        d|  d      d   }|d   }|d   }|r	 ||i |}nWt        j                  |      }t        |      D ],  }	|	|j                  vst	        j                  d	|	z          ||	= .  ||i |}|d
   }
t        ||
       |S # 1 sw Y   xY w)zvLoad 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$$& 	.!!(+"Djju-G	. 	.
 ,_,=Q?@@GE6?DXFt&v&&< 	 C#..(>DE3K	  t&v&GEeUL1	. 	.s   .DDc           	         |b|rt         j                  nd}| j                         j                         D ci c]#  \  }}||j                  j                  d|      % }}}|S |j                         }d|d<   |S c c}}w )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_`TQAFFIIU%I88`` L --/#mL	 as   (A<c                     |j                  d      r4||j                  | |d          |S t                ddlm}  || |       |S | j	                  |       |S )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 --eU;5GH L N5#E51 L 	e$Lr   c                 V   t        j                         }t        j                  | |       t	        j
                  |j                               j                         dd }|j                  |j                  dz   |z   |j                  z   z  }|j                  |j                                y)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   sy     **,C	JJw
..
(
2
2
4Ra
8C;;$))c/C/$++=>DS\\^$r   c                     | j                   \  }}| j                  }t        | ||      }||||t        j                  |d      d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   sN    **LD&OOEeY-E"//tL r   c                     | j                         D ci c]$  \  }}||j                         j                         & c}}S c c}}w N)r=   r   clone)r    rA   vs      r
   
copy_statere      s1    +0;;=941aAquuw}}999s   )Ac              #      K   t        | j                               }| j                  |d       	 d | j                  |       y# | j                  |       w xY ww)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      sR      5++-.I	%.)i(i(s   -AA AAAc                 B     t        j                          fd       }|S )Nc                 0    ||f| _          | g|i | y rb   )r]   )selfr   r   inits      r
   __init__zcapture_init.<locals>.__init__   s!    "&T#D#F#r   )	functoolswraps)rl   rm   s   ` r
   capture_initrp      s%    __T$ $ Or   rb   )F)NT)__doc__
contextlibr   rn   rO   r*   rL   pathlibr   r$   	omegaconfr   dora.logr   r'   r   r   r6   rC   r/   rY   r`   re   rh   rp    r   r
   <module>rw      sm    &    	     A$B
%: ) )"r   