
    Vh                         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dS )z$
Utilities to save and load models.
    )contextmanagerN)Path)	OmegaConf)fatalc                  P    	 dd l } d S # t          $ r t          d           Y d S 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    S/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/demucs/states.py_check_diffqr      sa    A A A A @ 	A 	A 	A 	A 	A 	AAs    %%c                    d}|j         rDt                       ddl m}  || |j        |j                  }||                    |           n3|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z >''''''!MDMdoG G G	 %%i000	 >******$$DHt}> > >	    Fc                 Z   t          | t                    r| }nt          | t          t          f          rWt	          j                    5  t	          j        d           | }t          j        |d          }ddd           n# 1 swxY w Y   nt          d|  d          |d         }|d         }|d         }|r	 ||i |}nQt          j        |          }t          |          D ]%}	|	|j        vrt	          j        d	|	z              ||	= & ||i |}|d
         }
t          ||
           |S )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(( A!	Oc4[	1	1 A$&& 	. 	.!(+++"Dju--G	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.
 ?_???@@@GE6?DXF 't&v&&&&<< 	  	 C#.((>DEEE3Kt&v&&GEeULs   ,B  BBc                     |C|rt           j        ndfd|                                                                 D             }n|                                }d|d<   |S )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.Nc                 P    i | ]"\  }}||j                             d           #S )r   )devicedtype)datato).0kpr:   s      r
   
<dictcomp>zget_state.<locals>.<dictcomp>Y   s1    ```TQAFIIU%I88```r   T__quantized)r'   half
state_dictitemsget_quantized_state)r   r   rB   r    r:   s       @r
   	get_staterF   S   sn     ",

````UEUEUEWEWE]E]E_E_```--//#mLr   c                     |                     d          r@||                    | |d                    n6t                       ddlm}  || |           n|                     |           |S )zSet the state on a given model.rA   N	quantizedr   )restore_quantized_state)getrI   r   r   load_state_dict)r   r    r   rI   s       r
   r/   r/   `   s    yy % --eU;5GHHHHNNN555555##E51111e$$$Lr   c                 ^   t          j                    }t          j        | |           t	          j        |                                                                          dd         }|j        |j	        dz   |z   |j
        z   z  }|                    |                                           dS )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_checksumr\   n   s     *,,C	Jw
.
(
(
2
2
4
4RaR
8C;$)c/C/$+=>DS\\^^$$$$$r   Tc                 ~    | j         \  }}| j        }t          | ||          }||||t          j        |d          dS )NT)resolve)r   r   r   r    training_args)_init_args_kwargs	__class__rF   r   to_container)r   r_   r   rB   r   r   r   r    s           r
   serialize_modelrc   y   sT    *LD&OEeY--E"/tLLL  r   c                 >    d |                                  D             S )Nc                 b    i | ],\  }}||                                                                 -S  )r   clone)r=   r>   vs      r
   r@   zcopy_state.<locals>.<dictcomp>   s.    99941aAquuww}}999r   )rD   )r    s    r
   
copy_stateri      s    995;;==9999r   c              #      K   t          |                                           }|                     |d           	 dV  |                     |           dS # |                     |           w xY 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)ri   rC   rK   )r   r    	old_states      r
   
swap_staterl      s|       5++--..I	%...)i(((((i((((s   A A.c                 F     t          j                    fd            }|S )Nc                 2    ||f| _          | g|R i | d S N)r`   )selfr   r   inits      r
   __init__zcapture_init.<locals>.__init__   s5    "&T#D###F#####r   )	functoolswraps)rq   rr   s   ` r
   capture_initru      s8    _T$ $ $ $ $ Or   ro   )F)NT)__doc__
contextlibr   rs   rR   r*   rO   pathlibr   r$   	omegaconfr   dora.logr   r'   r   r   r6   rF   r/   r\   rc   ri   rl   ru   rf   r   r
   <module>r{      s[    & % % % % %       				                    A A A   $   B
 
 
 
   % % %   : : : ) ) )"    r   