o
    9wi                     @   s   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d Zdd	d
ZdddZdddZdddZdd ZdddZdd Zedd Zdd ZdS ) z$
Utilities to save and load models.
    )contextmanagerN)Path)	OmegaConf)fatalc                  C   s,   zdd l } W d S  ty   td Y d S 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    r   J/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/demucs/states.py_check_diffq   s
   r
   c                 C   sr   d}|j r"t  ddl m} || |j|jd}|dur || |S |jr7t  ddl m} || |j|jd}|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   r   r   r	   get_quantizer    s"   


r   Fc                 C   s   t | tr| }n1t | ttfr1t  td | }t|d}W d   n1 s+w   Y  nt	d|  d|d }|d }|d }|rO||i |}n"t
|}t|D ]}	|	|jvritd	|	  ||	= qX||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   r   r   r	   
load_model2   s2   





r2   c                    sH   |du r|r	t jnd  fdd|   D }|S | }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                    s"   i | ]\}}||j jd  dqS )r   )devicedtype)datato).0kpr4   r   r	   
<dictcomp>Y   s   " zget_state.<locals>.<dictcomp>T__quantized)r#   half
state_dictitemsget_quantized_state)r   r   r=   r   r   r:   r	   	get_stateS   s   rA   c                 C   sT   | dr#|dur|| |d  |S t  ddlm} || | |S | | |S )zSet the state on a given model.r<   N	quantizedr   )restore_quantized_state)getrC   r
   r   load_state_dict)r   r   r   rC   r   r   r	   r+   `   s   


r+   c                 C   sZ   t  }t| | t|  dd }|j|j	d | |j
  }||  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)contentr/   bufr0   r   r   r	   save_with_checksumn   s
   rU   Tc                 C   s6   | j \}}| j}t| ||}||||tj|dddS )NT)resolve)r   r   r   r   training_args)_init_args_kwargs	__class__rA   r   to_container)r   rW   r   r=   r   r   r   r   r   r   r	   serialize_modely   s   
r[   c                 C   s   dd |   D S )Nc                 S   s   i | ]\}}||   qS r   )r   clone)r7   r8   vr   r   r	   r;      s    zcopy_state.<locals>.<dictcomp>)r?   )r   r   r   r	   
copy_state   s   r^   c              	   c   s@    t |  }| j|dd zdV  W | | dS | | 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)r-   N)r^   r>   rE   )r   r   	old_stater   r   r	   
swap_state   s   
r`   c                    s   t   fdd}|S )Nc                    s$   ||f| _  | g|R i | d S N)rX   )selfr   r   initr   r	   __init__   s   
zcapture_init.<locals>.__init__)	functoolswraps)rd   re   r   rc   r	   capture_init   s   rh   ra   )F)NT)__doc__
contextlibr   rf   rK   r&   rH   pathlibr   r    	omegaconfr   dora.logr   r#   r
   r   r2   rA   r+   rU   r[   r^   r`   rh   r   r   r   r	   <module>   s,   
	

!


